80 lines
2.6 KiB
Go
80 lines
2.6 KiB
Go
package symm
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
// AES-XTS vectors from IEEE P1619/D16 Annex B (same set used by golang.org/x/crypto/xts tests).
|
|
var aesXTSStandardVectors = []struct {
|
|
key string
|
|
dataUnitIndex uint64
|
|
plaintext string
|
|
ciphertext string
|
|
}{
|
|
{
|
|
key: "0000000000000000000000000000000000000000000000000000000000000000",
|
|
dataUnitIndex: 0,
|
|
plaintext: "0000000000000000000000000000000000000000000000000000000000000000",
|
|
ciphertext: "917cf69ebd68b2ec9b9fe9a3eadda692cd43d2f59598ed858c02c2652fbf922e",
|
|
},
|
|
{
|
|
key: "1111111111111111111111111111111122222222222222222222222222222222",
|
|
dataUnitIndex: 0x3333333333,
|
|
plaintext: "4444444444444444444444444444444444444444444444444444444444444444",
|
|
ciphertext: "c454185e6a16936e39334038acef838bfb186fff7480adc4289382ecd6d394f0",
|
|
},
|
|
{
|
|
key: "fffefdfcfbfaf9f8f7f6f5f4f3f2f1f022222222222222222222222222222222",
|
|
dataUnitIndex: 0x3333333333,
|
|
plaintext: "4444444444444444444444444444444444444444444444444444444444444444",
|
|
ciphertext: "af85336b597afc1a900b2eb21ec949d292df4c047e0b21532186a5971a227a89",
|
|
},
|
|
}
|
|
|
|
func TestAesXTSStandardVectors(t *testing.T) {
|
|
for i, tc := range aesXTSStandardVectors {
|
|
master := mustHex(t, tc.key)
|
|
key1, key2, err := SplitAesXTSMasterKey(master)
|
|
if err != nil {
|
|
t.Fatalf("#%d split key failed: %v", i, err)
|
|
}
|
|
|
|
plain := mustHex(t, tc.plaintext)
|
|
wantCipher := mustHex(t, tc.ciphertext)
|
|
dataUnitSize := len(plain)
|
|
|
|
gotCipher, err := EncryptAesXTSAt(plain, key1, key2, dataUnitSize, tc.dataUnitIndex)
|
|
if err != nil {
|
|
t.Fatalf("#%d EncryptAesXTSAt failed: %v", i, err)
|
|
}
|
|
if !bytes.Equal(gotCipher, wantCipher) {
|
|
t.Fatalf("#%d ciphertext mismatch", i)
|
|
}
|
|
|
|
gotPlain, err := DecryptAesXTSAt(wantCipher, key1, key2, dataUnitSize, tc.dataUnitIndex)
|
|
if err != nil {
|
|
t.Fatalf("#%d DecryptAesXTSAt failed: %v", i, err)
|
|
}
|
|
if !bytes.Equal(gotPlain, plain) {
|
|
t.Fatalf("#%d plaintext mismatch", i)
|
|
}
|
|
|
|
encStream := &bytes.Buffer{}
|
|
if err := EncryptAesXTSStreamAt(encStream, bytes.NewReader(plain), key1, key2, dataUnitSize, tc.dataUnitIndex); err != nil {
|
|
t.Fatalf("#%d EncryptAesXTSStreamAt failed: %v", i, err)
|
|
}
|
|
if !bytes.Equal(encStream.Bytes(), wantCipher) {
|
|
t.Fatalf("#%d stream ciphertext mismatch", i)
|
|
}
|
|
|
|
decStream := &bytes.Buffer{}
|
|
if err := DecryptAesXTSStreamAt(decStream, bytes.NewReader(wantCipher), key1, key2, dataUnitSize, tc.dataUnitIndex); err != nil {
|
|
t.Fatalf("#%d DecryptAesXTSStreamAt failed: %v", i, err)
|
|
}
|
|
if !bytes.Equal(decStream.Bytes(), plain) {
|
|
t.Fatalf("#%d stream plaintext mismatch", i)
|
|
}
|
|
}
|
|
}
|