|
| 1 | +package xaes256gcm_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "xaes256gcm" |
| 9 | + |
| 10 | + "golang.org/x/crypto/sha3" |
| 11 | +) |
| 12 | + |
| 13 | +func TestVectors(t *testing.T) { |
| 14 | + key := bytes.Repeat([]byte{0x01}, xaes256gcm.KeySize) |
| 15 | + nonce := []byte("ABCDEFGHIJKLMNOPQRSTUVWX") |
| 16 | + plaintext := []byte("XAES-256-GCM") |
| 17 | + c, err := xaes256gcm.New(key) |
| 18 | + if err != nil { |
| 19 | + t.Fatal(err) |
| 20 | + } |
| 21 | + ciphertext := c.Seal(nil, nonce, plaintext, nil) |
| 22 | + expected := "ce546ef63c9cc60765923609b33a9a1974e96e52daf2fcf7075e2271" |
| 23 | + if got := hex.EncodeToString(ciphertext); got != expected { |
| 24 | + t.Errorf("got: %s", got) |
| 25 | + } |
| 26 | + if decrypted, err := c.Open(nil, nonce, ciphertext, nil); err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } else if !bytes.Equal(plaintext, decrypted) { |
| 29 | + t.Errorf("plaintext and decrypted are not equal") |
| 30 | + } |
| 31 | + |
| 32 | + key = bytes.Repeat([]byte{0x03}, xaes256gcm.KeySize) |
| 33 | + aad := []byte("c2sp.org/XAES-256-GCM") |
| 34 | + c, err = xaes256gcm.New(key) |
| 35 | + if err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + ciphertext = c.Seal(nil, nonce, plaintext, aad) |
| 39 | + expected = "986ec1832593df5443a179437fd083bf3fdb41abd740a21f71eb769d" |
| 40 | + if got := hex.EncodeToString(ciphertext); got != expected { |
| 41 | + t.Errorf("got: %s", got) |
| 42 | + } |
| 43 | + if decrypted, err := c.Open(nil, nonce, ciphertext, aad); err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } else if !bytes.Equal(plaintext, decrypted) { |
| 46 | + t.Errorf("plaintext and decrypted are not equal") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestAccumulated(t *testing.T) { |
| 51 | + iterations := 10_000 |
| 52 | + expected := "e6b9edf2df6cec60c8cbd864e2211b597fb69a529160cd040d56c0c210081939" |
| 53 | + if !testing.Short() { |
| 54 | + iterations = 1_000_000 |
| 55 | + expected = "2163ae1445985a30b60585ee67daa55674df06901b890593e824b8a7c885ab15" |
| 56 | + } |
| 57 | + |
| 58 | + s, d := sha3.NewShake128(), sha3.NewShake128() |
| 59 | + for i := 0; i < iterations; i++ { |
| 60 | + key := make([]byte, xaes256gcm.KeySize) |
| 61 | + s.Read(key) |
| 62 | + nonce := make([]byte, xaes256gcm.NonceSize) |
| 63 | + s.Read(nonce) |
| 64 | + lenByte := make([]byte, 1) |
| 65 | + s.Read(lenByte) |
| 66 | + plaintext := make([]byte, int(lenByte[0])) |
| 67 | + s.Read(plaintext) |
| 68 | + s.Read(lenByte) |
| 69 | + aad := make([]byte, int(lenByte[0])) |
| 70 | + s.Read(aad) |
| 71 | + |
| 72 | + c, err := xaes256gcm.New(key) |
| 73 | + if err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + ciphertext := c.Seal(nil, nonce, plaintext, aad) |
| 77 | + decrypted, err := c.Open(nil, nonce, ciphertext, aad) |
| 78 | + if err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + if !bytes.Equal(plaintext, decrypted) { |
| 82 | + t.Errorf("plaintext and decrypted are not equal") |
| 83 | + } |
| 84 | + |
| 85 | + d.Write(ciphertext) |
| 86 | + } |
| 87 | + if got := hex.EncodeToString(d.Sum(nil)); got != expected { |
| 88 | + t.Errorf("got: %s", got) |
| 89 | + } |
| 90 | +} |
0 commit comments