1
0
mirror of https://github.com/golang/go synced 2024-11-23 16:20:04 -07:00

crypto/hmac: add tests for Size() and BlockSize()

R=golang-codereviews, dave
CC=golang-codereviews
https://golang.org/cl/41320044
This commit is contained in:
Shawn Smith 2013-12-29 20:56:05 +11:00 committed by Dave Cheney
parent d2fe44d568
commit 351caa4b91

View File

@ -15,10 +15,12 @@ import (
)
type hmacTest struct {
hash func() hash.Hash
key []byte
in []byte
out string
hash func() hash.Hash
key []byte
in []byte
out string
size int
blocksize int
}
var hmacTests = []hmacTest{
@ -38,6 +40,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample #1"),
"4f4ca3d5d68ba7cc0a1208c9c61e9c5da0403c0a",
sha1.Size,
sha1.BlockSize,
},
{
sha1.New,
@ -48,6 +52,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample #2"),
"0922d3405faa3d194f82a45830737d5cc6c75d24",
sha1.Size,
sha1.BlockSize,
},
{
sha1.New,
@ -68,6 +74,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample #3"),
"bcf41eab8bb2d802f3d05caf7cb092ecf8d1a3aa",
sha1.Size,
sha1.BlockSize,
},
// Test from Plan 9.
@ -76,6 +84,8 @@ var hmacTests = []hmacTest{
[]byte("Jefe"),
[]byte("what do ya want for nothing?"),
"750c783e6ab0b503eaa86e310a5db738",
md5.Size,
md5.BlockSize,
},
// Tests from RFC 4231
@ -88,12 +98,16 @@ var hmacTests = []hmacTest{
},
[]byte("Hi There"),
"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7",
sha256.Size,
sha256.BlockSize,
},
{
sha256.New,
[]byte("Jefe"),
[]byte("what do ya want for nothing?"),
"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
sha256.Size,
sha256.BlockSize,
},
{
sha256.New,
@ -112,6 +126,8 @@ var hmacTests = []hmacTest{
0xdd, 0xdd,
},
"773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe",
sha256.Size,
sha256.BlockSize,
},
{
sha256.New,
@ -131,6 +147,8 @@ var hmacTests = []hmacTest{
0xcd, 0xcd,
},
"82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b",
sha256.Size,
sha256.BlockSize,
},
{
sha256.New,
@ -155,6 +173,8 @@ var hmacTests = []hmacTest{
},
[]byte("Test Using Larger Than Block-Size Key - Hash Key First"),
"60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54",
sha256.Size,
sha256.BlockSize,
},
{
sha256.New,
@ -181,6 +201,8 @@ var hmacTests = []hmacTest{
"and a larger than block-size data. The key needs to " +
"be hashed before being used by the HMAC algorithm."),
"9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2",
sha256.Size,
sha256.BlockSize,
},
// Tests from http://csrc.nist.gov/groups/ST/toolkit/examples.html
@ -199,6 +221,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen=blocklen"),
"5fd596ee78d5553c8ff4e72d266dfd192366da29",
sha1.Size,
sha1.BlockSize,
},
{
sha1.New,
@ -209,6 +233,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen<blocklen"),
"4c99ff0cb1b31bd33f8431dbaf4d17fcd356a807",
sha1.Size,
sha1.BlockSize,
},
{
sha1.New,
@ -229,6 +255,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen=blocklen"),
"2d51b2f7750e410584662e38f133435f4c4fd42a",
sha1.Size,
sha1.BlockSize,
},
{
sha256.New224,
@ -244,6 +272,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen=blocklen"),
"c7405e3ae058e8cd30b08b4140248581ed174cb34e1224bcc1efc81b",
sha256.Size224,
sha256.BlockSize,
},
{
sha256.New224,
@ -255,6 +285,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen<blocklen"),
"e3d249a8cfb67ef8b7a169e9a0a599714a2cecba65999a51beb8fbbe",
sha256.Size224,
sha256.BlockSize,
},
{
sha256.New224,
@ -275,6 +307,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen=blocklen"),
"91c52509e5af8531601ae6230099d90bef88aaefb961f4080abc014d",
sha256.Size224,
sha256.BlockSize,
},
{
sha256.New,
@ -290,6 +324,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen=blocklen"),
"8bb9a1db9806f20df7f77b82138c7914d174d59e13dc4d0169c9057b133e1d62",
sha256.Size,
sha256.BlockSize,
},
{
sha256.New,
@ -301,6 +337,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen<blocklen"),
"a28cf43130ee696a98f14a37678b56bcfcbdd9e5cf69717fecf5480f0ebdf790",
sha256.Size,
sha256.BlockSize,
},
{
sha256.New,
@ -321,6 +359,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen=blocklen"),
"bdccb6c72ddeadb500ae768386cb38cc41c63dbb0878ddb9c7a38a431b78378d",
sha256.Size,
sha256.BlockSize,
},
{
sha512.New384,
@ -344,6 +384,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen=blocklen"),
"63c5daa5e651847ca897c95814ab830bededc7d25e83eef9195cd45857a37f448947858f5af50cc2b1b730ddf29671a9",
sha512.Size384,
sha512.BlockSize,
},
{
sha512.New384,
@ -357,6 +399,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen<blocklen"),
"6eb242bdbb582ca17bebfa481b1e23211464d2b7f8c20b9ff2201637b93646af5ae9ac316e98db45d9cae773675eeed0",
sha512.Size384,
sha512.BlockSize,
},
{
sha512.New384,
@ -389,6 +433,8 @@ var hmacTests = []hmacTest{
},
[]byte("Sample message for keylen=blocklen"),
"5b664436df69b0ca22551231a3f0a3d5b4f97991713cfa84bff4d0792eff96c27dccbbb6f79b65d548b40e8564cef594",
sha512.Size384,
sha512.BlockSize,
},
{
sha512.New,
@ -414,6 +460,8 @@ var hmacTests = []hmacTest{
"fc25e240658ca785b7a811a8d3f7b4ca" +
"48cfa26a8a366bf2cd1f836b05fcb024bd36853081811d6c" +
"ea4216ebad79da1cfcb95ea4586b8a0ce356596a55fb1347",
sha512.Size,
sha512.BlockSize,
},
{
sha512.New,
@ -431,6 +479,8 @@ var hmacTests = []hmacTest{
"fd44c18bda0bb0a6ce0e82b031bf2818" +
"f6539bd56ec00bdc10a8a2d730b3634de2545d639b0f2cf7" +
"10d0692c72a1896f1f211c2b922d1a96c392e07e7ea9fedc",
sha512.Size,
sha512.BlockSize,
},
{
sha512.New,
@ -465,12 +515,20 @@ var hmacTests = []hmacTest{
"d93ec8d2de1ad2a9957cb9b83f14e76a" +
"d6b5e0cce285079a127d3b14bccb7aa7286d4ac0d4ce6421" +
"5f2bc9e6870b33d97438be4aaa20cda5c5a912b48b8e27f3",
sha512.Size,
sha512.BlockSize,
},
}
func TestHMAC(t *testing.T) {
for i, tt := range hmacTests {
h := New(tt.hash, tt.key)
if s := h.Size(); s != tt.size {
t.Errorf("Size: got %v, want %v", s, tt.size)
}
if b := h.BlockSize(); b != tt.blocksize {
t.Errorf("BlockSize: got %v, want %v", b, tt.blocksize)
}
for j := 0; j < 2; j++ {
n, err := h.Write(tt.in)
if n != len(tt.in) || err != nil {