52 #ifndef OPENSSL_NO_AES
91 void (*stream)(
const unsigned char *
in,
94 const unsigned char iv[16]);
109 #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
112 int vpaes_set_encrypt_key(
const unsigned char *userKey,
int bits,
114 int vpaes_set_decrypt_key(
const unsigned char *userKey,
int bits,
117 void vpaes_encrypt(
const unsigned char *in,
unsigned char *out,
119 void vpaes_decrypt(
const unsigned char *in,
unsigned char *out,
122 void vpaes_cbc_encrypt(
const unsigned char *in,
126 unsigned char *ivec,
int enc);
129 void bsaes_cbc_encrypt(
const unsigned char *in,
unsigned char *out,
131 unsigned char ivec[16],
int enc);
132 void bsaes_ctr32_encrypt_blocks(
const unsigned char *in,
unsigned char *out,
134 const unsigned char ivec[16]);
135 void bsaes_xts_encrypt(
const unsigned char *inp,
unsigned char *out,
137 const AES_KEY *key2,
const unsigned char iv[16]);
138 void bsaes_xts_decrypt(
const unsigned char *inp,
unsigned char *out,
140 const AES_KEY *key2,
const unsigned char iv[16]);
143 void AES_ctr32_encrypt(
const unsigned char *in,
unsigned char *out,
148 void AES_xts_encrypt(
const char *inp,
char *out,
size_t len,
150 const unsigned char iv[16]);
151 void AES_xts_decrypt(
const char *inp,
char *out,
size_t len,
153 const unsigned char iv[16]);
156 #if defined(AES_ASM) && !defined(I386_ONLY) && ( \
157 ((defined(__i386) || defined(__i386__) || \
158 defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
159 defined(__x86_64) || defined(__x86_64__) || \
160 defined(_M_AMD64) || defined(_M_X64) || \
166 #define VPAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
169 #define BSAES_CAPABLE VPAES_CAPABLE
174 #define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
176 int aesni_set_encrypt_key(
const unsigned char *userKey,
int bits,
178 int aesni_set_decrypt_key(
const unsigned char *userKey,
int bits,
181 void aesni_encrypt(
const unsigned char *in,
unsigned char *out,
183 void aesni_decrypt(
const unsigned char *in,
unsigned char *out,
186 void aesni_ecb_encrypt(
const unsigned char *in,
191 void aesni_cbc_encrypt(
const unsigned char *in,
195 unsigned char *ivec,
int enc);
197 void aesni_ctr32_encrypt_blocks(
const unsigned char *in,
201 const unsigned char *ivec);
203 void aesni_xts_encrypt(
const unsigned char *in,
207 const unsigned char iv[16]);
209 void aesni_xts_decrypt(
const unsigned char *in,
213 const unsigned char iv[16]);
215 void aesni_ccm64_encrypt_blocks (
const unsigned char *in,
219 const unsigned char ivec[16],
220 unsigned char cmac[16]);
222 void aesni_ccm64_decrypt_blocks (
const unsigned char *in,
226 const unsigned char ivec[16],
227 unsigned char cmac[16]);
230 const unsigned char *iv,
int enc)
265 static int aesni_cbc_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
266 const unsigned char *in,
size_t len)
273 static int aesni_ecb_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
274 const unsigned char *in,
size_t len)
278 if (len<bl)
return 1;
285 #define aesni_ofb_cipher aes_ofb_cipher
286 static int aesni_ofb_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
287 const unsigned char *in,
size_t len);
289 #define aesni_cfb_cipher aes_cfb_cipher
290 static int aesni_cfb_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
291 const unsigned char *in,
size_t len);
293 #define aesni_cfb8_cipher aes_cfb8_cipher
294 static int aesni_cfb8_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
295 const unsigned char *in,
size_t len);
297 #define aesni_cfb1_cipher aes_cfb1_cipher
298 static int aesni_cfb1_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
299 const unsigned char *in,
size_t len);
301 #define aesni_ctr_cipher aes_ctr_cipher
302 static int aesni_ctr_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
303 const unsigned char *in,
size_t len);
305 static int aesni_gcm_init_key(
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
306 const unsigned char *iv,
int enc)
313 aesni_set_encrypt_key(key, ctx->
key_len * 8, &gctx->
ks);
320 if (iv == NULL && gctx->
iv_set)
335 memcpy(gctx->
iv, iv, gctx->
ivlen);
342 #define aesni_gcm_cipher aes_gcm_cipher
343 static int aesni_gcm_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
344 const unsigned char *in,
size_t len);
346 static int aesni_xts_init_key(
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
347 const unsigned char *iv,
int enc)
358 aesni_set_encrypt_key(key, ctx->
key_len * 4, &xctx->
ks1);
360 xctx->
stream = aesni_xts_encrypt;
364 aesni_set_decrypt_key(key, ctx->
key_len * 4, &xctx->
ks1);
366 xctx->
stream = aesni_xts_decrypt;
369 aesni_set_encrypt_key(key + ctx->
key_len/2,
379 memcpy(ctx->
iv, iv, 16);
385 #define aesni_xts_cipher aes_xts_cipher
386 static int aesni_xts_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
387 const unsigned char *in,
size_t len);
389 static int aesni_ccm_init_key(
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
390 const unsigned char *iv,
int enc)
397 aesni_set_encrypt_key(key, ctx->
key_len * 8, &cctx->
ks);
400 cctx->
str = enc?(
ccm128_f)aesni_ccm64_encrypt_blocks :
401 (
ccm128_f)aesni_ccm64_decrypt_blocks;
406 memcpy(ctx->
iv, iv, 15 - cctx->
L);
412 #define aesni_ccm_cipher aes_ccm_cipher
413 static int aesni_ccm_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
414 const unsigned char *in,
size_t len);
416 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
417 static const EVP_CIPHER aesni_##keylen##_##mode = { \
418 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
419 flags|EVP_CIPH_##MODE##_MODE, \
421 aesni_##mode##_cipher, \
423 sizeof(EVP_AES_KEY), \
424 NULL,NULL,NULL,NULL }; \
425 static const EVP_CIPHER aes_##keylen##_##mode = { \
426 nid##_##keylen##_##nmode,blocksize, \
428 flags|EVP_CIPH_##MODE##_MODE, \
430 aes_##mode##_cipher, \
432 sizeof(EVP_AES_KEY), \
433 NULL,NULL,NULL,NULL }; \
434 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
435 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
437 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
438 static const EVP_CIPHER aesni_##keylen##_##mode = { \
439 nid##_##keylen##_##mode,blocksize, \
440 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
441 flags|EVP_CIPH_##MODE##_MODE, \
442 aesni_##mode##_init_key, \
443 aesni_##mode##_cipher, \
444 aes_##mode##_cleanup, \
445 sizeof(EVP_AES_##MODE##_CTX), \
446 NULL,NULL,aes_##mode##_ctrl,NULL }; \
447 static const EVP_CIPHER aes_##keylen##_##mode = { \
448 nid##_##keylen##_##mode,blocksize, \
449 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
450 flags|EVP_CIPH_##MODE##_MODE, \
451 aes_##mode##_init_key, \
452 aes_##mode##_cipher, \
453 aes_##mode##_cleanup, \
454 sizeof(EVP_AES_##MODE##_CTX), \
455 NULL,NULL,aes_##mode##_ctrl,NULL }; \
456 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
457 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
461 #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
462 static const EVP_CIPHER aes_##keylen##_##mode = { \
463 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
464 flags|EVP_CIPH_##MODE##_MODE, \
466 aes_##mode##_cipher, \
468 sizeof(EVP_AES_KEY), \
469 NULL,NULL,NULL,NULL }; \
470 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
471 { return &aes_##keylen##_##mode; }
473 #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
474 static const EVP_CIPHER aes_##keylen##_##mode = { \
475 nid##_##keylen##_##mode,blocksize, \
476 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
477 flags|EVP_CIPH_##MODE##_MODE, \
478 aes_##mode##_init_key, \
479 aes_##mode##_cipher, \
480 aes_##mode##_cleanup, \
481 sizeof(EVP_AES_##MODE##_CTX), \
482 NULL,NULL,aes_##mode##_ctrl,NULL }; \
483 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
484 { return &aes_##keylen##_##mode; }
487 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
488 BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
489 BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
490 BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
491 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
492 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
493 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
494 BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
496 static int aes_init_key(
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
497 const unsigned char *iv,
int enc)
517 ret = vpaes_set_decrypt_key(key,ctx->
key_len*8,&dat->
ks);
545 ret = vpaes_set_encrypt_key(key,ctx->
key_len*8,&dat->
ks);
575 const unsigned char *in,
size_t len)
590 const unsigned char *in,
size_t len)
596 if (len<bl)
return 1;
598 for (i=0,len-=bl;i<=
len;i+=bl)
599 (*dat->
block)(in+i,out+i,&dat->
ks);
605 const unsigned char *in,
size_t len)
615 const unsigned char *in,
size_t len)
624 static int aes_cfb8_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
625 const unsigned char *in,
size_t len)
634 static int aes_cfb1_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
635 const unsigned char *in,
size_t len)
657 static int aes_ctr_cipher (
EVP_CIPHER_CTX *ctx,
unsigned char *out,
658 const unsigned char *in,
size_t len)
660 unsigned int num = ctx->
num;
669 ctx->
num = (size_t)num;
681 if (gctx->
iv != c->iv)
687 static void ctr64_inc(
unsigned char *counter) {
726 if (gctx->
iv != c->
iv)
736 if (arg <= 0 || arg > 16 || c->
encrypt)
738 memcpy(c->
buf, ptr, arg);
745 memcpy(ptr, c->
buf, arg);
752 memcpy(gctx->
iv, ptr, gctx->
ivlen);
759 if ((arg < 4) || (gctx->
ivlen - arg) < 8)
762 memcpy(gctx->
iv, ptr, arg);
773 if (arg <= 0 || arg > gctx->
ivlen)
775 memcpy(ptr, gctx->
iv + gctx->
ivlen - arg, arg);
780 ctr64_inc(gctx->
iv + gctx->
ivlen - 8);
787 memcpy(gctx->
iv + gctx->
ivlen - arg, ptr, arg);
796 memcpy(c->
buf, ptr, arg);
799 unsigned int len=c->
buf[arg-2]<<8|c->
buf[arg-1];
805 c->
buf[arg-2] = len>>8;
806 c->
buf[arg-1] = len & 0xff;
817 static int aes_gcm_init_key(
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
818 const unsigned char *iv,
int enc)
839 vpaes_set_encrypt_key(key,ctx->
key_len*8,&gctx->
ks);
858 if (iv == NULL && gctx->
iv_set)
873 memcpy(gctx->
iv, iv, gctx->
ivlen);
886 static int aes_gcm_tls_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
887 const unsigned char *in,
size_t len)
943 EVP_GCM_TLS_TAG_LEN);
945 if (memcmp(ctx->
buf, in + len, EVP_GCM_TLS_TAG_LEN))
959 static int aes_gcm_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
960 const unsigned char *in,
size_t len)
968 return aes_gcm_tls_cipher(ctx, out, in, len);
1030 #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
1031 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
1032 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1052 static int aes_xts_init_key(
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
1053 const unsigned char *iv,
int enc)
1062 xctx->
stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
1067 #ifdef BSAES_CAPABLE
1069 xctx->
stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
1072 #ifdef VPAES_CAPABLE
1077 vpaes_set_encrypt_key(key, ctx->
key_len * 4, &xctx->
ks1);
1082 vpaes_set_decrypt_key(key, ctx->
key_len * 4, &xctx->
ks1);
1086 vpaes_set_encrypt_key(key + ctx->
key_len/2,
1115 memcpy(ctx->
iv, iv, 16);
1121 static int aes_xts_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
1122 const unsigned char *in,
size_t len)
1132 (len > (1UL<<20)*16))
1139 (*xctx->
stream)(in, out, len,
1147 #define aes_xts_cleanup NULL
1149 #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
1150 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
1155 static
int aes_ccm_ctrl(
EVP_CIPHER_CTX *c,
int type,
int arg,
void *ptr)
1172 if (arg < 2 || arg > 8)
1178 if ((arg & 1) || arg < 4 || arg > 16)
1180 if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
1185 memcpy(c->buf, ptr, arg);
1191 if (!c->encrypt || !cctx->
tag_set)
1206 static int aes_ccm_init_key(
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
1207 const unsigned char *iv,
int enc)
1214 #ifdef VPAES_CAPABLE
1217 vpaes_set_encrypt_key(key, ctx->
key_len*8, &cctx->
ks);
1232 memcpy(ctx->
iv, iv, 15 - cctx->
L);
1238 static int aes_ccm_cipher(
EVP_CIPHER_CTX *ctx,
unsigned char *out,
1239 const unsigned char *in,
size_t len)
1289 unsigned char tag[16];
1292 if (!memcmp(tag, ctx->
buf, cctx->
M))
1306 #define aes_ccm_cleanup NULL