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