| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package serpent |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"encoding/hex" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func fromHex(s string) []byte { |
| 13 |
b, err := hex.DecodeString(s) |
| 14 |
if err != nil { |
| 15 |
panic(err) |
| 16 |
} |
| 17 |
return b |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
var vectors = []struct { |
| 22 |
key, plaintext, ciphertext string |
| 23 |
}{ |
| 24 |
|
| 25 |
|
| 26 |
{ |
| 27 |
key: "80000000000000000000000000000000", |
| 28 |
plaintext: "00000000000000000000000000000000", |
| 29 |
ciphertext: "264E5481EFF42A4606ABDA06C0BFDA3D", |
| 30 |
}, |
| 31 |
{ |
| 32 |
key: "40000000000000000000000000000000", |
| 33 |
plaintext: "00000000000000000000000000000000", |
| 34 |
ciphertext: "4A231B3BC727993407AC6EC8350E8524", |
| 35 |
}, |
| 36 |
|
| 37 |
|
| 38 |
{ |
| 39 |
key: "800000000000000000000000000000000000000000000000", |
| 40 |
plaintext: "00000000000000000000000000000000", |
| 41 |
ciphertext: "9E274EAD9B737BB21EFCFCA548602689", |
| 42 |
}, |
| 43 |
{ |
| 44 |
key: "100000000000000000000000000000000000000000000000", |
| 45 |
plaintext: "00000000000000000000000000000000", |
| 46 |
ciphertext: "BEC1E37824CF721E5D87F6CB4EBFB9BE", |
| 47 |
}, |
| 48 |
|
| 49 |
|
| 50 |
{ |
| 51 |
key: "0101010101010101010101010101010101010101010101010101010101010101", |
| 52 |
plaintext: "01010101010101010101010101010101", |
| 53 |
ciphertext: "EC9723B15B2A6489F84C4524FFFC2748", |
| 54 |
}, |
| 55 |
{ |
| 56 |
key: "0202020202020202020202020202020202020202020202020202020202020202", |
| 57 |
plaintext: "02020202020202020202020202020202", |
| 58 |
ciphertext: "1187F485538514476184E567DA0421C7", |
| 59 |
}, |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func TestVectors(t *testing.T) { |
| 64 |
for i, v := range vectors { |
| 65 |
key := fromHex(v.key) |
| 66 |
plaintext := fromHex(v.plaintext) |
| 67 |
ciphertext := fromHex(v.ciphertext) |
| 68 |
c, err := NewCipher(key) |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("Test vector %d: Failed to create cipher instance: %s", i, err) |
| 71 |
} |
| 72 |
|
| 73 |
buf := make([]byte, BlockSize) |
| 74 |
|
| 75 |
c.Encrypt(buf, plaintext) |
| 76 |
if !bytes.Equal(ciphertext, buf) { |
| 77 |
t.Fatalf("Test vector %d:\nEncryption failed\nFound: %s\nExpected: %s", i, hex.EncodeToString(buf), hex.EncodeToString(ciphertext)) |
| 78 |
} |
| 79 |
c.Decrypt(buf, buf) |
| 80 |
if !bytes.Equal(plaintext, buf) { |
| 81 |
t.Fatalf("Test vector %d:\nDecryption failed\nFound: %s\nExpected: %s", i, hex.EncodeToString(buf), hex.EncodeToString(plaintext)) |
| 82 |
} |
| 83 |
} |
| 84 |
} |