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