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