OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package block | 5 package block |
6 | 6 |
7 import ( | 7 import ( |
8 » "bytes"; | 8 » "bytes" |
9 » "crypto/aes"; | 9 » "crypto/aes" |
10 » "fmt"; | 10 » "fmt" |
11 » "io"; | 11 » "io" |
12 » "testing"; | 12 » "testing" |
13 ) | 13 ) |
14 | 14 |
15 // Test vectors from http://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf | 15 // Test vectors from http://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf |
16 | 16 |
17 type eaxAESTest struct { | 17 type eaxAESTest struct { |
18 » msg» []byte; | 18 » msg []byte |
19 » key» []byte; | 19 » key []byte |
20 » nonce» []byte; | 20 » nonce []byte |
21 » header» []byte; | 21 » header []byte |
22 » cipher» []byte; | 22 » cipher []byte |
23 } | 23 } |
24 | 24 |
25 var eaxAESTests = []eaxAESTest{ | 25 var eaxAESTests = []eaxAESTest{ |
26 eaxAESTest{ | 26 eaxAESTest{ |
27 []byte{}, | 27 []byte{}, |
28 []byte{0x23, 0x39, 0x52, 0xDE, 0xE4, 0xD5, 0xED, 0x5F, 0x9B, 0x9
C, 0x6D, 0x6F, 0xF8, 0x0F, 0xF4, 0x78}, | 28 []byte{0x23, 0x39, 0x52, 0xDE, 0xE4, 0xD5, 0xED, 0x5F, 0x9B, 0x9
C, 0x6D, 0x6F, 0xF8, 0x0F, 0xF4, 0x78}, |
29 []byte{0x62, 0xEC, 0x67, 0xF9, 0xC3, 0xA4, 0xA4, 0x07, 0xFC, 0xB
2, 0xA8, 0xC4, 0x90, 0x31, 0xA8, 0xB3}, | 29 []byte{0x62, 0xEC, 0x67, 0xF9, 0xC3, 0xA4, 0xA4, 0x07, 0xFC, 0xB
2, 0xA8, 0xC4, 0x90, 0x31, 0xA8, 0xB3}, |
30 []byte{0x6B, 0xFB, 0x91, 0x4F, 0xD0, 0x7E, 0xAE, 0x6B}, | 30 []byte{0x6B, 0xFB, 0x91, 0x4F, 0xD0, 0x7E, 0xAE, 0x6B}, |
31 []byte{0xE0, 0x37, 0x83, 0x0E, 0x83, 0x89, 0xF2, 0x7B, 0x02, 0x5
A, 0x2D, 0x65, 0x27, 0xE7, 0x9D, 0x01}, | 31 []byte{0xE0, 0x37, 0x83, 0x0E, 0x83, 0x89, 0xF2, 0x7B, 0x02, 0x5
A, 0x2D, 0x65, 0x27, 0xE7, 0x9D, 0x01}, |
32 }, | 32 }, |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 eaxAESTest{ | 89 eaxAESTest{ |
90 []byte{0xCA, 0x40, 0xD7, 0x44, 0x6E, 0x54, 0x5F, 0xFA, 0xED, 0x3
B, 0xD1, 0x2A, 0x74, 0x0A, 0x65, 0x9F, 0xFB, 0xBB, 0x3C, 0xEA, 0xB7}, | 90 []byte{0xCA, 0x40, 0xD7, 0x44, 0x6E, 0x54, 0x5F, 0xFA, 0xED, 0x3
B, 0xD1, 0x2A, 0x74, 0x0A, 0x65, 0x9F, 0xFB, 0xBB, 0x3C, 0xEA, 0xB7}, |
91 []byte{0x83, 0x95, 0xFC, 0xF1, 0xE9, 0x5B, 0xEB, 0xD6, 0x97, 0xB
D, 0x01, 0x0B, 0xC7, 0x66, 0xAA, 0xC3}, | 91 []byte{0x83, 0x95, 0xFC, 0xF1, 0xE9, 0x5B, 0xEB, 0xD6, 0x97, 0xB
D, 0x01, 0x0B, 0xC7, 0x66, 0xAA, 0xC3}, |
92 []byte{0x22, 0xE7, 0xAD, 0xD9, 0x3C, 0xFC, 0x63, 0x93, 0xC5, 0x7
E, 0xC0, 0xB3, 0xC1, 0x7D, 0x6B, 0x44}, | 92 []byte{0x22, 0xE7, 0xAD, 0xD9, 0x3C, 0xFC, 0x63, 0x93, 0xC5, 0x7
E, 0xC0, 0xB3, 0xC1, 0x7D, 0x6B, 0x44}, |
93 []byte{0x12, 0x67, 0x35, 0xFC, 0xC3, 0x20, 0xD2, 0x5A}, | 93 []byte{0x12, 0x67, 0x35, 0xFC, 0xC3, 0x20, 0xD2, 0x5A}, |
94 []byte{0xCB, 0x89, 0x20, 0xF8, 0x7A, 0x6C, 0x75, 0xCF, 0xF3, 0x9
6, 0x27, 0xB5, 0x6E, 0x3E, 0xD1, 0x97, 0xC5, 0x52, 0xD2, 0x95, 0xA7, 0xCF, 0xC4,
0x6A, 0xFC, 0x25, 0x3B, 0x46, 0x52, 0xB1, 0xAF, 0x37, 0x95, 0xB1, 0x24, 0xAB, 0
x6E}, | 94 []byte{0xCB, 0x89, 0x20, 0xF8, 0x7A, 0x6C, 0x75, 0xCF, 0xF3, 0x9
6, 0x27, 0xB5, 0x6E, 0x3E, 0xD1, 0x97, 0xC5, 0x52, 0xD2, 0x95, 0xA7, 0xCF, 0xC4,
0x6A, 0xFC, 0x25, 0x3B, 0x46, 0x52, 0xB1, 0xAF, 0x37, 0x95, 0xB1, 0x24, 0xAB, 0
x6E}, |
95 }, | 95 }, |
96 } | 96 } |
97 | 97 |
98 func TestEAXEncrypt_AES(t *testing.T) { | 98 func TestEAXEncrypt_AES(t *testing.T) { |
99 » b := new(bytes.Buffer); | 99 » b := new(bytes.Buffer) |
100 for i, tt := range eaxAESTests { | 100 for i, tt := range eaxAESTests { |
101 » » test := fmt.Sprintf("test %d", i); | 101 » » test := fmt.Sprintf("test %d", i) |
102 » » c, err := aes.NewCipher(tt.key); | 102 » » c, err := aes.NewCipher(tt.key) |
103 if err != nil { | 103 if err != nil { |
104 t.Fatalf("%s: NewCipher(%d bytes) = %s", test, len(tt.ke
y), err) | 104 t.Fatalf("%s: NewCipher(%d bytes) = %s", test, len(tt.ke
y), err) |
105 } | 105 } |
106 » » b.Reset(); | 106 » » b.Reset() |
107 » » enc := NewEAXEncrypter(c, tt.nonce, tt.header, 16, b); | 107 » » enc := NewEAXEncrypter(c, tt.nonce, tt.header, 16, b) |
108 » » n, err := io.Copy(enc, bytes.NewBuffer(tt.msg)); | 108 » » n, err := io.Copy(enc, bytes.NewBuffer(tt.msg)) |
109 if n != int64(len(tt.msg)) || err != nil { | 109 if n != int64(len(tt.msg)) || err != nil { |
110 t.Fatalf("%s: io.Copy into encrypter: %d, %s", test, n,
err) | 110 t.Fatalf("%s: io.Copy into encrypter: %d, %s", test, n,
err) |
111 } | 111 } |
112 » » err = enc.Close(); | 112 » » err = enc.Close() |
113 if err != nil { | 113 if err != nil { |
114 t.Fatalf("%s: enc.Close: %s", test, err) | 114 t.Fatalf("%s: enc.Close: %s", test, err) |
115 } | 115 } |
116 if d := b.Bytes(); !same(d, tt.cipher) { | 116 if d := b.Bytes(); !same(d, tt.cipher) { |
117 t.Fatalf("%s: got %x want %x", test, d, tt.cipher) | 117 t.Fatalf("%s: got %x want %x", test, d, tt.cipher) |
118 } | 118 } |
119 } | 119 } |
120 } | 120 } |
121 | 121 |
122 func TestEAXDecrypt_AES(t *testing.T) { | 122 func TestEAXDecrypt_AES(t *testing.T) { |
123 » b := new(bytes.Buffer); | 123 » b := new(bytes.Buffer) |
124 for i, tt := range eaxAESTests { | 124 for i, tt := range eaxAESTests { |
125 » » test := fmt.Sprintf("test %d", i); | 125 » » test := fmt.Sprintf("test %d", i) |
126 » » c, err := aes.NewCipher(tt.key); | 126 » » c, err := aes.NewCipher(tt.key) |
127 if err != nil { | 127 if err != nil { |
128 t.Fatalf("%s: NewCipher(%d bytes) = %s", test, len(tt.ke
y), err) | 128 t.Fatalf("%s: NewCipher(%d bytes) = %s", test, len(tt.ke
y), err) |
129 } | 129 } |
130 » » b.Reset(); | 130 » » b.Reset() |
131 » » dec := NewEAXDecrypter(c, tt.nonce, tt.header, 16, bytes.NewBuff
er(tt.cipher)); | 131 » » dec := NewEAXDecrypter(c, tt.nonce, tt.header, 16, bytes.NewBuff
er(tt.cipher)) |
132 » » n, err := io.Copy(b, dec); | 132 » » n, err := io.Copy(b, dec) |
133 if n != int64(len(tt.msg)) || err != nil { | 133 if n != int64(len(tt.msg)) || err != nil { |
134 t.Fatalf("%s: io.Copy into decrypter: %d, %s", test, n,
err) | 134 t.Fatalf("%s: io.Copy into decrypter: %d, %s", test, n,
err) |
135 } | 135 } |
136 if d := b.Bytes(); !same(d, tt.msg) { | 136 if d := b.Bytes(); !same(d, tt.msg) { |
137 t.Fatalf("%s: got %x want %x", test, d, tt.msg) | 137 t.Fatalf("%s: got %x want %x", test, d, tt.msg) |
138 } | 138 } |
139 } | 139 } |
140 } | 140 } |
OLD | NEW |