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