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