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