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