e_aes_cbc_hmac_sha256.c 32 KB

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