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.
This commit is contained in:
Frank Denis 2015-10-11 14:29:25 +02:00
parent dadc5d9906
commit aa965a580b
6 changed files with 136 additions and 139 deletions

View File

@ -7,7 +7,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "crypto_aead_aes256gcm_aesni.h" #include "crypto_aead_aes256gcm.h"
#include "export.h" #include "export.h"
#include "runtime.h" #include "runtime.h"
#include "utils.h" #include "utils.h"
@ -480,8 +480,8 @@ do { \
} while(0) } while(0)
int int
crypto_aead_aes256gcm_aesni_beforenm(crypto_aead_aes256gcm_state *ctx_, crypto_aead_aes256gcm_beforenm(crypto_aead_aes256gcm_state *ctx_,
const unsigned char *k) const unsigned char *k)
{ {
context *ctx = (context *) ctx_; context *ctx = (context *) ctx_;
__m128i *rkeys = ctx->rkeys; __m128i *rkeys = ctx->rkeys;
@ -496,12 +496,12 @@ crypto_aead_aes256gcm_aesni_beforenm(crypto_aead_aes256gcm_state *ctx_,
} }
int int
crypto_aead_aes256gcm_aesni_encrypt_afternm(unsigned char *c, unsigned long long *clen, crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, unsigned long long *clen,
const unsigned char *m, unsigned long long mlen, const unsigned char *m, unsigned long long mlen,
const unsigned char *ad, unsigned long long adlen, const unsigned char *ad, unsigned long long adlen,
const unsigned char *nsec, const unsigned char *nsec,
const unsigned char *npub, const unsigned char *npub,
const crypto_aead_aes256gcm_state *ctx_) const crypto_aead_aes256gcm_state *ctx_)
{ {
unsigned char H[16]; 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); 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 int
crypto_aead_aes256gcm_aesni_decrypt_afternm(unsigned char *m, unsigned long long *mlen_p, crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen_p,
unsigned char *nsec, unsigned char *nsec,
const unsigned char *c, unsigned long long clen, const unsigned char *c, unsigned long long clen,
const unsigned char *ad, unsigned long long adlen, const unsigned char *ad, unsigned long long adlen,
const unsigned char *npub, const unsigned char *npub,
const crypto_aead_aes256gcm_state *ctx_) const crypto_aead_aes256gcm_state *ctx_)
{ {
unsigned char H[16]; 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); 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 int
crypto_aead_aes256gcm_aesni_encrypt(unsigned char *c, crypto_aead_aes256gcm_encrypt(unsigned char *c,
unsigned long long *clen_p, unsigned long long *clen_p,
const unsigned char *m, const unsigned char *m,
unsigned long long mlen, unsigned long long mlen,
const unsigned char *ad, const unsigned char *ad,
unsigned long long adlen, unsigned long long adlen,
const unsigned char *nsec, const unsigned char *nsec,
const unsigned char *npub, const unsigned char *npub,
const unsigned char *k) const unsigned char *k)
{ {
crypto_aead_aes256gcm_state ctx; 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); (c, clen_p, m, mlen, ad, adlen, nsec, npub, &ctx);
} }
int int
crypto_aead_aes256gcm_aesni_decrypt(unsigned char *m, crypto_aead_aes256gcm_decrypt(unsigned char *m,
unsigned long long *mlen_p, unsigned long long *mlen_p,
unsigned char *nsec, unsigned char *nsec,
const unsigned char *c, const unsigned char *c,
unsigned long long clen, unsigned long long clen,
const unsigned char *ad, const unsigned char *ad,
unsigned long long adlen, unsigned long long adlen,
const unsigned char *npub, const unsigned char *npub,
const unsigned char *k) const unsigned char *k)
{ {
crypto_aead_aes256gcm_state ctx; crypto_aead_aes256gcm_state ctx;
crypto_aead_aes256gcm_aesni_beforenm((crypto_aead_aes256gcm_state *) crypto_aead_aes256gcm_beforenm(&ctx, k);
&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); (m, mlen_p, nsec, c, clen, ad, adlen, npub, &ctx);
} }
int int
crypto_aead_aes256gcm_aesni_is_available(void) crypto_aead_aes256gcm_is_available(void)
{ {
return sodium_runtime_has_pclmul() & sodium_runtime_has_aesni(); return sodium_runtime_has_pclmul() & sodium_runtime_has_aesni();
} }
@ -838,7 +837,7 @@ crypto_aead_aes256gcm_statebytes(void)
#else #else
int int
crypto_aead_aes256gcm_aesni_is_available(void) crypto_aead_aes256gcm_is_available(void)
{ {
return 0; return 0;
} }

