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