Skip to content

Commit

Permalink
Merge pull request #34 from microsoft/dev/qmuntal/nilseal
Browse files Browse the repository at this point in the history
Initialize empty AES-GCM inputs
  • Loading branch information
qmuntal authored Jul 27, 2022
2 parents 136d210 + 5909954 commit f0cde27
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
22 changes: 22 additions & 0 deletions openssl/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ func TestSealAndOpen(t *testing.T) {
}
}

func TestSealAndOpen_Empty(t *testing.T) {
key := []byte("D249BF6DEC97B1EBD69BC4D6B3A3C49D")
ci, err := NewAESCipher(key)
if err != nil {
t.Fatal(err)
}
c := ci.(*aesCipher)
gcm, err := c.NewGCM(gcmStandardNonceSize, gcmTagSize)
if err != nil {
t.Fatal(err)
}
nonce := []byte{0x91, 0xc7, 0xa7, 0x54, 0x52, 0xef, 0x10, 0xdb, 0x91, 0xa8, 0x6c, 0xf9}
sealed := gcm.Seal(nil, nonce, []byte{}, []byte{})
decrypted, err := gcm.Open(nil, nonce, sealed, []byte{})
if err != nil {
t.Error(err)
}
if !bytes.Equal(decrypted, []byte{}) {
t.Errorf("unexpected decrypted result\ngot: %#v\nexp: %#v", decrypted, []byte{})
}
}

func TestSealAndOpenTLS(t *testing.T) {
key := []byte("D249BF6DEC97B1EBD69BC4D6B3A3C49D")
ci, err := NewAESCipher(key)
Expand Down
14 changes: 10 additions & 4 deletions openssl/goopenssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,16 @@ go_openssl_EVP_CIPHER_CTX_seal_wrapper(const GO_EVP_CIPHER_CTX_PTR ctx,
unsigned char *out,
const unsigned char *nonce,
const unsigned char *in, int in_len,
const unsigned char *add, int add_len)
const unsigned char *aad, int aad_len)
{
if (in_len == 0) in = "";
if (aad_len == 0) aad = "";

if (go_openssl_EVP_CipherInit_ex(ctx, NULL, NULL, NULL, nonce, GO_AES_ENCRYPT) != 1)
return 0;

int discard_len, out_len;
if (go_openssl_EVP_EncryptUpdate(ctx, NULL, &discard_len, add, add_len) != 1
if (go_openssl_EVP_EncryptUpdate(ctx, NULL, &discard_len, aad, aad_len) != 1
|| go_openssl_EVP_EncryptUpdate(ctx, out, &out_len, in, in_len) != 1
|| go_openssl_EVP_EncryptFinal_ex(ctx, out + out_len, &discard_len) != 1)
{
Expand All @@ -115,14 +118,17 @@ go_openssl_EVP_CIPHER_CTX_open_wrapper(const GO_EVP_CIPHER_CTX_PTR ctx,
unsigned char *out,
const unsigned char *nonce,
const unsigned char *in, int in_len,
const unsigned char *add, int add_len,
const unsigned char *aad, int aad_len,
const unsigned char *tag)
{
if (in_len == 0) in = "";
if (aad_len == 0) aad = "";

if (go_openssl_EVP_CipherInit_ex(ctx, NULL, NULL, NULL, nonce, GO_AES_DECRYPT) != 1)
return 0;

int discard_len, out_len;
if (go_openssl_EVP_DecryptUpdate(ctx, NULL, &discard_len, add, add_len) != 1
if (go_openssl_EVP_DecryptUpdate(ctx, NULL, &discard_len, aad, aad_len) != 1
|| go_openssl_EVP_DecryptUpdate(ctx, out, &out_len, in, in_len) != 1)
{
return 0;
Expand Down

0 comments on commit f0cde27

Please sign in to comment.