|
| 1 | +package aesgcm |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/op/go-logging" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSealWithoutAdditionalData(t *testing.T) { |
| 14 | + |
| 15 | + logger := createLogger() |
| 16 | + |
| 17 | + // Use rand.Reader for entropy |
| 18 | + aesgcm := NewAESGCM(logger, true, nil) |
| 19 | + |
| 20 | + // Generate a key |
| 21 | + key := make([]byte, 32) |
| 22 | + _, err := io.ReadFull(rand.Reader, key) |
| 23 | + assert.Nil(t, err) |
| 24 | + |
| 25 | + // Seal the data and get back the cipher-text and nonce |
| 26 | + secret := []byte("my-secret") |
| 27 | + ciphertext, nonce, err := aesgcm.Seal(key, secret, nil) |
| 28 | + |
| 29 | + // Open the cipher-text using the nonce returned from Seal |
| 30 | + decrypted, err := aesgcm.Open(key, ciphertext, nonce, nil) |
| 31 | + assert.Nil(t, err) |
| 32 | + assert.Equal(t, secret, decrypted) |
| 33 | + |
| 34 | + // Ensure failure with a bad nonce |
| 35 | + _, err = aesgcm.Open(key, ciphertext, []byte("foo"), nil) |
| 36 | + assert.NotNil(t, err) |
| 37 | + assert.Equal(t, ErrInvalidNonce, err) |
| 38 | +} |
| 39 | + |
| 40 | +func TestSealWithAdditionalData(t *testing.T) { |
| 41 | + |
| 42 | + logger := createLogger() |
| 43 | + |
| 44 | + // Use rand.Reader for entropy |
| 45 | + aesgcm := NewAESGCM(logger, true, nil) |
| 46 | + |
| 47 | + // Generate a key |
| 48 | + key := make([]byte, 32) |
| 49 | + _, err := io.ReadFull(rand.Reader, key) |
| 50 | + assert.Nil(t, err) |
| 51 | + |
| 52 | + // Create "additional data". The Open operation will fail |
| 53 | + // if the additional data does not match the same data |
| 54 | + // that was passed to Seal. |
| 55 | + additionalData := []byte("some authorization data") |
| 56 | + |
| 57 | + // Seal the data and get back the cipher-text and nonce |
| 58 | + secret := []byte("my-secret") |
| 59 | + ciphertext, nonce, err := aesgcm.Seal(key, secret, additionalData) |
| 60 | + |
| 61 | + // Open the cipher-text using the nonce returned from Seal |
| 62 | + decrypted, err := aesgcm.Open(key, ciphertext, nonce, additionalData) |
| 63 | + assert.Nil(t, err) |
| 64 | + assert.Equal(t, secret, decrypted) |
| 65 | + |
| 66 | + // Ensure failure when additional data doesnt match |
| 67 | + _, err = aesgcm.Open(key, ciphertext, nonce, []byte("foo")) |
| 68 | + assert.NotNil(t, err) |
| 69 | +} |
| 70 | + |
| 71 | +// Create a logger for the tests |
| 72 | +func createLogger() *logging.Logger { |
| 73 | + stdout := logging.NewLogBackend(os.Stdout, "", 0) |
| 74 | + logging.SetBackend(stdout) |
| 75 | + logger := logging.MustGetLogger("tpm") |
| 76 | + |
| 77 | + logFormat := logging.MustStringFormatter( |
| 78 | + `%{color}%{time:15:04:05.000} %{shortpkg}.%{shortfunc} %{level:.4s} %{id:03x}%{color:reset} %{message}`, |
| 79 | + ) |
| 80 | + logFormatter := logging.NewBackendFormatter(stdout, logFormat) |
| 81 | + backends := logging.MultiLogger(stdout, logFormatter) |
| 82 | + logging.SetBackend(backends) |
| 83 | + |
| 84 | + return logger |
| 85 | +} |
0 commit comments