1 /* 2 * Copyright (c) 2014, STMicroelectronics International N.V. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #include <stdlib.h> 28 #include <string.h> 29 #include <string_ext.h> 30 31 #include <tee_api.h> 32 #include <tee_api_defines_extensions.h> 33 #include <tee_internal_api_extensions.h> 34 #include <utee_syscalls.h> 35 #include <utee_defines.h> 36 #include <util.h> 37 38 struct __TEE_OperationHandle { 39 TEE_OperationInfo info; 40 TEE_ObjectHandle key1; 41 TEE_ObjectHandle key2; 42 uint8_t *buffer; /* buffer to collect complete blocks */ 43 bool buffer_two_blocks; /* True if two blocks need to be buffered */ 44 size_t block_size; /* Block size of cipher */ 45 size_t buffer_offs; /* Offset in buffer */ 46 uint32_t state; /* Handle to state in TEE Core */ 47 uint32_t ae_tag_len; /* 48 * tag_len in bytes for AE operation else unused 49 */ 50 }; 51 52 /* Cryptographic Operations API - Generic Operation Functions */ 53 54 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 55 uint32_t algorithm, uint32_t mode, 56 uint32_t maxKeySize) 57 { 58 TEE_Result res; 59 TEE_OperationHandle op = TEE_HANDLE_NULL; 60 uint32_t handle_state = 0; 61 size_t block_size = 1; 62 uint32_t req_key_usage; 63 bool with_private_key = false; 64 bool buffer_two_blocks = false; 65 66 if (!operation) 67 TEE_Panic(0); 68 69 if (algorithm == TEE_ALG_AES_XTS) 70 handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 71 72 /* Check algorithm max key size */ 73 switch (algorithm) { 74 case TEE_ALG_DSA_SHA1: 75 if (maxKeySize < 512) 76 return TEE_ERROR_NOT_SUPPORTED; 77 if (maxKeySize > 1024) 78 return TEE_ERROR_NOT_SUPPORTED; 79 if (maxKeySize % 64 != 0) 80 return TEE_ERROR_NOT_SUPPORTED; 81 break; 82 83 case TEE_ALG_DSA_SHA224: 84 if (maxKeySize != 2048) 85 return TEE_ERROR_NOT_SUPPORTED; 86 break; 87 88 case TEE_ALG_DSA_SHA256: 89 if (maxKeySize != 2048 && maxKeySize != 3072) 90 return TEE_ERROR_NOT_SUPPORTED; 91 break; 92 93 case TEE_ALG_ECDSA_P192: 94 case TEE_ALG_ECDH_P192: 95 if (maxKeySize != 192) 96 return TEE_ERROR_NOT_SUPPORTED; 97 break; 98 99 case TEE_ALG_ECDSA_P224: 100 case TEE_ALG_ECDH_P224: 101 if (maxKeySize != 224) 102 return TEE_ERROR_NOT_SUPPORTED; 103 break; 104 105 case TEE_ALG_ECDSA_P256: 106 case TEE_ALG_ECDH_P256: 107 if (maxKeySize != 256) 108 return TEE_ERROR_NOT_SUPPORTED; 109 break; 110 111 case TEE_ALG_ECDSA_P384: 112 case TEE_ALG_ECDH_P384: 113 if (maxKeySize != 384) 114 return TEE_ERROR_NOT_SUPPORTED; 115 break; 116 117 case TEE_ALG_ECDSA_P521: 118 case TEE_ALG_ECDH_P521: 119 if (maxKeySize != 521) 120 return TEE_ERROR_NOT_SUPPORTED; 121 break; 122 123 default: 124 break; 125 } 126 127 /* Check algorithm mode */ 128 switch (algorithm) { 129 case TEE_ALG_AES_CTS: 130 case TEE_ALG_AES_XTS: 131 buffer_two_blocks = true; 132 /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD: 133 case TEE_ALG_AES_CBC_NOPAD: 134 case TEE_ALG_AES_CTR: 135 case TEE_ALG_AES_CCM: 136 case TEE_ALG_AES_GCM: 137 case TEE_ALG_DES_ECB_NOPAD: 138 case TEE_ALG_DES_CBC_NOPAD: 139 case TEE_ALG_DES3_ECB_NOPAD: 140 case TEE_ALG_DES3_CBC_NOPAD: 141 if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 142 block_size = TEE_AES_BLOCK_SIZE; 143 else 144 block_size = TEE_DES_BLOCK_SIZE; 145 146 if (mode == TEE_MODE_ENCRYPT) 147 req_key_usage = TEE_USAGE_ENCRYPT; 148 else if (mode == TEE_MODE_DECRYPT) 149 req_key_usage = TEE_USAGE_DECRYPT; 150 else 151 return TEE_ERROR_NOT_SUPPORTED; 152 break; 153 154 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 155 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 156 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 157 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 158 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 159 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 160 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 161 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 162 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 163 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 164 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 165 case TEE_ALG_DSA_SHA1: 166 case TEE_ALG_DSA_SHA224: 167 case TEE_ALG_DSA_SHA256: 168 case TEE_ALG_ECDSA_P192: 169 case TEE_ALG_ECDSA_P224: 170 case TEE_ALG_ECDSA_P256: 171 case TEE_ALG_ECDSA_P384: 172 case TEE_ALG_ECDSA_P521: 173 if (mode == TEE_MODE_SIGN) { 174 with_private_key = true; 175 req_key_usage = TEE_USAGE_SIGN; 176 } else if (mode == TEE_MODE_VERIFY) { 177 req_key_usage = TEE_USAGE_VERIFY; 178 } else { 179 return TEE_ERROR_NOT_SUPPORTED; 180 } 181 break; 182 183 case TEE_ALG_RSAES_PKCS1_V1_5: 184 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 185 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 186 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 187 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 188 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 189 if (mode == TEE_MODE_ENCRYPT) { 190 req_key_usage = TEE_USAGE_ENCRYPT; 191 } else if (mode == TEE_MODE_DECRYPT) { 192 with_private_key = true; 193 req_key_usage = TEE_USAGE_DECRYPT; 194 } else { 195 return TEE_ERROR_NOT_SUPPORTED; 196 } 197 break; 198 199 case TEE_ALG_RSA_NOPAD: 200 if (mode == TEE_MODE_ENCRYPT) { 201 req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 202 } else if (mode == TEE_MODE_DECRYPT) { 203 with_private_key = true; 204 req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 205 } else { 206 return TEE_ERROR_NOT_SUPPORTED; 207 } 208 break; 209 210 case TEE_ALG_DH_DERIVE_SHARED_SECRET: 211 case TEE_ALG_ECDH_P192: 212 case TEE_ALG_ECDH_P224: 213 case TEE_ALG_ECDH_P256: 214 case TEE_ALG_ECDH_P384: 215 case TEE_ALG_ECDH_P521: 216 case TEE_ALG_HKDF_MD5_DERIVE_KEY: 217 case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 218 case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 219 case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 220 case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 221 case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 222 case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 223 case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 224 case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 225 case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 226 case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 227 case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 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 if (mode != TEE_MODE_DIGEST) 241 return TEE_ERROR_NOT_SUPPORTED; 242 handle_state |= TEE_HANDLE_FLAG_KEY_SET; 243 req_key_usage = 0; 244 break; 245 246 case TEE_ALG_DES_CBC_MAC_NOPAD: 247 case TEE_ALG_AES_CBC_MAC_NOPAD: 248 case TEE_ALG_AES_CBC_MAC_PKCS5: 249 case TEE_ALG_AES_CMAC: 250 case TEE_ALG_DES_CBC_MAC_PKCS5: 251 case TEE_ALG_DES3_CBC_MAC_NOPAD: 252 case TEE_ALG_DES3_CBC_MAC_PKCS5: 253 case TEE_ALG_HMAC_MD5: 254 case TEE_ALG_HMAC_SHA1: 255 case TEE_ALG_HMAC_SHA224: 256 case TEE_ALG_HMAC_SHA256: 257 case TEE_ALG_HMAC_SHA384: 258 case TEE_ALG_HMAC_SHA512: 259 if (mode != TEE_MODE_MAC) 260 return TEE_ERROR_NOT_SUPPORTED; 261 req_key_usage = TEE_USAGE_MAC; 262 break; 263 264 default: 265 return TEE_ERROR_NOT_SUPPORTED; 266 } 267 268 op = TEE_Malloc(sizeof(*op), 0); 269 if (!op) 270 return TEE_ERROR_OUT_OF_MEMORY; 271 272 op->info.algorithm = algorithm; 273 op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 274 op->info.mode = mode; 275 op->info.maxKeySize = maxKeySize; 276 op->info.requiredKeyUsage = req_key_usage; 277 op->info.handleState = handle_state; 278 279 if (block_size > 1) { 280 size_t buffer_size = block_size; 281 282 if (buffer_two_blocks) 283 buffer_size *= 2; 284 285 op->buffer = TEE_Malloc(buffer_size, 286 TEE_USER_MEM_HINT_NO_FILL_ZERO); 287 if (op->buffer == NULL) { 288 res = TEE_ERROR_OUT_OF_MEMORY; 289 goto err0; 290 } 291 } 292 op->block_size = block_size; 293 op->buffer_two_blocks = buffer_two_blocks; 294 295 if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 296 uint32_t mks = maxKeySize; 297 TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 298 with_private_key); 299 300 /* 301 * If two keys are expected the max key size is the sum of 302 * the size of both keys. 303 */ 304 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 305 mks /= 2; 306 307 res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 308 if (res != TEE_SUCCESS) 309 goto err1; 310 311 if ((op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 312 0) { 313 res = TEE_AllocateTransientObject(key_type, mks, 314 &op->key2); 315 if (res != TEE_SUCCESS) 316 goto err2; 317 } 318 } 319 320 res = utee_cryp_state_alloc(algorithm, mode, (uint32_t) op->key1, 321 (uint32_t) op->key2, &op->state); 322 if (res != TEE_SUCCESS) { 323 if ((op->info.handleState & 324 TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0) 325 goto err2; 326 goto err1; 327 } 328 329 /* For multi-stage operation do an "init". */ 330 TEE_ResetOperation(op); 331 *operation = op; 332 goto out; 333 334 err2: 335 TEE_FreeTransientObject(op->key2); 336 err1: 337 TEE_FreeTransientObject(op->key1); 338 err0: 339 TEE_FreeOperation(op); 340 341 if (res != TEE_SUCCESS && 342 res != TEE_ERROR_OUT_OF_MEMORY && 343 res != TEE_ERROR_NOT_SUPPORTED) 344 TEE_Panic(0); 345 out: 346 return res; 347 } 348 349 void TEE_FreeOperation(TEE_OperationHandle operation) 350 { 351 TEE_Result res; 352 353 if (operation == TEE_HANDLE_NULL) 354 TEE_Panic(0); 355 356 /* 357 * Note that keys should not be freed here, since they are 358 * claimed by the operation they will be freed by 359 * utee_cryp_state_free(). 360 */ 361 res = utee_cryp_state_free(operation->state); 362 if (res != TEE_SUCCESS) 363 TEE_Panic(0); 364 365 TEE_Free(operation->buffer); 366 TEE_Free(operation); 367 } 368 369 void TEE_GetOperationInfo(TEE_OperationHandle operation, 370 TEE_OperationInfo *operationInfo) 371 { 372 if (operation == TEE_HANDLE_NULL) 373 TEE_Panic(0); 374 375 if (operationInfo == NULL) 376 TEE_Panic(0); 377 378 *operationInfo = operation->info; 379 } 380 381 void TEE_ResetOperation(TEE_OperationHandle operation) 382 { 383 TEE_Result res; 384 385 if (operation == TEE_HANDLE_NULL) 386 TEE_Panic(0); 387 388 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) != 0) { 389 if (operation->info.keySize == 0) 390 TEE_Panic(0); 391 } 392 393 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 394 res = utee_hash_init(operation->state, NULL, 0); 395 if (res != TEE_SUCCESS) 396 TEE_Panic(res); 397 } 398 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 399 } 400 401 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 402 TEE_ObjectHandle key) 403 { 404 TEE_Result res; 405 uint32_t key_size = 0; 406 TEE_ObjectInfo key_info; 407 408 /* Operation is not a valid handle */ 409 if (operation == TEE_HANDLE_NULL) { 410 res = TEE_ERROR_BAD_PARAMETERS; 411 goto out; 412 } 413 414 /* Key is not initialized */ 415 if (key == TEE_HANDLE_NULL) { 416 /* Operation key cleared */ 417 TEE_ResetTransientObject(operation->key1); 418 res = TEE_ERROR_BAD_PARAMETERS; 419 goto out; 420 } 421 422 /* No key for digest operation */ 423 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 424 res = TEE_ERROR_BAD_PARAMETERS; 425 goto out; 426 } 427 428 /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 429 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 430 0) { 431 res = TEE_ERROR_BAD_PARAMETERS; 432 goto out; 433 } 434 435 res = TEE_GetObjectInfo1(key, &key_info); 436 /* Key is not a valid handle */ 437 if (res != TEE_SUCCESS) 438 goto out; 439 440 /* Supplied key has to meet required usage */ 441 if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 442 operation->info.requiredKeyUsage) { 443 res = TEE_ERROR_BAD_PARAMETERS; 444 goto out; 445 } 446 447 if (operation->info.maxKeySize < key_info.keySize) { 448 res = TEE_ERROR_BAD_PARAMETERS; 449 goto out; 450 } 451 452 key_size = key_info.keySize; 453 454 TEE_ResetTransientObject(operation->key1); 455 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 456 457 res = TEE_CopyObjectAttributes1(operation->key1, key); 458 if (res != TEE_SUCCESS) 459 goto out; 460 461 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 462 463 operation->info.keySize = key_size; 464 465 out: 466 if (res != TEE_SUCCESS && 467 res != TEE_ERROR_CORRUPT_OBJECT && 468 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 469 TEE_Panic(0); 470 471 return res; 472 } 473 474 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 475 TEE_ObjectHandle key1, TEE_ObjectHandle key2) 476 { 477 TEE_Result res; 478 uint32_t key_size = 0; 479 TEE_ObjectInfo key_info1; 480 TEE_ObjectInfo key_info2; 481 482 /* Operation is not a valid handle */ 483 if (operation == TEE_HANDLE_NULL) { 484 res = TEE_ERROR_BAD_PARAMETERS; 485 goto out; 486 } 487 488 /* 489 * Key1/Key2 and/or are not initialized and 490 * Either both keys are NULL or both are not NULL 491 */ 492 if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 493 /* Clear operation key1 (if needed) */ 494 if (key1 == TEE_HANDLE_NULL) 495 TEE_ResetTransientObject(operation->key1); 496 /* Clear operation key2 (if needed) */ 497 if (key2 == TEE_HANDLE_NULL) 498 TEE_ResetTransientObject(operation->key2); 499 res = TEE_ERROR_BAD_PARAMETERS; 500 goto out; 501 } 502 503 /* No key for digest operation */ 504 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 505 res = TEE_ERROR_BAD_PARAMETERS; 506 goto out; 507 } 508 509 /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 510 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 511 0) { 512 res = TEE_ERROR_BAD_PARAMETERS; 513 goto out; 514 } 515 516 res = TEE_GetObjectInfo1(key1, &key_info1); 517 /* Key1 is not a valid handle */ 518 if (res != TEE_SUCCESS) 519 goto out; 520 521 /* Supplied key has to meet required usage */ 522 if ((key_info1.objectUsage & operation->info. 523 requiredKeyUsage) != operation->info.requiredKeyUsage) { 524 res = TEE_ERROR_BAD_PARAMETERS; 525 goto out; 526 } 527 528 res = TEE_GetObjectInfo1(key2, &key_info2); 529 /* Key2 is not a valid handle */ 530 if (res != TEE_SUCCESS) { 531 if (res == TEE_ERROR_CORRUPT_OBJECT) 532 res = TEE_ERROR_CORRUPT_OBJECT_2; 533 goto out; 534 } 535 536 /* Supplied key has to meet required usage */ 537 if ((key_info2.objectUsage & operation->info. 538 requiredKeyUsage) != operation->info.requiredKeyUsage) { 539 res = TEE_ERROR_BAD_PARAMETERS; 540 goto out; 541 } 542 543 /* 544 * AES-XTS (the only multi key algorithm supported, requires the 545 * keys to be of equal size. 546 */ 547 if (operation->info.algorithm == TEE_ALG_AES_XTS && 548 key_info1.keySize != key_info2.keySize) { 549 res = TEE_ERROR_BAD_PARAMETERS; 550 goto out; 551 552 } 553 554 if (operation->info.maxKeySize < key_info1.keySize) { 555 res = TEE_ERROR_BAD_PARAMETERS; 556 goto out; 557 } 558 559 /* 560 * Odd that only the size of one key should be reported while 561 * size of two key are used when allocating the operation. 562 */ 563 key_size = key_info1.keySize; 564 565 TEE_ResetTransientObject(operation->key1); 566 TEE_ResetTransientObject(operation->key2); 567 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 568 569 res = TEE_CopyObjectAttributes1(operation->key1, key1); 570 if (res != TEE_SUCCESS) 571 goto out; 572 573 res = TEE_CopyObjectAttributes1(operation->key2, key2); 574 if (res != TEE_SUCCESS) { 575 if (res == TEE_ERROR_CORRUPT_OBJECT) 576 res = TEE_ERROR_CORRUPT_OBJECT_2; 577 goto out; 578 } 579 580 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 581 582 operation->info.keySize = key_size; 583 584 out: 585 if (res != TEE_SUCCESS && 586 res != TEE_ERROR_CORRUPT_OBJECT && 587 res != TEE_ERROR_CORRUPT_OBJECT_2 && 588 res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 589 res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 590 TEE_Panic(0); 591 592 return res; 593 } 594 595 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 596 { 597 TEE_Result res; 598 599 if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 600 TEE_Panic(0); 601 if (dst_op->info.algorithm != src_op->info.algorithm) 602 TEE_Panic(0); 603 if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 604 TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 605 TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 606 607 if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 608 key1 = src_op->key1; 609 key2 = src_op->key2; 610 } 611 612 if ((src_op->info.handleState & 613 TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 614 TEE_SetOperationKey(dst_op, key1); 615 } else { 616 TEE_SetOperationKey2(dst_op, key1, key2); 617 } 618 } 619 dst_op->info.handleState = src_op->info.handleState; 620 dst_op->info.keySize = src_op->info.keySize; 621 622 if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 623 dst_op->block_size != src_op->block_size) 624 TEE_Panic(0); 625 626 if (dst_op->buffer != NULL) { 627 if (src_op->buffer == NULL) 628 TEE_Panic(0); 629 630 memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 631 dst_op->buffer_offs = src_op->buffer_offs; 632 } else if (src_op->buffer != NULL) { 633 TEE_Panic(0); 634 } 635 636 res = utee_cryp_state_copy(dst_op->state, src_op->state); 637 if (res != TEE_SUCCESS) 638 TEE_Panic(res); 639 } 640 641 /* Cryptographic Operations API - Message Digest Functions */ 642 643 void TEE_DigestUpdate(TEE_OperationHandle operation, 644 void *chunk, uint32_t chunkSize) 645 { 646 TEE_Result res = TEE_ERROR_GENERIC; 647 648 if (operation == TEE_HANDLE_NULL || 649 operation->info.operationClass != TEE_OPERATION_DIGEST) 650 TEE_Panic(0); 651 652 res = utee_hash_update(operation->state, chunk, chunkSize); 653 if (res != TEE_SUCCESS) 654 TEE_Panic(res); 655 } 656 657 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 658 uint32_t chunkLen, void *hash, uint32_t *hashLen) 659 { 660 TEE_Result res; 661 662 if ((operation == TEE_HANDLE_NULL) || 663 (!chunk && chunkLen) || 664 !hash || 665 !hashLen || 666 (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 667 res = TEE_ERROR_BAD_PARAMETERS; 668 goto out; 669 } 670 671 res = utee_hash_final(operation->state, chunk, chunkLen, hash, 672 hashLen); 673 674 out: 675 if (res != TEE_SUCCESS && 676 res != TEE_ERROR_SHORT_BUFFER) 677 TEE_Panic(0); 678 679 return res; 680 } 681 682 /* Cryptographic Operations API - Symmetric Cipher Functions */ 683 684 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 685 { 686 TEE_Result res; 687 688 if (operation == TEE_HANDLE_NULL) 689 TEE_Panic(0); 690 if (operation->info.operationClass != TEE_OPERATION_CIPHER) 691 TEE_Panic(0); 692 res = utee_cipher_init(operation->state, IV, IVLen); 693 if (res != TEE_SUCCESS) 694 TEE_Panic(res); 695 operation->buffer_offs = 0; 696 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 697 } 698 699 static TEE_Result tee_buffer_update( 700 TEE_OperationHandle op, 701 TEE_Result(*update_func) (uint32_t state, const void *src, 702 size_t slen, void *dst, uint32_t *dlen), 703 const void *src_data, size_t src_len, 704 void *dest_data, uint32_t *dest_len) 705 { 706 TEE_Result res; 707 const uint8_t *src = src_data; 708 size_t slen = src_len; 709 uint8_t *dst = dest_data; 710 size_t dlen = *dest_len; 711 size_t acc_dlen = 0; 712 uint32_t tmp_dlen; 713 size_t l; 714 size_t buffer_size; 715 size_t buffer_left; 716 717 if (op->buffer_two_blocks) { 718 buffer_size = op->block_size * 2; 719 buffer_left = 1; 720 } else { 721 buffer_size = op->block_size; 722 buffer_left = 0; 723 } 724 725 if (op->buffer_offs > 0) { 726 /* Fill up complete block */ 727 if (op->buffer_offs < op->block_size) 728 l = MIN(slen, op->block_size - op->buffer_offs); 729 else 730 l = MIN(slen, buffer_size - op->buffer_offs); 731 memcpy(op->buffer + op->buffer_offs, src, l); 732 op->buffer_offs += l; 733 src += l; 734 slen -= l; 735 if ((op->buffer_offs % op->block_size) != 0) 736 goto out; /* Nothing left to do */ 737 } 738 739 /* If we can feed from buffer */ 740 if ((op->buffer_offs > 0) && 741 ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 742 l = ROUNDUP(op->buffer_offs + slen - buffer_size, 743 op->block_size); 744 l = MIN(op->buffer_offs, l); 745 tmp_dlen = dlen; 746 res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 747 if (res != TEE_SUCCESS) 748 TEE_Panic(res); 749 dst += tmp_dlen; 750 dlen -= tmp_dlen; 751 acc_dlen += tmp_dlen; 752 op->buffer_offs -= l; 753 if (op->buffer_offs > 0) { 754 /* 755 * Slen is small enough to be contained in rest buffer. 756 */ 757 memcpy(op->buffer, op->buffer + l, buffer_size - l); 758 memcpy(op->buffer + op->buffer_offs, src, slen); 759 op->buffer_offs += slen; 760 goto out; /* Nothing left to do */ 761 } 762 } 763 764 if (slen >= (buffer_size + buffer_left)) { 765 /* Buffer is empty, feed as much as possible from src */ 766 if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) { 767 l = ROUNDUP(slen - buffer_size + 1, op->block_size); 768 769 tmp_dlen = dlen; 770 res = update_func(op->state, src, l, dst, &tmp_dlen); 771 if (res != TEE_SUCCESS) 772 TEE_Panic(res); 773 src += l; 774 slen -= l; 775 dst += tmp_dlen; 776 dlen -= tmp_dlen; 777 acc_dlen += tmp_dlen; 778 } else { 779 /* 780 * Supplied data isn't well aligned, we're forced to 781 * feed through the buffer. 782 */ 783 while (slen >= op->block_size) { 784 memcpy(op->buffer, src, op->block_size); 785 786 tmp_dlen = dlen; 787 res = 788 update_func(op->state, op->buffer, 789 op->block_size, dst, &tmp_dlen); 790 if (res != TEE_SUCCESS) 791 TEE_Panic(res); 792 src += op->block_size; 793 slen -= op->block_size; 794 dst += tmp_dlen; 795 dlen -= tmp_dlen; 796 acc_dlen += tmp_dlen; 797 } 798 } 799 } 800 801 /* Slen is small enough to be contained in buffer. */ 802 memcpy(op->buffer + op->buffer_offs, src, slen); 803 op->buffer_offs += slen; 804 805 out: 806 *dest_len = acc_dlen; 807 return TEE_SUCCESS; 808 } 809 810 TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData, 811 uint32_t srcLen, void *destData, uint32_t *destLen) 812 { 813 size_t req_dlen; 814 815 if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 816 destLen == NULL || (destData == NULL && *destLen != 0)) 817 TEE_Panic(0); 818 if (op->info.operationClass != TEE_OPERATION_CIPHER) 819 TEE_Panic(0); 820 if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 821 TEE_Panic(0); 822 823 /* Calculate required dlen */ 824 req_dlen = ((op->buffer_offs + srcLen) / op->block_size) * 825 op->block_size; 826 if (op->buffer_two_blocks) { 827 if (req_dlen > op->block_size * 2) 828 req_dlen -= op->block_size * 2; 829 else 830 req_dlen = 0; 831 } 832 /* 833 * Check that required destLen is big enough before starting to feed 834 * data to the algorithm. Errors during feeding of data are fatal as we 835 * can't restore sync with this API. 836 */ 837 if (*destLen < req_dlen) { 838 *destLen = req_dlen; 839 return TEE_ERROR_SHORT_BUFFER; 840 } 841 842 tee_buffer_update(op, utee_cipher_update, srcData, srcLen, destData, 843 destLen); 844 845 return TEE_SUCCESS; 846 } 847 848 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op, 849 const void *srcData, uint32_t srcLen, void *destData, 850 uint32_t *destLen) 851 { 852 TEE_Result res; 853 uint8_t *dst = destData; 854 size_t acc_dlen = 0; 855 uint32_t tmp_dlen; 856 size_t req_dlen; 857 858 if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 859 destLen == NULL || (destData == NULL && *destLen != 0)) 860 TEE_Panic(0); 861 if (op->info.operationClass != TEE_OPERATION_CIPHER) 862 TEE_Panic(0); 863 if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 864 TEE_Panic(0); 865 866 /* 867 * Check that the final block doesn't require padding for those 868 * algorithms that requires client to supply padding. 869 */ 870 if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 871 op->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 872 op->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 873 op->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 874 op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 875 op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 876 if (((op->buffer_offs + srcLen) % op->block_size) != 0) 877 return TEE_ERROR_BAD_PARAMETERS; 878 } 879 880 /* 881 * Check that required destLen is big enough before starting to feed 882 * data to the algorithm. Errors during feeding of data are fatal as we 883 * can't restore sync with this API. 884 */ 885 req_dlen = op->buffer_offs + srcLen; 886 if (*destLen < req_dlen) { 887 *destLen = req_dlen; 888 return TEE_ERROR_SHORT_BUFFER; 889 } 890 891 tmp_dlen = *destLen - acc_dlen; 892 tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst, 893 &tmp_dlen); 894 dst += tmp_dlen; 895 acc_dlen += tmp_dlen; 896 897 tmp_dlen = *destLen - acc_dlen; 898 res = utee_cipher_final(op->state, op->buffer, op->buffer_offs, 899 dst, &tmp_dlen); 900 if (res != TEE_SUCCESS) 901 TEE_Panic(res); 902 acc_dlen += tmp_dlen; 903 904 op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 905 *destLen = acc_dlen; 906 return TEE_SUCCESS; 907 } 908 909 /* Cryptographic Operations API - MAC Functions */ 910 911 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 912 { 913 TEE_Result res; 914 915 if (operation == TEE_HANDLE_NULL) 916 TEE_Panic(0); 917 if (IV == NULL && IVLen != 0) 918 TEE_Panic(0); 919 if (operation->info.operationClass != TEE_OPERATION_MAC) 920 TEE_Panic(0); 921 res = utee_hash_init(operation->state, IV, IVLen); 922 if (res != TEE_SUCCESS) 923 TEE_Panic(res); 924 operation->buffer_offs = 0; 925 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 926 } 927 928 void TEE_MACUpdate(TEE_OperationHandle op, const void *chunk, uint32_t chunkSize) 929 { 930 TEE_Result res; 931 932 if (op == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 933 TEE_Panic(0); 934 if (op->info.operationClass != TEE_OPERATION_MAC) 935 TEE_Panic(0); 936 if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 937 TEE_Panic(0); 938 939 res = utee_hash_update(op->state, chunk, chunkSize); 940 if (res != TEE_SUCCESS) 941 TEE_Panic(res); 942 } 943 944 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle op, 945 const void *message, uint32_t messageLen, 946 void *mac, uint32_t *macLen) 947 { 948 TEE_Result res; 949 950 if (op == TEE_HANDLE_NULL || (message == NULL && messageLen != 0) || 951 mac == NULL || macLen == NULL) 952 TEE_Panic(0); 953 if (op->info.operationClass != TEE_OPERATION_MAC) 954 TEE_Panic(0); 955 if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 956 TEE_Panic(0); 957 958 res = utee_hash_final(op->state, message, messageLen, mac, macLen); 959 op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 960 return res; 961 } 962 963 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 964 const void *message, uint32_t messageLen, 965 const void *mac, uint32_t macLen) 966 { 967 TEE_Result res; 968 uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 969 uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 970 971 res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 972 &computed_mac_size); 973 if (res != TEE_SUCCESS) 974 return res; 975 if (computed_mac_size != macLen) 976 return TEE_ERROR_MAC_INVALID; 977 if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) 978 return TEE_ERROR_MAC_INVALID; 979 return TEE_SUCCESS; 980 } 981 982 /* Cryptographic Operations API - Authenticated Encryption Functions */ 983 984 TEE_Result TEE_AEInit(TEE_OperationHandle op, const void *nonce, 985 uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 986 uint32_t payloadLen) 987 { 988 TEE_Result res; 989 990 if (op == TEE_HANDLE_NULL || nonce == NULL) 991 TEE_Panic(0); 992 if (op->info.operationClass != TEE_OPERATION_AE) 993 TEE_Panic(0); 994 995 /* 996 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 997 * in the implementation. But AES-GCM spec doesn't specify the tag len 998 * according to the same principle so we have to check here instead to 999 * be GP compliant. 1000 */ 1001 if (op->info.algorithm == TEE_ALG_AES_GCM) { 1002 /* 1003 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1004 */ 1005 if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) 1006 return TEE_ERROR_NOT_SUPPORTED; 1007 } 1008 1009 res = utee_authenc_init(op->state, nonce, nonceLen, tagLen / 8, AADLen, 1010 payloadLen); 1011 if (res != TEE_SUCCESS) { 1012 if (res != TEE_ERROR_NOT_SUPPORTED) 1013 TEE_Panic(res); 1014 return res; 1015 } 1016 op->ae_tag_len = tagLen / 8; 1017 1018 op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1019 return TEE_SUCCESS; 1020 } 1021 1022 void TEE_AEUpdateAAD(TEE_OperationHandle op, const void *AADdata, 1023 uint32_t AADdataLen) 1024 { 1025 TEE_Result res; 1026 1027 if (op == TEE_HANDLE_NULL || (AADdata == NULL && AADdataLen != 0)) 1028 TEE_Panic(0); 1029 if (op->info.operationClass != TEE_OPERATION_AE) 1030 TEE_Panic(0); 1031 if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1032 TEE_Panic(0); 1033 1034 res = utee_authenc_update_aad(op->state, AADdata, AADdataLen); 1035 if (res != TEE_SUCCESS) 1036 TEE_Panic(res); 1037 } 1038 1039 TEE_Result TEE_AEUpdate(TEE_OperationHandle op, const void *srcData, 1040 uint32_t srcLen, void *destData, uint32_t *destLen) 1041 { 1042 size_t req_dlen; 1043 1044 if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1045 destLen == NULL || (destData == NULL && *destLen != 0)) 1046 TEE_Panic(0); 1047 if (op->info.operationClass != TEE_OPERATION_AE) 1048 TEE_Panic(0); 1049 if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1050 TEE_Panic(0); 1051 1052 /* 1053 * Check that required destLen is big enough before starting to feed 1054 * data to the algorithm. Errors during feeding of data are fatal as we 1055 * can't restore sync with this API. 1056 */ 1057 req_dlen = ROUNDDOWN(op->buffer_offs + srcLen, op->block_size); 1058 if (*destLen < req_dlen) { 1059 *destLen = req_dlen; 1060 return TEE_ERROR_SHORT_BUFFER; 1061 } 1062 1063 tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen, 1064 destData, destLen); 1065 1066 return TEE_SUCCESS; 1067 } 1068 1069 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle op, 1070 const void *srcData, uint32_t srcLen, 1071 void *destData, uint32_t *destLen, void *tag, 1072 uint32_t *tagLen) 1073 { 1074 TEE_Result res; 1075 uint8_t *dst = destData; 1076 size_t acc_dlen = 0; 1077 uint32_t tmp_dlen; 1078 size_t req_dlen; 1079 1080 if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1081 destLen == NULL || (destData == NULL && *destLen != 0) || 1082 tag == NULL || tagLen == NULL) 1083 TEE_Panic(0); 1084 if (op->info.operationClass != TEE_OPERATION_AE) 1085 TEE_Panic(0); 1086 if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1087 TEE_Panic(0); 1088 1089 /* 1090 * Check that required destLen is big enough before starting to feed 1091 * data to the algorithm. Errors during feeding of data are fatal as we 1092 * can't restore sync with this API. 1093 */ 1094 req_dlen = op->buffer_offs + srcLen; 1095 if (*destLen < req_dlen) { 1096 *destLen = req_dlen; 1097 return TEE_ERROR_SHORT_BUFFER; 1098 } 1099 1100 /* 1101 * Need to check this before update_payload since sync would be lost if 1102 * we return short buffer after that. 1103 */ 1104 if (*tagLen < op->ae_tag_len) { 1105 *tagLen = op->ae_tag_len; 1106 return TEE_ERROR_SHORT_BUFFER; 1107 } 1108 1109 tmp_dlen = *destLen - acc_dlen; 1110 tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen, 1111 dst, &tmp_dlen); 1112 dst += tmp_dlen; 1113 acc_dlen += tmp_dlen; 1114 1115 tmp_dlen = *destLen - acc_dlen; 1116 res = 1117 utee_authenc_enc_final(op->state, op->buffer, op->buffer_offs, dst, 1118 &tmp_dlen, tag, tagLen); 1119 if (res != TEE_SUCCESS) 1120 TEE_Panic(res); 1121 acc_dlen += tmp_dlen; 1122 1123 *destLen = acc_dlen; 1124 op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1125 1126 return res; 1127 } 1128 1129 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle op, 1130 const void *srcData, uint32_t srcLen, 1131 void *destData, uint32_t *destLen, const void *tag, 1132 uint32_t tagLen) 1133 { 1134 TEE_Result res; 1135 uint8_t *dst = destData; 1136 size_t acc_dlen = 0; 1137 uint32_t tmp_dlen; 1138 size_t req_dlen; 1139 1140 if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1141 destLen == NULL || (destData == NULL && *destLen != 0) || 1142 (tag == NULL && tagLen != 0)) 1143 TEE_Panic(0); 1144 if (op->info.operationClass != TEE_OPERATION_AE) 1145 TEE_Panic(0); 1146 if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1147 TEE_Panic(0); 1148 1149 /* 1150 * Check that required destLen is big enough before starting to feed 1151 * data to the algorithm. Errors during feeding of data are fatal as we 1152 * can't restore sync with this API. 1153 */ 1154 req_dlen = op->buffer_offs + srcLen; 1155 if (*destLen < req_dlen) { 1156 *destLen = req_dlen; 1157 return TEE_ERROR_SHORT_BUFFER; 1158 } 1159 1160 tmp_dlen = *destLen - acc_dlen; 1161 tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen, 1162 dst, &tmp_dlen); 1163 dst += tmp_dlen; 1164 acc_dlen += tmp_dlen; 1165 1166 tmp_dlen = *destLen - acc_dlen; 1167 res = 1168 utee_authenc_dec_final(op->state, op->buffer, op->buffer_offs, dst, 1169 &tmp_dlen, tag, tagLen); 1170 if (res != TEE_SUCCESS && res != TEE_ERROR_MAC_INVALID) 1171 TEE_Panic(res); 1172 /* Supplied tagLen should match what we initiated with */ 1173 if (tagLen != op->ae_tag_len) 1174 res = TEE_ERROR_MAC_INVALID; 1175 1176 acc_dlen += tmp_dlen; 1177 1178 *destLen = acc_dlen; 1179 op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1180 1181 return res; 1182 } 1183 1184 /* Cryptographic Operations API - Asymmetric Functions */ 1185 1186 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle op, 1187 const TEE_Attribute *params, 1188 uint32_t paramCount, const void *srcData, 1189 uint32_t srcLen, void *destData, 1190 uint32_t *destLen) 1191 { 1192 TEE_Result res; 1193 1194 if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1195 destLen == NULL || (destData == NULL && *destLen != 0)) 1196 TEE_Panic(0); 1197 if (paramCount != 0 && params == NULL) 1198 TEE_Panic(0); 1199 if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1200 TEE_Panic(0); 1201 if (op->info.mode != TEE_MODE_ENCRYPT) 1202 TEE_Panic(0); 1203 1204 res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen, 1205 destData, destLen); 1206 if (res != TEE_SUCCESS && 1207 res != TEE_ERROR_SHORT_BUFFER && 1208 res != TEE_ERROR_BAD_PARAMETERS) 1209 TEE_Panic(res); 1210 return res; 1211 } 1212 1213 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle op, 1214 const TEE_Attribute *params, 1215 uint32_t paramCount, const void *srcData, 1216 uint32_t srcLen, void *destData, 1217 uint32_t *destLen) 1218 { 1219 TEE_Result res; 1220 1221 if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1222 destLen == NULL || (destData == NULL && *destLen != 0)) 1223 TEE_Panic(0); 1224 if (paramCount != 0 && params == NULL) 1225 TEE_Panic(0); 1226 if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1227 TEE_Panic(0); 1228 if (op->info.mode != TEE_MODE_DECRYPT) 1229 TEE_Panic(0); 1230 1231 res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen, 1232 destData, destLen); 1233 if (res != TEE_SUCCESS && 1234 res != TEE_ERROR_SHORT_BUFFER && 1235 res != TEE_ERROR_BAD_PARAMETERS) 1236 TEE_Panic(res); 1237 return res; 1238 } 1239 1240 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle op, 1241 const TEE_Attribute *params, 1242 uint32_t paramCount, const void *digest, 1243 uint32_t digestLen, void *signature, 1244 uint32_t *signatureLen) 1245 { 1246 TEE_Result res; 1247 1248 if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) || 1249 signature == NULL || signatureLen == NULL) 1250 TEE_Panic(0); 1251 if (paramCount != 0 && params == NULL) 1252 TEE_Panic(0); 1253 if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1254 TEE_Panic(0); 1255 if (op->info.mode != TEE_MODE_SIGN) 1256 TEE_Panic(0); 1257 1258 res = 1259 utee_asymm_operate(op->state, params, paramCount, digest, digestLen, 1260 signature, signatureLen); 1261 if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1262 TEE_Panic(res); 1263 return res; 1264 } 1265 1266 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle op, 1267 const TEE_Attribute *params, 1268 uint32_t paramCount, const void *digest, 1269 uint32_t digestLen, const void *signature, 1270 uint32_t signatureLen) 1271 { 1272 TEE_Result res; 1273 1274 if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) || 1275 (signature == NULL && signatureLen != 0)) 1276 TEE_Panic(0); 1277 if (paramCount != 0 && params == NULL) 1278 TEE_Panic(0); 1279 if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1280 TEE_Panic(0); 1281 if (op->info.mode != TEE_MODE_VERIFY) 1282 TEE_Panic(0); 1283 1284 res = 1285 utee_asymm_verify(op->state, params, paramCount, digest, digestLen, 1286 signature, signatureLen); 1287 if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1288 TEE_Panic(res); 1289 return res; 1290 } 1291 1292 /* Cryptographic Operations API - Key Derivation Functions */ 1293 1294 void TEE_DeriveKey(TEE_OperationHandle operation, 1295 const TEE_Attribute *params, uint32_t paramCount, 1296 TEE_ObjectHandle derivedKey) 1297 { 1298 TEE_Result res; 1299 TEE_ObjectInfo key_info; 1300 1301 if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1302 TEE_Panic(0); 1303 if (paramCount != 0 && params == NULL) 1304 TEE_Panic(0); 1305 if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 1306 TEE_OPERATION_KEY_DERIVATION) 1307 TEE_Panic(0); 1308 1309 if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1310 TEE_Panic(0); 1311 if (operation->info.mode != TEE_MODE_DERIVE) 1312 TEE_Panic(0); 1313 if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1314 TEE_Panic(0); 1315 1316 res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info); 1317 if (res != TEE_SUCCESS) 1318 TEE_Panic(0); 1319 1320 if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1321 TEE_Panic(0); 1322 if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1323 TEE_Panic(0); 1324 1325 res = utee_cryp_derive_key(operation->state, params, paramCount, 1326 (uint32_t) derivedKey); 1327 if (res != TEE_SUCCESS) 1328 TEE_Panic(res); 1329 } 1330 1331 /* Cryptographic Operations API - Random Number Generation Functions */ 1332 1333 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1334 { 1335 TEE_Result res; 1336 1337 res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1338 if (res != TEE_SUCCESS) 1339 TEE_Panic(res); 1340 } 1341