From 6d0e73cf26f771257995648e0fce401d0428e3db Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 14 Apr 2017 15:43:55 +0200 Subject: Pass tests --- bhash/bhash.go | 54 +++++++++++++++++++++-------------------------------- bhash/bhash_test.go | 24 ++++++++++-------------- 2 files changed, 31 insertions(+), 47 deletions(-) (limited to 'bhash') diff --git a/bhash/bhash.go b/bhash/bhash.go index 422ab76..2f1b626 100644 --- a/bhash/bhash.go +++ b/bhash/bhash.go @@ -3,7 +3,7 @@ package bhash import ( "bytes" "crypto/sha512" - "log" + "encoding/binary" "golang.org/x/crypto/blowfish" ) @@ -24,17 +24,17 @@ func Hash(pass, salt []byte) []byte { } // encryption buf := new(bytes.Buffer) - bSz := c.BlockSize() - for n := 0; n < len(magic)/bSz; n++ { - b := make([]byte, bSz) - copy(b, magic[n*bSz:(n+1)*bSz]) + blockSize := c.BlockSize() + for n := 0; n < len(magic)/blockSize; n++ { + b := make([]byte, blockSize) + copy(b, magic[n*blockSize:(n+1)*blockSize]) for i := 0; i < rounds; i++ { c.Encrypt(b, b) } - // copy out - b[0], b[1], b[2], b[3] = b[3], b[2], b[1], b[0] - b[4], b[5], b[6], b[7] = b[7], b[6], b[5], b[4] - buf.Write(b) + // swap bytes and copy out + var u [2]uint32 + binary.Read(bytes.NewReader(b), binary.BigEndian, &u) + binary.Write(buf, binary.LittleEndian, u) } return buf.Bytes() } @@ -45,41 +45,29 @@ func Pbkdf(pass, salt []byte, iter, keyLen int) []byte { h.Write(pass) sha2pass := h.Sum(nil) - out := make([]byte, 4*blowfish.BlockSize) // XXX + hashLen := 4 * blowfish.BlockSize + numBlocks := (keyLen + hashLen - 1) / hashLen - for count := 1; keyLen > 0; count++ { - countSalt := []byte{ - byte(count >> 24), - byte(count >> 16), - byte(count >> 8), - byte(count), - } + out := make([]byte, hashLen) + buf := new(bytes.Buffer) + for block := 1; block <= numBlocks; block++ { // first round, salt is salt h.Reset() h.Write(salt) - h.Write(countSalt) - sha2salt := h.Sum(nil) - tmp := Hash(sha2pass, sha2salt) + binary.Write(h, binary.BigEndian, uint32(block)) + tmp := Hash(sha2pass, h.Sum(nil)) copy(out, tmp) - log.Println(out) for i := 1; i < iter; i++ { h.Reset() h.Write(tmp) - sha2salt = h.Sum(nil) - tmp = Hash(sha2pass, sha2salt) - for i := range out { - out[i] ^= tmp[i] + tmp = Hash(sha2pass, h.Sum(nil)) + for x := range tmp { + out[x] ^= tmp[x] } - log.Println(out) } - - // pbkdf2 deviation: output the key material non-linearly - // TODO - - keyLen-- // XXX + buf.Write(out) } - - return nil + return buf.Bytes()[:keyLen] } diff --git a/bhash/bhash_test.go b/bhash/bhash_test.go index e3da3ef..492511f 100644 --- a/bhash/bhash_test.go +++ b/bhash/bhash_test.go @@ -6,19 +6,6 @@ import ( ) func TestHash(t *testing.T) { - golden := []byte{ - 0xa9, 0xd1, 0x46, 0x93, 0x6d, 0xc1, 0xe6, 0x22, - 0x87, 0xe0, 0xbd, 0x84, 0xe3, 0x0b, 0x4a, 0x0d, - 0x34, 0xd2, 0x08, 0x7d, 0x57, 0xea, 0x25, 0x75, - 0xa7, 0x3d, 0x5f, 0x42, 0xd5, 0x73, 0x41, 0x4a, - } - res := Hash([]byte("pass"), []byte("salt")) - if !bytes.Equal(res, golden) { - t.Errorf("got %x, want %x", res, golden) - } -} - -func TestHashZero(t *testing.T) { golden := []byte{ 0x46, 0x02, 0x86, 0xe9, 0x72, 0xfa, 0x83, 0x3f, 0x8b, 0x12, 0x83, 0xad, 0x8f, 0xa9, 0x19, 0xfa, @@ -33,6 +20,15 @@ func TestHashZero(t *testing.T) { } func TestPbkdf(t *testing.T) { + golden := []byte{ + 0xbf, 0x96, 0xd3, 0x86, 0x0f, 0x27, 0xda, 0x09, + 0x45, 0x52, 0x3f, 0xa1, 0x6b, 0x4c, 0xb0, 0xcd, + 0xbb, 0xbd, 0x74, 0x14, 0x83, 0x59, 0x66, 0xbc, + 0x21, 0x32, 0x6f, 0x1a, 0x18, 0xb8, 0x84, 0x53, + } zero := make([]byte, 32) - Pbkdf(zero, zero, 2, 2) + res := Pbkdf(zero, zero, 42, 32) + if !bytes.Equal(res, golden) { + t.Errorf("got %x, want %x", res, golden) + } } -- cgit v1.2.3