1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2020, 2022-2023 Linaro Limited 5 * Copyright (c) 2022, Technology Innovation Institute (TII) 6 */ 7 8 #include <assert.h> 9 #include <bitstring.h> 10 #include <compiler.h> 11 #include <config.h> 12 #include <crypto/crypto.h> 13 #include <kernel/tee_ta_manager.h> 14 #include <kernel/user_access.h> 15 #include <memtag.h> 16 #include <mm/vm.h> 17 #include <stdlib_ext.h> 18 #include <string_ext.h> 19 #include <string.h> 20 #include <sys/queue.h> 21 #include <tee_api_defines_extensions.h> 22 #include <tee_api_types.h> 23 #include <tee/tee_cryp_utl.h> 24 #include <tee/tee_obj.h> 25 #include <tee/tee_pobj.h> 26 #include <tee/tee_svc_cryp.h> 27 #include <tee/tee_svc_storage.h> 28 #include <tee/tee_svc.h> 29 #include <trace.h> 30 #include <utee_defines.h> 31 #include <util.h> 32 #if defined(CFG_CRYPTO_HKDF) 33 #include <tee/tee_cryp_hkdf.h> 34 #endif 35 #if defined(CFG_CRYPTO_CONCAT_KDF) 36 #include <tee/tee_cryp_concat_kdf.h> 37 #endif 38 #if defined(CFG_CRYPTO_PBKDF2) 39 #include <tee/tee_cryp_pbkdf2.h> 40 #endif 41 42 enum cryp_state { 43 CRYP_STATE_INITIALIZED = 0, 44 CRYP_STATE_UNINITIALIZED 45 }; 46 47 typedef void (*tee_cryp_ctx_finalize_func_t) (void *ctx); 48 struct tee_cryp_state { 49 TAILQ_ENTRY(tee_cryp_state) link; 50 uint32_t algo; 51 uint32_t mode; 52 vaddr_t key1; 53 vaddr_t key2; 54 void *ctx; 55 tee_cryp_ctx_finalize_func_t ctx_finalize; 56 enum cryp_state state; 57 }; 58 59 struct tee_cryp_obj_secret { 60 uint32_t key_size; 61 uint32_t alloc_size; 62 63 /* 64 * Pseudo code visualize layout of structure 65 * Next follows data, such as: 66 * uint8_t data[alloc_size] 67 * key_size must never exceed alloc_size 68 */ 69 }; 70 71 #define TEE_TYPE_ATTR_OPTIONAL BIT(0) 72 #define TEE_TYPE_ATTR_REQUIRED BIT(1) 73 #define TEE_TYPE_ATTR_OPTIONAL_GROUP BIT(2) 74 #define TEE_TYPE_ATTR_SIZE_INDICATOR BIT(3) 75 #define TEE_TYPE_ATTR_GEN_KEY_OPT BIT(4) 76 #define TEE_TYPE_ATTR_GEN_KEY_REQ BIT(5) 77 #define TEE_TYPE_ATTR_BIGNUM_MAXBITS BIT(6) 78 79 /* Handle storing of generic secret keys of varying lengths */ 80 #define ATTR_OPS_INDEX_SECRET 0 81 /* Convert to/from big-endian byte array and provider-specific bignum */ 82 #define ATTR_OPS_INDEX_BIGNUM 1 83 /* Convert to/from value attribute depending on direction */ 84 #define ATTR_OPS_INDEX_VALUE 2 85 /* Convert to/from curve25519 attribute depending on direction */ 86 #define ATTR_OPS_INDEX_25519 3 87 #define ATTR_OPS_INDEX_448 4 88 89 /* Curve25519 key bytes size is always 32 bytes*/ 90 #define KEY_SIZE_BYTES_25519 UL(32) 91 #define KEY_SIZE_BYTES_448 UL(56) 92 /* TEE Internal Core API v1.3.1, Table 6-8 */ 93 #define TEE_ED25519_CTX_MAX_LENGTH 255 94 95 struct tee_cryp_obj_type_attrs { 96 uint32_t attr_id; 97 uint16_t flags; 98 uint16_t ops_index; 99 uint16_t raw_offs; 100 uint16_t raw_size; 101 }; 102 103 #define RAW_DATA(_x, _y) \ 104 .raw_offs = offsetof(_x, _y), .raw_size = MEMBER_SIZE(_x, _y) 105 106 static const struct tee_cryp_obj_type_attrs 107 tee_cryp_obj_secret_value_attrs[] = { 108 { 109 .attr_id = TEE_ATTR_SECRET_VALUE, 110 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 111 .ops_index = ATTR_OPS_INDEX_SECRET, 112 .raw_offs = 0, 113 .raw_size = 0 114 }, 115 }; 116 117 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_pub_key_attrs[] = { 118 { 119 .attr_id = TEE_ATTR_RSA_MODULUS, 120 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 121 .ops_index = ATTR_OPS_INDEX_BIGNUM, 122 RAW_DATA(struct rsa_public_key, n) 123 }, 124 125 { 126 .attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT, 127 .flags = TEE_TYPE_ATTR_REQUIRED, 128 .ops_index = ATTR_OPS_INDEX_BIGNUM, 129 RAW_DATA(struct rsa_public_key, e) 130 }, 131 }; 132 133 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_keypair_attrs[] = { 134 { 135 .attr_id = TEE_ATTR_RSA_MODULUS, 136 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 137 .ops_index = ATTR_OPS_INDEX_BIGNUM, 138 RAW_DATA(struct rsa_keypair, n) 139 }, 140 141 { 142 .attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT, 143 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_OPT, 144 .ops_index = ATTR_OPS_INDEX_BIGNUM, 145 RAW_DATA(struct rsa_keypair, e) 146 }, 147 148 { 149 .attr_id = TEE_ATTR_RSA_PRIVATE_EXPONENT, 150 .flags = TEE_TYPE_ATTR_REQUIRED, 151 .ops_index = ATTR_OPS_INDEX_BIGNUM, 152 RAW_DATA(struct rsa_keypair, d) 153 }, 154 155 { 156 .attr_id = TEE_ATTR_RSA_PRIME1, 157 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 158 .ops_index = ATTR_OPS_INDEX_BIGNUM, 159 RAW_DATA(struct rsa_keypair, p) 160 }, 161 162 { 163 .attr_id = TEE_ATTR_RSA_PRIME2, 164 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 165 .ops_index = ATTR_OPS_INDEX_BIGNUM, 166 RAW_DATA(struct rsa_keypair, q) 167 }, 168 169 { 170 .attr_id = TEE_ATTR_RSA_EXPONENT1, 171 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 172 .ops_index = ATTR_OPS_INDEX_BIGNUM, 173 RAW_DATA(struct rsa_keypair, dp) 174 }, 175 176 { 177 .attr_id = TEE_ATTR_RSA_EXPONENT2, 178 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 179 .ops_index = ATTR_OPS_INDEX_BIGNUM, 180 RAW_DATA(struct rsa_keypair, dq) 181 }, 182 183 { 184 .attr_id = TEE_ATTR_RSA_COEFFICIENT, 185 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 186 .ops_index = ATTR_OPS_INDEX_BIGNUM, 187 RAW_DATA(struct rsa_keypair, qp) 188 }, 189 }; 190 191 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_pub_key_attrs[] = { 192 { 193 .attr_id = TEE_ATTR_DSA_PRIME, 194 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS | 195 TEE_TYPE_ATTR_SIZE_INDICATOR, 196 .ops_index = ATTR_OPS_INDEX_BIGNUM, 197 RAW_DATA(struct dsa_public_key, p) 198 }, 199 200 { 201 .attr_id = TEE_ATTR_DSA_SUBPRIME, 202 .flags = TEE_TYPE_ATTR_REQUIRED, 203 .ops_index = ATTR_OPS_INDEX_BIGNUM, 204 RAW_DATA(struct dsa_public_key, q) 205 }, 206 207 { 208 .attr_id = TEE_ATTR_DSA_BASE, 209 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS, 210 .ops_index = ATTR_OPS_INDEX_BIGNUM, 211 RAW_DATA(struct dsa_public_key, g) 212 }, 213 214 { 215 .attr_id = TEE_ATTR_DSA_PUBLIC_VALUE, 216 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS, 217 .ops_index = ATTR_OPS_INDEX_BIGNUM, 218 RAW_DATA(struct dsa_public_key, y) 219 }, 220 }; 221 222 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_keypair_attrs[] = { 223 { 224 .attr_id = TEE_ATTR_DSA_PRIME, 225 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ | 226 TEE_TYPE_ATTR_BIGNUM_MAXBITS | TEE_TYPE_ATTR_SIZE_INDICATOR, 227 .ops_index = ATTR_OPS_INDEX_BIGNUM, 228 RAW_DATA(struct dsa_keypair, p) 229 }, 230 231 { 232 .attr_id = TEE_ATTR_DSA_SUBPRIME, 233 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ, 234 .ops_index = ATTR_OPS_INDEX_BIGNUM, 235 RAW_DATA(struct dsa_keypair, q) 236 }, 237 238 { 239 .attr_id = TEE_ATTR_DSA_BASE, 240 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ | 241 TEE_TYPE_ATTR_BIGNUM_MAXBITS, 242 .ops_index = ATTR_OPS_INDEX_BIGNUM, 243 RAW_DATA(struct dsa_keypair, g) 244 }, 245 246 { 247 .attr_id = TEE_ATTR_DSA_PRIVATE_VALUE, 248 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS, 249 .ops_index = ATTR_OPS_INDEX_BIGNUM, 250 RAW_DATA(struct dsa_keypair, x) 251 }, 252 253 { 254 .attr_id = TEE_ATTR_DSA_PUBLIC_VALUE, 255 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS, 256 .ops_index = ATTR_OPS_INDEX_BIGNUM, 257 RAW_DATA(struct dsa_keypair, y) 258 }, 259 }; 260 261 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dh_keypair_attrs[] = { 262 { 263 .attr_id = TEE_ATTR_DH_PRIME, 264 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR | 265 TEE_TYPE_ATTR_GEN_KEY_REQ, 266 .ops_index = ATTR_OPS_INDEX_BIGNUM, 267 RAW_DATA(struct dh_keypair, p) 268 }, 269 270 { 271 .attr_id = TEE_ATTR_DH_BASE, 272 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ, 273 .ops_index = ATTR_OPS_INDEX_BIGNUM, 274 RAW_DATA(struct dh_keypair, g) 275 }, 276 277 { 278 .attr_id = TEE_ATTR_DH_PUBLIC_VALUE, 279 .flags = TEE_TYPE_ATTR_REQUIRED, 280 .ops_index = ATTR_OPS_INDEX_BIGNUM, 281 RAW_DATA(struct dh_keypair, y) 282 }, 283 284 { 285 .attr_id = TEE_ATTR_DH_PRIVATE_VALUE, 286 .flags = TEE_TYPE_ATTR_REQUIRED, 287 .ops_index = ATTR_OPS_INDEX_BIGNUM, 288 RAW_DATA(struct dh_keypair, x) 289 }, 290 291 { 292 .attr_id = TEE_ATTR_DH_SUBPRIME, 293 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP | TEE_TYPE_ATTR_GEN_KEY_OPT, 294 .ops_index = ATTR_OPS_INDEX_BIGNUM, 295 RAW_DATA(struct dh_keypair, q) 296 }, 297 298 { 299 .attr_id = TEE_ATTR_DH_X_BITS, 300 .flags = TEE_TYPE_ATTR_GEN_KEY_OPT, 301 .ops_index = ATTR_OPS_INDEX_VALUE, 302 RAW_DATA(struct dh_keypair, xbits) 303 }, 304 }; 305 306 #if defined(CFG_CRYPTO_HKDF) 307 static const struct tee_cryp_obj_type_attrs 308 tee_cryp_obj_hkdf_ikm_attrs[] = { 309 { 310 .attr_id = TEE_ATTR_HKDF_IKM, 311 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 312 .ops_index = ATTR_OPS_INDEX_SECRET, 313 .raw_offs = 0, 314 .raw_size = 0 315 }, 316 }; 317 #endif 318 319 #if defined(CFG_CRYPTO_CONCAT_KDF) 320 static const struct tee_cryp_obj_type_attrs 321 tee_cryp_obj_concat_kdf_z_attrs[] = { 322 { 323 .attr_id = TEE_ATTR_CONCAT_KDF_Z, 324 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 325 .ops_index = ATTR_OPS_INDEX_SECRET, 326 .raw_offs = 0, 327 .raw_size = 0 328 }, 329 }; 330 #endif 331 332 #if defined(CFG_CRYPTO_PBKDF2) 333 static const struct tee_cryp_obj_type_attrs 334 tee_cryp_obj_pbkdf2_passwd_attrs[] = { 335 { 336 .attr_id = TEE_ATTR_PBKDF2_PASSWORD, 337 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 338 .ops_index = ATTR_OPS_INDEX_SECRET, 339 .raw_offs = 0, 340 .raw_size = 0 341 }, 342 }; 343 #endif 344 345 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_pub_key_attrs[] = { 346 { 347 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 348 .flags = TEE_TYPE_ATTR_REQUIRED, 349 .ops_index = ATTR_OPS_INDEX_BIGNUM, 350 RAW_DATA(struct ecc_public_key, x) 351 }, 352 353 { 354 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 355 .flags = TEE_TYPE_ATTR_REQUIRED, 356 .ops_index = ATTR_OPS_INDEX_BIGNUM, 357 RAW_DATA(struct ecc_public_key, y) 358 }, 359 360 { 361 .attr_id = TEE_ATTR_ECC_CURVE, 362 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 363 .ops_index = ATTR_OPS_INDEX_VALUE, 364 RAW_DATA(struct ecc_public_key, curve) 365 }, 366 }; 367 368 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_keypair_attrs[] = { 369 { 370 .attr_id = TEE_ATTR_ECC_PRIVATE_VALUE, 371 .flags = TEE_TYPE_ATTR_REQUIRED, 372 .ops_index = ATTR_OPS_INDEX_BIGNUM, 373 RAW_DATA(struct ecc_keypair, d) 374 }, 375 376 { 377 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 378 .flags = TEE_TYPE_ATTR_REQUIRED, 379 .ops_index = ATTR_OPS_INDEX_BIGNUM, 380 RAW_DATA(struct ecc_keypair, x) 381 }, 382 383 { 384 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 385 .flags = TEE_TYPE_ATTR_REQUIRED, 386 .ops_index = ATTR_OPS_INDEX_BIGNUM, 387 RAW_DATA(struct ecc_keypair, y) 388 }, 389 390 { 391 .attr_id = TEE_ATTR_ECC_CURVE, 392 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR | 393 TEE_TYPE_ATTR_GEN_KEY_REQ, 394 .ops_index = ATTR_OPS_INDEX_VALUE, 395 RAW_DATA(struct ecc_keypair, curve) 396 }, 397 }; 398 399 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_sm2_pub_key_attrs[] = { 400 { 401 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 402 .flags = TEE_TYPE_ATTR_REQUIRED, 403 .ops_index = ATTR_OPS_INDEX_BIGNUM, 404 RAW_DATA(struct ecc_public_key, x) 405 }, 406 407 { 408 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 409 .flags = TEE_TYPE_ATTR_REQUIRED, 410 .ops_index = ATTR_OPS_INDEX_BIGNUM, 411 RAW_DATA(struct ecc_public_key, y) 412 }, 413 }; 414 415 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_sm2_keypair_attrs[] = { 416 { 417 .attr_id = TEE_ATTR_ECC_PRIVATE_VALUE, 418 .flags = TEE_TYPE_ATTR_REQUIRED, 419 .ops_index = ATTR_OPS_INDEX_BIGNUM, 420 RAW_DATA(struct ecc_keypair, d) 421 }, 422 423 { 424 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 425 .flags = TEE_TYPE_ATTR_REQUIRED, 426 .ops_index = ATTR_OPS_INDEX_BIGNUM, 427 RAW_DATA(struct ecc_keypair, x) 428 }, 429 430 { 431 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 432 .flags = TEE_TYPE_ATTR_REQUIRED, 433 .ops_index = ATTR_OPS_INDEX_BIGNUM, 434 RAW_DATA(struct ecc_keypair, y) 435 }, 436 }; 437 438 static 439 const struct tee_cryp_obj_type_attrs tee_cryp_obj_x25519_keypair_attrs[] = { 440 { 441 .attr_id = TEE_ATTR_X25519_PRIVATE_VALUE, 442 .flags = TEE_TYPE_ATTR_REQUIRED, 443 .ops_index = ATTR_OPS_INDEX_25519, 444 RAW_DATA(struct montgomery_keypair, priv) 445 }, 446 447 { 448 .attr_id = TEE_ATTR_X25519_PUBLIC_VALUE, 449 .flags = TEE_TYPE_ATTR_REQUIRED, 450 .ops_index = ATTR_OPS_INDEX_25519, 451 RAW_DATA(struct montgomery_keypair, pub) 452 }, 453 }; 454 455 static 456 const struct tee_cryp_obj_type_attrs tee_cryp_obj_x448_keypair_attrs[] = { 457 { 458 .attr_id = TEE_ATTR_X448_PRIVATE_VALUE, 459 .flags = TEE_TYPE_ATTR_REQUIRED, 460 .ops_index = ATTR_OPS_INDEX_448, 461 RAW_DATA(struct montgomery_keypair, priv) 462 }, 463 464 { 465 .attr_id = TEE_ATTR_X448_PUBLIC_VALUE, 466 .flags = TEE_TYPE_ATTR_REQUIRED, 467 .ops_index = ATTR_OPS_INDEX_448, 468 RAW_DATA(struct montgomery_keypair, pub) 469 }, 470 }; 471 472 static 473 const struct tee_cryp_obj_type_attrs tee_cryp_obj_ed25519_pub_key_attrs[] = { 474 { 475 .attr_id = TEE_ATTR_ED25519_PUBLIC_VALUE, 476 .flags = TEE_TYPE_ATTR_REQUIRED, 477 .ops_index = ATTR_OPS_INDEX_25519, 478 RAW_DATA(struct ed25519_public_key, pub) 479 }, 480 }; 481 482 static 483 const struct tee_cryp_obj_type_attrs tee_cryp_obj_ed25519_keypair_attrs[] = { 484 { 485 .attr_id = TEE_ATTR_ED25519_PRIVATE_VALUE, 486 .flags = TEE_TYPE_ATTR_REQUIRED, 487 .ops_index = ATTR_OPS_INDEX_25519, 488 RAW_DATA(struct ed25519_keypair, priv) 489 }, 490 491 { 492 .attr_id = TEE_ATTR_ED25519_PUBLIC_VALUE, 493 .flags = TEE_TYPE_ATTR_REQUIRED, 494 .ops_index = ATTR_OPS_INDEX_25519, 495 RAW_DATA(struct ed25519_keypair, pub) 496 }, 497 }; 498 499 struct tee_cryp_obj_type_props { 500 TEE_ObjectType obj_type; 501 uint16_t min_size; /* may not be smaller than this */ 502 uint16_t max_size; /* may not be larger than this */ 503 uint16_t alloc_size; /* this many bytes are allocated to hold data */ 504 uint8_t quanta; /* may only be an multiple of this */ 505 506 uint8_t num_type_attrs; 507 const struct tee_cryp_obj_type_attrs *type_attrs; 508 }; 509 510 #define PROP(obj_type, quanta, min_size, max_size, alloc_size, type_attrs) \ 511 { (obj_type), (min_size), (max_size), (alloc_size), (quanta), \ 512 ARRAY_SIZE(type_attrs), (type_attrs) } 513 514 static const struct tee_cryp_obj_type_props tee_cryp_obj_props[] = { 515 PROP(TEE_TYPE_AES, 64, 128, 256, /* valid sizes 128, 192, 256 */ 516 256 / 8 + sizeof(struct tee_cryp_obj_secret), 517 tee_cryp_obj_secret_value_attrs), 518 PROP(TEE_TYPE_DES, 64, 64, 64, 519 /* Valid size 64 with parity */ 520 64 / 8 + sizeof(struct tee_cryp_obj_secret), 521 tee_cryp_obj_secret_value_attrs), 522 PROP(TEE_TYPE_DES3, 64, 128, 192, 523 /* Valid sizes 128, 192 with parity */ 524 192 / 8 + sizeof(struct tee_cryp_obj_secret), 525 tee_cryp_obj_secret_value_attrs), 526 PROP(TEE_TYPE_SM4, 128, 128, 128, 527 128 / 8 + sizeof(struct tee_cryp_obj_secret), 528 tee_cryp_obj_secret_value_attrs), 529 PROP(TEE_TYPE_HMAC_MD5, 8, 64, 512, 530 512 / 8 + sizeof(struct tee_cryp_obj_secret), 531 tee_cryp_obj_secret_value_attrs), 532 #if defined(CFG_HMAC_64_1024_RANGE) 533 PROP(TEE_TYPE_HMAC_SHA1, 8, 64, 1024, 534 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 535 tee_cryp_obj_secret_value_attrs), 536 PROP(TEE_TYPE_HMAC_SHA224, 8, 64, 1024, 537 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 538 tee_cryp_obj_secret_value_attrs), 539 PROP(TEE_TYPE_HMAC_SHA256, 8, 64, 1024, 540 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 541 tee_cryp_obj_secret_value_attrs), 542 PROP(TEE_TYPE_HMAC_SHA384, 8, 64, 1024, 543 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 544 tee_cryp_obj_secret_value_attrs), 545 PROP(TEE_TYPE_HMAC_SHA512, 8, 64, 1024, 546 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 547 tee_cryp_obj_secret_value_attrs), 548 #else 549 PROP(TEE_TYPE_HMAC_SHA1, 8, 80, 512, 550 512 / 8 + sizeof(struct tee_cryp_obj_secret), 551 tee_cryp_obj_secret_value_attrs), 552 PROP(TEE_TYPE_HMAC_SHA224, 8, 112, 512, 553 512 / 8 + sizeof(struct tee_cryp_obj_secret), 554 tee_cryp_obj_secret_value_attrs), 555 PROP(TEE_TYPE_HMAC_SHA256, 8, 192, 1024, 556 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 557 tee_cryp_obj_secret_value_attrs), 558 PROP(TEE_TYPE_HMAC_SHA384, 8, 256, 1024, 559 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 560 tee_cryp_obj_secret_value_attrs), 561 PROP(TEE_TYPE_HMAC_SHA512, 8, 256, 1024, 562 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 563 tee_cryp_obj_secret_value_attrs), 564 #endif 565 PROP(TEE_TYPE_HMAC_SHA3_224, 8, 192, 1024, 566 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 567 tee_cryp_obj_secret_value_attrs), 568 PROP(TEE_TYPE_HMAC_SHA3_256, 8, 256, 1024, 569 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 570 tee_cryp_obj_secret_value_attrs), 571 PROP(TEE_TYPE_HMAC_SHA3_384, 8, 256, 1024, 572 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 573 tee_cryp_obj_secret_value_attrs), 574 PROP(TEE_TYPE_HMAC_SHA3_512, 8, 256, 1024, 575 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 576 tee_cryp_obj_secret_value_attrs), 577 PROP(TEE_TYPE_HMAC_SM3, 8, 80, 1024, 578 512 / 8 + sizeof(struct tee_cryp_obj_secret), 579 tee_cryp_obj_secret_value_attrs), 580 PROP(TEE_TYPE_GENERIC_SECRET, 8, 0, 4096, 581 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 582 tee_cryp_obj_secret_value_attrs), 583 #if defined(CFG_CRYPTO_HKDF) 584 PROP(TEE_TYPE_HKDF_IKM, 8, 0, 4096, 585 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 586 tee_cryp_obj_hkdf_ikm_attrs), 587 #endif 588 #if defined(CFG_CRYPTO_CONCAT_KDF) 589 PROP(TEE_TYPE_CONCAT_KDF_Z, 8, 0, 4096, 590 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 591 tee_cryp_obj_concat_kdf_z_attrs), 592 #endif 593 #if defined(CFG_CRYPTO_PBKDF2) 594 PROP(TEE_TYPE_PBKDF2_PASSWORD, 8, 0, 4096, 595 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 596 tee_cryp_obj_pbkdf2_passwd_attrs), 597 #endif 598 PROP(TEE_TYPE_RSA_PUBLIC_KEY, 1, 256, CFG_CORE_BIGNUM_MAX_BITS, 599 sizeof(struct rsa_public_key), 600 tee_cryp_obj_rsa_pub_key_attrs), 601 602 PROP(TEE_TYPE_RSA_KEYPAIR, 1, 256, CFG_CORE_BIGNUM_MAX_BITS, 603 sizeof(struct rsa_keypair), 604 tee_cryp_obj_rsa_keypair_attrs), 605 606 PROP(TEE_TYPE_DSA_PUBLIC_KEY, 64, 512, 3072, 607 sizeof(struct dsa_public_key), 608 tee_cryp_obj_dsa_pub_key_attrs), 609 610 PROP(TEE_TYPE_DSA_KEYPAIR, 64, 512, 3072, 611 sizeof(struct dsa_keypair), 612 tee_cryp_obj_dsa_keypair_attrs), 613 614 PROP(TEE_TYPE_DH_KEYPAIR, 1, 256, 2048, 615 sizeof(struct dh_keypair), 616 tee_cryp_obj_dh_keypair_attrs), 617 618 PROP(TEE_TYPE_ECDSA_PUBLIC_KEY, 1, 192, 521, 619 sizeof(struct ecc_public_key), 620 tee_cryp_obj_ecc_pub_key_attrs), 621 622 PROP(TEE_TYPE_ECDSA_KEYPAIR, 1, 192, 521, 623 sizeof(struct ecc_keypair), 624 tee_cryp_obj_ecc_keypair_attrs), 625 626 PROP(TEE_TYPE_ECDH_PUBLIC_KEY, 1, 192, 521, 627 sizeof(struct ecc_public_key), 628 tee_cryp_obj_ecc_pub_key_attrs), 629 630 PROP(TEE_TYPE_ECDH_KEYPAIR, 1, 192, 521, 631 sizeof(struct ecc_keypair), 632 tee_cryp_obj_ecc_keypair_attrs), 633 634 PROP(TEE_TYPE_SM2_DSA_PUBLIC_KEY, 1, 256, 256, 635 sizeof(struct ecc_public_key), 636 tee_cryp_obj_sm2_pub_key_attrs), 637 638 PROP(TEE_TYPE_SM2_DSA_KEYPAIR, 1, 256, 256, 639 sizeof(struct ecc_keypair), 640 tee_cryp_obj_sm2_keypair_attrs), 641 642 PROP(TEE_TYPE_SM2_PKE_PUBLIC_KEY, 1, 256, 256, 643 sizeof(struct ecc_public_key), 644 tee_cryp_obj_sm2_pub_key_attrs), 645 646 PROP(TEE_TYPE_SM2_PKE_KEYPAIR, 1, 256, 256, 647 sizeof(struct ecc_keypair), 648 tee_cryp_obj_sm2_keypair_attrs), 649 650 PROP(TEE_TYPE_SM2_KEP_PUBLIC_KEY, 1, 256, 256, 651 sizeof(struct ecc_public_key), 652 tee_cryp_obj_sm2_pub_key_attrs), 653 654 PROP(TEE_TYPE_SM2_KEP_KEYPAIR, 1, 256, 256, 655 sizeof(struct ecc_keypair), 656 tee_cryp_obj_sm2_keypair_attrs), 657 658 PROP(TEE_TYPE_X25519_KEYPAIR, 1, 256, 256, 659 sizeof(struct montgomery_keypair), 660 tee_cryp_obj_x25519_keypair_attrs), 661 662 PROP(TEE_TYPE_X448_KEYPAIR, 1, 448, 448, 663 sizeof(struct montgomery_keypair), 664 tee_cryp_obj_x448_keypair_attrs), 665 666 PROP(TEE_TYPE_ED25519_PUBLIC_KEY, 1, 256, 256, 667 sizeof(struct ed25519_public_key), 668 tee_cryp_obj_ed25519_pub_key_attrs), 669 670 PROP(TEE_TYPE_ED25519_KEYPAIR, 1, 256, 256, 671 sizeof(struct ed25519_keypair), 672 tee_cryp_obj_ed25519_keypair_attrs), 673 }; 674 675 struct attr_ops { 676 TEE_Result (*from_user)(void *attr, const void *buffer, size_t size); 677 TEE_Result (*to_user)(void *attr, struct ts_session *sess, 678 void *buffer, uint64_t *size); 679 TEE_Result (*to_binary)(void *attr, void *data, size_t data_len, 680 size_t *offs); 681 bool (*from_binary)(void *attr, const void *data, size_t data_len, 682 size_t *offs); 683 TEE_Result (*from_obj)(void *attr, void *src_attr); 684 void (*free)(void *attr); 685 void (*clear)(void *attr); 686 }; 687 688 static TEE_Result op_u32_to_binary_helper(uint32_t v, uint8_t *data, 689 size_t data_len, size_t *offs) 690 { 691 uint32_t field; 692 size_t next_offs; 693 694 if (ADD_OVERFLOW(*offs, sizeof(field), &next_offs)) 695 return TEE_ERROR_OVERFLOW; 696 697 if (data && next_offs <= data_len) { 698 field = TEE_U32_TO_BIG_ENDIAN(v); 699 memcpy(data + *offs, &field, sizeof(field)); 700 } 701 (*offs) = next_offs; 702 703 return TEE_SUCCESS; 704 } 705 706 static bool op_u32_from_binary_helper(uint32_t *v, const uint8_t *data, 707 size_t data_len, size_t *offs) 708 { 709 uint32_t field; 710 711 if (!data || (*offs + sizeof(field)) > data_len) 712 return false; 713 714 memcpy(&field, data + *offs, sizeof(field)); 715 *v = TEE_U32_FROM_BIG_ENDIAN(field); 716 (*offs) += sizeof(field); 717 return true; 718 } 719 720 static TEE_Result op_attr_secret_value_from_user(void *attr, const void *buffer, 721 size_t size) 722 { 723 TEE_Result res = TEE_SUCCESS; 724 struct tee_cryp_obj_secret *key = attr; 725 726 /* Data size has to fit in allocated buffer */ 727 if (size > key->alloc_size) 728 return TEE_ERROR_SECURITY; 729 res = copy_from_user(key + 1, buffer, size); 730 if (!res) 731 key->key_size = size; 732 733 return res; 734 } 735 736 static TEE_Result op_attr_secret_value_to_user(void *attr, 737 struct ts_session *sess __unused, 738 void *buffer, uint64_t *size) 739 { 740 TEE_Result res; 741 struct tee_cryp_obj_secret *key = attr; 742 uint64_t s; 743 uint64_t key_size; 744 745 res = copy_from_user(&s, size, sizeof(s)); 746 if (res != TEE_SUCCESS) 747 return res; 748 749 key_size = key->key_size; 750 res = copy_to_user(size, &key_size, sizeof(key_size)); 751 if (res != TEE_SUCCESS) 752 return res; 753 754 if (s < key->key_size || !buffer) 755 return TEE_ERROR_SHORT_BUFFER; 756 757 return copy_to_user(buffer, key + 1, key->key_size); 758 } 759 760 static TEE_Result op_attr_secret_value_to_binary(void *attr, void *data, 761 size_t data_len, size_t *offs) 762 { 763 TEE_Result res; 764 struct tee_cryp_obj_secret *key = attr; 765 size_t next_offs; 766 767 res = op_u32_to_binary_helper(key->key_size, data, data_len, offs); 768 if (res != TEE_SUCCESS) 769 return res; 770 771 if (ADD_OVERFLOW(*offs, key->key_size, &next_offs)) 772 return TEE_ERROR_OVERFLOW; 773 774 if (data && next_offs <= data_len) 775 memcpy((uint8_t *)data + *offs, key + 1, key->key_size); 776 (*offs) = next_offs; 777 778 return TEE_SUCCESS; 779 } 780 781 static bool op_attr_secret_value_from_binary(void *attr, const void *data, 782 size_t data_len, size_t *offs) 783 { 784 struct tee_cryp_obj_secret *key = attr; 785 uint32_t s; 786 787 if (!op_u32_from_binary_helper(&s, data, data_len, offs)) 788 return false; 789 790 if ((*offs + s) > data_len) 791 return false; 792 793 /* Data size has to fit in allocated buffer */ 794 if (s > key->alloc_size) 795 return false; 796 key->key_size = s; 797 memcpy(key + 1, (const uint8_t *)data + *offs, s); 798 (*offs) += s; 799 return true; 800 } 801 802 803 static TEE_Result op_attr_secret_value_from_obj(void *attr, void *src_attr) 804 { 805 struct tee_cryp_obj_secret *key = attr; 806 struct tee_cryp_obj_secret *src_key = src_attr; 807 808 if (src_key->key_size > key->alloc_size) 809 return TEE_ERROR_BAD_STATE; 810 memcpy(key + 1, src_key + 1, src_key->key_size); 811 key->key_size = src_key->key_size; 812 return TEE_SUCCESS; 813 } 814 815 static void op_attr_secret_value_clear(void *attr) 816 { 817 struct tee_cryp_obj_secret *key = attr; 818 819 key->key_size = 0; 820 memzero_explicit(key + 1, key->alloc_size); 821 } 822 823 static TEE_Result op_attr_bignum_from_user(void *attr, const void *buffer, 824 size_t size) 825 { 826 TEE_Result res = TEE_SUCCESS; 827 struct bignum **bn = attr; 828 void *bbuf = NULL; 829 830 res = bb_memdup_user(buffer, size, &bbuf); 831 if (res) 832 return res; 833 834 res = crypto_bignum_bin2bn(bbuf, size, *bn); 835 836 bb_free(bbuf, size); 837 838 return res; 839 } 840 841 static TEE_Result op_attr_bignum_to_user(void *attr, 842 struct ts_session *sess __unused, 843 void *buffer, uint64_t *size) 844 { 845 TEE_Result res = TEE_SUCCESS; 846 struct bignum **bn = attr; 847 uint64_t req_size = 0; 848 uint64_t s = 0; 849 void *bbuf = NULL; 850 851 res = copy_from_user(&s, size, sizeof(s)); 852 if (res != TEE_SUCCESS) 853 return res; 854 855 req_size = crypto_bignum_num_bytes(*bn); 856 res = copy_to_user(size, &req_size, sizeof(req_size)); 857 if (res != TEE_SUCCESS) 858 return res; 859 if (!req_size) 860 return TEE_SUCCESS; 861 if (s < req_size || !buffer) 862 return TEE_ERROR_SHORT_BUFFER; 863 864 bbuf = bb_alloc(req_size); 865 if (!bbuf) 866 return TEE_ERROR_OUT_OF_MEMORY; 867 868 /* 869 * Write the bignum (wich raw data points to) into an array of 870 * bytes (stored in buffer) 871 */ 872 crypto_bignum_bn2bin(*bn, bbuf); 873 res = copy_to_user(buffer, bbuf, req_size); 874 875 bb_free(bbuf, req_size); 876 return res; 877 } 878 879 static TEE_Result op_attr_bignum_to_binary(void *attr, void *data, 880 size_t data_len, size_t *offs) 881 { 882 TEE_Result res; 883 struct bignum **bn = attr; 884 uint32_t n = crypto_bignum_num_bytes(*bn); 885 size_t next_offs; 886 887 res = op_u32_to_binary_helper(n, data, data_len, offs); 888 if (res != TEE_SUCCESS) 889 return res; 890 891 if (ADD_OVERFLOW(*offs, n, &next_offs)) 892 return TEE_ERROR_OVERFLOW; 893 894 if (data && next_offs <= data_len) 895 crypto_bignum_bn2bin(*bn, (uint8_t *)data + *offs); 896 (*offs) = next_offs; 897 898 return TEE_SUCCESS; 899 } 900 901 static bool op_attr_bignum_from_binary(void *attr, const void *data, 902 size_t data_len, size_t *offs) 903 { 904 struct bignum **bn = attr; 905 uint32_t n; 906 907 if (!op_u32_from_binary_helper(&n, data, data_len, offs)) 908 return false; 909 910 if ((*offs + n) > data_len) 911 return false; 912 if (crypto_bignum_bin2bn((const uint8_t *)data + *offs, n, *bn)) 913 return false; 914 (*offs) += n; 915 return true; 916 } 917 918 static TEE_Result op_attr_bignum_from_obj(void *attr, void *src_attr) 919 { 920 struct bignum **bn = attr; 921 struct bignum **src_bn = src_attr; 922 923 crypto_bignum_copy(*bn, *src_bn); 924 return TEE_SUCCESS; 925 } 926 927 static void op_attr_bignum_clear(void *attr) 928 { 929 struct bignum **bn = attr; 930 931 crypto_bignum_clear(*bn); 932 } 933 934 static void op_attr_bignum_free(void *attr) 935 { 936 struct bignum **bn = attr; 937 938 crypto_bignum_free(bn); 939 } 940 941 static TEE_Result op_attr_value_from_user(void *attr, const void *buffer, 942 size_t size) 943 { 944 uint32_t *v = attr; 945 946 if (size != sizeof(uint32_t) * 2) 947 return TEE_ERROR_GENERIC; /* "can't happen */ 948 949 /* Note that only the first value is copied */ 950 memcpy(v, buffer, sizeof(uint32_t)); 951 return TEE_SUCCESS; 952 } 953 954 static TEE_Result op_attr_value_to_user(void *attr, 955 struct ts_session *sess __unused, 956 void *buffer, uint64_t *size) 957 { 958 TEE_Result res; 959 uint32_t *v = attr; 960 uint64_t s; 961 uint32_t value[2] = { *v }; 962 uint64_t req_size = sizeof(value); 963 964 res = copy_from_user(&s, size, sizeof(s)); 965 if (res != TEE_SUCCESS) 966 return res; 967 968 if (s < req_size || !buffer) 969 return TEE_ERROR_SHORT_BUFFER; 970 971 return copy_to_user(buffer, value, req_size); 972 } 973 974 static TEE_Result op_attr_value_to_binary(void *attr, void *data, 975 size_t data_len, size_t *offs) 976 { 977 uint32_t *v = attr; 978 979 return op_u32_to_binary_helper(*v, data, data_len, offs); 980 } 981 982 static bool op_attr_value_from_binary(void *attr, const void *data, 983 size_t data_len, size_t *offs) 984 { 985 uint32_t *v = attr; 986 987 return op_u32_from_binary_helper(v, data, data_len, offs); 988 } 989 990 static TEE_Result op_attr_value_from_obj(void *attr, void *src_attr) 991 { 992 uint32_t *v = attr; 993 uint32_t *src_v = src_attr; 994 995 *v = *src_v; 996 return TEE_SUCCESS; 997 } 998 999 static void op_attr_value_clear(void *attr) 1000 { 1001 uint32_t *v = attr; 1002 1003 *v = 0; 1004 } 1005 1006 static TEE_Result op_attr_25519_from_user(void *attr, const void *buffer, 1007 size_t size) 1008 { 1009 uint8_t **key = attr; 1010 1011 if (size != KEY_SIZE_BYTES_25519 || !*key) 1012 return TEE_ERROR_SECURITY; 1013 1014 return copy_from_user(*key, buffer, size); 1015 } 1016 1017 static TEE_Result op_attr_25519_to_user(void *attr, 1018 struct ts_session *sess __unused, 1019 void *buffer, uint64_t *size) 1020 { 1021 TEE_Result res = TEE_ERROR_GENERIC; 1022 uint8_t **key = attr; 1023 uint64_t s = 0; 1024 uint64_t key_size = (uint64_t)KEY_SIZE_BYTES_25519; 1025 1026 res = copy_from_user(&s, size, sizeof(s)); 1027 if (res != TEE_SUCCESS) 1028 return res; 1029 1030 res = copy_to_user(size, &key_size, sizeof(key_size)); 1031 if (res != TEE_SUCCESS) 1032 return res; 1033 1034 if (s < key_size || !buffer) 1035 return TEE_ERROR_SHORT_BUFFER; 1036 1037 return copy_to_user(buffer, *key, key_size); 1038 } 1039 1040 static TEE_Result op_attr_25519_to_binary(void *attr, void *data, 1041 size_t data_len, size_t *offs) 1042 { 1043 TEE_Result res = TEE_ERROR_GENERIC; 1044 uint8_t **key = attr; 1045 size_t next_offs = 0; 1046 uint64_t key_size = (uint64_t)KEY_SIZE_BYTES_25519; 1047 1048 res = op_u32_to_binary_helper(key_size, data, data_len, offs); 1049 if (res != TEE_SUCCESS) 1050 return res; 1051 1052 if (ADD_OVERFLOW(*offs, key_size, &next_offs)) 1053 return TEE_ERROR_OVERFLOW; 1054 1055 if (data && next_offs <= data_len) 1056 memcpy((uint8_t *)data + *offs, *key, key_size); 1057 *offs = next_offs; 1058 1059 return TEE_SUCCESS; 1060 } 1061 1062 static bool op_attr_25519_from_binary(void *attr, const void *data, 1063 size_t data_len, size_t *offs) 1064 { 1065 uint8_t **key = attr; 1066 uint32_t s = 0; 1067 1068 if (!op_u32_from_binary_helper(&s, data, data_len, offs)) 1069 return false; 1070 1071 if (*offs + s > data_len) 1072 return false; 1073 1074 if (s > (uint32_t)KEY_SIZE_BYTES_25519) 1075 return false; 1076 1077 memcpy(*key, (const uint8_t *)data + *offs, s); 1078 *offs += s; 1079 return true; 1080 } 1081 1082 static TEE_Result op_attr_25519_from_obj(void *attr, void *src_attr) 1083 { 1084 uint8_t **key = attr; 1085 uint8_t **src_key = src_attr; 1086 1087 if (!*key || !*src_key) 1088 return TEE_ERROR_SECURITY; 1089 1090 memcpy(*key, *src_key, KEY_SIZE_BYTES_25519); 1091 1092 return TEE_SUCCESS; 1093 } 1094 1095 static void op_attr_25519_clear(void *attr) 1096 { 1097 uint8_t **key = attr; 1098 1099 assert(*key); 1100 1101 memzero_explicit(*key, KEY_SIZE_BYTES_25519); 1102 } 1103 1104 static void op_attr_25519_free(void *attr) 1105 { 1106 uint8_t **key = attr; 1107 1108 op_attr_25519_clear(attr); 1109 free(*key); 1110 } 1111 1112 static const struct attr_ops attr_ops[] = { 1113 [ATTR_OPS_INDEX_SECRET] = { 1114 .from_user = op_attr_secret_value_from_user, 1115 .to_user = op_attr_secret_value_to_user, 1116 .to_binary = op_attr_secret_value_to_binary, 1117 .from_binary = op_attr_secret_value_from_binary, 1118 .from_obj = op_attr_secret_value_from_obj, 1119 .free = op_attr_secret_value_clear, /* not a typo */ 1120 .clear = op_attr_secret_value_clear, 1121 }, 1122 [ATTR_OPS_INDEX_BIGNUM] = { 1123 .from_user = op_attr_bignum_from_user, 1124 .to_user = op_attr_bignum_to_user, 1125 .to_binary = op_attr_bignum_to_binary, 1126 .from_binary = op_attr_bignum_from_binary, 1127 .from_obj = op_attr_bignum_from_obj, 1128 .free = op_attr_bignum_free, 1129 .clear = op_attr_bignum_clear, 1130 }, 1131 [ATTR_OPS_INDEX_VALUE] = { 1132 .from_user = op_attr_value_from_user, 1133 .to_user = op_attr_value_to_user, 1134 .to_binary = op_attr_value_to_binary, 1135 .from_binary = op_attr_value_from_binary, 1136 .from_obj = op_attr_value_from_obj, 1137 .free = op_attr_value_clear, /* not a typo */ 1138 .clear = op_attr_value_clear, 1139 }, 1140 [ATTR_OPS_INDEX_25519] = { 1141 .from_user = op_attr_25519_from_user, 1142 .to_user = op_attr_25519_to_user, 1143 .to_binary = op_attr_25519_to_binary, 1144 .from_binary = op_attr_25519_from_binary, 1145 .from_obj = op_attr_25519_from_obj, 1146 .free = op_attr_25519_free, 1147 .clear = op_attr_25519_clear, 1148 }, 1149 }; 1150 1151 static TEE_Result get_user_u64_as_size_t(size_t *dst, uint64_t *src) 1152 { 1153 uint64_t d = 0; 1154 TEE_Result res = copy_from_user(&d, src, sizeof(d)); 1155 1156 /* 1157 * On 32-bit systems a size_t can't hold a uint64_t so we need to 1158 * check that the value isn't too large. 1159 */ 1160 if (!res && ADD_OVERFLOW(0, d, dst)) 1161 return TEE_ERROR_OVERFLOW; 1162 1163 return res; 1164 } 1165 1166 static TEE_Result put_user_u64(uint64_t *dst, size_t value) 1167 { 1168 uint64_t v = value; 1169 1170 return copy_to_user(dst, &v, sizeof(v)); 1171 } 1172 1173 TEE_Result syscall_cryp_obj_get_info(unsigned long obj, 1174 struct utee_object_info *info) 1175 { 1176 struct ts_session *sess = ts_get_current_session(); 1177 struct utee_object_info o_info = { }; 1178 TEE_Result res = TEE_SUCCESS; 1179 struct tee_obj *o = NULL; 1180 1181 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1182 uref_to_vaddr(obj), &o); 1183 if (res != TEE_SUCCESS) 1184 goto exit; 1185 1186 o_info.obj_type = o->info.objectType; 1187 o_info.obj_size = o->info.objectSize; 1188 o_info.max_obj_size = o->info.maxObjectSize; 1189 if (o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) { 1190 tee_pobj_lock_usage(o->pobj); 1191 o_info.obj_usage = o->pobj->obj_info_usage; 1192 tee_pobj_unlock_usage(o->pobj); 1193 } else { 1194 o_info.obj_usage = o->info.objectUsage; 1195 } 1196 o_info.data_size = o->info.dataSize; 1197 o_info.data_pos = o->info.dataPosition; 1198 o_info.handle_flags = o->info.handleFlags; 1199 res = copy_to_user_private(info, &o_info, sizeof(o_info)); 1200 1201 exit: 1202 return res; 1203 } 1204 1205 TEE_Result syscall_cryp_obj_restrict_usage(unsigned long obj, 1206 unsigned long usage) 1207 { 1208 struct ts_session *sess = ts_get_current_session(); 1209 TEE_Result res = TEE_SUCCESS; 1210 struct tee_obj *o = NULL; 1211 1212 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1213 if (res) 1214 return res; 1215 1216 if (o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) { 1217 uint32_t new_usage = 0; 1218 1219 tee_pobj_lock_usage(o->pobj); 1220 new_usage = o->pobj->obj_info_usage & usage; 1221 res = tee_svc_storage_write_usage(o, new_usage); 1222 if (!res) 1223 o->pobj->obj_info_usage = new_usage; 1224 tee_pobj_unlock_usage(o->pobj); 1225 } else { 1226 o->info.objectUsage &= usage; 1227 } 1228 1229 return res; 1230 } 1231 1232 static int tee_svc_cryp_obj_find_type_attr_idx( 1233 uint32_t attr_id, 1234 const struct tee_cryp_obj_type_props *type_props) 1235 { 1236 size_t n; 1237 1238 for (n = 0; n < type_props->num_type_attrs; n++) { 1239 if (attr_id == type_props->type_attrs[n].attr_id) 1240 return n; 1241 } 1242 return -1; 1243 } 1244 1245 static const struct tee_cryp_obj_type_props *tee_svc_find_type_props( 1246 TEE_ObjectType obj_type) 1247 { 1248 size_t n; 1249 1250 for (n = 0; n < ARRAY_SIZE(tee_cryp_obj_props); n++) { 1251 if (tee_cryp_obj_props[n].obj_type == obj_type) 1252 return tee_cryp_obj_props + n; 1253 } 1254 1255 return NULL; 1256 } 1257 1258 /* Set an attribute on an object */ 1259 static void set_attribute(struct tee_obj *o, 1260 const struct tee_cryp_obj_type_props *props, 1261 uint32_t attr) 1262 { 1263 int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props); 1264 1265 if (idx < 0) 1266 return; 1267 o->have_attrs |= BIT(idx); 1268 } 1269 1270 /* Get an attribute on an object */ 1271 static uint32_t get_attribute(const struct tee_obj *o, 1272 const struct tee_cryp_obj_type_props *props, 1273 uint32_t attr) 1274 { 1275 int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props); 1276 1277 if (idx < 0) 1278 return 0; 1279 return o->have_attrs & BIT(idx); 1280 } 1281 1282 TEE_Result syscall_cryp_obj_get_attr(unsigned long obj, unsigned long attr_id, 1283 void *buffer, uint64_t *size) 1284 { 1285 struct ts_session *sess = ts_get_current_session(); 1286 TEE_Result res = TEE_SUCCESS; 1287 struct tee_obj *o = NULL; 1288 const struct tee_cryp_obj_type_props *type_props = NULL; 1289 int idx = 0; 1290 const struct attr_ops *ops = NULL; 1291 void *attr = NULL; 1292 uint32_t obj_usage = 0; 1293 1294 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1295 if (res != TEE_SUCCESS) 1296 return TEE_ERROR_ITEM_NOT_FOUND; 1297 1298 /* Check that the object is initialized */ 1299 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 1300 return TEE_ERROR_BAD_PARAMETERS; 1301 1302 /* Check that getting the attribute is allowed */ 1303 if (!(attr_id & TEE_ATTR_FLAG_PUBLIC)) { 1304 if (o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) { 1305 tee_pobj_lock_usage(o->pobj); 1306 obj_usage = o->pobj->obj_info_usage; 1307 tee_pobj_unlock_usage(o->pobj); 1308 } else { 1309 obj_usage = o->info.objectUsage; 1310 } 1311 if (!(obj_usage & TEE_USAGE_EXTRACTABLE)) 1312 return TEE_ERROR_BAD_PARAMETERS; 1313 } 1314 1315 type_props = tee_svc_find_type_props(o->info.objectType); 1316 if (!type_props) { 1317 /* Unknown object type, "can't happen" */ 1318 return TEE_ERROR_BAD_STATE; 1319 } 1320 1321 idx = tee_svc_cryp_obj_find_type_attr_idx(attr_id, type_props); 1322 if ((idx < 0) || ((o->have_attrs & (1 << idx)) == 0)) 1323 return TEE_ERROR_ITEM_NOT_FOUND; 1324 1325 ops = attr_ops + type_props->type_attrs[idx].ops_index; 1326 attr = (uint8_t *)o->attr + type_props->type_attrs[idx].raw_offs; 1327 return ops->to_user(attr, sess, buffer, size); 1328 } 1329 1330 void tee_obj_attr_free(struct tee_obj *o) 1331 { 1332 const struct tee_cryp_obj_type_props *tp; 1333 size_t n; 1334 1335 if (!o->attr) 1336 return; 1337 tp = tee_svc_find_type_props(o->info.objectType); 1338 if (!tp) 1339 return; 1340 1341 for (n = 0; n < tp->num_type_attrs; n++) { 1342 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1343 1344 attr_ops[ta->ops_index].free((uint8_t *)o->attr + ta->raw_offs); 1345 } 1346 } 1347 1348 void tee_obj_attr_clear(struct tee_obj *o) 1349 { 1350 const struct tee_cryp_obj_type_props *tp; 1351 size_t n; 1352 1353 if (!o->attr) 1354 return; 1355 tp = tee_svc_find_type_props(o->info.objectType); 1356 if (!tp) 1357 return; 1358 1359 for (n = 0; n < tp->num_type_attrs; n++) { 1360 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1361 1362 attr_ops[ta->ops_index].clear((uint8_t *)o->attr + 1363 ta->raw_offs); 1364 } 1365 } 1366 1367 TEE_Result tee_obj_attr_to_binary(struct tee_obj *o, void *data, 1368 size_t *data_len) 1369 { 1370 const struct tee_cryp_obj_type_props *tp; 1371 size_t n; 1372 size_t offs = 0; 1373 size_t len = data ? *data_len : 0; 1374 TEE_Result res; 1375 1376 if (o->info.objectType == TEE_TYPE_DATA) { 1377 *data_len = 0; 1378 return TEE_SUCCESS; /* pure data object */ 1379 } 1380 if (!o->attr) 1381 return TEE_ERROR_BAD_STATE; 1382 tp = tee_svc_find_type_props(o->info.objectType); 1383 if (!tp) 1384 return TEE_ERROR_BAD_STATE; 1385 1386 for (n = 0; n < tp->num_type_attrs; n++) { 1387 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1388 void *attr = (uint8_t *)o->attr + ta->raw_offs; 1389 1390 res = attr_ops[ta->ops_index].to_binary(attr, data, len, &offs); 1391 if (res != TEE_SUCCESS) 1392 return res; 1393 } 1394 1395 *data_len = offs; 1396 if (data && offs > len) 1397 return TEE_ERROR_SHORT_BUFFER; 1398 return TEE_SUCCESS; 1399 } 1400 1401 TEE_Result tee_obj_attr_from_binary(struct tee_obj *o, const void *data, 1402 size_t data_len) 1403 { 1404 const struct tee_cryp_obj_type_props *tp; 1405 size_t n; 1406 size_t offs = 0; 1407 1408 if (o->info.objectType == TEE_TYPE_DATA) 1409 return TEE_SUCCESS; /* pure data object */ 1410 if (!o->attr) 1411 return TEE_ERROR_BAD_STATE; 1412 tp = tee_svc_find_type_props(o->info.objectType); 1413 if (!tp) 1414 return TEE_ERROR_BAD_STATE; 1415 1416 for (n = 0; n < tp->num_type_attrs; n++) { 1417 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1418 void *attr = (uint8_t *)o->attr + ta->raw_offs; 1419 1420 if (!attr_ops[ta->ops_index].from_binary(attr, data, data_len, 1421 &offs)) 1422 return TEE_ERROR_CORRUPT_OBJECT; 1423 } 1424 return TEE_SUCCESS; 1425 } 1426 1427 TEE_Result tee_obj_attr_copy_from(struct tee_obj *o, const struct tee_obj *src) 1428 { 1429 TEE_Result res; 1430 const struct tee_cryp_obj_type_props *tp; 1431 const struct tee_cryp_obj_type_attrs *ta; 1432 size_t n; 1433 uint32_t have_attrs = 0; 1434 void *attr; 1435 void *src_attr; 1436 1437 if (o->info.objectType == TEE_TYPE_DATA) 1438 return TEE_SUCCESS; /* pure data object */ 1439 if (!o->attr) 1440 return TEE_ERROR_BAD_STATE; 1441 tp = tee_svc_find_type_props(o->info.objectType); 1442 if (!tp) 1443 return TEE_ERROR_BAD_STATE; 1444 1445 if (o->info.objectType == src->info.objectType) { 1446 have_attrs = src->have_attrs; 1447 for (n = 0; n < tp->num_type_attrs; n++) { 1448 ta = tp->type_attrs + n; 1449 attr = (uint8_t *)o->attr + ta->raw_offs; 1450 src_attr = (uint8_t *)src->attr + ta->raw_offs; 1451 res = attr_ops[ta->ops_index].from_obj(attr, src_attr); 1452 if (res != TEE_SUCCESS) 1453 return res; 1454 } 1455 } else { 1456 const struct tee_cryp_obj_type_props *tp_src; 1457 int idx; 1458 1459 if (o->info.objectType == TEE_TYPE_RSA_PUBLIC_KEY) { 1460 if (src->info.objectType != TEE_TYPE_RSA_KEYPAIR) 1461 return TEE_ERROR_BAD_PARAMETERS; 1462 } else if (o->info.objectType == TEE_TYPE_DSA_PUBLIC_KEY) { 1463 if (src->info.objectType != TEE_TYPE_DSA_KEYPAIR) 1464 return TEE_ERROR_BAD_PARAMETERS; 1465 } else if (o->info.objectType == TEE_TYPE_ECDSA_PUBLIC_KEY) { 1466 if (src->info.objectType != TEE_TYPE_ECDSA_KEYPAIR) 1467 return TEE_ERROR_BAD_PARAMETERS; 1468 } else if (o->info.objectType == TEE_TYPE_ECDH_PUBLIC_KEY) { 1469 if (src->info.objectType != TEE_TYPE_ECDH_KEYPAIR) 1470 return TEE_ERROR_BAD_PARAMETERS; 1471 } else if (o->info.objectType == TEE_TYPE_SM2_DSA_PUBLIC_KEY) { 1472 if (src->info.objectType != TEE_TYPE_SM2_DSA_KEYPAIR) 1473 return TEE_ERROR_BAD_PARAMETERS; 1474 } else if (o->info.objectType == TEE_TYPE_SM2_PKE_PUBLIC_KEY) { 1475 if (src->info.objectType != TEE_TYPE_SM2_PKE_KEYPAIR) 1476 return TEE_ERROR_BAD_PARAMETERS; 1477 } else if (o->info.objectType == TEE_TYPE_SM2_KEP_PUBLIC_KEY) { 1478 if (src->info.objectType != TEE_TYPE_SM2_KEP_KEYPAIR) 1479 return TEE_ERROR_BAD_PARAMETERS; 1480 } else if (o->info.objectType == TEE_TYPE_ED25519_PUBLIC_KEY) { 1481 if (src->info.objectType != TEE_TYPE_ED25519_KEYPAIR) 1482 return TEE_ERROR_BAD_PARAMETERS; 1483 } else if (o->info.objectType == TEE_TYPE_X25519_PUBLIC_KEY) { 1484 if (src->info.objectType != TEE_TYPE_X25519_KEYPAIR) 1485 return TEE_ERROR_BAD_PARAMETERS; 1486 } else if (o->info.objectType == TEE_TYPE_X448_PUBLIC_KEY) { 1487 if (src->info.objectType != TEE_TYPE_X448_KEYPAIR) 1488 return TEE_ERROR_BAD_PARAMETERS; 1489 } else { 1490 return TEE_ERROR_BAD_PARAMETERS; 1491 } 1492 1493 tp_src = tee_svc_find_type_props(src->info.objectType); 1494 if (!tp_src) 1495 return TEE_ERROR_BAD_STATE; 1496 1497 have_attrs = BIT32(tp->num_type_attrs) - 1; 1498 for (n = 0; n < tp->num_type_attrs; n++) { 1499 ta = tp->type_attrs + n; 1500 1501 idx = tee_svc_cryp_obj_find_type_attr_idx(ta->attr_id, 1502 tp_src); 1503 if (idx < 0) 1504 return TEE_ERROR_BAD_STATE; 1505 1506 attr = (uint8_t *)o->attr + ta->raw_offs; 1507 src_attr = (uint8_t *)src->attr + 1508 tp_src->type_attrs[idx].raw_offs; 1509 res = attr_ops[ta->ops_index].from_obj(attr, src_attr); 1510 if (res != TEE_SUCCESS) 1511 return res; 1512 } 1513 } 1514 1515 o->have_attrs = have_attrs; 1516 return TEE_SUCCESS; 1517 } 1518 1519 static bool is_gp_legacy_des_key_size(TEE_ObjectType type, size_t sz) 1520 { 1521 return IS_ENABLED(CFG_COMPAT_GP10_DES) && 1522 ((type == TEE_TYPE_DES && sz == 56) || 1523 (type == TEE_TYPE_DES3 && (sz == 112 || sz == 168))); 1524 } 1525 1526 static TEE_Result check_key_size(const struct tee_cryp_obj_type_props *props, 1527 size_t key_size) 1528 { 1529 size_t sz = key_size; 1530 1531 /* 1532 * In GP Internal API Specification 1.0 the partity bits aren't 1533 * counted when telling the size of the key in bits so add them 1534 * here if missing. 1535 */ 1536 if (is_gp_legacy_des_key_size(props->obj_type, sz)) 1537 sz += sz / 7; 1538 1539 if (sz % props->quanta != 0) 1540 return TEE_ERROR_NOT_SUPPORTED; 1541 if (sz < props->min_size) 1542 return TEE_ERROR_NOT_SUPPORTED; 1543 if (sz > props->max_size) 1544 return TEE_ERROR_NOT_SUPPORTED; 1545 1546 return TEE_SUCCESS; 1547 } 1548 1549 TEE_Result tee_obj_set_type(struct tee_obj *o, uint32_t obj_type, 1550 size_t max_key_size) 1551 { 1552 TEE_Result res = TEE_SUCCESS; 1553 const struct tee_cryp_obj_type_props *type_props; 1554 1555 /* Can only set type for newly allocated objs */ 1556 if (o->attr) 1557 return TEE_ERROR_BAD_STATE; 1558 1559 /* 1560 * Verify that maxObjectSize is supported and find out how 1561 * much should be allocated. 1562 */ 1563 1564 if (obj_type == TEE_TYPE_DATA) { 1565 if (max_key_size) 1566 return TEE_ERROR_NOT_SUPPORTED; 1567 } else { 1568 /* Find description of object */ 1569 type_props = tee_svc_find_type_props(obj_type); 1570 if (!type_props) 1571 return TEE_ERROR_NOT_SUPPORTED; 1572 1573 /* Check that max_key_size follows restrictions */ 1574 res = check_key_size(type_props, max_key_size); 1575 if (res) 1576 return res; 1577 1578 o->attr = calloc(1, type_props->alloc_size); 1579 if (!o->attr) 1580 return TEE_ERROR_OUT_OF_MEMORY; 1581 } 1582 1583 /* If we have a key structure, pre-allocate the bignums inside */ 1584 switch (obj_type) { 1585 case TEE_TYPE_RSA_PUBLIC_KEY: 1586 res = crypto_acipher_alloc_rsa_public_key(o->attr, 1587 max_key_size); 1588 break; 1589 case TEE_TYPE_RSA_KEYPAIR: 1590 res = crypto_acipher_alloc_rsa_keypair(o->attr, max_key_size); 1591 break; 1592 case TEE_TYPE_DSA_PUBLIC_KEY: 1593 res = crypto_acipher_alloc_dsa_public_key(o->attr, 1594 max_key_size); 1595 break; 1596 case TEE_TYPE_DSA_KEYPAIR: 1597 res = crypto_acipher_alloc_dsa_keypair(o->attr, max_key_size); 1598 break; 1599 case TEE_TYPE_DH_KEYPAIR: 1600 res = crypto_acipher_alloc_dh_keypair(o->attr, max_key_size); 1601 break; 1602 case TEE_TYPE_ECDSA_PUBLIC_KEY: 1603 case TEE_TYPE_ECDH_PUBLIC_KEY: 1604 case TEE_TYPE_SM2_DSA_PUBLIC_KEY: 1605 case TEE_TYPE_SM2_PKE_PUBLIC_KEY: 1606 case TEE_TYPE_SM2_KEP_PUBLIC_KEY: 1607 res = crypto_acipher_alloc_ecc_public_key(o->attr, obj_type, 1608 max_key_size); 1609 break; 1610 case TEE_TYPE_ECDSA_KEYPAIR: 1611 case TEE_TYPE_ECDH_KEYPAIR: 1612 case TEE_TYPE_SM2_DSA_KEYPAIR: 1613 case TEE_TYPE_SM2_PKE_KEYPAIR: 1614 case TEE_TYPE_SM2_KEP_KEYPAIR: 1615 res = crypto_acipher_alloc_ecc_keypair(o->attr, obj_type, 1616 max_key_size); 1617 break; 1618 case TEE_TYPE_X25519_KEYPAIR: 1619 res = crypto_acipher_alloc_x25519_keypair(o->attr, 1620 max_key_size); 1621 break; 1622 case TEE_TYPE_X448_KEYPAIR: 1623 res = crypto_acipher_alloc_x448_keypair(o->attr, 1624 max_key_size); 1625 break; 1626 case TEE_TYPE_ED25519_KEYPAIR: 1627 res = crypto_acipher_alloc_ed25519_keypair(o->attr, 1628 max_key_size); 1629 break; 1630 case TEE_TYPE_ED25519_PUBLIC_KEY: 1631 res = crypto_acipher_alloc_ed25519_public_key(o->attr, 1632 max_key_size); 1633 break; 1634 default: 1635 if (obj_type != TEE_TYPE_DATA) { 1636 struct tee_cryp_obj_secret *key = o->attr; 1637 1638 key->alloc_size = type_props->alloc_size - 1639 sizeof(*key); 1640 } 1641 break; 1642 } 1643 1644 if (res != TEE_SUCCESS) 1645 return res; 1646 1647 o->info.objectType = obj_type; 1648 o->info.maxObjectSize = max_key_size; 1649 if (o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) 1650 o->pobj->obj_info_usage = TEE_USAGE_DEFAULT; 1651 else 1652 o->info.objectUsage = TEE_USAGE_DEFAULT; 1653 1654 return TEE_SUCCESS; 1655 } 1656 1657 TEE_Result syscall_cryp_obj_alloc(unsigned long obj_type, 1658 unsigned long max_key_size, uint32_t *obj) 1659 { 1660 struct ts_session *sess = ts_get_current_session(); 1661 TEE_Result res = TEE_SUCCESS; 1662 struct tee_obj *o = NULL; 1663 1664 1665 o = tee_obj_alloc(); 1666 if (!o) 1667 return TEE_ERROR_OUT_OF_MEMORY; 1668 1669 res = tee_obj_set_type(o, obj_type, max_key_size); 1670 if (res != TEE_SUCCESS) { 1671 tee_obj_free(o); 1672 return res; 1673 } 1674 1675 tee_obj_add(to_user_ta_ctx(sess->ctx), o); 1676 1677 res = copy_kaddr_to_uref(obj, o); 1678 if (res != TEE_SUCCESS) 1679 tee_obj_close(to_user_ta_ctx(sess->ctx), o); 1680 return res; 1681 } 1682 1683 TEE_Result syscall_cryp_obj_close(unsigned long obj) 1684 { 1685 struct ts_session *sess = ts_get_current_session(); 1686 TEE_Result res = TEE_SUCCESS; 1687 struct tee_obj *o = NULL; 1688 1689 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1690 if (res != TEE_SUCCESS) 1691 return res; 1692 1693 /* 1694 * If it's busy it's used by an operation, a client should never have 1695 * this handle. 1696 */ 1697 if (o->busy) 1698 return TEE_ERROR_ITEM_NOT_FOUND; 1699 1700 tee_obj_close(to_user_ta_ctx(sess->ctx), o); 1701 return TEE_SUCCESS; 1702 } 1703 1704 TEE_Result syscall_cryp_obj_reset(unsigned long obj) 1705 { 1706 struct ts_session *sess = ts_get_current_session(); 1707 TEE_Result res = TEE_SUCCESS; 1708 struct tee_obj *o = NULL; 1709 1710 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1711 if (res != TEE_SUCCESS) 1712 return res; 1713 1714 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) == 0) { 1715 tee_obj_attr_clear(o); 1716 o->info.objectSize = 0; 1717 o->info.objectUsage = TEE_USAGE_DEFAULT; 1718 } else { 1719 return TEE_ERROR_BAD_PARAMETERS; 1720 } 1721 1722 /* the object is no more initialized */ 1723 o->info.handleFlags &= ~TEE_HANDLE_FLAG_INITIALIZED; 1724 1725 return TEE_SUCCESS; 1726 } 1727 1728 static TEE_Result copy_in_attrs(struct user_ta_ctx *utc, 1729 const struct utee_attribute *usr_attrs, 1730 uint32_t attr_count, TEE_Attribute *attrs) 1731 { 1732 TEE_Result res = TEE_SUCCESS; 1733 size_t size = 0; 1734 uint32_t n = 0; 1735 struct utee_attribute *usr_attrs_bbuf = NULL; 1736 1737 if (MUL_OVERFLOW(sizeof(struct utee_attribute), attr_count, &size)) 1738 return TEE_ERROR_OVERFLOW; 1739 1740 usr_attrs_bbuf = bb_alloc(size); 1741 if (!usr_attrs_bbuf) 1742 return TEE_ERROR_OUT_OF_MEMORY; 1743 1744 res = copy_from_user(usr_attrs_bbuf, usr_attrs, size); 1745 if (res) 1746 goto out; 1747 1748 for (n = 0; n < attr_count; n++) { 1749 attrs[n].attributeID = usr_attrs_bbuf[n].attribute_id; 1750 if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) { 1751 attrs[n].content.value.a = usr_attrs_bbuf[n].a; 1752 attrs[n].content.value.b = usr_attrs_bbuf[n].b; 1753 } else { 1754 uintptr_t buf = usr_attrs_bbuf[n].a; 1755 size_t len = usr_attrs_bbuf[n].b; 1756 uint32_t flags = TEE_MEMORY_ACCESS_READ | 1757 TEE_MEMORY_ACCESS_ANY_OWNER; 1758 1759 buf = memtag_strip_tag_vaddr((void *)buf); 1760 1761 res = vm_check_access_rights(&utc->uctx, flags, buf, 1762 len); 1763 if (res != TEE_SUCCESS) 1764 goto out; 1765 attrs[n].content.ref.buffer = (void *)buf; 1766 attrs[n].content.ref.length = len; 1767 } 1768 } 1769 1770 out: 1771 bb_free(usr_attrs_bbuf, size); 1772 return res; 1773 } 1774 1775 enum attr_usage { 1776 ATTR_USAGE_POPULATE, 1777 ATTR_USAGE_GENERATE_KEY 1778 }; 1779 1780 static TEE_Result tee_svc_cryp_check_attr(enum attr_usage usage, 1781 const struct tee_cryp_obj_type_props 1782 *type_props, 1783 const TEE_Attribute *attrs, 1784 uint32_t attr_count) 1785 { 1786 uint32_t required_flag = 0; 1787 uint32_t opt_flag = 0; 1788 bool all_opt_needed = false; 1789 uint32_t req_attrs = 0; 1790 uint32_t opt_grp_attrs = 0; 1791 uint32_t attrs_found = 0; 1792 size_t n = 0; 1793 uint32_t bit = 0; 1794 uint32_t flags = 0; 1795 int idx = 0; 1796 1797 if (usage == ATTR_USAGE_POPULATE) { 1798 required_flag = TEE_TYPE_ATTR_REQUIRED; 1799 opt_flag = TEE_TYPE_ATTR_OPTIONAL_GROUP; 1800 all_opt_needed = true; 1801 } else { 1802 required_flag = TEE_TYPE_ATTR_GEN_KEY_REQ; 1803 opt_flag = TEE_TYPE_ATTR_GEN_KEY_OPT; 1804 all_opt_needed = false; 1805 } 1806 1807 /* 1808 * First find out which attributes are required and which belong to 1809 * the optional group 1810 */ 1811 for (n = 0; n < type_props->num_type_attrs; n++) { 1812 bit = 1 << n; 1813 flags = type_props->type_attrs[n].flags; 1814 1815 if (flags & required_flag) 1816 req_attrs |= bit; 1817 else if (flags & opt_flag) 1818 opt_grp_attrs |= bit; 1819 } 1820 1821 /* 1822 * Verify that all required attributes are in place and 1823 * that the same attribute isn't repeated. 1824 */ 1825 for (n = 0; n < attr_count; n++) { 1826 idx = tee_svc_cryp_obj_find_type_attr_idx( 1827 attrs[n].attributeID, 1828 type_props); 1829 1830 /* attribute not defined in current object type */ 1831 if (idx < 0) 1832 return TEE_ERROR_ITEM_NOT_FOUND; 1833 1834 bit = 1 << idx; 1835 1836 /* attribute not repeated */ 1837 if ((attrs_found & bit) != 0) 1838 return TEE_ERROR_ITEM_NOT_FOUND; 1839 1840 /* 1841 * Attribute not defined in current object type for this 1842 * usage. 1843 */ 1844 if (!(bit & (req_attrs | opt_grp_attrs))) 1845 return TEE_ERROR_ITEM_NOT_FOUND; 1846 1847 attrs_found |= bit; 1848 } 1849 /* Required attribute missing */ 1850 if ((attrs_found & req_attrs) != req_attrs) 1851 return TEE_ERROR_ITEM_NOT_FOUND; 1852 1853 /* 1854 * If the flag says that "if one of the optional attributes are included 1855 * all of them has to be included" this must be checked. 1856 */ 1857 if (all_opt_needed && (attrs_found & opt_grp_attrs) != 0 && 1858 (attrs_found & opt_grp_attrs) != opt_grp_attrs) 1859 return TEE_ERROR_ITEM_NOT_FOUND; 1860 1861 return TEE_SUCCESS; 1862 } 1863 1864 static TEE_Result get_ec_key_size(uint32_t curve, size_t *key_size) 1865 { 1866 switch (curve) { 1867 case TEE_ECC_CURVE_NIST_P192: 1868 *key_size = 192; 1869 break; 1870 case TEE_ECC_CURVE_NIST_P224: 1871 *key_size = 224; 1872 break; 1873 case TEE_ECC_CURVE_NIST_P256: 1874 *key_size = 256; 1875 break; 1876 case TEE_ECC_CURVE_NIST_P384: 1877 *key_size = 384; 1878 break; 1879 case TEE_ECC_CURVE_NIST_P521: 1880 *key_size = 521; 1881 break; 1882 case TEE_ECC_CURVE_SM2: 1883 case TEE_ECC_CURVE_25519: 1884 *key_size = 256; 1885 break; 1886 default: 1887 return TEE_ERROR_NOT_SUPPORTED; 1888 } 1889 1890 return TEE_SUCCESS; 1891 } 1892 1893 static TEE_Result tee_svc_cryp_obj_populate_type( 1894 struct tee_obj *o, 1895 const struct tee_cryp_obj_type_props *type_props, 1896 const TEE_Attribute *attrs, 1897 uint32_t attr_count) 1898 { 1899 TEE_Result res = TEE_SUCCESS; 1900 uint32_t have_attrs = 0; 1901 size_t obj_size = 0; 1902 size_t n = 0; 1903 int idx = 0; 1904 const struct attr_ops *ops = NULL; 1905 void *attr = NULL; 1906 1907 for (n = 0; n < attr_count; n++) { 1908 idx = tee_svc_cryp_obj_find_type_attr_idx( 1909 attrs[n].attributeID, 1910 type_props); 1911 /* attribute not defined in current object type */ 1912 if (idx < 0) 1913 return TEE_ERROR_ITEM_NOT_FOUND; 1914 1915 have_attrs |= BIT32(idx); 1916 ops = attr_ops + type_props->type_attrs[idx].ops_index; 1917 attr = (uint8_t *)o->attr + 1918 type_props->type_attrs[idx].raw_offs; 1919 if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) 1920 res = ops->from_user(attr, &attrs[n].content.value, 1921 sizeof(attrs[n].content.value)); 1922 else 1923 res = ops->from_user(attr, attrs[n].content.ref.buffer, 1924 attrs[n].content.ref.length); 1925 if (res != TEE_SUCCESS) 1926 return res; 1927 1928 /* 1929 * The attribute that gives the size of the object is 1930 * flagged with TEE_TYPE_ATTR_SIZE_INDICATOR. 1931 */ 1932 if (type_props->type_attrs[idx].flags & 1933 TEE_TYPE_ATTR_SIZE_INDICATOR) { 1934 /* There should be only one */ 1935 if (obj_size) 1936 return TEE_ERROR_BAD_STATE; 1937 1938 /* 1939 * For ECDSA/ECDH we need to translate curve into 1940 * object size 1941 */ 1942 if (attrs[n].attributeID == TEE_ATTR_ECC_CURVE) { 1943 res = get_ec_key_size(attrs[n].content.value.a, 1944 &obj_size); 1945 if (res != TEE_SUCCESS) 1946 return res; 1947 } else { 1948 TEE_ObjectType obj_type = o->info.objectType; 1949 size_t sz = o->info.maxObjectSize; 1950 1951 obj_size = attrs[n].content.ref.length * 8; 1952 /* Drop the parity bits for legacy objects */ 1953 if (is_gp_legacy_des_key_size(obj_type, sz)) 1954 obj_size -= obj_size / 8; 1955 } 1956 if (obj_size > o->info.maxObjectSize) 1957 return TEE_ERROR_BAD_STATE; 1958 res = check_key_size(type_props, obj_size); 1959 if (res != TEE_SUCCESS) 1960 return TEE_ERROR_BAD_PARAMETERS; 1961 } 1962 1963 /* 1964 * Bignum attributes limited by the number of bits in 1965 * o->info.objectSize are flagged with 1966 * TEE_TYPE_ATTR_BIGNUM_MAXBITS. 1967 */ 1968 if (type_props->type_attrs[idx].flags & 1969 TEE_TYPE_ATTR_BIGNUM_MAXBITS) { 1970 if (crypto_bignum_num_bits(*(struct bignum **)attr) > 1971 o->info.maxObjectSize) 1972 return TEE_ERROR_BAD_STATE; 1973 } 1974 } 1975 1976 o->have_attrs = have_attrs; 1977 o->info.objectSize = obj_size; 1978 /* 1979 * In GP Internal API Specification 1.0 the partity bits aren't 1980 * counted when telling the size of the key in bits so remove the 1981 * parity bits here. 1982 */ 1983 if (is_gp_legacy_des_key_size(o->info.objectType, 1984 o->info.maxObjectSize)) 1985 o->info.objectSize -= o->info.objectSize / 8; 1986 1987 return TEE_SUCCESS; 1988 } 1989 1990 TEE_Result syscall_cryp_obj_populate(unsigned long obj, 1991 struct utee_attribute *usr_attrs, 1992 unsigned long attr_count) 1993 { 1994 struct ts_session *sess = ts_get_current_session(); 1995 TEE_Result res = TEE_SUCCESS; 1996 struct tee_obj *o = NULL; 1997 const struct tee_cryp_obj_type_props *type_props = NULL; 1998 TEE_Attribute *attrs = NULL; 1999 size_t alloc_size = 0; 2000 2001 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 2002 if (res != TEE_SUCCESS) 2003 return res; 2004 2005 /* Must be a transient object */ 2006 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 2007 return TEE_ERROR_BAD_PARAMETERS; 2008 2009 /* Must not be initialized already */ 2010 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 2011 return TEE_ERROR_BAD_PARAMETERS; 2012 2013 type_props = tee_svc_find_type_props(o->info.objectType); 2014 if (!type_props) 2015 return TEE_ERROR_NOT_IMPLEMENTED; 2016 2017 if (MUL_OVERFLOW(sizeof(TEE_Attribute), attr_count, &alloc_size)) 2018 return TEE_ERROR_OVERFLOW; 2019 2020 attrs = malloc(alloc_size); 2021 if (!attrs) 2022 return TEE_ERROR_OUT_OF_MEMORY; 2023 2024 res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_attrs, attr_count, 2025 attrs); 2026 if (res != TEE_SUCCESS) 2027 goto out; 2028 2029 res = tee_svc_cryp_check_attr(ATTR_USAGE_POPULATE, type_props, 2030 attrs, attr_count); 2031 if (res != TEE_SUCCESS) 2032 goto out; 2033 2034 res = tee_svc_cryp_obj_populate_type(o, type_props, attrs, attr_count); 2035 if (res == TEE_SUCCESS) 2036 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2037 2038 out: 2039 free_wipe(attrs); 2040 return res; 2041 } 2042 2043 TEE_Result syscall_cryp_obj_copy(unsigned long dst, unsigned long src) 2044 { 2045 struct ts_session *sess = ts_get_current_session(); 2046 TEE_Result res = TEE_SUCCESS; 2047 struct tee_obj *dst_o = NULL; 2048 struct tee_obj *src_o = NULL; 2049 2050 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 2051 uref_to_vaddr(dst), &dst_o); 2052 if (res != TEE_SUCCESS) 2053 return res; 2054 2055 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 2056 uref_to_vaddr(src), &src_o); 2057 if (res != TEE_SUCCESS) 2058 return res; 2059 2060 if ((src_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2061 return TEE_ERROR_BAD_PARAMETERS; 2062 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 2063 return TEE_ERROR_BAD_PARAMETERS; 2064 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 2065 return TEE_ERROR_BAD_PARAMETERS; 2066 2067 res = tee_obj_attr_copy_from(dst_o, src_o); 2068 if (res != TEE_SUCCESS) 2069 return res; 2070 2071 dst_o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2072 dst_o->info.objectSize = src_o->info.objectSize; 2073 if (src_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) { 2074 tee_pobj_lock_usage(src_o->pobj); 2075 dst_o->info.objectUsage = src_o->pobj->obj_info_usage; 2076 tee_pobj_unlock_usage(src_o->pobj); 2077 } else { 2078 dst_o->info.objectUsage = src_o->info.objectUsage; 2079 } 2080 return TEE_SUCCESS; 2081 } 2082 2083 static TEE_Result check_pub_rsa_key(struct bignum *e) 2084 { 2085 size_t n = crypto_bignum_num_bytes(e); 2086 uint8_t bin_key[256 / 8] = { 0 }; 2087 2088 /* 2089 * NIST SP800-56B requires public RSA key to be an odd integer in 2090 * the range 65537 <= e < 2^256. AOSP requires implementations to 2091 * support public exponents >= 3, which can be allowed by enabling 2092 * CFG_RSA_PUB_EXPONENT_3. 2093 */ 2094 2095 if (n > sizeof(bin_key) || n < 1) 2096 return TEE_ERROR_BAD_PARAMETERS; 2097 2098 crypto_bignum_bn2bin(e, bin_key); 2099 2100 if (!(bin_key[n - 1] & 1)) /* key must be odd */ 2101 return TEE_ERROR_BAD_PARAMETERS; 2102 2103 if (n <= 3) { 2104 uint32_t min_key = 65537; 2105 uint32_t key = 0; 2106 size_t m = 0; 2107 2108 if (IS_ENABLED(CFG_RSA_PUB_EXPONENT_3)) 2109 min_key = 3; 2110 2111 for (m = 0; m < n; m++) { 2112 key <<= 8; 2113 key |= bin_key[m]; 2114 } 2115 2116 if (key < min_key) 2117 return TEE_ERROR_BAD_PARAMETERS; 2118 } 2119 2120 return TEE_SUCCESS; 2121 } 2122 2123 static TEE_Result tee_svc_obj_generate_key_rsa( 2124 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2125 uint32_t key_size, 2126 const TEE_Attribute *params, uint32_t param_count) 2127 { 2128 TEE_Result res = TEE_SUCCESS; 2129 struct rsa_keypair *key = o->attr; 2130 uint32_t e = TEE_U32_TO_BIG_ENDIAN(65537); 2131 2132 /* Copy the present attributes into the obj before starting */ 2133 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2134 param_count); 2135 if (res != TEE_SUCCESS) 2136 return res; 2137 if (get_attribute(o, type_props, TEE_ATTR_RSA_PUBLIC_EXPONENT)) { 2138 res = check_pub_rsa_key(key->e); 2139 if (res) 2140 return res; 2141 } else { 2142 res = crypto_bignum_bin2bn((const uint8_t *)&e, sizeof(e), 2143 key->e); 2144 if (res) 2145 return res; 2146 } 2147 res = crypto_acipher_gen_rsa_key(key, key_size); 2148 if (res != TEE_SUCCESS) 2149 return res; 2150 2151 /* Set bits for all known attributes for this object type */ 2152 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 2153 2154 return TEE_SUCCESS; 2155 } 2156 2157 static TEE_Result tee_svc_obj_generate_key_dsa( 2158 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2159 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 2160 { 2161 TEE_Result res; 2162 2163 /* Copy the present attributes into the obj before starting */ 2164 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2165 param_count); 2166 if (res != TEE_SUCCESS) 2167 return res; 2168 2169 res = crypto_acipher_gen_dsa_key(o->attr, key_size); 2170 if (res != TEE_SUCCESS) 2171 return res; 2172 2173 /* Set bits for all known attributes for this object type */ 2174 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 2175 2176 return TEE_SUCCESS; 2177 } 2178 2179 static TEE_Result tee_svc_obj_generate_key_dh( 2180 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2181 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 2182 { 2183 TEE_Result res; 2184 struct dh_keypair *tee_dh_key; 2185 struct bignum *dh_q = NULL; 2186 uint32_t dh_xbits = 0; 2187 2188 /* Copy the present attributes into the obj before starting */ 2189 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2190 param_count); 2191 if (res != TEE_SUCCESS) 2192 return res; 2193 2194 tee_dh_key = (struct dh_keypair *)o->attr; 2195 2196 if (get_attribute(o, type_props, TEE_ATTR_DH_SUBPRIME)) 2197 dh_q = tee_dh_key->q; 2198 if (get_attribute(o, type_props, TEE_ATTR_DH_X_BITS)) 2199 dh_xbits = tee_dh_key->xbits; 2200 res = crypto_acipher_gen_dh_key(tee_dh_key, dh_q, dh_xbits, key_size); 2201 if (res != TEE_SUCCESS) 2202 return res; 2203 2204 /* Set bits for the generated public and private key */ 2205 set_attribute(o, type_props, TEE_ATTR_DH_PUBLIC_VALUE); 2206 set_attribute(o, type_props, TEE_ATTR_DH_PRIVATE_VALUE); 2207 set_attribute(o, type_props, TEE_ATTR_DH_X_BITS); 2208 return TEE_SUCCESS; 2209 } 2210 2211 static TEE_Result tee_svc_obj_generate_key_ecc( 2212 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2213 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 2214 { 2215 TEE_Result res; 2216 struct ecc_keypair *tee_ecc_key; 2217 2218 /* Copy the present attributes into the obj before starting */ 2219 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2220 param_count); 2221 if (res != TEE_SUCCESS) 2222 return res; 2223 2224 tee_ecc_key = (struct ecc_keypair *)o->attr; 2225 2226 res = crypto_acipher_gen_ecc_key(tee_ecc_key, key_size); 2227 if (res != TEE_SUCCESS) 2228 return res; 2229 2230 /* Set bits for the generated public and private key */ 2231 set_attribute(o, type_props, TEE_ATTR_ECC_PRIVATE_VALUE); 2232 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_X); 2233 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_Y); 2234 set_attribute(o, type_props, TEE_ATTR_ECC_CURVE); 2235 return TEE_SUCCESS; 2236 } 2237 2238 static TEE_Result 2239 tee_svc_obj_generate_key_x25519(struct tee_obj *o, 2240 const struct tee_cryp_obj_type_props 2241 *type_props, 2242 uint32_t key_size, 2243 const TEE_Attribute *params, 2244 uint32_t param_count) 2245 { 2246 TEE_Result res = TEE_ERROR_GENERIC; 2247 struct montgomery_keypair *tee_x25519_key = NULL; 2248 2249 /* Copy the present attributes into the obj before starting */ 2250 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2251 param_count); 2252 if (res != TEE_SUCCESS) 2253 return res; 2254 2255 tee_x25519_key = (struct montgomery_keypair *)o->attr; 2256 2257 res = crypto_acipher_gen_x25519_key(tee_x25519_key, key_size); 2258 if (res != TEE_SUCCESS) 2259 return res; 2260 2261 /* Set bits for the generated public and private key */ 2262 set_attribute(o, type_props, TEE_ATTR_X25519_PRIVATE_VALUE); 2263 set_attribute(o, type_props, TEE_ATTR_X25519_PUBLIC_VALUE); 2264 return TEE_SUCCESS; 2265 } 2266 2267 static TEE_Result 2268 tee_svc_obj_generate_key_x448(struct tee_obj *o, 2269 const struct tee_cryp_obj_type_props *type_props, 2270 uint32_t key_size, const TEE_Attribute *params, 2271 uint32_t param_count) 2272 { 2273 TEE_Result res = TEE_ERROR_GENERIC; 2274 struct montgomery_keypair *tee_x448_key = NULL; 2275 2276 /* Copy the present attributes into the obj before starting */ 2277 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2278 param_count); 2279 if (res != TEE_SUCCESS) 2280 return res; 2281 2282 tee_x448_key = (struct montgomery_keypair *)o->attr; 2283 res = crypto_acipher_gen_x448_key(tee_x448_key, key_size); 2284 if (res != TEE_SUCCESS) 2285 return res; 2286 2287 set_attribute(o, type_props, TEE_ATTR_X448_PRIVATE_VALUE); 2288 set_attribute(o, type_props, TEE_ATTR_X448_PUBLIC_VALUE); 2289 2290 return TEE_SUCCESS; 2291 } 2292 2293 static TEE_Result 2294 tee_svc_obj_generate_key_ed25519(struct tee_obj *o, 2295 const struct tee_cryp_obj_type_props 2296 *type_props, 2297 uint32_t key_size, 2298 const TEE_Attribute *params, 2299 uint32_t param_count) 2300 { 2301 TEE_Result res = TEE_ERROR_GENERIC; 2302 struct ed25519_keypair *key = NULL; 2303 2304 /* Copy the present attributes into the obj before starting */ 2305 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2306 param_count); 2307 if (res != TEE_SUCCESS) 2308 return res; 2309 2310 key = o->attr; 2311 2312 res = crypto_acipher_gen_ed25519_key(key, key_size); 2313 if (res != TEE_SUCCESS) 2314 return res; 2315 2316 /* Set bits for the generated public and private key */ 2317 set_attribute(o, type_props, TEE_ATTR_ED25519_PRIVATE_VALUE); 2318 set_attribute(o, type_props, TEE_ATTR_ED25519_PUBLIC_VALUE); 2319 return TEE_SUCCESS; 2320 } 2321 2322 static TEE_Result 2323 tee_svc_obj_ed25519_parse_params(const TEE_Attribute *params, size_t num_params, 2324 bool *ph_flag, const uint8_t **ctx, 2325 size_t *ctx_len) 2326 { 2327 size_t n = 0; 2328 2329 *ctx = NULL; 2330 2331 for (n = 0; n < num_params; n++) { 2332 switch (params[n].attributeID) { 2333 case TEE_ATTR_EDDSA_PREHASH: 2334 if (params[n].content.value.b) 2335 return TEE_ERROR_BAD_PARAMETERS; 2336 if (!params[n].content.value.a) 2337 *ph_flag = false; 2338 else if (params[n].content.value.a == 1) 2339 *ph_flag = true; 2340 else 2341 return TEE_ERROR_BAD_PARAMETERS; 2342 break; 2343 2344 case TEE_ATTR_EDDSA_CTX: 2345 /* several provided contexts are treated as error */ 2346 if (*ctx) 2347 return TEE_ERROR_BAD_PARAMETERS; 2348 2349 *ctx_len = params[n].content.ref.length; 2350 if (*ctx_len > TEE_ED25519_CTX_MAX_LENGTH) 2351 return TEE_ERROR_BAD_PARAMETERS; 2352 2353 if (!*ctx_len) 2354 break; 2355 2356 *ctx = params[n].content.ref.buffer; 2357 if (!*ctx) 2358 return TEE_ERROR_BAD_PARAMETERS; 2359 break; 2360 2361 default: 2362 return TEE_ERROR_BAD_PARAMETERS; 2363 } 2364 } 2365 2366 return TEE_SUCCESS; 2367 } 2368 2369 static TEE_Result 2370 tee_svc_obj_ed25519_sign(struct ed25519_keypair *key, 2371 const uint8_t *msg, size_t msg_len, 2372 uint8_t *sig, size_t *sig_len, 2373 const TEE_Attribute *params, size_t num_params) 2374 { 2375 TEE_Result err = TEE_ERROR_GENERIC; 2376 size_t ctx_len = 0; 2377 const uint8_t *ctx = NULL; 2378 bool ph_flag = false; 2379 2380 err = tee_svc_obj_ed25519_parse_params(params, num_params, &ph_flag, 2381 &ctx, &ctx_len); 2382 if (err != TEE_SUCCESS) 2383 return err; 2384 2385 if (ph_flag || ctx) { 2386 return crypto_acipher_ed25519ctx_sign(key, msg, msg_len, sig, 2387 sig_len, ph_flag, 2388 ctx, ctx_len); 2389 } 2390 2391 return crypto_acipher_ed25519_sign(key, msg, msg_len, sig, sig_len); 2392 } 2393 2394 static TEE_Result 2395 tee_svc_obj_ed25519_verify(struct ed25519_public_key *key, 2396 const uint8_t *msg, size_t msg_len, 2397 const uint8_t *sig, size_t sig_len, 2398 const TEE_Attribute *params, size_t num_params) 2399 { 2400 TEE_Result err = TEE_ERROR_GENERIC; 2401 size_t ctx_len = 0; 2402 const uint8_t *ctx = NULL; 2403 bool ph_flag = false; 2404 2405 err = tee_svc_obj_ed25519_parse_params(params, num_params, &ph_flag, 2406 &ctx, &ctx_len); 2407 if (err) 2408 return err; 2409 2410 if (ph_flag || ctx) { 2411 return crypto_acipher_ed25519ctx_verify(key, msg, msg_len, sig, 2412 sig_len, ph_flag, 2413 ctx, ctx_len); 2414 } 2415 2416 return crypto_acipher_ed25519_verify(key, msg, msg_len, sig, sig_len); 2417 } 2418 2419 TEE_Result syscall_obj_generate_key(unsigned long obj, unsigned long key_size, 2420 const struct utee_attribute *usr_params, 2421 unsigned long param_count) 2422 { 2423 struct ts_session *sess = ts_get_current_session(); 2424 TEE_Result res = TEE_SUCCESS; 2425 const struct tee_cryp_obj_type_props *type_props = NULL; 2426 struct tee_obj *o = NULL; 2427 struct tee_cryp_obj_secret *key = NULL; 2428 size_t byte_size = 0; 2429 TEE_Attribute *params = NULL; 2430 size_t alloc_size = 0; 2431 2432 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 2433 if (res != TEE_SUCCESS) 2434 return res; 2435 2436 /* Must be a transient object */ 2437 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 2438 return TEE_ERROR_BAD_STATE; 2439 2440 /* Must not be initialized already */ 2441 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 2442 return TEE_ERROR_BAD_STATE; 2443 2444 /* Find description of object */ 2445 type_props = tee_svc_find_type_props(o->info.objectType); 2446 if (!type_props) 2447 return TEE_ERROR_NOT_SUPPORTED; 2448 2449 /* Check that key_size follows restrictions */ 2450 res = check_key_size(type_props, key_size); 2451 if (res) 2452 return res; 2453 2454 if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size)) 2455 return TEE_ERROR_OVERFLOW; 2456 2457 params = malloc(alloc_size); 2458 if (!params) 2459 return TEE_ERROR_OUT_OF_MEMORY; 2460 res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_params, param_count, 2461 params); 2462 if (res != TEE_SUCCESS) 2463 goto out; 2464 2465 res = tee_svc_cryp_check_attr(ATTR_USAGE_GENERATE_KEY, type_props, 2466 params, param_count); 2467 if (res != TEE_SUCCESS) 2468 goto out; 2469 2470 switch (o->info.objectType) { 2471 case TEE_TYPE_AES: 2472 case TEE_TYPE_DES: 2473 case TEE_TYPE_DES3: 2474 case TEE_TYPE_SM4: 2475 case TEE_TYPE_HMAC_MD5: 2476 case TEE_TYPE_HMAC_SHA1: 2477 case TEE_TYPE_HMAC_SHA224: 2478 case TEE_TYPE_HMAC_SHA256: 2479 case TEE_TYPE_HMAC_SHA384: 2480 case TEE_TYPE_HMAC_SHA512: 2481 case TEE_TYPE_HMAC_SHA3_224: 2482 case TEE_TYPE_HMAC_SHA3_256: 2483 case TEE_TYPE_HMAC_SHA3_384: 2484 case TEE_TYPE_HMAC_SHA3_512: 2485 case TEE_TYPE_HMAC_SM3: 2486 case TEE_TYPE_GENERIC_SECRET: 2487 byte_size = key_size / 8; 2488 2489 /* 2490 * In GP Internal API Specification 1.0 the partity bits 2491 * aren't counted when telling the size of the key in bits. 2492 */ 2493 if (is_gp_legacy_des_key_size(o->info.objectType, key_size)) 2494 byte_size = (key_size + key_size / 7) / 8; 2495 2496 key = (struct tee_cryp_obj_secret *)o->attr; 2497 if (byte_size > key->alloc_size) { 2498 res = TEE_ERROR_EXCESS_DATA; 2499 goto out; 2500 } 2501 2502 res = crypto_rng_read((void *)(key + 1), byte_size); 2503 if (res != TEE_SUCCESS) 2504 goto out; 2505 2506 key->key_size = byte_size; 2507 2508 /* Set bits for all known attributes for this object type */ 2509 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 2510 2511 break; 2512 2513 case TEE_TYPE_RSA_KEYPAIR: 2514 res = tee_svc_obj_generate_key_rsa(o, type_props, key_size, 2515 params, param_count); 2516 if (res != TEE_SUCCESS) 2517 goto out; 2518 break; 2519 2520 case TEE_TYPE_DSA_KEYPAIR: 2521 res = tee_svc_obj_generate_key_dsa(o, type_props, key_size, 2522 params, param_count); 2523 if (res != TEE_SUCCESS) 2524 goto out; 2525 break; 2526 2527 case TEE_TYPE_DH_KEYPAIR: 2528 res = tee_svc_obj_generate_key_dh(o, type_props, key_size, 2529 params, param_count); 2530 if (res != TEE_SUCCESS) 2531 goto out; 2532 break; 2533 2534 case TEE_TYPE_ECDSA_KEYPAIR: 2535 case TEE_TYPE_ECDH_KEYPAIR: 2536 case TEE_TYPE_SM2_DSA_KEYPAIR: 2537 case TEE_TYPE_SM2_KEP_KEYPAIR: 2538 case TEE_TYPE_SM2_PKE_KEYPAIR: 2539 res = tee_svc_obj_generate_key_ecc(o, type_props, key_size, 2540 params, param_count); 2541 if (res != TEE_SUCCESS) 2542 goto out; 2543 break; 2544 2545 case TEE_TYPE_X25519_KEYPAIR: 2546 res = tee_svc_obj_generate_key_x25519(o, type_props, key_size, 2547 params, param_count); 2548 if (res != TEE_SUCCESS) 2549 goto out; 2550 break; 2551 case TEE_TYPE_X448_KEYPAIR: 2552 res = tee_svc_obj_generate_key_x448(o, type_props, key_size, 2553 params, param_count); 2554 if (res != TEE_SUCCESS) 2555 goto out; 2556 break; 2557 2558 case TEE_TYPE_ED25519_KEYPAIR: 2559 res = tee_svc_obj_generate_key_ed25519(o, type_props, key_size, 2560 params, param_count); 2561 if (res != TEE_SUCCESS) 2562 goto out; 2563 break; 2564 2565 default: 2566 res = TEE_ERROR_BAD_FORMAT; 2567 } 2568 2569 out: 2570 free_wipe(params); 2571 if (res == TEE_SUCCESS) { 2572 o->info.objectSize = key_size; 2573 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2574 } 2575 return res; 2576 } 2577 2578 static TEE_Result tee_svc_cryp_get_state(struct ts_session *sess, 2579 vaddr_t state_id, 2580 struct tee_cryp_state **state) 2581 { 2582 struct tee_cryp_state *s; 2583 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 2584 2585 TAILQ_FOREACH(s, &utc->cryp_states, link) { 2586 if (state_id == (vaddr_t)s) { 2587 *state = s; 2588 return TEE_SUCCESS; 2589 } 2590 } 2591 return TEE_ERROR_BAD_PARAMETERS; 2592 } 2593 2594 static void cryp_state_free(struct user_ta_ctx *utc, struct tee_cryp_state *cs) 2595 { 2596 struct tee_obj *o; 2597 2598 if (tee_obj_get(utc, cs->key1, &o) == TEE_SUCCESS) 2599 tee_obj_close(utc, o); 2600 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) 2601 tee_obj_close(utc, o); 2602 2603 TAILQ_REMOVE(&utc->cryp_states, cs, link); 2604 if (cs->ctx_finalize != NULL) 2605 cs->ctx_finalize(cs->ctx); 2606 2607 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2608 case TEE_OPERATION_CIPHER: 2609 crypto_cipher_free_ctx(cs->ctx); 2610 break; 2611 case TEE_OPERATION_AE: 2612 crypto_authenc_free_ctx(cs->ctx); 2613 break; 2614 case TEE_OPERATION_DIGEST: 2615 crypto_hash_free_ctx(cs->ctx); 2616 break; 2617 case TEE_OPERATION_MAC: 2618 crypto_mac_free_ctx(cs->ctx); 2619 break; 2620 default: 2621 assert(!cs->ctx); 2622 } 2623 2624 free(cs); 2625 } 2626 2627 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o, 2628 uint32_t algo, 2629 TEE_OperationMode mode) 2630 { 2631 uint32_t req_key_type; 2632 uint32_t req_key_type2 = 0; 2633 2634 switch (TEE_ALG_GET_MAIN_ALG(algo)) { 2635 case TEE_MAIN_ALGO_MD5: 2636 req_key_type = TEE_TYPE_HMAC_MD5; 2637 break; 2638 case TEE_MAIN_ALGO_SHA1: 2639 req_key_type = TEE_TYPE_HMAC_SHA1; 2640 break; 2641 case TEE_MAIN_ALGO_SHA224: 2642 req_key_type = TEE_TYPE_HMAC_SHA224; 2643 break; 2644 case TEE_MAIN_ALGO_SHA256: 2645 req_key_type = TEE_TYPE_HMAC_SHA256; 2646 break; 2647 case TEE_MAIN_ALGO_SHA384: 2648 req_key_type = TEE_TYPE_HMAC_SHA384; 2649 break; 2650 case TEE_MAIN_ALGO_SHA512: 2651 req_key_type = TEE_TYPE_HMAC_SHA512; 2652 break; 2653 case TEE_MAIN_ALGO_SHA3_224: 2654 req_key_type = TEE_TYPE_HMAC_SHA3_224; 2655 break; 2656 case TEE_MAIN_ALGO_SHA3_256: 2657 req_key_type = TEE_TYPE_HMAC_SHA3_256; 2658 break; 2659 case TEE_MAIN_ALGO_SHA3_384: 2660 req_key_type = TEE_TYPE_HMAC_SHA3_384; 2661 break; 2662 case TEE_MAIN_ALGO_SHA3_512: 2663 req_key_type = TEE_TYPE_HMAC_SHA3_512; 2664 break; 2665 case TEE_MAIN_ALGO_SM3: 2666 req_key_type = TEE_TYPE_HMAC_SM3; 2667 break; 2668 case TEE_MAIN_ALGO_AES: 2669 req_key_type = TEE_TYPE_AES; 2670 break; 2671 case TEE_MAIN_ALGO_DES: 2672 req_key_type = TEE_TYPE_DES; 2673 break; 2674 case TEE_MAIN_ALGO_DES3: 2675 req_key_type = TEE_TYPE_DES3; 2676 break; 2677 case TEE_MAIN_ALGO_SM4: 2678 req_key_type = TEE_TYPE_SM4; 2679 break; 2680 case TEE_MAIN_ALGO_RSA: 2681 req_key_type = TEE_TYPE_RSA_KEYPAIR; 2682 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 2683 req_key_type2 = TEE_TYPE_RSA_PUBLIC_KEY; 2684 break; 2685 case TEE_MAIN_ALGO_DSA: 2686 req_key_type = TEE_TYPE_DSA_KEYPAIR; 2687 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 2688 req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY; 2689 break; 2690 case TEE_MAIN_ALGO_DH: 2691 req_key_type = TEE_TYPE_DH_KEYPAIR; 2692 break; 2693 case TEE_MAIN_ALGO_ECDSA: 2694 req_key_type = TEE_TYPE_ECDSA_KEYPAIR; 2695 if (mode == TEE_MODE_VERIFY) 2696 req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY; 2697 break; 2698 case TEE_MAIN_ALGO_ECDH: 2699 req_key_type = TEE_TYPE_ECDH_KEYPAIR; 2700 break; 2701 case TEE_MAIN_ALGO_ED25519: 2702 req_key_type = TEE_TYPE_ED25519_KEYPAIR; 2703 if (mode == TEE_MODE_VERIFY) 2704 req_key_type2 = TEE_TYPE_ED25519_PUBLIC_KEY; 2705 break; 2706 case TEE_MAIN_ALGO_SM2_PKE: 2707 if (mode == TEE_MODE_ENCRYPT) 2708 req_key_type = TEE_TYPE_SM2_PKE_PUBLIC_KEY; 2709 else 2710 req_key_type = TEE_TYPE_SM2_PKE_KEYPAIR; 2711 break; 2712 case TEE_MAIN_ALGO_SM2_DSA_SM3: 2713 if (mode == TEE_MODE_VERIFY) 2714 req_key_type = TEE_TYPE_SM2_DSA_PUBLIC_KEY; 2715 else 2716 req_key_type = TEE_TYPE_SM2_DSA_KEYPAIR; 2717 break; 2718 #if defined(CFG_CRYPTO_SM2_KEP) 2719 case TEE_MAIN_ALGO_SM2_KEP: 2720 req_key_type = TEE_TYPE_SM2_KEP_KEYPAIR; 2721 req_key_type2 = TEE_TYPE_SM2_KEP_PUBLIC_KEY; 2722 break; 2723 #endif 2724 #if defined(CFG_CRYPTO_HKDF) 2725 case TEE_MAIN_ALGO_HKDF: 2726 req_key_type = TEE_TYPE_HKDF_IKM; 2727 break; 2728 #endif 2729 #if defined(CFG_CRYPTO_CONCAT_KDF) 2730 case TEE_MAIN_ALGO_CONCAT_KDF: 2731 req_key_type = TEE_TYPE_CONCAT_KDF_Z; 2732 break; 2733 #endif 2734 #if defined(CFG_CRYPTO_PBKDF2) 2735 case TEE_MAIN_ALGO_PBKDF2: 2736 req_key_type = TEE_TYPE_PBKDF2_PASSWORD; 2737 break; 2738 #endif 2739 case TEE_MAIN_ALGO_X25519: 2740 req_key_type = TEE_TYPE_X25519_KEYPAIR; 2741 break; 2742 case TEE_MAIN_ALGO_X448: 2743 req_key_type = TEE_TYPE_X448_KEYPAIR; 2744 break; 2745 default: 2746 return TEE_ERROR_BAD_PARAMETERS; 2747 } 2748 2749 if (req_key_type != o->info.objectType && 2750 req_key_type2 != o->info.objectType) 2751 return TEE_ERROR_BAD_PARAMETERS; 2752 return TEE_SUCCESS; 2753 } 2754 2755 static uint32_t translate_compat_algo(uint32_t algo) 2756 { 2757 switch (algo) { 2758 case __OPTEE_ALG_ECDSA_P192: 2759 return TEE_ALG_ECDSA_SHA1; 2760 case __OPTEE_ALG_ECDSA_P224: 2761 return TEE_ALG_ECDSA_SHA224; 2762 case __OPTEE_ALG_ECDSA_P256: 2763 return TEE_ALG_ECDSA_SHA256; 2764 case __OPTEE_ALG_ECDSA_P384: 2765 return TEE_ALG_ECDSA_SHA384; 2766 case __OPTEE_ALG_ECDSA_P521: 2767 return TEE_ALG_ECDSA_SHA512; 2768 case __OPTEE_ALG_ECDH_P192: 2769 case __OPTEE_ALG_ECDH_P224: 2770 case __OPTEE_ALG_ECDH_P256: 2771 case __OPTEE_ALG_ECDH_P384: 2772 case __OPTEE_ALG_ECDH_P521: 2773 return TEE_ALG_ECDH_DERIVE_SHARED_SECRET; 2774 default: 2775 return algo; 2776 } 2777 } 2778 2779 TEE_Result syscall_cryp_state_alloc(unsigned long algo, unsigned long mode, 2780 unsigned long key1, unsigned long key2, 2781 uint32_t *state) 2782 { 2783 struct ts_session *sess = ts_get_current_session(); 2784 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 2785 TEE_Result res = TEE_SUCCESS; 2786 struct tee_cryp_state *cs = NULL; 2787 struct tee_obj *o1 = NULL; 2788 struct tee_obj *o2 = NULL; 2789 2790 algo = translate_compat_algo(algo); 2791 2792 if (key1 != 0) { 2793 res = tee_obj_get(utc, uref_to_vaddr(key1), &o1); 2794 if (res != TEE_SUCCESS) 2795 return res; 2796 if (o1->busy) 2797 return TEE_ERROR_BAD_PARAMETERS; 2798 res = tee_svc_cryp_check_key_type(o1, algo, mode); 2799 if (res != TEE_SUCCESS) 2800 return res; 2801 } 2802 if (key2 != 0) { 2803 res = tee_obj_get(utc, uref_to_vaddr(key2), &o2); 2804 if (res != TEE_SUCCESS) 2805 return res; 2806 if (o2->busy) 2807 return TEE_ERROR_BAD_PARAMETERS; 2808 res = tee_svc_cryp_check_key_type(o2, algo, mode); 2809 if (res != TEE_SUCCESS) 2810 return res; 2811 } 2812 2813 cs = calloc(1, sizeof(struct tee_cryp_state)); 2814 if (!cs) 2815 return TEE_ERROR_OUT_OF_MEMORY; 2816 TAILQ_INSERT_TAIL(&utc->cryp_states, cs, link); 2817 cs->algo = algo; 2818 cs->mode = mode; 2819 cs->state = CRYP_STATE_UNINITIALIZED; 2820 2821 switch (TEE_ALG_GET_CLASS(algo)) { 2822 case TEE_OPERATION_CIPHER: 2823 if ((TEE_ALG_GET_CHAIN_MODE(algo) == TEE_CHAIN_MODE_XTS && 2824 (key1 == 0 || key2 == 0)) || 2825 (TEE_ALG_GET_CHAIN_MODE(algo) != TEE_CHAIN_MODE_XTS && 2826 (key1 == 0 || key2 != 0))) { 2827 res = TEE_ERROR_BAD_PARAMETERS; 2828 } else { 2829 res = crypto_cipher_alloc_ctx(&cs->ctx, algo); 2830 if (res != TEE_SUCCESS) 2831 break; 2832 } 2833 break; 2834 case TEE_OPERATION_AE: 2835 if (key1 == 0 || key2 != 0) { 2836 res = TEE_ERROR_BAD_PARAMETERS; 2837 } else { 2838 res = crypto_authenc_alloc_ctx(&cs->ctx, algo); 2839 if (res != TEE_SUCCESS) 2840 break; 2841 } 2842 break; 2843 case TEE_OPERATION_MAC: 2844 if (key1 == 0 || key2 != 0) { 2845 res = TEE_ERROR_BAD_PARAMETERS; 2846 } else { 2847 res = crypto_mac_alloc_ctx(&cs->ctx, algo); 2848 if (res != TEE_SUCCESS) 2849 break; 2850 } 2851 break; 2852 case TEE_OPERATION_DIGEST: 2853 if (key1 != 0 || key2 != 0) { 2854 res = TEE_ERROR_BAD_PARAMETERS; 2855 } else { 2856 res = crypto_hash_alloc_ctx(&cs->ctx, algo); 2857 if (res != TEE_SUCCESS) 2858 break; 2859 } 2860 break; 2861 case TEE_OPERATION_ASYMMETRIC_CIPHER: 2862 case TEE_OPERATION_ASYMMETRIC_SIGNATURE: 2863 if (algo == TEE_ALG_RSASSA_PKCS1_V1_5 && 2864 !IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 2865 res = TEE_ERROR_NOT_SUPPORTED; 2866 break; 2867 } 2868 if (key1 == 0 || key2 != 0) 2869 res = TEE_ERROR_BAD_PARAMETERS; 2870 break; 2871 case TEE_OPERATION_KEY_DERIVATION: 2872 if (algo == TEE_ALG_SM2_KEP) { 2873 if (key1 == 0 || key2 == 0) 2874 res = TEE_ERROR_BAD_PARAMETERS; 2875 } else { 2876 if (key1 == 0 || key2 != 0) 2877 res = TEE_ERROR_BAD_PARAMETERS; 2878 } 2879 break; 2880 default: 2881 res = TEE_ERROR_NOT_SUPPORTED; 2882 break; 2883 } 2884 if (res != TEE_SUCCESS) 2885 goto out; 2886 2887 res = copy_kaddr_to_uref(state, cs); 2888 if (res != TEE_SUCCESS) 2889 goto out; 2890 2891 /* Register keys */ 2892 if (o1 != NULL) { 2893 o1->busy = true; 2894 cs->key1 = (vaddr_t)o1; 2895 } 2896 if (o2 != NULL) { 2897 o2->busy = true; 2898 cs->key2 = (vaddr_t)o2; 2899 } 2900 2901 out: 2902 if (res != TEE_SUCCESS) 2903 cryp_state_free(utc, cs); 2904 return res; 2905 } 2906 2907 TEE_Result syscall_cryp_state_copy(unsigned long dst, unsigned long src) 2908 { 2909 struct ts_session *sess = ts_get_current_session(); 2910 TEE_Result res = TEE_SUCCESS; 2911 struct tee_cryp_state *cs_dst = NULL; 2912 struct tee_cryp_state *cs_src = NULL; 2913 2914 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(dst), &cs_dst); 2915 if (res != TEE_SUCCESS) 2916 return res; 2917 2918 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(src), &cs_src); 2919 if (res != TEE_SUCCESS) 2920 return res; 2921 if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode) 2922 return TEE_ERROR_BAD_PARAMETERS; 2923 2924 switch (TEE_ALG_GET_CLASS(cs_src->algo)) { 2925 case TEE_OPERATION_CIPHER: 2926 crypto_cipher_copy_state(cs_dst->ctx, cs_src->ctx); 2927 break; 2928 case TEE_OPERATION_AE: 2929 crypto_authenc_copy_state(cs_dst->ctx, cs_src->ctx); 2930 break; 2931 case TEE_OPERATION_DIGEST: 2932 crypto_hash_copy_state(cs_dst->ctx, cs_src->ctx); 2933 break; 2934 case TEE_OPERATION_MAC: 2935 crypto_mac_copy_state(cs_dst->ctx, cs_src->ctx); 2936 break; 2937 default: 2938 return TEE_ERROR_BAD_STATE; 2939 } 2940 2941 cs_dst->state = cs_src->state; 2942 cs_dst->ctx_finalize = cs_src->ctx_finalize; 2943 2944 return TEE_SUCCESS; 2945 } 2946 2947 void tee_svc_cryp_free_states(struct user_ta_ctx *utc) 2948 { 2949 struct tee_cryp_state_head *states = &utc->cryp_states; 2950 2951 while (!TAILQ_EMPTY(states)) 2952 cryp_state_free(utc, TAILQ_FIRST(states)); 2953 } 2954 2955 TEE_Result syscall_cryp_state_free(unsigned long state) 2956 { 2957 struct ts_session *sess = ts_get_current_session(); 2958 TEE_Result res = TEE_SUCCESS; 2959 struct tee_cryp_state *cs = NULL; 2960 2961 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2962 if (res != TEE_SUCCESS) 2963 return res; 2964 cryp_state_free(to_user_ta_ctx(sess->ctx), cs); 2965 return TEE_SUCCESS; 2966 } 2967 2968 TEE_Result syscall_hash_init(unsigned long state, 2969 const void *iv __maybe_unused, 2970 size_t iv_len __maybe_unused) 2971 { 2972 struct ts_session *sess = ts_get_current_session(); 2973 TEE_Result res = TEE_SUCCESS; 2974 struct tee_cryp_state *cs = NULL; 2975 2976 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2977 if (res != TEE_SUCCESS) 2978 return res; 2979 2980 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2981 case TEE_OPERATION_DIGEST: 2982 res = crypto_hash_init(cs->ctx); 2983 if (res != TEE_SUCCESS) 2984 return res; 2985 break; 2986 case TEE_OPERATION_MAC: 2987 { 2988 struct tee_obj *o; 2989 struct tee_cryp_obj_secret *key; 2990 2991 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 2992 cs->key1, &o); 2993 if (res != TEE_SUCCESS) 2994 return res; 2995 if ((o->info.handleFlags & 2996 TEE_HANDLE_FLAG_INITIALIZED) == 0) 2997 return TEE_ERROR_BAD_PARAMETERS; 2998 2999 key = (struct tee_cryp_obj_secret *)o->attr; 3000 res = crypto_mac_init(cs->ctx, (void *)(key + 1), 3001 key->key_size); 3002 if (res != TEE_SUCCESS) 3003 return res; 3004 break; 3005 } 3006 default: 3007 return TEE_ERROR_BAD_PARAMETERS; 3008 } 3009 3010 cs->state = CRYP_STATE_INITIALIZED; 3011 3012 return TEE_SUCCESS; 3013 } 3014 3015 TEE_Result syscall_hash_update(unsigned long state, const void *chunk, 3016 size_t chunk_size) 3017 { 3018 struct ts_session *sess = ts_get_current_session(); 3019 struct tee_cryp_state *cs = NULL; 3020 TEE_Result res = TEE_SUCCESS; 3021 3022 /* No data, but size provided isn't valid parameters. */ 3023 if (!chunk && chunk_size) 3024 return TEE_ERROR_BAD_PARAMETERS; 3025 3026 /* Zero length hash is valid, but nothing we need to do. */ 3027 if (!chunk_size) 3028 return TEE_SUCCESS; 3029 3030 chunk = memtag_strip_tag_const(chunk); 3031 3032 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3033 TEE_MEMORY_ACCESS_READ | 3034 TEE_MEMORY_ACCESS_ANY_OWNER, 3035 (uaddr_t)chunk, chunk_size); 3036 if (res != TEE_SUCCESS) 3037 return res; 3038 3039 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3040 if (res != TEE_SUCCESS) 3041 return res; 3042 3043 if (cs->state != CRYP_STATE_INITIALIZED) 3044 return TEE_ERROR_BAD_STATE; 3045 3046 switch (TEE_ALG_GET_CLASS(cs->algo)) { 3047 case TEE_OPERATION_DIGEST: 3048 enter_user_access(); 3049 res = crypto_hash_update(cs->ctx, chunk, chunk_size); 3050 exit_user_access(); 3051 if (res != TEE_SUCCESS) 3052 return res; 3053 break; 3054 case TEE_OPERATION_MAC: 3055 enter_user_access(); 3056 res = crypto_mac_update(cs->ctx, chunk, chunk_size); 3057 exit_user_access(); 3058 if (res != TEE_SUCCESS) 3059 return res; 3060 break; 3061 default: 3062 return TEE_ERROR_BAD_PARAMETERS; 3063 } 3064 3065 return TEE_SUCCESS; 3066 } 3067 3068 static bool is_xof_algo(uint32_t algo) 3069 { 3070 return algo == TEE_ALG_SHAKE128 || algo == TEE_ALG_SHAKE256; 3071 } 3072 3073 TEE_Result syscall_hash_final(unsigned long state, const void *chunk, 3074 size_t chunk_size, void *hash, uint64_t *hash_len) 3075 { 3076 struct ts_session *sess = ts_get_current_session(); 3077 struct tee_cryp_state *cs = NULL; 3078 TEE_Result res2 = TEE_SUCCESS; 3079 TEE_Result res = TEE_SUCCESS; 3080 size_t hash_size = 0; 3081 size_t hlen = 0; 3082 3083 /* No data, but size provided isn't valid parameters. */ 3084 if (!chunk && chunk_size) 3085 return TEE_ERROR_BAD_PARAMETERS; 3086 3087 chunk = memtag_strip_tag_const(chunk); 3088 hash = memtag_strip_tag(hash); 3089 3090 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3091 TEE_MEMORY_ACCESS_READ | 3092 TEE_MEMORY_ACCESS_ANY_OWNER, 3093 (uaddr_t)chunk, chunk_size); 3094 if (res != TEE_SUCCESS) 3095 return res; 3096 3097 res = get_user_u64_as_size_t(&hlen, hash_len); 3098 if (res != TEE_SUCCESS) 3099 return res; 3100 3101 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3102 TEE_MEMORY_ACCESS_READ | 3103 TEE_MEMORY_ACCESS_WRITE | 3104 TEE_MEMORY_ACCESS_ANY_OWNER, 3105 (uaddr_t)hash, hlen); 3106 if (res != TEE_SUCCESS) 3107 return res; 3108 3109 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3110 if (res != TEE_SUCCESS) 3111 return res; 3112 3113 if (cs->state != CRYP_STATE_INITIALIZED) 3114 return TEE_ERROR_BAD_STATE; 3115 3116 switch (TEE_ALG_GET_CLASS(cs->algo)) { 3117 case TEE_OPERATION_DIGEST: 3118 if (is_xof_algo(cs->algo)) { 3119 if (chunk_size) { 3120 enter_user_access(); 3121 res = crypto_hash_update(cs->ctx, chunk, 3122 chunk_size); 3123 exit_user_access(); 3124 if (res) 3125 return res; 3126 } 3127 3128 /* 3129 * hash_size is supposed to be unchanged for XOF 3130 * algorithms so return directly. 3131 */ 3132 enter_user_access(); 3133 res = crypto_hash_final(cs->ctx, hash, hlen); 3134 exit_user_access(); 3135 return res; 3136 } 3137 3138 res = tee_alg_get_digest_size(cs->algo, &hash_size); 3139 if (res != TEE_SUCCESS) 3140 return res; 3141 if (hlen < hash_size) { 3142 res = TEE_ERROR_SHORT_BUFFER; 3143 goto out; 3144 } 3145 3146 if (chunk_size) { 3147 enter_user_access(); 3148 res = crypto_hash_update(cs->ctx, chunk, chunk_size); 3149 exit_user_access(); 3150 if (res != TEE_SUCCESS) 3151 return res; 3152 } 3153 3154 enter_user_access(); 3155 res = crypto_hash_final(cs->ctx, hash, hash_size); 3156 exit_user_access(); 3157 if (res != TEE_SUCCESS) 3158 return res; 3159 break; 3160 3161 case TEE_OPERATION_MAC: 3162 res = tee_alg_get_digest_size(cs->algo, &hash_size); 3163 if (res != TEE_SUCCESS) 3164 return res; 3165 if (hlen < hash_size) { 3166 res = TEE_ERROR_SHORT_BUFFER; 3167 goto out; 3168 } 3169 3170 if (chunk_size) { 3171 enter_user_access(); 3172 res = crypto_mac_update(cs->ctx, chunk, chunk_size); 3173 exit_user_access(); 3174 if (res != TEE_SUCCESS) 3175 return res; 3176 } 3177 3178 enter_user_access(); 3179 res = crypto_mac_final(cs->ctx, hash, hash_size); 3180 exit_user_access(); 3181 if (res != TEE_SUCCESS) 3182 return res; 3183 break; 3184 3185 default: 3186 return TEE_ERROR_BAD_PARAMETERS; 3187 } 3188 out: 3189 res2 = put_user_u64(hash_len, hash_size); 3190 if (res2 != TEE_SUCCESS) 3191 return res2; 3192 return res; 3193 } 3194 3195 TEE_Result syscall_cipher_init(unsigned long state, const void *iv, 3196 size_t iv_len) 3197 { 3198 struct ts_session *sess = ts_get_current_session(); 3199 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 3200 struct tee_cryp_obj_secret *key1 = NULL; 3201 struct tee_cryp_state *cs = NULL; 3202 TEE_Result res = TEE_SUCCESS; 3203 struct tee_obj *o = NULL; 3204 void *iv_bbuf = NULL; 3205 3206 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3207 if (res != TEE_SUCCESS) 3208 return res; 3209 3210 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_CIPHER) 3211 return TEE_ERROR_BAD_STATE; 3212 3213 res = tee_obj_get(utc, cs->key1, &o); 3214 if (res != TEE_SUCCESS) 3215 return res; 3216 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 3217 return TEE_ERROR_BAD_PARAMETERS; 3218 3219 key1 = o->attr; 3220 3221 res = bb_memdup_user(iv, iv_len, &iv_bbuf); 3222 if (res) 3223 return res; 3224 3225 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) { 3226 struct tee_cryp_obj_secret *key2 = o->attr; 3227 3228 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 3229 return TEE_ERROR_BAD_PARAMETERS; 3230 3231 res = crypto_cipher_init(cs->ctx, cs->mode, 3232 (uint8_t *)(key1 + 1), key1->key_size, 3233 (uint8_t *)(key2 + 1), key2->key_size, 3234 iv_bbuf, iv_len); 3235 } else { 3236 res = crypto_cipher_init(cs->ctx, cs->mode, 3237 (uint8_t *)(key1 + 1), key1->key_size, 3238 NULL, 0, iv_bbuf, iv_len); 3239 } 3240 if (res != TEE_SUCCESS) 3241 return res; 3242 3243 cs->ctx_finalize = crypto_cipher_final; 3244 cs->state = CRYP_STATE_INITIALIZED; 3245 3246 return TEE_SUCCESS; 3247 } 3248 3249 static TEE_Result tee_svc_cipher_update_helper(unsigned long state, 3250 bool last_block, const void *src, size_t src_len, 3251 void *dst, uint64_t *dst_len) 3252 { 3253 struct ts_session *sess = ts_get_current_session(); 3254 struct tee_cryp_state *cs = NULL; 3255 TEE_Result res = TEE_SUCCESS; 3256 size_t dlen = 0; 3257 3258 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3259 if (res != TEE_SUCCESS) 3260 return res; 3261 3262 if (cs->state != CRYP_STATE_INITIALIZED) 3263 return TEE_ERROR_BAD_STATE; 3264 3265 src = memtag_strip_tag_const(src); 3266 dst = memtag_strip_tag(dst); 3267 3268 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3269 TEE_MEMORY_ACCESS_READ | 3270 TEE_MEMORY_ACCESS_ANY_OWNER, 3271 (uaddr_t)src, src_len); 3272 if (res != TEE_SUCCESS) 3273 return res; 3274 3275 if (!dst_len) { 3276 dlen = 0; 3277 } else { 3278 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 3279 uint32_t flags = TEE_MEMORY_ACCESS_READ | 3280 TEE_MEMORY_ACCESS_WRITE | 3281 TEE_MEMORY_ACCESS_ANY_OWNER; 3282 3283 res = get_user_u64_as_size_t(&dlen, dst_len); 3284 if (res != TEE_SUCCESS) 3285 return res; 3286 3287 res = vm_check_access_rights(uctx, flags, (uaddr_t)dst, dlen); 3288 if (res != TEE_SUCCESS) 3289 return res; 3290 } 3291 3292 if (dlen < src_len) { 3293 res = TEE_ERROR_SHORT_BUFFER; 3294 goto out; 3295 } 3296 3297 if (src_len > 0) { 3298 /* Permit src_len == 0 to finalize the operation */ 3299 enter_user_access(); 3300 res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode, 3301 last_block, src, src_len, dst); 3302 exit_user_access(); 3303 } 3304 3305 if (last_block && cs->ctx_finalize != NULL) { 3306 cs->ctx_finalize(cs->ctx); 3307 cs->ctx_finalize = NULL; 3308 } 3309 3310 out: 3311 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 3312 dst_len != NULL) { 3313 TEE_Result res2; 3314 3315 res2 = put_user_u64(dst_len, src_len); 3316 if (res2 != TEE_SUCCESS) 3317 res = res2; 3318 } 3319 3320 return res; 3321 } 3322 3323 TEE_Result syscall_cipher_update(unsigned long state, const void *src, 3324 size_t src_len, void *dst, uint64_t *dst_len) 3325 { 3326 return tee_svc_cipher_update_helper(state, false /* last_block */, 3327 src, src_len, dst, dst_len); 3328 } 3329 3330 TEE_Result syscall_cipher_final(unsigned long state, const void *src, 3331 size_t src_len, void *dst, uint64_t *dst_len) 3332 { 3333 return tee_svc_cipher_update_helper(state, true /* last_block */, 3334 src, src_len, dst, dst_len); 3335 } 3336 3337 #if defined(CFG_CRYPTO_HKDF) 3338 static TEE_Result get_hkdf_params(uint32_t algo, const TEE_Attribute *params, 3339 uint32_t param_count, 3340 void **salt, size_t *salt_len, void **info, 3341 size_t *info_len, size_t *okm_len, 3342 uint32_t *hash_id) 3343 { 3344 TEE_Result res = TEE_SUCCESS; 3345 size_t n; 3346 enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4, HASH = 0x8 }; 3347 uint8_t found = 0; 3348 3349 *salt = *info = NULL; 3350 *salt_len = *info_len = *okm_len = 0; 3351 3352 if (algo == TEE_ALG_HKDF) { 3353 *hash_id = TEE_ALG_SHA256; 3354 } else { 3355 *hash_id = TEE_ALG_GET_DIGEST_HASH(algo); 3356 found |= HASH; 3357 } 3358 3359 for (n = 0; n < param_count; n++) { 3360 const TEE_Attribute *p = ¶ms[n]; 3361 3362 switch (p->attributeID) { 3363 case __OPTEE_TEE_ATTR_HKDF_SALT: 3364 case TEE_ATTR_HKDF_SALT: 3365 if (!(found & SALT)) { 3366 *salt_len = p->content.ref.length; 3367 res = bb_memdup_user(p->content.ref.buffer, 3368 *salt_len, salt); 3369 if (res) 3370 return res; 3371 3372 found |= SALT; 3373 } 3374 break; 3375 case TEE_ATTR_KDF_KEY_SIZE: 3376 case TEE_ATTR_HKDF_OKM_LENGTH: 3377 if (!(found & LENGTH)) { 3378 *okm_len = p->content.value.a; 3379 found |= LENGTH; 3380 } 3381 break; 3382 case __OPTEE_ATTR_HKDF_INFO: 3383 case TEE_ATTR_HKDF_INFO: 3384 if (!(found & INFO)) { 3385 *info_len = p->content.ref.length; 3386 res = bb_memdup_user(p->content.ref.buffer, 3387 *info_len, info); 3388 if (res) 3389 return res; 3390 3391 found |= INFO; 3392 } 3393 break; 3394 case TEE_ATTR_HKDF_HASH_ALGORITHM: 3395 if (!(found & HASH)) { 3396 *hash_id = p->content.value.a; 3397 found |= HASH; 3398 } 3399 break; 3400 default: 3401 /* Unexpected attribute */ 3402 return TEE_ERROR_BAD_PARAMETERS; 3403 } 3404 3405 } 3406 3407 if (!(found & LENGTH)) 3408 return TEE_ERROR_BAD_PARAMETERS; 3409 3410 return TEE_SUCCESS; 3411 } 3412 #endif 3413 3414 #if defined(CFG_CRYPTO_CONCAT_KDF) 3415 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params, 3416 uint32_t param_count, 3417 void **other_info, 3418 size_t *other_info_len, 3419 size_t *derived_key_len) 3420 { 3421 size_t n; 3422 enum { LENGTH = 0x1, INFO = 0x2 }; 3423 uint8_t found = 0; 3424 3425 *other_info = NULL; 3426 *other_info_len = *derived_key_len = 0; 3427 3428 for (n = 0; n < param_count; n++) { 3429 const TEE_Attribute *p = ¶ms[n]; 3430 3431 switch (p->attributeID) { 3432 case TEE_ATTR_CONCAT_KDF_OTHER_INFO: 3433 if (!(found & INFO)) { 3434 TEE_Result res = TEE_SUCCESS; 3435 3436 *other_info_len = p->content.ref.length; 3437 res = bb_memdup_user(p->content.ref.buffer, 3438 *other_info_len, 3439 other_info); 3440 if (res) 3441 return res; 3442 3443 found |= INFO; 3444 } 3445 break; 3446 case TEE_ATTR_CONCAT_KDF_DKM_LENGTH: 3447 if (!(found & LENGTH)) { 3448 *derived_key_len = p->content.value.a; 3449 found |= LENGTH; 3450 } 3451 break; 3452 default: 3453 /* Unexpected attribute */ 3454 return TEE_ERROR_BAD_PARAMETERS; 3455 } 3456 } 3457 3458 if (!(found & LENGTH)) 3459 return TEE_ERROR_BAD_PARAMETERS; 3460 3461 return TEE_SUCCESS; 3462 } 3463 #endif 3464 3465 #if defined(CFG_CRYPTO_PBKDF2) 3466 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params, 3467 uint32_t param_count, void **salt, 3468 size_t *salt_len, size_t *derived_key_len, 3469 size_t *iteration_count) 3470 { 3471 size_t n; 3472 enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 }; 3473 uint8_t found = 0; 3474 3475 *salt = NULL; 3476 *salt_len = *derived_key_len = *iteration_count = 0; 3477 3478 for (n = 0; n < param_count; n++) { 3479 const TEE_Attribute *p = ¶ms[n]; 3480 3481 switch (p->attributeID) { 3482 case TEE_ATTR_PBKDF2_SALT: 3483 if (!(found & SALT)) { 3484 TEE_Result res = TEE_SUCCESS; 3485 3486 *salt_len = p->content.ref.length; 3487 res = bb_memdup_user(p->content.ref.buffer, 3488 *salt_len, salt); 3489 if (res) 3490 return res; 3491 3492 found |= SALT; 3493 } 3494 break; 3495 case TEE_ATTR_PBKDF2_DKM_LENGTH: 3496 if (!(found & LENGTH)) { 3497 *derived_key_len = p->content.value.a; 3498 found |= LENGTH; 3499 } 3500 break; 3501 case TEE_ATTR_PBKDF2_ITERATION_COUNT: 3502 if (!(found & COUNT)) { 3503 *iteration_count = p->content.value.a; 3504 found |= COUNT; 3505 } 3506 break; 3507 default: 3508 /* Unexpected attribute */ 3509 return TEE_ERROR_BAD_PARAMETERS; 3510 } 3511 } 3512 3513 if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT)) 3514 return TEE_ERROR_BAD_PARAMETERS; 3515 3516 return TEE_SUCCESS; 3517 } 3518 #endif 3519 3520 #if defined(CFG_CRYPTO_SM2_KEP) 3521 static TEE_Result get_sm2_kep_params(const TEE_Attribute *params, 3522 uint32_t param_count, 3523 struct ecc_public_key *peer_key, 3524 struct ecc_public_key *peer_eph_key, 3525 struct sm2_kep_parms *kep_parms) 3526 { 3527 TEE_Result res = TEE_ERROR_GENERIC; 3528 size_t n; 3529 enum { 3530 IS_INITIATOR, 3531 PEER_KEY_X, 3532 PEER_KEY_Y, 3533 PEER_EPH_KEY_X, 3534 PEER_EPH_KEY_Y, 3535 INITIATOR_ID, 3536 RESPONDER_ID, 3537 }; 3538 uint8_t mandatory = BIT(IS_INITIATOR) | BIT(PEER_KEY_X) | 3539 BIT(PEER_KEY_Y) | BIT(PEER_EPH_KEY_X) | BIT(PEER_EPH_KEY_Y) | 3540 BIT(INITIATOR_ID) | BIT(RESPONDER_ID); 3541 uint8_t found = 0; 3542 3543 res = crypto_acipher_alloc_ecc_public_key(peer_key, 3544 TEE_TYPE_SM2_KEP_PUBLIC_KEY, 3545 256); 3546 if (res) 3547 return res; 3548 3549 res = crypto_acipher_alloc_ecc_public_key(peer_eph_key, 3550 TEE_TYPE_SM2_KEP_PUBLIC_KEY, 3551 256); 3552 if (res) 3553 goto out_p; 3554 3555 peer_key->curve = TEE_ECC_CURVE_SM2; 3556 peer_eph_key->curve = TEE_ECC_CURVE_SM2; 3557 3558 for (n = 0; n < param_count; n++) { 3559 const TEE_Attribute *p = ¶ms[n]; 3560 void *bbuf = NULL; 3561 3562 switch (p->attributeID) { 3563 case TEE_ATTR_SM2_KEP_USER: 3564 kep_parms->is_initiator = !p->content.value.a; 3565 found |= BIT(IS_INITIATOR); 3566 break; 3567 case TEE_ATTR_ECC_PUBLIC_VALUE_X: 3568 res = bb_memdup_user(p->content.ref.buffer, 3569 p->content.ref.length, 3570 &bbuf); 3571 if (res) 3572 return res; 3573 3574 crypto_bignum_bin2bn(bbuf, 3575 p->content.ref.length, 3576 peer_key->x); 3577 found |= BIT(PEER_KEY_X); 3578 bb_free(bbuf, p->content.ref.length); 3579 break; 3580 case TEE_ATTR_ECC_PUBLIC_VALUE_Y: 3581 res = bb_memdup_user(p->content.ref.buffer, 3582 p->content.ref.length, 3583 &bbuf); 3584 if (res) 3585 return res; 3586 3587 crypto_bignum_bin2bn(bbuf, 3588 p->content.ref.length, 3589 peer_key->y); 3590 found |= BIT(PEER_KEY_Y); 3591 bb_free(bbuf, p->content.ref.length); 3592 break; 3593 case __OPTEE_SM2_KEP_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_X: 3594 case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_X: 3595 res = bb_memdup_user(p->content.ref.buffer, 3596 p->content.ref.length, 3597 &bbuf); 3598 if (res) 3599 return res; 3600 3601 crypto_bignum_bin2bn(bbuf, 3602 p->content.ref.length, 3603 peer_eph_key->x); 3604 found |= BIT(PEER_EPH_KEY_X); 3605 bb_free(bbuf, p->content.ref.length); 3606 break; 3607 case __OPTEE_SM2_KEP_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_Y: 3608 case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_Y: 3609 res = bb_memdup_user(p->content.ref.buffer, 3610 p->content.ref.length, 3611 &bbuf); 3612 if (res) 3613 return res; 3614 3615 crypto_bignum_bin2bn(bbuf, 3616 p->content.ref.length, 3617 peer_eph_key->y); 3618 found |= BIT(PEER_EPH_KEY_Y); 3619 bb_free(bbuf, p->content.ref.length); 3620 break; 3621 case TEE_ATTR_SM2_ID_INITIATOR: 3622 res = bb_memdup_user(p->content.ref.buffer, 3623 p->content.ref.length, 3624 &bbuf); 3625 if (res) 3626 return res; 3627 3628 kep_parms->initiator_id = bbuf; 3629 kep_parms->initiator_id_len = p->content.ref.length; 3630 found |= BIT(INITIATOR_ID); 3631 break; 3632 case TEE_ATTR_SM2_ID_RESPONDER: 3633 res = bb_memdup_user(p->content.ref.buffer, 3634 p->content.ref.length, 3635 &bbuf); 3636 if (res) 3637 return res; 3638 3639 kep_parms->responder_id = bbuf; 3640 kep_parms->responder_id_len = p->content.ref.length; 3641 found |= BIT(RESPONDER_ID); 3642 break; 3643 case TEE_ATTR_SM2_KEP_CONFIRMATION_IN: 3644 res = bb_memdup_user(p->content.ref.buffer, 3645 p->content.ref.length, 3646 &bbuf); 3647 if (res) 3648 return res; 3649 3650 kep_parms->conf_in = bbuf; 3651 kep_parms->conf_in_len = p->content.ref.length; 3652 break; 3653 case TEE_ATTR_SM2_KEP_CONFIRMATION_OUT: 3654 res = bb_memdup_user(p->content.ref.buffer, 3655 p->content.ref.length, 3656 &bbuf); 3657 if (res) 3658 return res; 3659 3660 kep_parms->conf_out = bbuf; 3661 kep_parms->conf_out_len = p->content.ref.length; 3662 break; 3663 default: 3664 /* Unexpected attribute */ 3665 res = TEE_ERROR_BAD_PARAMETERS; 3666 goto out; 3667 } 3668 } 3669 3670 if ((found & mandatory) != mandatory) { 3671 res = TEE_ERROR_BAD_PARAMETERS; 3672 goto out; 3673 } 3674 3675 return TEE_SUCCESS; 3676 out: 3677 crypto_acipher_free_ecc_public_key(peer_eph_key); 3678 out_p: 3679 crypto_acipher_free_ecc_public_key(peer_key); 3680 return res; 3681 } 3682 #endif 3683 3684 TEE_Result syscall_cryp_derive_key(unsigned long state, 3685 const struct utee_attribute *usr_params, 3686 unsigned long param_count, unsigned long derived_key) 3687 { 3688 struct ts_session *sess = ts_get_current_session(); 3689 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 3690 TEE_Result res = TEE_ERROR_NOT_SUPPORTED; 3691 struct tee_obj *ko = NULL; 3692 struct tee_obj *so = NULL; 3693 struct tee_cryp_state *cs = NULL; 3694 struct tee_cryp_obj_secret *sk = NULL; 3695 const struct tee_cryp_obj_type_props *type_props = NULL; 3696 TEE_Attribute *params = NULL; 3697 size_t alloc_size = 0; 3698 3699 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3700 if (res != TEE_SUCCESS) 3701 return res; 3702 3703 if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size)) 3704 return TEE_ERROR_OVERFLOW; 3705 3706 params = malloc(alloc_size); 3707 if (!params) 3708 return TEE_ERROR_OUT_OF_MEMORY; 3709 res = copy_in_attrs(utc, usr_params, param_count, params); 3710 if (res != TEE_SUCCESS) 3711 goto out; 3712 3713 /* Get key set in operation */ 3714 res = tee_obj_get(utc, cs->key1, &ko); 3715 if (res != TEE_SUCCESS) 3716 goto out; 3717 3718 res = tee_obj_get(utc, uref_to_vaddr(derived_key), &so); 3719 if (res != TEE_SUCCESS) 3720 goto out; 3721 3722 /* Find information needed about the object to initialize */ 3723 sk = so->attr; 3724 3725 /* Find description of object */ 3726 type_props = tee_svc_find_type_props(so->info.objectType); 3727 if (!type_props) { 3728 res = TEE_ERROR_NOT_SUPPORTED; 3729 goto out; 3730 } 3731 3732 if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) { 3733 struct bignum *pub = NULL; 3734 struct bignum *ss = NULL; 3735 size_t bin_size = 0; 3736 void *bbuf = NULL; 3737 3738 if (param_count != 1 || 3739 params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE) { 3740 res = TEE_ERROR_BAD_PARAMETERS; 3741 goto out; 3742 } 3743 3744 bin_size = params[0].content.ref.length; 3745 3746 if (MUL_OVERFLOW(bin_size, 8, &alloc_size)) { 3747 res = TEE_ERROR_OVERFLOW; 3748 goto out; 3749 } 3750 3751 res = bb_memdup_user(params[0].content.ref.buffer, bin_size, 3752 &bbuf); 3753 if (res) 3754 goto out; 3755 3756 pub = crypto_bignum_allocate(alloc_size); 3757 ss = crypto_bignum_allocate(alloc_size); 3758 if (pub && ss) { 3759 crypto_bignum_bin2bn(bbuf, bin_size, pub); 3760 res = crypto_acipher_dh_shared_secret(ko->attr, 3761 pub, ss); 3762 if (res == TEE_SUCCESS) { 3763 sk->key_size = crypto_bignum_num_bytes(ss); 3764 crypto_bignum_bn2bin(ss, (uint8_t *)(sk + 1)); 3765 so->info.handleFlags |= 3766 TEE_HANDLE_FLAG_INITIALIZED; 3767 set_attribute(so, type_props, 3768 TEE_ATTR_SECRET_VALUE); 3769 } 3770 } else { 3771 res = TEE_ERROR_OUT_OF_MEMORY; 3772 } 3773 crypto_bignum_free(&pub); 3774 crypto_bignum_free(&ss); 3775 } else if (cs->algo == TEE_ALG_ECDH_DERIVE_SHARED_SECRET) { 3776 uint32_t curve = ((struct ecc_keypair *)ko->attr)->curve; 3777 struct ecc_public_key key_public = { }; 3778 uint8_t *pt_secret = NULL; 3779 unsigned long pt_secret_len = 0; 3780 uint32_t key_type = TEE_TYPE_ECDH_PUBLIC_KEY; 3781 void *x_bbuf = NULL; 3782 void *y_bbuf = NULL; 3783 3784 if (param_count != 2 || 3785 params[0].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_X || 3786 params[1].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_Y) { 3787 res = TEE_ERROR_BAD_PARAMETERS; 3788 goto out; 3789 } 3790 3791 switch (curve) { 3792 case TEE_ECC_CURVE_NIST_P192: 3793 alloc_size = 192; 3794 break; 3795 case TEE_ECC_CURVE_NIST_P224: 3796 alloc_size = 224; 3797 break; 3798 case TEE_ECC_CURVE_NIST_P256: 3799 alloc_size = 256; 3800 break; 3801 case TEE_ECC_CURVE_NIST_P384: 3802 alloc_size = 384; 3803 break; 3804 case TEE_ECC_CURVE_NIST_P521: 3805 alloc_size = 521; 3806 break; 3807 default: 3808 res = TEE_ERROR_NOT_IMPLEMENTED; 3809 goto out; 3810 } 3811 3812 res = bb_memdup_user(params[0].content.ref.buffer, 3813 params[0].content.ref.length, 3814 &x_bbuf); 3815 if (res) 3816 goto out; 3817 3818 res = bb_memdup_user(params[1].content.ref.buffer, 3819 params[1].content.ref.length, 3820 &y_bbuf); 3821 if (res) 3822 goto out; 3823 3824 /* Create the public key */ 3825 res = crypto_acipher_alloc_ecc_public_key(&key_public, key_type, 3826 alloc_size); 3827 if (res != TEE_SUCCESS) 3828 goto out; 3829 key_public.curve = curve; 3830 crypto_bignum_bin2bn(x_bbuf, params[0].content.ref.length, 3831 key_public.x); 3832 crypto_bignum_bin2bn(y_bbuf, params[1].content.ref.length, 3833 key_public.y); 3834 3835 pt_secret = (uint8_t *)(sk + 1); 3836 pt_secret_len = sk->alloc_size; 3837 res = crypto_acipher_ecc_shared_secret(ko->attr, &key_public, 3838 pt_secret, 3839 &pt_secret_len); 3840 3841 if (res == TEE_SUCCESS) { 3842 sk->key_size = pt_secret_len; 3843 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3844 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3845 } 3846 3847 /* free the public key */ 3848 crypto_acipher_free_ecc_public_key(&key_public); 3849 } 3850 #if defined(CFG_CRYPTO_HKDF) 3851 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) { 3852 void *salt, *info; 3853 size_t salt_len, info_len, okm_len; 3854 uint32_t hash_id = 0; 3855 struct tee_cryp_obj_secret *ik = ko->attr; 3856 const uint8_t *ikm = (const uint8_t *)(ik + 1); 3857 3858 res = get_hkdf_params(cs->algo, params, param_count, &salt, 3859 &salt_len, &info, &info_len, &okm_len, 3860 &hash_id); 3861 if (res != TEE_SUCCESS) 3862 goto out; 3863 3864 /* Requested size must fit into the output object's buffer */ 3865 if (okm_len > ik->alloc_size) { 3866 res = TEE_ERROR_BAD_PARAMETERS; 3867 goto out; 3868 } 3869 3870 res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len, 3871 info, info_len, (uint8_t *)(sk + 1), 3872 okm_len); 3873 if (res == TEE_SUCCESS) { 3874 sk->key_size = okm_len; 3875 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3876 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3877 } 3878 } 3879 #endif 3880 #if defined(CFG_CRYPTO_CONCAT_KDF) 3881 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) { 3882 void *info; 3883 size_t info_len, derived_key_len; 3884 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3885 struct tee_cryp_obj_secret *ss = ko->attr; 3886 const uint8_t *shared_secret = (const uint8_t *)(ss + 1); 3887 3888 res = get_concat_kdf_params(params, param_count, &info, 3889 &info_len, &derived_key_len); 3890 if (res != TEE_SUCCESS) 3891 goto out; 3892 3893 /* Requested size must fit into the output object's buffer */ 3894 if (derived_key_len > ss->alloc_size) { 3895 res = TEE_ERROR_BAD_PARAMETERS; 3896 goto out; 3897 } 3898 3899 res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size, 3900 info, info_len, (uint8_t *)(sk + 1), 3901 derived_key_len); 3902 if (res == TEE_SUCCESS) { 3903 sk->key_size = derived_key_len; 3904 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3905 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3906 } 3907 } 3908 #endif 3909 #if defined(CFG_CRYPTO_PBKDF2) 3910 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) { 3911 void *salt; 3912 size_t salt_len, iteration_count, derived_key_len; 3913 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3914 struct tee_cryp_obj_secret *ss = ko->attr; 3915 const uint8_t *password = (const uint8_t *)(ss + 1); 3916 3917 res = get_pbkdf2_params(params, param_count, &salt, &salt_len, 3918 &derived_key_len, &iteration_count); 3919 if (res != TEE_SUCCESS) 3920 goto out; 3921 3922 /* Requested size must fit into the output object's buffer */ 3923 if (derived_key_len > ss->alloc_size) { 3924 res = TEE_ERROR_BAD_PARAMETERS; 3925 goto out; 3926 } 3927 3928 res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt, 3929 salt_len, iteration_count, 3930 (uint8_t *)(sk + 1), derived_key_len); 3931 if (res == TEE_SUCCESS) { 3932 sk->key_size = derived_key_len; 3933 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3934 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3935 } 3936 } 3937 #endif 3938 #if defined(CFG_CRYPTO_SM2_KEP) 3939 else if (cs->algo == TEE_ALG_SM2_KEP) { 3940 struct ecc_public_key peer_eph_key = { }; 3941 struct ecc_public_key peer_key = { }; 3942 struct sm2_kep_parms kep_parms = { 3943 .out = (uint8_t *)(sk + 1), 3944 .out_len = so->info.maxObjectSize, 3945 }; 3946 struct tee_obj *ko2 = NULL; 3947 3948 res = tee_obj_get(utc, cs->key2, &ko2); 3949 if (res != TEE_SUCCESS) 3950 goto out; 3951 3952 res = get_sm2_kep_params(params, param_count, &peer_key, 3953 &peer_eph_key, &kep_parms); 3954 if (res != TEE_SUCCESS) 3955 goto out; 3956 3957 /* 3958 * key1 is our private keypair, key2 is our ephemeral public key 3959 */ 3960 res = crypto_acipher_sm2_kep_derive(ko->attr, /* key1 */ 3961 ko2->attr, /* key2 */ 3962 &peer_key, &peer_eph_key, 3963 &kep_parms); 3964 3965 if (res == TEE_SUCCESS) { 3966 sk->key_size = kep_parms.out_len; 3967 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3968 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3969 } 3970 crypto_acipher_free_ecc_public_key(&peer_key); 3971 crypto_acipher_free_ecc_public_key(&peer_eph_key); 3972 } 3973 #endif 3974 #if defined(CFG_CRYPTO_X25519) 3975 else if (cs->algo == TEE_ALG_X25519) { 3976 uint8_t *x25519_pub_key = NULL; 3977 uint8_t *pt_secret = NULL; 3978 unsigned long pt_secret_len = 0; 3979 void *bbuf = NULL; 3980 3981 if (param_count != 1 || 3982 params[0].attributeID != TEE_ATTR_X25519_PUBLIC_VALUE) { 3983 res = TEE_ERROR_BAD_PARAMETERS; 3984 goto out; 3985 } 3986 3987 /* X25519 public key size is 32 bytes */ 3988 if (params[0].content.ref.length != KEY_SIZE_BYTES_25519) { 3989 res = TEE_ERROR_BAD_PARAMETERS; 3990 goto out; 3991 } 3992 3993 res = bb_memdup_user(params[0].content.ref.buffer, 3994 params[0].content.ref.length, 3995 &bbuf); 3996 if (res) 3997 goto out; 3998 3999 /* Set the public key */ 4000 x25519_pub_key = bbuf; 4001 4002 pt_secret = (uint8_t *)(sk + 1); 4003 pt_secret_len = sk->alloc_size; 4004 res = crypto_acipher_x25519_shared_secret(ko->attr, 4005 x25519_pub_key, 4006 pt_secret, 4007 &pt_secret_len); 4008 4009 if (res == TEE_SUCCESS) { 4010 sk->key_size = pt_secret_len; 4011 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 4012 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 4013 } 4014 } 4015 #endif 4016 #if defined(CFG_CRYPTO_X448) 4017 else if (cs->algo == TEE_ALG_X448) { 4018 uint8_t *x448_pub_key = NULL; 4019 uint8_t *pt_secret = NULL; 4020 unsigned long pt_secret_len = 0; 4021 void *bbuf = NULL; 4022 4023 if (param_count != 1 || 4024 params[0].attributeID != TEE_ATTR_X448_PUBLIC_VALUE) { 4025 res = TEE_ERROR_BAD_PARAMETERS; 4026 goto out; 4027 } 4028 4029 /* X448 public key size is 56 bytes */ 4030 if (params[0].content.ref.length != KEY_SIZE_BYTES_448) { 4031 res = TEE_ERROR_BAD_PARAMETERS; 4032 goto out; 4033 } 4034 4035 res = bb_memdup_user(params[0].content.ref.buffer, 4036 params[0].content.ref.length, 4037 &bbuf); 4038 if (res) 4039 goto out; 4040 4041 /* Set the public key */ 4042 x448_pub_key = bbuf; 4043 4044 pt_secret = (uint8_t *)(sk + 1); 4045 pt_secret_len = sk->alloc_size; 4046 res = crypto_acipher_x448_shared_secret(ko->attr, 4047 x448_pub_key, 4048 pt_secret, 4049 &pt_secret_len); 4050 4051 if (res == TEE_SUCCESS) { 4052 sk->key_size = pt_secret_len; 4053 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 4054 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 4055 } 4056 } 4057 #endif 4058 else 4059 res = TEE_ERROR_NOT_SUPPORTED; 4060 4061 out: 4062 free_wipe(params); 4063 return res; 4064 } 4065 4066 TEE_Result syscall_cryp_random_number_generate(void *buf, size_t blen) 4067 { 4068 TEE_Result res = TEE_SUCCESS; 4069 void *bbuf = NULL; 4070 4071 bbuf = bb_alloc(blen); 4072 if (!bbuf) 4073 return TEE_ERROR_OUT_OF_MEMORY; 4074 4075 res = crypto_rng_read(bbuf, blen); 4076 if (res != TEE_SUCCESS) 4077 return res; 4078 4079 res = copy_to_user(buf, bbuf, blen); 4080 return res; 4081 } 4082 4083 TEE_Result syscall_authenc_init(unsigned long state, const void *nonce, 4084 size_t nonce_len, size_t tag_len, 4085 size_t aad_len, size_t payload_len) 4086 { 4087 struct ts_session *sess = ts_get_current_session(); 4088 struct tee_cryp_obj_secret *key = NULL; 4089 struct tee_cryp_state *cs = NULL; 4090 TEE_Result res = TEE_SUCCESS; 4091 struct tee_obj *o = NULL; 4092 void *nonce_bbuf = NULL; 4093 4094 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4095 if (res != TEE_SUCCESS) 4096 return res; 4097 4098 res = tee_obj_get(to_user_ta_ctx(sess->ctx), cs->key1, &o); 4099 if (res != TEE_SUCCESS) 4100 return res; 4101 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 4102 return TEE_ERROR_BAD_PARAMETERS; 4103 4104 key = o->attr; 4105 4106 res = bb_memdup_user(nonce, nonce_len, &nonce_bbuf); 4107 if (res) 4108 return res; 4109 4110 res = crypto_authenc_init(cs->ctx, cs->mode, (uint8_t *)(key + 1), 4111 key->key_size, nonce_bbuf, nonce_len, tag_len, 4112 aad_len, payload_len); 4113 if (res != TEE_SUCCESS) 4114 return res; 4115 4116 cs->ctx_finalize = crypto_authenc_final; 4117 cs->state = CRYP_STATE_INITIALIZED; 4118 4119 return TEE_SUCCESS; 4120 } 4121 4122 TEE_Result syscall_authenc_update_aad(unsigned long state, 4123 const void *aad_data, size_t aad_data_len) 4124 { 4125 struct ts_session *sess = ts_get_current_session(); 4126 TEE_Result res = TEE_SUCCESS; 4127 struct tee_cryp_state *cs = NULL; 4128 4129 aad_data = memtag_strip_tag_const(aad_data); 4130 4131 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 4132 TEE_MEMORY_ACCESS_READ | 4133 TEE_MEMORY_ACCESS_ANY_OWNER, 4134 (uaddr_t)aad_data, aad_data_len); 4135 if (res != TEE_SUCCESS) 4136 return res; 4137 4138 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4139 if (res != TEE_SUCCESS) 4140 return res; 4141 4142 if (cs->state != CRYP_STATE_INITIALIZED) 4143 return TEE_ERROR_BAD_STATE; 4144 4145 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 4146 return TEE_ERROR_BAD_STATE; 4147 4148 enter_user_access(); 4149 res = crypto_authenc_update_aad(cs->ctx, cs->mode, aad_data, 4150 aad_data_len); 4151 exit_user_access(); 4152 if (res != TEE_SUCCESS) 4153 return res; 4154 4155 return TEE_SUCCESS; 4156 } 4157 4158 TEE_Result syscall_authenc_update_payload(unsigned long state, 4159 const void *src_data, 4160 size_t src_len, void *dst_data, 4161 uint64_t *dst_len) 4162 { 4163 struct ts_session *sess = ts_get_current_session(); 4164 struct tee_cryp_state *cs = NULL; 4165 TEE_Result res = TEE_SUCCESS; 4166 size_t dlen = 0; 4167 4168 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4169 if (res != TEE_SUCCESS) 4170 return res; 4171 4172 if (cs->state != CRYP_STATE_INITIALIZED) 4173 return TEE_ERROR_BAD_STATE; 4174 4175 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 4176 return TEE_ERROR_BAD_STATE; 4177 4178 src_data = memtag_strip_tag_const(src_data); 4179 dst_data = memtag_strip_tag(dst_data); 4180 4181 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 4182 TEE_MEMORY_ACCESS_READ | 4183 TEE_MEMORY_ACCESS_ANY_OWNER, 4184 (uaddr_t)src_data, src_len); 4185 if (res != TEE_SUCCESS) 4186 return res; 4187 4188 res = get_user_u64_as_size_t(&dlen, dst_len); 4189 if (res != TEE_SUCCESS) 4190 return res; 4191 4192 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 4193 TEE_MEMORY_ACCESS_READ | 4194 TEE_MEMORY_ACCESS_WRITE | 4195 TEE_MEMORY_ACCESS_ANY_OWNER, 4196 (uaddr_t)dst_data, dlen); 4197 if (res != TEE_SUCCESS) 4198 return res; 4199 4200 if (dlen < src_len) { 4201 res = TEE_ERROR_SHORT_BUFFER; 4202 goto out; 4203 } 4204 4205 enter_user_access(); 4206 res = crypto_authenc_update_payload(cs->ctx, cs->mode, src_data, 4207 src_len, dst_data, &dlen); 4208 exit_user_access(); 4209 out: 4210 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 4211 TEE_Result res2 = put_user_u64(dst_len, dlen); 4212 4213 if (res2 != TEE_SUCCESS) 4214 res = res2; 4215 } 4216 4217 return res; 4218 } 4219 4220 TEE_Result syscall_authenc_enc_final(unsigned long state, const void *src_data, 4221 size_t src_len, void *dst_data, 4222 uint64_t *dst_len, void *tag, 4223 uint64_t *tag_len) 4224 { 4225 struct ts_session *sess = ts_get_current_session(); 4226 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 4227 struct tee_cryp_state *cs = NULL; 4228 TEE_Result res = TEE_SUCCESS; 4229 size_t dlen = 0; 4230 size_t tlen = 0; 4231 4232 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4233 if (res != TEE_SUCCESS) 4234 return res; 4235 4236 if (cs->state != CRYP_STATE_INITIALIZED) 4237 return TEE_ERROR_BAD_STATE; 4238 4239 if (cs->mode != TEE_MODE_ENCRYPT) 4240 return TEE_ERROR_BAD_PARAMETERS; 4241 4242 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 4243 return TEE_ERROR_BAD_STATE; 4244 4245 src_data = memtag_strip_tag_const(src_data); 4246 dst_data = memtag_strip_tag(dst_data); 4247 tag = memtag_strip_tag(tag); 4248 4249 res = vm_check_access_rights(uctx, 4250 TEE_MEMORY_ACCESS_READ | 4251 TEE_MEMORY_ACCESS_ANY_OWNER, 4252 (uaddr_t)src_data, src_len); 4253 if (res != TEE_SUCCESS) 4254 return res; 4255 4256 if (!dst_len) { 4257 dlen = 0; 4258 } else { 4259 res = get_user_u64_as_size_t(&dlen, dst_len); 4260 if (res != TEE_SUCCESS) 4261 return res; 4262 4263 res = vm_check_access_rights(uctx, 4264 TEE_MEMORY_ACCESS_READ | 4265 TEE_MEMORY_ACCESS_WRITE | 4266 TEE_MEMORY_ACCESS_ANY_OWNER, 4267 (uaddr_t)dst_data, dlen); 4268 if (res != TEE_SUCCESS) 4269 return res; 4270 } 4271 4272 if (dlen < src_len) { 4273 res = TEE_ERROR_SHORT_BUFFER; 4274 goto out; 4275 } 4276 4277 res = get_user_u64_as_size_t(&tlen, tag_len); 4278 if (res != TEE_SUCCESS) 4279 return res; 4280 4281 res = vm_check_access_rights(uctx, 4282 TEE_MEMORY_ACCESS_READ | 4283 TEE_MEMORY_ACCESS_WRITE | 4284 TEE_MEMORY_ACCESS_ANY_OWNER, 4285 (uaddr_t)tag, tlen); 4286 if (res != TEE_SUCCESS) 4287 return res; 4288 4289 enter_user_access(); 4290 res = crypto_authenc_enc_final(cs->ctx, src_data, src_len, dst_data, 4291 &dlen, tag, &tlen); 4292 exit_user_access(); 4293 4294 out: 4295 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 4296 TEE_Result res2 = TEE_SUCCESS; 4297 4298 if (dst_len != NULL) { 4299 res2 = put_user_u64(dst_len, dlen); 4300 if (res2 != TEE_SUCCESS) 4301 return res2; 4302 } 4303 4304 res2 = put_user_u64(tag_len, tlen); 4305 if (res2 != TEE_SUCCESS) 4306 return res2; 4307 } 4308 4309 return res; 4310 } 4311 4312 TEE_Result syscall_authenc_dec_final(unsigned long state, 4313 const void *src_data, size_t src_len, void *dst_data, 4314 uint64_t *dst_len, const void *tag, size_t tag_len) 4315 { 4316 struct ts_session *sess = ts_get_current_session(); 4317 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 4318 struct tee_cryp_state *cs = NULL; 4319 TEE_Result res = TEE_SUCCESS; 4320 size_t dlen = 0; 4321 4322 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4323 if (res != TEE_SUCCESS) 4324 return res; 4325 4326 if (cs->state != CRYP_STATE_INITIALIZED) 4327 return TEE_ERROR_BAD_STATE; 4328 4329 if (cs->mode != TEE_MODE_DECRYPT) 4330 return TEE_ERROR_BAD_PARAMETERS; 4331 4332 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 4333 return TEE_ERROR_BAD_STATE; 4334 4335 src_data = memtag_strip_tag_const(src_data); 4336 dst_data = memtag_strip_tag(dst_data); 4337 tag = memtag_strip_tag_const(tag); 4338 4339 res = vm_check_access_rights(uctx, 4340 TEE_MEMORY_ACCESS_READ | 4341 TEE_MEMORY_ACCESS_ANY_OWNER, 4342 (uaddr_t)src_data, src_len); 4343 if (res != TEE_SUCCESS) 4344 return res; 4345 4346 if (!dst_len) { 4347 dlen = 0; 4348 } else { 4349 res = get_user_u64_as_size_t(&dlen, dst_len); 4350 if (res != TEE_SUCCESS) 4351 return res; 4352 4353 res = vm_check_access_rights(uctx, 4354 TEE_MEMORY_ACCESS_READ | 4355 TEE_MEMORY_ACCESS_WRITE | 4356 TEE_MEMORY_ACCESS_ANY_OWNER, 4357 (uaddr_t)dst_data, dlen); 4358 if (res != TEE_SUCCESS) 4359 return res; 4360 } 4361 4362 if (dlen < src_len) { 4363 res = TEE_ERROR_SHORT_BUFFER; 4364 goto out; 4365 } 4366 4367 /* Despite TEE Internal Core API up to v1.3, tag is [inbuf], not [in] */ 4368 res = vm_check_access_rights(uctx, 4369 TEE_MEMORY_ACCESS_READ | 4370 TEE_MEMORY_ACCESS_ANY_OWNER, 4371 (uaddr_t)tag, tag_len); 4372 if (res != TEE_SUCCESS) 4373 return res; 4374 4375 enter_user_access(); 4376 res = crypto_authenc_dec_final(cs->ctx, src_data, src_len, dst_data, 4377 &dlen, tag, tag_len); 4378 exit_user_access(); 4379 4380 out: 4381 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 4382 dst_len != NULL) { 4383 TEE_Result res2 = put_user_u64(dst_len, dlen); 4384 4385 if (res2 != TEE_SUCCESS) 4386 return res2; 4387 } 4388 4389 return res; 4390 } 4391 4392 static int pkcs1_get_salt_len(const TEE_Attribute *params, uint32_t num_params, 4393 size_t default_len) 4394 { 4395 size_t n; 4396 4397 assert(default_len < INT_MAX); 4398 4399 for (n = 0; n < num_params; n++) { 4400 if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) { 4401 if (params[n].content.value.a < INT_MAX) 4402 return params[n].content.value.a; 4403 break; 4404 } 4405 } 4406 /* 4407 * If salt length isn't provided use the default value which is 4408 * the length of the digest. 4409 */ 4410 return default_len; 4411 } 4412 4413 TEE_Result syscall_asymm_operate(unsigned long state, 4414 const struct utee_attribute *usr_params, 4415 size_t num_params, const void *src_data, size_t src_len, 4416 void *dst_data, uint64_t *dst_len) 4417 { 4418 struct ts_session *sess = ts_get_current_session(); 4419 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 4420 TEE_Result res = TEE_SUCCESS; 4421 struct tee_cryp_state *cs = NULL; 4422 size_t dlen = 0; 4423 struct tee_obj *o = NULL; 4424 void *label = NULL; 4425 size_t label_len = 0; 4426 size_t n = 0; 4427 int salt_len = 0; 4428 TEE_Attribute *params = NULL; 4429 size_t alloc_size = 0; 4430 uint32_t mgf_algo = 0; 4431 4432 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4433 if (res != TEE_SUCCESS) 4434 return res; 4435 4436 src_data = memtag_strip_tag_const(src_data); 4437 dst_data = memtag_strip_tag(dst_data); 4438 4439 res = vm_check_access_rights(&utc->uctx, 4440 TEE_MEMORY_ACCESS_READ | 4441 TEE_MEMORY_ACCESS_ANY_OWNER, 4442 (uaddr_t)src_data, src_len); 4443 if (res != TEE_SUCCESS) 4444 return res; 4445 4446 res = get_user_u64_as_size_t(&dlen, dst_len); 4447 if (res != TEE_SUCCESS) 4448 return res; 4449 4450 res = vm_check_access_rights(&utc->uctx, 4451 TEE_MEMORY_ACCESS_READ | 4452 TEE_MEMORY_ACCESS_WRITE | 4453 TEE_MEMORY_ACCESS_ANY_OWNER, 4454 (uaddr_t)dst_data, dlen); 4455 if (res != TEE_SUCCESS) 4456 return res; 4457 4458 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 4459 return TEE_ERROR_OVERFLOW; 4460 4461 params = malloc(alloc_size); 4462 if (!params) 4463 return TEE_ERROR_OUT_OF_MEMORY; 4464 res = copy_in_attrs(utc, usr_params, num_params, params); 4465 if (res != TEE_SUCCESS) 4466 goto out; 4467 4468 res = tee_obj_get(utc, cs->key1, &o); 4469 if (res != TEE_SUCCESS) 4470 goto out; 4471 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 4472 res = TEE_ERROR_GENERIC; 4473 goto out; 4474 } 4475 4476 switch (cs->algo) { 4477 case TEE_ALG_RSA_NOPAD: 4478 if (cs->mode == TEE_MODE_ENCRYPT) { 4479 enter_user_access(); 4480 res = crypto_acipher_rsanopad_encrypt(o->attr, src_data, 4481 src_len, dst_data, 4482 &dlen); 4483 exit_user_access(); 4484 } else if (cs->mode == TEE_MODE_DECRYPT) { 4485 enter_user_access(); 4486 res = crypto_acipher_rsanopad_decrypt(o->attr, src_data, 4487 src_len, dst_data, 4488 &dlen); 4489 exit_user_access(); 4490 } else { 4491 /* 4492 * We will panic because "the mode is not compatible 4493 * with the function" 4494 */ 4495 res = TEE_ERROR_GENERIC; 4496 } 4497 break; 4498 4499 case TEE_ALG_SM2_PKE: 4500 if (cs->mode == TEE_MODE_ENCRYPT) { 4501 enter_user_access(); 4502 res = crypto_acipher_sm2_pke_encrypt(o->attr, src_data, 4503 src_len, dst_data, 4504 &dlen); 4505 exit_user_access(); 4506 } else if (cs->mode == TEE_MODE_DECRYPT) { 4507 enter_user_access(); 4508 res = crypto_acipher_sm2_pke_decrypt(o->attr, src_data, 4509 src_len, dst_data, 4510 &dlen); 4511 exit_user_access(); 4512 } else { 4513 res = TEE_ERROR_GENERIC; 4514 } 4515 break; 4516 4517 case TEE_ALG_RSAES_PKCS1_V1_5: 4518 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_MD5: 4519 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 4520 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 4521 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 4522 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 4523 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 4524 for (n = 0; n < num_params; n++) { 4525 if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) { 4526 label = params[n].content.ref.buffer; 4527 label_len = params[n].content.ref.length; 4528 break; 4529 } 4530 4531 if (cs->algo != TEE_ALG_RSAES_PKCS1_V1_5 && 4532 params[n].attributeID == 4533 TEE_ATTR_RSA_OAEP_MGF_HASH) { 4534 void *buf = params[n].content.ref.buffer; 4535 4536 if (params[n].content.ref.length != 4537 sizeof(mgf_algo)) { 4538 res = TEE_ERROR_BAD_PARAMETERS; 4539 goto out; 4540 } 4541 4542 res = copy_from_user(&mgf_algo, buf, 4543 sizeof(mgf_algo)); 4544 if (res) 4545 goto out; 4546 } 4547 } 4548 4549 if (!mgf_algo) 4550 mgf_algo = TEE_INTERNAL_HASH_TO_ALGO(cs->algo); 4551 4552 if (cs->mode == TEE_MODE_ENCRYPT) { 4553 enter_user_access(); 4554 res = crypto_acipher_rsaes_encrypt(cs->algo, o->attr, 4555 label, label_len, 4556 mgf_algo, 4557 src_data, src_len, 4558 dst_data, &dlen); 4559 exit_user_access(); 4560 } else if (cs->mode == TEE_MODE_DECRYPT) { 4561 enter_user_access(); 4562 res = crypto_acipher_rsaes_decrypt( 4563 cs->algo, o->attr, label, label_len, 4564 mgf_algo, 4565 src_data, src_len, dst_data, &dlen); 4566 exit_user_access(); 4567 } else { 4568 res = TEE_ERROR_BAD_PARAMETERS; 4569 } 4570 break; 4571 4572 #if defined(CFG_CRYPTO_RSASSA_NA1) 4573 case TEE_ALG_RSASSA_PKCS1_V1_5: 4574 #endif 4575 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 4576 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 4577 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 4578 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 4579 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 4580 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 4581 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_MD5: 4582 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 4583 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 4584 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 4585 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 4586 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 4587 if (cs->mode != TEE_MODE_SIGN) { 4588 res = TEE_ERROR_BAD_PARAMETERS; 4589 break; 4590 } 4591 salt_len = pkcs1_get_salt_len(params, num_params, src_len); 4592 enter_user_access(); 4593 res = crypto_acipher_rsassa_sign(cs->algo, o->attr, salt_len, 4594 src_data, src_len, dst_data, 4595 &dlen); 4596 exit_user_access(); 4597 break; 4598 4599 case TEE_ALG_DSA_SHA1: 4600 case TEE_ALG_DSA_SHA224: 4601 case TEE_ALG_DSA_SHA256: 4602 enter_user_access(); 4603 res = crypto_acipher_dsa_sign(cs->algo, o->attr, src_data, 4604 src_len, dst_data, &dlen); 4605 exit_user_access(); 4606 break; 4607 4608 case TEE_ALG_ED25519: 4609 enter_user_access(); 4610 res = tee_svc_obj_ed25519_sign(o->attr, src_data, src_len, 4611 dst_data, &dlen, params, 4612 num_params); 4613 exit_user_access(); 4614 break; 4615 4616 case TEE_ALG_ECDSA_SHA1: 4617 case TEE_ALG_ECDSA_SHA224: 4618 case TEE_ALG_ECDSA_SHA256: 4619 case TEE_ALG_ECDSA_SHA384: 4620 case TEE_ALG_ECDSA_SHA512: 4621 case TEE_ALG_SM2_DSA_SM3: 4622 enter_user_access(); 4623 res = crypto_acipher_ecc_sign(cs->algo, o->attr, src_data, 4624 src_len, dst_data, &dlen); 4625 exit_user_access(); 4626 break; 4627 default: 4628 res = TEE_ERROR_BAD_PARAMETERS; 4629 break; 4630 } 4631 4632 out: 4633 free_wipe(params); 4634 4635 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 4636 TEE_Result res2 = put_user_u64(dst_len, dlen); 4637 4638 if (res2 != TEE_SUCCESS) 4639 return res2; 4640 } 4641 4642 return res; 4643 } 4644 4645 TEE_Result syscall_asymm_verify(unsigned long state, 4646 const struct utee_attribute *usr_params, 4647 size_t num_params, const void *data, size_t data_len, 4648 const void *sig, size_t sig_len) 4649 { 4650 struct ts_session *sess = ts_get_current_session(); 4651 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 4652 struct tee_cryp_state *cs = NULL; 4653 TEE_Result res = TEE_SUCCESS; 4654 TEE_Attribute *params = NULL; 4655 struct tee_obj *o = NULL; 4656 size_t hash_size = 0; 4657 uint32_t hash_algo = 0; 4658 int salt_len = 0; 4659 size_t alloc_size = 0; 4660 4661 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4662 if (res != TEE_SUCCESS) 4663 return res; 4664 4665 if (cs->mode != TEE_MODE_VERIFY) 4666 return TEE_ERROR_BAD_PARAMETERS; 4667 4668 data = memtag_strip_tag_const(data); 4669 sig = memtag_strip_tag_const(sig); 4670 4671 res = vm_check_access_rights(&utc->uctx, 4672 TEE_MEMORY_ACCESS_READ | 4673 TEE_MEMORY_ACCESS_ANY_OWNER, 4674 (uaddr_t)data, data_len); 4675 if (res != TEE_SUCCESS) 4676 return res; 4677 4678 res = vm_check_access_rights(&utc->uctx, 4679 TEE_MEMORY_ACCESS_READ | 4680 TEE_MEMORY_ACCESS_ANY_OWNER, 4681 (uaddr_t)sig, sig_len); 4682 if (res != TEE_SUCCESS) 4683 return res; 4684 4685 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 4686 return TEE_ERROR_OVERFLOW; 4687 4688 params = malloc(alloc_size); 4689 if (!params) 4690 return TEE_ERROR_OUT_OF_MEMORY; 4691 res = copy_in_attrs(utc, usr_params, num_params, params); 4692 if (res != TEE_SUCCESS) 4693 goto out; 4694 4695 res = tee_obj_get(utc, cs->key1, &o); 4696 if (res != TEE_SUCCESS) 4697 goto out; 4698 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 4699 res = TEE_ERROR_BAD_PARAMETERS; 4700 goto out; 4701 } 4702 4703 switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) { 4704 case TEE_MAIN_ALGO_RSA: 4705 if (cs->algo != TEE_ALG_RSASSA_PKCS1_V1_5) { 4706 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 4707 res = tee_alg_get_digest_size(hash_algo, &hash_size); 4708 if (res != TEE_SUCCESS) 4709 break; 4710 if (data_len != hash_size) { 4711 res = TEE_ERROR_BAD_PARAMETERS; 4712 break; 4713 } 4714 salt_len = pkcs1_get_salt_len(params, num_params, 4715 hash_size); 4716 } 4717 enter_user_access(); 4718 res = crypto_acipher_rsassa_verify(cs->algo, o->attr, salt_len, 4719 data, data_len, sig, 4720 sig_len); 4721 exit_user_access(); 4722 break; 4723 4724 case TEE_MAIN_ALGO_DSA: 4725 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 4726 res = tee_alg_get_digest_size(hash_algo, &hash_size); 4727 if (res != TEE_SUCCESS) 4728 break; 4729 4730 if (data_len != hash_size) { 4731 struct dsa_public_key *key = o->attr; 4732 4733 /* 4734 * Depending on the DSA algorithm (NIST), the 4735 * digital signature output size may be truncated 4736 * to the size of a key pair (Q prime size). Q 4737 * prime size must be less or equal than the hash 4738 * output length of the hash algorithm involved. 4739 * 4740 * We're checking here in order to be able to 4741 * return this particular error code, which will 4742 * cause TEE_AsymmetricVerifyDigest() to panic as 4743 * required by GP. crypto_acipher_dsa_verify() is 4744 * implemented in the glue layer of the crypto 4745 * library and it might be a bit harder to catch 4746 * this particular case there or lead to duplicated 4747 * code in different crypto glue layers. 4748 * 4749 * The GP spec says that we SHOULD panic if 4750 * data_len != hash_size, but that would break a 4751 * few of the DSA tests in xtest where the 4752 * hash_size is larger than possible data_len. So 4753 * the compromise is in case data_len != hash_size 4754 * check that it's not smaller than what makes 4755 * sense. 4756 */ 4757 if (data_len != crypto_bignum_num_bytes(key->q)) { 4758 res = TEE_ERROR_BAD_PARAMETERS; 4759 break; 4760 } 4761 } 4762 enter_user_access(); 4763 res = crypto_acipher_dsa_verify(cs->algo, o->attr, data, 4764 data_len, sig, sig_len); 4765 exit_user_access(); 4766 break; 4767 4768 case TEE_MAIN_ALGO_ED25519: 4769 enter_user_access(); 4770 res = tee_svc_obj_ed25519_verify(o->attr, data, 4771 data_len, sig, sig_len, 4772 params, num_params); 4773 exit_user_access(); 4774 break; 4775 4776 case TEE_MAIN_ALGO_ECDSA: 4777 case TEE_MAIN_ALGO_SM2_DSA_SM3: 4778 enter_user_access(); 4779 res = crypto_acipher_ecc_verify(cs->algo, o->attr, data, 4780 data_len, sig, sig_len); 4781 exit_user_access(); 4782 break; 4783 4784 default: 4785 res = TEE_ERROR_NOT_SUPPORTED; 4786 } 4787 4788 out: 4789 free_wipe(params); 4790 return res; 4791 } 4792