|
| 1 | +package aesgcm_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/rand" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/binary" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/0xsequence/nitrocontrol/aesgcm" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestEncrypt(t *testing.T) { |
| 16 | + t.Run("no additional data", func(t *testing.T) { |
| 17 | + random := bytes.NewBuffer([]byte("123456789012")) // 12 bytes |
| 18 | + key := []byte("12345678901234567890123456789012") // 32 bytes |
| 19 | + plaintext := []byte("Hello world") |
| 20 | + enc, err := aesgcm.Encrypt(random, key[:], plaintext, nil) |
| 21 | + require.NoError(t, err) |
| 22 | + assert.Equal(t, "MTIzNDU2Nzg5MDEyyJeRgm1hto6XflsTaSRZcpV9masIpIeV/7/d", base64.StdEncoding.EncodeToString(enc)) |
| 23 | + }) |
| 24 | + |
| 25 | + t.Run("with additional data", func(t *testing.T) { |
| 26 | + random := bytes.NewBuffer([]byte("123456789012")) // 12 bytes |
| 27 | + key := []byte("12345678901234567890123456789012") // 32 bytes |
| 28 | + plaintext := []byte("Hello world") |
| 29 | + additionalData := []byte("additional data") |
| 30 | + enc, err := aesgcm.Encrypt(random, key[:], plaintext, additionalData) |
| 31 | + require.NoError(t, err) |
| 32 | + assert.Equal(t, "MTIzNDU2Nzg5MDEyyJeRgm1hto6XfluPj9+SAYdHVgK9WuQz9M6U", base64.StdEncoding.EncodeToString(enc)) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +func TestDecrypt(t *testing.T) { |
| 37 | + t.Run("no additional data", func(t *testing.T) { |
| 38 | + key := []byte("12345678901234567890123456789012") // 32 bytes |
| 39 | + ciphertext, _ := base64.StdEncoding.DecodeString("MTIzNDU2Nzg5MDEyyJeRgm1hto6XflsTaSRZcpV9masIpIeV/7/d") |
| 40 | + dec, err := aesgcm.Decrypt(key, ciphertext, nil) |
| 41 | + require.NoError(t, err) |
| 42 | + assert.Equal(t, []byte("Hello world"), dec) |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("with additional data", func(t *testing.T) { |
| 46 | + key := []byte("12345678901234567890123456789012") // 32 bytes |
| 47 | + ciphertext, _ := base64.StdEncoding.DecodeString("MTIzNDU2Nzg5MDEyyJeRgm1hto6XfluPj9+SAYdHVgK9WuQz9M6U") |
| 48 | + additionalData := []byte("additional data") |
| 49 | + dec, err := aesgcm.Decrypt(key, ciphertext, additionalData) |
| 50 | + require.NoError(t, err) |
| 51 | + assert.Equal(t, []byte("Hello world"), dec) |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +func FuzzEncryptAndDecrypt(f *testing.F) { |
| 56 | + f.Add(uint64(0), uint64(0), uint64(0), uint64(0), []byte("hello"), []byte("aad")) |
| 57 | + f.Fuzz(func(t *testing.T, u0, u1, u2, u3 uint64, plaintext []byte, additionalData []byte) { |
| 58 | + key := make([]byte, 32) |
| 59 | + binary.LittleEndian.PutUint64(key, u0) |
| 60 | + binary.LittleEndian.PutUint64(key[8:], u1) |
| 61 | + binary.LittleEndian.PutUint64(key[16:], u2) |
| 62 | + binary.LittleEndian.PutUint64(key[24:], u3) |
| 63 | + |
| 64 | + enc, err := aesgcm.Encrypt(rand.Reader, key[:], plaintext, additionalData) |
| 65 | + require.NoError(t, err) |
| 66 | + require.Greater(t, len(enc), 16) |
| 67 | + |
| 68 | + dec, err := aesgcm.Decrypt(key, enc, additionalData) |
| 69 | + require.NoError(t, err) |
| 70 | + require.Equal(t, plaintext, dec) |
| 71 | + }) |
| 72 | +} |
0 commit comments