| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package cipher |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"crypto/aes" |
| 9 |
"encoding/hex" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
type testVector struct { |
| 14 |
msg, key, nonce, data string |
| 15 |
ciphertext string |
| 16 |
macSize int |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
var vectors = []testVector{ |
| 22 |
testVector{ |
| 23 |
msg: "", |
| 24 |
key: "233952DEE4D5ED5F9B9C6D6FF80FF478", |
| 25 |
nonce: "62EC67F9C3A4A407FCB2A8C49031A8B3", |
| 26 |
data: "6BFB914FD07EAE6B", |
| 27 |
ciphertext: "E037830E8389F27B025A2D6527E79D01", |
| 28 |
macSize: 16, |
| 29 |
}, |
| 30 |
testVector{ |
| 31 |
msg: "F7FB", |
| 32 |
key: "91945D3F4DCBEE0BF45EF52255F095A4", |
| 33 |
nonce: "BECAF043B0A23D843194BA972C66DEBD", |
| 34 |
data: "FA3BFD4806EB53FA", |
| 35 |
ciphertext: "19DD5C4C9331049D0BDAB0277408F67967E5", |
| 36 |
macSize: 16, |
| 37 |
}, |
| 38 |
testVector{ |
| 39 |
msg: "1A47CB4933", |
| 40 |
key: "01F74AD64077F2E704C0F60ADA3DD523", |
| 41 |
nonce: "70C3DB4F0D26368400A10ED05D2BFF5E", |
| 42 |
data: "234A3463C1264AC6", |
| 43 |
ciphertext: "D851D5BAE03A59F238A23E39199DC9266626C40F80", |
| 44 |
macSize: 16, |
| 45 |
}, |
| 46 |
testVector{ |
| 47 |
msg: "1A47CB4933", |
| 48 |
key: "01F74AD64077F2E704C0F60ADA3DD523", |
| 49 |
nonce: "70C3DB4F0D26368400A10ED05D2BFF5E", |
| 50 |
data: "234A3463C1264AC6", |
| 51 |
ciphertext: "D851D5BAE03A59F238A23E39199DC9266626C4", |
| 52 |
macSize: 14, |
| 53 |
}, |
| 54 |
testVector{ |
| 55 |
msg: "8B0A79306C9CE7ED99DAE4F87F8DD61636", |
| 56 |
key: "7C77D6E813BED5AC98BAA417477A2E7D", |
| 57 |
nonce: "1A8C98DCD73D38393B2BF1569DEEFC19", |
| 58 |
data: "65D2017990D62528", |
| 59 |
ciphertext: "02083E3979DA014812F59F11D52630DA30137327D10" + |
| 60 |
"649B0AA6E1C181DB617D7F2", |
| 61 |
macSize: 16, |
| 62 |
}, |
| 63 |
testVector{ |
| 64 |
msg: "8B0A79306C9CE7ED99DAE4F87F8DD61636", |
| 65 |
key: "7C77D6E813BED5AC98BAA417477A2E7D", |
| 66 |
nonce: "1A8C98DCD73D38393B2BF1569DEEFC19", |
| 67 |
data: "65D2017990D62528", |
| 68 |
ciphertext: "02083E3979DA014812F59F11D52630DA30137327D10" + |
| 69 |
"649B0AA6E1C181D", |
| 70 |
macSize: 12, |
| 71 |
}, |
| 72 |
} |
| 73 |
|
| 74 |
func TestVectors(t *testing.T) { |
| 75 |
for i, v := range vectors { |
| 76 |
msg, err := hex.DecodeString(v.msg) |
| 77 |
if err != nil { |
| 78 |
t.Fatalf("TestVector %d: Failed to decode hex msg: %s", i, err) |
| 79 |
} |
| 80 |
key, err := hex.DecodeString(v.key) |
| 81 |
if err != nil { |
| 82 |
t.Fatalf("TestVector %d: Failed to decode hex key: %s", i, err) |
| 83 |
} |
| 84 |
nonce, err := hex.DecodeString(v.nonce) |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("TestVector %d: Failed to decode hex nonce: %s", i, err) |
| 87 |
} |
| 88 |
data, err := hex.DecodeString(v.data) |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("TestVector %d: Failed to decode hex data: %s", i, err) |
| 91 |
} |
| 92 |
ciphertext, err := hex.DecodeString(v.ciphertext) |
| 93 |
if err != nil { |
| 94 |
t.Fatalf("TestVector %d: Failed to decode hex ciphertext: %s", i, err) |
| 95 |
} |
| 96 |
cAES, err := aes.NewCipher(key) |
| 97 |
if err != nil { |
| 98 |
t.Fatalf("TestVector %d: Failed to create AES instance: %s", i, err) |
| 99 |
} |
| 100 |
eax, err := NewEAX(cAES, v.macSize) |
| 101 |
if err != nil { |
| 102 |
t.Fatalf("TestVector %d: Failed to create EAX instance: %s", i, err) |
| 103 |
} |
| 104 |
|
| 105 |
buf := make([]byte, len(ciphertext)) |
| 106 |
buf = eax.Seal(buf, nonce, msg, data) |
| 107 |
|
| 108 |
if !bytes.Equal(buf, ciphertext) { |
| 109 |
t.Fatalf("TestVector %d Seal failed:\nFound : %s\nExpected: %s", i, hex.EncodeToString(buf), hex.EncodeToString(ciphertext)) |
| 110 |
} |
| 111 |
|
| 112 |
buf, err = eax.Open(buf, nonce, buf, data) |
| 113 |
|
| 114 |
if err != nil { |
| 115 |
t.Fatalf("TestVector %d: Open failed: %s", i, err) |
| 116 |
} |
| 117 |
if !bytes.Equal(buf, msg) { |
| 118 |
t.Fatalf("TestVector %d Open failed:\nFound : %s\nExpected: %s", i, hex.EncodeToString(buf), hex.EncodeToString(msg)) |
| 119 |
} |
| 120 |
} |
| 121 |
} |