1 /* $OpenBSD: e_aes_cbc_hmac_sha1.c,v 1.15 2019/04/03 15:33:37 tb Exp $ */ 2 /* ==================================================================== 3 * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. All advertising materials mentioning features or use of this 18 * software must display the following acknowledgment: 19 * "This product includes software developed by the OpenSSL Project 20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 21 * 22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23 * endorse or promote products derived from this software without 24 * prior written permission. For written permission, please contact 25 * licensing@OpenSSL.org. 26 * 27 * 5. Products derived from this software may not be called "OpenSSL" 28 * nor may "OpenSSL" appear in their names without prior written 29 * permission of the OpenSSL Project. 30 * 31 * 6. Redistributions of any form whatsoever must retain the following 32 * acknowledgment: 33 * "This product includes software developed by the OpenSSL Project 34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47 * OF THE POSSIBILITY OF SUCH DAMAGE. 48 * ==================================================================== 49 */ 50 51 #include <stdio.h> 52 #include <string.h> 53 54 #include <openssl/opensslconf.h> 55 56 #if !defined(OPENSSL_NO_AES) && !defined(OPENSSL_NO_SHA1) 57 58 #include <openssl/evp.h> 59 #include <openssl/objects.h> 60 #include <openssl/aes.h> 61 #include <openssl/sha.h> 62 #include "evp_locl.h" 63 #include "constant_time_locl.h" 64 65 #define TLS1_1_VERSION 0x0302 66 67 typedef struct { 68 AES_KEY ks; 69 SHA_CTX head, tail, md; 70 size_t payload_length; /* AAD length in decrypt case */ 71 union { 72 unsigned int tls_ver; 73 unsigned char tls_aad[16]; /* 13 used */ 74 } aux; 75 } EVP_AES_HMAC_SHA1; 76 77 #define NO_PAYLOAD_LENGTH ((size_t)-1) 78 79 #if defined(AES_ASM) && ( \ 80 defined(__x86_64) || defined(__x86_64__) || \ 81 defined(_M_AMD64) || defined(_M_X64) || \ 82 defined(__INTEL__) ) 83 84 #include "x86_arch.h" 85 86 #if defined(__GNUC__) && __GNUC__>=2 87 # define BSWAP(x) ({ unsigned int r=(x); asm ("bswapl %0":"=r"(r):"0"(r)); r; }) 88 #endif 89 90 int aesni_set_encrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); 91 int aesni_set_decrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); 92 93 void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, 94 size_t length, const AES_KEY *key, unsigned char *ivec, int enc); 95 96 void aesni_cbc_sha1_enc (const void *inp, void *out, size_t blocks, 97 const AES_KEY *key, unsigned char iv[16], SHA_CTX *ctx, const void *in0); 98 99 #define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data) 100 101 static int 102 aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *inkey, 103 const unsigned char *iv, int enc) 104 { 105 EVP_AES_HMAC_SHA1 *key = data(ctx); 106 int ret; 107 108 if (enc) 109 ret = aesni_set_encrypt_key(inkey, ctx->key_len * 8, &key->ks); 110 else 111 ret = aesni_set_decrypt_key(inkey, ctx->key_len * 8, &key->ks); 112 113 SHA1_Init(&key->head); /* handy when benchmarking */ 114 key->tail = key->head; 115 key->md = key->head; 116 117 key->payload_length = NO_PAYLOAD_LENGTH; 118 119 return ret < 0 ? 0 : 1; 120 } 121 122 #define STITCHED_CALL 123 124 #if !defined(STITCHED_CALL) 125 #define aes_off 0 126 #endif 127 128 void sha1_block_data_order (void *c, const void *p, size_t len); 129 130 static void 131 sha1_update(SHA_CTX *c, const void *data, size_t len) 132 { 133 const unsigned char *ptr = data; 134 size_t res; 135 136 if ((res = c->num)) { 137 res = SHA_CBLOCK - res; 138 if (len < res) 139 res = len; 140 SHA1_Update(c, ptr, res); 141 ptr += res; 142 len -= res; 143 } 144 145 res = len % SHA_CBLOCK; 146 len -= res; 147 148 if (len) { 149 sha1_block_data_order(c, ptr, len / SHA_CBLOCK); 150 151 ptr += len; 152 c->Nh += len >> 29; 153 c->Nl += len <<= 3; 154 if (c->Nl < (unsigned int)len) 155 c->Nh++; 156 } 157 158 if (res) 159 SHA1_Update(c, ptr, res); 160 } 161 162 #ifdef SHA1_Update 163 #undef SHA1_Update 164 #endif 165 #define SHA1_Update sha1_update 166 167 static int 168 aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 169 const unsigned char *in, size_t len) 170 { 171 EVP_AES_HMAC_SHA1 *key = data(ctx); 172 unsigned int l; 173 size_t plen = key->payload_length, 174 iv = 0, /* explicit IV in TLS 1.1 and later */ 175 sha_off = 0; 176 #if defined(STITCHED_CALL) 177 size_t aes_off = 0, blocks; 178 179 sha_off = SHA_CBLOCK - key->md.num; 180 #endif 181 182 key->payload_length = NO_PAYLOAD_LENGTH; 183 184 if (len % AES_BLOCK_SIZE) 185 return 0; 186 187 if (ctx->encrypt) { 188 if (plen == NO_PAYLOAD_LENGTH) 189 plen = len; 190 else if (len != ((plen + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE) & 191 -AES_BLOCK_SIZE)) 192 return 0; 193 else if (key->aux.tls_ver >= TLS1_1_VERSION) 194 iv = AES_BLOCK_SIZE; 195 196 #if defined(STITCHED_CALL) 197 if (plen > (sha_off + iv) && 198 (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) { 199 SHA1_Update(&key->md, in + iv, sha_off); 200 201 aesni_cbc_sha1_enc(in, out, blocks, &key->ks, 202 ctx->iv, &key->md, in + iv + sha_off); 203 blocks *= SHA_CBLOCK; 204 aes_off += blocks; 205 sha_off += blocks; 206 key->md.Nh += blocks >> 29; 207 key->md.Nl += blocks <<= 3; 208 if (key->md.Nl < (unsigned int)blocks) 209 key->md.Nh++; 210 } else { 211 sha_off = 0; 212 } 213 #endif 214 sha_off += iv; 215 SHA1_Update(&key->md, in + sha_off, plen - sha_off); 216 217 if (plen != len) { /* "TLS" mode of operation */ 218 if (in != out) 219 memcpy(out + aes_off, in + aes_off, 220 plen - aes_off); 221 222 /* calculate HMAC and append it to payload */ 223 SHA1_Final(out + plen, &key->md); 224 key->md = key->tail; 225 SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH); 226 SHA1_Final(out + plen, &key->md); 227 228 /* pad the payload|hmac */ 229 plen += SHA_DIGEST_LENGTH; 230 for (l = len - plen - 1; plen < len; plen++) 231 out[plen] = l; 232 233 /* encrypt HMAC|padding at once */ 234 aesni_cbc_encrypt(out + aes_off, out + aes_off, 235 len - aes_off, &key->ks, ctx->iv, 1); 236 } else { 237 aesni_cbc_encrypt(in + aes_off, out + aes_off, 238 len - aes_off, &key->ks, ctx->iv, 1); 239 } 240 } else { 241 union { 242 unsigned int u[SHA_DIGEST_LENGTH/sizeof(unsigned int)]; 243 unsigned char c[32 + SHA_DIGEST_LENGTH]; 244 } mac, *pmac; 245 246 /* arrange cache line alignment */ 247 pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32)); 248 249 /* decrypt HMAC|padding at once */ 250 aesni_cbc_encrypt(in, out, len, &key->ks, ctx->iv, 0); 251 252 if (plen == 0 || plen == NO_PAYLOAD_LENGTH) { 253 SHA1_Update(&key->md, out, len); 254 } else if (plen < 4) { 255 return 0; 256 } else { /* "TLS" mode of operation */ 257 size_t inp_len, mask, j, i; 258 unsigned int res, maxpad, pad, bitlen; 259 int ret = 1; 260 union { 261 unsigned int u[SHA_LBLOCK]; 262 unsigned char c[SHA_CBLOCK]; 263 } 264 *data = (void *)key->md.data; 265 266 if ((key->aux.tls_aad[plen - 4] << 8 | 267 key->aux.tls_aad[plen - 3]) >= TLS1_1_VERSION) 268 iv = AES_BLOCK_SIZE; 269 270 if (len < (iv + SHA_DIGEST_LENGTH + 1)) 271 return 0; 272 273 /* omit explicit iv */ 274 out += iv; 275 len -= iv; 276 277 /* figure out payload length */ 278 pad = out[len - 1]; 279 maxpad = len - (SHA_DIGEST_LENGTH + 1); 280 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8); 281 maxpad &= 255; 282 283 ret &= constant_time_ge(maxpad, pad); 284 285 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1); 286 mask = (0 - ((inp_len - len) >> 287 (sizeof(inp_len) * 8 - 1))); 288 inp_len &= mask; 289 ret &= (int)mask; 290 291 key->aux.tls_aad[plen - 2] = inp_len >> 8; 292 key->aux.tls_aad[plen - 1] = inp_len; 293 294 /* calculate HMAC */ 295 key->md = key->head; 296 SHA1_Update(&key->md, key->aux.tls_aad, plen); 297 298 #if 1 299 len -= SHA_DIGEST_LENGTH; /* amend mac */ 300 if (len >= (256 + SHA_CBLOCK)) { 301 j = (len - (256 + SHA_CBLOCK)) & 302 (0 - SHA_CBLOCK); 303 j += SHA_CBLOCK - key->md.num; 304 SHA1_Update(&key->md, out, j); 305 out += j; 306 len -= j; 307 inp_len -= j; 308 } 309 310 /* but pretend as if we hashed padded payload */ 311 bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */ 312 #ifdef BSWAP 313 bitlen = BSWAP(bitlen); 314 #else 315 mac.c[0] = 0; 316 mac.c[1] = (unsigned char)(bitlen >> 16); 317 mac.c[2] = (unsigned char)(bitlen >> 8); 318 mac.c[3] = (unsigned char)bitlen; 319 bitlen = mac.u[0]; 320 #endif 321 322 pmac->u[0] = 0; 323 pmac->u[1] = 0; 324 pmac->u[2] = 0; 325 pmac->u[3] = 0; 326 pmac->u[4] = 0; 327 328 for (res = key->md.num, j = 0; j < len; j++) { 329 size_t c = out[j]; 330 mask = (j - inp_len) >> (sizeof(j) * 8 - 8); 331 c &= mask; 332 c |= 0x80 & ~mask & 333 ~((inp_len - j) >> (sizeof(j) * 8 - 8)); 334 data->c[res++] = (unsigned char)c; 335 336 if (res != SHA_CBLOCK) 337 continue; 338 339 /* j is not incremented yet */ 340 mask = 0 - ((inp_len + 7 - j) >> 341 (sizeof(j) * 8 - 1)); 342 data->u[SHA_LBLOCK - 1] |= bitlen&mask; 343 sha1_block_data_order(&key->md, data, 1); 344 mask &= 0 - ((j - inp_len - 72) >> 345 (sizeof(j) * 8 - 1)); 346 pmac->u[0] |= key->md.h0 & mask; 347 pmac->u[1] |= key->md.h1 & mask; 348 pmac->u[2] |= key->md.h2 & mask; 349 pmac->u[3] |= key->md.h3 & mask; 350 pmac->u[4] |= key->md.h4 & mask; 351 res = 0; 352 } 353 354 for (i = res; i < SHA_CBLOCK; i++, j++) 355 data->c[i] = 0; 356 357 if (res > SHA_CBLOCK - 8) { 358 mask = 0 - ((inp_len + 8 - j) >> 359 (sizeof(j) * 8 - 1)); 360 data->u[SHA_LBLOCK - 1] |= bitlen & mask; 361 sha1_block_data_order(&key->md, data, 1); 362 mask &= 0 - ((j - inp_len - 73) >> 363 (sizeof(j) * 8 - 1)); 364 pmac->u[0] |= key->md.h0 & mask; 365 pmac->u[1] |= key->md.h1 & mask; 366 pmac->u[2] |= key->md.h2 & mask; 367 pmac->u[3] |= key->md.h3 & mask; 368 pmac->u[4] |= key->md.h4 & mask; 369 370 memset(data, 0, SHA_CBLOCK); 371 j += 64; 372 } 373 data->u[SHA_LBLOCK - 1] = bitlen; 374 sha1_block_data_order(&key->md, data, 1); 375 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1)); 376 pmac->u[0] |= key->md.h0 & mask; 377 pmac->u[1] |= key->md.h1 & mask; 378 pmac->u[2] |= key->md.h2 & mask; 379 pmac->u[3] |= key->md.h3 & mask; 380 pmac->u[4] |= key->md.h4 & mask; 381 382 #ifdef BSWAP 383 pmac->u[0] = BSWAP(pmac->u[0]); 384 pmac->u[1] = BSWAP(pmac->u[1]); 385 pmac->u[2] = BSWAP(pmac->u[2]); 386 pmac->u[3] = BSWAP(pmac->u[3]); 387 pmac->u[4] = BSWAP(pmac->u[4]); 388 #else 389 for (i = 0; i < 5; i++) { 390 res = pmac->u[i]; 391 pmac->c[4 * i + 0] = (unsigned char)(res >> 24); 392 pmac->c[4 * i + 1] = (unsigned char)(res >> 16); 393 pmac->c[4 * i + 2] = (unsigned char)(res >> 8); 394 pmac->c[4 * i + 3] = (unsigned char)res; 395 } 396 #endif 397 len += SHA_DIGEST_LENGTH; 398 #else 399 SHA1_Update(&key->md, out, inp_len); 400 res = key->md.num; 401 SHA1_Final(pmac->c, &key->md); 402 403 { 404 unsigned int inp_blocks, pad_blocks; 405 406 /* but pretend as if we hashed padded payload */ 407 inp_blocks = 1 + ((SHA_CBLOCK - 9 - res) >> 408 (sizeof(res) * 8 - 1)); 409 res += (unsigned int)(len - inp_len); 410 pad_blocks = res / SHA_CBLOCK; 411 res %= SHA_CBLOCK; 412 pad_blocks += 1 + ((SHA_CBLOCK - 9 - res) >> 413 (sizeof(res) * 8 - 1)); 414 for (; inp_blocks < pad_blocks; inp_blocks++) 415 sha1_block_data_order(&key->md, 416 data, 1); 417 } 418 #endif 419 key->md = key->tail; 420 SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH); 421 SHA1_Final(pmac->c, &key->md); 422 423 /* verify HMAC */ 424 out += inp_len; 425 len -= inp_len; 426 #if 1 427 { 428 unsigned char *p = 429 out + len - 1 - maxpad - SHA_DIGEST_LENGTH; 430 size_t off = out - p; 431 unsigned int c, cmask; 432 433 maxpad += SHA_DIGEST_LENGTH; 434 for (res = 0, i = 0, j = 0; j < maxpad; j++) { 435 c = p[j]; 436 cmask = ((int)(j - off - 437 SHA_DIGEST_LENGTH)) >> 438 (sizeof(int) * 8 - 1); 439 res |= (c ^ pad) & ~cmask; /* ... and padding */ 440 cmask &= ((int)(off - 1 - j)) >> 441 (sizeof(int) * 8 - 1); 442 res |= (c ^ pmac->c[i]) & cmask; 443 i += 1 & cmask; 444 } 445 maxpad -= SHA_DIGEST_LENGTH; 446 447 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); 448 ret &= (int)~res; 449 } 450 #else 451 for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++) 452 res |= out[i] ^ pmac->c[i]; 453 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); 454 ret &= (int)~res; 455 456 /* verify padding */ 457 pad = (pad & ~res) | (maxpad & res); 458 out = out + len - 1 - pad; 459 for (res = 0, i = 0; i < pad; i++) 460 res |= out[i] ^ pad; 461 462 res = (0 - res) >> (sizeof(res) * 8 - 1); 463 ret &= (int)~res; 464 #endif 465 return ret; 466 } 467 } 468 469 return 1; 470 } 471 472 static int 473 aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) 474 { 475 EVP_AES_HMAC_SHA1 *key = data(ctx); 476 477 switch (type) { 478 case EVP_CTRL_AEAD_SET_MAC_KEY: 479 { 480 unsigned int i; 481 unsigned char hmac_key[64]; 482 483 memset(hmac_key, 0, sizeof(hmac_key)); 484 485 if (arg > (int)sizeof(hmac_key)) { 486 SHA1_Init(&key->head); 487 SHA1_Update(&key->head, ptr, arg); 488 SHA1_Final(hmac_key, &key->head); 489 } else { 490 memcpy(hmac_key, ptr, arg); 491 } 492 493 for (i = 0; i < sizeof(hmac_key); i++) 494 hmac_key[i] ^= 0x36; /* ipad */ 495 SHA1_Init(&key->head); 496 SHA1_Update(&key->head, hmac_key, sizeof(hmac_key)); 497 498 for (i = 0; i < sizeof(hmac_key); i++) 499 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */ 500 SHA1_Init(&key->tail); 501 SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key)); 502 503 explicit_bzero(hmac_key, sizeof(hmac_key)); 504 505 return 1; 506 } 507 case EVP_CTRL_AEAD_TLS1_AAD: 508 { 509 unsigned char *p = ptr; 510 unsigned int len; 511 512 /* RFC 5246, 6.2.3.3: additional data has length 13 */ 513 if (arg != 13) 514 return -1; 515 516 len = p[arg - 2] << 8 | p[arg - 1]; 517 518 if (ctx->encrypt) { 519 key->payload_length = len; 520 if ((key->aux.tls_ver = p[arg - 4] << 8 | 521 p[arg - 3]) >= TLS1_1_VERSION) { 522 len -= AES_BLOCK_SIZE; 523 p[arg - 2] = len >> 8; 524 p[arg - 1] = len; 525 } 526 key->md = key->head; 527 SHA1_Update(&key->md, p, arg); 528 529 return (int)(((len + SHA_DIGEST_LENGTH + 530 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE) - len); 531 } else { 532 memcpy(key->aux.tls_aad, ptr, arg); 533 key->payload_length = arg; 534 535 return SHA_DIGEST_LENGTH; 536 } 537 } 538 default: 539 return -1; 540 } 541 } 542 543 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = { 544 #ifdef NID_aes_128_cbc_hmac_sha1 545 .nid = NID_aes_128_cbc_hmac_sha1, 546 #else 547 .nid = NID_undef, 548 #endif 549 .block_size = 16, 550 .key_len = 16, 551 .iv_len = 16, 552 .flags = EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 | 553 EVP_CIPH_FLAG_AEAD_CIPHER, 554 .init = aesni_cbc_hmac_sha1_init_key, 555 .do_cipher = aesni_cbc_hmac_sha1_cipher, 556 .ctx_size = sizeof(EVP_AES_HMAC_SHA1), 557 .ctrl = aesni_cbc_hmac_sha1_ctrl 558 }; 559 560 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = { 561 #ifdef NID_aes_256_cbc_hmac_sha1 562 .nid = NID_aes_256_cbc_hmac_sha1, 563 #else 564 .nid = NID_undef, 565 #endif 566 .block_size = 16, 567 .key_len = 32, 568 .iv_len = 16, 569 .flags = EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 | 570 EVP_CIPH_FLAG_AEAD_CIPHER, 571 .init = aesni_cbc_hmac_sha1_init_key, 572 .do_cipher = aesni_cbc_hmac_sha1_cipher, 573 .ctx_size = sizeof(EVP_AES_HMAC_SHA1), 574 .ctrl = aesni_cbc_hmac_sha1_ctrl 575 }; 576 577 const EVP_CIPHER * 578 EVP_aes_128_cbc_hmac_sha1(void) 579 { 580 return (OPENSSL_cpu_caps() & CPUCAP_MASK_AESNI) ? 581 &aesni_128_cbc_hmac_sha1_cipher : NULL; 582 } 583 584 const EVP_CIPHER * 585 EVP_aes_256_cbc_hmac_sha1(void) 586 { 587 return (OPENSSL_cpu_caps() & CPUCAP_MASK_AESNI) ? 588 &aesni_256_cbc_hmac_sha1_cipher : NULL; 589 } 590 #else 591 const EVP_CIPHER * 592 EVP_aes_128_cbc_hmac_sha1(void) 593 { 594 return NULL; 595 } 596 597 const EVP_CIPHER * 598 EVP_aes_256_cbc_hmac_sha1(void) 599 { 600 return NULL; 601 } 602 #endif 603 #endif 604