Permalink
Browse files

Add 224-bit version.

  • Loading branch information...
1 parent 5ee928c commit f8b4022f6fb22f1cccf7ed95961b0dba2d6b9844 @dchest committed Jun 25, 2011
Showing with 91 additions and 37 deletions.
  1. +52 −25 blake256.go
  2. +39 −12 blake256_test.go
View
@@ -10,19 +10,17 @@ import (
"hash"
)
-// The size of the checksum in bytes.
-const Size = 32
-
// The block size of the hash algorithm in bytes.
const BlockSize = 64
type digest struct {
- h [8]uint32
- salt [4]uint32
- t [2]uint32
- nullt bool
- buf [BlockSize]uint8
- buflen int // buffer length in bits
+ h [8]uint32
+ salt [4]uint32
+ t [2]uint32
+ nullt bool
+ buf [BlockSize]uint8
+ buflen int // buffer length in bits
+ hashSize int // hash output size in bits
}
var sigma = [16][16]uint8{
@@ -161,14 +159,25 @@ func (d *digest) _Block(p []uint8) {
}
func (d *digest) Reset() {
- d.h[0] = 0x6A09E667
- d.h[1] = 0xBB67AE85
- d.h[2] = 0x3C6EF372
- d.h[3] = 0xA54FF53A
- d.h[4] = 0x510E527F
- d.h[5] = 0x9B05688C
- d.h[6] = 0x1F83D9AB
- d.h[7] = 0x5BE0CD19
+ if d.hashSize == 224 {
+ d.h[0] = 0xC1059ED8
+ d.h[1] = 0x367CD507
+ d.h[2] = 0x3070DD17
+ d.h[3] = 0xF70E5939
+ d.h[4] = 0xFFC00B31
+ d.h[5] = 0x68581511
+ d.h[6] = 0x64F98FA7
+ d.h[7] = 0xBEFA4FA4
+ } else {
+ d.h[0] = 0x6A09E667
+ d.h[1] = 0xBB67AE85
+ d.h[2] = 0x3C6EF372
+ d.h[3] = 0xA54FF53A
+ d.h[4] = 0x510E527F
+ d.h[5] = 0x9B05688C
+ d.h[6] = 0x1F83D9AB
+ d.h[7] = 0x5BE0CD19
+ }
d.t[0] = 0
d.t[1] = 0
d.nullt = false
@@ -179,7 +188,7 @@ func (d *digest) Reset() {
d.buflen = 0
}
-func (d *digest) Size() int { return Size }
+func (d *digest) Size() int { return d.hashSize >> 3 }
// update updates the internal state of digest with the given data of
// datalen in bits (not bytes!).
@@ -248,7 +257,11 @@ func (d0 *digest) Sum() []byte {
if d.buflen == 440 { // one padding byte
d.t[0] -= 8
- d.update([]byte{0x81}, 8)
+ if d.hashSize == 224 {
+ d.update([]byte{0x80}, 8)
+ } else {
+ d.update([]byte{0x81}, 8)
+ }
} else {
if d.buflen < 440 { // enought space to fill the block
if d.buflen == 0 {
@@ -263,15 +276,19 @@ func (d0 *digest) Sum() []byte {
d.update(padding[1:], 440)
d.nullt = true
}
- d.update([]byte{0x01}, 8)
+ if d.hashSize == 224 {
+ d.update([]byte{0x00}, 8)
+ } else {
+ d.update([]byte{0x01}, 8)
+ }
d.t[0] -= 8
}
d.t[0] -= 64
d.update(msglen, 64)
- out := make([]byte, 32)
+ out := make([]byte, d.Size())
j := 0
- for _, s := range d.h {
+ for _, s := range d.h[:d.hashSize>>5] {
out[j+0] = byte(s >> 24)
out[j+1] = byte(s >> 16)
out[j+2] = byte(s >> 8)
@@ -281,9 +298,19 @@ func (d0 *digest) Sum() []byte {
return out
}
+func newHash(bitSize int) (d *digest) {
+ d = new(digest)
+ d.hashSize = bitSize
+ d.Reset()
+ return
+}
+
// New returns a new hash.Hash computing the BLAKE-256 checksum.
func New() hash.Hash {
- d := new(digest)
- d.Reset()
- return d
+ return newHash(256)
+}
+
+// New224 returns a new hash.Hash computing the BLAKE-224 checksum.
+func New224() hash.Hash {
+ return newHash(224)
}
View
@@ -8,7 +8,7 @@ import (
"testing"
)
-func TestBlake256(t *testing.T) {
+func Test256C(t *testing.T) {
// Test as in C program.
var hashes = [][]byte{
{
@@ -50,32 +50,59 @@ func TestBlake256(t *testing.T) {
if !bytes.Equal(hashes[1], sum) {
t.Errorf("1(2): expected %X, got %X", hashes[1], sum)
}
+}
+
+type blakeVector struct {
+ out, in string
+}
- // Other test vectors.
- vectors := []struct{ out, in string }{
- {"7576698ee9cad30173080678e5965916adbb11cb5245d386bf1ffda1cb26c9d7",
+var vectors256 = []blakeVector{
+ {"7576698ee9cad30173080678e5965916adbb11cb5245d386bf1ffda1cb26c9d7",
"The quick brown fox jumps over the lazy dog"},
- {"07663e00cf96fbc136cf7b1ee099c95346ba3920893d18cc8851f22ee2e36aa6",
+ {"07663e00cf96fbc136cf7b1ee099c95346ba3920893d18cc8851f22ee2e36aa6",
"BLAKE"},
- {"716f6e863f744b9ac22c97ec7b76ea5f5908bc5b2f67c61510bfc4751384ea7a",
+ {"716f6e863f744b9ac22c97ec7b76ea5f5908bc5b2f67c61510bfc4751384ea7a",
""},
- {"18a393b4e62b1887a2edf79a5c5a5464daf5bbb976f4007bea16a73e4c1e198e",
+ {"18a393b4e62b1887a2edf79a5c5a5464daf5bbb976f4007bea16a73e4c1e198e",
"'BLAKE wins SHA-3! Hooray!!!' (I have time machine)"},
- {"fd7282ecc105ef201bb94663fc413db1b7696414682090015f17e309b835f1c2",
+ {"fd7282ecc105ef201bb94663fc413db1b7696414682090015f17e309b835f1c2",
"Go"},
- {"1e75db2a709081f853c2229b65fd1558540aa5e7bd17b04b9a4b31989effa711",
+ {"1e75db2a709081f853c2229b65fd1558540aa5e7bd17b04b9a4b31989effa711",
"HELP! I'm trapped in hash!"},
- }
+}
+
+var vectors224 = []blakeVector{
+ {"c8e92d7088ef87c1530aee2ad44dc720cc10589cc2ec58f95a15e51b",
+ "The quick brown fox jumps over the lazy dog"},
+ {"cfb6848add73e1cb47994c4765df33b8f973702705a30a71fe4747a3",
+ "BLAKE"},
+ {"7dc5313b1c04512a174bd6503b89607aecbee0903d40a8a569c94eed",
+ ""},
+ {"dde9e442003c24495db607b17e07ec1f67396cc1907642a09a96594e",
+ "Go"},
+ {"9f655b0a92d4155754fa35e055ce7c5e18eb56347081ea1e5158e751",
+ "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo"},
+}
+
+func testVectors(t *testing.T, hashfunc func() hash.Hash, vectors []blakeVector) {
for i, v := range vectors {
- h := New()
+ h := hashfunc()
h.Write([]byte(v.in))
res := fmt.Sprintf("%x", h.Sum())
if res != v.out {
- t.Errorf("[v] %d: expected %q, got %q", i, v.out, res)
+ t.Errorf("%d: expected %q, got %q", i, v.out, res)
}
}
}
+func Test256(t *testing.T) {
+ testVectors(t, New, vectors256)
+}
+
+func Test224(t *testing.T) {
+ testVectors(t, New224, vectors224)
+}
+
var longData, shortData []byte
func init() {

0 comments on commit f8b4022

Please sign in to comment.