From 79592a8b16ff453ed9738b0a7c7f5117b403bbb8 Mon Sep 17 00:00:00 2001 From: edsgerlin Date: Wed, 12 Sep 2018 00:52:33 +0900 Subject: [PATCH] Use libsodium for `aes-256-gcm` when available. --- src/aead.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/aead.c b/src/aead.c index 9e2da725..fade6c31 100644 --- a/src/aead.c +++ b/src/aead.c @@ -166,9 +166,17 @@ aead_cipher_encrypt(cipher_ctx_t *cipher_ctx, size_t tlen = cipher_ctx->cipher->tag_len; switch (cipher_ctx->cipher->method) { - case AES128GCM: + case AES256GCM: // Only AES-256-GCM is supported by libsodium. + if (crypto_aead_aes256gcm_is_available()) { // Use it if availble + err = crypto_aead_aes256gcm_encrypt(c, &long_clen, m, mlen, + ad, adlen, NULL, n, k); + *clen = (size_t)long_clen; // it's safe to cast 64bit to 32bit length here + break; + } + // Otherwise, just use the mbedTLS one with crappy AES-NI. case AES192GCM: - case AES256GCM: + case AES128GCM: + err = mbedtls_cipher_auth_encrypt(cipher_ctx->evp, n, nlen, ad, adlen, m, mlen, c, clen, c + mlen, tlen); *clen += tlen; @@ -206,9 +214,16 @@ aead_cipher_decrypt(cipher_ctx_t *cipher_ctx, size_t tlen = cipher_ctx->cipher->tag_len; switch (cipher_ctx->cipher->method) { - case AES128GCM: + case AES256GCM: // Only AES-256-GCM is supported by libsodium. + if (crypto_aead_aes256gcm_is_available()) { // Use it if availble + err = crypto_aead_aes256gcm_decrypt(p, &long_plen, NULL, m, mlen, + ad, adlen, n, k); + *plen = (size_t)long_plen; // it's safe to cast 64bit to 32bit length here + break; + } + // Otherwise, just use the mbedTLS one with crappy AES-NI. case AES192GCM: - case AES256GCM: + case AES128GCM: err = mbedtls_cipher_auth_decrypt(cipher_ctx->evp, n, nlen, ad, adlen, m, mlen - tlen, p, plen, m + mlen - tlen, tlen); break;