View File

@ -2,7 +2,7 @@
SODIUM_EXPORT = \ SODIUM_EXPORT = \
sodium.h \ sodium.h \
sodium/core.h \ sodium/core.h \
sodium/crypto_aead_aes256gcm_aesni.h \ sodium/crypto_aead_aes256gcm.h \
sodium/crypto_aead_chacha20poly1305.h \ sodium/crypto_aead_chacha20poly1305.h \
sodium/crypto_auth.h \ sodium/crypto_auth.h \
sodium/crypto_auth_hmacsha256.h \ sodium/crypto_auth_hmacsha256.h \

View File

@ -3,7 +3,7 @@
#define sodium_H #define sodium_H
#include "sodium/core.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_aead_chacha20poly1305.h"
#include "sodium/crypto_auth.h" #include "sodium/crypto_auth.h"
#include "sodium/crypto_auth_hmacsha256.h" #include "sodium/crypto_auth_hmacsha256.h"

View File

@ -0,0 +1,88 @@
#ifndef crypto_aead_aes256gcm_H
#define crypto_aead_aes256gcm_H
#include <stddef.h>
#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

View File

@ -1,88 +0,0 @@
#ifndef crypto_aead_aes256gcm_aesni_H
#define crypto_aead_aes256gcm_aesni_H
#include <stddef.h>
#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

View File

@ -3059,20 +3059,18 @@ tv(void)
NULL, NULL, NULL); NULL, NULL, NULL);
ciphertext = sodium_malloc(ciphertext_len); ciphertext = sodium_malloc(ciphertext_len);
crypto_aead_aes256gcm_aesni_encrypt(ciphertext, &found_ciphertext_len, crypto_aead_aes256gcm_encrypt(ciphertext, &found_ciphertext_len,
message, message_len, message, message_len,
ad, ad_len, ad, ad_len, NULL, nonce, key);
NULL, nonce, key);
assert((size_t) found_ciphertext_len == ciphertext_len); assert((size_t) found_ciphertext_len == ciphertext_len);
if (memcmp(ciphertext, expected_ciphertext, ciphertext_len) != 0) { if (memcmp(ciphertext, expected_ciphertext, ciphertext_len) != 0) {
printf("Encryption of test vector #%u failed\n", (unsigned int) i); printf("Encryption of test vector #%u failed\n", (unsigned int) i);
} }
decrypted = sodium_malloc(message_len); decrypted = sodium_malloc(message_len);
if (crypto_aead_aes256gcm_aesni_decrypt(decrypted, &found_message_len, if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len,
NULL, NULL, ciphertext, ciphertext_len,
ciphertext, ciphertext_len, ad, ad_len, nonce, key) != 0) {
ad, ad_len, nonce, key) != 0) {
printf("Verification of test vector #%u failed\n", (unsigned int) i); printf("Verification of test vector #%u failed\n", (unsigned int) i);
} }
assert((size_t) found_message_len == message_len); assert((size_t) found_message_len == message_len);
@ -3095,7 +3093,7 @@ tv(void)
int int
main(void) main(void)
{ {
if (crypto_aead_aes256gcm_aesni_is_available()) { if (crypto_aead_aes256gcm_is_available()) {
tv(); tv();
} }
printf("OK\n"); printf("OK\n");