e_aes_cbc_hmac_sha1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* ====================================================================
  2. * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * [email protected].
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. */
  49. #include <openssl/opensslconf.h>
  50. #include <stdio.h>
  51. #include <string.h>
  52. #if !defined(OPENSSL_NO_AES) && !defined(OPENSSL_NO_SHA1)
  53. # include <openssl/evp.h>
  54. # include <openssl/objects.h>
  55. # include <openssl/aes.h>
  56. # include <openssl/sha.h>
  57. # include "evp_locl.h"
  58. # ifndef EVP_CIPH_FLAG_AEAD_CIPHER
  59. # define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
  60. # define EVP_CTRL_AEAD_TLS1_AAD 0x16
  61. # define EVP_CTRL_AEAD_SET_MAC_KEY 0x17
  62. # endif
  63. # if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1)
  64. # define EVP_CIPH_FLAG_DEFAULT_ASN1 0
  65. # endif
  66. # define TLS1_1_VERSION 0x0302
  67. typedef struct {
  68. AES_KEY ks;
  69. SHA_CTX head, tail, md;
  70. size_t payload_length; /* AAD length in decrypt case */
  71. union {
  72. unsigned int tls_ver;
  73. unsigned char tls_aad[16]; /* 13 used */
  74. } aux;
  75. } EVP_AES_HMAC_SHA1;
  76. # define NO_PAYLOAD_LENGTH ((size_t)-1)
  77. # if defined(AES_ASM) && ( \
  78. defined(__x86_64) || defined(__x86_64__) || \
  79. defined(_M_AMD64) || defined(_M_X64) || \
  80. defined(__INTEL__) )
  81. # if defined(__GNUC__) && __GNUC__>=2 && !defined(PEDANTIC)
  82. # define BSWAP(x) ({ unsigned int r=(x); asm ("bswapl %0":"=r"(r):"0"(r)); r; })
  83. # endif
  84. extern unsigned int OPENSSL_ia32cap_P[2];
  85. # define AESNI_CAPABLE (1<<(57-32))
  86. int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
  87. AES_KEY *key);
  88. int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
  89. AES_KEY *key);
  90. void aesni_cbc_encrypt(const unsigned char *in,
  91. unsigned char *out,
  92. size_t length,
  93. const AES_KEY *key, unsigned char *ivec, int enc);
  94. void aesni_cbc_sha1_enc(const void *inp, void *out, size_t blocks,
  95. const AES_KEY *key, unsigned char iv[16],
  96. SHA_CTX *ctx, const void *in0);
  97. # define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data)
  98. static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  99. const unsigned char *inkey,
  100. const unsigned char *iv, int enc)
  101. {
  102. EVP_AES_HMAC_SHA1 *key = data(ctx);
  103. int ret;
  104. if (enc)
  105. ret = aesni_set_encrypt_key(inkey, ctx->key_len * 8, &key->ks);
  106. else
  107. ret = aesni_set_decrypt_key(inkey, ctx->key_len * 8, &key->ks);
  108. SHA1_Init(&key->head); /* handy when benchmarking */
  109. key->tail = key->head;
  110. key->md = key->head;
  111. key->payload_length = NO_PAYLOAD_LENGTH;
  112. return ret < 0 ? 0 : 1;
  113. }
  114. # define STITCHED_CALL
  115. # if !defined(STITCHED_CALL)
  116. # define aes_off 0
  117. # endif
  118. void sha1_block_data_order(void *c, const void *p, size_t len);
  119. static void sha1_update(SHA_CTX *c, const void *data, size_t len)
  120. {
  121. const unsigned char *ptr = data;
  122. size_t res;
  123. if ((res = c->num)) {
  124. res = SHA_CBLOCK - res;
  125. if (len < res)
  126. res = len;
  127. SHA1_Update(c, ptr, res);
  128. ptr += res;
  129. len -= res;
  130. }
  131. res = len % SHA_CBLOCK;
  132. len -= res;
  133. if (len) {
  134. sha1_block_data_order(c, ptr, len / SHA_CBLOCK);
  135. ptr += len;
  136. c->Nh += len >> 29;
  137. c->Nl += len <<= 3;
  138. if (c->Nl < (unsigned int)len)
  139. c->Nh++;
  140. }
  141. if (res)
  142. SHA1_Update(c, ptr, res);
  143. }
  144. # ifdef SHA1_Update
  145. # undef SHA1_Update
  146. # endif
  147. # define SHA1_Update sha1_update
  148. static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  149. const unsigned char *in, size_t len)
  150. {
  151. EVP_AES_HMAC_SHA1 *key = data(ctx);
  152. unsigned int l;
  153. size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
  154. * later */
  155. sha_off = 0;
  156. # if defined(STITCHED_CALL)
  157. size_t aes_off = 0, blocks;
  158. sha_off = SHA_CBLOCK - key->md.num;
  159. # endif
  160. key->payload_length = NO_PAYLOAD_LENGTH;
  161. if (len % AES_BLOCK_SIZE)
  162. return 0;
  163. if (ctx->encrypt) {
  164. if (plen == NO_PAYLOAD_LENGTH)
  165. plen = len;
  166. else if (len !=
  167. ((plen + SHA_DIGEST_LENGTH +
  168. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
  169. return 0;
  170. else if (key->aux.tls_ver >= TLS1_1_VERSION)
  171. iv = AES_BLOCK_SIZE;
  172. # if defined(STITCHED_CALL)
  173. if (plen > (sha_off + iv)
  174. && (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) {
  175. SHA1_Update(&key->md, in + iv, sha_off);
  176. aesni_cbc_sha1_enc(in, out, blocks, &key->ks,
  177. ctx->iv, &key->md, in + iv + sha_off);
  178. blocks *= SHA_CBLOCK;
  179. aes_off += blocks;
  180. sha_off += blocks;
  181. key->md.Nh += blocks >> 29;
  182. key->md.Nl += blocks <<= 3;
  183. if (key->md.Nl < (unsigned int)blocks)
  184. key->md.Nh++;
  185. } else {
  186. sha_off = 0;
  187. }
  188. # endif
  189. sha_off += iv;
  190. SHA1_Update(&key->md, in + sha_off, plen - sha_off);
  191. if (plen != len) { /* "TLS" mode of operation */
  192. if (in != out)
  193. memcpy(out + aes_off, in + aes_off, plen - aes_off);
  194. /* calculate HMAC and append it to payload */
  195. SHA1_Final(out + plen, &key->md);
  196. key->md = key->tail;
  197. SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH);
  198. SHA1_Final(out + plen, &key->md);
  199. /* pad the payload|hmac */
  200. plen += SHA_DIGEST_LENGTH;
  201. for (l = len - plen - 1; plen < len; plen++)
  202. out[plen] = l;
  203. /* encrypt HMAC|padding at once */
  204. aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
  205. &key->ks, ctx->iv, 1);
  206. } else {
  207. aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
  208. &key->ks, ctx->iv, 1);
  209. }
  210. } else {
  211. union {
  212. unsigned int u[SHA_DIGEST_LENGTH / sizeof(unsigned int)];
  213. unsigned char c[32 + SHA_DIGEST_LENGTH];
  214. } mac, *pmac;
  215. /* arrange cache line alignment */
  216. pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32));
  217. /* decrypt HMAC|padding at once */
  218. aesni_cbc_encrypt(in, out, len, &key->ks, ctx->iv, 0);
  219. if (plen) { /* "TLS" mode of operation */
  220. size_t inp_len, mask, j, i;
  221. unsigned int res, maxpad, pad, bitlen;
  222. int ret = 1;
  223. union {
  224. unsigned int u[SHA_LBLOCK];
  225. unsigned char c[SHA_CBLOCK];
  226. } *data = (void *)key->md.data;
  227. if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
  228. >= TLS1_1_VERSION)
  229. iv = AES_BLOCK_SIZE;
  230. if (len < (iv + SHA_DIGEST_LENGTH + 1))
  231. return 0;
  232. /* omit explicit iv */
  233. out += iv;
  234. len -= iv;
  235. /* figure out payload length */
  236. pad = out[len - 1];
  237. maxpad = len - (SHA_DIGEST_LENGTH + 1);
  238. maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
  239. maxpad &= 255;
  240. inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
  241. mask = (0 - ((inp_len - len) >> (sizeof(inp_len) * 8 - 1)));
  242. inp_len &= mask;
  243. ret &= (int)mask;
  244. key->aux.tls_aad[plen - 2] = inp_len >> 8;
  245. key->aux.tls_aad[plen - 1] = inp_len;
  246. /* calculate HMAC */
  247. key->md = key->head;
  248. SHA1_Update(&key->md, key->aux.tls_aad, plen);
  249. # if 1
  250. len -= SHA_DIGEST_LENGTH; /* amend mac */
  251. if (len >= (256 + SHA_CBLOCK)) {
  252. j = (len - (256 + SHA_CBLOCK)) & (0 - SHA_CBLOCK);
  253. j += SHA_CBLOCK - key->md.num;
  254. SHA1_Update(&key->md, out, j);
  255. out += j;
  256. len -= j;
  257. inp_len -= j;
  258. }
  259. /* but pretend as if we hashed padded payload */
  260. bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
  261. # ifdef BSWAP
  262. bitlen = BSWAP(bitlen);
  263. # else
  264. mac.c[0] = 0;
  265. mac.c[1] = (unsigned char)(bitlen >> 16);
  266. mac.c[2] = (unsigned char)(bitlen >> 8);
  267. mac.c[3] = (unsigned char)bitlen;
  268. bitlen = mac.u[0];
  269. # endif
  270. pmac->u[0] = 0;
  271. pmac->u[1] = 0;
  272. pmac->u[2] = 0;
  273. pmac->u[3] = 0;
  274. pmac->u[4] = 0;
  275. for (res = key->md.num, j = 0; j < len; j++) {
  276. size_t c = out[j];
  277. mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
  278. c &= mask;
  279. c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
  280. data->c[res++] = (unsigned char)c;
  281. if (res != SHA_CBLOCK)
  282. continue;
  283. /* j is not incremented yet */
  284. mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
  285. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  286. sha1_block_data_order(&key->md, data, 1);
  287. mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
  288. pmac->u[0] |= key->md.h0 & mask;
  289. pmac->u[1] |= key->md.h1 & mask;
  290. pmac->u[2] |= key->md.h2 & mask;
  291. pmac->u[3] |= key->md.h3 & mask;
  292. pmac->u[4] |= key->md.h4 & mask;
  293. res = 0;
  294. }
  295. for (i = res; i < SHA_CBLOCK; i++, j++)
  296. data->c[i] = 0;
  297. if (res > SHA_CBLOCK - 8) {
  298. mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
  299. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  300. sha1_block_data_order(&key->md, data, 1);
  301. mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  302. pmac->u[0] |= key->md.h0 & mask;
  303. pmac->u[1] |= key->md.h1 & mask;
  304. pmac->u[2] |= key->md.h2 & mask;
  305. pmac->u[3] |= key->md.h3 & mask;
  306. pmac->u[4] |= key->md.h4 & mask;
  307. memset(data, 0, SHA_CBLOCK);
  308. j += 64;
  309. }
  310. data->u[SHA_LBLOCK - 1] = bitlen;
  311. sha1_block_data_order(&key->md, data, 1);
  312. mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  313. pmac->u[0] |= key->md.h0 & mask;
  314. pmac->u[1] |= key->md.h1 & mask;
  315. pmac->u[2] |= key->md.h2 & mask;
  316. pmac->u[3] |= key->md.h3 & mask;
  317. pmac->u[4] |= key->md.h4 & mask;
  318. # ifdef BSWAP
  319. pmac->u[0] = BSWAP(pmac->u[0]);
  320. pmac->u[1] = BSWAP(pmac->u[1]);
  321. pmac->u[2] = BSWAP(pmac->u[2]);
  322. pmac->u[3] = BSWAP(pmac->u[3]);
  323. pmac->u[4] = BSWAP(pmac->u[4]);
  324. # else
  325. for (i = 0; i < 5; i++) {
  326. res = pmac->u[i];
  327. pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
  328. pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
  329. pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
  330. pmac->c[4 * i + 3] = (unsigned char)res;
  331. }
  332. # endif
  333. len += SHA_DIGEST_LENGTH;
  334. # else
  335. SHA1_Update(&key->md, out, inp_len);
  336. res = key->md.num;
  337. SHA1_Final(pmac->c, &key->md);
  338. {
  339. unsigned int inp_blocks, pad_blocks;
  340. /* but pretend as if we hashed padded payload */
  341. inp_blocks =
  342. 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  343. res += (unsigned int)(len - inp_len);
  344. pad_blocks = res / SHA_CBLOCK;
  345. res %= SHA_CBLOCK;
  346. pad_blocks +=
  347. 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  348. for (; inp_blocks < pad_blocks; inp_blocks++)
  349. sha1_block_data_order(&key->md, data, 1);
  350. }
  351. # endif
  352. key->md = key->tail;
  353. SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH);
  354. SHA1_Final(pmac->c, &key->md);
  355. /* verify HMAC */
  356. out += inp_len;
  357. len -= inp_len;
  358. # if 1
  359. {
  360. unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
  361. size_t off = out - p;
  362. unsigned int c, cmask;
  363. maxpad += SHA_DIGEST_LENGTH;
  364. for (res = 0, i = 0, j = 0; j < maxpad; j++) {
  365. c = p[j];
  366. cmask =
  367. ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
  368. 8 - 1);
  369. res |= (c ^ pad) & ~cmask; /* ... and padding */
  370. cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
  371. res |= (c ^ pmac->c[i]) & cmask;
  372. i += 1 & cmask;
  373. }
  374. maxpad -= SHA_DIGEST_LENGTH;
  375. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  376. ret &= (int)~res;
  377. }
  378. # else
  379. for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++)
  380. res |= out[i] ^ pmac->c[i];
  381. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  382. ret &= (int)~res;
  383. /* verify padding */
  384. pad = (pad & ~res) | (maxpad & res);
  385. out = out + len - 1 - pad;
  386. for (res = 0, i = 0; i < pad; i++)
  387. res |= out[i] ^ pad;
  388. res = (0 - res) >> (sizeof(res) * 8 - 1);
  389. ret &= (int)~res;
  390. # endif
  391. return ret;
  392. } else {
  393. SHA1_Update(&key->md, out, len);
  394. }
  395. }
  396. return 1;
  397. }
  398. static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  399. void *ptr)
  400. {
  401. EVP_AES_HMAC_SHA1 *key = data(ctx);
  402. switch (type) {
  403. case EVP_CTRL_AEAD_SET_MAC_KEY:
  404. {
  405. unsigned int i;
  406. unsigned char hmac_key[64];
  407. memset(hmac_key, 0, sizeof(hmac_key));
  408. if (arg > (int)sizeof(hmac_key)) {
  409. SHA1_Init(&key->head);
  410. SHA1_Update(&key->head, ptr, arg);
  411. SHA1_Final(hmac_key, &key->head);
  412. } else {
  413. memcpy(hmac_key, ptr, arg);
  414. }
  415. for (i = 0; i < sizeof(hmac_key); i++)
  416. hmac_key[i] ^= 0x36; /* ipad */
  417. SHA1_Init(&key->head);
  418. SHA1_Update(&key->head, hmac_key, sizeof(hmac_key));
  419. for (i = 0; i < sizeof(hmac_key); i++)
  420. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  421. SHA1_Init(&key->tail);
  422. SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key));
  423. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  424. return 1;
  425. }
  426. case EVP_CTRL_AEAD_TLS1_AAD:
  427. {
  428. unsigned char *p = ptr;
  429. unsigned int len;
  430. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  431. return -1;
  432. len = p[arg - 2] << 8 | p[arg - 1];
  433. if (ctx->encrypt) {
  434. key->payload_length = len;
  435. if ((key->aux.tls_ver =
  436. p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  437. len -= AES_BLOCK_SIZE;
  438. p[arg - 2] = len >> 8;
  439. p[arg - 1] = len;
  440. }
  441. key->md = key->head;
  442. SHA1_Update(&key->md, p, arg);
  443. return (int)(((len + SHA_DIGEST_LENGTH +
  444. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
  445. - len);
  446. } else {
  447. memcpy(key->aux.tls_aad, ptr, arg);
  448. key->payload_length = arg;
  449. return SHA_DIGEST_LENGTH;
  450. }
  451. }
  452. default:
  453. return -1;
  454. }
  455. }
  456. static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = {
  457. # ifdef NID_aes_128_cbc_hmac_sha1
  458. NID_aes_128_cbc_hmac_sha1,
  459. # else
  460. NID_undef,
  461. # endif
  462. 16, 16, 16,
  463. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  464. EVP_CIPH_FLAG_AEAD_CIPHER,
  465. aesni_cbc_hmac_sha1_init_key,
  466. aesni_cbc_hmac_sha1_cipher,
  467. NULL,
  468. sizeof(EVP_AES_HMAC_SHA1),
  469. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  470. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  471. aesni_cbc_hmac_sha1_ctrl,
  472. NULL
  473. };
  474. static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = {
  475. # ifdef NID_aes_256_cbc_hmac_sha1
  476. NID_aes_256_cbc_hmac_sha1,
  477. # else
  478. NID_undef,
  479. # endif
  480. 16, 32, 16,
  481. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  482. EVP_CIPH_FLAG_AEAD_CIPHER,
  483. aesni_cbc_hmac_sha1_init_key,
  484. aesni_cbc_hmac_sha1_cipher,
  485. NULL,
  486. sizeof(EVP_AES_HMAC_SHA1),
  487. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  488. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  489. aesni_cbc_hmac_sha1_ctrl,
  490. NULL
  491. };
  492. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
  493. {
  494. return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
  495. &aesni_128_cbc_hmac_sha1_cipher : NULL);
  496. }
  497. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
  498. {
  499. return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
  500. &aesni_256_cbc_hmac_sha1_cipher : NULL);
  501. }
  502. # else
  503. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
  504. {
  505. return NULL;
  506. }
  507. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
  508. {
  509. return NULL;
  510. }
  511. # endif
  512. #endif