From aa965a580b4fac6d2f0bc27154e4d2603344bb43 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 11 Oct 2015 14:29:25 +0200 Subject: [PATCH] Expose only crypto_aead_aes256gcm_*() not crypto_aead_aes256gcm_aesni_*() libsodium typically doesn't expose specific implementations. It shouldn't be the case for that construction either, especially since an ARM8 implementation might be added later. We want a single interface for both. --- .../aes256gcm/aesni/aead_aes256gcm_aesni.c | 79 ++++++++--------- src/libsodium/include/Makefile.am | 2 +- src/libsodium/include/sodium.h | 2 +- .../include/sodium/crypto_aead_aes256gcm.h | 88 +++++++++++++++++++ .../sodium/crypto_aead_aes256gcm_aesni.h | 88 ------------------- test/default/aead_aes256gcm.c | 16 ++-- 6 files changed, 136 insertions(+), 139 deletions(-) create mode 100644 src/libsodium/include/sodium/crypto_aead_aes256gcm.h delete mode 100644 src/libsodium/include/sodium/crypto_aead_aes256gcm_aesni.h diff --git a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c index bfd6922d..1920c7aa 100644 --- a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +++ b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c @@ -7,7 +7,7 @@ #include #include -#include "crypto_aead_aes256gcm_aesni.h" +#include "crypto_aead_aes256gcm.h" #include "export.h" #include "runtime.h" #include "utils.h" @@ -480,8 +480,8 @@ do { \ } while(0) int -crypto_aead_aes256gcm_aesni_beforenm(crypto_aead_aes256gcm_state *ctx_, - const unsigned char *k) +crypto_aead_aes256gcm_beforenm(crypto_aead_aes256gcm_state *ctx_, + const unsigned char *k) { context *ctx = (context *) ctx_; __m128i *rkeys = ctx->rkeys; @@ -496,12 +496,12 @@ crypto_aead_aes256gcm_aesni_beforenm(crypto_aead_aes256gcm_state *ctx_, } int -crypto_aead_aes256gcm_aesni_encrypt_afternm(unsigned char *c, unsigned long long *clen, - const unsigned char *m, unsigned long long mlen, - const unsigned char *ad, unsigned long long adlen, - const unsigned char *nsec, - const unsigned char *npub, - const crypto_aead_aes256gcm_state *ctx_) +crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, unsigned long long *clen, + const unsigned char *m, unsigned long long mlen, + const unsigned char *ad, unsigned long long adlen, + const unsigned char *nsec, + const unsigned char *npub, + const crypto_aead_aes256gcm_state *ctx_) { unsigned char H[16]; const __m128i rev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); @@ -612,12 +612,12 @@ crypto_aead_aes256gcm_aesni_encrypt_afternm(unsigned char *c, unsigned long long } int -crypto_aead_aes256gcm_aesni_decrypt_afternm(unsigned char *m, unsigned long long *mlen_p, - unsigned char *nsec, - const unsigned char *c, unsigned long long clen, - const unsigned char *ad, unsigned long long adlen, - const unsigned char *npub, - const crypto_aead_aes256gcm_state *ctx_) +crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen_p, + unsigned char *nsec, + const unsigned char *c, unsigned long long clen, + const unsigned char *ad, unsigned long long adlen, + const unsigned char *npub, + const crypto_aead_aes256gcm_state *ctx_) { unsigned char H[16]; const __m128i rev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); @@ -761,46 +761,45 @@ crypto_aead_aes256gcm_aesni_decrypt_afternm(unsigned char *m, unsigned long long } int -crypto_aead_aes256gcm_aesni_encrypt(unsigned char *c, - unsigned long long *clen_p, - const unsigned char *m, - unsigned long long mlen, - const unsigned char *ad, - unsigned long long adlen, - const unsigned char *nsec, - const unsigned char *npub, - const unsigned char *k) +crypto_aead_aes256gcm_encrypt(unsigned char *c, + unsigned long long *clen_p, + const unsigned char *m, + unsigned long long mlen, + const unsigned char *ad, + unsigned long long adlen, + const unsigned char *nsec, + const unsigned char *npub, + const unsigned char *k) { crypto_aead_aes256gcm_state ctx; - crypto_aead_aes256gcm_aesni_beforenm(&ctx, k); + crypto_aead_aes256gcm_beforenm(&ctx, k); - return crypto_aead_aes256gcm_aesni_encrypt_afternm + return crypto_aead_aes256gcm_encrypt_afternm (c, clen_p, m, mlen, ad, adlen, nsec, npub, &ctx); } int -crypto_aead_aes256gcm_aesni_decrypt(unsigned char *m, - unsigned long long *mlen_p, - unsigned char *nsec, - const unsigned char *c, - unsigned long long clen, - const unsigned char *ad, - unsigned long long adlen, - const unsigned char *npub, - const unsigned char *k) +crypto_aead_aes256gcm_decrypt(unsigned char *m, + unsigned long long *mlen_p, + unsigned char *nsec, + const unsigned char *c, + unsigned long long clen, + const unsigned char *ad, + unsigned long long adlen, + const unsigned char *npub, + const unsigned char *k) { crypto_aead_aes256gcm_state ctx; - crypto_aead_aes256gcm_aesni_beforenm((crypto_aead_aes256gcm_state *) - &ctx, k); + crypto_aead_aes256gcm_beforenm(&ctx, k); - return crypto_aead_aes256gcm_aesni_decrypt_afternm + return crypto_aead_aes256gcm_decrypt_afternm (m, mlen_p, nsec, c, clen, ad, adlen, npub, &ctx); } int -crypto_aead_aes256gcm_aesni_is_available(void) +crypto_aead_aes256gcm_is_available(void) { return sodium_runtime_has_pclmul() & sodium_runtime_has_aesni(); } @@ -838,7 +837,7 @@ crypto_aead_aes256gcm_statebytes(void) #else int -crypto_aead_aes256gcm_aesni_is_available(void) +crypto_aead_aes256gcm_is_available(void) { return 0; } diff --git a/src/libsodium/include/Makefile.am b/src/libsodium/include/Makefile.am index 854e35fa..31d2c1c8 100644 --- a/src/libsodium/include/Makefile.am +++ b/src/libsodium/include/Makefile.am @@ -2,7 +2,7 @@ SODIUM_EXPORT = \ sodium.h \ sodium/core.h \ - sodium/crypto_aead_aes256gcm_aesni.h \ + sodium/crypto_aead_aes256gcm.h \ sodium/crypto_aead_chacha20poly1305.h \ sodium/crypto_auth.h \ sodium/crypto_auth_hmacsha256.h \ diff --git a/src/libsodium/include/sodium.h b/src/libsodium/include/sodium.h index b615cad8..b9a44ca3 100644 --- a/src/libsodium/include/sodium.h +++ b/src/libsodium/include/sodium.h @@ -3,7 +3,7 @@ #define sodium_H #include "sodium/core.h" -#include "sodium/crypto_aead_aes256gcm_aesni.h" +#include "sodium/crypto_aead_aes256gcm.h" #include "sodium/crypto_aead_chacha20poly1305.h" #include "sodium/crypto_auth.h" #include "sodium/crypto_auth_hmacsha256.h" diff --git a/src/libsodium/include/sodium/crypto_aead_aes256gcm.h b/src/libsodium/include/sodium/crypto_aead_aes256gcm.h new file mode 100644 index 00000000..9ce476f6 --- /dev/null +++ b/src/libsodium/include/sodium/crypto_aead_aes256gcm.h @@ -0,0 +1,88 @@ +#ifndef crypto_aead_aes256gcm_H +#define crypto_aead_aes256gcm_H + +#include +#include "export.h" + +#ifdef __cplusplus +# if __GNUC__ +# pragma GCC diagnostic ignored "-Wlong-long" +# endif +extern "C" { +#endif + +SODIUM_EXPORT +int crypto_aead_aes256gcm_is_available(void); + +#define crypto_aead_aes256gcm_KEYBYTES 32U +SODIUM_EXPORT +size_t crypto_aead_aes256gcm_keybytes(void); + +#define crypto_aead_aes256gcm_NSECBYTES 0U +SODIUM_EXPORT +size_t crypto_aead_aes256gcm_nsecbytes(void); + +#define crypto_aead_aes256gcm_NPUBBYTES 12U +SODIUM_EXPORT +size_t crypto_aead_aes256gcm_pubbytes(void); + +#define crypto_aead_aes256gcm_ABYTES 16U +SODIUM_EXPORT +size_t crypto_aead_aes256gcm_abytes(void); + +typedef CRYPTO_ALIGN(16) unsigned char crypto_aead_aes256gcm_state[512]; +SODIUM_EXPORT +size_t crypto_aead_aes256gcm_statebytes(void); + +SODIUM_EXPORT +int crypto_aead_aes256gcm_encrypt(unsigned char *c, + unsigned long long *clen_p, + const unsigned char *m, + unsigned long long mlen, + const unsigned char *ad, + unsigned long long adlen, + const unsigned char *nsec, + const unsigned char *npub, + const unsigned char *k); + +SODIUM_EXPORT +int crypto_aead_aes256gcm_decrypt(unsigned char *m, + unsigned long long *mlen_p, + unsigned char *nsec, + const unsigned char *c, + unsigned long long clen, + const unsigned char *ad, + unsigned long long adlen, + const unsigned char *npub, + const unsigned char *k); + +SODIUM_EXPORT +int crypto_aead_aes256gcm_beforenm(crypto_aead_aes256gcm_state *ctx_, + const unsigned char *k); + +SODIUM_EXPORT +int crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, + unsigned long long *clen_p, + const unsigned char *m, + unsigned long long mlen, + const unsigned char *ad, + unsigned long long adlen, + const unsigned char *nsec, + const unsigned char *npub, + const crypto_aead_aes256gcm_state *ctx_); + +SODIUM_EXPORT +int crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, + unsigned long long *mlen_p, + unsigned char *nsec, + const unsigned char *c, + unsigned long long clen, + const unsigned char *ad, + unsigned long long adlen, + const unsigned char *npub, + const crypto_aead_aes256gcm_state *ctx_); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/libsodium/include/sodium/crypto_aead_aes256gcm_aesni.h b/src/libsodium/include/sodium/crypto_aead_aes256gcm_aesni.h deleted file mode 100644 index 990a4091..00000000 --- a/src/libsodium/include/sodium/crypto_aead_aes256gcm_aesni.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef crypto_aead_aes256gcm_aesni_H -#define crypto_aead_aes256gcm_aesni_H - -#include -#include "export.h" - -#ifdef __cplusplus -# if __GNUC__ -# pragma GCC diagnostic ignored "-Wlong-long" -# endif -extern "C" { -#endif - -SODIUM_EXPORT -int crypto_aead_aes256gcm_aesni_is_available(void); - -#define crypto_aead_aes256gcm_KEYBYTES 32U -SODIUM_EXPORT -size_t crypto_aead_aes256gcm_keybytes(void); - -#define crypto_aead_aes256gcm_NSECBYTES 0U -SODIUM_EXPORT -size_t crypto_aead_aes256gcm_nsecbytes(void); - -#define crypto_aead_aes256gcm_NPUBBYTES 12U -SODIUM_EXPORT -size_t crypto_aead_aes256gcm_pubbytes(void); - -#define crypto_aead_aes256gcm_ABYTES 16U -SODIUM_EXPORT -size_t crypto_aead_aes256gcm_abytes(void); - -typedef CRYPTO_ALIGN(16) unsigned char crypto_aead_aes256gcm_state[512]; -SODIUM_EXPORT -size_t crypto_aead_aes256gcm_statebytes(void); - -SODIUM_EXPORT -int crypto_aead_aes256gcm_aesni_encrypt(unsigned char *c, - unsigned long long *clen_p, - const unsigned char *m, - unsigned long long mlen, - const unsigned char *ad, - unsigned long long adlen, - const unsigned char *nsec, - const unsigned char *npub, - const unsigned char *k); - -SODIUM_EXPORT -int crypto_aead_aes256gcm_aesni_decrypt(unsigned char *m, - unsigned long long *mlen_p, - unsigned char *nsec, - const unsigned char *c, - unsigned long long clen, - const unsigned char *ad, - unsigned long long adlen, - const unsigned char *npub, - const unsigned char *k); - -SODIUM_EXPORT -int crypto_aead_aes256gcm_aesni_beforenm(crypto_aead_aes256gcm_state *ctx_, - const unsigned char *k); - -SODIUM_EXPORT -int crypto_aead_aes256gcm_aesni_encrypt_afternm(unsigned char *c, - unsigned long long *clen_p, - const unsigned char *m, - unsigned long long mlen, - const unsigned char *ad, - unsigned long long adlen, - const unsigned char *nsec, - const unsigned char *npub, - const crypto_aead_aes256gcm_state *ctx_); - -SODIUM_EXPORT -int crypto_aead_aes256gcm_aesni_decrypt_afternm(unsigned char *m, - unsigned long long *mlen_p, - unsigned char *nsec, - const unsigned char *c, - unsigned long long clen, - const unsigned char *ad, - unsigned long long adlen, - const unsigned char *npub, - const crypto_aead_aes256gcm_state *ctx_); -#ifdef __cplusplus -} -#endif - -#endif diff --git a/test/default/aead_aes256gcm.c b/test/default/aead_aes256gcm.c index cb402a7d..385c5e75 100644 --- a/test/default/aead_aes256gcm.c +++ b/test/default/aead_aes256gcm.c @@ -3059,20 +3059,18 @@ tv(void) NULL, NULL, NULL); ciphertext = sodium_malloc(ciphertext_len); - crypto_aead_aes256gcm_aesni_encrypt(ciphertext, &found_ciphertext_len, - message, message_len, - ad, ad_len, - NULL, nonce, key); + crypto_aead_aes256gcm_encrypt(ciphertext, &found_ciphertext_len, + message, message_len, + ad, ad_len, NULL, nonce, key); assert((size_t) found_ciphertext_len == ciphertext_len); if (memcmp(ciphertext, expected_ciphertext, ciphertext_len) != 0) { printf("Encryption of test vector #%u failed\n", (unsigned int) i); } decrypted = sodium_malloc(message_len); - if (crypto_aead_aes256gcm_aesni_decrypt(decrypted, &found_message_len, - NULL, - ciphertext, ciphertext_len, - ad, ad_len, nonce, key) != 0) { + if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len, + NULL, ciphertext, ciphertext_len, + ad, ad_len, nonce, key) != 0) { printf("Verification of test vector #%u failed\n", (unsigned int) i); } assert((size_t) found_message_len == message_len); @@ -3095,7 +3093,7 @@ tv(void) int main(void) { - if (crypto_aead_aes256gcm_aesni_is_available()) { + if (crypto_aead_aes256gcm_is_available()) { tv(); } printf("OK\n");