{
AES_KEY ks1, ks2; /* AES key schedules to use */
XTS128_CONTEXT xts;
+ void (*stream)(const unsigned char *in,
+ unsigned char *out, size_t length,
+ const AES_KEY *key1, const AES_KEY *key2,
+ const unsigned char iv[16]);
} EVP_AES_XTS_CTX;
typedef struct
int len_set; /* Set if message length set */
int L, M; /* L and M parameters from RFC3610 */
CCM128_CONTEXT ccm;
+ ccm128_f str;
} EVP_AES_CCM_CTX;
#define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
unsigned char *ivec, int enc);
#endif
#ifdef BSAES_ASM
+void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out,
+ size_t length, const AES_KEY *key,
+ unsigned char ivec[16], int enc);
void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
size_t len, const AES_KEY *key,
const unsigned char ivec[16]);
+void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out,
+ size_t len, const AES_KEY *key1,
+ const AES_KEY *key2, const unsigned char iv[16]);
+void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out,
+ size_t len, const AES_KEY *key1,
+ const AES_KEY *key2, const unsigned char iv[16]);
#endif
#ifdef AES_CTR_ASM
void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
size_t blocks, const AES_KEY *key,
const unsigned char ivec[AES_BLOCK_SIZE]);
#endif
+#ifdef AES_XTS_ASM
+void AES_xts_encrypt(const char *inp,char *out,size_t len,
+ const AES_KEY *key1, const AES_KEY *key2,
+ const unsigned char iv[16]);
+void AES_xts_decrypt(const char *inp,char *out,size_t len,
+ const AES_KEY *key1, const AES_KEY *key2,
+ const unsigned char iv[16]);
+#endif
#if defined(AES_ASM) && !defined(I386_ONLY) && ( \
((defined(__i386) || defined(__i386__) || \
{
aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
xctx->xts.block1 = (block128_f)aesni_encrypt;
+ xctx->stream = aesni_xts_encrypt;
}
else
{
aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
xctx->xts.block1 = (block128_f)aesni_decrypt;
+ xctx->stream = aesni_xts_decrypt;
}
aesni_set_encrypt_key(key + ctx->key_len/2,
return 1;
}
+#define aesni_xts_cipher aes_xts_cipher
static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
- const unsigned char *in, size_t len)
- {
- EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
- if (!xctx->xts.key1 || !xctx->xts.key2)
- return -1;
- if (!out || !in)
- return -1;
-#ifdef OPENSSL_FIPS
- /* Requirement of SP800-38E */
- if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
- (len > (1L<<20)*16))
- {
- EVPerr(EVP_F_AESNI_XTS_CIPHER, EVP_R_TOO_LARGE);
- return -1;
- }
-#endif
- if (ctx->encrypt)
- aesni_xts_encrypt(in, out, len,
- xctx->xts.key1, xctx->xts.key2, ctx->iv);
- else
- aesni_xts_decrypt(in, out, len,
- xctx->xts.key1, xctx->xts.key2, ctx->iv);
-
- return len;
- }
+ const unsigned char *in, size_t len);
static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
&cctx->ks, (block128_f)aesni_encrypt);
+ cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks :
+ (ccm128_f)aesni_ccm64_decrypt_blocks;
cctx->key_set = 1;
}
if (iv)
return 1;
}
+#define aesni_ccm_cipher aes_ccm_cipher
static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
- const unsigned char *in, size_t len)
- {
- EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
- CCM128_CONTEXT *ccm = &cctx->ccm;
- /* If not set up, return error */
- if (!cctx->iv_set && !cctx->key_set)
- return -1;
- if (!ctx->encrypt && !cctx->tag_set)
- return -1;
- if (!out)
- {
- if (!in)
- {
- if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
- return -1;
- cctx->len_set = 1;
- return len;
- }
- /* If have AAD need message length */
- if (!cctx->len_set && len)
- return -1;
- CRYPTO_ccm128_aad(ccm, in, len);
- return len;
- }
- /* EVP_*Final() doesn't return any data */
- if (!in)
- return 0;
- /* If not set length yet do it */
- if (!cctx->len_set)
- {
- if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
- return -1;
- cctx->len_set = 1;
- }
- if (ctx->encrypt)
- {
- if (CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
- aesni_ccm64_encrypt_blocks))
- return -1;
- cctx->tag_set = 1;
- return len;
- }
- else
- {
- int rv = -1;
- if (!CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
- aesni_ccm64_decrypt_blocks))
- {
- unsigned char tag[16];
- if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
- {
- if (!memcmp(tag, ctx->buf, cctx->M))
- rv = len;
- }
- }
- if (rv == -1)
- OPENSSL_cleanse(out, len);
- cctx->iv_set = 0;
- cctx->tag_set = 0;
- cctx->len_set = 0;
- return rv;
- }
-
- }
+ const unsigned char *in, size_t len);
#define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
static const EVP_CIPHER aesni_##keylen##_##mode = { \
mode = ctx->cipher->flags & EVP_CIPH_MODE;
if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
&& !enc)
+#ifdef BSAES_CAPABLE
+ if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE)
+ {
+ ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks);
+ dat->block = (block128_f)AES_decrypt;
+ dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
+ }
+ else
+#endif
#ifdef VPAES_CAPABLE
if (VPAES_CAPABLE)
{
EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
int rv = -1;
/* Encrypt/decrypt must be performed in place */
- if (out != in)
+ if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN))
return -1;
/* Set IV from start of buffer or generate IV and write to start
* of buffer.
if (key) do
{
+#ifdef AES_XTS_ASM
+ xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
+#else
+ xctx->stream = NULL;
+#endif
/* key_len is two AES keys */
+#ifdef BSAES_CAPABLE
+ if (BSAES_CAPABLE)
+ xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
+ else
+#endif
#ifdef VPAES_CAPABLE
if (VPAES_CAPABLE)
{
{
EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
if (!xctx->xts.key1 || !xctx->xts.key2)
- return -1;
- if (!out || !in)
- return -1;
+ return 0;
+ if (!out || !in || len<AES_BLOCK_SIZE)
+ return 0;
#ifdef OPENSSL_FIPS
/* Requirement of SP800-38E */
if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
- (len > (1L<<20)*16))
+ (len > (1UL<<20)*16))
{
EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE);
- return -1;
+ return 0;
}
#endif
- if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
+ if (xctx->stream)
+ (*xctx->stream)(in, out, len,
+ xctx->xts.key1, xctx->xts.key2, ctx->iv);
+ else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
ctx->encrypt))
- return -1;
- return len;
+ return 0;
+ return 1;
}
#define aes_xts_cleanup NULL
-BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
-BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
+#define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
+ | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
+
+BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
+BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
{
AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
&cctx->ks, (block128_f)AES_encrypt);
+ cctx->str = NULL;
cctx->key_set = 1;
} while (0);
if (iv)
}
if (ctx->encrypt)
{
- if (CRYPTO_ccm128_encrypt(ccm, in, out, len))
+ if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
+ cctx->str) :
+ CRYPTO_ccm128_encrypt(ccm, in, out, len))
return -1;
cctx->tag_set = 1;
return len;
else
{
int rv = -1;
- if (!CRYPTO_ccm128_decrypt(ccm, in, out, len))
+ if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
+ cctx->str) :
+ !CRYPTO_ccm128_decrypt(ccm, in, out, len))
{
unsigned char tag[16];
if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))