2
0

e_aes_cbc_hmac_sha256.c 31 KB

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