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