e_aes_cbc_hmac_sha256.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. */
  389. if (OPENSSL_ia32cap_P[1] & (1 << (60 - 32)) && /* AVX? */
  390. ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
  391. | (OPENSSL_ia32cap_P[0] & (1<<30))) && /* "Intel CPU"? */
  392. plen > (sha_off + iv) &&
  393. (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
  394. SHA256_Update(&key->md, in + iv, sha_off);
  395. (void)aesni_cbc_sha256_enc(in, out, blocks, &key->ks,
  396. EVP_CIPHER_CTX_iv_noconst(ctx),
  397. &key->md, in + iv + sha_off);
  398. blocks *= SHA256_CBLOCK;
  399. aes_off += blocks;
  400. sha_off += blocks;
  401. key->md.Nh += blocks >> 29;
  402. key->md.Nl += blocks <<= 3;
  403. if (key->md.Nl < (unsigned int)blocks)
  404. key->md.Nh++;
  405. } else {
  406. sha_off = 0;
  407. }
  408. # endif
  409. sha_off += iv;
  410. SHA256_Update(&key->md, in + sha_off, plen - sha_off);
  411. if (plen != len) { /* "TLS" mode of operation */
  412. if (in != out)
  413. memcpy(out + aes_off, in + aes_off, plen - aes_off);
  414. /* calculate HMAC and append it to payload */
  415. SHA256_Final(out + plen, &key->md);
  416. key->md = key->tail;
  417. SHA256_Update(&key->md, out + plen, SHA256_DIGEST_LENGTH);
  418. SHA256_Final(out + plen, &key->md);
  419. /* pad the payload|hmac */
  420. plen += SHA256_DIGEST_LENGTH;
  421. for (l = len - plen - 1; plen < len; plen++)
  422. out[plen] = l;
  423. /* encrypt HMAC|padding at once */
  424. aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
  425. &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
  426. } else {
  427. aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
  428. &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
  429. }
  430. } else {
  431. union {
  432. unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
  433. unsigned char c[64 + SHA256_DIGEST_LENGTH];
  434. } mac, *pmac;
  435. /* arrange cache line alignment */
  436. pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
  437. /* decrypt HMAC|padding at once */
  438. aesni_cbc_encrypt(in, out, len, &key->ks,
  439. EVP_CIPHER_CTX_iv_noconst(ctx), 0);
  440. if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
  441. size_t inp_len, mask, j, i;
  442. unsigned int res, maxpad, pad, bitlen;
  443. int ret = 1;
  444. union {
  445. unsigned int u[SHA_LBLOCK];
  446. unsigned char c[SHA256_CBLOCK];
  447. } *data = (void *)key->md.data;
  448. if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
  449. >= TLS1_1_VERSION)
  450. iv = AES_BLOCK_SIZE;
  451. if (len < (iv + SHA256_DIGEST_LENGTH + 1))
  452. return 0;
  453. /* omit explicit iv */
  454. out += iv;
  455. len -= iv;
  456. /* figure out payload length */
  457. pad = out[len - 1];
  458. maxpad = len - (SHA256_DIGEST_LENGTH + 1);
  459. maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
  460. maxpad &= 255;
  461. ret &= constant_time_ge(maxpad, pad);
  462. inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
  463. mask = (0 - ((inp_len - len) >> (sizeof(inp_len) * 8 - 1)));
  464. inp_len &= mask;
  465. ret &= (int)mask;
  466. key->aux.tls_aad[plen - 2] = inp_len >> 8;
  467. key->aux.tls_aad[plen - 1] = inp_len;
  468. /* calculate HMAC */
  469. key->md = key->head;
  470. SHA256_Update(&key->md, key->aux.tls_aad, plen);
  471. # if 1 /* see original reference version in #else */
  472. len -= SHA256_DIGEST_LENGTH; /* amend mac */
  473. if (len >= (256 + SHA256_CBLOCK)) {
  474. j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
  475. j += SHA256_CBLOCK - key->md.num;
  476. SHA256_Update(&key->md, out, j);
  477. out += j;
  478. len -= j;
  479. inp_len -= j;
  480. }
  481. /* but pretend as if we hashed padded payload */
  482. bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
  483. # ifdef BSWAP4
  484. bitlen = BSWAP4(bitlen);
  485. # else
  486. mac.c[0] = 0;
  487. mac.c[1] = (unsigned char)(bitlen >> 16);
  488. mac.c[2] = (unsigned char)(bitlen >> 8);
  489. mac.c[3] = (unsigned char)bitlen;
  490. bitlen = mac.u[0];
  491. # endif
  492. pmac->u[0] = 0;
  493. pmac->u[1] = 0;
  494. pmac->u[2] = 0;
  495. pmac->u[3] = 0;
  496. pmac->u[4] = 0;
  497. pmac->u[5] = 0;
  498. pmac->u[6] = 0;
  499. pmac->u[7] = 0;
  500. for (res = key->md.num, j = 0; j < len; j++) {
  501. size_t c = out[j];
  502. mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
  503. c &= mask;
  504. c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
  505. data->c[res++] = (unsigned char)c;
  506. if (res != SHA256_CBLOCK)
  507. continue;
  508. /* j is not incremented yet */
  509. mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
  510. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  511. sha256_block_data_order(&key->md, data, 1);
  512. mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
  513. pmac->u[0] |= key->md.h[0] & mask;
  514. pmac->u[1] |= key->md.h[1] & mask;
  515. pmac->u[2] |= key->md.h[2] & mask;
  516. pmac->u[3] |= key->md.h[3] & mask;
  517. pmac->u[4] |= key->md.h[4] & mask;
  518. pmac->u[5] |= key->md.h[5] & mask;
  519. pmac->u[6] |= key->md.h[6] & mask;
  520. pmac->u[7] |= key->md.h[7] & mask;
  521. res = 0;
  522. }
  523. for (i = res; i < SHA256_CBLOCK; i++, j++)
  524. data->c[i] = 0;
  525. if (res > SHA256_CBLOCK - 8) {
  526. mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
  527. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  528. sha256_block_data_order(&key->md, data, 1);
  529. mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  530. pmac->u[0] |= key->md.h[0] & mask;
  531. pmac->u[1] |= key->md.h[1] & mask;
  532. pmac->u[2] |= key->md.h[2] & mask;
  533. pmac->u[3] |= key->md.h[3] & mask;
  534. pmac->u[4] |= key->md.h[4] & mask;
  535. pmac->u[5] |= key->md.h[5] & mask;
  536. pmac->u[6] |= key->md.h[6] & mask;
  537. pmac->u[7] |= key->md.h[7] & mask;
  538. memset(data, 0, SHA256_CBLOCK);
  539. j += 64;
  540. }
  541. data->u[SHA_LBLOCK - 1] = bitlen;
  542. sha256_block_data_order(&key->md, data, 1);
  543. mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  544. pmac->u[0] |= key->md.h[0] & mask;
  545. pmac->u[1] |= key->md.h[1] & mask;
  546. pmac->u[2] |= key->md.h[2] & mask;
  547. pmac->u[3] |= key->md.h[3] & mask;
  548. pmac->u[4] |= key->md.h[4] & mask;
  549. pmac->u[5] |= key->md.h[5] & mask;
  550. pmac->u[6] |= key->md.h[6] & mask;
  551. pmac->u[7] |= key->md.h[7] & mask;
  552. # ifdef BSWAP4
  553. pmac->u[0] = BSWAP4(pmac->u[0]);
  554. pmac->u[1] = BSWAP4(pmac->u[1]);
  555. pmac->u[2] = BSWAP4(pmac->u[2]);
  556. pmac->u[3] = BSWAP4(pmac->u[3]);
  557. pmac->u[4] = BSWAP4(pmac->u[4]);
  558. pmac->u[5] = BSWAP4(pmac->u[5]);
  559. pmac->u[6] = BSWAP4(pmac->u[6]);
  560. pmac->u[7] = BSWAP4(pmac->u[7]);
  561. # else
  562. for (i = 0; i < 8; i++) {
  563. res = pmac->u[i];
  564. pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
  565. pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
  566. pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
  567. pmac->c[4 * i + 3] = (unsigned char)res;
  568. }
  569. # endif
  570. len += SHA256_DIGEST_LENGTH;
  571. # else
  572. SHA256_Update(&key->md, out, inp_len);
  573. res = key->md.num;
  574. SHA256_Final(pmac->c, &key->md);
  575. {
  576. unsigned int inp_blocks, pad_blocks;
  577. /* but pretend as if we hashed padded payload */
  578. inp_blocks =
  579. 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  580. res += (unsigned int)(len - inp_len);
  581. pad_blocks = res / SHA256_CBLOCK;
  582. res %= SHA256_CBLOCK;
  583. pad_blocks +=
  584. 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  585. for (; inp_blocks < pad_blocks; inp_blocks++)
  586. sha1_block_data_order(&key->md, data, 1);
  587. }
  588. # endif /* pre-lucky-13 reference version of above */
  589. key->md = key->tail;
  590. SHA256_Update(&key->md, pmac->c, SHA256_DIGEST_LENGTH);
  591. SHA256_Final(pmac->c, &key->md);
  592. /* verify HMAC */
  593. out += inp_len;
  594. len -= inp_len;
  595. # if 1 /* see original reference version in #else */
  596. {
  597. unsigned char *p =
  598. out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
  599. size_t off = out - p;
  600. unsigned int c, cmask;
  601. maxpad += SHA256_DIGEST_LENGTH;
  602. for (res = 0, i = 0, j = 0; j < maxpad; j++) {
  603. c = p[j];
  604. cmask =
  605. ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
  606. (sizeof(int) * 8 - 1);
  607. res |= (c ^ pad) & ~cmask; /* ... and padding */
  608. cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
  609. res |= (c ^ pmac->c[i]) & cmask;
  610. i += 1 & cmask;
  611. }
  612. maxpad -= SHA256_DIGEST_LENGTH;
  613. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  614. ret &= (int)~res;
  615. }
  616. # else /* pre-lucky-13 reference version of above */
  617. for (res = 0, i = 0; i < SHA256_DIGEST_LENGTH; i++)
  618. res |= out[i] ^ pmac->c[i];
  619. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  620. ret &= (int)~res;
  621. /* verify padding */
  622. pad = (pad & ~res) | (maxpad & res);
  623. out = out + len - 1 - pad;
  624. for (res = 0, i = 0; i < pad; i++)
  625. res |= out[i] ^ pad;
  626. res = (0 - res) >> (sizeof(res) * 8 - 1);
  627. ret &= (int)~res;
  628. # endif
  629. return ret;
  630. } else {
  631. SHA256_Update(&key->md, out, len);
  632. }
  633. }
  634. return 1;
  635. }
  636. static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  637. void *ptr)
  638. {
  639. EVP_AES_HMAC_SHA256 *key = data(ctx);
  640. unsigned int u_arg = (unsigned int)arg;
  641. switch (type) {
  642. case EVP_CTRL_AEAD_SET_MAC_KEY:
  643. {
  644. unsigned int i;
  645. unsigned char hmac_key[64];
  646. memset(hmac_key, 0, sizeof(hmac_key));
  647. if (arg < 0)
  648. return -1;
  649. if (u_arg > sizeof(hmac_key)) {
  650. SHA256_Init(&key->head);
  651. SHA256_Update(&key->head, ptr, arg);
  652. SHA256_Final(hmac_key, &key->head);
  653. } else {
  654. memcpy(hmac_key, ptr, arg);
  655. }
  656. for (i = 0; i < sizeof(hmac_key); i++)
  657. hmac_key[i] ^= 0x36; /* ipad */
  658. SHA256_Init(&key->head);
  659. SHA256_Update(&key->head, hmac_key, sizeof(hmac_key));
  660. for (i = 0; i < sizeof(hmac_key); i++)
  661. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  662. SHA256_Init(&key->tail);
  663. SHA256_Update(&key->tail, hmac_key, sizeof(hmac_key));
  664. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  665. return 1;
  666. }
  667. case EVP_CTRL_AEAD_TLS1_AAD:
  668. {
  669. unsigned char *p = ptr;
  670. unsigned int len = p[arg - 2] << 8 | p[arg - 1];
  671. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  672. return -1;
  673. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  674. key->payload_length = len;
  675. if ((key->aux.tls_ver =
  676. p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  677. len -= AES_BLOCK_SIZE;
  678. p[arg - 2] = len >> 8;
  679. p[arg - 1] = len;
  680. }
  681. key->md = key->head;
  682. SHA256_Update(&key->md, p, arg);
  683. return (int)(((len + SHA256_DIGEST_LENGTH +
  684. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
  685. - len);
  686. } else {
  687. memcpy(key->aux.tls_aad, ptr, arg);
  688. key->payload_length = arg;
  689. return SHA256_DIGEST_LENGTH;
  690. }
  691. }
  692. # if !defined(OPENSSL_NO_MULTIBLOCK)
  693. case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
  694. return (int)(5 + 16 + ((arg + 32 + 16) & -16));
  695. case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
  696. {
  697. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  698. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  699. unsigned int n4x = 1, x4;
  700. unsigned int frag, last, packlen, inp_len;
  701. if (arg < 0)
  702. return -1;
  703. if (u_arg < sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
  704. return -1;
  705. inp_len = param->inp[11] << 8 | param->inp[12];
  706. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  707. if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
  708. return -1;
  709. if (inp_len) {
  710. if (inp_len < 4096)
  711. return 0; /* too short */
  712. if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
  713. n4x = 2; /* AVX2 */
  714. } else if ((n4x = param->interleave / 4) && n4x <= 2)
  715. inp_len = param->len;
  716. else
  717. return -1;
  718. key->md = key->head;
  719. SHA256_Update(&key->md, param->inp, 13);
  720. x4 = 4 * n4x;
  721. n4x += 1;
  722. frag = inp_len >> n4x;
  723. last = inp_len + frag - (frag << n4x);
  724. if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
  725. frag++;
  726. last -= x4 - 1;
  727. }
  728. packlen = 5 + 16 + ((frag + 32 + 16) & -16);
  729. packlen = (packlen << n4x) - packlen;
  730. packlen += 5 + 16 + ((last + 32 + 16) & -16);
  731. param->interleave = x4;
  732. return (int)packlen;
  733. } else
  734. return -1; /* not yet */
  735. }
  736. case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
  737. {
  738. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  739. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  740. return (int)tls1_1_multi_block_encrypt(key, param->out,
  741. param->inp, param->len,
  742. param->interleave / 4);
  743. }
  744. case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
  745. # endif
  746. default:
  747. return -1;
  748. }
  749. }
  750. static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher = {
  751. # ifdef NID_aes_128_cbc_hmac_sha256
  752. NID_aes_128_cbc_hmac_sha256,
  753. # else
  754. NID_undef,
  755. # endif
  756. AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
  757. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  758. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  759. aesni_cbc_hmac_sha256_init_key,
  760. aesni_cbc_hmac_sha256_cipher,
  761. NULL,
  762. sizeof(EVP_AES_HMAC_SHA256),
  763. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  764. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  765. aesni_cbc_hmac_sha256_ctrl,
  766. NULL
  767. };
  768. static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher = {
  769. # ifdef NID_aes_256_cbc_hmac_sha256
  770. NID_aes_256_cbc_hmac_sha256,
  771. # else
  772. NID_undef,
  773. # endif
  774. AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
  775. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  776. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  777. aesni_cbc_hmac_sha256_init_key,
  778. aesni_cbc_hmac_sha256_cipher,
  779. NULL,
  780. sizeof(EVP_AES_HMAC_SHA256),
  781. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  782. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  783. aesni_cbc_hmac_sha256_ctrl,
  784. NULL
  785. };
  786. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
  787. {
  788. return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
  789. aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
  790. &aesni_128_cbc_hmac_sha256_cipher : NULL);
  791. }
  792. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
  793. {
  794. return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
  795. aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
  796. &aesni_256_cbc_hmac_sha256_cipher : NULL);
  797. }
  798. #else
  799. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
  800. {
  801. return NULL;
  802. }
  803. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
  804. {
  805. return NULL;
  806. }
  807. #endif