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