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