1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 #include <stdlib.h> 6 #include <string.h> 7 #include <string_ext.h> 8 9 #include <tee_api.h> 10 #include <tee_api_defines_extensions.h> 11 #include <tee_internal_api_extensions.h> 12 #include <utee_syscalls.h> 13 #include <utee_defines.h> 14 #include <util.h> 15 #include "tee_api_private.h" 16 17 struct __TEE_OperationHandle { 18 TEE_OperationInfo info; 19 TEE_ObjectHandle key1; 20 TEE_ObjectHandle key2; 21 uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 22 uint8_t *buffer; /* buffer to collect complete blocks */ 23 bool buffer_two_blocks; /* True if two blocks need to be buffered */ 24 size_t block_size; /* Block size of cipher */ 25 size_t buffer_offs; /* Offset in buffer */ 26 uint32_t state; /* Handle to state in TEE Core */ 27 uint32_t ae_tag_len; /* 28 * tag_len in bytes for AE operation else unused 29 */ 30 }; 31 32 /* Cryptographic Operations API - Generic Operation Functions */ 33 34 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 35 uint32_t algorithm, uint32_t mode, 36 uint32_t maxKeySize) 37 { 38 TEE_Result res; 39 TEE_OperationHandle op = TEE_HANDLE_NULL; 40 uint32_t handle_state = 0; 41 size_t block_size = 1; 42 uint32_t req_key_usage; 43 bool with_private_key = false; 44 bool buffer_two_blocks = false; 45 46 if (!operation) 47 TEE_Panic(0); 48 49 if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP) 50 handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 51 52 /* Check algorithm max key size */ 53 switch (algorithm) { 54 case TEE_ALG_DSA_SHA1: 55 if (maxKeySize < 512) 56 return TEE_ERROR_NOT_SUPPORTED; 57 if (maxKeySize > 1024) 58 return TEE_ERROR_NOT_SUPPORTED; 59 if (maxKeySize % 64 != 0) 60 return TEE_ERROR_NOT_SUPPORTED; 61 break; 62 63 case TEE_ALG_DSA_SHA224: 64 if (maxKeySize != 2048) 65 return TEE_ERROR_NOT_SUPPORTED; 66 break; 67 68 case TEE_ALG_DSA_SHA256: 69 if (maxKeySize != 2048 && maxKeySize != 3072) 70 return TEE_ERROR_NOT_SUPPORTED; 71 break; 72 73 case TEE_ALG_ECDSA_P192: 74 case TEE_ALG_ECDH_P192: 75 if (maxKeySize != 192) 76 return TEE_ERROR_NOT_SUPPORTED; 77 break; 78 79 case TEE_ALG_ECDSA_P224: 80 case TEE_ALG_ECDH_P224: 81 if (maxKeySize != 224) 82 return TEE_ERROR_NOT_SUPPORTED; 83 break; 84 85 case TEE_ALG_ECDSA_P256: 86 case TEE_ALG_ECDH_P256: 87 case TEE_ALG_SM2_PKE: 88 case TEE_ALG_SM2_DSA_SM3: 89 if (maxKeySize != 256) 90 return TEE_ERROR_NOT_SUPPORTED; 91 break; 92 93 case TEE_ALG_SM2_KEP: 94 /* Two 256-bit keys */ 95 if (maxKeySize != 512) 96 return TEE_ERROR_NOT_SUPPORTED; 97 break; 98 99 case TEE_ALG_ECDSA_P384: 100 case TEE_ALG_ECDH_P384: 101 if (maxKeySize != 384) 102 return TEE_ERROR_NOT_SUPPORTED; 103 break; 104 105 case TEE_ALG_ECDSA_P521: 106 case TEE_ALG_ECDH_P521: 107 if (maxKeySize != 521) 108 return TEE_ERROR_NOT_SUPPORTED; 109 break; 110 111 default: 112 break; 113 } 114 115 /* Check algorithm mode */ 116 switch (algorithm) { 117 case TEE_ALG_AES_CTS: 118 case TEE_ALG_AES_XTS: 119 buffer_two_blocks = true; 120 /* FALLTHROUGH */ 121 case TEE_ALG_AES_ECB_NOPAD: 122 case TEE_ALG_AES_CBC_NOPAD: 123 case TEE_ALG_AES_CCM: 124 case TEE_ALG_DES_ECB_NOPAD: 125 case TEE_ALG_DES_CBC_NOPAD: 126 case TEE_ALG_DES3_ECB_NOPAD: 127 case TEE_ALG_DES3_CBC_NOPAD: 128 case TEE_ALG_SM4_ECB_NOPAD: 129 case TEE_ALG_SM4_CBC_NOPAD: 130 case TEE_ALG_SM4_CTR: 131 if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 132 block_size = TEE_AES_BLOCK_SIZE; 133 else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 134 block_size = TEE_SM4_BLOCK_SIZE; 135 else 136 block_size = TEE_DES_BLOCK_SIZE; 137 /* FALLTHROUGH */ 138 case TEE_ALG_AES_CTR: 139 case TEE_ALG_AES_GCM: 140 if (mode == TEE_MODE_ENCRYPT) 141 req_key_usage = TEE_USAGE_ENCRYPT; 142 else if (mode == TEE_MODE_DECRYPT) 143 req_key_usage = TEE_USAGE_DECRYPT; 144 else 145 return TEE_ERROR_NOT_SUPPORTED; 146 break; 147 148 #if defined(CFG_CRYPTO_RSASSA_NA1) 149 case TEE_ALG_RSASSA_PKCS1_V1_5: 150 #endif 151 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 152 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 153 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 154 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 155 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 156 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 157 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 158 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 159 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 160 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 161 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 162 case TEE_ALG_DSA_SHA1: 163 case TEE_ALG_DSA_SHA224: 164 case TEE_ALG_DSA_SHA256: 165 case TEE_ALG_ECDSA_P192: 166 case TEE_ALG_ECDSA_P224: 167 case TEE_ALG_ECDSA_P256: 168 case TEE_ALG_ECDSA_P384: 169 case TEE_ALG_ECDSA_P521: 170 case TEE_ALG_SM2_DSA_SM3: 171 if (mode == TEE_MODE_SIGN) { 172 with_private_key = true; 173 req_key_usage = TEE_USAGE_SIGN; 174 } else if (mode == TEE_MODE_VERIFY) { 175 req_key_usage = TEE_USAGE_VERIFY; 176 } else { 177 return TEE_ERROR_NOT_SUPPORTED; 178 } 179 break; 180 181 case TEE_ALG_RSAES_PKCS1_V1_5: 182 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 183 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 184 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 185 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 186 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 187 case TEE_ALG_SM2_PKE: 188 if (mode == TEE_MODE_ENCRYPT) { 189 req_key_usage = TEE_USAGE_ENCRYPT; 190 } else if (mode == TEE_MODE_DECRYPT) { 191 with_private_key = true; 192 req_key_usage = TEE_USAGE_DECRYPT; 193 } else { 194 return TEE_ERROR_NOT_SUPPORTED; 195 } 196 break; 197 198 case TEE_ALG_RSA_NOPAD: 199 if (mode == TEE_MODE_ENCRYPT) { 200 req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 201 } else if (mode == TEE_MODE_DECRYPT) { 202 with_private_key = true; 203 req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 204 } else { 205 return TEE_ERROR_NOT_SUPPORTED; 206 } 207 break; 208 209 case TEE_ALG_DH_DERIVE_SHARED_SECRET: 210 case TEE_ALG_ECDH_P192: 211 case TEE_ALG_ECDH_P224: 212 case TEE_ALG_ECDH_P256: 213 case TEE_ALG_ECDH_P384: 214 case TEE_ALG_ECDH_P521: 215 case TEE_ALG_HKDF_MD5_DERIVE_KEY: 216 case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 217 case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 218 case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 219 case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 220 case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 221 case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 222 case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 223 case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 224 case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 225 case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 226 case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 227 case TEE_ALG_SM2_KEP: 228 if (mode != TEE_MODE_DERIVE) 229 return TEE_ERROR_NOT_SUPPORTED; 230 with_private_key = true; 231 req_key_usage = TEE_USAGE_DERIVE; 232 break; 233 234 case TEE_ALG_MD5: 235 case TEE_ALG_SHA1: 236 case TEE_ALG_SHA224: 237 case TEE_ALG_SHA256: 238 case TEE_ALG_SHA384: 239 case TEE_ALG_SHA512: 240 case TEE_ALG_SM3: 241 if (mode != TEE_MODE_DIGEST) 242 return TEE_ERROR_NOT_SUPPORTED; 243 /* v1.1: flags always set for digest operations */ 244 handle_state |= TEE_HANDLE_FLAG_KEY_SET; 245 req_key_usage = 0; 246 break; 247 248 case TEE_ALG_DES_CBC_MAC_NOPAD: 249 case TEE_ALG_AES_CBC_MAC_NOPAD: 250 case TEE_ALG_AES_CBC_MAC_PKCS5: 251 case TEE_ALG_AES_CMAC: 252 case TEE_ALG_DES_CBC_MAC_PKCS5: 253 case TEE_ALG_DES3_CBC_MAC_NOPAD: 254 case TEE_ALG_DES3_CBC_MAC_PKCS5: 255 case TEE_ALG_HMAC_MD5: 256 case TEE_ALG_HMAC_SHA1: 257 case TEE_ALG_HMAC_SHA224: 258 case TEE_ALG_HMAC_SHA256: 259 case TEE_ALG_HMAC_SHA384: 260 case TEE_ALG_HMAC_SHA512: 261 case TEE_ALG_HMAC_SM3: 262 if (mode != TEE_MODE_MAC) 263 return TEE_ERROR_NOT_SUPPORTED; 264 req_key_usage = TEE_USAGE_MAC; 265 break; 266 267 default: 268 return TEE_ERROR_NOT_SUPPORTED; 269 } 270 271 op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 272 if (!op) 273 return TEE_ERROR_OUT_OF_MEMORY; 274 275 op->info.algorithm = algorithm; 276 op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 277 #ifdef CFG_CRYPTO_RSASSA_NA1 278 if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 279 op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 280 #endif 281 op->info.mode = mode; 282 op->info.maxKeySize = maxKeySize; 283 op->info.requiredKeyUsage = req_key_usage; 284 op->info.handleState = handle_state; 285 286 if (block_size > 1) { 287 size_t buffer_size = block_size; 288 289 if (buffer_two_blocks) 290 buffer_size *= 2; 291 292 op->buffer = TEE_Malloc(buffer_size, 293 TEE_USER_MEM_HINT_NO_FILL_ZERO); 294 if (op->buffer == NULL) { 295 res = TEE_ERROR_OUT_OF_MEMORY; 296 goto out; 297 } 298 } 299 op->block_size = block_size; 300 op->buffer_two_blocks = buffer_two_blocks; 301 302 if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 303 uint32_t mks = maxKeySize; 304 TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 305 with_private_key); 306 307 /* 308 * If two keys are expected the max key size is the sum of 309 * the size of both keys. 310 */ 311 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 312 mks /= 2; 313 314 res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 315 if (res != TEE_SUCCESS) 316 goto out; 317 318 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 319 res = TEE_AllocateTransientObject(key_type, mks, 320 &op->key2); 321 if (res != TEE_SUCCESS) 322 goto out; 323 } 324 } 325 326 res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 327 (unsigned long)op->key2, &op->state); 328 if (res != TEE_SUCCESS) 329 goto out; 330 331 /* 332 * Initialize digest operations 333 * Other multi-stage operations initialized w/ TEE_xxxInit functions 334 * Non-applicable on asymmetric operations 335 */ 336 if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 337 res = utee_hash_init(op->state, NULL, 0); 338 if (res != TEE_SUCCESS) 339 goto out; 340 /* v1.1: flags always set for digest operations */ 341 op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 342 } 343 344 op->operationState = TEE_OPERATION_STATE_INITIAL; 345 346 *operation = op; 347 348 out: 349 if (res != TEE_SUCCESS) { 350 if (res != TEE_ERROR_OUT_OF_MEMORY && 351 res != TEE_ERROR_NOT_SUPPORTED) 352 TEE_Panic(res); 353 if (op) { 354 if (op->state) { 355 TEE_FreeOperation(op); 356 } else { 357 TEE_Free(op->buffer); 358 TEE_FreeTransientObject(op->key1); 359 TEE_FreeTransientObject(op->key2); 360 TEE_Free(op); 361 } 362 } 363 } 364 365 return res; 366 } 367 368 void TEE_FreeOperation(TEE_OperationHandle operation) 369 { 370 TEE_Result res; 371 372 if (operation == TEE_HANDLE_NULL) 373 TEE_Panic(0); 374 375 /* 376 * Note that keys should not be freed here, since they are 377 * claimed by the operation they will be freed by 378 * utee_cryp_state_free(). 379 */ 380 res = utee_cryp_state_free(operation->state); 381 if (res != TEE_SUCCESS) 382 TEE_Panic(res); 383 384 TEE_Free(operation->buffer); 385 TEE_Free(operation); 386 } 387 388 void TEE_GetOperationInfo(TEE_OperationHandle operation, 389 TEE_OperationInfo *operationInfo) 390 { 391 if (operation == TEE_HANDLE_NULL) 392 TEE_Panic(0); 393 394 if (!operationInfo) 395 TEE_Panic(0); 396 397 *operationInfo = operation->info; 398 } 399 400 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 401 TEE_OperationInfoMultiple *operationInfoMultiple, 402 uint32_t *operationSize) 403 { 404 TEE_Result res = TEE_SUCCESS; 405 TEE_ObjectInfo key_info1; 406 TEE_ObjectInfo key_info2; 407 uint32_t num_of_keys; 408 size_t n; 409 410 if (operation == TEE_HANDLE_NULL) { 411 res = TEE_ERROR_BAD_PARAMETERS; 412 goto out; 413 } 414 415 if (!operationInfoMultiple) { 416 res = TEE_ERROR_BAD_PARAMETERS; 417 goto out; 418 } 419 420 if (!operationSize) { 421 res = TEE_ERROR_BAD_PARAMETERS; 422 goto out; 423 } 424 425 num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 426 sizeof(TEE_OperationInfoKey); 427 428 if (num_of_keys > 2) { 429 res = TEE_ERROR_BAD_PARAMETERS; 430 goto out; 431 } 432 433 /* Two keys flag (TEE_ALG_AES_XTS only) */ 434 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 435 0 && 436 (num_of_keys != 2)) { 437 res = TEE_ERROR_SHORT_BUFFER; 438 goto out; 439 } 440 441 /* Clear */ 442 for (n = 0; n < num_of_keys; n++) { 443 operationInfoMultiple->keyInformation[n].keySize = 0; 444 operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 445 } 446 447 if (num_of_keys == 2) { 448 res = TEE_GetObjectInfo1(operation->key2, &key_info2); 449 /* Key2 is not a valid handle */ 450 if (res != TEE_SUCCESS) 451 goto out; 452 453 operationInfoMultiple->keyInformation[1].keySize = 454 key_info2.keySize; 455 operationInfoMultiple->keyInformation[1].requiredKeyUsage = 456 operation->info.requiredKeyUsage; 457 } 458 459 if (num_of_keys >= 1) { 460 res = TEE_GetObjectInfo1(operation->key1, &key_info1); 461 /* Key1 is not a valid handle */ 462 if (res != TEE_SUCCESS) { 463 if (num_of_keys == 2) { 464 operationInfoMultiple->keyInformation[1]. 465 keySize = 0; 466 operationInfoMultiple->keyInformation[1]. 467 requiredKeyUsage = 0; 468 } 469 goto out; 470 } 471 472 operationInfoMultiple->keyInformation[0].keySize = 473 key_info1.keySize; 474 operationInfoMultiple->keyInformation[0].requiredKeyUsage = 475 operation->info.requiredKeyUsage; 476 } 477 478 /* No key */ 479 operationInfoMultiple->algorithm = operation->info.algorithm; 480 operationInfoMultiple->operationClass = operation->info.operationClass; 481 operationInfoMultiple->mode = operation->info.mode; 482 operationInfoMultiple->digestLength = operation->info.digestLength; 483 operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 484 operationInfoMultiple->handleState = operation->info.handleState; 485 operationInfoMultiple->operationState = operation->operationState; 486 operationInfoMultiple->numberOfKeys = num_of_keys; 487 488 out: 489 if (res != TEE_SUCCESS && 490 res != TEE_ERROR_SHORT_BUFFER) 491 TEE_Panic(res); 492 493 return res; 494 } 495 496 void TEE_ResetOperation(TEE_OperationHandle operation) 497 { 498 TEE_Result res; 499 500 if (operation == TEE_HANDLE_NULL) 501 TEE_Panic(0); 502 503 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 504 TEE_Panic(0); 505 506 operation->operationState = TEE_OPERATION_STATE_INITIAL; 507 508 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 509 res = utee_hash_init(operation->state, NULL, 0); 510 if (res != TEE_SUCCESS) 511 TEE_Panic(res); 512 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 513 } else { 514 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 515 } 516 } 517 518 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 519 TEE_ObjectHandle key) 520 { 521 TEE_Result res; 522 uint32_t key_size = 0; 523 TEE_ObjectInfo key_info; 524 525 if (operation == TEE_HANDLE_NULL) { 526 res = TEE_ERROR_BAD_PARAMETERS; 527 goto out; 528 } 529 530 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 531 res = TEE_ERROR_BAD_PARAMETERS; 532 goto out; 533 } 534 535 if (key == TEE_HANDLE_NULL) { 536 /* Operation key cleared */ 537 TEE_ResetTransientObject(operation->key1); 538 res = TEE_ERROR_BAD_PARAMETERS; 539 goto out; 540 } 541 542 /* No key for digest operation */ 543 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 544 res = TEE_ERROR_BAD_PARAMETERS; 545 goto out; 546 } 547 548 /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 549 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 550 0) { 551 res = TEE_ERROR_BAD_PARAMETERS; 552 goto out; 553 } 554 555 res = TEE_GetObjectInfo1(key, &key_info); 556 /* Key is not a valid handle */ 557 if (res != TEE_SUCCESS) 558 goto out; 559 560 /* Supplied key has to meet required usage */ 561 if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 562 operation->info.requiredKeyUsage) { 563 res = TEE_ERROR_BAD_PARAMETERS; 564 goto out; 565 } 566 567 if (operation->info.maxKeySize < key_info.keySize) { 568 res = TEE_ERROR_BAD_PARAMETERS; 569 goto out; 570 } 571 572 key_size = key_info.keySize; 573 574 TEE_ResetTransientObject(operation->key1); 575 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 576 577 res = TEE_CopyObjectAttributes1(operation->key1, key); 578 if (res != TEE_SUCCESS) 579 goto out; 580 581 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 582 583 operation->info.keySize = key_size; 584 585 out: 586 if (res != TEE_SUCCESS && 587 res != TEE_ERROR_CORRUPT_OBJECT && 588 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 589 TEE_Panic(res); 590 591 return res; 592 } 593 594 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 595 TEE_ObjectHandle key1, TEE_ObjectHandle key2) 596 { 597 TEE_Result res; 598 uint32_t key_size = 0; 599 TEE_ObjectInfo key_info1; 600 TEE_ObjectInfo key_info2; 601 602 if (operation == TEE_HANDLE_NULL) { 603 res = TEE_ERROR_BAD_PARAMETERS; 604 goto out; 605 } 606 607 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 608 res = TEE_ERROR_BAD_PARAMETERS; 609 goto out; 610 } 611 612 /* 613 * Key1/Key2 and/or are not initialized and 614 * Either both keys are NULL or both are not NULL 615 */ 616 if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 617 /* Clear operation key1 (if needed) */ 618 if (key1 == TEE_HANDLE_NULL) 619 TEE_ResetTransientObject(operation->key1); 620 /* Clear operation key2 (if needed) */ 621 if (key2 == TEE_HANDLE_NULL) 622 TEE_ResetTransientObject(operation->key2); 623 res = TEE_ERROR_BAD_PARAMETERS; 624 goto out; 625 } 626 627 /* No key for digest operation */ 628 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 629 res = TEE_ERROR_BAD_PARAMETERS; 630 goto out; 631 } 632 633 /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 634 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 635 0) { 636 res = TEE_ERROR_BAD_PARAMETERS; 637 goto out; 638 } 639 640 res = TEE_GetObjectInfo1(key1, &key_info1); 641 /* Key1 is not a valid handle */ 642 if (res != TEE_SUCCESS) 643 goto out; 644 645 /* Supplied key has to meet required usage */ 646 if ((key_info1.objectUsage & operation->info. 647 requiredKeyUsage) != operation->info.requiredKeyUsage) { 648 res = TEE_ERROR_BAD_PARAMETERS; 649 goto out; 650 } 651 652 res = TEE_GetObjectInfo1(key2, &key_info2); 653 /* Key2 is not a valid handle */ 654 if (res != TEE_SUCCESS) { 655 if (res == TEE_ERROR_CORRUPT_OBJECT) 656 res = TEE_ERROR_CORRUPT_OBJECT_2; 657 goto out; 658 } 659 660 /* Supplied key has to meet required usage */ 661 if ((key_info2.objectUsage & operation->info. 662 requiredKeyUsage) != operation->info.requiredKeyUsage) { 663 res = TEE_ERROR_BAD_PARAMETERS; 664 goto out; 665 } 666 667 /* 668 * All the multi key algorithm currently supported requires the keys to 669 * be of equal size. 670 */ 671 if (key_info1.keySize != key_info2.keySize) { 672 res = TEE_ERROR_BAD_PARAMETERS; 673 goto out; 674 675 } 676 677 if (operation->info.maxKeySize < key_info1.keySize) { 678 res = TEE_ERROR_BAD_PARAMETERS; 679 goto out; 680 } 681 682 /* 683 * Odd that only the size of one key should be reported while 684 * size of two key are used when allocating the operation. 685 */ 686 key_size = key_info1.keySize; 687 688 TEE_ResetTransientObject(operation->key1); 689 TEE_ResetTransientObject(operation->key2); 690 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 691 692 res = TEE_CopyObjectAttributes1(operation->key1, key1); 693 if (res != TEE_SUCCESS) 694 goto out; 695 res = TEE_CopyObjectAttributes1(operation->key2, key2); 696 if (res != TEE_SUCCESS) { 697 if (res == TEE_ERROR_CORRUPT_OBJECT) 698 res = TEE_ERROR_CORRUPT_OBJECT_2; 699 goto out; 700 } 701 702 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 703 704 operation->info.keySize = key_size; 705 706 out: 707 if (res != TEE_SUCCESS && 708 res != TEE_ERROR_CORRUPT_OBJECT && 709 res != TEE_ERROR_CORRUPT_OBJECT_2 && 710 res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 711 res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 712 TEE_Panic(res); 713 714 return res; 715 } 716 717 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 718 { 719 TEE_Result res; 720 721 if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 722 TEE_Panic(0); 723 if (dst_op->info.algorithm != src_op->info.algorithm) 724 TEE_Panic(0); 725 if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 726 TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 727 TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 728 729 if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 730 key1 = src_op->key1; 731 key2 = src_op->key2; 732 } 733 734 if ((src_op->info.handleState & 735 TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 736 TEE_SetOperationKey(dst_op, key1); 737 } else { 738 TEE_SetOperationKey2(dst_op, key1, key2); 739 } 740 } 741 dst_op->info.handleState = src_op->info.handleState; 742 dst_op->info.keySize = src_op->info.keySize; 743 dst_op->operationState = src_op->operationState; 744 745 if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 746 dst_op->block_size != src_op->block_size) 747 TEE_Panic(0); 748 749 if (dst_op->buffer != NULL) { 750 if (src_op->buffer == NULL) 751 TEE_Panic(0); 752 753 memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 754 dst_op->buffer_offs = src_op->buffer_offs; 755 } else if (src_op->buffer != NULL) { 756 TEE_Panic(0); 757 } 758 759 res = utee_cryp_state_copy(dst_op->state, src_op->state); 760 if (res != TEE_SUCCESS) 761 TEE_Panic(res); 762 } 763 764 /* Cryptographic Operations API - Message Digest Functions */ 765 766 static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 767 uint32_t IVLen) 768 { 769 TEE_Result res; 770 771 /* 772 * Note : IV and IVLen are never used in current implementation 773 * This is why coherent values of IV and IVLen are not checked 774 */ 775 res = utee_hash_init(operation->state, IV, IVLen); 776 if (res != TEE_SUCCESS) 777 TEE_Panic(res); 778 operation->buffer_offs = 0; 779 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 780 } 781 782 void TEE_DigestUpdate(TEE_OperationHandle operation, 783 const void *chunk, uint32_t chunkSize) 784 { 785 TEE_Result res = TEE_ERROR_GENERIC; 786 787 if (operation == TEE_HANDLE_NULL || 788 operation->info.operationClass != TEE_OPERATION_DIGEST) 789 TEE_Panic(0); 790 791 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 792 793 res = utee_hash_update(operation->state, chunk, chunkSize); 794 if (res != TEE_SUCCESS) 795 TEE_Panic(res); 796 } 797 798 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 799 uint32_t chunkLen, void *hash, uint32_t *hashLen) 800 { 801 TEE_Result res; 802 uint64_t hl; 803 804 if ((operation == TEE_HANDLE_NULL) || 805 (!chunk && chunkLen) || 806 !hash || 807 !hashLen || 808 (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 809 res = TEE_ERROR_BAD_PARAMETERS; 810 goto out; 811 } 812 813 hl = *hashLen; 814 res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 815 *hashLen = hl; 816 if (res != TEE_SUCCESS) 817 goto out; 818 819 /* Reset operation state */ 820 init_hash_operation(operation, NULL, 0); 821 822 operation->operationState = TEE_OPERATION_STATE_INITIAL; 823 824 out: 825 if (res != TEE_SUCCESS && 826 res != TEE_ERROR_SHORT_BUFFER) 827 TEE_Panic(res); 828 829 return res; 830 } 831 832 /* Cryptographic Operations API - Symmetric Cipher Functions */ 833 834 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 835 uint32_t IVLen) 836 { 837 TEE_Result res; 838 839 if (operation == TEE_HANDLE_NULL) 840 TEE_Panic(0); 841 842 if (operation->info.operationClass != TEE_OPERATION_CIPHER) 843 TEE_Panic(0); 844 845 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 846 !(operation->key1)) 847 TEE_Panic(0); 848 849 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 850 TEE_ResetOperation(operation); 851 852 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 853 854 res = utee_cipher_init(operation->state, IV, IVLen); 855 if (res != TEE_SUCCESS) 856 TEE_Panic(res); 857 858 operation->buffer_offs = 0; 859 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 860 } 861 862 static TEE_Result tee_buffer_update( 863 TEE_OperationHandle op, 864 TEE_Result(*update_func)(unsigned long state, const void *src, 865 size_t slen, void *dst, uint64_t *dlen), 866 const void *src_data, size_t src_len, 867 void *dest_data, uint64_t *dest_len) 868 { 869 TEE_Result res; 870 const uint8_t *src = src_data; 871 size_t slen = src_len; 872 uint8_t *dst = dest_data; 873 size_t dlen = *dest_len; 874 size_t acc_dlen = 0; 875 uint64_t tmp_dlen; 876 size_t l; 877 size_t buffer_size; 878 size_t buffer_left; 879 880 if (!src) { 881 if (slen) 882 TEE_Panic(0); 883 goto out; 884 } 885 886 if (op->buffer_two_blocks) { 887 buffer_size = op->block_size * 2; 888 buffer_left = 1; 889 } else { 890 buffer_size = op->block_size; 891 buffer_left = 0; 892 } 893 894 if (op->buffer_offs > 0) { 895 /* Fill up complete block */ 896 if (op->buffer_offs < op->block_size) 897 l = MIN(slen, op->block_size - op->buffer_offs); 898 else 899 l = MIN(slen, buffer_size - op->buffer_offs); 900 memcpy(op->buffer + op->buffer_offs, src, l); 901 op->buffer_offs += l; 902 src += l; 903 slen -= l; 904 if ((op->buffer_offs % op->block_size) != 0) 905 goto out; /* Nothing left to do */ 906 } 907 908 /* If we can feed from buffer */ 909 if ((op->buffer_offs > 0) && 910 ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 911 l = ROUNDUP(op->buffer_offs + slen - buffer_size, 912 op->block_size); 913 l = MIN(op->buffer_offs, l); 914 tmp_dlen = dlen; 915 res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 916 if (res != TEE_SUCCESS) 917 TEE_Panic(res); 918 dst += tmp_dlen; 919 dlen -= tmp_dlen; 920 acc_dlen += tmp_dlen; 921 op->buffer_offs -= l; 922 if (op->buffer_offs > 0) { 923 /* 924 * Slen is small enough to be contained in rest buffer. 925 */ 926 memcpy(op->buffer, op->buffer + l, buffer_size - l); 927 memcpy(op->buffer + op->buffer_offs, src, slen); 928 op->buffer_offs += slen; 929 goto out; /* Nothing left to do */ 930 } 931 } 932 933 if (slen >= (buffer_size + buffer_left)) { 934 /* Buffer is empty, feed as much as possible from src */ 935 if (op->info.algorithm == TEE_ALG_AES_CTS) 936 l = ROUNDUP(slen - buffer_size, op->block_size); 937 else 938 l = ROUNDUP(slen - buffer_size + 1, op->block_size); 939 940 tmp_dlen = dlen; 941 res = update_func(op->state, src, l, dst, &tmp_dlen); 942 if (res != TEE_SUCCESS) 943 TEE_Panic(res); 944 src += l; 945 slen -= l; 946 dst += tmp_dlen; 947 dlen -= tmp_dlen; 948 acc_dlen += tmp_dlen; 949 } 950 951 /* Slen is small enough to be contained in buffer. */ 952 memcpy(op->buffer + op->buffer_offs, src, slen); 953 op->buffer_offs += slen; 954 955 out: 956 *dest_len = acc_dlen; 957 return TEE_SUCCESS; 958 } 959 960 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 961 uint32_t srcLen, void *destData, uint32_t *destLen) 962 { 963 TEE_Result res; 964 size_t req_dlen; 965 uint64_t dl; 966 967 if (operation == TEE_HANDLE_NULL || 968 (srcData == NULL && srcLen != 0) || 969 destLen == NULL || 970 (destData == NULL && *destLen != 0)) { 971 res = TEE_ERROR_BAD_PARAMETERS; 972 goto out; 973 } 974 975 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 976 res = TEE_ERROR_BAD_PARAMETERS; 977 goto out; 978 } 979 980 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 981 res = TEE_ERROR_BAD_PARAMETERS; 982 goto out; 983 } 984 985 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 986 res = TEE_ERROR_BAD_PARAMETERS; 987 goto out; 988 } 989 990 if (!srcData && !srcLen) { 991 *destLen = 0; 992 res = TEE_SUCCESS; 993 goto out; 994 } 995 996 /* Calculate required dlen */ 997 if (operation->block_size > 1) { 998 req_dlen = ((operation->buffer_offs + srcLen) / 999 operation->block_size) * operation->block_size; 1000 } else { 1001 req_dlen = srcLen; 1002 } 1003 if (operation->buffer_two_blocks) { 1004 if (req_dlen > operation->block_size * 2) 1005 req_dlen -= operation->block_size * 2; 1006 else 1007 req_dlen = 0; 1008 } 1009 /* 1010 * Check that required destLen is big enough before starting to feed 1011 * data to the algorithm. Errors during feeding of data are fatal as we 1012 * can't restore sync with this API. 1013 */ 1014 if (*destLen < req_dlen) { 1015 *destLen = req_dlen; 1016 res = TEE_ERROR_SHORT_BUFFER; 1017 goto out; 1018 } 1019 1020 dl = *destLen; 1021 if (operation->block_size > 1) { 1022 res = tee_buffer_update(operation, utee_cipher_update, srcData, 1023 srcLen, destData, &dl); 1024 } else { 1025 if (srcLen > 0) { 1026 res = utee_cipher_update(operation->state, srcData, 1027 srcLen, destData, &dl); 1028 } else { 1029 res = TEE_SUCCESS; 1030 dl = 0; 1031 } 1032 } 1033 *destLen = dl; 1034 1035 out: 1036 if (res != TEE_SUCCESS && 1037 res != TEE_ERROR_SHORT_BUFFER) 1038 TEE_Panic(res); 1039 1040 return res; 1041 } 1042 1043 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1044 const void *srcData, uint32_t srcLen, 1045 void *destData, uint32_t *destLen) 1046 { 1047 TEE_Result res; 1048 uint8_t *dst = destData; 1049 size_t acc_dlen = 0; 1050 uint64_t tmp_dlen; 1051 size_t req_dlen; 1052 1053 if (operation == TEE_HANDLE_NULL || 1054 (srcData == NULL && srcLen != 0) || 1055 destLen == NULL || 1056 (destData == NULL && *destLen != 0)) { 1057 res = TEE_ERROR_BAD_PARAMETERS; 1058 goto out; 1059 } 1060 1061 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1062 res = TEE_ERROR_BAD_PARAMETERS; 1063 goto out; 1064 } 1065 1066 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1067 res = TEE_ERROR_BAD_PARAMETERS; 1068 goto out; 1069 } 1070 1071 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1072 res = TEE_ERROR_BAD_PARAMETERS; 1073 goto out; 1074 } 1075 1076 /* 1077 * Check that the final block doesn't require padding for those 1078 * algorithms that requires client to supply padding. 1079 */ 1080 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1081 operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1082 operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1083 operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1084 operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1085 operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1086 operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1087 operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1088 if (((operation->buffer_offs + srcLen) % operation->block_size) 1089 != 0) { 1090 res = TEE_ERROR_BAD_PARAMETERS; 1091 goto out; 1092 } 1093 } 1094 1095 /* 1096 * Check that required destLen is big enough before starting to feed 1097 * data to the algorithm. Errors during feeding of data are fatal as we 1098 * can't restore sync with this API. 1099 */ 1100 if (operation->block_size > 1) { 1101 req_dlen = operation->buffer_offs + srcLen; 1102 } else { 1103 req_dlen = srcLen; 1104 } 1105 if (*destLen < req_dlen) { 1106 *destLen = req_dlen; 1107 res = TEE_ERROR_SHORT_BUFFER; 1108 goto out; 1109 } 1110 1111 tmp_dlen = *destLen - acc_dlen; 1112 if (operation->block_size > 1) { 1113 res = tee_buffer_update(operation, utee_cipher_update, 1114 srcData, srcLen, dst, &tmp_dlen); 1115 if (res != TEE_SUCCESS) 1116 goto out; 1117 1118 dst += tmp_dlen; 1119 acc_dlen += tmp_dlen; 1120 1121 tmp_dlen = *destLen - acc_dlen; 1122 res = utee_cipher_final(operation->state, operation->buffer, 1123 operation->buffer_offs, dst, &tmp_dlen); 1124 } else { 1125 res = utee_cipher_final(operation->state, srcData, 1126 srcLen, dst, &tmp_dlen); 1127 } 1128 if (res != TEE_SUCCESS) 1129 goto out; 1130 1131 acc_dlen += tmp_dlen; 1132 *destLen = acc_dlen; 1133 1134 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1135 1136 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1137 1138 out: 1139 if (res != TEE_SUCCESS && 1140 res != TEE_ERROR_SHORT_BUFFER) 1141 TEE_Panic(res); 1142 1143 return res; 1144 } 1145 1146 /* Cryptographic Operations API - MAC Functions */ 1147 1148 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1149 { 1150 if (operation == TEE_HANDLE_NULL) 1151 TEE_Panic(0); 1152 1153 if (operation->info.operationClass != TEE_OPERATION_MAC) 1154 TEE_Panic(0); 1155 1156 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1157 !(operation->key1)) 1158 TEE_Panic(0); 1159 1160 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1161 TEE_ResetOperation(operation); 1162 1163 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1164 1165 init_hash_operation(operation, IV, IVLen); 1166 } 1167 1168 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 1169 uint32_t chunkSize) 1170 { 1171 TEE_Result res; 1172 1173 if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1174 TEE_Panic(0); 1175 1176 if (operation->info.operationClass != TEE_OPERATION_MAC) 1177 TEE_Panic(0); 1178 1179 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1180 TEE_Panic(0); 1181 1182 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1183 TEE_Panic(0); 1184 1185 res = utee_hash_update(operation->state, chunk, chunkSize); 1186 if (res != TEE_SUCCESS) 1187 TEE_Panic(res); 1188 } 1189 1190 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 1191 const void *message, uint32_t messageLen, 1192 void *mac, uint32_t *macLen) 1193 { 1194 TEE_Result res; 1195 uint64_t ml; 1196 1197 if (operation == TEE_HANDLE_NULL || 1198 (message == NULL && messageLen != 0) || 1199 mac == NULL || 1200 macLen == NULL) { 1201 res = TEE_ERROR_BAD_PARAMETERS; 1202 goto out; 1203 } 1204 1205 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1206 res = TEE_ERROR_BAD_PARAMETERS; 1207 goto out; 1208 } 1209 1210 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1211 res = TEE_ERROR_BAD_PARAMETERS; 1212 goto out; 1213 } 1214 1215 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1216 res = TEE_ERROR_BAD_PARAMETERS; 1217 goto out; 1218 } 1219 1220 ml = *macLen; 1221 res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1222 *macLen = ml; 1223 if (res != TEE_SUCCESS) 1224 goto out; 1225 1226 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1227 1228 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1229 1230 out: 1231 if (res != TEE_SUCCESS && 1232 res != TEE_ERROR_SHORT_BUFFER) 1233 TEE_Panic(res); 1234 1235 return res; 1236 } 1237 1238 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 1239 const void *message, uint32_t messageLen, 1240 const void *mac, uint32_t macLen) 1241 { 1242 TEE_Result res; 1243 uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 1244 uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1245 1246 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1247 res = TEE_ERROR_BAD_PARAMETERS; 1248 goto out; 1249 } 1250 1251 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1252 res = TEE_ERROR_BAD_PARAMETERS; 1253 goto out; 1254 } 1255 1256 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1257 res = TEE_ERROR_BAD_PARAMETERS; 1258 goto out; 1259 } 1260 1261 res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1262 &computed_mac_size); 1263 if (res != TEE_SUCCESS) 1264 goto out; 1265 1266 if (computed_mac_size != macLen) { 1267 res = TEE_ERROR_MAC_INVALID; 1268 goto out; 1269 } 1270 1271 if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 1272 res = TEE_ERROR_MAC_INVALID; 1273 goto out; 1274 } 1275 1276 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1277 1278 out: 1279 if (res != TEE_SUCCESS && 1280 res != TEE_ERROR_MAC_INVALID) 1281 TEE_Panic(res); 1282 1283 return res; 1284 } 1285 1286 /* Cryptographic Operations API - Authenticated Encryption Functions */ 1287 1288 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 1289 uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1290 uint32_t payloadLen) 1291 { 1292 TEE_Result res; 1293 1294 if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1295 res = TEE_ERROR_BAD_PARAMETERS; 1296 goto out; 1297 } 1298 1299 if (operation->info.operationClass != TEE_OPERATION_AE) { 1300 res = TEE_ERROR_BAD_PARAMETERS; 1301 goto out; 1302 } 1303 1304 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1305 res = TEE_ERROR_BAD_PARAMETERS; 1306 goto out; 1307 } 1308 1309 /* 1310 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1311 * in the implementation. But AES-GCM spec doesn't specify the tag len 1312 * according to the same principle so we have to check here instead to 1313 * be GP compliant. 1314 */ 1315 if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1316 /* 1317 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1318 */ 1319 if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1320 res = TEE_ERROR_NOT_SUPPORTED; 1321 goto out; 1322 } 1323 } 1324 1325 res = utee_authenc_init(operation->state, nonce, nonceLen, 1326 tagLen / 8, AADLen, payloadLen); 1327 if (res != TEE_SUCCESS) 1328 goto out; 1329 1330 operation->ae_tag_len = tagLen / 8; 1331 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1332 1333 out: 1334 if (res != TEE_SUCCESS && 1335 res != TEE_ERROR_NOT_SUPPORTED) 1336 TEE_Panic(res); 1337 1338 return res; 1339 } 1340 1341 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 1342 uint32_t AADdataLen) 1343 { 1344 TEE_Result res; 1345 1346 if (operation == TEE_HANDLE_NULL || 1347 (AADdata == NULL && AADdataLen != 0)) 1348 TEE_Panic(0); 1349 1350 if (operation->info.operationClass != TEE_OPERATION_AE) 1351 TEE_Panic(0); 1352 1353 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1354 TEE_Panic(0); 1355 1356 res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1357 1358 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1359 1360 if (res != TEE_SUCCESS) 1361 TEE_Panic(res); 1362 } 1363 1364 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 1365 uint32_t srcLen, void *destData, uint32_t *destLen) 1366 { 1367 TEE_Result res; 1368 size_t req_dlen; 1369 uint64_t dl; 1370 1371 if (operation == TEE_HANDLE_NULL || 1372 (srcData == NULL && srcLen != 0) || 1373 destLen == NULL || 1374 (destData == NULL && *destLen != 0)) { 1375 res = TEE_ERROR_BAD_PARAMETERS; 1376 goto out; 1377 } 1378 1379 if (operation->info.operationClass != TEE_OPERATION_AE) { 1380 res = TEE_ERROR_BAD_PARAMETERS; 1381 goto out; 1382 } 1383 1384 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1385 res = TEE_ERROR_BAD_PARAMETERS; 1386 goto out; 1387 } 1388 1389 if (!srcData && !srcLen) { 1390 *destLen = 0; 1391 res = TEE_SUCCESS; 1392 goto out; 1393 } 1394 1395 /* 1396 * Check that required destLen is big enough before starting to feed 1397 * data to the algorithm. Errors during feeding of data are fatal as we 1398 * can't restore sync with this API. 1399 */ 1400 if (operation->block_size > 1) { 1401 req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1402 operation->block_size); 1403 } else { 1404 req_dlen = srcLen; 1405 } 1406 1407 if (*destLen < req_dlen) { 1408 *destLen = req_dlen; 1409 res = TEE_ERROR_SHORT_BUFFER; 1410 goto out; 1411 } 1412 1413 dl = *destLen; 1414 if (operation->block_size > 1) { 1415 res = tee_buffer_update(operation, utee_authenc_update_payload, 1416 srcData, srcLen, destData, &dl); 1417 } else { 1418 if (srcLen > 0) { 1419 res = utee_authenc_update_payload(operation->state, 1420 srcData, srcLen, 1421 destData, &dl); 1422 } else { 1423 dl = 0; 1424 res = TEE_SUCCESS; 1425 } 1426 } 1427 if (res != TEE_SUCCESS) 1428 goto out; 1429 1430 *destLen = dl; 1431 1432 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1433 1434 out: 1435 if (res != TEE_SUCCESS && 1436 res != TEE_ERROR_SHORT_BUFFER) 1437 TEE_Panic(res); 1438 1439 return res; 1440 } 1441 1442 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1443 const void *srcData, uint32_t srcLen, 1444 void *destData, uint32_t *destLen, void *tag, 1445 uint32_t *tagLen) 1446 { 1447 TEE_Result res; 1448 uint8_t *dst = destData; 1449 size_t acc_dlen = 0; 1450 uint64_t tmp_dlen; 1451 size_t req_dlen; 1452 uint64_t tl; 1453 1454 if (operation == TEE_HANDLE_NULL || 1455 (srcData == NULL && srcLen != 0) || 1456 destLen == NULL || 1457 (destData == NULL && *destLen != 0) || 1458 tag == NULL || tagLen == NULL) { 1459 res = TEE_ERROR_BAD_PARAMETERS; 1460 goto out; 1461 } 1462 1463 if (operation->info.operationClass != TEE_OPERATION_AE) { 1464 res = TEE_ERROR_BAD_PARAMETERS; 1465 goto out; 1466 } 1467 1468 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1469 res = TEE_ERROR_BAD_PARAMETERS; 1470 goto out; 1471 } 1472 1473 /* 1474 * Check that required destLen is big enough before starting to feed 1475 * data to the algorithm. Errors during feeding of data are fatal as we 1476 * can't restore sync with this API. 1477 * 1478 * Need to check this before update_payload since sync would be lost if 1479 * we return short buffer after that. 1480 */ 1481 res = TEE_ERROR_GENERIC; 1482 1483 req_dlen = operation->buffer_offs + srcLen; 1484 if (*destLen < req_dlen) { 1485 *destLen = req_dlen; 1486 res = TEE_ERROR_SHORT_BUFFER; 1487 } 1488 1489 if (*tagLen < operation->ae_tag_len) { 1490 *tagLen = operation->ae_tag_len; 1491 res = TEE_ERROR_SHORT_BUFFER; 1492 } 1493 1494 if (res == TEE_ERROR_SHORT_BUFFER) 1495 goto out; 1496 1497 tl = *tagLen; 1498 tmp_dlen = *destLen - acc_dlen; 1499 if (operation->block_size > 1) { 1500 res = tee_buffer_update(operation, utee_authenc_update_payload, 1501 srcData, srcLen, dst, &tmp_dlen); 1502 if (res != TEE_SUCCESS) 1503 goto out; 1504 1505 dst += tmp_dlen; 1506 acc_dlen += tmp_dlen; 1507 1508 tmp_dlen = *destLen - acc_dlen; 1509 res = utee_authenc_enc_final(operation->state, 1510 operation->buffer, 1511 operation->buffer_offs, dst, 1512 &tmp_dlen, tag, &tl); 1513 } else { 1514 res = utee_authenc_enc_final(operation->state, srcData, 1515 srcLen, dst, &tmp_dlen, 1516 tag, &tl); 1517 } 1518 *tagLen = tl; 1519 if (res != TEE_SUCCESS) 1520 goto out; 1521 1522 acc_dlen += tmp_dlen; 1523 *destLen = acc_dlen; 1524 1525 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1526 1527 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1528 1529 out: 1530 if (res != TEE_SUCCESS && 1531 res != TEE_ERROR_SHORT_BUFFER) 1532 TEE_Panic(res); 1533 1534 return res; 1535 } 1536 1537 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1538 const void *srcData, uint32_t srcLen, 1539 void *destData, uint32_t *destLen, void *tag, 1540 uint32_t tagLen) 1541 { 1542 TEE_Result res; 1543 uint8_t *dst = destData; 1544 size_t acc_dlen = 0; 1545 uint64_t tmp_dlen; 1546 size_t req_dlen; 1547 1548 if (operation == TEE_HANDLE_NULL || 1549 (srcData == NULL && srcLen != 0) || 1550 destLen == NULL || 1551 (destData == NULL && *destLen != 0) || 1552 (tag == NULL && tagLen != 0)) { 1553 res = TEE_ERROR_BAD_PARAMETERS; 1554 goto out; 1555 } 1556 1557 if (operation->info.operationClass != TEE_OPERATION_AE) { 1558 res = TEE_ERROR_BAD_PARAMETERS; 1559 goto out; 1560 } 1561 1562 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1563 res = TEE_ERROR_BAD_PARAMETERS; 1564 goto out; 1565 } 1566 1567 /* 1568 * Check that required destLen is big enough before starting to feed 1569 * data to the algorithm. Errors during feeding of data are fatal as we 1570 * can't restore sync with this API. 1571 */ 1572 req_dlen = operation->buffer_offs + srcLen; 1573 if (*destLen < req_dlen) { 1574 *destLen = req_dlen; 1575 res = TEE_ERROR_SHORT_BUFFER; 1576 goto out; 1577 } 1578 1579 tmp_dlen = *destLen - acc_dlen; 1580 if (operation->block_size > 1) { 1581 res = tee_buffer_update(operation, utee_authenc_update_payload, 1582 srcData, srcLen, dst, &tmp_dlen); 1583 if (res != TEE_SUCCESS) 1584 goto out; 1585 1586 dst += tmp_dlen; 1587 acc_dlen += tmp_dlen; 1588 1589 tmp_dlen = *destLen - acc_dlen; 1590 res = utee_authenc_dec_final(operation->state, 1591 operation->buffer, 1592 operation->buffer_offs, dst, 1593 &tmp_dlen, tag, tagLen); 1594 } else { 1595 res = utee_authenc_dec_final(operation->state, srcData, 1596 srcLen, dst, &tmp_dlen, 1597 tag, tagLen); 1598 } 1599 if (res != TEE_SUCCESS) 1600 goto out; 1601 1602 /* Supplied tagLen should match what we initiated with */ 1603 if (tagLen != operation->ae_tag_len) 1604 res = TEE_ERROR_MAC_INVALID; 1605 1606 acc_dlen += tmp_dlen; 1607 *destLen = acc_dlen; 1608 1609 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1610 1611 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1612 1613 out: 1614 if (res != TEE_SUCCESS && 1615 res != TEE_ERROR_SHORT_BUFFER && 1616 res != TEE_ERROR_MAC_INVALID) 1617 TEE_Panic(res); 1618 1619 return res; 1620 } 1621 1622 /* Cryptographic Operations API - Asymmetric Functions */ 1623 1624 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 1625 const TEE_Attribute *params, 1626 uint32_t paramCount, const void *srcData, 1627 uint32_t srcLen, void *destData, 1628 uint32_t *destLen) 1629 { 1630 TEE_Result res; 1631 struct utee_attribute ua[paramCount]; 1632 uint64_t dl; 1633 1634 if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1635 destLen == NULL || (destData == NULL && *destLen != 0)) 1636 TEE_Panic(0); 1637 if (params == NULL && paramCount != 0) 1638 TEE_Panic(0); 1639 if (!operation->key1) 1640 TEE_Panic(0); 1641 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1642 TEE_Panic(0); 1643 if (operation->info.mode != TEE_MODE_ENCRYPT) 1644 TEE_Panic(0); 1645 1646 __utee_from_attr(ua, params, paramCount); 1647 dl = *destLen; 1648 res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1649 srcLen, destData, &dl); 1650 *destLen = dl; 1651 1652 if (res != TEE_SUCCESS && 1653 res != TEE_ERROR_SHORT_BUFFER && 1654 res != TEE_ERROR_BAD_PARAMETERS) 1655 TEE_Panic(res); 1656 1657 return res; 1658 } 1659 1660 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 1661 const TEE_Attribute *params, 1662 uint32_t paramCount, const void *srcData, 1663 uint32_t srcLen, void *destData, 1664 uint32_t *destLen) 1665 { 1666 TEE_Result res; 1667 struct utee_attribute ua[paramCount]; 1668 uint64_t dl; 1669 1670 if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1671 destLen == NULL || (destData == NULL && *destLen != 0)) 1672 TEE_Panic(0); 1673 if (params == NULL && paramCount != 0) 1674 TEE_Panic(0); 1675 if (!operation->key1) 1676 TEE_Panic(0); 1677 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1678 TEE_Panic(0); 1679 if (operation->info.mode != TEE_MODE_DECRYPT) 1680 TEE_Panic(0); 1681 1682 __utee_from_attr(ua, params, paramCount); 1683 dl = *destLen; 1684 res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1685 srcLen, destData, &dl); 1686 *destLen = dl; 1687 1688 if (res != TEE_SUCCESS && 1689 res != TEE_ERROR_SHORT_BUFFER && 1690 res != TEE_ERROR_BAD_PARAMETERS) 1691 TEE_Panic(res); 1692 1693 return res; 1694 } 1695 1696 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 1697 const TEE_Attribute *params, 1698 uint32_t paramCount, const void *digest, 1699 uint32_t digestLen, void *signature, 1700 uint32_t *signatureLen) 1701 { 1702 TEE_Result res; 1703 struct utee_attribute ua[paramCount]; 1704 uint64_t sl; 1705 1706 if (operation == TEE_HANDLE_NULL || 1707 (digest == NULL && digestLen != 0) || 1708 signature == NULL || signatureLen == NULL) 1709 TEE_Panic(0); 1710 if (params == NULL && paramCount != 0) 1711 TEE_Panic(0); 1712 if (!operation->key1) 1713 TEE_Panic(0); 1714 if (operation->info.operationClass != 1715 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1716 TEE_Panic(0); 1717 if (operation->info.mode != TEE_MODE_SIGN) 1718 TEE_Panic(0); 1719 1720 __utee_from_attr(ua, params, paramCount); 1721 sl = *signatureLen; 1722 res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1723 digestLen, signature, &sl); 1724 *signatureLen = sl; 1725 1726 if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1727 TEE_Panic(res); 1728 1729 return res; 1730 } 1731 1732 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 1733 const TEE_Attribute *params, 1734 uint32_t paramCount, const void *digest, 1735 uint32_t digestLen, 1736 const void *signature, 1737 uint32_t signatureLen) 1738 { 1739 TEE_Result res; 1740 struct utee_attribute ua[paramCount]; 1741 1742 if (operation == TEE_HANDLE_NULL || 1743 (digest == NULL && digestLen != 0) || 1744 (signature == NULL && signatureLen != 0)) 1745 TEE_Panic(0); 1746 if (params == NULL && paramCount != 0) 1747 TEE_Panic(0); 1748 if (!operation->key1) 1749 TEE_Panic(0); 1750 if (operation->info.operationClass != 1751 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1752 TEE_Panic(0); 1753 if (operation->info.mode != TEE_MODE_VERIFY) 1754 TEE_Panic(0); 1755 1756 __utee_from_attr(ua, params, paramCount); 1757 res = utee_asymm_verify(operation->state, ua, paramCount, digest, 1758 digestLen, signature, signatureLen); 1759 1760 if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1761 TEE_Panic(res); 1762 1763 return res; 1764 } 1765 1766 /* Cryptographic Operations API - Key Derivation Functions */ 1767 1768 void TEE_DeriveKey(TEE_OperationHandle operation, 1769 const TEE_Attribute *params, uint32_t paramCount, 1770 TEE_ObjectHandle derivedKey) 1771 { 1772 TEE_Result res; 1773 TEE_ObjectInfo key_info; 1774 struct utee_attribute ua[paramCount]; 1775 1776 if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1777 TEE_Panic(0); 1778 if (params == NULL && paramCount != 0) 1779 TEE_Panic(0); 1780 if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 1781 TEE_OPERATION_KEY_DERIVATION) 1782 TEE_Panic(0); 1783 1784 if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1785 TEE_Panic(0); 1786 if (!operation->key1) 1787 TEE_Panic(0); 1788 if (operation->info.mode != TEE_MODE_DERIVE) 1789 TEE_Panic(0); 1790 if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1791 TEE_Panic(0); 1792 1793 res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1794 if (res != TEE_SUCCESS) 1795 TEE_Panic(res); 1796 1797 if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1798 TEE_Panic(0); 1799 if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1800 TEE_Panic(0); 1801 1802 __utee_from_attr(ua, params, paramCount); 1803 res = utee_cryp_derive_key(operation->state, ua, paramCount, 1804 (unsigned long)derivedKey); 1805 if (res != TEE_SUCCESS) 1806 TEE_Panic(res); 1807 } 1808 1809 /* Cryptographic Operations API - Random Number Generation Functions */ 1810 1811 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1812 { 1813 TEE_Result res; 1814 1815 res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1816 if (res != TEE_SUCCESS) 1817 TEE_Panic(res); 1818 } 1819 1820 int rand(void) 1821 { 1822 int rc; 1823 1824 TEE_GenerateRandom(&rc, sizeof(rc)); 1825 1826 /* 1827 * RAND_MAX is the larges int, INT_MAX which is all bits but the 1828 * highest bit set. 1829 */ 1830 return rc & RAND_MAX; 1831 } 1832