2017-07-14 23:22:15 +02:00
|
|
|
// +build !without_openssl
|
|
|
|
|
2016-05-04 22:34:52 +02:00
|
|
|
// We compare against Go's built-in GCM implementation. Since stupidgcm only
|
|
|
|
// supports 128-bit IVs and Go only supports that from 1.5 onward, we cannot
|
|
|
|
// run these tests on older Go versions.
|
2016-05-01 22:26:47 +02:00
|
|
|
package stupidgcm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
2016-12-10 11:50:16 +01:00
|
|
|
"log"
|
2016-05-01 22:26:47 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get "n" random bytes from /dev/urandom or panic
|
|
|
|
func randBytes(n int) []byte {
|
|
|
|
b := make([]byte, n)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
2016-12-10 11:50:16 +01:00
|
|
|
log.Panic("Failed to read random bytes: " + err.Error())
|
2016-05-01 22:26:47 +02:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestEncryptDecrypt encrypts and decrypts using both stupidgcm and Go's built-in
|
2018-12-27 12:03:00 +01:00
|
|
|
// GCM implementation and verifies that the results are identical.
|
2016-05-01 22:26:47 +02:00
|
|
|
func TestEncryptDecrypt(t *testing.T) {
|
|
|
|
key := randBytes(32)
|
2017-04-08 02:09:28 +02:00
|
|
|
sGCM := New(key, false)
|
2016-05-01 22:26:47 +02:00
|
|
|
|
|
|
|
gAES, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
gGCM, err := cipher.NewGCMWithNonceSize(gAES, 16)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 09:57:06 +02:00
|
|
|
testEncryptDecrypt(t, sGCM, gGCM)
|
2016-05-01 22:26:47 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 18:52:33 +02:00
|
|
|
// Seal re-uses the "dst" buffer it is large enough.
|
|
|
|
// Check that this works correctly by testing different "dst" capacities from
|
|
|
|
// 5000 to 16 and "in" lengths from 1 to 5000.
|
|
|
|
func TestInplaceSeal(t *testing.T) {
|
|
|
|
key := randBytes(32)
|
|
|
|
sGCM := New(key, false)
|
|
|
|
|
|
|
|
gAES, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
gGCM, err := cipher.NewGCMWithNonceSize(gAES, 16)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 09:57:06 +02:00
|
|
|
testInplaceSeal(t, sGCM, gGCM)
|
2017-06-29 18:52:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 23:24:12 +02:00
|
|
|
// Open re-uses the "dst" buffer it is large enough.
|
|
|
|
// Check that this works correctly by testing different "dst" capacities from
|
|
|
|
// 5000 to 16 and "in" lengths from 1 to 5000.
|
|
|
|
func TestInplaceOpen(t *testing.T) {
|
2017-07-01 09:55:14 +02:00
|
|
|
key := randBytes(32)
|
|
|
|
sGCM := New(key, false)
|
|
|
|
|
|
|
|
gAES, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
gGCM, err := cipher.NewGCMWithNonceSize(gAES, 16)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 09:57:06 +02:00
|
|
|
testInplaceOpen(t, sGCM, gGCM)
|
2017-06-30 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 22:26:47 +02:00
|
|
|
// TestCorruption verifies that changes in the ciphertext result in a decryption
|
|
|
|
// error
|
|
|
|
func TestCorruption(t *testing.T) {
|
|
|
|
key := randBytes(32)
|
2017-04-08 02:09:28 +02:00
|
|
|
sGCM := New(key, false)
|
2016-05-01 22:26:47 +02:00
|
|
|
|
2021-09-02 09:57:06 +02:00
|
|
|
testCorruption(t, sGCM)
|
2016-05-01 22:26:47 +02:00
|
|
|
}
|