From 94c4e68d500bcdc225c6cc0c5a995003dcceab01 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 25 Sep 2016 11:25:34 -0400 Subject: [PATCH] Split the byte manipulation functions into a separate function (called byteops to avoid conflicting with the standard package bytes). --- bridge/bridge.go | 1 + bridge/initio.go | 24 +++++++++++++----------- bridge/jmicron.go | 18 ++++++++++-------- bridge/symwave.go | 13 +++++++------ bridge/bytes.go => byteops/byteops.go | 4 +--- 5 files changed, 32 insertions(+), 28 deletions(-) rename bridge/bytes.go => byteops/byteops.go (89%) diff --git a/bridge/bridge.go b/bridge/bridge.go index 942ab3d..a531807 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -38,6 +38,7 @@ func IdentifyKeySector(possibleKeySector []byte) Bridge { type IncompleteImplementationError string +// TODO unexport func IncompleteImplementation(format string, args ...interface{}) IncompleteImplementationError { return IncompleteImplementationError(fmt.Sprintf(format, args...)) } diff --git a/bridge/initio.go b/bridge/initio.go index f8769fe..b0976d8 100644 --- a/bridge/initio.go +++ b/bridge/initio.go @@ -6,6 +6,8 @@ import ( "crypto/cipher" "crypto/aes" "encoding/binary" + + "github.com/andlabs/reallymine/byteops" ) type Initio struct{} @@ -36,18 +38,18 @@ type InitioKeySector struct { func (Initio) DecryptKeySector(keySector []byte, kek []byte) (KeySector, error) { // copy to avoid clobbering - keySector = DupBytes(keySector) - kek = DupBytes(kek) + keySector = byteops.DupBytes(keySector) + kek = byteops.DupBytes(kek) - SwapHalves(kek) - Reverse(kek) + byteops.SwapHalves(kek) + byteops.Reverse(kek) kekcipher, err := aes.NewCipher(kek) if err != nil { return nil, err } for i := 0; i < len(keySector); i += 16 { block := keySector[i : i+16] - SwapLongs(block) + byteops.SwapLongs(block) kekcipher.Decrypt(block, block) // Don't swap back; it'll be correct as-is. } @@ -84,20 +86,20 @@ func (ks *InitioKeySector) DEK() (dek []byte, err error) { } // make a copy to avoid altering ks.d - dek = DupBytes(ks.d.Key[:]) - SwapLongs(dek) // undo the little-endian-ness - SwapHalves(dek) - Reverse(dek) + dek = byteops.DupBytes(ks.d.Key[:]) + byteops.SwapLongs(dek) // undo the little-endian-ness + byteops.SwapHalves(dek) + byteops.Reverse(dek) return dek, nil } func (Initio) Decrypt(c cipher.Block, b []byte) { for i := 0; i < len(b); i += 16 { block := b[i : i+16] - SwapLongs(block) + byteops.SwapLongs(block) c.Decrypt(block, block) // We DO need to swap again after this, though! - SwapLongs(block) + byteops.SwapLongs(block) } } diff --git a/bridge/jmicron.go b/bridge/jmicron.go index 16293f5..d1931c9 100644 --- a/bridge/jmicron.go +++ b/bridge/jmicron.go @@ -6,6 +6,8 @@ import ( "crypto/cipher" "crypto/aes" "encoding/binary" + + "github.com/andlabs/reallymine/byteops" ) type JMicron struct{} @@ -51,19 +53,19 @@ type JMicronKeySector struct { func (JMicron) DecryptKeySector(keySector []byte, kek []byte) (KeySector, error) { // copy these to avoid overwriting them - keySector = DupBytes(keySector) - kek = DupBytes(kek) + keySector = byteops.DupBytes(keySector) + kek = byteops.DupBytes(kek) - Reverse(kek) + byteops.Reverse(kek) kekcipher, err := aes.NewCipher(kek) if err != nil { return nil, err } for i := 0; i < len(keySector); i += 16 { block := keySector[i : i+16] - Reverse(block) + byteops.Reverse(block) kekcipher.Decrypt(block, block) - Reverse(block) + byteops.Reverse(block) } return &JMicronKeySector{ @@ -111,16 +113,16 @@ func (ks *JMicronKeySector) DEK() (dek []byte, err error) { dek = make([]byte, 32) copy(dek[:16], ks.d.Key3EE2[:]) copy(dek[16:], ks.d.Key3EF2[:]) - Reverse(dek) + byteops.Reverse(dek) return dek, nil } func (JMicron) Decrypt(c cipher.Block, b []byte) { for i := 0; i < len(b); i += 16 { block := b[i : i+16] - Reverse(block) + byteops.Reverse(block) c.Decrypt(block, block) - Reverse(block) + byteops.Reverse(block) } } diff --git a/bridge/symwave.go b/bridge/symwave.go index d3396ec..384121e 100644 --- a/bridge/symwave.go +++ b/bridge/symwave.go @@ -6,6 +6,7 @@ import ( "bytes" "encoding/binary" + "github.com/andlabs/reallymine/byteops" "github.com/mendsley/gojwe" ) @@ -55,7 +56,7 @@ var symwaveKEKWrappingKey = []byte{ func (Symwave) DecryptKeySector(keySector []byte, kek []byte) (KeySector, error) { return &SymwaveKeySector{ - raw: DupBytes(keySector), + raw: byteops.DupBytes(keySector), }, nil } @@ -73,30 +74,30 @@ func (ks *SymwaveKeySector) DEK() (dek []byte, err error) { // And again with the endianness stuff... wrapped := ks.d.WrappedKEK[:] - SwapLongs(wrapped) + byteops.SwapLongs(wrapped) kek, err := gojwe.AesKeyUnwrap(symwaveKEKWrappingKey, wrapped) if err != nil { return nil, err } wrapped = ks.d.WrappedDEK1[:] - SwapLongs(wrapped) + byteops.SwapLongs(wrapped) dek1, err := gojwe.AesKeyUnwrap(kek, wrapped) if err != nil { return nil, err } wrapped = ks.d.WrappedDEK2[:] - SwapLongs(wrapped) + byteops.SwapLongs(wrapped) dek2, err := gojwe.AesKeyUnwrap(kek, wrapped) if err != nil { return nil, err } - dek = DupBytes(dek1) + dek = byteops.DupBytes(dek1) _ = dek2 // doesn't seem to be used // And finally we just need one last endian correction... - SwapLongs(dek) + byteops.SwapLongs(dek) return dek, nil } diff --git a/bridge/bytes.go b/byteops/byteops.go similarity index 89% rename from bridge/bytes.go rename to byteops/byteops.go index b402e92..c2dcda9 100644 --- a/bridge/bytes.go +++ b/byteops/byteops.go @@ -1,7 +1,5 @@ // 21 october 2015 -package bridge - -// TODO either unexport these or split into a separate package +package byteops func Reverse(b []byte) { if len(b)%2 == 1 {