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. 2091 */ 2092 2093 if (n > sizeof(bin_key) || n < 3) 2094 return TEE_ERROR_BAD_PARAMETERS; 2095 2096 crypto_bignum_bn2bin(e, bin_key); 2097 2098 if (!(bin_key[n - 1] & 1)) /* key must be odd */ 2099 return TEE_ERROR_BAD_PARAMETERS; 2100 2101 if (n == 3) { 2102 uint32_t key = 0; 2103 2104 for (n = 0; n < 3; n++) { 2105 key <<= 8; 2106 key |= bin_key[n]; 2107 } 2108 2109 if (key < 65537) 2110 return TEE_ERROR_BAD_PARAMETERS; 2111 } 2112 2113 /* key is larger than 65537 */ 2114 return TEE_SUCCESS; 2115 } 2116 2117 static TEE_Result tee_svc_obj_generate_key_rsa( 2118 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2119 uint32_t key_size, 2120 const TEE_Attribute *params, uint32_t param_count) 2121 { 2122 TEE_Result res = TEE_SUCCESS; 2123 struct rsa_keypair *key = o->attr; 2124 uint32_t e = TEE_U32_TO_BIG_ENDIAN(65537); 2125 2126 /* Copy the present attributes into the obj before starting */ 2127 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2128 param_count); 2129 if (res != TEE_SUCCESS) 2130 return res; 2131 if (get_attribute(o, type_props, TEE_ATTR_RSA_PUBLIC_EXPONENT)) { 2132 res = check_pub_rsa_key(key->e); 2133 if (res) 2134 return res; 2135 } else { 2136 crypto_bignum_bin2bn((const uint8_t *)&e, sizeof(e), key->e); 2137 } 2138 res = crypto_acipher_gen_rsa_key(key, key_size); 2139 if (res != TEE_SUCCESS) 2140 return res; 2141 2142 /* Set bits for all known attributes for this object type */ 2143 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 2144 2145 return TEE_SUCCESS; 2146 } 2147 2148 static TEE_Result tee_svc_obj_generate_key_dsa( 2149 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2150 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 2151 { 2152 TEE_Result res; 2153 2154 /* Copy the present attributes into the obj before starting */ 2155 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2156 param_count); 2157 if (res != TEE_SUCCESS) 2158 return res; 2159 2160 res = crypto_acipher_gen_dsa_key(o->attr, key_size); 2161 if (res != TEE_SUCCESS) 2162 return res; 2163 2164 /* Set bits for all known attributes for this object type */ 2165 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 2166 2167 return TEE_SUCCESS; 2168 } 2169 2170 static TEE_Result tee_svc_obj_generate_key_dh( 2171 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2172 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 2173 { 2174 TEE_Result res; 2175 struct dh_keypair *tee_dh_key; 2176 struct bignum *dh_q = NULL; 2177 uint32_t dh_xbits = 0; 2178 2179 /* Copy the present attributes into the obj before starting */ 2180 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2181 param_count); 2182 if (res != TEE_SUCCESS) 2183 return res; 2184 2185 tee_dh_key = (struct dh_keypair *)o->attr; 2186 2187 if (get_attribute(o, type_props, TEE_ATTR_DH_SUBPRIME)) 2188 dh_q = tee_dh_key->q; 2189 if (get_attribute(o, type_props, TEE_ATTR_DH_X_BITS)) 2190 dh_xbits = tee_dh_key->xbits; 2191 res = crypto_acipher_gen_dh_key(tee_dh_key, dh_q, dh_xbits, key_size); 2192 if (res != TEE_SUCCESS) 2193 return res; 2194 2195 /* Set bits for the generated public and private key */ 2196 set_attribute(o, type_props, TEE_ATTR_DH_PUBLIC_VALUE); 2197 set_attribute(o, type_props, TEE_ATTR_DH_PRIVATE_VALUE); 2198 set_attribute(o, type_props, TEE_ATTR_DH_X_BITS); 2199 return TEE_SUCCESS; 2200 } 2201 2202 static TEE_Result tee_svc_obj_generate_key_ecc( 2203 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2204 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 2205 { 2206 TEE_Result res; 2207 struct ecc_keypair *tee_ecc_key; 2208 2209 /* Copy the present attributes into the obj before starting */ 2210 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2211 param_count); 2212 if (res != TEE_SUCCESS) 2213 return res; 2214 2215 tee_ecc_key = (struct ecc_keypair *)o->attr; 2216 2217 res = crypto_acipher_gen_ecc_key(tee_ecc_key, key_size); 2218 if (res != TEE_SUCCESS) 2219 return res; 2220 2221 /* Set bits for the generated public and private key */ 2222 set_attribute(o, type_props, TEE_ATTR_ECC_PRIVATE_VALUE); 2223 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_X); 2224 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_Y); 2225 set_attribute(o, type_props, TEE_ATTR_ECC_CURVE); 2226 return TEE_SUCCESS; 2227 } 2228 2229 static TEE_Result 2230 tee_svc_obj_generate_key_x25519(struct tee_obj *o, 2231 const struct tee_cryp_obj_type_props 2232 *type_props, 2233 uint32_t key_size, 2234 const TEE_Attribute *params, 2235 uint32_t param_count) 2236 { 2237 TEE_Result res = TEE_ERROR_GENERIC; 2238 struct montgomery_keypair *tee_x25519_key = NULL; 2239 2240 /* Copy the present attributes into the obj before starting */ 2241 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2242 param_count); 2243 if (res != TEE_SUCCESS) 2244 return res; 2245 2246 tee_x25519_key = (struct montgomery_keypair *)o->attr; 2247 2248 res = crypto_acipher_gen_x25519_key(tee_x25519_key, key_size); 2249 if (res != TEE_SUCCESS) 2250 return res; 2251 2252 /* Set bits for the generated public and private key */ 2253 set_attribute(o, type_props, TEE_ATTR_X25519_PRIVATE_VALUE); 2254 set_attribute(o, type_props, TEE_ATTR_X25519_PUBLIC_VALUE); 2255 return TEE_SUCCESS; 2256 } 2257 2258 static TEE_Result 2259 tee_svc_obj_generate_key_x448(struct tee_obj *o, 2260 const struct tee_cryp_obj_type_props *type_props, 2261 uint32_t key_size, const TEE_Attribute *params, 2262 uint32_t param_count) 2263 { 2264 TEE_Result res = TEE_ERROR_GENERIC; 2265 struct montgomery_keypair *tee_x448_key = NULL; 2266 2267 /* Copy the present attributes into the obj before starting */ 2268 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2269 param_count); 2270 if (res != TEE_SUCCESS) 2271 return res; 2272 2273 tee_x448_key = (struct montgomery_keypair *)o->attr; 2274 res = crypto_acipher_gen_x448_key(tee_x448_key, key_size); 2275 if (res != TEE_SUCCESS) 2276 return res; 2277 2278 set_attribute(o, type_props, TEE_ATTR_X448_PRIVATE_VALUE); 2279 set_attribute(o, type_props, TEE_ATTR_X448_PUBLIC_VALUE); 2280 2281 return TEE_SUCCESS; 2282 } 2283 2284 static TEE_Result 2285 tee_svc_obj_generate_key_ed25519(struct tee_obj *o, 2286 const struct tee_cryp_obj_type_props 2287 *type_props, 2288 uint32_t key_size, 2289 const TEE_Attribute *params, 2290 uint32_t param_count) 2291 { 2292 TEE_Result res = TEE_ERROR_GENERIC; 2293 struct ed25519_keypair *key = NULL; 2294 2295 /* Copy the present attributes into the obj before starting */ 2296 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2297 param_count); 2298 if (res != TEE_SUCCESS) 2299 return res; 2300 2301 key = o->attr; 2302 2303 res = crypto_acipher_gen_ed25519_key(key, key_size); 2304 if (res != TEE_SUCCESS) 2305 return res; 2306 2307 /* Set bits for the generated public and private key */ 2308 set_attribute(o, type_props, TEE_ATTR_ED25519_PRIVATE_VALUE); 2309 set_attribute(o, type_props, TEE_ATTR_ED25519_PUBLIC_VALUE); 2310 return TEE_SUCCESS; 2311 } 2312 2313 static TEE_Result 2314 tee_svc_obj_ed25519_parse_params(const TEE_Attribute *params, size_t num_params, 2315 bool *ph_flag, const uint8_t **ctx, 2316 size_t *ctx_len) 2317 { 2318 size_t n = 0; 2319 2320 *ctx = NULL; 2321 2322 for (n = 0; n < num_params; n++) { 2323 switch (params[n].attributeID) { 2324 case TEE_ATTR_EDDSA_PREHASH: 2325 if (params[n].content.value.b) 2326 return TEE_ERROR_BAD_PARAMETERS; 2327 if (!params[n].content.value.a) 2328 *ph_flag = false; 2329 else if (params[n].content.value.a == 1) 2330 *ph_flag = true; 2331 else 2332 return TEE_ERROR_BAD_PARAMETERS; 2333 break; 2334 2335 case TEE_ATTR_EDDSA_CTX: 2336 /* several provided contexts are treated as error */ 2337 if (*ctx) 2338 return TEE_ERROR_BAD_PARAMETERS; 2339 2340 *ctx_len = params[n].content.ref.length; 2341 if (*ctx_len > TEE_ED25519_CTX_MAX_LENGTH) 2342 return TEE_ERROR_BAD_PARAMETERS; 2343 2344 if (!*ctx_len) 2345 break; 2346 2347 *ctx = params[n].content.ref.buffer; 2348 if (!*ctx) 2349 return TEE_ERROR_BAD_PARAMETERS; 2350 break; 2351 2352 default: 2353 return TEE_ERROR_BAD_PARAMETERS; 2354 } 2355 } 2356 2357 return TEE_SUCCESS; 2358 } 2359 2360 static TEE_Result 2361 tee_svc_obj_ed25519_sign(struct ed25519_keypair *key, 2362 const uint8_t *msg, size_t msg_len, 2363 uint8_t *sig, size_t *sig_len, 2364 const TEE_Attribute *params, size_t num_params) 2365 { 2366 TEE_Result err = TEE_ERROR_GENERIC; 2367 size_t ctx_len = 0; 2368 const uint8_t *ctx = NULL; 2369 bool ph_flag = false; 2370 2371 err = tee_svc_obj_ed25519_parse_params(params, num_params, &ph_flag, 2372 &ctx, &ctx_len); 2373 if (err != TEE_SUCCESS) 2374 return err; 2375 2376 if (ph_flag || ctx) { 2377 return crypto_acipher_ed25519ctx_sign(key, msg, msg_len, sig, 2378 sig_len, ph_flag, 2379 ctx, ctx_len); 2380 } 2381 2382 return crypto_acipher_ed25519_sign(key, msg, msg_len, sig, sig_len); 2383 } 2384 2385 static TEE_Result 2386 tee_svc_obj_ed25519_verify(struct ed25519_public_key *key, 2387 const uint8_t *msg, size_t msg_len, 2388 const uint8_t *sig, size_t sig_len, 2389 const TEE_Attribute *params, size_t num_params) 2390 { 2391 TEE_Result err = TEE_ERROR_GENERIC; 2392 size_t ctx_len = 0; 2393 const uint8_t *ctx = NULL; 2394 bool ph_flag = false; 2395 2396 err = tee_svc_obj_ed25519_parse_params(params, num_params, &ph_flag, 2397 &ctx, &ctx_len); 2398 if (err) 2399 return err; 2400 2401 if (ph_flag || ctx) { 2402 return crypto_acipher_ed25519ctx_verify(key, msg, msg_len, sig, 2403 sig_len, ph_flag, 2404 ctx, ctx_len); 2405 } 2406 2407 return crypto_acipher_ed25519_verify(key, msg, msg_len, sig, sig_len); 2408 } 2409 2410 TEE_Result syscall_obj_generate_key(unsigned long obj, unsigned long key_size, 2411 const struct utee_attribute *usr_params, 2412 unsigned long param_count) 2413 { 2414 struct ts_session *sess = ts_get_current_session(); 2415 TEE_Result res = TEE_SUCCESS; 2416 const struct tee_cryp_obj_type_props *type_props = NULL; 2417 struct tee_obj *o = NULL; 2418 struct tee_cryp_obj_secret *key = NULL; 2419 size_t byte_size = 0; 2420 TEE_Attribute *params = NULL; 2421 size_t alloc_size = 0; 2422 2423 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 2424 if (res != TEE_SUCCESS) 2425 return res; 2426 2427 /* Must be a transient object */ 2428 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 2429 return TEE_ERROR_BAD_STATE; 2430 2431 /* Must not be initialized already */ 2432 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 2433 return TEE_ERROR_BAD_STATE; 2434 2435 /* Find description of object */ 2436 type_props = tee_svc_find_type_props(o->info.objectType); 2437 if (!type_props) 2438 return TEE_ERROR_NOT_SUPPORTED; 2439 2440 /* Check that key_size follows restrictions */ 2441 res = check_key_size(type_props, key_size); 2442 if (res) 2443 return res; 2444 2445 if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size)) 2446 return TEE_ERROR_OVERFLOW; 2447 2448 params = malloc(alloc_size); 2449 if (!params) 2450 return TEE_ERROR_OUT_OF_MEMORY; 2451 res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_params, param_count, 2452 params); 2453 if (res != TEE_SUCCESS) 2454 goto out; 2455 2456 res = tee_svc_cryp_check_attr(ATTR_USAGE_GENERATE_KEY, type_props, 2457 params, param_count); 2458 if (res != TEE_SUCCESS) 2459 goto out; 2460 2461 switch (o->info.objectType) { 2462 case TEE_TYPE_AES: 2463 case TEE_TYPE_DES: 2464 case TEE_TYPE_DES3: 2465 case TEE_TYPE_SM4: 2466 case TEE_TYPE_HMAC_MD5: 2467 case TEE_TYPE_HMAC_SHA1: 2468 case TEE_TYPE_HMAC_SHA224: 2469 case TEE_TYPE_HMAC_SHA256: 2470 case TEE_TYPE_HMAC_SHA384: 2471 case TEE_TYPE_HMAC_SHA512: 2472 case TEE_TYPE_HMAC_SHA3_224: 2473 case TEE_TYPE_HMAC_SHA3_256: 2474 case TEE_TYPE_HMAC_SHA3_384: 2475 case TEE_TYPE_HMAC_SHA3_512: 2476 case TEE_TYPE_HMAC_SM3: 2477 case TEE_TYPE_GENERIC_SECRET: 2478 byte_size = key_size / 8; 2479 2480 /* 2481 * In GP Internal API Specification 1.0 the partity bits 2482 * aren't counted when telling the size of the key in bits. 2483 */ 2484 if (is_gp_legacy_des_key_size(o->info.objectType, key_size)) 2485 byte_size = (key_size + key_size / 7) / 8; 2486 2487 key = (struct tee_cryp_obj_secret *)o->attr; 2488 if (byte_size > key->alloc_size) { 2489 res = TEE_ERROR_EXCESS_DATA; 2490 goto out; 2491 } 2492 2493 res = crypto_rng_read((void *)(key + 1), byte_size); 2494 if (res != TEE_SUCCESS) 2495 goto out; 2496 2497 key->key_size = byte_size; 2498 2499 /* Set bits for all known attributes for this object type */ 2500 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 2501 2502 break; 2503 2504 case TEE_TYPE_RSA_KEYPAIR: 2505 res = tee_svc_obj_generate_key_rsa(o, type_props, key_size, 2506 params, param_count); 2507 if (res != TEE_SUCCESS) 2508 goto out; 2509 break; 2510 2511 case TEE_TYPE_DSA_KEYPAIR: 2512 res = tee_svc_obj_generate_key_dsa(o, type_props, key_size, 2513 params, param_count); 2514 if (res != TEE_SUCCESS) 2515 goto out; 2516 break; 2517 2518 case TEE_TYPE_DH_KEYPAIR: 2519 res = tee_svc_obj_generate_key_dh(o, type_props, key_size, 2520 params, param_count); 2521 if (res != TEE_SUCCESS) 2522 goto out; 2523 break; 2524 2525 case TEE_TYPE_ECDSA_KEYPAIR: 2526 case TEE_TYPE_ECDH_KEYPAIR: 2527 case TEE_TYPE_SM2_DSA_KEYPAIR: 2528 case TEE_TYPE_SM2_KEP_KEYPAIR: 2529 case TEE_TYPE_SM2_PKE_KEYPAIR: 2530 res = tee_svc_obj_generate_key_ecc(o, type_props, key_size, 2531 params, param_count); 2532 if (res != TEE_SUCCESS) 2533 goto out; 2534 break; 2535 2536 case TEE_TYPE_X25519_KEYPAIR: 2537 res = tee_svc_obj_generate_key_x25519(o, type_props, key_size, 2538 params, param_count); 2539 if (res != TEE_SUCCESS) 2540 goto out; 2541 break; 2542 case TEE_TYPE_X448_KEYPAIR: 2543 res = tee_svc_obj_generate_key_x448(o, type_props, key_size, 2544 params, param_count); 2545 if (res != TEE_SUCCESS) 2546 goto out; 2547 break; 2548 2549 case TEE_TYPE_ED25519_KEYPAIR: 2550 res = tee_svc_obj_generate_key_ed25519(o, type_props, key_size, 2551 params, param_count); 2552 if (res != TEE_SUCCESS) 2553 goto out; 2554 break; 2555 2556 default: 2557 res = TEE_ERROR_BAD_FORMAT; 2558 } 2559 2560 out: 2561 free_wipe(params); 2562 if (res == TEE_SUCCESS) { 2563 o->info.objectSize = key_size; 2564 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2565 } 2566 return res; 2567 } 2568 2569 static TEE_Result tee_svc_cryp_get_state(struct ts_session *sess, 2570 vaddr_t state_id, 2571 struct tee_cryp_state **state) 2572 { 2573 struct tee_cryp_state *s; 2574 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 2575 2576 TAILQ_FOREACH(s, &utc->cryp_states, link) { 2577 if (state_id == (vaddr_t)s) { 2578 *state = s; 2579 return TEE_SUCCESS; 2580 } 2581 } 2582 return TEE_ERROR_BAD_PARAMETERS; 2583 } 2584 2585 static void cryp_state_free(struct user_ta_ctx *utc, struct tee_cryp_state *cs) 2586 { 2587 struct tee_obj *o; 2588 2589 if (tee_obj_get(utc, cs->key1, &o) == TEE_SUCCESS) 2590 tee_obj_close(utc, o); 2591 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) 2592 tee_obj_close(utc, o); 2593 2594 TAILQ_REMOVE(&utc->cryp_states, cs, link); 2595 if (cs->ctx_finalize != NULL) 2596 cs->ctx_finalize(cs->ctx); 2597 2598 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2599 case TEE_OPERATION_CIPHER: 2600 crypto_cipher_free_ctx(cs->ctx); 2601 break; 2602 case TEE_OPERATION_AE: 2603 crypto_authenc_free_ctx(cs->ctx); 2604 break; 2605 case TEE_OPERATION_DIGEST: 2606 crypto_hash_free_ctx(cs->ctx); 2607 break; 2608 case TEE_OPERATION_MAC: 2609 crypto_mac_free_ctx(cs->ctx); 2610 break; 2611 default: 2612 assert(!cs->ctx); 2613 } 2614 2615 free(cs); 2616 } 2617 2618 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o, 2619 uint32_t algo, 2620 TEE_OperationMode mode) 2621 { 2622 uint32_t req_key_type; 2623 uint32_t req_key_type2 = 0; 2624 2625 switch (TEE_ALG_GET_MAIN_ALG(algo)) { 2626 case TEE_MAIN_ALGO_MD5: 2627 req_key_type = TEE_TYPE_HMAC_MD5; 2628 break; 2629 case TEE_MAIN_ALGO_SHA1: 2630 req_key_type = TEE_TYPE_HMAC_SHA1; 2631 break; 2632 case TEE_MAIN_ALGO_SHA224: 2633 req_key_type = TEE_TYPE_HMAC_SHA224; 2634 break; 2635 case TEE_MAIN_ALGO_SHA256: 2636 req_key_type = TEE_TYPE_HMAC_SHA256; 2637 break; 2638 case TEE_MAIN_ALGO_SHA384: 2639 req_key_type = TEE_TYPE_HMAC_SHA384; 2640 break; 2641 case TEE_MAIN_ALGO_SHA512: 2642 req_key_type = TEE_TYPE_HMAC_SHA512; 2643 break; 2644 case TEE_MAIN_ALGO_SHA3_224: 2645 req_key_type = TEE_TYPE_HMAC_SHA3_224; 2646 break; 2647 case TEE_MAIN_ALGO_SHA3_256: 2648 req_key_type = TEE_TYPE_HMAC_SHA3_256; 2649 break; 2650 case TEE_MAIN_ALGO_SHA3_384: 2651 req_key_type = TEE_TYPE_HMAC_SHA3_384; 2652 break; 2653 case TEE_MAIN_ALGO_SHA3_512: 2654 req_key_type = TEE_TYPE_HMAC_SHA3_512; 2655 break; 2656 case TEE_MAIN_ALGO_SM3: 2657 req_key_type = TEE_TYPE_HMAC_SM3; 2658 break; 2659 case TEE_MAIN_ALGO_AES: 2660 req_key_type = TEE_TYPE_AES; 2661 break; 2662 case TEE_MAIN_ALGO_DES: 2663 req_key_type = TEE_TYPE_DES; 2664 break; 2665 case TEE_MAIN_ALGO_DES3: 2666 req_key_type = TEE_TYPE_DES3; 2667 break; 2668 case TEE_MAIN_ALGO_SM4: 2669 req_key_type = TEE_TYPE_SM4; 2670 break; 2671 case TEE_MAIN_ALGO_RSA: 2672 req_key_type = TEE_TYPE_RSA_KEYPAIR; 2673 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 2674 req_key_type2 = TEE_TYPE_RSA_PUBLIC_KEY; 2675 break; 2676 case TEE_MAIN_ALGO_DSA: 2677 req_key_type = TEE_TYPE_DSA_KEYPAIR; 2678 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 2679 req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY; 2680 break; 2681 case TEE_MAIN_ALGO_DH: 2682 req_key_type = TEE_TYPE_DH_KEYPAIR; 2683 break; 2684 case TEE_MAIN_ALGO_ECDSA: 2685 req_key_type = TEE_TYPE_ECDSA_KEYPAIR; 2686 if (mode == TEE_MODE_VERIFY) 2687 req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY; 2688 break; 2689 case TEE_MAIN_ALGO_ECDH: 2690 req_key_type = TEE_TYPE_ECDH_KEYPAIR; 2691 break; 2692 case TEE_MAIN_ALGO_ED25519: 2693 req_key_type = TEE_TYPE_ED25519_KEYPAIR; 2694 if (mode == TEE_MODE_VERIFY) 2695 req_key_type2 = TEE_TYPE_ED25519_PUBLIC_KEY; 2696 break; 2697 case TEE_MAIN_ALGO_SM2_PKE: 2698 if (mode == TEE_MODE_ENCRYPT) 2699 req_key_type = TEE_TYPE_SM2_PKE_PUBLIC_KEY; 2700 else 2701 req_key_type = TEE_TYPE_SM2_PKE_KEYPAIR; 2702 break; 2703 case TEE_MAIN_ALGO_SM2_DSA_SM3: 2704 if (mode == TEE_MODE_VERIFY) 2705 req_key_type = TEE_TYPE_SM2_DSA_PUBLIC_KEY; 2706 else 2707 req_key_type = TEE_TYPE_SM2_DSA_KEYPAIR; 2708 break; 2709 #if defined(CFG_CRYPTO_SM2_KEP) 2710 case TEE_MAIN_ALGO_SM2_KEP: 2711 req_key_type = TEE_TYPE_SM2_KEP_KEYPAIR; 2712 req_key_type2 = TEE_TYPE_SM2_KEP_PUBLIC_KEY; 2713 break; 2714 #endif 2715 #if defined(CFG_CRYPTO_HKDF) 2716 case TEE_MAIN_ALGO_HKDF: 2717 req_key_type = TEE_TYPE_HKDF_IKM; 2718 break; 2719 #endif 2720 #if defined(CFG_CRYPTO_CONCAT_KDF) 2721 case TEE_MAIN_ALGO_CONCAT_KDF: 2722 req_key_type = TEE_TYPE_CONCAT_KDF_Z; 2723 break; 2724 #endif 2725 #if defined(CFG_CRYPTO_PBKDF2) 2726 case TEE_MAIN_ALGO_PBKDF2: 2727 req_key_type = TEE_TYPE_PBKDF2_PASSWORD; 2728 break; 2729 #endif 2730 case TEE_MAIN_ALGO_X25519: 2731 req_key_type = TEE_TYPE_X25519_KEYPAIR; 2732 break; 2733 case TEE_MAIN_ALGO_X448: 2734 req_key_type = TEE_TYPE_X448_KEYPAIR; 2735 break; 2736 default: 2737 return TEE_ERROR_BAD_PARAMETERS; 2738 } 2739 2740 if (req_key_type != o->info.objectType && 2741 req_key_type2 != o->info.objectType) 2742 return TEE_ERROR_BAD_PARAMETERS; 2743 return TEE_SUCCESS; 2744 } 2745 2746 static uint32_t translate_compat_algo(uint32_t algo) 2747 { 2748 switch (algo) { 2749 case __OPTEE_ALG_ECDSA_P192: 2750 return TEE_ALG_ECDSA_SHA1; 2751 case __OPTEE_ALG_ECDSA_P224: 2752 return TEE_ALG_ECDSA_SHA224; 2753 case __OPTEE_ALG_ECDSA_P256: 2754 return TEE_ALG_ECDSA_SHA256; 2755 case __OPTEE_ALG_ECDSA_P384: 2756 return TEE_ALG_ECDSA_SHA384; 2757 case __OPTEE_ALG_ECDSA_P521: 2758 return TEE_ALG_ECDSA_SHA512; 2759 case __OPTEE_ALG_ECDH_P192: 2760 case __OPTEE_ALG_ECDH_P224: 2761 case __OPTEE_ALG_ECDH_P256: 2762 case __OPTEE_ALG_ECDH_P384: 2763 case __OPTEE_ALG_ECDH_P521: 2764 return TEE_ALG_ECDH_DERIVE_SHARED_SECRET; 2765 default: 2766 return algo; 2767 } 2768 } 2769 2770 TEE_Result syscall_cryp_state_alloc(unsigned long algo, unsigned long mode, 2771 unsigned long key1, unsigned long key2, 2772 uint32_t *state) 2773 { 2774 struct ts_session *sess = ts_get_current_session(); 2775 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 2776 TEE_Result res = TEE_SUCCESS; 2777 struct tee_cryp_state *cs = NULL; 2778 struct tee_obj *o1 = NULL; 2779 struct tee_obj *o2 = NULL; 2780 2781 algo = translate_compat_algo(algo); 2782 2783 if (key1 != 0) { 2784 res = tee_obj_get(utc, uref_to_vaddr(key1), &o1); 2785 if (res != TEE_SUCCESS) 2786 return res; 2787 if (o1->busy) 2788 return TEE_ERROR_BAD_PARAMETERS; 2789 res = tee_svc_cryp_check_key_type(o1, algo, mode); 2790 if (res != TEE_SUCCESS) 2791 return res; 2792 } 2793 if (key2 != 0) { 2794 res = tee_obj_get(utc, uref_to_vaddr(key2), &o2); 2795 if (res != TEE_SUCCESS) 2796 return res; 2797 if (o2->busy) 2798 return TEE_ERROR_BAD_PARAMETERS; 2799 res = tee_svc_cryp_check_key_type(o2, algo, mode); 2800 if (res != TEE_SUCCESS) 2801 return res; 2802 } 2803 2804 cs = calloc(1, sizeof(struct tee_cryp_state)); 2805 if (!cs) 2806 return TEE_ERROR_OUT_OF_MEMORY; 2807 TAILQ_INSERT_TAIL(&utc->cryp_states, cs, link); 2808 cs->algo = algo; 2809 cs->mode = mode; 2810 cs->state = CRYP_STATE_UNINITIALIZED; 2811 2812 switch (TEE_ALG_GET_CLASS(algo)) { 2813 case TEE_OPERATION_CIPHER: 2814 if ((TEE_ALG_GET_CHAIN_MODE(algo) == TEE_CHAIN_MODE_XTS && 2815 (key1 == 0 || key2 == 0)) || 2816 (TEE_ALG_GET_CHAIN_MODE(algo) != TEE_CHAIN_MODE_XTS && 2817 (key1 == 0 || key2 != 0))) { 2818 res = TEE_ERROR_BAD_PARAMETERS; 2819 } else { 2820 res = crypto_cipher_alloc_ctx(&cs->ctx, algo); 2821 if (res != TEE_SUCCESS) 2822 break; 2823 } 2824 break; 2825 case TEE_OPERATION_AE: 2826 if (key1 == 0 || key2 != 0) { 2827 res = TEE_ERROR_BAD_PARAMETERS; 2828 } else { 2829 res = crypto_authenc_alloc_ctx(&cs->ctx, algo); 2830 if (res != TEE_SUCCESS) 2831 break; 2832 } 2833 break; 2834 case TEE_OPERATION_MAC: 2835 if (key1 == 0 || key2 != 0) { 2836 res = TEE_ERROR_BAD_PARAMETERS; 2837 } else { 2838 res = crypto_mac_alloc_ctx(&cs->ctx, algo); 2839 if (res != TEE_SUCCESS) 2840 break; 2841 } 2842 break; 2843 case TEE_OPERATION_DIGEST: 2844 if (key1 != 0 || key2 != 0) { 2845 res = TEE_ERROR_BAD_PARAMETERS; 2846 } else { 2847 res = crypto_hash_alloc_ctx(&cs->ctx, algo); 2848 if (res != TEE_SUCCESS) 2849 break; 2850 } 2851 break; 2852 case TEE_OPERATION_ASYMMETRIC_CIPHER: 2853 case TEE_OPERATION_ASYMMETRIC_SIGNATURE: 2854 if (algo == TEE_ALG_RSASSA_PKCS1_V1_5 && 2855 !IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 2856 res = TEE_ERROR_NOT_SUPPORTED; 2857 break; 2858 } 2859 if (key1 == 0 || key2 != 0) 2860 res = TEE_ERROR_BAD_PARAMETERS; 2861 break; 2862 case TEE_OPERATION_KEY_DERIVATION: 2863 if (algo == TEE_ALG_SM2_KEP) { 2864 if (key1 == 0 || key2 == 0) 2865 res = TEE_ERROR_BAD_PARAMETERS; 2866 } else { 2867 if (key1 == 0 || key2 != 0) 2868 res = TEE_ERROR_BAD_PARAMETERS; 2869 } 2870 break; 2871 default: 2872 res = TEE_ERROR_NOT_SUPPORTED; 2873 break; 2874 } 2875 if (res != TEE_SUCCESS) 2876 goto out; 2877 2878 res = copy_kaddr_to_uref(state, cs); 2879 if (res != TEE_SUCCESS) 2880 goto out; 2881 2882 /* Register keys */ 2883 if (o1 != NULL) { 2884 o1->busy = true; 2885 cs->key1 = (vaddr_t)o1; 2886 } 2887 if (o2 != NULL) { 2888 o2->busy = true; 2889 cs->key2 = (vaddr_t)o2; 2890 } 2891 2892 out: 2893 if (res != TEE_SUCCESS) 2894 cryp_state_free(utc, cs); 2895 return res; 2896 } 2897 2898 TEE_Result syscall_cryp_state_copy(unsigned long dst, unsigned long src) 2899 { 2900 struct ts_session *sess = ts_get_current_session(); 2901 TEE_Result res = TEE_SUCCESS; 2902 struct tee_cryp_state *cs_dst = NULL; 2903 struct tee_cryp_state *cs_src = NULL; 2904 2905 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(dst), &cs_dst); 2906 if (res != TEE_SUCCESS) 2907 return res; 2908 2909 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(src), &cs_src); 2910 if (res != TEE_SUCCESS) 2911 return res; 2912 if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode) 2913 return TEE_ERROR_BAD_PARAMETERS; 2914 2915 switch (TEE_ALG_GET_CLASS(cs_src->algo)) { 2916 case TEE_OPERATION_CIPHER: 2917 crypto_cipher_copy_state(cs_dst->ctx, cs_src->ctx); 2918 break; 2919 case TEE_OPERATION_AE: 2920 crypto_authenc_copy_state(cs_dst->ctx, cs_src->ctx); 2921 break; 2922 case TEE_OPERATION_DIGEST: 2923 crypto_hash_copy_state(cs_dst->ctx, cs_src->ctx); 2924 break; 2925 case TEE_OPERATION_MAC: 2926 crypto_mac_copy_state(cs_dst->ctx, cs_src->ctx); 2927 break; 2928 default: 2929 return TEE_ERROR_BAD_STATE; 2930 } 2931 2932 cs_dst->state = cs_src->state; 2933 cs_dst->ctx_finalize = cs_src->ctx_finalize; 2934 2935 return TEE_SUCCESS; 2936 } 2937 2938 void tee_svc_cryp_free_states(struct user_ta_ctx *utc) 2939 { 2940 struct tee_cryp_state_head *states = &utc->cryp_states; 2941 2942 while (!TAILQ_EMPTY(states)) 2943 cryp_state_free(utc, TAILQ_FIRST(states)); 2944 } 2945 2946 TEE_Result syscall_cryp_state_free(unsigned long state) 2947 { 2948 struct ts_session *sess = ts_get_current_session(); 2949 TEE_Result res = TEE_SUCCESS; 2950 struct tee_cryp_state *cs = NULL; 2951 2952 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2953 if (res != TEE_SUCCESS) 2954 return res; 2955 cryp_state_free(to_user_ta_ctx(sess->ctx), cs); 2956 return TEE_SUCCESS; 2957 } 2958 2959 TEE_Result syscall_hash_init(unsigned long state, 2960 const void *iv __maybe_unused, 2961 size_t iv_len __maybe_unused) 2962 { 2963 struct ts_session *sess = ts_get_current_session(); 2964 TEE_Result res = TEE_SUCCESS; 2965 struct tee_cryp_state *cs = NULL; 2966 2967 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2968 if (res != TEE_SUCCESS) 2969 return res; 2970 2971 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2972 case TEE_OPERATION_DIGEST: 2973 res = crypto_hash_init(cs->ctx); 2974 if (res != TEE_SUCCESS) 2975 return res; 2976 break; 2977 case TEE_OPERATION_MAC: 2978 { 2979 struct tee_obj *o; 2980 struct tee_cryp_obj_secret *key; 2981 2982 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 2983 cs->key1, &o); 2984 if (res != TEE_SUCCESS) 2985 return res; 2986 if ((o->info.handleFlags & 2987 TEE_HANDLE_FLAG_INITIALIZED) == 0) 2988 return TEE_ERROR_BAD_PARAMETERS; 2989 2990 key = (struct tee_cryp_obj_secret *)o->attr; 2991 res = crypto_mac_init(cs->ctx, (void *)(key + 1), 2992 key->key_size); 2993 if (res != TEE_SUCCESS) 2994 return res; 2995 break; 2996 } 2997 default: 2998 return TEE_ERROR_BAD_PARAMETERS; 2999 } 3000 3001 cs->state = CRYP_STATE_INITIALIZED; 3002 3003 return TEE_SUCCESS; 3004 } 3005 3006 TEE_Result syscall_hash_update(unsigned long state, const void *chunk, 3007 size_t chunk_size) 3008 { 3009 struct ts_session *sess = ts_get_current_session(); 3010 struct tee_cryp_state *cs = NULL; 3011 TEE_Result res = TEE_SUCCESS; 3012 3013 /* No data, but size provided isn't valid parameters. */ 3014 if (!chunk && chunk_size) 3015 return TEE_ERROR_BAD_PARAMETERS; 3016 3017 /* Zero length hash is valid, but nothing we need to do. */ 3018 if (!chunk_size) 3019 return TEE_SUCCESS; 3020 3021 chunk = memtag_strip_tag_const(chunk); 3022 3023 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3024 TEE_MEMORY_ACCESS_READ | 3025 TEE_MEMORY_ACCESS_ANY_OWNER, 3026 (uaddr_t)chunk, chunk_size); 3027 if (res != TEE_SUCCESS) 3028 return res; 3029 3030 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3031 if (res != TEE_SUCCESS) 3032 return res; 3033 3034 if (cs->state != CRYP_STATE_INITIALIZED) 3035 return TEE_ERROR_BAD_STATE; 3036 3037 switch (TEE_ALG_GET_CLASS(cs->algo)) { 3038 case TEE_OPERATION_DIGEST: 3039 enter_user_access(); 3040 res = crypto_hash_update(cs->ctx, chunk, chunk_size); 3041 exit_user_access(); 3042 if (res != TEE_SUCCESS) 3043 return res; 3044 break; 3045 case TEE_OPERATION_MAC: 3046 enter_user_access(); 3047 res = crypto_mac_update(cs->ctx, chunk, chunk_size); 3048 exit_user_access(); 3049 if (res != TEE_SUCCESS) 3050 return res; 3051 break; 3052 default: 3053 return TEE_ERROR_BAD_PARAMETERS; 3054 } 3055 3056 return TEE_SUCCESS; 3057 } 3058 3059 static bool is_xof_algo(uint32_t algo) 3060 { 3061 return algo == TEE_ALG_SHAKE128 || algo == TEE_ALG_SHAKE256; 3062 } 3063 3064 TEE_Result syscall_hash_final(unsigned long state, const void *chunk, 3065 size_t chunk_size, void *hash, uint64_t *hash_len) 3066 { 3067 struct ts_session *sess = ts_get_current_session(); 3068 struct tee_cryp_state *cs = NULL; 3069 TEE_Result res2 = TEE_SUCCESS; 3070 TEE_Result res = TEE_SUCCESS; 3071 size_t hash_size = 0; 3072 size_t hlen = 0; 3073 3074 /* No data, but size provided isn't valid parameters. */ 3075 if (!chunk && chunk_size) 3076 return TEE_ERROR_BAD_PARAMETERS; 3077 3078 chunk = memtag_strip_tag_const(chunk); 3079 hash = memtag_strip_tag(hash); 3080 3081 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3082 TEE_MEMORY_ACCESS_READ | 3083 TEE_MEMORY_ACCESS_ANY_OWNER, 3084 (uaddr_t)chunk, chunk_size); 3085 if (res != TEE_SUCCESS) 3086 return res; 3087 3088 res = get_user_u64_as_size_t(&hlen, hash_len); 3089 if (res != TEE_SUCCESS) 3090 return res; 3091 3092 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3093 TEE_MEMORY_ACCESS_READ | 3094 TEE_MEMORY_ACCESS_WRITE | 3095 TEE_MEMORY_ACCESS_ANY_OWNER, 3096 (uaddr_t)hash, hlen); 3097 if (res != TEE_SUCCESS) 3098 return res; 3099 3100 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3101 if (res != TEE_SUCCESS) 3102 return res; 3103 3104 if (cs->state != CRYP_STATE_INITIALIZED) 3105 return TEE_ERROR_BAD_STATE; 3106 3107 switch (TEE_ALG_GET_CLASS(cs->algo)) { 3108 case TEE_OPERATION_DIGEST: 3109 if (is_xof_algo(cs->algo)) { 3110 if (chunk_size) { 3111 enter_user_access(); 3112 res = crypto_hash_update(cs->ctx, chunk, 3113 chunk_size); 3114 exit_user_access(); 3115 if (res) 3116 return res; 3117 } 3118 3119 /* 3120 * hash_size is supposed to be unchanged for XOF 3121 * algorithms so return directly. 3122 */ 3123 enter_user_access(); 3124 res = crypto_hash_final(cs->ctx, hash, hlen); 3125 exit_user_access(); 3126 return res; 3127 } 3128 3129 res = tee_alg_get_digest_size(cs->algo, &hash_size); 3130 if (res != TEE_SUCCESS) 3131 return res; 3132 if (hlen < hash_size) { 3133 res = TEE_ERROR_SHORT_BUFFER; 3134 goto out; 3135 } 3136 3137 if (chunk_size) { 3138 enter_user_access(); 3139 res = crypto_hash_update(cs->ctx, chunk, chunk_size); 3140 exit_user_access(); 3141 if (res != TEE_SUCCESS) 3142 return res; 3143 } 3144 3145 enter_user_access(); 3146 res = crypto_hash_final(cs->ctx, hash, hash_size); 3147 exit_user_access(); 3148 if (res != TEE_SUCCESS) 3149 return res; 3150 break; 3151 3152 case TEE_OPERATION_MAC: 3153 res = tee_alg_get_digest_size(cs->algo, &hash_size); 3154 if (res != TEE_SUCCESS) 3155 return res; 3156 if (hlen < hash_size) { 3157 res = TEE_ERROR_SHORT_BUFFER; 3158 goto out; 3159 } 3160 3161 if (chunk_size) { 3162 enter_user_access(); 3163 res = crypto_mac_update(cs->ctx, chunk, chunk_size); 3164 exit_user_access(); 3165 if (res != TEE_SUCCESS) 3166 return res; 3167 } 3168 3169 enter_user_access(); 3170 res = crypto_mac_final(cs->ctx, hash, hash_size); 3171 exit_user_access(); 3172 if (res != TEE_SUCCESS) 3173 return res; 3174 break; 3175 3176 default: 3177 return TEE_ERROR_BAD_PARAMETERS; 3178 } 3179 out: 3180 res2 = put_user_u64(hash_len, hash_size); 3181 if (res2 != TEE_SUCCESS) 3182 return res2; 3183 return res; 3184 } 3185 3186 TEE_Result syscall_cipher_init(unsigned long state, const void *iv, 3187 size_t iv_len) 3188 { 3189 struct ts_session *sess = ts_get_current_session(); 3190 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 3191 struct tee_cryp_obj_secret *key1 = NULL; 3192 struct tee_cryp_state *cs = NULL; 3193 TEE_Result res = TEE_SUCCESS; 3194 struct tee_obj *o = NULL; 3195 void *iv_bbuf = NULL; 3196 3197 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3198 if (res != TEE_SUCCESS) 3199 return res; 3200 3201 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_CIPHER) 3202 return TEE_ERROR_BAD_STATE; 3203 3204 res = tee_obj_get(utc, cs->key1, &o); 3205 if (res != TEE_SUCCESS) 3206 return res; 3207 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 3208 return TEE_ERROR_BAD_PARAMETERS; 3209 3210 key1 = o->attr; 3211 3212 res = bb_memdup_user(iv, iv_len, &iv_bbuf); 3213 if (res) 3214 return res; 3215 3216 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) { 3217 struct tee_cryp_obj_secret *key2 = o->attr; 3218 3219 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 3220 return TEE_ERROR_BAD_PARAMETERS; 3221 3222 res = crypto_cipher_init(cs->ctx, cs->mode, 3223 (uint8_t *)(key1 + 1), key1->key_size, 3224 (uint8_t *)(key2 + 1), key2->key_size, 3225 iv_bbuf, iv_len); 3226 } else { 3227 res = crypto_cipher_init(cs->ctx, cs->mode, 3228 (uint8_t *)(key1 + 1), key1->key_size, 3229 NULL, 0, iv_bbuf, iv_len); 3230 } 3231 if (res != TEE_SUCCESS) 3232 return res; 3233 3234 cs->ctx_finalize = crypto_cipher_final; 3235 cs->state = CRYP_STATE_INITIALIZED; 3236 3237 return TEE_SUCCESS; 3238 } 3239 3240 static TEE_Result tee_svc_cipher_update_helper(unsigned long state, 3241 bool last_block, const void *src, size_t src_len, 3242 void *dst, uint64_t *dst_len) 3243 { 3244 struct ts_session *sess = ts_get_current_session(); 3245 struct tee_cryp_state *cs = NULL; 3246 TEE_Result res = TEE_SUCCESS; 3247 size_t dlen = 0; 3248 3249 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3250 if (res != TEE_SUCCESS) 3251 return res; 3252 3253 if (cs->state != CRYP_STATE_INITIALIZED) 3254 return TEE_ERROR_BAD_STATE; 3255 3256 src = memtag_strip_tag_const(src); 3257 dst = memtag_strip_tag(dst); 3258 3259 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3260 TEE_MEMORY_ACCESS_READ | 3261 TEE_MEMORY_ACCESS_ANY_OWNER, 3262 (uaddr_t)src, src_len); 3263 if (res != TEE_SUCCESS) 3264 return res; 3265 3266 if (!dst_len) { 3267 dlen = 0; 3268 } else { 3269 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 3270 uint32_t flags = TEE_MEMORY_ACCESS_READ | 3271 TEE_MEMORY_ACCESS_WRITE | 3272 TEE_MEMORY_ACCESS_ANY_OWNER; 3273 3274 res = get_user_u64_as_size_t(&dlen, dst_len); 3275 if (res != TEE_SUCCESS) 3276 return res; 3277 3278 res = vm_check_access_rights(uctx, flags, (uaddr_t)dst, dlen); 3279 if (res != TEE_SUCCESS) 3280 return res; 3281 } 3282 3283 if (dlen < src_len) { 3284 res = TEE_ERROR_SHORT_BUFFER; 3285 goto out; 3286 } 3287 3288 if (src_len > 0) { 3289 /* Permit src_len == 0 to finalize the operation */ 3290 enter_user_access(); 3291 res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode, 3292 last_block, src, src_len, dst); 3293 exit_user_access(); 3294 } 3295 3296 if (last_block && cs->ctx_finalize != NULL) { 3297 cs->ctx_finalize(cs->ctx); 3298 cs->ctx_finalize = NULL; 3299 } 3300 3301 out: 3302 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 3303 dst_len != NULL) { 3304 TEE_Result res2; 3305 3306 res2 = put_user_u64(dst_len, src_len); 3307 if (res2 != TEE_SUCCESS) 3308 res = res2; 3309 } 3310 3311 return res; 3312 } 3313 3314 TEE_Result syscall_cipher_update(unsigned long state, const void *src, 3315 size_t src_len, void *dst, uint64_t *dst_len) 3316 { 3317 return tee_svc_cipher_update_helper(state, false /* last_block */, 3318 src, src_len, dst, dst_len); 3319 } 3320 3321 TEE_Result syscall_cipher_final(unsigned long state, const void *src, 3322 size_t src_len, void *dst, uint64_t *dst_len) 3323 { 3324 return tee_svc_cipher_update_helper(state, true /* last_block */, 3325 src, src_len, dst, dst_len); 3326 } 3327 3328 #if defined(CFG_CRYPTO_HKDF) 3329 static TEE_Result get_hkdf_params(uint32_t algo, const TEE_Attribute *params, 3330 uint32_t param_count, 3331 void **salt, size_t *salt_len, void **info, 3332 size_t *info_len, size_t *okm_len, 3333 uint32_t *hash_id) 3334 { 3335 TEE_Result res = TEE_SUCCESS; 3336 size_t n; 3337 enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4, HASH = 0x8 }; 3338 uint8_t found = 0; 3339 3340 *salt = *info = NULL; 3341 *salt_len = *info_len = *okm_len = 0; 3342 3343 if (algo == TEE_ALG_HKDF) { 3344 *hash_id = TEE_ALG_SHA256; 3345 } else { 3346 *hash_id = TEE_ALG_GET_DIGEST_HASH(algo); 3347 found |= HASH; 3348 } 3349 3350 for (n = 0; n < param_count; n++) { 3351 const TEE_Attribute *p = ¶ms[n]; 3352 3353 switch (p->attributeID) { 3354 case __OPTEE_TEE_ATTR_HKDF_SALT: 3355 case TEE_ATTR_HKDF_SALT: 3356 if (!(found & SALT)) { 3357 *salt_len = p->content.ref.length; 3358 res = bb_memdup_user(p->content.ref.buffer, 3359 *salt_len, salt); 3360 if (res) 3361 return res; 3362 3363 found |= SALT; 3364 } 3365 break; 3366 case TEE_ATTR_KDF_KEY_SIZE: 3367 case TEE_ATTR_HKDF_OKM_LENGTH: 3368 if (!(found & LENGTH)) { 3369 *okm_len = p->content.value.a; 3370 found |= LENGTH; 3371 } 3372 break; 3373 case __OPTEE_ATTR_HKDF_INFO: 3374 case TEE_ATTR_HKDF_INFO: 3375 if (!(found & INFO)) { 3376 *info_len = p->content.ref.length; 3377 res = bb_memdup_user(p->content.ref.buffer, 3378 *info_len, info); 3379 if (res) 3380 return res; 3381 3382 found |= INFO; 3383 } 3384 break; 3385 case TEE_ATTR_HKDF_HASH_ALGORITHM: 3386 if (!(found & HASH)) { 3387 *hash_id = p->content.value.a; 3388 found |= HASH; 3389 } 3390 break; 3391 default: 3392 /* Unexpected attribute */ 3393 return TEE_ERROR_BAD_PARAMETERS; 3394 } 3395 3396 } 3397 3398 if (!(found & LENGTH)) 3399 return TEE_ERROR_BAD_PARAMETERS; 3400 3401 return TEE_SUCCESS; 3402 } 3403 #endif 3404 3405 #if defined(CFG_CRYPTO_CONCAT_KDF) 3406 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params, 3407 uint32_t param_count, 3408 void **other_info, 3409 size_t *other_info_len, 3410 size_t *derived_key_len) 3411 { 3412 size_t n; 3413 enum { LENGTH = 0x1, INFO = 0x2 }; 3414 uint8_t found = 0; 3415 3416 *other_info = NULL; 3417 *other_info_len = *derived_key_len = 0; 3418 3419 for (n = 0; n < param_count; n++) { 3420 const TEE_Attribute *p = ¶ms[n]; 3421 3422 switch (p->attributeID) { 3423 case TEE_ATTR_CONCAT_KDF_OTHER_INFO: 3424 if (!(found & INFO)) { 3425 TEE_Result res = TEE_SUCCESS; 3426 3427 *other_info_len = p->content.ref.length; 3428 res = bb_memdup_user(p->content.ref.buffer, 3429 *other_info_len, 3430 other_info); 3431 if (res) 3432 return res; 3433 3434 found |= INFO; 3435 } 3436 break; 3437 case TEE_ATTR_CONCAT_KDF_DKM_LENGTH: 3438 if (!(found & LENGTH)) { 3439 *derived_key_len = p->content.value.a; 3440 found |= LENGTH; 3441 } 3442 break; 3443 default: 3444 /* Unexpected attribute */ 3445 return TEE_ERROR_BAD_PARAMETERS; 3446 } 3447 } 3448 3449 if (!(found & LENGTH)) 3450 return TEE_ERROR_BAD_PARAMETERS; 3451 3452 return TEE_SUCCESS; 3453 } 3454 #endif 3455 3456 #if defined(CFG_CRYPTO_PBKDF2) 3457 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params, 3458 uint32_t param_count, void **salt, 3459 size_t *salt_len, size_t *derived_key_len, 3460 size_t *iteration_count) 3461 { 3462 size_t n; 3463 enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 }; 3464 uint8_t found = 0; 3465 3466 *salt = NULL; 3467 *salt_len = *derived_key_len = *iteration_count = 0; 3468 3469 for (n = 0; n < param_count; n++) { 3470 const TEE_Attribute *p = ¶ms[n]; 3471 3472 switch (p->attributeID) { 3473 case TEE_ATTR_PBKDF2_SALT: 3474 if (!(found & SALT)) { 3475 TEE_Result res = TEE_SUCCESS; 3476 3477 *salt_len = p->content.ref.length; 3478 res = bb_memdup_user(p->content.ref.buffer, 3479 *salt_len, salt); 3480 if (res) 3481 return res; 3482 3483 found |= SALT; 3484 } 3485 break; 3486 case TEE_ATTR_PBKDF2_DKM_LENGTH: 3487 if (!(found & LENGTH)) { 3488 *derived_key_len = p->content.value.a; 3489 found |= LENGTH; 3490 } 3491 break; 3492 case TEE_ATTR_PBKDF2_ITERATION_COUNT: 3493 if (!(found & COUNT)) { 3494 *iteration_count = p->content.value.a; 3495 found |= COUNT; 3496 } 3497 break; 3498 default: 3499 /* Unexpected attribute */ 3500 return TEE_ERROR_BAD_PARAMETERS; 3501 } 3502 } 3503 3504 if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT)) 3505 return TEE_ERROR_BAD_PARAMETERS; 3506 3507 return TEE_SUCCESS; 3508 } 3509 #endif 3510 3511 #if defined(CFG_CRYPTO_SM2_KEP) 3512 static TEE_Result get_sm2_kep_params(const TEE_Attribute *params, 3513 uint32_t param_count, 3514 struct ecc_public_key *peer_key, 3515 struct ecc_public_key *peer_eph_key, 3516 struct sm2_kep_parms *kep_parms) 3517 { 3518 TEE_Result res = TEE_ERROR_GENERIC; 3519 size_t n; 3520 enum { 3521 IS_INITIATOR, 3522 PEER_KEY_X, 3523 PEER_KEY_Y, 3524 PEER_EPH_KEY_X, 3525 PEER_EPH_KEY_Y, 3526 INITIATOR_ID, 3527 RESPONDER_ID, 3528 }; 3529 uint8_t mandatory = BIT(IS_INITIATOR) | BIT(PEER_KEY_X) | 3530 BIT(PEER_KEY_Y) | BIT(PEER_EPH_KEY_X) | BIT(PEER_EPH_KEY_Y) | 3531 BIT(INITIATOR_ID) | BIT(RESPONDER_ID); 3532 uint8_t found = 0; 3533 3534 res = crypto_acipher_alloc_ecc_public_key(peer_key, 3535 TEE_TYPE_SM2_KEP_PUBLIC_KEY, 3536 256); 3537 if (res) 3538 return res; 3539 3540 res = crypto_acipher_alloc_ecc_public_key(peer_eph_key, 3541 TEE_TYPE_SM2_KEP_PUBLIC_KEY, 3542 256); 3543 if (res) 3544 goto out_p; 3545 3546 peer_key->curve = TEE_ECC_CURVE_SM2; 3547 peer_eph_key->curve = TEE_ECC_CURVE_SM2; 3548 3549 for (n = 0; n < param_count; n++) { 3550 const TEE_Attribute *p = ¶ms[n]; 3551 void *bbuf = NULL; 3552 3553 switch (p->attributeID) { 3554 case TEE_ATTR_SM2_KEP_USER: 3555 kep_parms->is_initiator = !p->content.value.a; 3556 found |= BIT(IS_INITIATOR); 3557 break; 3558 case TEE_ATTR_ECC_PUBLIC_VALUE_X: 3559 res = bb_memdup_user(p->content.ref.buffer, 3560 p->content.ref.length, 3561 &bbuf); 3562 if (res) 3563 return res; 3564 3565 crypto_bignum_bin2bn(bbuf, 3566 p->content.ref.length, 3567 peer_key->x); 3568 found |= BIT(PEER_KEY_X); 3569 bb_free(bbuf, p->content.ref.length); 3570 break; 3571 case TEE_ATTR_ECC_PUBLIC_VALUE_Y: 3572 res = bb_memdup_user(p->content.ref.buffer, 3573 p->content.ref.length, 3574 &bbuf); 3575 if (res) 3576 return res; 3577 3578 crypto_bignum_bin2bn(bbuf, 3579 p->content.ref.length, 3580 peer_key->y); 3581 found |= BIT(PEER_KEY_Y); 3582 bb_free(bbuf, p->content.ref.length); 3583 break; 3584 case __OPTEE_SM2_KEP_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_X: 3585 case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_X: 3586 res = bb_memdup_user(p->content.ref.buffer, 3587 p->content.ref.length, 3588 &bbuf); 3589 if (res) 3590 return res; 3591 3592 crypto_bignum_bin2bn(bbuf, 3593 p->content.ref.length, 3594 peer_eph_key->x); 3595 found |= BIT(PEER_EPH_KEY_X); 3596 bb_free(bbuf, p->content.ref.length); 3597 break; 3598 case __OPTEE_SM2_KEP_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_Y: 3599 case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_Y: 3600 res = bb_memdup_user(p->content.ref.buffer, 3601 p->content.ref.length, 3602 &bbuf); 3603 if (res) 3604 return res; 3605 3606 crypto_bignum_bin2bn(bbuf, 3607 p->content.ref.length, 3608 peer_eph_key->y); 3609 found |= BIT(PEER_EPH_KEY_Y); 3610 bb_free(bbuf, p->content.ref.length); 3611 break; 3612 case TEE_ATTR_SM2_ID_INITIATOR: 3613 res = bb_memdup_user(p->content.ref.buffer, 3614 p->content.ref.length, 3615 &bbuf); 3616 if (res) 3617 return res; 3618 3619 kep_parms->initiator_id = bbuf; 3620 kep_parms->initiator_id_len = p->content.ref.length; 3621 found |= BIT(INITIATOR_ID); 3622 break; 3623 case TEE_ATTR_SM2_ID_RESPONDER: 3624 res = bb_memdup_user(p->content.ref.buffer, 3625 p->content.ref.length, 3626 &bbuf); 3627 if (res) 3628 return res; 3629 3630 kep_parms->responder_id = bbuf; 3631 kep_parms->responder_id_len = p->content.ref.length; 3632 found |= BIT(RESPONDER_ID); 3633 break; 3634 case TEE_ATTR_SM2_KEP_CONFIRMATION_IN: 3635 res = bb_memdup_user(p->content.ref.buffer, 3636 p->content.ref.length, 3637 &bbuf); 3638 if (res) 3639 return res; 3640 3641 kep_parms->conf_in = bbuf; 3642 kep_parms->conf_in_len = p->content.ref.length; 3643 break; 3644 case TEE_ATTR_SM2_KEP_CONFIRMATION_OUT: 3645 res = bb_memdup_user(p->content.ref.buffer, 3646 p->content.ref.length, 3647 &bbuf); 3648 if (res) 3649 return res; 3650 3651 kep_parms->conf_out = bbuf; 3652 kep_parms->conf_out_len = p->content.ref.length; 3653 break; 3654 default: 3655 /* Unexpected attribute */ 3656 res = TEE_ERROR_BAD_PARAMETERS; 3657 goto out; 3658 } 3659 } 3660 3661 if ((found & mandatory) != mandatory) { 3662 res = TEE_ERROR_BAD_PARAMETERS; 3663 goto out; 3664 } 3665 3666 return TEE_SUCCESS; 3667 out: 3668 crypto_acipher_free_ecc_public_key(peer_eph_key); 3669 out_p: 3670 crypto_acipher_free_ecc_public_key(peer_key); 3671 return res; 3672 } 3673 #endif 3674 3675 TEE_Result syscall_cryp_derive_key(unsigned long state, 3676 const struct utee_attribute *usr_params, 3677 unsigned long param_count, unsigned long derived_key) 3678 { 3679 struct ts_session *sess = ts_get_current_session(); 3680 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 3681 TEE_Result res = TEE_ERROR_NOT_SUPPORTED; 3682 struct tee_obj *ko = NULL; 3683 struct tee_obj *so = NULL; 3684 struct tee_cryp_state *cs = NULL; 3685 struct tee_cryp_obj_secret *sk = NULL; 3686 const struct tee_cryp_obj_type_props *type_props = NULL; 3687 TEE_Attribute *params = NULL; 3688 size_t alloc_size = 0; 3689 3690 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3691 if (res != TEE_SUCCESS) 3692 return res; 3693 3694 if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size)) 3695 return TEE_ERROR_OVERFLOW; 3696 3697 params = malloc(alloc_size); 3698 if (!params) 3699 return TEE_ERROR_OUT_OF_MEMORY; 3700 res = copy_in_attrs(utc, usr_params, param_count, params); 3701 if (res != TEE_SUCCESS) 3702 goto out; 3703 3704 /* Get key set in operation */ 3705 res = tee_obj_get(utc, cs->key1, &ko); 3706 if (res != TEE_SUCCESS) 3707 goto out; 3708 3709 res = tee_obj_get(utc, uref_to_vaddr(derived_key), &so); 3710 if (res != TEE_SUCCESS) 3711 goto out; 3712 3713 /* Find information needed about the object to initialize */ 3714 sk = so->attr; 3715 3716 /* Find description of object */ 3717 type_props = tee_svc_find_type_props(so->info.objectType); 3718 if (!type_props) { 3719 res = TEE_ERROR_NOT_SUPPORTED; 3720 goto out; 3721 } 3722 3723 if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) { 3724 struct bignum *pub = NULL; 3725 struct bignum *ss = NULL; 3726 size_t bin_size = 0; 3727 void *bbuf = NULL; 3728 3729 if (param_count != 1 || 3730 params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE) { 3731 res = TEE_ERROR_BAD_PARAMETERS; 3732 goto out; 3733 } 3734 3735 bin_size = params[0].content.ref.length; 3736 3737 if (MUL_OVERFLOW(bin_size, 8, &alloc_size)) { 3738 res = TEE_ERROR_OVERFLOW; 3739 goto out; 3740 } 3741 3742 res = bb_memdup_user(params[0].content.ref.buffer, bin_size, 3743 &bbuf); 3744 if (res) 3745 goto out; 3746 3747 pub = crypto_bignum_allocate(alloc_size); 3748 ss = crypto_bignum_allocate(alloc_size); 3749 if (pub && ss) { 3750 crypto_bignum_bin2bn(bbuf, bin_size, pub); 3751 res = crypto_acipher_dh_shared_secret(ko->attr, 3752 pub, ss); 3753 if (res == TEE_SUCCESS) { 3754 sk->key_size = crypto_bignum_num_bytes(ss); 3755 crypto_bignum_bn2bin(ss, (uint8_t *)(sk + 1)); 3756 so->info.handleFlags |= 3757 TEE_HANDLE_FLAG_INITIALIZED; 3758 set_attribute(so, type_props, 3759 TEE_ATTR_SECRET_VALUE); 3760 } 3761 } else { 3762 res = TEE_ERROR_OUT_OF_MEMORY; 3763 } 3764 crypto_bignum_free(&pub); 3765 crypto_bignum_free(&ss); 3766 } else if (cs->algo == TEE_ALG_ECDH_DERIVE_SHARED_SECRET) { 3767 uint32_t curve = ((struct ecc_keypair *)ko->attr)->curve; 3768 struct ecc_public_key key_public = { }; 3769 uint8_t *pt_secret = NULL; 3770 unsigned long pt_secret_len = 0; 3771 uint32_t key_type = TEE_TYPE_ECDH_PUBLIC_KEY; 3772 void *x_bbuf = NULL; 3773 void *y_bbuf = NULL; 3774 3775 if (param_count != 2 || 3776 params[0].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_X || 3777 params[1].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_Y) { 3778 res = TEE_ERROR_BAD_PARAMETERS; 3779 goto out; 3780 } 3781 3782 switch (curve) { 3783 case TEE_ECC_CURVE_NIST_P192: 3784 alloc_size = 192; 3785 break; 3786 case TEE_ECC_CURVE_NIST_P224: 3787 alloc_size = 224; 3788 break; 3789 case TEE_ECC_CURVE_NIST_P256: 3790 alloc_size = 256; 3791 break; 3792 case TEE_ECC_CURVE_NIST_P384: 3793 alloc_size = 384; 3794 break; 3795 case TEE_ECC_CURVE_NIST_P521: 3796 alloc_size = 521; 3797 break; 3798 default: 3799 res = TEE_ERROR_NOT_IMPLEMENTED; 3800 goto out; 3801 } 3802 3803 res = bb_memdup_user(params[0].content.ref.buffer, 3804 params[0].content.ref.length, 3805 &x_bbuf); 3806 if (res) 3807 goto out; 3808 3809 res = bb_memdup_user(params[1].content.ref.buffer, 3810 params[1].content.ref.length, 3811 &y_bbuf); 3812 if (res) 3813 goto out; 3814 3815 /* Create the public key */ 3816 res = crypto_acipher_alloc_ecc_public_key(&key_public, key_type, 3817 alloc_size); 3818 if (res != TEE_SUCCESS) 3819 goto out; 3820 key_public.curve = curve; 3821 crypto_bignum_bin2bn(x_bbuf, params[0].content.ref.length, 3822 key_public.x); 3823 crypto_bignum_bin2bn(y_bbuf, params[1].content.ref.length, 3824 key_public.y); 3825 3826 pt_secret = (uint8_t *)(sk + 1); 3827 pt_secret_len = sk->alloc_size; 3828 res = crypto_acipher_ecc_shared_secret(ko->attr, &key_public, 3829 pt_secret, 3830 &pt_secret_len); 3831 3832 if (res == TEE_SUCCESS) { 3833 sk->key_size = pt_secret_len; 3834 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3835 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3836 } 3837 3838 /* free the public key */ 3839 crypto_acipher_free_ecc_public_key(&key_public); 3840 } 3841 #if defined(CFG_CRYPTO_HKDF) 3842 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) { 3843 void *salt, *info; 3844 size_t salt_len, info_len, okm_len; 3845 uint32_t hash_id = 0; 3846 struct tee_cryp_obj_secret *ik = ko->attr; 3847 const uint8_t *ikm = (const uint8_t *)(ik + 1); 3848 3849 res = get_hkdf_params(cs->algo, params, param_count, &salt, 3850 &salt_len, &info, &info_len, &okm_len, 3851 &hash_id); 3852 if (res != TEE_SUCCESS) 3853 goto out; 3854 3855 /* Requested size must fit into the output object's buffer */ 3856 if (okm_len > ik->alloc_size) { 3857 res = TEE_ERROR_BAD_PARAMETERS; 3858 goto out; 3859 } 3860 3861 res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len, 3862 info, info_len, (uint8_t *)(sk + 1), 3863 okm_len); 3864 if (res == TEE_SUCCESS) { 3865 sk->key_size = okm_len; 3866 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3867 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3868 } 3869 } 3870 #endif 3871 #if defined(CFG_CRYPTO_CONCAT_KDF) 3872 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) { 3873 void *info; 3874 size_t info_len, derived_key_len; 3875 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3876 struct tee_cryp_obj_secret *ss = ko->attr; 3877 const uint8_t *shared_secret = (const uint8_t *)(ss + 1); 3878 3879 res = get_concat_kdf_params(params, param_count, &info, 3880 &info_len, &derived_key_len); 3881 if (res != TEE_SUCCESS) 3882 goto out; 3883 3884 /* Requested size must fit into the output object's buffer */ 3885 if (derived_key_len > ss->alloc_size) { 3886 res = TEE_ERROR_BAD_PARAMETERS; 3887 goto out; 3888 } 3889 3890 res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size, 3891 info, info_len, (uint8_t *)(sk + 1), 3892 derived_key_len); 3893 if (res == TEE_SUCCESS) { 3894 sk->key_size = derived_key_len; 3895 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3896 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3897 } 3898 } 3899 #endif 3900 #if defined(CFG_CRYPTO_PBKDF2) 3901 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) { 3902 void *salt; 3903 size_t salt_len, iteration_count, derived_key_len; 3904 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3905 struct tee_cryp_obj_secret *ss = ko->attr; 3906 const uint8_t *password = (const uint8_t *)(ss + 1); 3907 3908 res = get_pbkdf2_params(params, param_count, &salt, &salt_len, 3909 &derived_key_len, &iteration_count); 3910 if (res != TEE_SUCCESS) 3911 goto out; 3912 3913 /* Requested size must fit into the output object's buffer */ 3914 if (derived_key_len > ss->alloc_size) { 3915 res = TEE_ERROR_BAD_PARAMETERS; 3916 goto out; 3917 } 3918 3919 res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt, 3920 salt_len, iteration_count, 3921 (uint8_t *)(sk + 1), derived_key_len); 3922 if (res == TEE_SUCCESS) { 3923 sk->key_size = derived_key_len; 3924 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3925 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3926 } 3927 } 3928 #endif 3929 #if defined(CFG_CRYPTO_SM2_KEP) 3930 else if (cs->algo == TEE_ALG_SM2_KEP) { 3931 struct ecc_public_key peer_eph_key = { }; 3932 struct ecc_public_key peer_key = { }; 3933 struct sm2_kep_parms kep_parms = { 3934 .out = (uint8_t *)(sk + 1), 3935 .out_len = so->info.maxObjectSize, 3936 }; 3937 struct tee_obj *ko2 = NULL; 3938 3939 res = tee_obj_get(utc, cs->key2, &ko2); 3940 if (res != TEE_SUCCESS) 3941 goto out; 3942 3943 res = get_sm2_kep_params(params, param_count, &peer_key, 3944 &peer_eph_key, &kep_parms); 3945 if (res != TEE_SUCCESS) 3946 goto out; 3947 3948 /* 3949 * key1 is our private keypair, key2 is our ephemeral public key 3950 */ 3951 res = crypto_acipher_sm2_kep_derive(ko->attr, /* key1 */ 3952 ko2->attr, /* key2 */ 3953 &peer_key, &peer_eph_key, 3954 &kep_parms); 3955 3956 if (res == TEE_SUCCESS) { 3957 sk->key_size = kep_parms.out_len; 3958 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3959 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3960 } 3961 crypto_acipher_free_ecc_public_key(&peer_key); 3962 crypto_acipher_free_ecc_public_key(&peer_eph_key); 3963 } 3964 #endif 3965 #if defined(CFG_CRYPTO_X25519) 3966 else if (cs->algo == TEE_ALG_X25519) { 3967 uint8_t *x25519_pub_key = NULL; 3968 uint8_t *pt_secret = NULL; 3969 unsigned long pt_secret_len = 0; 3970 void *bbuf = NULL; 3971 3972 if (param_count != 1 || 3973 params[0].attributeID != TEE_ATTR_X25519_PUBLIC_VALUE) { 3974 res = TEE_ERROR_BAD_PARAMETERS; 3975 goto out; 3976 } 3977 3978 /* X25519 public key size is 32 bytes */ 3979 if (params[0].content.ref.length != KEY_SIZE_BYTES_25519) { 3980 res = TEE_ERROR_BAD_PARAMETERS; 3981 goto out; 3982 } 3983 3984 res = bb_memdup_user(params[0].content.ref.buffer, 3985 params[0].content.ref.length, 3986 &bbuf); 3987 if (res) 3988 goto out; 3989 3990 /* Set the public key */ 3991 x25519_pub_key = bbuf; 3992 3993 pt_secret = (uint8_t *)(sk + 1); 3994 pt_secret_len = sk->alloc_size; 3995 res = crypto_acipher_x25519_shared_secret(ko->attr, 3996 x25519_pub_key, 3997 pt_secret, 3998 &pt_secret_len); 3999 4000 if (res == TEE_SUCCESS) { 4001 sk->key_size = pt_secret_len; 4002 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 4003 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 4004 } 4005 } 4006 #endif 4007 #if defined(CFG_CRYPTO_X448) 4008 else if (cs->algo == TEE_ALG_X448) { 4009 uint8_t *x448_pub_key = NULL; 4010 uint8_t *pt_secret = NULL; 4011 unsigned long pt_secret_len = 0; 4012 void *bbuf = NULL; 4013 4014 if (param_count != 1 || 4015 params[0].attributeID != TEE_ATTR_X448_PUBLIC_VALUE) { 4016 res = TEE_ERROR_BAD_PARAMETERS; 4017 goto out; 4018 } 4019 4020 /* X448 public key size is 56 bytes */ 4021 if (params[0].content.ref.length != KEY_SIZE_BYTES_448) { 4022 res = TEE_ERROR_BAD_PARAMETERS; 4023 goto out; 4024 } 4025 4026 res = bb_memdup_user(params[0].content.ref.buffer, 4027 params[0].content.ref.length, 4028 &bbuf); 4029 if (res) 4030 goto out; 4031 4032 /* Set the public key */ 4033 x448_pub_key = bbuf; 4034 4035 pt_secret = (uint8_t *)(sk + 1); 4036 pt_secret_len = sk->alloc_size; 4037 res = crypto_acipher_x448_shared_secret(ko->attr, 4038 x448_pub_key, 4039 pt_secret, 4040 &pt_secret_len); 4041 4042 if (res == TEE_SUCCESS) { 4043 sk->key_size = pt_secret_len; 4044 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 4045 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 4046 } 4047 } 4048 #endif 4049 else 4050 res = TEE_ERROR_NOT_SUPPORTED; 4051 4052 out: 4053 free_wipe(params); 4054 return res; 4055 } 4056 4057 TEE_Result syscall_cryp_random_number_generate(void *buf, size_t blen) 4058 { 4059 TEE_Result res = TEE_SUCCESS; 4060 void *bbuf = NULL; 4061 4062 bbuf = bb_alloc(blen); 4063 if (!bbuf) 4064 return TEE_ERROR_OUT_OF_MEMORY; 4065 4066 res = crypto_rng_read(bbuf, blen); 4067 if (res != TEE_SUCCESS) 4068 return res; 4069 4070 res = copy_to_user(buf, bbuf, blen); 4071 return res; 4072 } 4073 4074 TEE_Result syscall_authenc_init(unsigned long state, const void *nonce, 4075 size_t nonce_len, size_t tag_len, 4076 size_t aad_len, size_t payload_len) 4077 { 4078 struct ts_session *sess = ts_get_current_session(); 4079 struct tee_cryp_obj_secret *key = NULL; 4080 struct tee_cryp_state *cs = NULL; 4081 TEE_Result res = TEE_SUCCESS; 4082 struct tee_obj *o = NULL; 4083 void *nonce_bbuf = NULL; 4084 4085 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4086 if (res != TEE_SUCCESS) 4087 return res; 4088 4089 res = tee_obj_get(to_user_ta_ctx(sess->ctx), cs->key1, &o); 4090 if (res != TEE_SUCCESS) 4091 return res; 4092 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 4093 return TEE_ERROR_BAD_PARAMETERS; 4094 4095 key = o->attr; 4096 4097 res = bb_memdup_user(nonce, nonce_len, &nonce_bbuf); 4098 if (res) 4099 return res; 4100 4101 res = crypto_authenc_init(cs->ctx, cs->mode, (uint8_t *)(key + 1), 4102 key->key_size, nonce_bbuf, nonce_len, tag_len, 4103 aad_len, payload_len); 4104 if (res != TEE_SUCCESS) 4105 return res; 4106 4107 cs->ctx_finalize = crypto_authenc_final; 4108 cs->state = CRYP_STATE_INITIALIZED; 4109 4110 return TEE_SUCCESS; 4111 } 4112 4113 TEE_Result syscall_authenc_update_aad(unsigned long state, 4114 const void *aad_data, size_t aad_data_len) 4115 { 4116 struct ts_session *sess = ts_get_current_session(); 4117 TEE_Result res = TEE_SUCCESS; 4118 struct tee_cryp_state *cs = NULL; 4119 4120 aad_data = memtag_strip_tag_const(aad_data); 4121 4122 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 4123 TEE_MEMORY_ACCESS_READ | 4124 TEE_MEMORY_ACCESS_ANY_OWNER, 4125 (uaddr_t)aad_data, aad_data_len); 4126 if (res != TEE_SUCCESS) 4127 return res; 4128 4129 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4130 if (res != TEE_SUCCESS) 4131 return res; 4132 4133 if (cs->state != CRYP_STATE_INITIALIZED) 4134 return TEE_ERROR_BAD_STATE; 4135 4136 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 4137 return TEE_ERROR_BAD_STATE; 4138 4139 enter_user_access(); 4140 res = crypto_authenc_update_aad(cs->ctx, cs->mode, aad_data, 4141 aad_data_len); 4142 exit_user_access(); 4143 if (res != TEE_SUCCESS) 4144 return res; 4145 4146 return TEE_SUCCESS; 4147 } 4148 4149 TEE_Result syscall_authenc_update_payload(unsigned long state, 4150 const void *src_data, 4151 size_t src_len, void *dst_data, 4152 uint64_t *dst_len) 4153 { 4154 struct ts_session *sess = ts_get_current_session(); 4155 struct tee_cryp_state *cs = NULL; 4156 TEE_Result res = TEE_SUCCESS; 4157 size_t dlen = 0; 4158 4159 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4160 if (res != TEE_SUCCESS) 4161 return res; 4162 4163 if (cs->state != CRYP_STATE_INITIALIZED) 4164 return TEE_ERROR_BAD_STATE; 4165 4166 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 4167 return TEE_ERROR_BAD_STATE; 4168 4169 src_data = memtag_strip_tag_const(src_data); 4170 dst_data = memtag_strip_tag(dst_data); 4171 4172 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 4173 TEE_MEMORY_ACCESS_READ | 4174 TEE_MEMORY_ACCESS_ANY_OWNER, 4175 (uaddr_t)src_data, src_len); 4176 if (res != TEE_SUCCESS) 4177 return res; 4178 4179 res = get_user_u64_as_size_t(&dlen, dst_len); 4180 if (res != TEE_SUCCESS) 4181 return res; 4182 4183 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 4184 TEE_MEMORY_ACCESS_READ | 4185 TEE_MEMORY_ACCESS_WRITE | 4186 TEE_MEMORY_ACCESS_ANY_OWNER, 4187 (uaddr_t)dst_data, dlen); 4188 if (res != TEE_SUCCESS) 4189 return res; 4190 4191 if (dlen < src_len) { 4192 res = TEE_ERROR_SHORT_BUFFER; 4193 goto out; 4194 } 4195 4196 enter_user_access(); 4197 res = crypto_authenc_update_payload(cs->ctx, cs->mode, src_data, 4198 src_len, dst_data, &dlen); 4199 exit_user_access(); 4200 out: 4201 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 4202 TEE_Result res2 = put_user_u64(dst_len, dlen); 4203 4204 if (res2 != TEE_SUCCESS) 4205 res = res2; 4206 } 4207 4208 return res; 4209 } 4210 4211 TEE_Result syscall_authenc_enc_final(unsigned long state, const void *src_data, 4212 size_t src_len, void *dst_data, 4213 uint64_t *dst_len, void *tag, 4214 uint64_t *tag_len) 4215 { 4216 struct ts_session *sess = ts_get_current_session(); 4217 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 4218 struct tee_cryp_state *cs = NULL; 4219 TEE_Result res = TEE_SUCCESS; 4220 size_t dlen = 0; 4221 size_t tlen = 0; 4222 4223 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4224 if (res != TEE_SUCCESS) 4225 return res; 4226 4227 if (cs->state != CRYP_STATE_INITIALIZED) 4228 return TEE_ERROR_BAD_STATE; 4229 4230 if (cs->mode != TEE_MODE_ENCRYPT) 4231 return TEE_ERROR_BAD_PARAMETERS; 4232 4233 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 4234 return TEE_ERROR_BAD_STATE; 4235 4236 src_data = memtag_strip_tag_const(src_data); 4237 dst_data = memtag_strip_tag(dst_data); 4238 tag = memtag_strip_tag(tag); 4239 4240 res = vm_check_access_rights(uctx, 4241 TEE_MEMORY_ACCESS_READ | 4242 TEE_MEMORY_ACCESS_ANY_OWNER, 4243 (uaddr_t)src_data, src_len); 4244 if (res != TEE_SUCCESS) 4245 return res; 4246 4247 if (!dst_len) { 4248 dlen = 0; 4249 } else { 4250 res = get_user_u64_as_size_t(&dlen, dst_len); 4251 if (res != TEE_SUCCESS) 4252 return res; 4253 4254 res = vm_check_access_rights(uctx, 4255 TEE_MEMORY_ACCESS_READ | 4256 TEE_MEMORY_ACCESS_WRITE | 4257 TEE_MEMORY_ACCESS_ANY_OWNER, 4258 (uaddr_t)dst_data, dlen); 4259 if (res != TEE_SUCCESS) 4260 return res; 4261 } 4262 4263 if (dlen < src_len) { 4264 res = TEE_ERROR_SHORT_BUFFER; 4265 goto out; 4266 } 4267 4268 res = get_user_u64_as_size_t(&tlen, tag_len); 4269 if (res != TEE_SUCCESS) 4270 return res; 4271 4272 res = vm_check_access_rights(uctx, 4273 TEE_MEMORY_ACCESS_READ | 4274 TEE_MEMORY_ACCESS_WRITE | 4275 TEE_MEMORY_ACCESS_ANY_OWNER, 4276 (uaddr_t)tag, tlen); 4277 if (res != TEE_SUCCESS) 4278 return res; 4279 4280 enter_user_access(); 4281 res = crypto_authenc_enc_final(cs->ctx, src_data, src_len, dst_data, 4282 &dlen, tag, &tlen); 4283 exit_user_access(); 4284 4285 out: 4286 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 4287 TEE_Result res2 = TEE_SUCCESS; 4288 4289 if (dst_len != NULL) { 4290 res2 = put_user_u64(dst_len, dlen); 4291 if (res2 != TEE_SUCCESS) 4292 return res2; 4293 } 4294 4295 res2 = put_user_u64(tag_len, tlen); 4296 if (res2 != TEE_SUCCESS) 4297 return res2; 4298 } 4299 4300 return res; 4301 } 4302 4303 TEE_Result syscall_authenc_dec_final(unsigned long state, 4304 const void *src_data, size_t src_len, void *dst_data, 4305 uint64_t *dst_len, const void *tag, size_t tag_len) 4306 { 4307 struct ts_session *sess = ts_get_current_session(); 4308 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 4309 struct tee_cryp_state *cs = NULL; 4310 TEE_Result res = TEE_SUCCESS; 4311 size_t dlen = 0; 4312 4313 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4314 if (res != TEE_SUCCESS) 4315 return res; 4316 4317 if (cs->state != CRYP_STATE_INITIALIZED) 4318 return TEE_ERROR_BAD_STATE; 4319 4320 if (cs->mode != TEE_MODE_DECRYPT) 4321 return TEE_ERROR_BAD_PARAMETERS; 4322 4323 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 4324 return TEE_ERROR_BAD_STATE; 4325 4326 src_data = memtag_strip_tag_const(src_data); 4327 dst_data = memtag_strip_tag(dst_data); 4328 tag = memtag_strip_tag_const(tag); 4329 4330 res = vm_check_access_rights(uctx, 4331 TEE_MEMORY_ACCESS_READ | 4332 TEE_MEMORY_ACCESS_ANY_OWNER, 4333 (uaddr_t)src_data, src_len); 4334 if (res != TEE_SUCCESS) 4335 return res; 4336 4337 if (!dst_len) { 4338 dlen = 0; 4339 } else { 4340 res = get_user_u64_as_size_t(&dlen, dst_len); 4341 if (res != TEE_SUCCESS) 4342 return res; 4343 4344 res = vm_check_access_rights(uctx, 4345 TEE_MEMORY_ACCESS_READ | 4346 TEE_MEMORY_ACCESS_WRITE | 4347 TEE_MEMORY_ACCESS_ANY_OWNER, 4348 (uaddr_t)dst_data, dlen); 4349 if (res != TEE_SUCCESS) 4350 return res; 4351 } 4352 4353 if (dlen < src_len) { 4354 res = TEE_ERROR_SHORT_BUFFER; 4355 goto out; 4356 } 4357 4358 /* Despite TEE Internal Core API up to v1.3, tag is [inbuf], not [in] */ 4359 res = vm_check_access_rights(uctx, 4360 TEE_MEMORY_ACCESS_READ | 4361 TEE_MEMORY_ACCESS_ANY_OWNER, 4362 (uaddr_t)tag, tag_len); 4363 if (res != TEE_SUCCESS) 4364 return res; 4365 4366 enter_user_access(); 4367 res = crypto_authenc_dec_final(cs->ctx, src_data, src_len, dst_data, 4368 &dlen, tag, tag_len); 4369 exit_user_access(); 4370 4371 out: 4372 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 4373 dst_len != NULL) { 4374 TEE_Result res2 = put_user_u64(dst_len, dlen); 4375 4376 if (res2 != TEE_SUCCESS) 4377 return res2; 4378 } 4379 4380 return res; 4381 } 4382 4383 static int pkcs1_get_salt_len(const TEE_Attribute *params, uint32_t num_params, 4384 size_t default_len) 4385 { 4386 size_t n; 4387 4388 assert(default_len < INT_MAX); 4389 4390 for (n = 0; n < num_params; n++) { 4391 if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) { 4392 if (params[n].content.value.a < INT_MAX) 4393 return params[n].content.value.a; 4394 break; 4395 } 4396 } 4397 /* 4398 * If salt length isn't provided use the default value which is 4399 * the length of the digest. 4400 */ 4401 return default_len; 4402 } 4403 4404 TEE_Result syscall_asymm_operate(unsigned long state, 4405 const struct utee_attribute *usr_params, 4406 size_t num_params, const void *src_data, size_t src_len, 4407 void *dst_data, uint64_t *dst_len) 4408 { 4409 struct ts_session *sess = ts_get_current_session(); 4410 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 4411 TEE_Result res = TEE_SUCCESS; 4412 struct tee_cryp_state *cs = NULL; 4413 size_t dlen = 0; 4414 struct tee_obj *o = NULL; 4415 void *label = NULL; 4416 size_t label_len = 0; 4417 size_t n = 0; 4418 int salt_len = 0; 4419 TEE_Attribute *params = NULL; 4420 size_t alloc_size = 0; 4421 4422 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4423 if (res != TEE_SUCCESS) 4424 return res; 4425 4426 src_data = memtag_strip_tag_const(src_data); 4427 dst_data = memtag_strip_tag(dst_data); 4428 4429 res = vm_check_access_rights(&utc->uctx, 4430 TEE_MEMORY_ACCESS_READ | 4431 TEE_MEMORY_ACCESS_ANY_OWNER, 4432 (uaddr_t)src_data, src_len); 4433 if (res != TEE_SUCCESS) 4434 return res; 4435 4436 res = get_user_u64_as_size_t(&dlen, dst_len); 4437 if (res != TEE_SUCCESS) 4438 return res; 4439 4440 res = vm_check_access_rights(&utc->uctx, 4441 TEE_MEMORY_ACCESS_READ | 4442 TEE_MEMORY_ACCESS_WRITE | 4443 TEE_MEMORY_ACCESS_ANY_OWNER, 4444 (uaddr_t)dst_data, dlen); 4445 if (res != TEE_SUCCESS) 4446 return res; 4447 4448 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 4449 return TEE_ERROR_OVERFLOW; 4450 4451 params = malloc(alloc_size); 4452 if (!params) 4453 return TEE_ERROR_OUT_OF_MEMORY; 4454 res = copy_in_attrs(utc, usr_params, num_params, params); 4455 if (res != TEE_SUCCESS) 4456 goto out; 4457 4458 res = tee_obj_get(utc, cs->key1, &o); 4459 if (res != TEE_SUCCESS) 4460 goto out; 4461 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 4462 res = TEE_ERROR_GENERIC; 4463 goto out; 4464 } 4465 4466 switch (cs->algo) { 4467 case TEE_ALG_RSA_NOPAD: 4468 if (cs->mode == TEE_MODE_ENCRYPT) { 4469 enter_user_access(); 4470 res = crypto_acipher_rsanopad_encrypt(o->attr, src_data, 4471 src_len, dst_data, 4472 &dlen); 4473 exit_user_access(); 4474 } else if (cs->mode == TEE_MODE_DECRYPT) { 4475 enter_user_access(); 4476 res = crypto_acipher_rsanopad_decrypt(o->attr, src_data, 4477 src_len, dst_data, 4478 &dlen); 4479 exit_user_access(); 4480 } else { 4481 /* 4482 * We will panic because "the mode is not compatible 4483 * with the function" 4484 */ 4485 res = TEE_ERROR_GENERIC; 4486 } 4487 break; 4488 4489 case TEE_ALG_SM2_PKE: 4490 if (cs->mode == TEE_MODE_ENCRYPT) { 4491 enter_user_access(); 4492 res = crypto_acipher_sm2_pke_encrypt(o->attr, src_data, 4493 src_len, dst_data, 4494 &dlen); 4495 exit_user_access(); 4496 } else if (cs->mode == TEE_MODE_DECRYPT) { 4497 enter_user_access(); 4498 res = crypto_acipher_sm2_pke_decrypt(o->attr, src_data, 4499 src_len, dst_data, 4500 &dlen); 4501 exit_user_access(); 4502 } else { 4503 res = TEE_ERROR_GENERIC; 4504 } 4505 break; 4506 4507 case TEE_ALG_RSAES_PKCS1_V1_5: 4508 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_MD5: 4509 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 4510 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 4511 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 4512 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 4513 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 4514 for (n = 0; n < num_params; n++) { 4515 if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) { 4516 label = params[n].content.ref.buffer; 4517 label_len = params[n].content.ref.length; 4518 break; 4519 } 4520 /* 4521 * If the optional TEE_ATTR_RSA_OAEP_MGF_HASH is 4522 * provided for algorithm 4523 * TEE_ALG_RSAES_PKCS1_OAEP_MGF1_x it must match 4524 * the internal hash x since we don't support using 4525 * a different hash for MGF1 yet. 4526 */ 4527 if (cs->algo != TEE_ALG_RSAES_PKCS1_V1_5 && 4528 params[n].attributeID == 4529 TEE_ATTR_RSA_OAEP_MGF_HASH) { 4530 uint32_t hash = 0; 4531 void *buf = params[n].content.ref.buffer; 4532 4533 if (params[n].content.ref.length != 4534 sizeof(hash)) { 4535 res = TEE_ERROR_BAD_PARAMETERS; 4536 goto out; 4537 } 4538 4539 res = copy_from_user(&hash, buf, sizeof(hash)); 4540 if (res) 4541 goto out; 4542 4543 if (hash != 4544 TEE_INTERNAL_HASH_TO_ALGO(cs->algo)) { 4545 res = TEE_ERROR_NOT_SUPPORTED; 4546 goto out; 4547 } 4548 } 4549 } 4550 4551 if (cs->mode == TEE_MODE_ENCRYPT) { 4552 enter_user_access(); 4553 res = crypto_acipher_rsaes_encrypt(cs->algo, o->attr, 4554 label, label_len, 4555 src_data, src_len, 4556 dst_data, &dlen); 4557 exit_user_access(); 4558 } else if (cs->mode == TEE_MODE_DECRYPT) { 4559 enter_user_access(); 4560 res = crypto_acipher_rsaes_decrypt( 4561 cs->algo, o->attr, label, label_len, 4562 src_data, src_len, dst_data, &dlen); 4563 exit_user_access(); 4564 } else { 4565 res = TEE_ERROR_BAD_PARAMETERS; 4566 } 4567 break; 4568 4569 #if defined(CFG_CRYPTO_RSASSA_NA1) 4570 case TEE_ALG_RSASSA_PKCS1_V1_5: 4571 #endif 4572 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 4573 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 4574 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 4575 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 4576 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 4577 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 4578 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_MD5: 4579 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 4580 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 4581 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 4582 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 4583 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 4584 if (cs->mode != TEE_MODE_SIGN) { 4585 res = TEE_ERROR_BAD_PARAMETERS; 4586 break; 4587 } 4588 salt_len = pkcs1_get_salt_len(params, num_params, src_len); 4589 enter_user_access(); 4590 res = crypto_acipher_rsassa_sign(cs->algo, o->attr, salt_len, 4591 src_data, src_len, dst_data, 4592 &dlen); 4593 exit_user_access(); 4594 break; 4595 4596 case TEE_ALG_DSA_SHA1: 4597 case TEE_ALG_DSA_SHA224: 4598 case TEE_ALG_DSA_SHA256: 4599 enter_user_access(); 4600 res = crypto_acipher_dsa_sign(cs->algo, o->attr, src_data, 4601 src_len, dst_data, &dlen); 4602 exit_user_access(); 4603 break; 4604 4605 case TEE_ALG_ED25519: 4606 enter_user_access(); 4607 res = tee_svc_obj_ed25519_sign(o->attr, src_data, src_len, 4608 dst_data, &dlen, params, 4609 num_params); 4610 exit_user_access(); 4611 break; 4612 4613 case TEE_ALG_ECDSA_SHA1: 4614 case TEE_ALG_ECDSA_SHA224: 4615 case TEE_ALG_ECDSA_SHA256: 4616 case TEE_ALG_ECDSA_SHA384: 4617 case TEE_ALG_ECDSA_SHA512: 4618 case TEE_ALG_SM2_DSA_SM3: 4619 enter_user_access(); 4620 res = crypto_acipher_ecc_sign(cs->algo, o->attr, src_data, 4621 src_len, dst_data, &dlen); 4622 exit_user_access(); 4623 break; 4624 default: 4625 res = TEE_ERROR_BAD_PARAMETERS; 4626 break; 4627 } 4628 4629 out: 4630 free_wipe(params); 4631 4632 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 4633 TEE_Result res2 = put_user_u64(dst_len, dlen); 4634 4635 if (res2 != TEE_SUCCESS) 4636 return res2; 4637 } 4638 4639 return res; 4640 } 4641 4642 TEE_Result syscall_asymm_verify(unsigned long state, 4643 const struct utee_attribute *usr_params, 4644 size_t num_params, const void *data, size_t data_len, 4645 const void *sig, size_t sig_len) 4646 { 4647 struct ts_session *sess = ts_get_current_session(); 4648 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 4649 struct tee_cryp_state *cs = NULL; 4650 TEE_Result res = TEE_SUCCESS; 4651 TEE_Attribute *params = NULL; 4652 struct tee_obj *o = NULL; 4653 size_t hash_size = 0; 4654 uint32_t hash_algo = 0; 4655 int salt_len = 0; 4656 size_t alloc_size = 0; 4657 4658 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4659 if (res != TEE_SUCCESS) 4660 return res; 4661 4662 if (cs->mode != TEE_MODE_VERIFY) 4663 return TEE_ERROR_BAD_PARAMETERS; 4664 4665 data = memtag_strip_tag_const(data); 4666 sig = memtag_strip_tag_const(sig); 4667 4668 res = vm_check_access_rights(&utc->uctx, 4669 TEE_MEMORY_ACCESS_READ | 4670 TEE_MEMORY_ACCESS_ANY_OWNER, 4671 (uaddr_t)data, data_len); 4672 if (res != TEE_SUCCESS) 4673 return res; 4674 4675 res = vm_check_access_rights(&utc->uctx, 4676 TEE_MEMORY_ACCESS_READ | 4677 TEE_MEMORY_ACCESS_ANY_OWNER, 4678 (uaddr_t)sig, sig_len); 4679 if (res != TEE_SUCCESS) 4680 return res; 4681 4682 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 4683 return TEE_ERROR_OVERFLOW; 4684 4685 params = malloc(alloc_size); 4686 if (!params) 4687 return TEE_ERROR_OUT_OF_MEMORY; 4688 res = copy_in_attrs(utc, usr_params, num_params, params); 4689 if (res != TEE_SUCCESS) 4690 goto out; 4691 4692 res = tee_obj_get(utc, cs->key1, &o); 4693 if (res != TEE_SUCCESS) 4694 goto out; 4695 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 4696 res = TEE_ERROR_BAD_PARAMETERS; 4697 goto out; 4698 } 4699 4700 switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) { 4701 case TEE_MAIN_ALGO_RSA: 4702 if (cs->algo != TEE_ALG_RSASSA_PKCS1_V1_5) { 4703 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 4704 res = tee_alg_get_digest_size(hash_algo, &hash_size); 4705 if (res != TEE_SUCCESS) 4706 break; 4707 if (data_len != hash_size) { 4708 res = TEE_ERROR_BAD_PARAMETERS; 4709 break; 4710 } 4711 salt_len = pkcs1_get_salt_len(params, num_params, 4712 hash_size); 4713 } 4714 enter_user_access(); 4715 res = crypto_acipher_rsassa_verify(cs->algo, o->attr, salt_len, 4716 data, data_len, sig, 4717 sig_len); 4718 exit_user_access(); 4719 break; 4720 4721 case TEE_MAIN_ALGO_DSA: 4722 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 4723 res = tee_alg_get_digest_size(hash_algo, &hash_size); 4724 if (res != TEE_SUCCESS) 4725 break; 4726 4727 if (data_len != hash_size) { 4728 struct dsa_public_key *key = o->attr; 4729 4730 /* 4731 * Depending on the DSA algorithm (NIST), the 4732 * digital signature output size may be truncated 4733 * to the size of a key pair (Q prime size). Q 4734 * prime size must be less or equal than the hash 4735 * output length of the hash algorithm involved. 4736 * 4737 * We're checking here in order to be able to 4738 * return this particular error code, which will 4739 * cause TEE_AsymmetricVerifyDigest() to panic as 4740 * required by GP. crypto_acipher_dsa_verify() is 4741 * implemented in the glue layer of the crypto 4742 * library and it might be a bit harder to catch 4743 * this particular case there or lead to duplicated 4744 * code in different crypto glue layers. 4745 * 4746 * The GP spec says that we SHOULD panic if 4747 * data_len != hash_size, but that would break a 4748 * few of the DSA tests in xtest where the 4749 * hash_size is larger than possible data_len. So 4750 * the compromise is in case data_len != hash_size 4751 * check that it's not smaller than what makes 4752 * sense. 4753 */ 4754 if (data_len != crypto_bignum_num_bytes(key->q)) { 4755 res = TEE_ERROR_BAD_PARAMETERS; 4756 break; 4757 } 4758 } 4759 enter_user_access(); 4760 res = crypto_acipher_dsa_verify(cs->algo, o->attr, data, 4761 data_len, sig, sig_len); 4762 exit_user_access(); 4763 break; 4764 4765 case TEE_MAIN_ALGO_ED25519: 4766 enter_user_access(); 4767 res = tee_svc_obj_ed25519_verify(o->attr, data, 4768 data_len, sig, sig_len, 4769 params, num_params); 4770 exit_user_access(); 4771 break; 4772 4773 case TEE_MAIN_ALGO_ECDSA: 4774 case TEE_MAIN_ALGO_SM2_DSA_SM3: 4775 enter_user_access(); 4776 res = crypto_acipher_ecc_verify(cs->algo, o->attr, data, 4777 data_len, sig, sig_len); 4778 exit_user_access(); 4779 break; 4780 4781 default: 4782 res = TEE_ERROR_NOT_SUPPORTED; 4783 } 4784 4785 out: 4786 free_wipe(params); 4787 return res; 4788 } 4789