1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2017-2020, Linaro Limited 4 */ 5 6 #include <assert.h> 7 #include <crypto/internal_aes-gcm.h> 8 #include <crypto/crypto_impl.h> 9 #include <io.h> 10 #include <string_ext.h> 11 #include <string.h> 12 #include <tee_api_types.h> 13 #include <types_ext.h> 14 #include <utee_defines.h> 15 #include <util.h> 16 17 static void xor_buf(uint8_t *dst, const uint8_t *src, size_t len) 18 { 19 size_t n; 20 21 for (n = 0; n < len; n++) 22 dst[n] ^= src[n]; 23 } 24 25 26 static void ghash_update_pad_zero(struct internal_aes_gcm_state *state, 27 const uint8_t *data, size_t len) 28 { 29 size_t n = len / TEE_AES_BLOCK_SIZE; 30 uint64_t block[2]; 31 32 if (n) { 33 if (internal_aes_gcm_ptr_is_block_aligned(data)) { 34 internal_aes_gcm_ghash_update(state, NULL, data, n); 35 } else { 36 size_t m; 37 38 for (m = 0; m < n; m++) { 39 40 memcpy(block, data + m * sizeof(block), 41 sizeof(block)); 42 internal_aes_gcm_ghash_update(state, NULL, 43 (void *)block, 1); 44 } 45 } 46 } 47 48 if (len - n * TEE_AES_BLOCK_SIZE) { 49 memset(block, 0, sizeof(block)); 50 memcpy(block, data + n * TEE_AES_BLOCK_SIZE, 51 len - n * TEE_AES_BLOCK_SIZE); 52 internal_aes_gcm_ghash_update(state, block, NULL, 0); 53 } 54 } 55 56 static void ghash_update_lengths(struct internal_aes_gcm_state *state, 57 uint32_t l1, uint32_t l2) 58 { 59 uint64_t len_fields[2] = { 60 TEE_U64_TO_BIG_ENDIAN(l1 * 8), 61 TEE_U64_TO_BIG_ENDIAN(l2 * 8) 62 }; 63 64 COMPILE_TIME_ASSERT(sizeof(len_fields) == TEE_AES_BLOCK_SIZE); 65 internal_aes_gcm_ghash_update(state, (uint8_t *)len_fields, NULL, 0); 66 } 67 68 static TEE_Result __gcm_init(struct internal_aes_gcm_state *state, 69 const struct internal_aes_gcm_key *ek, 70 TEE_OperationMode mode, const void *nonce, 71 size_t nonce_len, size_t tag_len) 72 { 73 COMPILE_TIME_ASSERT(sizeof(state->ctr) == TEE_AES_BLOCK_SIZE); 74 75 if (tag_len > sizeof(state->buf_tag)) 76 return TEE_ERROR_BAD_PARAMETERS; 77 78 memset(state, 0, sizeof(*state)); 79 80 state->tag_len = tag_len; 81 internal_aes_gcm_set_key(state, ek); 82 83 if (nonce_len == (96 / 8)) { 84 memcpy(state->ctr, nonce, nonce_len); 85 internal_aes_gcm_inc_ctr(state); 86 } else { 87 ghash_update_pad_zero(state, nonce, nonce_len); 88 ghash_update_lengths(state, 0, nonce_len); 89 90 memcpy(state->ctr, state->hash_state, sizeof(state->ctr)); 91 memset(state->hash_state, 0, sizeof(state->hash_state)); 92 } 93 94 internal_aes_gcm_encrypt_block(ek, state->ctr, state->buf_tag); 95 internal_aes_gcm_inc_ctr(state); 96 if (mode == TEE_MODE_ENCRYPT) { 97 /* 98 * Encryption uses the pre-encrypted xor-buffer to encrypt 99 * while decryption encrypts the xor-buffer when needed 100 * instead. 101 * 102 * The reason for this is that the combined encryption and 103 * ghash implementation does both operations intertwined. 104 * In the decrypt case the xor-buffer is needed at the end 105 * of processing each block, while the encryption case 106 * needs xor-buffer before processing each block. 107 * 108 * In a pure software implementation we wouldn't have any 109 * use for this kind of optimization, but since this 110 * AES-GCM implementation is aimed at being combined with 111 * accelerated routines it's more convenient to always have 112 * this optimization activated. 113 */ 114 internal_aes_gcm_encrypt_block(ek, state->ctr, state->buf_cryp); 115 internal_aes_gcm_inc_ctr(state); 116 } 117 118 return TEE_SUCCESS; 119 } 120 121 TEE_Result internal_aes_gcm_init(struct internal_aes_gcm_ctx *ctx, 122 TEE_OperationMode mode, const void *key, 123 size_t key_len, const void *nonce, 124 size_t nonce_len, size_t tag_len) 125 { 126 TEE_Result res = internal_aes_gcm_expand_enc_key(key, key_len, 127 &ctx->key); 128 if (res) 129 return res; 130 131 return __gcm_init(&ctx->state, &ctx->key, mode, nonce, nonce_len, 132 tag_len); 133 } 134 135 static TEE_Result __gcm_update_aad(struct internal_aes_gcm_state *state, 136 const void *data, size_t len) 137 { 138 const uint8_t *d = data; 139 size_t l = len; 140 const uint8_t *head = NULL; 141 size_t n; 142 143 if (state->payload_bytes) 144 return TEE_ERROR_BAD_PARAMETERS; 145 146 state->aad_bytes += len; 147 148 while (l) { 149 if (state->buf_pos || 150 !internal_aes_gcm_ptr_is_block_aligned(d) || 151 l < TEE_AES_BLOCK_SIZE) { 152 n = MIN(TEE_AES_BLOCK_SIZE - state->buf_pos, l); 153 memcpy(state->buf_hash + state->buf_pos, d, n); 154 state->buf_pos += n; 155 156 if (state->buf_pos != TEE_AES_BLOCK_SIZE) 157 return TEE_SUCCESS; 158 159 state->buf_pos = 0; 160 head = state->buf_hash; 161 d += n; 162 l -= n; 163 } 164 165 if (internal_aes_gcm_ptr_is_block_aligned(d)) 166 n = l / TEE_AES_BLOCK_SIZE; 167 else 168 n = 0; 169 170 internal_aes_gcm_ghash_update(state, head, d, n); 171 l -= n * TEE_AES_BLOCK_SIZE; 172 d += n * TEE_AES_BLOCK_SIZE; 173 } 174 175 return TEE_SUCCESS; 176 } 177 178 TEE_Result internal_aes_gcm_update_aad(struct internal_aes_gcm_ctx *ctx, 179 const void *data, size_t len) 180 { 181 return __gcm_update_aad(&ctx->state, data, len); 182 } 183 184 static TEE_Result 185 __gcm_update_payload(struct internal_aes_gcm_state *state, 186 const struct internal_aes_gcm_key *ek, 187 TEE_OperationMode mode, const void *src, 188 size_t len, void *dst) 189 { 190 size_t n; 191 const uint8_t *s = src; 192 uint8_t *d = dst; 193 size_t l = len; 194 195 if (!state->payload_bytes && state->buf_pos) { 196 /* AAD part done, finish up the last bits. */ 197 memset(state->buf_hash + state->buf_pos, 0, 198 TEE_AES_BLOCK_SIZE - state->buf_pos); 199 internal_aes_gcm_ghash_update(state, state->buf_hash, NULL, 0); 200 state->buf_pos = 0; 201 } 202 203 state->payload_bytes += len; 204 205 while (l) { 206 if (state->buf_pos || 207 !internal_aes_gcm_ptr_is_block_aligned(s) || 208 !internal_aes_gcm_ptr_is_block_aligned(d) || 209 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 internal_aes_gcm_encrypt_block(ek, state->ctr, 214 state->buf_cryp); 215 } 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 internal_aes_gcm_encrypt_block(ek, state->ctr, 239 state->buf_cryp); 240 internal_aes_gcm_inc_ctr(state); 241 } else { 242 n = l / TEE_AES_BLOCK_SIZE; 243 internal_aes_gcm_update_payload_block_aligned(state, ek, 244 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 __weak internal_aes_gcm_update_payload_block_aligned( 537 struct internal_aes_gcm_state *state, 538 const struct internal_aes_gcm_key *ek, 539 TEE_OperationMode m, const void *src, 540 size_t num_blocks, void *dst) 541 { 542 size_t n; 543 const uint8_t *s = src; 544 uint8_t *d = dst; 545 void *ctr = state->ctr; 546 void *buf_cryp = state->buf_cryp; 547 548 assert(!state->buf_pos && num_blocks && 549 internal_aes_gcm_ptr_is_block_aligned(s) && 550 internal_aes_gcm_ptr_is_block_aligned(d)); 551 552 for (n = 0; n < num_blocks; n++) { 553 if (m == TEE_MODE_ENCRYPT) { 554 internal_aes_gcm_xor_block(buf_cryp, s); 555 internal_aes_gcm_ghash_update(state, buf_cryp, NULL, 0); 556 memcpy(d, buf_cryp, sizeof(state->buf_cryp)); 557 558 internal_aes_gcm_encrypt_block(ek, ctr, buf_cryp); 559 internal_aes_gcm_inc_ctr(state); 560 } else { 561 internal_aes_gcm_encrypt_block(ek, ctr, buf_cryp); 562 563 internal_aes_gcm_xor_block(buf_cryp, s); 564 internal_aes_gcm_ghash_update(state, s, NULL, 0); 565 memcpy(d, buf_cryp, sizeof(state->buf_cryp)); 566 567 internal_aes_gcm_inc_ctr(state); 568 } 569 s += TEE_AES_BLOCK_SIZE; 570 d += TEE_AES_BLOCK_SIZE; 571 } 572 } 573 #endif /*!CFG_CRYPTO_AES_GCM_FROM_CRYPTOLIB*/ 574