Skip to content

Commit

Permalink
Split the byte manipulation functions into a separate function (calle…
Browse files Browse the repository at this point in the history
…d byteops to avoid conflicting with the standard package bytes).
  • Loading branch information
andlabs committed Sep 25, 2016
1 parent 545b83e commit 94c4e68
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
1 change: 1 addition & 0 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...))
}
Expand Down
24 changes: 13 additions & 11 deletions bridge/initio.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/cipher"
"crypto/aes"
"encoding/binary"

"github.com/andlabs/reallymine/byteops"
)

type Initio struct{}
Expand Down Expand Up @@ -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.
}
Expand Down Expand Up @@ -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)
}
}

Expand Down
18 changes: 10 additions & 8 deletions bridge/jmicron.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/cipher"
"crypto/aes"
"encoding/binary"

"github.com/andlabs/reallymine/byteops"
)

type JMicron struct{}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
}

Expand Down
13 changes: 7 additions & 6 deletions bridge/symwave.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"encoding/binary"

"github.com/andlabs/reallymine/byteops"
"github.com/mendsley/gojwe"
)

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions bridge/bytes.go → byteops/byteops.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 94c4e68

Please sign in to comment.