1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2017-2020, Linaro Limited 4 */ 5 6 #include <assert.h> 7 #include <crypto/crypto.h> 8 #include <crypto/crypto_impl.h> 9 #include <crypto/internal_aes-gcm.h> 10 #include <io.h> 11 #include <string_ext.h> 12 #include <string.h> 13 #include <tee_api_types.h> 14 #include <types_ext.h> 15 #include <utee_defines.h> 16 #include <util.h> 17 18 static void xor_buf(uint8_t *dst, const uint8_t *src, size_t len) 19 { 20 size_t n; 21 22 for (n = 0; n < len; n++) 23 dst[n] ^= src[n]; 24 } 25 26 27 static void ghash_update_pad_zero(struct internal_aes_gcm_state *state, 28 const uint8_t *data, size_t len) 29 { 30 size_t n = len / TEE_AES_BLOCK_SIZE; 31 uint64_t block[2]; 32 33 if (n) { 34 if (internal_aes_gcm_ptr_is_block_aligned(data)) { 35 internal_aes_gcm_ghash_update(state, NULL, data, n); 36 } else { 37 size_t m; 38 39 for (m = 0; m < n; m++) { 40 41 memcpy(block, data + m * sizeof(block), 42 sizeof(block)); 43 internal_aes_gcm_ghash_update(state, NULL, 44 (void *)block, 1); 45 } 46 } 47 } 48 49 if (len - n * TEE_AES_BLOCK_SIZE) { 50 memset(block, 0, sizeof(block)); 51 memcpy(block, data + n * TEE_AES_BLOCK_SIZE, 52 len - n * TEE_AES_BLOCK_SIZE); 53 internal_aes_gcm_ghash_update(state, block, NULL, 0); 54 } 55 } 56 57 static void ghash_update_lengths(struct internal_aes_gcm_state *state, 58 uint32_t l1, uint32_t l2) 59 { 60 uint64_t len_fields[2] = { 61 TEE_U64_TO_BIG_ENDIAN(l1 * 8), 62 TEE_U64_TO_BIG_ENDIAN(l2 * 8) 63 }; 64 65 COMPILE_TIME_ASSERT(sizeof(len_fields) == TEE_AES_BLOCK_SIZE); 66 internal_aes_gcm_ghash_update(state, (uint8_t *)len_fields, NULL, 0); 67 } 68 69 static TEE_Result __gcm_init(struct internal_aes_gcm_state *state, 70 const struct internal_aes_gcm_key *ek, 71 TEE_OperationMode mode, const void *nonce, 72 size_t nonce_len, size_t tag_len) 73 { 74 COMPILE_TIME_ASSERT(sizeof(state->ctr) == TEE_AES_BLOCK_SIZE); 75 76 if (tag_len > sizeof(state->buf_tag)) 77 return TEE_ERROR_BAD_PARAMETERS; 78 79 memset(state, 0, sizeof(*state)); 80 81 state->tag_len = tag_len; 82 internal_aes_gcm_set_key(state, ek); 83 84 if (nonce_len == (96 / 8)) { 85 memcpy(state->ctr, nonce, nonce_len); 86 internal_aes_gcm_inc_ctr(state); 87 } else { 88 ghash_update_pad_zero(state, nonce, nonce_len); 89 ghash_update_lengths(state, 0, nonce_len); 90 91 memcpy(state->ctr, state->hash_state, sizeof(state->ctr)); 92 memset(state->hash_state, 0, sizeof(state->hash_state)); 93 } 94 95 crypto_aes_enc_block(ek->data, sizeof(ek->data), ek->rounds, 96 state->ctr, state->buf_tag); 97 internal_aes_gcm_inc_ctr(state); 98 if (mode == TEE_MODE_ENCRYPT) { 99 /* 100 * Encryption uses the pre-encrypted xor-buffer to encrypt 101 * while decryption encrypts the xor-buffer when needed 102 * instead. 103 * 104 * The reason for this is that the combined encryption and 105 * ghash implementation does both operations intertwined. 106 * In the decrypt case the xor-buffer is needed at the end 107 * of processing each block, while the encryption case 108 * needs xor-buffer before processing each block. 109 * 110 * In a pure software implementation we wouldn't have any 111 * use for this kind of optimization, but since this 112 * AES-GCM implementation is aimed at being combined with 113 * accelerated routines it's more convenient to always have 114 * this optimization activated. 115 */ 116 crypto_aes_enc_block(ek->data, sizeof(ek->data), ek->rounds, 117 state->ctr, state->buf_cryp); 118 internal_aes_gcm_inc_ctr(state); 119 } 120 121 return TEE_SUCCESS; 122 } 123 124 TEE_Result internal_aes_gcm_init(struct internal_aes_gcm_ctx *ctx, 125 TEE_OperationMode mode, const void *key, 126 size_t key_len, const void *nonce, 127 size_t nonce_len, size_t tag_len) 128 { 129 TEE_Result res = internal_aes_gcm_expand_enc_key(key, key_len, 130 &ctx->key); 131 if (res) 132 return res; 133 134 return __gcm_init(&ctx->state, &ctx->key, mode, nonce, nonce_len, 135 tag_len); 136 } 137 138 static TEE_Result __gcm_update_aad(struct internal_aes_gcm_state *state, 139 const void *data, size_t len) 140 { 141 const uint8_t *d = data; 142 size_t l = len; 143 const uint8_t *head = NULL; 144 size_t n; 145 146 if (state->payload_bytes) 147 return TEE_ERROR_BAD_PARAMETERS; 148 149 state->aad_bytes += len; 150 151 while (l) { 152 if (state->buf_pos || 153 !internal_aes_gcm_ptr_is_block_aligned(d) || 154 l < TEE_AES_BLOCK_SIZE) { 155 n = MIN(TEE_AES_BLOCK_SIZE - state->buf_pos, l); 156 memcpy(state->buf_hash + state->buf_pos, d, n); 157 state->buf_pos += n; 158 159 if (state->buf_pos != TEE_AES_BLOCK_SIZE) 160 return TEE_SUCCESS; 161 162 state->buf_pos = 0; 163 head = state->buf_hash; 164 d += n; 165 l -= n; 166 } 167 168 if (internal_aes_gcm_ptr_is_block_aligned(d)) 169 n = l / TEE_AES_BLOCK_SIZE; 170 else 171 n = 0; 172 173 internal_aes_gcm_ghash_update(state, head, d, n); 174 l -= n * TEE_AES_BLOCK_SIZE; 175 d += n * TEE_AES_BLOCK_SIZE; 176 } 177 178 return TEE_SUCCESS; 179 } 180 181 TEE_Result internal_aes_gcm_update_aad(struct internal_aes_gcm_ctx *ctx, 182 const void *data, size_t len) 183 { 184 return __gcm_update_aad(&ctx->state, data, len); 185 } 186 187 static TEE_Result 188 __gcm_update_payload(struct internal_aes_gcm_state *state, 189 const struct internal_aes_gcm_key *ek, 190 TEE_OperationMode mode, const void *src, 191 size_t len, void *dst) 192 { 193 size_t n; 194 const uint8_t *s = src; 195 uint8_t *d = dst; 196 size_t l = len; 197 198 if (!state->payload_bytes && state->buf_pos) { 199 /* AAD part done, finish up the last bits. */ 200 memset(state->buf_hash + state->buf_pos, 0, 201 TEE_AES_BLOCK_SIZE - state->buf_pos); 202 internal_aes_gcm_ghash_update(state, state->buf_hash, NULL, 0); 203 state->buf_pos = 0; 204 } 205 206 state->payload_bytes += len; 207 208 while (l) { 209 if (state->buf_pos || l < TEE_AES_BLOCK_SIZE) { 210 n = MIN(TEE_AES_BLOCK_SIZE - state->buf_pos, l); 211 212 if (!state->buf_pos && mode == TEE_MODE_DECRYPT) 213 crypto_aes_enc_block(ek->data, sizeof(ek->data), 214 ek->rounds, state->ctr, 215 state->buf_cryp); 216 217 xor_buf(state->buf_cryp + state->buf_pos, s, n); 218 memcpy(d, state->buf_cryp + state->buf_pos, n); 219 if (mode == TEE_MODE_ENCRYPT) 220 memcpy(state->buf_hash + state->buf_pos, 221 state->buf_cryp + state->buf_pos, n); 222 else 223 memcpy(state->buf_hash + state->buf_pos, s, n); 224 225 state->buf_pos += n; 226 227 if (state->buf_pos != TEE_AES_BLOCK_SIZE) 228 return TEE_SUCCESS; 229 230 internal_aes_gcm_ghash_update(state, state->buf_hash, 231 NULL, 0); 232 state->buf_pos = 0; 233 d += n; 234 s += n; 235 l -= n; 236 237 if (mode == TEE_MODE_ENCRYPT) 238 crypto_aes_enc_block(ek->data, sizeof(ek->data), 239 ek->rounds, state->ctr, 240 state->buf_cryp); 241 internal_aes_gcm_inc_ctr(state); 242 } else { 243 n = l / TEE_AES_BLOCK_SIZE; 244 internal_aes_gcm_update_payload_blocks(state, ek, mode, 245 s, n, d); 246 s += n * TEE_AES_BLOCK_SIZE; 247 d += n * TEE_AES_BLOCK_SIZE; 248 l -= n * TEE_AES_BLOCK_SIZE; 249 } 250 } 251 252 return TEE_SUCCESS; 253 } 254 255 TEE_Result internal_aes_gcm_update_payload(struct internal_aes_gcm_ctx *ctx, 256 TEE_OperationMode mode, 257 const void *src, size_t len, 258 void *dst) 259 { 260 return __gcm_update_payload(&ctx->state, &ctx->key, mode, src, len, 261 dst); 262 } 263 264 static TEE_Result operation_final(struct internal_aes_gcm_state *state, 265 const struct internal_aes_gcm_key *enc_key, 266 TEE_OperationMode m, const uint8_t *src, 267 size_t len, uint8_t *dst) 268 { 269 TEE_Result res; 270 271 res = __gcm_update_payload(state, enc_key, m, src, len, dst); 272 if (res) 273 return res; 274 275 if (state->buf_pos) { 276 memset(state->buf_hash + state->buf_pos, 0, 277 sizeof(state->buf_hash) - state->buf_pos); 278 internal_aes_gcm_ghash_update(state, state->buf_hash, NULL, 0); 279 } 280 281 ghash_update_lengths(state, state->aad_bytes, state->payload_bytes); 282 /* buf_tag was filled in with the first counter block aes_gcm_init() */ 283 xor_buf(state->buf_tag, state->hash_state, state->tag_len); 284 285 return TEE_SUCCESS; 286 } 287 288 static TEE_Result __gcm_enc_final(struct internal_aes_gcm_state *state, 289 const struct internal_aes_gcm_key *enc_key, 290 const void *src, size_t len, void *dst, 291 void *tag, size_t *tag_len) 292 { 293 TEE_Result res; 294 295 if (*tag_len < state->tag_len) 296 return TEE_ERROR_SHORT_BUFFER; 297 298 res = operation_final(state, enc_key, TEE_MODE_ENCRYPT, src, len, dst); 299 if (res) 300 return res; 301 302 memcpy(tag, state->buf_tag, state->tag_len); 303 *tag_len = state->tag_len; 304 305 return TEE_SUCCESS; 306 } 307 308 TEE_Result internal_aes_gcm_enc_final(struct internal_aes_gcm_ctx *ctx, 309 const void *src, size_t len, void *dst, 310 void *tag, size_t *tag_len) 311 { 312 return __gcm_enc_final(&ctx->state, &ctx->key, src, len, dst, tag, 313 tag_len); 314 } 315 316 static TEE_Result __gcm_dec_final(struct internal_aes_gcm_state *state, 317 const struct internal_aes_gcm_key *enc_key, 318 const void *src, size_t len, void *dst, 319 const void *tag, size_t tag_len) 320 { 321 TEE_Result res; 322 323 if (tag_len != state->tag_len) 324 return TEE_ERROR_MAC_INVALID; 325 326 res = operation_final(state, enc_key, TEE_MODE_DECRYPT, src, len, dst); 327 if (res) 328 return res; 329 330 if (consttime_memcmp(state->buf_tag, tag, tag_len)) 331 return TEE_ERROR_MAC_INVALID; 332 333 return TEE_SUCCESS; 334 } 335 336 TEE_Result internal_aes_gcm_dec_final(struct internal_aes_gcm_ctx *ctx, 337 const void *src, size_t len, void *dst, 338 const void *tag, size_t tag_len) 339 { 340 return __gcm_dec_final(&ctx->state, &ctx->key, src, len, dst, tag, 341 tag_len); 342 } 343 344 void internal_aes_gcm_inc_ctr(struct internal_aes_gcm_state *state) 345 { 346 uint64_t c; 347 348 c = TEE_U64_FROM_BIG_ENDIAN(state->ctr[1]) + 1; 349 state->ctr[1] = TEE_U64_TO_BIG_ENDIAN(c); 350 if (!c) { 351 c = TEE_U64_FROM_BIG_ENDIAN(state->ctr[0]) + 1; 352 state->ctr[0] = TEE_U64_TO_BIG_ENDIAN(c); 353 } 354 } 355 356 TEE_Result internal_aes_gcm_enc(const struct internal_aes_gcm_key *enc_key, 357 const void *nonce, size_t nonce_len, 358 const void *aad, size_t aad_len, 359 const void *src, size_t len, void *dst, 360 void *tag, size_t *tag_len) 361 { 362 TEE_Result res; 363 struct internal_aes_gcm_state state; 364 365 res = __gcm_init(&state, enc_key, TEE_MODE_ENCRYPT, nonce, nonce_len, 366 *tag_len); 367 if (res) 368 return res; 369 370 if (aad) { 371 res = __gcm_update_aad(&state, aad, aad_len); 372 if (res) 373 return res; 374 } 375 376 return __gcm_enc_final(&state, enc_key, src, len, dst, tag, tag_len); 377 } 378 379 TEE_Result internal_aes_gcm_dec(const struct internal_aes_gcm_key *enc_key, 380 const void *nonce, size_t nonce_len, 381 const void *aad, size_t aad_len, 382 const void *src, size_t len, void *dst, 383 const void *tag, size_t tag_len) 384 { 385 TEE_Result res; 386 struct internal_aes_gcm_state state; 387 388 res = __gcm_init(&state, enc_key, TEE_MODE_DECRYPT, nonce, nonce_len, 389 tag_len); 390 if (res) 391 return res; 392 393 if (aad) { 394 res = __gcm_update_aad(&state, aad, aad_len); 395 if (res) 396 return res; 397 } 398 399 return __gcm_dec_final(&state, enc_key, src, len, dst, tag, tag_len); 400 } 401 402 403 #ifndef CFG_CRYPTO_AES_GCM_FROM_CRYPTOLIB 404 #include <stdlib.h> 405 #include <crypto/crypto.h> 406 407 struct aes_gcm_ctx { 408 struct crypto_authenc_ctx aec; 409 struct internal_aes_gcm_ctx ctx; 410 }; 411 412 static const struct crypto_authenc_ops aes_gcm_ops; 413 414 static struct aes_gcm_ctx * 415 to_aes_gcm_ctx(struct crypto_authenc_ctx *aec) 416 { 417 assert(aec->ops == &aes_gcm_ops); 418 419 return container_of(aec, struct aes_gcm_ctx, aec); 420 } 421 422 TEE_Result crypto_aes_gcm_alloc_ctx(struct crypto_authenc_ctx **ctx_ret) 423 { 424 struct aes_gcm_ctx *ctx = calloc(1, sizeof(*ctx)); 425 426 if (!ctx) 427 return TEE_ERROR_OUT_OF_MEMORY; 428 ctx->aec.ops = &aes_gcm_ops; 429 430 *ctx_ret = &ctx->aec; 431 432 return TEE_SUCCESS; 433 } 434 435 static void aes_gcm_free_ctx(struct crypto_authenc_ctx *aec) 436 { 437 free(to_aes_gcm_ctx(aec)); 438 } 439 440 static void aes_gcm_copy_state(struct crypto_authenc_ctx *dst_ctx, 441 struct crypto_authenc_ctx *src_ctx) 442 { 443 to_aes_gcm_ctx(dst_ctx)->ctx = to_aes_gcm_ctx(src_ctx)->ctx; 444 } 445 446 static TEE_Result aes_gcm_init(struct crypto_authenc_ctx *aec, 447 TEE_OperationMode mode, 448 const uint8_t *key, size_t key_len, 449 const uint8_t *nonce, size_t nonce_len, 450 size_t tag_len, size_t aad_len __unused, 451 size_t payload_len __unused) 452 { 453 return internal_aes_gcm_init(&to_aes_gcm_ctx(aec)->ctx, mode, key, 454 key_len, nonce, nonce_len, tag_len); 455 } 456 457 static TEE_Result aes_gcm_update_aad(struct crypto_authenc_ctx *aec, 458 const uint8_t *data, size_t len) 459 { 460 return internal_aes_gcm_update_aad(&to_aes_gcm_ctx(aec)->ctx, data, 461 len); 462 } 463 464 static TEE_Result aes_gcm_update_payload(struct crypto_authenc_ctx *aec, 465 TEE_OperationMode m, 466 const uint8_t *src, size_t len, 467 uint8_t *dst) 468 { 469 return internal_aes_gcm_update_payload(&to_aes_gcm_ctx(aec)->ctx, 470 m, src, len, dst); 471 } 472 473 static TEE_Result aes_gcm_enc_final(struct crypto_authenc_ctx *aec, 474 const uint8_t *src, size_t len, 475 uint8_t *dst, uint8_t *tag, size_t *tag_len) 476 { 477 return internal_aes_gcm_enc_final(&to_aes_gcm_ctx(aec)->ctx, src, len, 478 dst, tag, tag_len); 479 } 480 481 static TEE_Result aes_gcm_dec_final(struct crypto_authenc_ctx *aec, 482 const uint8_t *src, size_t len, 483 uint8_t *dst, const uint8_t *tag, 484 size_t tag_len) 485 { 486 return internal_aes_gcm_dec_final(&to_aes_gcm_ctx(aec)->ctx, src, len, 487 dst, tag, tag_len); 488 } 489 490 static void aes_gcm_final(struct crypto_authenc_ctx *aec __unused) 491 { 492 } 493 494 static const struct crypto_authenc_ops aes_gcm_ops = { 495 .init = aes_gcm_init, 496 .update_aad = aes_gcm_update_aad, 497 .update_payload = aes_gcm_update_payload, 498 .enc_final = aes_gcm_enc_final, 499 .dec_final = aes_gcm_dec_final, 500 .final = aes_gcm_final, 501 .free_ctx = aes_gcm_free_ctx, 502 .copy_state = aes_gcm_copy_state, 503 }; 504 505 /* 506 * internal_aes_gcm_gfmul() is based on ghash_gfmul() from 507 * https://github.com/openbsd/src/blob/master/sys/crypto/gmac.c 508 */ 509 void internal_aes_gcm_gfmul(const uint64_t X[2], const uint64_t Y[2], 510 uint64_t product[2]) 511 { 512 uint64_t y[2] = { 0 }; 513 uint64_t z[2] = { 0 }; 514 const uint8_t *x = (const uint8_t *)X; 515 uint32_t mul = 0; 516 size_t n = 0; 517 518 y[0] = TEE_U64_FROM_BIG_ENDIAN(Y[0]); 519 y[1] = TEE_U64_FROM_BIG_ENDIAN(Y[1]); 520 521 for (n = 0; n < TEE_AES_BLOCK_SIZE * 8; n++) { 522 /* update Z */ 523 if (x[n >> 3] & (1 << (~n & 7))) 524 internal_aes_gcm_xor_block(z, y); 525 526 /* update Y */ 527 mul = y[1] & 1; 528 y[1] = (y[0] << 63) | (y[1] >> 1); 529 y[0] = (y[0] >> 1) ^ (0xe100000000000000 * mul); 530 } 531 532 product[0] = TEE_U64_TO_BIG_ENDIAN(z[0]); 533 product[1] = TEE_U64_TO_BIG_ENDIAN(z[1]); 534 } 535 536 void internal_aes_gcm_encrypt_block(struct internal_aes_gcm_state *state, 537 const struct internal_aes_gcm_key *enc_key, 538 const uint64_t src[2], uint64_t dst[2]) 539 { 540 void *buf_cryp = state->buf_cryp; 541 void *ctr = state->ctr; 542 543 internal_aes_gcm_xor_block(buf_cryp, src); 544 internal_aes_gcm_ghash_update(state, buf_cryp, NULL, 0); 545 memcpy(dst, buf_cryp, sizeof(state->buf_cryp)); 546 547 crypto_aes_enc_block(enc_key->data, sizeof(enc_key->data), 548 enc_key->rounds, ctr, buf_cryp); 549 internal_aes_gcm_inc_ctr(state); 550 } 551 552 static void encrypt_pl(struct internal_aes_gcm_state *state, 553 const struct internal_aes_gcm_key *ek, 554 const uint8_t *src, size_t num_blocks, uint8_t *dst) 555 { 556 size_t n = 0; 557 558 if (ALIGNMENT_IS_OK(src, uint64_t)) { 559 for (n = 0; n < num_blocks; n++) { 560 const void *s = src + n * TEE_AES_BLOCK_SIZE; 561 void *d = dst + n * TEE_AES_BLOCK_SIZE; 562 563 internal_aes_gcm_encrypt_block(state, ek, s, d); 564 } 565 } else { 566 for (n = 0; n < num_blocks; n++) { 567 uint64_t tmp[2] = { 0 }; 568 void *d = dst + n * TEE_AES_BLOCK_SIZE; 569 570 memcpy(tmp, src + n * TEE_AES_BLOCK_SIZE, sizeof(tmp)); 571 internal_aes_gcm_encrypt_block(state, ek, tmp, d); 572 } 573 } 574 } 575 576 void internal_aes_gcm_decrypt_block(struct internal_aes_gcm_state *state, 577 const struct internal_aes_gcm_key *enc_key, 578 const uint64_t src[2], uint64_t dst[2]) 579 { 580 void *buf_cryp = state->buf_cryp; 581 void *ctr = state->ctr; 582 583 crypto_aes_enc_block(enc_key->data, sizeof(enc_key->data), 584 enc_key->rounds, ctr, buf_cryp); 585 internal_aes_gcm_inc_ctr(state); 586 587 internal_aes_gcm_xor_block(buf_cryp, src); 588 internal_aes_gcm_ghash_update(state, src, NULL, 0); 589 memcpy(dst, buf_cryp, sizeof(state->buf_cryp)); 590 } 591 592 static void decrypt_pl(struct internal_aes_gcm_state *state, 593 const struct internal_aes_gcm_key *ek, 594 const uint8_t *src, size_t num_blocks, uint8_t *dst) 595 { 596 size_t n = 0; 597 598 if (ALIGNMENT_IS_OK(src, uint64_t)) { 599 for (n = 0; n < num_blocks; n++) { 600 const void *s = src + n * TEE_AES_BLOCK_SIZE; 601 void *d = dst + n * TEE_AES_BLOCK_SIZE; 602 603 internal_aes_gcm_decrypt_block(state, ek, s, d); 604 } 605 } else { 606 for (n = 0; n < num_blocks; n++) { 607 uint64_t tmp[2] = { 0 }; 608 void *d = dst + n * TEE_AES_BLOCK_SIZE; 609 610 memcpy(tmp, src + n * TEE_AES_BLOCK_SIZE, sizeof(tmp)); 611 internal_aes_gcm_decrypt_block(state, ek, tmp, d); 612 } 613 } 614 } 615 616 void __weak 617 internal_aes_gcm_update_payload_blocks(struct internal_aes_gcm_state *state, 618 const struct internal_aes_gcm_key *ek, 619 TEE_OperationMode m, const void *src, 620 size_t num_blocks, void *dst) 621 { 622 assert(!state->buf_pos && num_blocks); 623 624 if (m == TEE_MODE_ENCRYPT) 625 encrypt_pl(state, ek, src, num_blocks, dst); 626 else 627 decrypt_pl(state, ek, src, num_blocks, dst); 628 } 629 #endif /*!CFG_CRYPTO_AES_GCM_FROM_CRYPTOLIB*/ 630