| 22 | ) |
| 23 | |
| 24 | func TestEncryptDecryptGCM(t *testing.T) { |
| 25 | randomKey := &[32]byte{} |
| 26 | _, err := io.ReadFull(rand.Reader, randomKey[:]) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | |
| 31 | gcmTests := []struct { |
| 32 | plaintext []byte |
| 33 | key *[32]byte |
| 34 | }{ |
| 35 | { |
| 36 | plaintext: []byte("Hello, world!"), |
| 37 | key: randomKey, |
| 38 | }, |
| 39 | } |
| 40 | |
| 41 | for _, tt := range gcmTests { |
| 42 | ciphertext, err := Encrypt(tt.plaintext, tt.key) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | |
| 47 | plaintext, err := Decrypt(ciphertext, tt.key) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | |
| 52 | if !bytes.Equal(plaintext, tt.plaintext) { |
| 53 | t.Errorf("plaintexts don't match") |
| 54 | } |
| 55 | |
| 56 | ciphertext[0] ^= 0xff |
| 57 | plaintext, err = Decrypt(ciphertext, tt.key) |
| 58 | if err == nil { |
| 59 | t.Errorf("gcmOpen should not have worked, but did") |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func BenchmarkAESGCM(b *testing.B) { |
| 65 | randomKey := &[32]byte{} |