| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package cmac |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"crypto/aes" |
| 9 |
"encoding/hex" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
type dummyCipher int |
| 16 |
|
| 17 |
func (c dummyCipher) BlockSize() int { return int(c) } |
| 18 |
|
| 19 |
func (c dummyCipher) Encrypt(dst, src []byte) { copy(dst, src) } |
| 20 |
|
| 21 |
func (c dummyCipher) Decrypt(dst, src []byte) { copy(dst, src) } |
| 22 |
|
| 23 |
func TestNew(t *testing.T) { |
| 24 |
_, err := New(nil) |
| 25 |
if err == nil { |
| 26 |
t.Fatal("CMac allowed nil for block cipher") |
| 27 |
} |
| 28 |
|
| 29 |
var ciphers = [5]dummyCipher{8, 16, 32, 64, 128} |
| 30 |
for _, c := range ciphers { |
| 31 |
_, err := New(c) |
| 32 |
if err != nil { |
| 33 |
t.Fatalf("BlockSize: %d, Failed to create CMac instance: %s", int(c), err) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
_, err = New(dummyCipher(20)) |
| 38 |
if err == nil { |
| 39 |
t.Fatalf("CMac allowed invalid block size: %d", 20) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestBlockSize(t *testing.T) { |
| 44 |
c, err := aes.NewCipher(make([]byte, 16)) |
| 45 |
if err != nil { |
| 46 |
t.Fatalf("Could not create AES instance: %s", err) |
| 47 |
} |
| 48 |
h, err := New(c) |
| 49 |
if err != nil { |
| 50 |
t.Fatalf("Could not create CMac instance: %s", err) |
| 51 |
} |
| 52 |
if bs := h.BlockSize(); bs != c.BlockSize() { |
| 53 |
t.Fatalf("BlockSize() returned: %d - but expected: %d", bs, c.BlockSize()) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestSize(t *testing.T) { |
| 58 |
c, err := aes.NewCipher(make([]byte, 16)) |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("Could not create AES instance: %s", err) |
| 61 |
} |
| 62 |
h, err := New(c) |
| 63 |
if err != nil { |
| 64 |
t.Fatalf("Could not create CMac instance: %s", err) |
| 65 |
} |
| 66 |
if bs := h.Size(); bs != c.BlockSize() { |
| 67 |
t.Fatalf("Size() returned: %d - but expected: %d", bs, c.BlockSize()) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func TestReset(t *testing.T) { |
| 72 |
cipher, err := aes.NewCipher(make([]byte, 16)) |
| 73 |
if err != nil { |
| 74 |
t.Fatalf("Could not create AES instance: %s", err) |
| 75 |
} |
| 76 |
h, err := New(cipher) |
| 77 |
if err != nil { |
| 78 |
t.Fatalf("Failed to use CMac with the specified cipher") |
| 79 |
} |
| 80 |
c, ok := h.(*macFunc) |
| 81 |
if !ok { |
| 82 |
t.Fatal("Impossible situation: New returns no CMac struct") |
| 83 |
} |
| 84 |
orig := *c |
| 85 |
|
| 86 |
c.Write(make([]byte, c.BlockSize()+1)) |
| 87 |
c.Reset() |
| 88 |
|
| 89 |
if !bytes.Equal(c.buf, orig.buf) { |
| 90 |
t.Fatalf("Reseted buf field: %d - but expected: %d", c.buf, orig.buf) |
| 91 |
} |
| 92 |
if !bytes.Equal(c.k0, orig.k0) { |
| 93 |
t.Fatalf("Reseted k0 field: %d - but expected: %d", c.k0, orig.k0) |
| 94 |
} |
| 95 |
if !bytes.Equal(c.k1, orig.k1) { |
| 96 |
t.Fatalf("Reseted k1 field: %d - but expected: %d", c.k1, orig.k1) |
| 97 |
} |
| 98 |
if c.off != orig.off { |
| 99 |
t.Fatalf("Reseted off field: %d - but expected: %d", c.off, orig.off) |
| 100 |
} |
| 101 |
if c.cipher != orig.cipher { |
| 102 |
t.Fatalf("Reseted cipher field: %v - but expected: %v", c.cipher, orig.cipher) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func TestWrite(t *testing.T) { |
| 107 |
c, err := aes.NewCipher(make([]byte, 16)) |
| 108 |
if err != nil { |
| 109 |
t.Fatalf("Could not create AES instance: %s", err) |
| 110 |
} |
| 111 |
h, err := New(c) |
| 112 |
if err != nil { |
| 113 |
t.Fatalf("Failed to create CMac instance: %s", err) |
| 114 |
} |
| 115 |
|
| 116 |
var msg1 []byte |
| 117 |
msg0 := make([]byte, 64) |
| 118 |
for i := range msg0 { |
| 119 |
h.Write(msg0[:i]) |
| 120 |
msg1 = append(msg1, msg0[:i]...) |
| 121 |
} |
| 122 |
|
| 123 |
tag0 := h.Sum(nil) |
| 124 |
tag1, err := Sum(msg1, c) |
| 125 |
if err != nil { |
| 126 |
t.Fatalf("Failed to compute CMac tag: %s", err) |
| 127 |
} |
| 128 |
|
| 129 |
if !bytes.Equal(tag0, tag1) { |
| 130 |
t.Fatalf("Sum differ from cmac.Sum\n Sum: %s \n cmac.Sum: %s", hex.EncodeToString(tag0), hex.EncodeToString(tag1)) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestSum(t *testing.T) { |
| 135 |
c, err := aes.NewCipher(make([]byte, 16)) |
| 136 |
if err != nil { |
| 137 |
t.Fatalf("Could not create AES instance: %s", err) |
| 138 |
} |
| 139 |
|
| 140 |
msg := make([]byte, 64) |
| 141 |
for i := range msg { |
| 142 |
h, err := New(c) |
| 143 |
if err != nil { |
| 144 |
t.Fatalf("Iteration %d: Failed to create CMac instance: %s", i, err) |
| 145 |
} |
| 146 |
|
| 147 |
h.Write(msg[:i]) |
| 148 |
tag0 := h.Sum(nil) |
| 149 |
|
| 150 |
tag1, err := Sum(msg[:i], c) |
| 151 |
if err != nil { |
| 152 |
t.Fatalf("Iteration %d: Failed to compute CMac tag: %s", i, err) |
| 153 |
} |
| 154 |
|
| 155 |
if !bytes.Equal(tag0, tag1) { |
| 156 |
t.Fatalf("Iteration %d: Sum differ from cmac.Sum\n Sum: %s \n cmac.Sum %s", i, hex.EncodeToString(tag0), hex.EncodeToString(tag1)) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
_, err = Sum(nil, dummyCipher(20)) |
| 161 |
if err == nil { |
| 162 |
t.Fatalf("cmac.Sum allowed invalid block size: %d", 20) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestVerify(t *testing.T) { |
| 167 |
var mac [16]byte |
| 168 |
mac[0] = 128 |
| 169 |
|
| 170 |
if Verify(mac[:], nil, dummyCipher(20)) { |
| 171 |
t.Fatalf("cmac.Verify allowed invalid block size: %d", 20) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
func BenchmarkWrite_16B(b *testing.B) { benchmarkWrite(b, 16) } |
| 178 |
|
| 179 |
func BenchmarkWrite_1K(b *testing.B) { benchmarkWrite(b, 1024) } |
| 180 |
|
| 181 |
func BenchmarkWrite_64K(b *testing.B) { benchmarkWrite(b, 64*1024) } |
| 182 |
|
| 183 |
func BenchmarkSum_16B(b *testing.B) { benchmarkSum(b, 16) } |
| 184 |
|
| 185 |
func BenchmarkSum_1K(b *testing.B) { benchmarkSum(b, 1024) } |
| 186 |
|
| 187 |
func BenchmarkSum_64K(b *testing.B) { benchmarkSum(b, 64*1024) } |
| 188 |
|
| 189 |
func benchmarkWrite(b *testing.B, nBytes int) { |
| 190 |
c, err := aes.NewCipher(make([]byte, 16)) |
| 191 |
if err != nil { |
| 192 |
b.Fatalf("Failed to create AES instance: %s", err) |
| 193 |
} |
| 194 |
h, err := New(c) |
| 195 |
if err != nil { |
| 196 |
b.Fatalf("Failed to create CMac instance: %s", err) |
| 197 |
} |
| 198 |
|
| 199 |
buf := make([]byte, nBytes) |
| 200 |
b.SetBytes(int64(nBytes)) |
| 201 |
for i := 0; i < b.N; i++ { |
| 202 |
h.Write(buf) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func benchmarkSum(b *testing.B, nBytes int) { |
| 207 |
c, err := aes.NewCipher(make([]byte, 16)) |
| 208 |
if err != nil { |
| 209 |
b.Fatalf("Failed to create AES instance: %s", err) |
| 210 |
} |
| 211 |
|
| 212 |
buf := make([]byte, nBytes) |
| 213 |
b.SetBytes(int64(nBytes)) |
| 214 |
for i := 0; i < b.N; i++ { |
| 215 |
Sum(buf, c) |
| 216 |
} |
| 217 |
} |