e_aes_cbc_hmac_sha256.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. * licensing@OpenSSL.org.
  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_SHA256)
  53. # include <openssl/evp.h>
  54. # include <openssl/objects.h>
  55. # include <openssl/aes.h>
  56. # include <openssl/sha.h>
  57. # include <openssl/rand.h>
  58. # include "modes_lcl.h"
  59. # include "constant_time_locl.h"
  60. # ifndef EVP_CIPH_FLAG_AEAD_CIPHER
  61. # define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
  62. # define EVP_CTRL_AEAD_TLS1_AAD 0x16
  63. # define EVP_CTRL_AEAD_SET_MAC_KEY 0x17
  64. # endif
  65. # if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1)
  66. # define EVP_CIPH_FLAG_DEFAULT_ASN1 0
  67. # endif
  68. # if !defined(EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)
  69. # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
  70. # endif
  71. # define TLS1_1_VERSION 0x0302
  72. typedef struct {
  73. AES_KEY ks;
  74. SHA256_CTX head, tail, md;
  75. size_t payload_length; /* AAD length in decrypt case */
  76. union {
  77. unsigned int tls_ver;
  78. unsigned char tls_aad[16]; /* 13 used */
  79. } aux;
  80. } EVP_AES_HMAC_SHA256;
  81. # define NO_PAYLOAD_LENGTH ((size_t)-1)
  82. # if defined(AES_ASM) && ( \
  83. defined(__x86_64) || defined(__x86_64__) || \
  84. defined(_M_AMD64) || defined(_M_X64) || \
  85. defined(__INTEL__) )
  86. extern unsigned int OPENSSL_ia32cap_P[];
  87. # define AESNI_CAPABLE (1<<(57-32))
  88. int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
  89. AES_KEY *key);
  90. int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
  91. AES_KEY *key);
  92. void aesni_cbc_encrypt(const unsigned char *in,
  93. unsigned char *out,
  94. size_t length,
  95. const AES_KEY *key, unsigned char *ivec, int enc);
  96. int aesni_cbc_sha256_enc(const void *inp, void *out, size_t blocks,
  97. const AES_KEY *key, unsigned char iv[16],
  98. SHA256_CTX *ctx, const void *in0);
  99. # define data(ctx) ((EVP_AES_HMAC_SHA256 *)(ctx)->cipher_data)
  100. static int aesni_cbc_hmac_sha256_init_key(EVP_CIPHER_CTX *ctx,
  101. const unsigned char *inkey,
  102. const unsigned char *iv, int enc)
  103. {
  104. EVP_AES_HMAC_SHA256 *key = data(ctx);
  105. int ret;
  106. if (enc)
  107. memset(&key->ks, 0, sizeof(key->ks.rd_key)),
  108. ret = aesni_set_encrypt_key(inkey, ctx->key_len * 8, &key->ks);
  109. else
  110. ret = aesni_set_decrypt_key(inkey, ctx->key_len * 8, &key->ks);
  111. SHA256_Init(&key->head); /* handy when benchmarking */
  112. key->tail = key->head;
  113. key->md = key->head;
  114. key->payload_length = NO_PAYLOAD_LENGTH;
  115. return ret < 0 ? 0 : 1;
  116. }
  117. # define STITCHED_CALL
  118. # if !defined(STITCHED_CALL)
  119. # define aes_off 0
  120. # endif
  121. void sha256_block_data_order(void *c, const void *p, size_t len);
  122. static void sha256_update(SHA256_CTX *c, const void *data, size_t len)
  123. {
  124. const unsigned char *ptr = data;
  125. size_t res;
  126. if ((res = c->num)) {
  127. res = SHA256_CBLOCK - res;
  128. if (len < res)
  129. res = len;
  130. SHA256_Update(c, ptr, res);
  131. ptr += res;
  132. len -= res;
  133. }
  134. res = len % SHA256_CBLOCK;
  135. len -= res;
  136. if (len) {
  137. sha256_block_data_order(c, ptr, len / SHA256_CBLOCK);
  138. ptr += len;
  139. c->Nh += len >> 29;
  140. c->Nl += len <<= 3;
  141. if (c->Nl < (unsigned int)len)
  142. c->Nh++;
  143. }
  144. if (res)
  145. SHA256_Update(c, ptr, res);
  146. }
  147. # ifdef SHA256_Update
  148. # undef SHA256_Update
  149. # endif
  150. # define SHA256_Update sha256_update
  151. # if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  152. typedef struct {
  153. unsigned int A[8], B[8], C[8], D[8], E[8], F[8], G[8], H[8];
  154. } SHA256_MB_CTX;
  155. typedef struct {
  156. const unsigned char *ptr;
  157. int blocks;
  158. } HASH_DESC;
  159. void sha256_multi_block(SHA256_MB_CTX *, const HASH_DESC *, int);
  160. typedef struct {
  161. const unsigned char *inp;
  162. unsigned char *out;
  163. int blocks;
  164. u64 iv[2];
  165. } CIPH_DESC;
  166. void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
  167. static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA256 *key,
  168. unsigned char *out,
  169. const unsigned char *inp,
  170. size_t inp_len, int n4x)
  171. { /* n4x is 1 or 2 */
  172. HASH_DESC hash_d[8], edges[8];
  173. CIPH_DESC ciph_d[8];
  174. unsigned char storage[sizeof(SHA256_MB_CTX) + 32];
  175. union {
  176. u64 q[16];
  177. u32 d[32];
  178. u8 c[128];
  179. } blocks[8];
  180. SHA256_MB_CTX *ctx;
  181. unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
  182. 0;
  183. size_t ret = 0;
  184. u8 *IVs;
  185. # if defined(BSWAP8)
  186. u64 seqnum;
  187. # endif
  188. /* ask for IVs in bulk */
  189. if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
  190. return 0;
  191. /* align */
  192. ctx = (SHA256_MB_CTX *) (storage + 32 - ((size_t)storage % 32));
  193. frag = (unsigned int)inp_len >> (1 + n4x);
  194. last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
  195. if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
  196. frag++;
  197. last -= x4 - 1;
  198. }
  199. packlen = 5 + 16 + ((frag + 32 + 16) & -16);
  200. /* populate descriptors with pointers and IVs */
  201. hash_d[0].ptr = inp;
  202. ciph_d[0].inp = inp;
  203. /* 5+16 is place for header and explicit IV */
  204. ciph_d[0].out = out + 5 + 16;
  205. memcpy(ciph_d[0].out - 16, IVs, 16);
  206. memcpy(ciph_d[0].iv, IVs, 16);
  207. IVs += 16;
  208. for (i = 1; i < x4; i++) {
  209. ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
  210. ciph_d[i].out = ciph_d[i - 1].out + packlen;
  211. memcpy(ciph_d[i].out - 16, IVs, 16);
  212. memcpy(ciph_d[i].iv, IVs, 16);
  213. IVs += 16;
  214. }
  215. # if defined(BSWAP8)
  216. memcpy(blocks[0].c, key->md.data, 8);
  217. seqnum = BSWAP8(blocks[0].q[0]);
  218. # endif
  219. for (i = 0; i < x4; i++) {
  220. unsigned int len = (i == (x4 - 1) ? last : frag);
  221. # if !defined(BSWAP8)
  222. unsigned int carry, j;
  223. # endif
  224. ctx->A[i] = key->md.h[0];
  225. ctx->B[i] = key->md.h[1];
  226. ctx->C[i] = key->md.h[2];
  227. ctx->D[i] = key->md.h[3];
  228. ctx->E[i] = key->md.h[4];
  229. ctx->F[i] = key->md.h[5];
  230. ctx->G[i] = key->md.h[6];
  231. ctx->H[i] = key->md.h[7];
  232. /* fix seqnum */
  233. # if defined(BSWAP8)
  234. blocks[i].q[0] = BSWAP8(seqnum + i);
  235. # else
  236. for (carry = i, j = 8; j--;) {
  237. blocks[i].c[j] = ((u8 *)key->md.data)[j] + carry;
  238. carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
  239. }
  240. # endif
  241. blocks[i].c[8] = ((u8 *)key->md.data)[8];
  242. blocks[i].c[9] = ((u8 *)key->md.data)[9];
  243. blocks[i].c[10] = ((u8 *)key->md.data)[10];
  244. /* fix length */
  245. blocks[i].c[11] = (u8)(len >> 8);
  246. blocks[i].c[12] = (u8)(len);
  247. memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
  248. hash_d[i].ptr += 64 - 13;
  249. hash_d[i].blocks = (len - (64 - 13)) / 64;
  250. edges[i].ptr = blocks[i].c;
  251. edges[i].blocks = 1;
  252. }
  253. /* hash 13-byte headers and first 64-13 bytes of inputs */
  254. sha256_multi_block(ctx, edges, n4x);
  255. /* hash bulk inputs */
  256. # define MAXCHUNKSIZE 2048
  257. # if MAXCHUNKSIZE%64
  258. # error "MAXCHUNKSIZE is not divisible by 64"
  259. # elif MAXCHUNKSIZE
  260. /*
  261. * goal is to minimize pressure on L1 cache by moving in shorter steps,
  262. * so that hashed data is still in the cache by the time we encrypt it
  263. */
  264. minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
  265. if (minblocks > MAXCHUNKSIZE / 64) {
  266. for (i = 0; i < x4; i++) {
  267. edges[i].ptr = hash_d[i].ptr;
  268. edges[i].blocks = MAXCHUNKSIZE / 64;
  269. ciph_d[i].blocks = MAXCHUNKSIZE / 16;
  270. }
  271. do {
  272. sha256_multi_block(ctx, edges, n4x);
  273. aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
  274. for (i = 0; i < x4; i++) {
  275. edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
  276. hash_d[i].blocks -= MAXCHUNKSIZE / 64;
  277. edges[i].blocks = MAXCHUNKSIZE / 64;
  278. ciph_d[i].inp += MAXCHUNKSIZE;
  279. ciph_d[i].out += MAXCHUNKSIZE;
  280. ciph_d[i].blocks = MAXCHUNKSIZE / 16;
  281. memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
  282. }
  283. processed += MAXCHUNKSIZE;
  284. minblocks -= MAXCHUNKSIZE / 64;
  285. } while (minblocks > MAXCHUNKSIZE / 64);
  286. }
  287. # endif
  288. # undef MAXCHUNKSIZE
  289. sha256_multi_block(ctx, hash_d, n4x);
  290. memset(blocks, 0, sizeof(blocks));
  291. for (i = 0; i < x4; i++) {
  292. unsigned int len = (i == (x4 - 1) ? last : frag),
  293. off = hash_d[i].blocks * 64;
  294. const unsigned char *ptr = hash_d[i].ptr + off;
  295. off = (len - processed) - (64 - 13) - off; /* remainder actually */
  296. memcpy(blocks[i].c, ptr, off);
  297. blocks[i].c[off] = 0x80;
  298. len += 64 + 13; /* 64 is HMAC header */
  299. len *= 8; /* convert to bits */
  300. if (off < (64 - 8)) {
  301. # ifdef BSWAP4
  302. blocks[i].d[15] = BSWAP4(len);
  303. # else
  304. PUTU32(blocks[i].c + 60, len);
  305. # endif
  306. edges[i].blocks = 1;
  307. } else {
  308. # ifdef BSWAP4
  309. blocks[i].d[31] = BSWAP4(len);
  310. # else
  311. PUTU32(blocks[i].c + 124, len);
  312. # endif
  313. edges[i].blocks = 2;
  314. }
  315. edges[i].ptr = blocks[i].c;
  316. }
  317. /* hash input tails and finalize */
  318. sha256_multi_block(ctx, edges, n4x);
  319. memset(blocks, 0, sizeof(blocks));
  320. for (i = 0; i < x4; i++) {
  321. # ifdef BSWAP4
  322. blocks[i].d[0] = BSWAP4(ctx->A[i]);
  323. ctx->A[i] = key->tail.h[0];
  324. blocks[i].d[1] = BSWAP4(ctx->B[i]);
  325. ctx->B[i] = key->tail.h[1];
  326. blocks[i].d[2] = BSWAP4(ctx->C[i]);
  327. ctx->C[i] = key->tail.h[2];
  328. blocks[i].d[3] = BSWAP4(ctx->D[i]);
  329. ctx->D[i] = key->tail.h[3];
  330. blocks[i].d[4] = BSWAP4(ctx->E[i]);
  331. ctx->E[i] = key->tail.h[4];
  332. blocks[i].d[5] = BSWAP4(ctx->F[i]);
  333. ctx->F[i] = key->tail.h[5];
  334. blocks[i].d[6] = BSWAP4(ctx->G[i]);
  335. ctx->G[i] = key->tail.h[6];
  336. blocks[i].d[7] = BSWAP4(ctx->H[i]);
  337. ctx->H[i] = key->tail.h[7];
  338. blocks[i].c[32] = 0x80;
  339. blocks[i].d[15] = BSWAP4((64 + 32) * 8);
  340. # else
  341. PUTU32(blocks[i].c + 0, ctx->A[i]);
  342. ctx->A[i] = key->tail.h[0];
  343. PUTU32(blocks[i].c + 4, ctx->B[i]);
  344. ctx->B[i] = key->tail.h[1];
  345. PUTU32(blocks[i].c + 8, ctx->C[i]);
  346. ctx->C[i] = key->tail.h[2];
  347. PUTU32(blocks[i].c + 12, ctx->D[i]);
  348. ctx->D[i] = key->tail.h[3];
  349. PUTU32(blocks[i].c + 16, ctx->E[i]);
  350. ctx->E[i] = key->tail.h[4];
  351. PUTU32(blocks[i].c + 20, ctx->F[i]);
  352. ctx->F[i] = key->tail.h[5];
  353. PUTU32(blocks[i].c + 24, ctx->G[i]);
  354. ctx->G[i] = key->tail.h[6];
  355. PUTU32(blocks[i].c + 28, ctx->H[i]);
  356. ctx->H[i] = key->tail.h[7];
  357. blocks[i].c[32] = 0x80;
  358. PUTU32(blocks[i].c + 60, (64 + 32) * 8);
  359. # endif
  360. edges[i].ptr = blocks[i].c;
  361. edges[i].blocks = 1;
  362. }
  363. /* finalize MACs */
  364. sha256_multi_block(ctx, edges, n4x);
  365. for (i = 0; i < x4; i++) {
  366. unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
  367. unsigned char *out0 = out;
  368. memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
  369. ciph_d[i].inp = ciph_d[i].out;
  370. out += 5 + 16 + len;
  371. /* write MAC */
  372. PUTU32(out + 0, ctx->A[i]);
  373. PUTU32(out + 4, ctx->B[i]);
  374. PUTU32(out + 8, ctx->C[i]);
  375. PUTU32(out + 12, ctx->D[i]);
  376. PUTU32(out + 16, ctx->E[i]);
  377. PUTU32(out + 20, ctx->F[i]);
  378. PUTU32(out + 24, ctx->G[i]);
  379. PUTU32(out + 28, ctx->H[i]);
  380. out += 32;
  381. len += 32;
  382. /* pad */
  383. pad = 15 - len % 16;
  384. for (j = 0; j <= pad; j++)
  385. *(out++) = pad;
  386. len += pad + 1;
  387. ciph_d[i].blocks = (len - processed) / 16;
  388. len += 16; /* account for explicit iv */
  389. /* arrange header */
  390. out0[0] = ((u8 *)key->md.data)[8];
  391. out0[1] = ((u8 *)key->md.data)[9];
  392. out0[2] = ((u8 *)key->md.data)[10];
  393. out0[3] = (u8)(len >> 8);
  394. out0[4] = (u8)(len);
  395. ret += len + 5;
  396. inp += frag;
  397. }
  398. aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
  399. OPENSSL_cleanse(blocks, sizeof(blocks));
  400. OPENSSL_cleanse(ctx, sizeof(*ctx));
  401. return ret;
  402. }
  403. # endif
  404. static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx,
  405. unsigned char *out,
  406. const unsigned char *in, size_t len)
  407. {
  408. EVP_AES_HMAC_SHA256 *key = data(ctx);
  409. unsigned int l;
  410. size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
  411. * later */
  412. sha_off = 0;
  413. # if defined(STITCHED_CALL)
  414. size_t aes_off = 0, blocks;
  415. sha_off = SHA256_CBLOCK - key->md.num;
  416. # endif
  417. key->payload_length = NO_PAYLOAD_LENGTH;
  418. if (len % AES_BLOCK_SIZE)
  419. return 0;
  420. if (ctx->encrypt) {
  421. if (plen == NO_PAYLOAD_LENGTH)
  422. plen = len;
  423. else if (len !=
  424. ((plen + SHA256_DIGEST_LENGTH +
  425. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
  426. return 0;
  427. else if (key->aux.tls_ver >= TLS1_1_VERSION)
  428. iv = AES_BLOCK_SIZE;
  429. # if defined(STITCHED_CALL)
  430. /*
  431. * Assembly stitch handles AVX-capable processors, but its
  432. * performance is not optimal on AMD Jaguar, ~40% worse, for
  433. * unknown reasons. Incidentally processor in question supports
  434. * AVX, but not AMD-specific XOP extension, which can be used
  435. * to identify it and avoid stitch invocation. So that after we
  436. * establish that current CPU supports AVX, we even see if it's
  437. * either even XOP-capable Bulldozer-based or GenuineIntel one.
  438. */
  439. if (OPENSSL_ia32cap_P[1] & (1 << (60 - 32)) && /* AVX? */
  440. ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
  441. | (OPENSSL_ia32cap_P[0] & (1<<30))) && /* "Intel CPU"? */
  442. plen > (sha_off + iv) &&
  443. (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
  444. SHA256_Update(&key->md, in + iv, sha_off);
  445. (void)aesni_cbc_sha256_enc(in, out, blocks, &key->ks,
  446. ctx->iv, &key->md, in + iv + sha_off);
  447. blocks *= SHA256_CBLOCK;
  448. aes_off += blocks;
  449. sha_off += blocks;
  450. key->md.Nh += blocks >> 29;
  451. key->md.Nl += blocks <<= 3;
  452. if (key->md.Nl < (unsigned int)blocks)
  453. key->md.Nh++;
  454. } else {
  455. sha_off = 0;
  456. }
  457. # endif
  458. sha_off += iv;
  459. SHA256_Update(&key->md, in + sha_off, plen - sha_off);
  460. if (plen != len) { /* "TLS" mode of operation */
  461. if (in != out)
  462. memcpy(out + aes_off, in + aes_off, plen - aes_off);
  463. /* calculate HMAC and append it to payload */
  464. SHA256_Final(out + plen, &key->md);
  465. key->md = key->tail;
  466. SHA256_Update(&key->md, out + plen, SHA256_DIGEST_LENGTH);
  467. SHA256_Final(out + plen, &key->md);
  468. /* pad the payload|hmac */
  469. plen += SHA256_DIGEST_LENGTH;
  470. for (l = len - plen - 1; plen < len; plen++)
  471. out[plen] = l;
  472. /* encrypt HMAC|padding at once */
  473. aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
  474. &key->ks, ctx->iv, 1);
  475. } else {
  476. aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
  477. &key->ks, ctx->iv, 1);
  478. }
  479. } else {
  480. union {
  481. unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
  482. unsigned char c[64 + SHA256_DIGEST_LENGTH];
  483. } mac, *pmac;
  484. /* arrange cache line alignment */
  485. pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
  486. /* decrypt HMAC|padding at once */
  487. aesni_cbc_encrypt(in, out, len, &key->ks, ctx->iv, 0);
  488. if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
  489. size_t inp_len, mask, j, i;
  490. unsigned int res, maxpad, pad, bitlen;
  491. int ret = 1;
  492. union {
  493. unsigned int u[SHA_LBLOCK];
  494. unsigned char c[SHA256_CBLOCK];
  495. } *data = (void *)key->md.data;
  496. if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
  497. >= TLS1_1_VERSION)
  498. iv = AES_BLOCK_SIZE;
  499. if (len < (iv + SHA256_DIGEST_LENGTH + 1))
  500. return 0;
  501. /* omit explicit iv */
  502. out += iv;
  503. len -= iv;
  504. /* figure out payload length */
  505. pad = out[len - 1];
  506. maxpad = len - (SHA256_DIGEST_LENGTH + 1);
  507. maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
  508. maxpad &= 255;
  509. ret &= constant_time_ge(maxpad, pad);
  510. inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
  511. mask = (0 - ((inp_len - len) >> (sizeof(inp_len) * 8 - 1)));
  512. inp_len &= mask;
  513. ret &= (int)mask;
  514. key->aux.tls_aad[plen - 2] = inp_len >> 8;
  515. key->aux.tls_aad[plen - 1] = inp_len;
  516. /* calculate HMAC */
  517. key->md = key->head;
  518. SHA256_Update(&key->md, key->aux.tls_aad, plen);
  519. # if 1
  520. len -= SHA256_DIGEST_LENGTH; /* amend mac */
  521. if (len >= (256 + SHA256_CBLOCK)) {
  522. j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
  523. j += SHA256_CBLOCK - key->md.num;
  524. SHA256_Update(&key->md, out, j);
  525. out += j;
  526. len -= j;
  527. inp_len -= j;
  528. }
  529. /* but pretend as if we hashed padded payload */
  530. bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
  531. # ifdef BSWAP4
  532. bitlen = BSWAP4(bitlen);
  533. # else
  534. mac.c[0] = 0;
  535. mac.c[1] = (unsigned char)(bitlen >> 16);
  536. mac.c[2] = (unsigned char)(bitlen >> 8);
  537. mac.c[3] = (unsigned char)bitlen;
  538. bitlen = mac.u[0];
  539. # endif
  540. pmac->u[0] = 0;
  541. pmac->u[1] = 0;
  542. pmac->u[2] = 0;
  543. pmac->u[3] = 0;
  544. pmac->u[4] = 0;
  545. pmac->u[5] = 0;
  546. pmac->u[6] = 0;
  547. pmac->u[7] = 0;
  548. for (res = key->md.num, j = 0; j < len; j++) {
  549. size_t c = out[j];
  550. mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
  551. c &= mask;
  552. c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
  553. data->c[res++] = (unsigned char)c;
  554. if (res != SHA256_CBLOCK)
  555. continue;
  556. /* j is not incremented yet */
  557. mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
  558. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  559. sha256_block_data_order(&key->md, data, 1);
  560. mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
  561. pmac->u[0] |= key->md.h[0] & mask;
  562. pmac->u[1] |= key->md.h[1] & mask;
  563. pmac->u[2] |= key->md.h[2] & mask;
  564. pmac->u[3] |= key->md.h[3] & mask;
  565. pmac->u[4] |= key->md.h[4] & mask;
  566. pmac->u[5] |= key->md.h[5] & mask;
  567. pmac->u[6] |= key->md.h[6] & mask;
  568. pmac->u[7] |= key->md.h[7] & mask;
  569. res = 0;
  570. }
  571. for (i = res; i < SHA256_CBLOCK; i++, j++)
  572. data->c[i] = 0;
  573. if (res > SHA256_CBLOCK - 8) {
  574. mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
  575. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  576. sha256_block_data_order(&key->md, data, 1);
  577. mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  578. pmac->u[0] |= key->md.h[0] & mask;
  579. pmac->u[1] |= key->md.h[1] & mask;
  580. pmac->u[2] |= key->md.h[2] & mask;
  581. pmac->u[3] |= key->md.h[3] & mask;
  582. pmac->u[4] |= key->md.h[4] & mask;
  583. pmac->u[5] |= key->md.h[5] & mask;
  584. pmac->u[6] |= key->md.h[6] & mask;
  585. pmac->u[7] |= key->md.h[7] & mask;
  586. memset(data, 0, SHA256_CBLOCK);
  587. j += 64;
  588. }
  589. data->u[SHA_LBLOCK - 1] = bitlen;
  590. sha256_block_data_order(&key->md, data, 1);
  591. mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  592. pmac->u[0] |= key->md.h[0] & mask;
  593. pmac->u[1] |= key->md.h[1] & mask;
  594. pmac->u[2] |= key->md.h[2] & mask;
  595. pmac->u[3] |= key->md.h[3] & mask;
  596. pmac->u[4] |= key->md.h[4] & mask;
  597. pmac->u[5] |= key->md.h[5] & mask;
  598. pmac->u[6] |= key->md.h[6] & mask;
  599. pmac->u[7] |= key->md.h[7] & mask;
  600. # ifdef BSWAP4
  601. pmac->u[0] = BSWAP4(pmac->u[0]);
  602. pmac->u[1] = BSWAP4(pmac->u[1]);
  603. pmac->u[2] = BSWAP4(pmac->u[2]);
  604. pmac->u[3] = BSWAP4(pmac->u[3]);
  605. pmac->u[4] = BSWAP4(pmac->u[4]);
  606. pmac->u[5] = BSWAP4(pmac->u[5]);
  607. pmac->u[6] = BSWAP4(pmac->u[6]);
  608. pmac->u[7] = BSWAP4(pmac->u[7]);
  609. # else
  610. for (i = 0; i < 8; i++) {
  611. res = pmac->u[i];
  612. pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
  613. pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
  614. pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
  615. pmac->c[4 * i + 3] = (unsigned char)res;
  616. }
  617. # endif
  618. len += SHA256_DIGEST_LENGTH;
  619. # else
  620. SHA256_Update(&key->md, out, inp_len);
  621. res = key->md.num;
  622. SHA256_Final(pmac->c, &key->md);
  623. {
  624. unsigned int inp_blocks, pad_blocks;
  625. /* but pretend as if we hashed padded payload */
  626. inp_blocks =
  627. 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  628. res += (unsigned int)(len - inp_len);
  629. pad_blocks = res / SHA256_CBLOCK;
  630. res %= SHA256_CBLOCK;
  631. pad_blocks +=
  632. 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  633. for (; inp_blocks < pad_blocks; inp_blocks++)
  634. sha1_block_data_order(&key->md, data, 1);
  635. }
  636. # endif
  637. key->md = key->tail;
  638. SHA256_Update(&key->md, pmac->c, SHA256_DIGEST_LENGTH);
  639. SHA256_Final(pmac->c, &key->md);
  640. /* verify HMAC */
  641. out += inp_len;
  642. len -= inp_len;
  643. # if 1
  644. {
  645. unsigned char *p =
  646. out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
  647. size_t off = out - p;
  648. unsigned int c, cmask;
  649. maxpad += SHA256_DIGEST_LENGTH;
  650. for (res = 0, i = 0, j = 0; j < maxpad; j++) {
  651. c = p[j];
  652. cmask =
  653. ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
  654. (sizeof(int) * 8 - 1);
  655. res |= (c ^ pad) & ~cmask; /* ... and padding */
  656. cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
  657. res |= (c ^ pmac->c[i]) & cmask;
  658. i += 1 & cmask;
  659. }
  660. maxpad -= SHA256_DIGEST_LENGTH;
  661. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  662. ret &= (int)~res;
  663. }
  664. # else
  665. for (res = 0, i = 0; i < SHA256_DIGEST_LENGTH; i++)
  666. res |= out[i] ^ pmac->c[i];
  667. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  668. ret &= (int)~res;
  669. /* verify padding */
  670. pad = (pad & ~res) | (maxpad & res);
  671. out = out + len - 1 - pad;
  672. for (res = 0, i = 0; i < pad; i++)
  673. res |= out[i] ^ pad;
  674. res = (0 - res) >> (sizeof(res) * 8 - 1);
  675. ret &= (int)~res;
  676. # endif
  677. return ret;
  678. } else {
  679. SHA256_Update(&key->md, out, len);
  680. }
  681. }
  682. return 1;
  683. }
  684. static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  685. void *ptr)
  686. {
  687. EVP_AES_HMAC_SHA256 *key = data(ctx);
  688. switch (type) {
  689. case EVP_CTRL_AEAD_SET_MAC_KEY:
  690. {
  691. unsigned int i;
  692. unsigned char hmac_key[64];
  693. memset(hmac_key, 0, sizeof(hmac_key));
  694. if (arg > (int)sizeof(hmac_key)) {
  695. SHA256_Init(&key->head);
  696. SHA256_Update(&key->head, ptr, arg);
  697. SHA256_Final(hmac_key, &key->head);
  698. } else {
  699. memcpy(hmac_key, ptr, arg);
  700. }
  701. for (i = 0; i < sizeof(hmac_key); i++)
  702. hmac_key[i] ^= 0x36; /* ipad */
  703. SHA256_Init(&key->head);
  704. SHA256_Update(&key->head, hmac_key, sizeof(hmac_key));
  705. for (i = 0; i < sizeof(hmac_key); i++)
  706. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  707. SHA256_Init(&key->tail);
  708. SHA256_Update(&key->tail, hmac_key, sizeof(hmac_key));
  709. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  710. return 1;
  711. }
  712. case EVP_CTRL_AEAD_TLS1_AAD:
  713. {
  714. unsigned char *p = ptr;
  715. unsigned int len;
  716. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  717. return -1;
  718. len = p[arg - 2] << 8 | p[arg - 1];
  719. if (ctx->encrypt) {
  720. key->payload_length = len;
  721. if ((key->aux.tls_ver =
  722. p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  723. if (len < AES_BLOCK_SIZE)
  724. return 0;
  725. len -= AES_BLOCK_SIZE;
  726. p[arg - 2] = len >> 8;
  727. p[arg - 1] = len;
  728. }
  729. key->md = key->head;
  730. SHA256_Update(&key->md, p, arg);
  731. return (int)(((len + SHA256_DIGEST_LENGTH +
  732. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
  733. - len);
  734. } else {
  735. memcpy(key->aux.tls_aad, ptr, arg);
  736. key->payload_length = arg;
  737. return SHA256_DIGEST_LENGTH;
  738. }
  739. }
  740. # if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  741. case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
  742. return (int)(5 + 16 + ((arg + 32 + 16) & -16));
  743. case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
  744. {
  745. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  746. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  747. unsigned int n4x = 1, x4;
  748. unsigned int frag, last, packlen, inp_len;
  749. if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
  750. return -1;
  751. inp_len = param->inp[11] << 8 | param->inp[12];
  752. if (ctx->encrypt) {
  753. if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
  754. return -1;
  755. if (inp_len) {
  756. if (inp_len < 4096)
  757. return 0; /* too short */
  758. if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
  759. n4x = 2; /* AVX2 */
  760. } else if ((n4x = param->interleave / 4) && n4x <= 2)
  761. inp_len = param->len;
  762. else
  763. return -1;
  764. key->md = key->head;
  765. SHA256_Update(&key->md, param->inp, 13);
  766. x4 = 4 * n4x;
  767. n4x += 1;
  768. frag = inp_len >> n4x;
  769. last = inp_len + frag - (frag << n4x);
  770. if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
  771. frag++;
  772. last -= x4 - 1;
  773. }
  774. packlen = 5 + 16 + ((frag + 32 + 16) & -16);
  775. packlen = (packlen << n4x) - packlen;
  776. packlen += 5 + 16 + ((last + 32 + 16) & -16);
  777. param->interleave = x4;
  778. return (int)packlen;
  779. } else
  780. return -1; /* not yet */
  781. }
  782. case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
  783. {
  784. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  785. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  786. return (int)tls1_1_multi_block_encrypt(key, param->out,
  787. param->inp, param->len,
  788. param->interleave / 4);
  789. }
  790. case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
  791. # endif
  792. default:
  793. return -1;
  794. }
  795. }
  796. static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher = {
  797. # ifdef NID_aes_128_cbc_hmac_sha256
  798. NID_aes_128_cbc_hmac_sha256,
  799. # else
  800. NID_undef,
  801. # endif
  802. 16, 16, 16,
  803. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  804. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  805. aesni_cbc_hmac_sha256_init_key,
  806. aesni_cbc_hmac_sha256_cipher,
  807. NULL,
  808. sizeof(EVP_AES_HMAC_SHA256),
  809. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  810. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  811. aesni_cbc_hmac_sha256_ctrl,
  812. NULL
  813. };
  814. static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher = {
  815. # ifdef NID_aes_256_cbc_hmac_sha256
  816. NID_aes_256_cbc_hmac_sha256,
  817. # else
  818. NID_undef,
  819. # endif
  820. 16, 32, 16,
  821. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  822. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  823. aesni_cbc_hmac_sha256_init_key,
  824. aesni_cbc_hmac_sha256_cipher,
  825. NULL,
  826. sizeof(EVP_AES_HMAC_SHA256),
  827. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  828. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  829. aesni_cbc_hmac_sha256_ctrl,
  830. NULL
  831. };
  832. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
  833. {
  834. return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
  835. aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
  836. &aesni_128_cbc_hmac_sha256_cipher : NULL);
  837. }
  838. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
  839. {
  840. return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
  841. aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
  842. &aesni_256_cbc_hmac_sha256_cipher : NULL);
  843. }
  844. # else
  845. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
  846. {
  847. return NULL;
  848. }
  849. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
  850. {
  851. return NULL;
  852. }
  853. # endif
  854. #endif