1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2021, SumUp Services GmbH 5 */ 6 #include <config.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <string_ext.h> 10 #include <tee_api.h> 11 #include <tee_api_defines_extensions.h> 12 #include <tee_internal_api_extensions.h> 13 #include <utee_syscalls.h> 14 #include <utee_defines.h> 15 #include <util.h> 16 #include "tee_api_private.h" 17 18 struct __TEE_OperationHandle { 19 TEE_OperationInfo info; 20 TEE_ObjectHandle key1; 21 TEE_ObjectHandle key2; 22 uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 23 uint8_t *buffer; /* buffer to collect complete blocks */ 24 bool buffer_two_blocks; /* True if two blocks need to be buffered */ 25 size_t block_size; /* Block size of cipher */ 26 size_t buffer_offs; /* Offset in buffer */ 27 uint32_t state; /* Handle to state in TEE Core */ 28 }; 29 30 /* Cryptographic Operations API - Generic Operation Functions */ 31 32 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 33 uint32_t algorithm, uint32_t mode, 34 uint32_t maxKeySize) 35 { 36 TEE_Result res; 37 TEE_OperationHandle op = TEE_HANDLE_NULL; 38 uint32_t handle_state = 0; 39 size_t block_size = 1; 40 uint32_t req_key_usage; 41 bool with_private_key = false; 42 bool buffer_two_blocks = false; 43 44 if (!operation) 45 TEE_Panic(0); 46 47 if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP || 48 algorithm == TEE_ALG_SM4_XTS) 49 handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 50 51 /* Check algorithm max key size */ 52 switch (algorithm) { 53 case TEE_ALG_DSA_SHA1: 54 if (maxKeySize < 512) 55 return TEE_ERROR_NOT_SUPPORTED; 56 if (maxKeySize > 1024) 57 return TEE_ERROR_NOT_SUPPORTED; 58 if (maxKeySize % 64 != 0) 59 return TEE_ERROR_NOT_SUPPORTED; 60 break; 61 62 case TEE_ALG_DSA_SHA224: 63 if (maxKeySize != 2048) 64 return TEE_ERROR_NOT_SUPPORTED; 65 break; 66 67 case TEE_ALG_DSA_SHA256: 68 if (maxKeySize != 2048 && maxKeySize != 3072) 69 return TEE_ERROR_NOT_SUPPORTED; 70 break; 71 72 case TEE_ALG_ECDSA_SHA1: 73 case __OPTEE_ALG_ECDSA_P192: 74 case __OPTEE_ALG_ECDH_P192: 75 if (maxKeySize != 192) 76 return TEE_ERROR_NOT_SUPPORTED; 77 break; 78 79 case TEE_ALG_ECDSA_SHA224: 80 case __OPTEE_ALG_ECDSA_P224: 81 case __OPTEE_ALG_ECDH_P224: 82 if (maxKeySize != 224) 83 return TEE_ERROR_NOT_SUPPORTED; 84 break; 85 86 case TEE_ALG_ECDSA_SHA256: 87 case __OPTEE_ALG_ECDSA_P256: 88 case __OPTEE_ALG_ECDH_P256: 89 case TEE_ALG_SM2_PKE: 90 case TEE_ALG_SM2_DSA_SM3: 91 if (maxKeySize != 256) 92 return TEE_ERROR_NOT_SUPPORTED; 93 break; 94 95 case TEE_ALG_SM2_KEP: 96 /* Two 256-bit keys */ 97 if (maxKeySize != 512) 98 return TEE_ERROR_NOT_SUPPORTED; 99 break; 100 101 case TEE_ALG_ECDSA_SHA384: 102 case __OPTEE_ALG_ECDSA_P384: 103 case __OPTEE_ALG_ECDH_P384: 104 if (maxKeySize != 384) 105 return TEE_ERROR_NOT_SUPPORTED; 106 break; 107 108 case TEE_ALG_ECDSA_SHA512: 109 case __OPTEE_ALG_ECDSA_P521: 110 case __OPTEE_ALG_ECDH_P521: 111 if (maxKeySize != 521) 112 return TEE_ERROR_NOT_SUPPORTED; 113 break; 114 115 case TEE_ALG_ECDH_DERIVE_SHARED_SECRET: 116 if (maxKeySize > 521) 117 return TEE_ERROR_NOT_SUPPORTED; 118 break; 119 120 case TEE_ALG_ED25519: 121 case TEE_ALG_X25519: 122 if (maxKeySize != 256) 123 return TEE_ERROR_NOT_SUPPORTED; 124 break; 125 default: 126 break; 127 } 128 129 /* Check algorithm mode (and maxKeySize for digests) */ 130 switch (algorithm) { 131 case TEE_ALG_AES_CTS: 132 case TEE_ALG_AES_XTS: 133 case TEE_ALG_SM4_XTS: 134 buffer_two_blocks = true; 135 fallthrough; 136 case TEE_ALG_AES_ECB_NOPAD: 137 case TEE_ALG_AES_CBC_NOPAD: 138 case TEE_ALG_AES_CCM: 139 case TEE_ALG_DES_ECB_NOPAD: 140 case TEE_ALG_DES_CBC_NOPAD: 141 case TEE_ALG_DES3_ECB_NOPAD: 142 case TEE_ALG_DES3_CBC_NOPAD: 143 case TEE_ALG_SM4_ECB_NOPAD: 144 case TEE_ALG_SM4_CBC_NOPAD: 145 case TEE_ALG_SM4_CTR: 146 if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 147 block_size = TEE_AES_BLOCK_SIZE; 148 else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 149 block_size = TEE_SM4_BLOCK_SIZE; 150 else 151 block_size = TEE_DES_BLOCK_SIZE; 152 fallthrough; 153 case TEE_ALG_AES_CTR: 154 case TEE_ALG_AES_GCM: 155 if (mode == TEE_MODE_ENCRYPT) 156 req_key_usage = TEE_USAGE_ENCRYPT; 157 else if (mode == TEE_MODE_DECRYPT) 158 req_key_usage = TEE_USAGE_DECRYPT; 159 else 160 return TEE_ERROR_NOT_SUPPORTED; 161 break; 162 163 #if defined(CFG_CRYPTO_RSASSA_NA1) 164 case TEE_ALG_RSASSA_PKCS1_V1_5: 165 #endif 166 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 167 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 168 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 169 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 170 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 171 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 172 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 173 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 174 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 175 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 176 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 177 case TEE_ALG_DSA_SHA1: 178 case TEE_ALG_DSA_SHA224: 179 case TEE_ALG_DSA_SHA256: 180 case TEE_ALG_ECDSA_SHA1: 181 case TEE_ALG_ECDSA_SHA224: 182 case TEE_ALG_ECDSA_SHA256: 183 case TEE_ALG_ECDSA_SHA384: 184 case TEE_ALG_ECDSA_SHA512: 185 case __OPTEE_ALG_ECDSA_P192: 186 case __OPTEE_ALG_ECDSA_P224: 187 case __OPTEE_ALG_ECDSA_P256: 188 case __OPTEE_ALG_ECDSA_P384: 189 case __OPTEE_ALG_ECDSA_P521: 190 case TEE_ALG_SM2_DSA_SM3: 191 case TEE_ALG_ED25519: 192 if (mode == TEE_MODE_SIGN) { 193 with_private_key = true; 194 req_key_usage = TEE_USAGE_SIGN; 195 } else if (mode == TEE_MODE_VERIFY) { 196 req_key_usage = TEE_USAGE_VERIFY; 197 } else { 198 return TEE_ERROR_NOT_SUPPORTED; 199 } 200 break; 201 202 case TEE_ALG_RSAES_PKCS1_V1_5: 203 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 204 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 205 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 206 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 207 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 208 case TEE_ALG_SM2_PKE: 209 if (mode == TEE_MODE_ENCRYPT) { 210 req_key_usage = TEE_USAGE_ENCRYPT; 211 } else if (mode == TEE_MODE_DECRYPT) { 212 with_private_key = true; 213 req_key_usage = TEE_USAGE_DECRYPT; 214 } else { 215 return TEE_ERROR_NOT_SUPPORTED; 216 } 217 break; 218 219 case TEE_ALG_RSA_NOPAD: 220 if (mode == TEE_MODE_ENCRYPT) { 221 req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 222 } else if (mode == TEE_MODE_DECRYPT) { 223 with_private_key = true; 224 req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 225 } else { 226 return TEE_ERROR_NOT_SUPPORTED; 227 } 228 break; 229 230 case TEE_ALG_DH_DERIVE_SHARED_SECRET: 231 case TEE_ALG_ECDH_DERIVE_SHARED_SECRET: 232 case __OPTEE_ALG_ECDH_P192: 233 case __OPTEE_ALG_ECDH_P224: 234 case __OPTEE_ALG_ECDH_P256: 235 case __OPTEE_ALG_ECDH_P384: 236 case __OPTEE_ALG_ECDH_P521: 237 case TEE_ALG_HKDF_MD5_DERIVE_KEY: 238 case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 239 case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 240 case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 241 case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 242 case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 243 case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 244 case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 245 case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 246 case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 247 case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 248 case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 249 case TEE_ALG_SM2_KEP: 250 case TEE_ALG_X25519: 251 if (mode != TEE_MODE_DERIVE) 252 return TEE_ERROR_NOT_SUPPORTED; 253 with_private_key = true; 254 req_key_usage = TEE_USAGE_DERIVE; 255 break; 256 257 case TEE_ALG_MD5: 258 case TEE_ALG_SHA1: 259 case TEE_ALG_SHA224: 260 case TEE_ALG_SHA256: 261 case TEE_ALG_SHA384: 262 case TEE_ALG_SHA512: 263 case TEE_ALG_SM3: 264 if (mode != TEE_MODE_DIGEST) 265 return TEE_ERROR_NOT_SUPPORTED; 266 if (maxKeySize) 267 return TEE_ERROR_NOT_SUPPORTED; 268 /* v1.1: flags always set for digest operations */ 269 handle_state |= TEE_HANDLE_FLAG_KEY_SET; 270 req_key_usage = 0; 271 break; 272 273 case TEE_ALG_DES_CBC_MAC_NOPAD: 274 case TEE_ALG_AES_CBC_MAC_NOPAD: 275 case TEE_ALG_AES_CBC_MAC_PKCS5: 276 case TEE_ALG_AES_CMAC: 277 case TEE_ALG_DES_CBC_MAC_PKCS5: 278 case TEE_ALG_DES3_CBC_MAC_NOPAD: 279 case TEE_ALG_DES3_CBC_MAC_PKCS5: 280 case TEE_ALG_DES3_CMAC: 281 case TEE_ALG_HMAC_MD5: 282 case TEE_ALG_HMAC_SHA1: 283 case TEE_ALG_HMAC_SHA224: 284 case TEE_ALG_HMAC_SHA256: 285 case TEE_ALG_HMAC_SHA384: 286 case TEE_ALG_HMAC_SHA512: 287 case TEE_ALG_HMAC_SM3: 288 if (mode != TEE_MODE_MAC) 289 return TEE_ERROR_NOT_SUPPORTED; 290 req_key_usage = TEE_USAGE_MAC; 291 break; 292 293 default: 294 return TEE_ERROR_NOT_SUPPORTED; 295 } 296 297 op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 298 if (!op) 299 return TEE_ERROR_OUT_OF_MEMORY; 300 301 op->info.algorithm = algorithm; 302 op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 303 #ifdef CFG_CRYPTO_RSASSA_NA1 304 if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 305 op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 306 #endif 307 op->info.mode = mode; 308 op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm); 309 op->info.maxKeySize = maxKeySize; 310 op->info.requiredKeyUsage = req_key_usage; 311 op->info.handleState = handle_state; 312 313 if (block_size > 1) { 314 size_t buffer_size = block_size; 315 316 if (buffer_two_blocks) 317 buffer_size *= 2; 318 319 op->buffer = TEE_Malloc(buffer_size, 320 TEE_USER_MEM_HINT_NO_FILL_ZERO); 321 if (op->buffer == NULL) { 322 res = TEE_ERROR_OUT_OF_MEMORY; 323 goto out; 324 } 325 } 326 op->block_size = block_size; 327 op->buffer_two_blocks = buffer_two_blocks; 328 329 if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 330 uint32_t mks = maxKeySize; 331 TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 332 with_private_key); 333 334 /* 335 * If two keys are expected the max key size is the sum of 336 * the size of both keys. 337 */ 338 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 339 mks /= 2; 340 341 res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 342 if (res != TEE_SUCCESS) 343 goto out; 344 345 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 346 res = TEE_AllocateTransientObject(key_type, mks, 347 &op->key2); 348 if (res != TEE_SUCCESS) 349 goto out; 350 } 351 } 352 353 res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 354 (unsigned long)op->key2, &op->state); 355 if (res != TEE_SUCCESS) 356 goto out; 357 358 /* 359 * Initialize digest operations 360 * Other multi-stage operations initialized w/ TEE_xxxInit functions 361 * Non-applicable on asymmetric operations 362 */ 363 if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 364 res = _utee_hash_init(op->state, NULL, 0); 365 if (res != TEE_SUCCESS) 366 goto out; 367 /* v1.1: flags always set for digest operations */ 368 op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 369 } 370 371 op->operationState = TEE_OPERATION_STATE_INITIAL; 372 373 *operation = op; 374 375 out: 376 if (res != TEE_SUCCESS) { 377 if (res != TEE_ERROR_OUT_OF_MEMORY && 378 res != TEE_ERROR_NOT_SUPPORTED) 379 TEE_Panic(res); 380 if (op) { 381 if (op->state) { 382 TEE_FreeOperation(op); 383 } else { 384 TEE_Free(op->buffer); 385 TEE_FreeTransientObject(op->key1); 386 TEE_FreeTransientObject(op->key2); 387 TEE_Free(op); 388 } 389 } 390 } 391 392 return res; 393 } 394 395 void TEE_FreeOperation(TEE_OperationHandle operation) 396 { 397 TEE_Result res; 398 399 if (operation == TEE_HANDLE_NULL) 400 TEE_Panic(0); 401 402 /* 403 * Note that keys should not be freed here, since they are 404 * claimed by the operation they will be freed by 405 * utee_cryp_state_free(). 406 */ 407 res = _utee_cryp_state_free(operation->state); 408 if (res != TEE_SUCCESS) 409 TEE_Panic(res); 410 411 TEE_Free(operation->buffer); 412 TEE_Free(operation); 413 } 414 415 void TEE_GetOperationInfo(TEE_OperationHandle operation, 416 TEE_OperationInfo *operationInfo) 417 { 418 if (operation == TEE_HANDLE_NULL) 419 TEE_Panic(0); 420 421 __utee_check_out_annotation(operationInfo, sizeof(*operationInfo)); 422 423 *operationInfo = operation->info; 424 if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 425 operationInfo->keySize = 0; 426 operationInfo->requiredKeyUsage = 0; 427 } 428 } 429 430 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op, 431 TEE_OperationInfoMultiple *op_info, 432 size_t *size) 433 { 434 TEE_Result res = TEE_SUCCESS; 435 TEE_ObjectInfo kinfo = { }; 436 size_t max_key_count = 0; 437 bool two_keys = false; 438 439 if (op == TEE_HANDLE_NULL) { 440 res = TEE_ERROR_BAD_PARAMETERS; 441 goto out; 442 } 443 444 __utee_check_outbuf_annotation(op_info, size); 445 446 if (*size < sizeof(*op_info)) { 447 res = TEE_ERROR_BAD_PARAMETERS; 448 goto out; 449 } 450 max_key_count = (*size - sizeof(*op_info)) / 451 sizeof(TEE_OperationInfoKey); 452 453 TEE_MemFill(op_info, 0, *size); 454 455 /* Two keys flag (TEE_ALG_AES_XTS only) */ 456 two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 457 458 if (op->info.mode == TEE_MODE_DIGEST) { 459 op_info->numberOfKeys = 0; 460 } else if (!two_keys) { 461 if (max_key_count < 1) { 462 res = TEE_ERROR_SHORT_BUFFER; 463 goto out; 464 } 465 466 res = TEE_GetObjectInfo1(op->key1, &kinfo); 467 /* Key1 is not a valid handle, "can't happen". */ 468 if (res) 469 goto out; 470 471 op_info->keyInformation[0].keySize = kinfo.objectSize; 472 op_info->keyInformation[0].requiredKeyUsage = 473 op->info.requiredKeyUsage; 474 op_info->numberOfKeys = 1; 475 } else { 476 if (max_key_count < 2) { 477 res = TEE_ERROR_SHORT_BUFFER; 478 goto out; 479 } 480 481 res = TEE_GetObjectInfo1(op->key1, &kinfo); 482 /* Key1 is not a valid handle, "can't happen". */ 483 if (res) 484 goto out; 485 486 op_info->keyInformation[0].keySize = kinfo.objectSize; 487 op_info->keyInformation[0].requiredKeyUsage = 488 op->info.requiredKeyUsage; 489 490 res = TEE_GetObjectInfo1(op->key2, &kinfo); 491 /* Key2 is not a valid handle, "can't happen". */ 492 if (res) 493 goto out; 494 495 op_info->keyInformation[1].keySize = kinfo.objectSize; 496 op_info->keyInformation[1].requiredKeyUsage = 497 op->info.requiredKeyUsage; 498 499 op_info->numberOfKeys = 2; 500 } 501 502 op_info->algorithm = op->info.algorithm; 503 op_info->operationClass = op->info.operationClass; 504 op_info->mode = op->info.mode; 505 op_info->digestLength = op->info.digestLength; 506 op_info->maxKeySize = op->info.maxKeySize; 507 op_info->handleState = op->info.handleState; 508 op_info->operationState = op->operationState; 509 510 out: 511 if (res != TEE_SUCCESS && 512 res != TEE_ERROR_SHORT_BUFFER) 513 TEE_Panic(res); 514 515 return res; 516 } 517 518 TEE_Result 519 __GP11_TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 520 TEE_OperationInfoMultiple *info, 521 uint32_t *operationSize) 522 { 523 TEE_Result res = TEE_SUCCESS; 524 size_t s = 0; 525 526 __utee_check_gp11_outbuf_annotation(info, operationSize); 527 s = *operationSize; 528 res = TEE_GetOperationInfoMultiple(operation, info, &s); 529 *operationSize = s; 530 return res; 531 } 532 533 void TEE_ResetOperation(TEE_OperationHandle operation) 534 { 535 TEE_Result res; 536 537 if (operation == TEE_HANDLE_NULL) 538 TEE_Panic(0); 539 540 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 541 TEE_Panic(0); 542 543 operation->operationState = TEE_OPERATION_STATE_INITIAL; 544 545 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 546 res = _utee_hash_init(operation->state, NULL, 0); 547 if (res != TEE_SUCCESS) 548 TEE_Panic(res); 549 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 550 } else { 551 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 552 } 553 } 554 555 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 556 TEE_ObjectHandle key) 557 { 558 TEE_Result res; 559 uint32_t key_size = 0; 560 TEE_ObjectInfo key_info; 561 562 if (operation == TEE_HANDLE_NULL) { 563 res = TEE_ERROR_BAD_PARAMETERS; 564 goto out; 565 } 566 567 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 568 res = TEE_ERROR_BAD_PARAMETERS; 569 goto out; 570 } 571 572 if (key == TEE_HANDLE_NULL) { 573 /* Operation key cleared */ 574 TEE_ResetTransientObject(operation->key1); 575 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 576 return TEE_SUCCESS; 577 } 578 579 /* No key for digest operation */ 580 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 581 res = TEE_ERROR_BAD_PARAMETERS; 582 goto out; 583 } 584 585 /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 586 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 587 0) { 588 res = TEE_ERROR_BAD_PARAMETERS; 589 goto out; 590 } 591 592 res = TEE_GetObjectInfo1(key, &key_info); 593 /* Key is not a valid handle */ 594 if (res != TEE_SUCCESS) 595 goto out; 596 597 /* Supplied key has to meet required usage */ 598 if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 599 operation->info.requiredKeyUsage) { 600 res = TEE_ERROR_BAD_PARAMETERS; 601 goto out; 602 } 603 604 if (operation->info.maxKeySize < key_info.objectSize) { 605 res = TEE_ERROR_BAD_PARAMETERS; 606 goto out; 607 } 608 609 key_size = key_info.objectSize; 610 611 TEE_ResetTransientObject(operation->key1); 612 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 613 614 res = TEE_CopyObjectAttributes1(operation->key1, key); 615 if (res != TEE_SUCCESS) 616 goto out; 617 618 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 619 620 operation->info.keySize = key_size; 621 622 out: 623 if (res != TEE_SUCCESS && 624 res != TEE_ERROR_CORRUPT_OBJECT && 625 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 626 TEE_Panic(res); 627 628 return res; 629 } 630 631 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 632 TEE_ObjectHandle key1, TEE_ObjectHandle key2) 633 { 634 TEE_Result res; 635 uint32_t key_size = 0; 636 TEE_ObjectInfo key_info1; 637 TEE_ObjectInfo key_info2; 638 639 if (operation == TEE_HANDLE_NULL) { 640 res = TEE_ERROR_BAD_PARAMETERS; 641 goto out; 642 } 643 644 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 645 res = TEE_ERROR_BAD_PARAMETERS; 646 goto out; 647 } 648 649 /* 650 * Key1/Key2 and/or are not initialized and 651 * Either both keys are NULL or both are not NULL 652 */ 653 if (!key1 && !key2) { 654 /* Clear the keys */ 655 TEE_ResetTransientObject(operation->key1); 656 TEE_ResetTransientObject(operation->key2); 657 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 658 return TEE_SUCCESS; 659 } else if (!key1 || !key2) { 660 /* Both keys are obviously not valid. */ 661 res = TEE_ERROR_BAD_PARAMETERS; 662 goto out; 663 } 664 665 /* No key for digest operation */ 666 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 667 res = TEE_ERROR_BAD_PARAMETERS; 668 goto out; 669 } 670 671 /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 672 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 673 0) { 674 res = TEE_ERROR_BAD_PARAMETERS; 675 goto out; 676 } 677 678 res = TEE_GetObjectInfo1(key1, &key_info1); 679 /* Key1 is not a valid handle */ 680 if (res != TEE_SUCCESS) 681 goto out; 682 683 /* Supplied key has to meet required usage */ 684 if ((key_info1.objectUsage & operation->info. 685 requiredKeyUsage) != operation->info.requiredKeyUsage) { 686 res = TEE_ERROR_BAD_PARAMETERS; 687 goto out; 688 } 689 690 res = TEE_GetObjectInfo1(key2, &key_info2); 691 /* Key2 is not a valid handle */ 692 if (res != TEE_SUCCESS) { 693 if (res == TEE_ERROR_CORRUPT_OBJECT) 694 res = TEE_ERROR_CORRUPT_OBJECT_2; 695 goto out; 696 } 697 698 /* Supplied key has to meet required usage */ 699 if ((key_info2.objectUsage & operation->info. 700 requiredKeyUsage) != operation->info.requiredKeyUsage) { 701 res = TEE_ERROR_BAD_PARAMETERS; 702 goto out; 703 } 704 705 /* 706 * All the multi key algorithm currently supported requires the keys to 707 * be of equal size. 708 */ 709 if (key_info1.objectSize != key_info2.objectSize) { 710 res = TEE_ERROR_BAD_PARAMETERS; 711 goto out; 712 713 } 714 715 if (operation->info.maxKeySize < key_info1.objectSize) { 716 res = TEE_ERROR_BAD_PARAMETERS; 717 goto out; 718 } 719 720 /* 721 * Odd that only the size of one key should be reported while 722 * size of two key are used when allocating the operation. 723 */ 724 key_size = key_info1.objectSize; 725 726 TEE_ResetTransientObject(operation->key1); 727 TEE_ResetTransientObject(operation->key2); 728 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 729 730 res = TEE_CopyObjectAttributes1(operation->key1, key1); 731 if (res != TEE_SUCCESS) 732 goto out; 733 res = TEE_CopyObjectAttributes1(operation->key2, key2); 734 if (res != TEE_SUCCESS) { 735 if (res == TEE_ERROR_CORRUPT_OBJECT) 736 res = TEE_ERROR_CORRUPT_OBJECT_2; 737 goto out; 738 } 739 740 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 741 742 operation->info.keySize = key_size; 743 744 out: 745 if (res != TEE_SUCCESS && 746 res != TEE_ERROR_CORRUPT_OBJECT && 747 res != TEE_ERROR_CORRUPT_OBJECT_2 && 748 res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 749 res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 750 TEE_Panic(res); 751 752 return res; 753 } 754 755 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 756 { 757 TEE_Result res; 758 759 if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 760 TEE_Panic(0); 761 if (dst_op->info.algorithm != src_op->info.algorithm) 762 TEE_Panic(0); 763 if (dst_op->info.mode != src_op->info.mode) 764 TEE_Panic(0); 765 if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 766 TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 767 TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 768 769 if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 770 key1 = src_op->key1; 771 key2 = src_op->key2; 772 } 773 774 if ((src_op->info.handleState & 775 TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 776 TEE_SetOperationKey(dst_op, key1); 777 } else { 778 TEE_SetOperationKey2(dst_op, key1, key2); 779 } 780 } 781 dst_op->info.handleState = src_op->info.handleState; 782 dst_op->info.keySize = src_op->info.keySize; 783 dst_op->info.digestLength = src_op->info.digestLength; 784 dst_op->operationState = src_op->operationState; 785 786 if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 787 dst_op->block_size != src_op->block_size) 788 TEE_Panic(0); 789 790 if (dst_op->buffer != NULL) { 791 if (src_op->buffer == NULL) 792 TEE_Panic(0); 793 794 memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 795 dst_op->buffer_offs = src_op->buffer_offs; 796 } else if (src_op->buffer != NULL) { 797 TEE_Panic(0); 798 } 799 800 res = _utee_cryp_state_copy(dst_op->state, src_op->state); 801 if (res != TEE_SUCCESS) 802 TEE_Panic(res); 803 } 804 805 /* Cryptographic Operations API - Message Digest Functions */ 806 807 static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 808 uint32_t IVLen) 809 { 810 TEE_Result res; 811 812 /* 813 * Note : IV and IVLen are never used in current implementation 814 * This is why coherent values of IV and IVLen are not checked 815 */ 816 res = _utee_hash_init(operation->state, IV, IVLen); 817 if (res != TEE_SUCCESS) 818 TEE_Panic(res); 819 operation->buffer_offs = 0; 820 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 821 } 822 823 void TEE_DigestUpdate(TEE_OperationHandle operation, 824 const void *chunk, size_t chunkSize) 825 { 826 TEE_Result res = TEE_ERROR_GENERIC; 827 828 if (operation == TEE_HANDLE_NULL || 829 operation->info.operationClass != TEE_OPERATION_DIGEST) 830 TEE_Panic(0); 831 832 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 833 834 res = _utee_hash_update(operation->state, chunk, chunkSize); 835 if (res != TEE_SUCCESS) 836 TEE_Panic(res); 837 } 838 839 void __GP11_TEE_DigestUpdate(TEE_OperationHandle operation, 840 const void *chunk, uint32_t chunkSize) 841 { 842 return TEE_DigestUpdate(operation, chunk, chunkSize); 843 } 844 845 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 846 size_t chunkLen, void *hash, size_t *hashLen) 847 { 848 TEE_Result res; 849 uint64_t hl; 850 851 if ((operation == TEE_HANDLE_NULL) || 852 (!chunk && chunkLen) || 853 (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 854 res = TEE_ERROR_BAD_PARAMETERS; 855 goto out; 856 } 857 __utee_check_inout_annotation(hashLen, sizeof(*hashLen)); 858 859 hl = *hashLen; 860 res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 861 *hashLen = hl; 862 if (res != TEE_SUCCESS) 863 goto out; 864 865 /* Reset operation state */ 866 init_hash_operation(operation, NULL, 0); 867 868 operation->operationState = TEE_OPERATION_STATE_INITIAL; 869 870 out: 871 if (res != TEE_SUCCESS && 872 res != TEE_ERROR_SHORT_BUFFER) 873 TEE_Panic(res); 874 875 return res; 876 } 877 878 TEE_Result __GP11_TEE_DigestDoFinal(TEE_OperationHandle operation, 879 const void *chunk, uint32_t chunkLen, 880 void *hash, uint32_t *hashLen) 881 { 882 TEE_Result res = TEE_SUCCESS; 883 size_t l = 0; 884 885 __utee_check_inout_annotation(hashLen, sizeof(*hashLen)); 886 l = *hashLen; 887 res = TEE_DigestDoFinal(operation, chunk, chunkLen, hash, &l); 888 *hashLen = l; 889 return res; 890 } 891 892 /* Cryptographic Operations API - Symmetric Cipher Functions */ 893 894 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 895 size_t IVLen) 896 { 897 TEE_Result res; 898 899 if (operation == TEE_HANDLE_NULL) 900 TEE_Panic(0); 901 902 if (operation->info.operationClass != TEE_OPERATION_CIPHER) 903 TEE_Panic(0); 904 905 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 906 !(operation->key1)) 907 TEE_Panic(0); 908 909 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 910 TEE_ResetOperation(operation); 911 912 if (IV && IVLen) { 913 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 914 operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 915 operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 916 operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD) 917 TEE_Panic(0); 918 } 919 920 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 921 922 res = _utee_cipher_init(operation->state, IV, IVLen); 923 if (res != TEE_SUCCESS) 924 TEE_Panic(res); 925 926 operation->buffer_offs = 0; 927 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 928 } 929 930 void __GP11_TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 931 uint32_t IVLen) 932 { 933 return TEE_CipherInit(operation, IV, IVLen); 934 } 935 936 static TEE_Result tee_buffer_update( 937 TEE_OperationHandle op, 938 TEE_Result(*update_func)(unsigned long state, const void *src, 939 size_t slen, void *dst, uint64_t *dlen), 940 const void *src_data, size_t src_len, 941 void *dest_data, uint64_t *dest_len) 942 { 943 TEE_Result res; 944 const uint8_t *src = src_data; 945 size_t slen = src_len; 946 uint8_t *dst = dest_data; 947 size_t dlen = *dest_len; 948 size_t acc_dlen = 0; 949 uint64_t tmp_dlen; 950 size_t l; 951 size_t buffer_size; 952 size_t buffer_left; 953 954 if (!src) { 955 if (slen) 956 TEE_Panic(0); 957 goto out; 958 } 959 960 if (op->buffer_two_blocks) { 961 buffer_size = op->block_size * 2; 962 buffer_left = 1; 963 } else { 964 buffer_size = op->block_size; 965 buffer_left = 0; 966 } 967 968 if (op->buffer_offs > 0) { 969 /* Fill up complete block */ 970 if (op->buffer_offs < op->block_size) 971 l = MIN(slen, op->block_size - op->buffer_offs); 972 else 973 l = MIN(slen, buffer_size - op->buffer_offs); 974 memcpy(op->buffer + op->buffer_offs, src, l); 975 op->buffer_offs += l; 976 src += l; 977 slen -= l; 978 if ((op->buffer_offs % op->block_size) != 0) 979 goto out; /* Nothing left to do */ 980 } 981 982 /* If we can feed from buffer */ 983 if ((op->buffer_offs > 0) && 984 ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 985 l = ROUNDUP(op->buffer_offs + slen - buffer_size, 986 op->block_size); 987 l = MIN(op->buffer_offs, l); 988 tmp_dlen = dlen; 989 res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 990 if (res != TEE_SUCCESS) 991 TEE_Panic(res); 992 dst += tmp_dlen; 993 dlen -= tmp_dlen; 994 acc_dlen += tmp_dlen; 995 op->buffer_offs -= l; 996 if (op->buffer_offs > 0) { 997 /* 998 * Slen is small enough to be contained in rest buffer. 999 */ 1000 memcpy(op->buffer, op->buffer + l, buffer_size - l); 1001 memcpy(op->buffer + op->buffer_offs, src, slen); 1002 op->buffer_offs += slen; 1003 goto out; /* Nothing left to do */ 1004 } 1005 } 1006 1007 if (slen >= (buffer_size + buffer_left)) { 1008 /* Buffer is empty, feed as much as possible from src */ 1009 if (op->info.algorithm == TEE_ALG_AES_CTS) 1010 l = ROUNDUP(slen - buffer_size, op->block_size); 1011 else 1012 l = ROUNDUP(slen - buffer_size + 1, op->block_size); 1013 1014 tmp_dlen = dlen; 1015 res = update_func(op->state, src, l, dst, &tmp_dlen); 1016 if (res != TEE_SUCCESS) 1017 TEE_Panic(res); 1018 src += l; 1019 slen -= l; 1020 dst += tmp_dlen; 1021 dlen -= tmp_dlen; 1022 acc_dlen += tmp_dlen; 1023 } 1024 1025 /* Slen is small enough to be contained in buffer. */ 1026 memcpy(op->buffer + op->buffer_offs, src, slen); 1027 op->buffer_offs += slen; 1028 1029 out: 1030 *dest_len = acc_dlen; 1031 return TEE_SUCCESS; 1032 } 1033 1034 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 1035 size_t srcLen, void *destData, size_t *destLen) 1036 { 1037 TEE_Result res; 1038 size_t req_dlen; 1039 uint64_t dl; 1040 1041 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1042 res = TEE_ERROR_BAD_PARAMETERS; 1043 goto out; 1044 } 1045 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1046 1047 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1048 res = TEE_ERROR_BAD_PARAMETERS; 1049 goto out; 1050 } 1051 1052 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1053 res = TEE_ERROR_BAD_PARAMETERS; 1054 goto out; 1055 } 1056 1057 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1058 res = TEE_ERROR_BAD_PARAMETERS; 1059 goto out; 1060 } 1061 1062 if (!srcData && !srcLen) { 1063 *destLen = 0; 1064 res = TEE_SUCCESS; 1065 goto out; 1066 } 1067 1068 /* Calculate required dlen */ 1069 if (operation->block_size > 1) { 1070 req_dlen = ((operation->buffer_offs + srcLen) / 1071 operation->block_size) * operation->block_size; 1072 } else { 1073 req_dlen = srcLen; 1074 } 1075 if (operation->buffer_two_blocks) { 1076 if (req_dlen > operation->block_size * 2) 1077 req_dlen -= operation->block_size * 2; 1078 else 1079 req_dlen = 0; 1080 } 1081 /* 1082 * Check that required destLen is big enough before starting to feed 1083 * data to the algorithm. Errors during feeding of data are fatal as we 1084 * can't restore sync with this API. 1085 */ 1086 if (*destLen < req_dlen) { 1087 *destLen = req_dlen; 1088 res = TEE_ERROR_SHORT_BUFFER; 1089 goto out; 1090 } 1091 1092 dl = *destLen; 1093 if (operation->block_size > 1) { 1094 res = tee_buffer_update(operation, _utee_cipher_update, srcData, 1095 srcLen, destData, &dl); 1096 } else { 1097 if (srcLen > 0) { 1098 res = _utee_cipher_update(operation->state, srcData, 1099 srcLen, destData, &dl); 1100 } else { 1101 res = TEE_SUCCESS; 1102 dl = 0; 1103 } 1104 } 1105 *destLen = dl; 1106 1107 out: 1108 if (res != TEE_SUCCESS && 1109 res != TEE_ERROR_SHORT_BUFFER) 1110 TEE_Panic(res); 1111 1112 return res; 1113 } 1114 1115 TEE_Result __GP11_TEE_CipherUpdate(TEE_OperationHandle operation, 1116 const void *srcData, uint32_t srcLen, 1117 void *destData, uint32_t *destLen) 1118 { 1119 TEE_Result res = TEE_SUCCESS; 1120 size_t dl = 0; 1121 1122 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1123 dl = *destLen; 1124 res = TEE_CipherUpdate(operation, srcData, srcLen, destData, &dl); 1125 *destLen = dl; 1126 return res; 1127 } 1128 1129 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1130 const void *srcData, size_t srcLen, 1131 void *destData, size_t *destLen) 1132 { 1133 TEE_Result res = TEE_SUCCESS; 1134 uint8_t *dst = destData; 1135 size_t acc_dlen = 0; 1136 uint64_t tmp_dlen = 0; 1137 size_t req_dlen = 0; 1138 1139 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1140 res = TEE_ERROR_BAD_PARAMETERS; 1141 goto out; 1142 } 1143 if (destLen) 1144 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1145 1146 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1147 res = TEE_ERROR_BAD_PARAMETERS; 1148 goto out; 1149 } 1150 1151 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1152 res = TEE_ERROR_BAD_PARAMETERS; 1153 goto out; 1154 } 1155 1156 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1157 res = TEE_ERROR_BAD_PARAMETERS; 1158 goto out; 1159 } 1160 1161 /* 1162 * Check that the final block doesn't require padding for those 1163 * algorithms that requires client to supply padding. 1164 */ 1165 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1166 operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1167 operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1168 operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1169 operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1170 operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1171 operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1172 operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1173 if (((operation->buffer_offs + srcLen) % operation->block_size) 1174 != 0) { 1175 res = TEE_ERROR_BAD_PARAMETERS; 1176 goto out; 1177 } 1178 } 1179 1180 /* 1181 * Check that required destLen is big enough before starting to feed 1182 * data to the algorithm. Errors during feeding of data are fatal as we 1183 * can't restore sync with this API. 1184 */ 1185 if (operation->block_size > 1) { 1186 req_dlen = operation->buffer_offs + srcLen; 1187 } else { 1188 req_dlen = srcLen; 1189 } 1190 if (destLen) 1191 tmp_dlen = *destLen; 1192 if (tmp_dlen < req_dlen) { 1193 if (destLen) 1194 *destLen = req_dlen; 1195 res = TEE_ERROR_SHORT_BUFFER; 1196 goto out; 1197 } 1198 1199 if (operation->block_size > 1) { 1200 if (srcLen) { 1201 res = tee_buffer_update(operation, _utee_cipher_update, 1202 srcData, srcLen, dst, 1203 &tmp_dlen); 1204 if (res != TEE_SUCCESS) 1205 goto out; 1206 1207 dst += tmp_dlen; 1208 acc_dlen += tmp_dlen; 1209 1210 tmp_dlen = *destLen - acc_dlen; 1211 } 1212 res = _utee_cipher_final(operation->state, operation->buffer, 1213 operation->buffer_offs, dst, 1214 &tmp_dlen); 1215 } else { 1216 res = _utee_cipher_final(operation->state, srcData, srcLen, dst, 1217 &tmp_dlen); 1218 } 1219 if (res != TEE_SUCCESS) 1220 goto out; 1221 1222 acc_dlen += tmp_dlen; 1223 if (destLen) 1224 *destLen = acc_dlen; 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 __GP11_TEE_CipherDoFinal(TEE_OperationHandle operation, 1239 const void *srcData, uint32_t srcLen, 1240 void *destData, uint32_t *destLen) 1241 { 1242 TEE_Result res = TEE_SUCCESS; 1243 size_t dl = 0; 1244 1245 if (destLen) { 1246 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1247 dl = *destLen; 1248 } 1249 res = TEE_CipherDoFinal(operation, srcData, srcLen, destData, &dl); 1250 if (destLen) 1251 *destLen = dl; 1252 return res; 1253 } 1254 1255 /* Cryptographic Operations API - MAC Functions */ 1256 1257 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, size_t IVLen) 1258 { 1259 if (operation == TEE_HANDLE_NULL) 1260 TEE_Panic(0); 1261 1262 if (operation->info.operationClass != TEE_OPERATION_MAC) 1263 TEE_Panic(0); 1264 1265 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1266 !(operation->key1)) 1267 TEE_Panic(0); 1268 1269 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1270 TEE_ResetOperation(operation); 1271 1272 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1273 1274 init_hash_operation(operation, IV, IVLen); 1275 } 1276 1277 void __GP11_TEE_MACInit(TEE_OperationHandle operation, const void *IV, 1278 uint32_t IVLen) 1279 { 1280 return TEE_MACInit(operation, IV, IVLen); 1281 } 1282 1283 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 1284 size_t chunkSize) 1285 { 1286 TEE_Result res; 1287 1288 if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1289 TEE_Panic(0); 1290 1291 if (operation->info.operationClass != TEE_OPERATION_MAC) 1292 TEE_Panic(0); 1293 1294 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1295 TEE_Panic(0); 1296 1297 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1298 TEE_Panic(0); 1299 1300 res = _utee_hash_update(operation->state, chunk, chunkSize); 1301 if (res != TEE_SUCCESS) 1302 TEE_Panic(res); 1303 } 1304 1305 void __GP11_TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 1306 uint32_t chunkSize) 1307 { 1308 return TEE_MACUpdate(operation, chunk, chunkSize); 1309 } 1310 1311 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 1312 const void *message, size_t messageLen, 1313 void *mac, size_t *macLen) 1314 { 1315 TEE_Result res; 1316 uint64_t ml; 1317 1318 if (operation == TEE_HANDLE_NULL || (!message && messageLen)) { 1319 res = TEE_ERROR_BAD_PARAMETERS; 1320 goto out; 1321 } 1322 __utee_check_inout_annotation(macLen, sizeof(*macLen)); 1323 1324 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1325 res = TEE_ERROR_BAD_PARAMETERS; 1326 goto out; 1327 } 1328 1329 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1330 res = TEE_ERROR_BAD_PARAMETERS; 1331 goto out; 1332 } 1333 1334 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1335 res = TEE_ERROR_BAD_PARAMETERS; 1336 goto out; 1337 } 1338 1339 ml = *macLen; 1340 res = _utee_hash_final(operation->state, message, messageLen, mac, &ml); 1341 *macLen = ml; 1342 if (res != TEE_SUCCESS) 1343 goto out; 1344 1345 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1346 1347 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1348 1349 out: 1350 if (res != TEE_SUCCESS && 1351 res != TEE_ERROR_SHORT_BUFFER) 1352 TEE_Panic(res); 1353 1354 return res; 1355 } 1356 1357 TEE_Result __GP11_TEE_MACComputeFinal(TEE_OperationHandle operation, 1358 const void *message, uint32_t messageLen, 1359 void *mac, uint32_t *macLen) 1360 { 1361 TEE_Result res = TEE_SUCCESS; 1362 size_t ml = 0; 1363 1364 __utee_check_inout_annotation(macLen, sizeof(*macLen)); 1365 ml = *macLen; 1366 res = TEE_MACComputeFinal(operation, message, messageLen, mac, &ml); 1367 *macLen = ml; 1368 return res; 1369 } 1370 1371 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 1372 const void *message, size_t messageLen, 1373 const void *mac, size_t macLen) 1374 { 1375 TEE_Result res; 1376 uint8_t computed_mac[TEE_MAX_HASH_SIZE] = { 0 }; 1377 size_t computed_mac_size = TEE_MAX_HASH_SIZE; 1378 1379 if (operation->info.operationClass != TEE_OPERATION_MAC) { 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 (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1390 res = TEE_ERROR_BAD_PARAMETERS; 1391 goto out; 1392 } 1393 1394 res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1395 &computed_mac_size); 1396 if (res != TEE_SUCCESS) 1397 goto out; 1398 1399 if (computed_mac_size != macLen) { 1400 res = TEE_ERROR_MAC_INVALID; 1401 goto out; 1402 } 1403 1404 if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 1405 res = TEE_ERROR_MAC_INVALID; 1406 goto out; 1407 } 1408 1409 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1410 1411 out: 1412 if (res != TEE_SUCCESS && 1413 res != TEE_ERROR_MAC_INVALID) 1414 TEE_Panic(res); 1415 1416 return res; 1417 } 1418 1419 TEE_Result __GP11_TEE_MACCompareFinal(TEE_OperationHandle operation, 1420 const void *message, uint32_t messageLen, 1421 const void *mac, uint32_t macLen) 1422 { 1423 return TEE_MACCompareFinal(operation, message, messageLen, mac, macLen); 1424 } 1425 1426 /* Cryptographic Operations API - Authenticated Encryption Functions */ 1427 1428 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 1429 uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1430 uint32_t payloadLen) 1431 { 1432 TEE_Result res; 1433 1434 if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1435 res = TEE_ERROR_BAD_PARAMETERS; 1436 goto out; 1437 } 1438 1439 if (operation->info.operationClass != TEE_OPERATION_AE) { 1440 res = TEE_ERROR_BAD_PARAMETERS; 1441 goto out; 1442 } 1443 1444 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1445 res = TEE_ERROR_BAD_PARAMETERS; 1446 goto out; 1447 } 1448 1449 /* 1450 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1451 * in the implementation. But AES-GCM spec doesn't specify the tag len 1452 * according to the same principle so we have to check here instead to 1453 * be GP compliant. 1454 */ 1455 if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1456 /* 1457 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1458 */ 1459 if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1460 res = TEE_ERROR_NOT_SUPPORTED; 1461 goto out; 1462 } 1463 } 1464 1465 res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8, 1466 AADLen, payloadLen); 1467 if (res != TEE_SUCCESS) 1468 goto out; 1469 1470 operation->info.digestLength = tagLen / 8; 1471 operation->buffer_offs = 0; 1472 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1473 1474 out: 1475 if (res != TEE_SUCCESS && 1476 res != TEE_ERROR_NOT_SUPPORTED) 1477 TEE_Panic(res); 1478 1479 return res; 1480 } 1481 1482 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 1483 uint32_t AADdataLen) 1484 { 1485 TEE_Result res; 1486 1487 if (operation == TEE_HANDLE_NULL || 1488 (AADdata == NULL && AADdataLen != 0)) 1489 TEE_Panic(0); 1490 1491 if (operation->info.operationClass != TEE_OPERATION_AE) 1492 TEE_Panic(0); 1493 1494 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1495 TEE_Panic(0); 1496 1497 res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1498 1499 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1500 1501 if (res != TEE_SUCCESS) 1502 TEE_Panic(res); 1503 } 1504 1505 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 1506 uint32_t srcLen, void *destData, uint32_t *destLen) 1507 { 1508 TEE_Result res = TEE_SUCCESS; 1509 size_t req_dlen = 0; 1510 uint64_t dl = 0; 1511 1512 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1513 res = TEE_ERROR_BAD_PARAMETERS; 1514 goto out; 1515 } 1516 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1517 1518 if (operation->info.operationClass != TEE_OPERATION_AE) { 1519 res = TEE_ERROR_BAD_PARAMETERS; 1520 goto out; 1521 } 1522 1523 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1524 res = TEE_ERROR_BAD_PARAMETERS; 1525 goto out; 1526 } 1527 1528 if (!srcData && !srcLen) { 1529 *destLen = 0; 1530 res = TEE_SUCCESS; 1531 goto out; 1532 } 1533 1534 /* 1535 * Check that required destLen is big enough before starting to feed 1536 * data to the algorithm. Errors during feeding of data are fatal as we 1537 * can't restore sync with this API. 1538 */ 1539 if (operation->block_size > 1) { 1540 req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1541 operation->block_size); 1542 } else { 1543 req_dlen = srcLen; 1544 } 1545 1546 dl = *destLen; 1547 if (dl < req_dlen) { 1548 *destLen = req_dlen; 1549 res = TEE_ERROR_SHORT_BUFFER; 1550 goto out; 1551 } 1552 1553 if (operation->block_size > 1) { 1554 res = tee_buffer_update(operation, _utee_authenc_update_payload, 1555 srcData, srcLen, destData, &dl); 1556 } else { 1557 if (srcLen > 0) { 1558 res = _utee_authenc_update_payload(operation->state, 1559 srcData, srcLen, 1560 destData, &dl); 1561 } else { 1562 dl = 0; 1563 res = TEE_SUCCESS; 1564 } 1565 } 1566 if (res != TEE_SUCCESS) 1567 goto out; 1568 1569 *destLen = dl; 1570 1571 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1572 1573 out: 1574 if (res != TEE_SUCCESS && 1575 res != TEE_ERROR_SHORT_BUFFER) 1576 TEE_Panic(res); 1577 1578 return res; 1579 } 1580 1581 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1582 const void *srcData, uint32_t srcLen, 1583 void *destData, uint32_t *destLen, void *tag, 1584 uint32_t *tagLen) 1585 { 1586 TEE_Result res; 1587 uint8_t *dst = destData; 1588 size_t acc_dlen = 0; 1589 uint64_t tmp_dlen; 1590 size_t req_dlen; 1591 uint64_t tl; 1592 1593 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1594 res = TEE_ERROR_BAD_PARAMETERS; 1595 goto out; 1596 } 1597 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1598 __utee_check_inout_annotation(tagLen, sizeof(*tagLen)); 1599 1600 if (operation->info.operationClass != TEE_OPERATION_AE) { 1601 res = TEE_ERROR_BAD_PARAMETERS; 1602 goto out; 1603 } 1604 1605 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1606 res = TEE_ERROR_BAD_PARAMETERS; 1607 goto out; 1608 } 1609 1610 /* 1611 * Check that required destLen is big enough before starting to feed 1612 * data to the algorithm. Errors during feeding of data are fatal as we 1613 * can't restore sync with this API. 1614 * 1615 * Need to check this before update_payload since sync would be lost if 1616 * we return short buffer after that. 1617 */ 1618 res = TEE_ERROR_GENERIC; 1619 1620 req_dlen = operation->buffer_offs + srcLen; 1621 if (*destLen < req_dlen) { 1622 *destLen = req_dlen; 1623 res = TEE_ERROR_SHORT_BUFFER; 1624 } 1625 1626 if (*tagLen < operation->info.digestLength) { 1627 *tagLen = operation->info.digestLength; 1628 res = TEE_ERROR_SHORT_BUFFER; 1629 } 1630 1631 if (res == TEE_ERROR_SHORT_BUFFER) 1632 goto out; 1633 1634 tl = *tagLen; 1635 tmp_dlen = *destLen - acc_dlen; 1636 if (operation->block_size > 1) { 1637 res = tee_buffer_update(operation, _utee_authenc_update_payload, 1638 srcData, srcLen, dst, &tmp_dlen); 1639 if (res != TEE_SUCCESS) 1640 goto out; 1641 1642 dst += tmp_dlen; 1643 acc_dlen += tmp_dlen; 1644 1645 tmp_dlen = *destLen - acc_dlen; 1646 res = _utee_authenc_enc_final(operation->state, 1647 operation->buffer, 1648 operation->buffer_offs, dst, 1649 &tmp_dlen, tag, &tl); 1650 } else { 1651 res = _utee_authenc_enc_final(operation->state, srcData, 1652 srcLen, dst, &tmp_dlen, 1653 tag, &tl); 1654 } 1655 *tagLen = tl; 1656 if (res != TEE_SUCCESS) 1657 goto out; 1658 1659 acc_dlen += tmp_dlen; 1660 *destLen = acc_dlen; 1661 1662 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1663 1664 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1665 1666 out: 1667 if (res != TEE_SUCCESS && 1668 res != TEE_ERROR_SHORT_BUFFER) 1669 TEE_Panic(res); 1670 1671 return res; 1672 } 1673 1674 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1675 const void *srcData, uint32_t srcLen, 1676 void *destData, uint32_t *destLen, void *tag, 1677 uint32_t tagLen) 1678 { 1679 TEE_Result res; 1680 uint8_t *dst = destData; 1681 size_t acc_dlen = 0; 1682 uint64_t tmp_dlen; 1683 size_t req_dlen; 1684 1685 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1686 res = TEE_ERROR_BAD_PARAMETERS; 1687 goto out; 1688 } 1689 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1690 1691 if (operation->info.operationClass != TEE_OPERATION_AE) { 1692 res = TEE_ERROR_BAD_PARAMETERS; 1693 goto out; 1694 } 1695 1696 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1697 res = TEE_ERROR_BAD_PARAMETERS; 1698 goto out; 1699 } 1700 1701 /* 1702 * Check that required destLen is big enough before starting to feed 1703 * data to the algorithm. Errors during feeding of data are fatal as we 1704 * can't restore sync with this API. 1705 */ 1706 req_dlen = operation->buffer_offs + srcLen; 1707 if (*destLen < req_dlen) { 1708 *destLen = req_dlen; 1709 res = TEE_ERROR_SHORT_BUFFER; 1710 goto out; 1711 } 1712 1713 tmp_dlen = *destLen - acc_dlen; 1714 if (operation->block_size > 1) { 1715 res = tee_buffer_update(operation, _utee_authenc_update_payload, 1716 srcData, srcLen, dst, &tmp_dlen); 1717 if (res != TEE_SUCCESS) 1718 goto out; 1719 1720 dst += tmp_dlen; 1721 acc_dlen += tmp_dlen; 1722 1723 tmp_dlen = *destLen - acc_dlen; 1724 res = _utee_authenc_dec_final(operation->state, 1725 operation->buffer, 1726 operation->buffer_offs, dst, 1727 &tmp_dlen, tag, tagLen); 1728 } else { 1729 res = _utee_authenc_dec_final(operation->state, srcData, 1730 srcLen, dst, &tmp_dlen, 1731 tag, tagLen); 1732 } 1733 if (res != TEE_SUCCESS) 1734 goto out; 1735 1736 /* Supplied tagLen should match what we initiated with */ 1737 if (tagLen != operation->info.digestLength) 1738 res = TEE_ERROR_MAC_INVALID; 1739 1740 acc_dlen += tmp_dlen; 1741 *destLen = acc_dlen; 1742 1743 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1744 1745 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1746 1747 out: 1748 if (res != TEE_SUCCESS && 1749 res != TEE_ERROR_SHORT_BUFFER && 1750 res != TEE_ERROR_MAC_INVALID) 1751 TEE_Panic(res); 1752 1753 return res; 1754 } 1755 1756 /* Cryptographic Operations API - Asymmetric Functions */ 1757 1758 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 1759 const TEE_Attribute *params, 1760 uint32_t paramCount, const void *srcData, 1761 uint32_t srcLen, void *destData, 1762 uint32_t *destLen) 1763 { 1764 TEE_Result res = TEE_SUCCESS; 1765 struct utee_attribute ua[paramCount]; 1766 uint64_t dl = 0; 1767 1768 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1769 TEE_Panic(0); 1770 1771 __utee_check_attr_in_annotation(params, paramCount); 1772 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1773 1774 if (!operation->key1) 1775 TEE_Panic(0); 1776 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1777 TEE_Panic(0); 1778 if (operation->info.mode != TEE_MODE_ENCRYPT) 1779 TEE_Panic(0); 1780 1781 __utee_from_attr(ua, params, paramCount); 1782 dl = *destLen; 1783 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1784 srcLen, destData, &dl); 1785 *destLen = dl; 1786 1787 if (res != TEE_SUCCESS && 1788 res != TEE_ERROR_SHORT_BUFFER && 1789 res != TEE_ERROR_BAD_PARAMETERS) 1790 TEE_Panic(res); 1791 1792 return res; 1793 } 1794 1795 TEE_Result __GP11_TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 1796 const __GP11_TEE_Attribute *params, 1797 uint32_t paramCount, 1798 const void *srcData, uint32_t srcLen, 1799 void *destData, uint32_t *destLen) 1800 { 1801 TEE_Result res = TEE_SUCCESS; 1802 struct utee_attribute ua[paramCount]; 1803 uint64_t dl = 0; 1804 1805 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1806 TEE_Panic(0); 1807 1808 __utee_check_gp11_attr_in_annotation(params, paramCount); 1809 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1810 1811 if (!operation->key1) 1812 TEE_Panic(0); 1813 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1814 TEE_Panic(0); 1815 if (operation->info.mode != TEE_MODE_ENCRYPT) 1816 TEE_Panic(0); 1817 1818 __utee_from_gp11_attr(ua, params, paramCount); 1819 dl = *destLen; 1820 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1821 srcLen, destData, &dl); 1822 *destLen = dl; 1823 1824 if (res != TEE_SUCCESS && 1825 res != TEE_ERROR_SHORT_BUFFER && 1826 res != TEE_ERROR_BAD_PARAMETERS) 1827 TEE_Panic(res); 1828 1829 return res; 1830 } 1831 1832 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 1833 const TEE_Attribute *params, 1834 uint32_t paramCount, const void *srcData, 1835 uint32_t srcLen, void *destData, 1836 uint32_t *destLen) 1837 { 1838 TEE_Result res = TEE_SUCCESS; 1839 struct utee_attribute ua[paramCount]; 1840 uint64_t dl = 0; 1841 1842 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1843 TEE_Panic(0); 1844 1845 __utee_check_attr_in_annotation(params, paramCount); 1846 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1847 1848 if (!operation->key1) 1849 TEE_Panic(0); 1850 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1851 TEE_Panic(0); 1852 if (operation->info.mode != TEE_MODE_DECRYPT) 1853 TEE_Panic(0); 1854 1855 __utee_from_attr(ua, params, paramCount); 1856 dl = *destLen; 1857 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1858 srcLen, destData, &dl); 1859 *destLen = dl; 1860 1861 if (res != TEE_SUCCESS && 1862 res != TEE_ERROR_SHORT_BUFFER && 1863 res != TEE_ERROR_BAD_PARAMETERS) 1864 TEE_Panic(res); 1865 1866 return res; 1867 } 1868 1869 TEE_Result __GP11_TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 1870 const __GP11_TEE_Attribute *params, 1871 uint32_t paramCount, 1872 const void *srcData, uint32_t srcLen, 1873 void *destData, uint32_t *destLen) 1874 { 1875 TEE_Result res = TEE_SUCCESS; 1876 struct utee_attribute ua[paramCount]; 1877 uint64_t dl = 0; 1878 1879 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1880 TEE_Panic(0); 1881 1882 __utee_check_gp11_attr_in_annotation(params, paramCount); 1883 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1884 1885 if (!operation->key1) 1886 TEE_Panic(0); 1887 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1888 TEE_Panic(0); 1889 if (operation->info.mode != TEE_MODE_DECRYPT) 1890 TEE_Panic(0); 1891 1892 __utee_from_gp11_attr(ua, params, paramCount); 1893 dl = *destLen; 1894 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1895 srcLen, destData, &dl); 1896 *destLen = dl; 1897 1898 if (res != TEE_SUCCESS && 1899 res != TEE_ERROR_SHORT_BUFFER && 1900 res != TEE_ERROR_BAD_PARAMETERS) 1901 TEE_Panic(res); 1902 1903 return res; 1904 } 1905 1906 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 1907 const TEE_Attribute *params, 1908 uint32_t paramCount, const void *digest, 1909 uint32_t digestLen, void *signature, 1910 uint32_t *signatureLen) 1911 { 1912 TEE_Result res = TEE_SUCCESS; 1913 struct utee_attribute ua[paramCount]; 1914 uint64_t sl = 0; 1915 1916 if (operation == TEE_HANDLE_NULL || (!digest && digestLen)) 1917 TEE_Panic(0); 1918 1919 __utee_check_attr_in_annotation(params, paramCount); 1920 __utee_check_inout_annotation(signatureLen, sizeof(*signatureLen)); 1921 1922 if (!operation->key1) 1923 TEE_Panic(0); 1924 if (operation->info.operationClass != 1925 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1926 TEE_Panic(0); 1927 if (operation->info.mode != TEE_MODE_SIGN) 1928 TEE_Panic(0); 1929 1930 __utee_from_attr(ua, params, paramCount); 1931 sl = *signatureLen; 1932 res = _utee_asymm_operate(operation->state, ua, paramCount, digest, 1933 digestLen, signature, &sl); 1934 *signatureLen = sl; 1935 1936 if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1937 TEE_Panic(res); 1938 1939 return res; 1940 } 1941 1942 TEE_Result __GP11_TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 1943 const __GP11_TEE_Attribute *params, 1944 uint32_t paramCount, 1945 const void *digest, 1946 uint32_t digestLen, void *signature, 1947 uint32_t *signatureLen) 1948 { 1949 TEE_Result res = TEE_SUCCESS; 1950 struct utee_attribute ua[paramCount]; 1951 uint64_t sl = 0; 1952 1953 if (operation == TEE_HANDLE_NULL || (!digest && digestLen)) 1954 TEE_Panic(0); 1955 1956 __utee_check_gp11_attr_in_annotation(params, paramCount); 1957 __utee_check_inout_annotation(signatureLen, sizeof(*signatureLen)); 1958 1959 if (!operation->key1) 1960 TEE_Panic(0); 1961 if (operation->info.operationClass != 1962 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1963 TEE_Panic(0); 1964 if (operation->info.mode != TEE_MODE_SIGN) 1965 TEE_Panic(0); 1966 1967 __utee_from_gp11_attr(ua, params, paramCount); 1968 sl = *signatureLen; 1969 res = _utee_asymm_operate(operation->state, ua, paramCount, digest, 1970 digestLen, signature, &sl); 1971 *signatureLen = sl; 1972 1973 if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1974 TEE_Panic(res); 1975 1976 return res; 1977 } 1978 1979 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 1980 const TEE_Attribute *params, 1981 uint32_t paramCount, const void *digest, 1982 uint32_t digestLen, 1983 const void *signature, 1984 uint32_t signatureLen) 1985 { 1986 TEE_Result res; 1987 struct utee_attribute ua[paramCount]; 1988 1989 if (operation == TEE_HANDLE_NULL || 1990 (digest == NULL && digestLen != 0) || 1991 (signature == NULL && signatureLen != 0)) 1992 TEE_Panic(0); 1993 1994 __utee_check_attr_in_annotation(params, paramCount); 1995 1996 if (!operation->key1) 1997 TEE_Panic(0); 1998 if (operation->info.operationClass != 1999 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 2000 TEE_Panic(0); 2001 if (operation->info.mode != TEE_MODE_VERIFY) 2002 TEE_Panic(0); 2003 2004 __utee_from_attr(ua, params, paramCount); 2005 res = _utee_asymm_verify(operation->state, ua, paramCount, digest, 2006 digestLen, signature, signatureLen); 2007 2008 if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 2009 TEE_Panic(res); 2010 2011 return res; 2012 } 2013 2014 TEE_Result __GP11_TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 2015 const __GP11_TEE_Attribute *params, 2016 uint32_t paramCount, 2017 const void *digest, 2018 uint32_t digestLen, 2019 const void *signature, 2020 uint32_t signatureLen) 2021 { 2022 TEE_Result res = TEE_SUCCESS; 2023 struct utee_attribute ua[paramCount]; 2024 2025 if (operation == TEE_HANDLE_NULL || (!digest && digestLen) || 2026 (!signature && signatureLen)) 2027 TEE_Panic(0); 2028 2029 __utee_check_gp11_attr_in_annotation(params, paramCount); 2030 2031 if (!operation->key1) 2032 TEE_Panic(0); 2033 if (operation->info.operationClass != 2034 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 2035 TEE_Panic(0); 2036 if (operation->info.mode != TEE_MODE_VERIFY) 2037 TEE_Panic(0); 2038 2039 __utee_from_gp11_attr(ua, params, paramCount); 2040 res = _utee_asymm_verify(operation->state, ua, paramCount, digest, 2041 digestLen, signature, signatureLen); 2042 2043 if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 2044 TEE_Panic(res); 2045 2046 return res; 2047 } 2048 2049 /* Cryptographic Operations API - Key Derivation Functions */ 2050 2051 void TEE_DeriveKey(TEE_OperationHandle operation, 2052 const TEE_Attribute *params, uint32_t paramCount, 2053 TEE_ObjectHandle derivedKey) 2054 { 2055 struct utee_attribute ua[paramCount]; 2056 struct utee_object_info key_info = { }; 2057 TEE_Result res = TEE_SUCCESS; 2058 2059 if (operation == TEE_HANDLE_NULL || derivedKey == 0) 2060 TEE_Panic(0); 2061 2062 __utee_check_attr_in_annotation(params, paramCount); 2063 2064 if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 2065 TEE_OPERATION_KEY_DERIVATION) 2066 TEE_Panic(0); 2067 2068 if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 2069 TEE_Panic(0); 2070 if (!operation->key1) 2071 TEE_Panic(0); 2072 if (operation->info.mode != TEE_MODE_DERIVE) 2073 TEE_Panic(0); 2074 if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 2075 TEE_Panic(0); 2076 2077 res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 2078 if (res != TEE_SUCCESS) 2079 TEE_Panic(res); 2080 2081 if (key_info.obj_type != TEE_TYPE_GENERIC_SECRET) 2082 TEE_Panic(0); 2083 if ((key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 2084 TEE_Panic(0); 2085 2086 __utee_from_attr(ua, params, paramCount); 2087 res = _utee_cryp_derive_key(operation->state, ua, paramCount, 2088 (unsigned long)derivedKey); 2089 if (res != TEE_SUCCESS) 2090 TEE_Panic(res); 2091 } 2092 2093 void __GP11_TEE_DeriveKey(TEE_OperationHandle operation, 2094 const __GP11_TEE_Attribute *params, 2095 uint32_t paramCount, TEE_ObjectHandle derivedKey) 2096 { 2097 struct utee_attribute ua[paramCount]; 2098 struct utee_object_info key_info = { }; 2099 TEE_Result res = TEE_SUCCESS; 2100 2101 if (operation == TEE_HANDLE_NULL || derivedKey == 0) 2102 TEE_Panic(0); 2103 2104 __utee_check_gp11_attr_in_annotation(params, paramCount); 2105 2106 if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 2107 TEE_OPERATION_KEY_DERIVATION) 2108 TEE_Panic(0); 2109 2110 if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 2111 TEE_Panic(0); 2112 if (!operation->key1) 2113 TEE_Panic(0); 2114 if (operation->info.mode != TEE_MODE_DERIVE) 2115 TEE_Panic(0); 2116 if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 2117 TEE_Panic(0); 2118 2119 res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 2120 if (res != TEE_SUCCESS) 2121 TEE_Panic(res); 2122 2123 if (key_info.obj_type != TEE_TYPE_GENERIC_SECRET) 2124 TEE_Panic(0); 2125 if ((key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 2126 TEE_Panic(0); 2127 2128 __utee_from_gp11_attr(ua, params, paramCount); 2129 res = _utee_cryp_derive_key(operation->state, ua, paramCount, 2130 (unsigned long)derivedKey); 2131 if (res != TEE_SUCCESS) 2132 TEE_Panic(res); 2133 } 2134 2135 /* Cryptographic Operations API - Random Number Generation Functions */ 2136 2137 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 2138 { 2139 TEE_Result res; 2140 2141 res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 2142 if (res != TEE_SUCCESS) 2143 TEE_Panic(res); 2144 } 2145 2146 int rand(void) 2147 { 2148 int rc; 2149 2150 TEE_GenerateRandom(&rc, sizeof(rc)); 2151 2152 /* 2153 * RAND_MAX is the larges int, INT_MAX which is all bits but the 2154 * highest bit set. 2155 */ 2156 return rc & RAND_MAX; 2157 } 2158 2159 TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element) 2160 { 2161 if (IS_ENABLED(CFG_CRYPTO_AES)) { 2162 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 2163 if (alg == TEE_ALG_AES_ECB_NOPAD) 2164 goto check_element_none; 2165 } 2166 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 2167 if (alg == TEE_ALG_AES_CBC_NOPAD) 2168 goto check_element_none; 2169 } 2170 if (IS_ENABLED(CFG_CRYPTO_CTR)) { 2171 if (alg == TEE_ALG_AES_CTR) 2172 goto check_element_none; 2173 } 2174 if (IS_ENABLED(CFG_CRYPTO_CTS)) { 2175 if (alg == TEE_ALG_AES_CTS) 2176 goto check_element_none; 2177 } 2178 if (IS_ENABLED(CFG_CRYPTO_XTS)) { 2179 if (alg == TEE_ALG_AES_XTS) 2180 goto check_element_none; 2181 } 2182 if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 2183 if (alg == TEE_ALG_AES_CBC_MAC_NOPAD || 2184 alg == TEE_ALG_AES_CBC_MAC_PKCS5) 2185 goto check_element_none; 2186 } 2187 if (IS_ENABLED(CFG_CRYPTO_CMAC)) { 2188 if (alg == TEE_ALG_AES_CMAC) 2189 goto check_element_none; 2190 } 2191 if (IS_ENABLED(CFG_CRYPTO_CCM)) { 2192 if (alg == TEE_ALG_AES_CCM) 2193 goto check_element_none; 2194 } 2195 if (IS_ENABLED(CFG_CRYPTO_GCM)) { 2196 if (alg == TEE_ALG_AES_GCM) 2197 goto check_element_none; 2198 } 2199 } 2200 if (IS_ENABLED(CFG_CRYPTO_DES)) { 2201 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 2202 if (alg == TEE_ALG_DES_ECB_NOPAD || 2203 alg == TEE_ALG_DES3_ECB_NOPAD) 2204 goto check_element_none; 2205 } 2206 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 2207 if (alg == TEE_ALG_DES_CBC_NOPAD || 2208 alg == TEE_ALG_DES3_CBC_NOPAD) 2209 goto check_element_none; 2210 } 2211 if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 2212 if (alg == TEE_ALG_DES_CBC_MAC_NOPAD || 2213 alg == TEE_ALG_DES_CBC_MAC_PKCS5 || 2214 alg == TEE_ALG_DES3_CBC_MAC_NOPAD || 2215 alg == TEE_ALG_DES3_CBC_MAC_PKCS5) 2216 goto check_element_none; 2217 } 2218 } 2219 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 2220 if (alg == TEE_ALG_MD5) 2221 goto check_element_none; 2222 } 2223 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2224 if (alg == TEE_ALG_SHA1) 2225 goto check_element_none; 2226 } 2227 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2228 if (alg == TEE_ALG_SHA224) 2229 goto check_element_none; 2230 } 2231 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2232 if (alg == TEE_ALG_SHA256) 2233 goto check_element_none; 2234 } 2235 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 2236 if (alg == TEE_ALG_SHA384) 2237 goto check_element_none; 2238 } 2239 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 2240 if (alg == TEE_ALG_SHA512) 2241 goto check_element_none; 2242 } 2243 if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 2244 if (alg == TEE_ALG_MD5SHA1) 2245 goto check_element_none; 2246 } 2247 if (IS_ENABLED(CFG_CRYPTO_HMAC)) { 2248 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 2249 if (alg == TEE_ALG_HMAC_MD5) 2250 goto check_element_none; 2251 } 2252 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2253 if (alg == TEE_ALG_HMAC_SHA1) 2254 goto check_element_none; 2255 } 2256 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2257 if (alg == TEE_ALG_HMAC_SHA224) 2258 goto check_element_none; 2259 } 2260 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2261 if (alg == TEE_ALG_HMAC_SHA256) 2262 goto check_element_none; 2263 } 2264 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 2265 if (alg == TEE_ALG_HMAC_SHA384) 2266 goto check_element_none; 2267 } 2268 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 2269 if (alg == TEE_ALG_HMAC_SHA512) 2270 goto check_element_none; 2271 } 2272 if (IS_ENABLED(CFG_CRYPTO_SM3)) { 2273 if (alg == TEE_ALG_HMAC_SM3) 2274 goto check_element_none; 2275 } 2276 } 2277 if (IS_ENABLED(CFG_CRYPTO_SM3)) { 2278 if (alg == TEE_ALG_SM3) 2279 goto check_element_none; 2280 } 2281 if (IS_ENABLED(CFG_CRYPTO_SM4)) { 2282 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 2283 if (alg == TEE_ALG_SM4_ECB_NOPAD) 2284 goto check_element_none; 2285 } 2286 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 2287 if (alg == TEE_ALG_SM4_CBC_NOPAD) 2288 goto check_element_none; 2289 } 2290 if (IS_ENABLED(CFG_CRYPTO_CTR)) { 2291 if (alg == TEE_ALG_SM4_CTR) 2292 goto check_element_none; 2293 } 2294 if (IS_ENABLED(CFG_CRYPTO_XTS)) { 2295 if (alg == TEE_ALG_SM4_XTS) 2296 goto check_element_none; 2297 } 2298 } 2299 if (IS_ENABLED(CFG_CRYPTO_RSA)) { 2300 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 2301 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5) 2302 goto check_element_none; 2303 } 2304 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2305 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 || 2306 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 || 2307 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1) 2308 goto check_element_none; 2309 } 2310 if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 2311 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1) 2312 goto check_element_none; 2313 } 2314 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2315 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 || 2316 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 || 2317 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224) 2318 goto check_element_none; 2319 } 2320 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2321 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 || 2322 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 || 2323 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256) 2324 goto check_element_none; 2325 } 2326 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 2327 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 || 2328 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 || 2329 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384) 2330 goto check_element_none; 2331 } 2332 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 2333 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 || 2334 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 || 2335 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512) 2336 goto check_element_none; 2337 } 2338 if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 2339 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5) 2340 goto check_element_none; 2341 } 2342 if (alg == TEE_ALG_RSA_NOPAD) 2343 goto check_element_none; 2344 } 2345 if (IS_ENABLED(CFG_CRYPTO_DSA)) { 2346 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2347 if (alg == TEE_ALG_DSA_SHA1) 2348 goto check_element_none; 2349 } 2350 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2351 if (alg == TEE_ALG_DSA_SHA224) 2352 goto check_element_none; 2353 } 2354 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2355 if (alg == TEE_ALG_DSA_SHA256) 2356 goto check_element_none; 2357 } 2358 } 2359 if (IS_ENABLED(CFG_CRYPTO_DH)) { 2360 if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET) 2361 goto check_element_none; 2362 } 2363 if (IS_ENABLED(CFG_CRYPTO_ECC)) { 2364 if ((alg == __OPTEE_ALG_ECDH_P192 || 2365 alg == __OPTEE_ALG_ECDSA_P192 || 2366 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2367 alg == TEE_ALG_ECDSA_SHA1) && 2368 element == TEE_ECC_CURVE_NIST_P192) 2369 return TEE_SUCCESS; 2370 if ((alg == __OPTEE_ALG_ECDH_P224 || 2371 alg == __OPTEE_ALG_ECDSA_P224 || 2372 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2373 alg == TEE_ALG_ECDSA_SHA224) && 2374 element == TEE_ECC_CURVE_NIST_P224) 2375 return TEE_SUCCESS; 2376 if ((alg == __OPTEE_ALG_ECDH_P256 || 2377 alg == __OPTEE_ALG_ECDSA_P256 || 2378 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2379 alg == TEE_ALG_ECDSA_SHA256) && 2380 element == TEE_ECC_CURVE_NIST_P256) 2381 return TEE_SUCCESS; 2382 if ((alg == __OPTEE_ALG_ECDH_P384 || 2383 alg == __OPTEE_ALG_ECDSA_P384 || 2384 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2385 alg == TEE_ALG_ECDSA_SHA384) && 2386 element == TEE_ECC_CURVE_NIST_P384) 2387 return TEE_SUCCESS; 2388 if ((alg == __OPTEE_ALG_ECDH_P521 || 2389 alg == __OPTEE_ALG_ECDSA_P521 || 2390 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2391 alg == TEE_ALG_ECDSA_SHA512) && 2392 element == TEE_ECC_CURVE_NIST_P521) 2393 return TEE_SUCCESS; 2394 } 2395 if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) { 2396 if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2) 2397 return TEE_SUCCESS; 2398 } 2399 if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) { 2400 if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2) 2401 return TEE_SUCCESS; 2402 } 2403 if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) { 2404 if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2) 2405 return TEE_SUCCESS; 2406 } 2407 if (IS_ENABLED(CFG_CRYPTO_X25519)) { 2408 if (alg == TEE_ALG_X25519 && element == TEE_ECC_CURVE_25519) 2409 return TEE_SUCCESS; 2410 } 2411 if (IS_ENABLED(CFG_CRYPTO_ED25519)) { 2412 if (alg == TEE_ALG_ED25519 && element == TEE_ECC_CURVE_25519) 2413 return TEE_SUCCESS; 2414 } 2415 2416 return TEE_ERROR_NOT_SUPPORTED; 2417 check_element_none: 2418 if (element == TEE_CRYPTO_ELEMENT_NONE) 2419 return TEE_SUCCESS; 2420 return TEE_ERROR_NOT_SUPPORTED; 2421 } 2422