1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2017, Linaro Limited 4 */ 5 6 #include <compiler.h> 7 #include <crypto/aes-ccm.h> 8 #include <crypto/aes-gcm.h> 9 #include <crypto/crypto.h> 10 #include <kernel/panic.h> 11 12 #if !defined(_CFG_CRYPTO_WITH_HASH) 13 TEE_Result crypto_hash_alloc_ctx(void **ctx __unused, uint32_t algo __unused) 14 { 15 return TEE_ERROR_NOT_IMPLEMENTED; 16 } 17 18 void crypto_hash_free_ctx(void *ctx __unused, uint32_t algo __unused) 19 { 20 assert(0); 21 } 22 23 void crypto_hash_copy_state(void *dst_ctx __unused, void *src_ctx __unused, 24 uint32_t algo __unused) 25 { 26 assert(0); 27 } 28 29 TEE_Result crypto_hash_init(void *ctx __unused, uint32_t algo __unused) 30 { 31 return TEE_ERROR_NOT_IMPLEMENTED; 32 } 33 TEE_Result crypto_hash_update(void *ctx __unused, uint32_t algo __unused, 34 const uint8_t *data __unused, size_t len __unused) 35 { 36 return TEE_ERROR_NOT_IMPLEMENTED; 37 } 38 TEE_Result crypto_hash_final(void *ctx __unused, uint32_t algo __unused, 39 uint8_t *digest __unused, size_t len __unused) 40 { 41 return TEE_ERROR_NOT_IMPLEMENTED; 42 } 43 #endif /*_CFG_CRYPTO_WITH_HASH*/ 44 45 #if !defined(_CFG_CRYPTO_WITH_CIPHER) 46 TEE_Result crypto_cipher_get_ctx_size(uint32_t algo __unused, 47 size_t *size __unused) 48 { 49 return TEE_ERROR_NOT_IMPLEMENTED; 50 } 51 52 TEE_Result crypto_cipher_init(void *ctx __unused, uint32_t algo __unused, 53 TEE_OperationMode mode __unused, 54 const uint8_t *key1 __unused, 55 size_t key1_len __unused, 56 const uint8_t *key2 __unused, 57 size_t key2_len __unused, 58 const uint8_t *iv __unused, 59 size_t iv_len __unused) 60 { 61 return TEE_ERROR_NOT_IMPLEMENTED; 62 } 63 64 TEE_Result crypto_cipher_update(void *ctx __unused, uint32_t algo __unused, 65 TEE_OperationMode mode __unused, 66 bool last_block __unused, 67 const uint8_t *data __unused, 68 size_t len __unused, uint8_t *dst __unused) 69 { 70 return TEE_ERROR_NOT_IMPLEMENTED; 71 } 72 73 void crypto_cipher_final(void *ctx __unused, uint32_t algo __unused) 74 { 75 } 76 77 TEE_Result crypto_cipher_get_block_size(uint32_t algo __unused, 78 size_t *size __unused) 79 { 80 return TEE_ERROR_NOT_IMPLEMENTED; 81 } 82 #endif /*_CFG_CRYPTO_WITH_CIPHER*/ 83 84 #if !defined(_CFG_CRYPTO_WITH_MAC) 85 TEE_Result crypto_mac_alloc_ctx(void **ctx __unused, uint32_t algo __unused) 86 { 87 return TEE_ERROR_NOT_IMPLEMENTED; 88 } 89 90 void crypto_mac_free_ctx(void *ctx __unused, uint32_t algo __unused) 91 { 92 assert(0); 93 } 94 95 void crypto_mac_copy_state(void *dst_ctx __unused, void *src_ctx __unused, 96 uint32_t algo __unused) 97 { 98 assert(0); 99 } 100 101 TEE_Result crypto_mac_init(void *ctx __unused, uint32_t algo __unused, 102 const uint8_t *key __unused, size_t len __unused) 103 { 104 return TEE_ERROR_NOT_IMPLEMENTED; 105 } 106 107 TEE_Result crypto_mac_update(void *ctx __unused, uint32_t algo __unused, 108 const uint8_t *data __unused, size_t len __unused) 109 { 110 return TEE_ERROR_NOT_IMPLEMENTED; 111 } 112 113 TEE_Result crypto_mac_final(void *ctx __unused, uint32_t algo __unused, 114 uint8_t *digest __unused, 115 size_t digest_len __unused) 116 { 117 return TEE_ERROR_NOT_IMPLEMENTED; 118 } 119 #endif /*_CFG_CRYPTO_WITH_MAC*/ 120 121 TEE_Result crypto_authenc_get_ctx_size(uint32_t algo __maybe_unused, 122 size_t *size __maybe_unused) 123 { 124 switch (algo) { 125 #if defined(CFG_CRYPTO_CCM) 126 case TEE_ALG_AES_CCM: 127 *size = crypto_aes_ccm_get_ctx_size(); 128 return TEE_SUCCESS; 129 #endif 130 #if defined(CFG_CRYPTO_GCM) 131 case TEE_ALG_AES_GCM: 132 *size = crypto_aes_gcm_get_ctx_size(); 133 return TEE_SUCCESS; 134 #endif 135 default: 136 return TEE_ERROR_NOT_IMPLEMENTED; 137 } 138 } 139 140 TEE_Result crypto_authenc_init(void *ctx __maybe_unused, 141 uint32_t algo __maybe_unused, 142 TEE_OperationMode mode __maybe_unused, 143 const uint8_t *key __maybe_unused, 144 size_t key_len __maybe_unused, 145 const uint8_t *nonce __maybe_unused, 146 size_t nonce_len __maybe_unused, 147 size_t tag_len __maybe_unused, 148 size_t aad_len __maybe_unused, 149 size_t payload_len __maybe_unused) 150 { 151 switch (algo) { 152 #if defined(CFG_CRYPTO_CCM) 153 case TEE_ALG_AES_CCM: 154 return crypto_aes_ccm_init(ctx, mode, key, key_len, nonce, 155 nonce_len, tag_len, aad_len, 156 payload_len); 157 #endif 158 #if defined(CFG_CRYPTO_GCM) 159 case TEE_ALG_AES_GCM: 160 return crypto_aes_gcm_init(ctx, mode, key, key_len, nonce, 161 nonce_len, tag_len); 162 #endif 163 default: 164 return TEE_ERROR_NOT_IMPLEMENTED; 165 } 166 } 167 168 TEE_Result crypto_authenc_update_aad(void *ctx __maybe_unused, 169 uint32_t algo __maybe_unused, 170 TEE_OperationMode mode __unused, 171 const uint8_t *data __maybe_unused, 172 size_t len __maybe_unused) 173 { 174 switch (algo) { 175 #if defined(CFG_CRYPTO_CCM) 176 case TEE_ALG_AES_CCM: 177 return crypto_aes_ccm_update_aad(ctx, data, len); 178 #endif 179 #if defined(CFG_CRYPTO_GCM) 180 case TEE_ALG_AES_GCM: 181 return crypto_aes_gcm_update_aad(ctx, data, len); 182 #endif 183 default: 184 return TEE_ERROR_NOT_IMPLEMENTED; 185 } 186 } 187 188 TEE_Result crypto_authenc_update_payload(void *ctx __maybe_unused, 189 uint32_t algo __maybe_unused, 190 TEE_OperationMode mode __maybe_unused, 191 const uint8_t *src_data __maybe_unused, 192 size_t src_len __maybe_unused, 193 uint8_t *dst_data __maybe_unused, 194 size_t *dst_len __maybe_unused) 195 { 196 size_t dl = *dst_len; 197 198 *dst_len = src_len; 199 if (dl < src_len) 200 return TEE_ERROR_SHORT_BUFFER; 201 202 switch (algo) { 203 #if defined(CFG_CRYPTO_CCM) 204 case TEE_ALG_AES_CCM: 205 return crypto_aes_ccm_update_payload(ctx, mode, src_data, 206 src_len, dst_data); 207 #endif 208 #if defined(CFG_CRYPTO_GCM) 209 case TEE_ALG_AES_GCM: 210 return crypto_aes_gcm_update_payload(ctx, mode, src_data, 211 src_len, dst_data); 212 #endif 213 default: 214 return TEE_ERROR_NOT_IMPLEMENTED; 215 } 216 } 217 218 TEE_Result crypto_authenc_enc_final(void *ctx __maybe_unused, 219 uint32_t algo __maybe_unused, 220 const uint8_t *src_data __maybe_unused, 221 size_t src_len __maybe_unused, 222 uint8_t *dst_data __maybe_unused, 223 size_t *dst_len __maybe_unused, 224 uint8_t *dst_tag __maybe_unused, 225 size_t *dst_tag_len __maybe_unused) 226 { 227 size_t dl = *dst_len; 228 229 *dst_len = src_len; 230 if (dl < src_len) 231 return TEE_ERROR_SHORT_BUFFER; 232 233 switch (algo) { 234 #if defined(CFG_CRYPTO_CCM) 235 case TEE_ALG_AES_CCM: 236 return crypto_aes_ccm_enc_final(ctx, src_data, src_len, 237 dst_data, dst_tag, dst_tag_len); 238 #endif 239 #if defined(CFG_CRYPTO_GCM) 240 case TEE_ALG_AES_GCM: 241 return crypto_aes_gcm_enc_final(ctx, src_data, src_len, 242 dst_data, dst_tag, dst_tag_len); 243 #endif 244 default: 245 return TEE_ERROR_NOT_IMPLEMENTED; 246 } 247 } 248 249 TEE_Result crypto_authenc_dec_final(void *ctx __maybe_unused, 250 uint32_t algo __maybe_unused, 251 const uint8_t *src_data __maybe_unused, 252 size_t src_len __maybe_unused, 253 uint8_t *dst_data __maybe_unused, 254 size_t *dst_len __maybe_unused, 255 const uint8_t *tag __maybe_unused, 256 size_t tag_len __maybe_unused) 257 { 258 size_t dl = *dst_len; 259 260 *dst_len = src_len; 261 if (dl < src_len) 262 return TEE_ERROR_SHORT_BUFFER; 263 264 switch (algo) { 265 #if defined(CFG_CRYPTO_CCM) 266 case TEE_ALG_AES_CCM: 267 return crypto_aes_ccm_dec_final(ctx, src_data, src_len, 268 dst_data, tag, tag_len); 269 #endif 270 #if defined(CFG_CRYPTO_GCM) 271 case TEE_ALG_AES_GCM: 272 return crypto_aes_gcm_dec_final(ctx, src_data, src_len, 273 dst_data, tag, tag_len); 274 #endif 275 default: 276 return TEE_ERROR_NOT_IMPLEMENTED; 277 } 278 } 279 280 void crypto_authenc_final(void *ctx __maybe_unused, 281 uint32_t algo __maybe_unused) 282 { 283 switch (algo) { 284 #if defined(CFG_CRYPTO_CCM) 285 case TEE_ALG_AES_CCM: 286 crypto_aes_ccm_final(ctx); 287 break; 288 #endif 289 #if defined(CFG_CRYPTO_GCM) 290 case TEE_ALG_AES_GCM: 291 crypto_aes_gcm_final(ctx); 292 break; 293 #endif 294 default: 295 break; 296 } 297 } 298 299 #if !defined(_CFG_CRYPTO_WITH_ACIPHER) 300 struct bignum *crypto_bignum_allocate(size_t size_bits __unused) 301 { 302 return NULL; 303 } 304 305 TEE_Result crypto_bignum_bin2bn(const uint8_t *from __unused, 306 size_t fromsize __unused, 307 struct bignum *to __unused) 308 { 309 return TEE_ERROR_NOT_IMPLEMENTED; 310 } 311 312 size_t crypto_bignum_num_bytes(struct bignum *a __unused) 313 { 314 return 0; 315 } 316 317 size_t crypto_bignum_num_bits(struct bignum *a __unused) 318 { 319 return 0; 320 } 321 322 /* 323 * crypto_bignum_allocate() and crypto_bignum_bin2bn() failing should be 324 * enough to guarantee that the functions calling this function aren't 325 * called, but just in case add a panic() here to avoid unexpected 326 * behavoir. 327 */ 328 static void bignum_cant_happen(void) 329 { 330 volatile bool b = true; 331 332 /* Avoid warning about function does not return */ 333 if (b) 334 panic(); 335 } 336 337 void crypto_bignum_bn2bin(const struct bignum *from __unused, 338 uint8_t *to __unused) 339 { 340 bignum_cant_happen(); 341 } 342 343 void crypto_bignum_copy(struct bignum *to __unused, 344 const struct bignum *from __unused) 345 { 346 bignum_cant_happen(); 347 } 348 349 void crypto_bignum_free(struct bignum *a) 350 { 351 if (a) 352 panic(); 353 } 354 355 void crypto_bignum_clear(struct bignum *a __unused) 356 { 357 bignum_cant_happen(); 358 } 359 360 /* return -1 if a<b, 0 if a==b, +1 if a>b */ 361 int32_t crypto_bignum_compare(struct bignum *a __unused, 362 struct bignum *b __unused) 363 { 364 bignum_cant_happen(); 365 return -1; 366 } 367 #endif /*!_CFG_CRYPTO_WITH_ACIPHER*/ 368 369 #if !defined(CFG_CRYPTO_RSA) || !defined(_CFG_CRYPTO_WITH_ACIPHER) 370 TEE_Result crypto_acipher_alloc_rsa_keypair(struct rsa_keypair *s __unused, 371 size_t key_size_bits __unused) 372 { 373 return TEE_ERROR_NOT_IMPLEMENTED; 374 } 375 376 TEE_Result 377 crypto_acipher_alloc_rsa_public_key(struct rsa_public_key *s __unused, 378 size_t key_size_bits __unused) 379 { 380 return TEE_ERROR_NOT_IMPLEMENTED; 381 } 382 383 void crypto_acipher_free_rsa_public_key(struct rsa_public_key *s __unused) 384 { 385 } 386 387 TEE_Result crypto_acipher_gen_rsa_key(struct rsa_keypair *key __unused, 388 size_t key_size __unused) 389 { 390 return TEE_ERROR_NOT_IMPLEMENTED; 391 } 392 393 TEE_Result crypto_acipher_rsanopad_decrypt(struct rsa_keypair *key __unused, 394 const uint8_t *src __unused, 395 size_t src_len __unused, 396 uint8_t *dst __unused, 397 size_t *dst_len __unused) 398 { 399 return TEE_ERROR_NOT_IMPLEMENTED; 400 } 401 402 TEE_Result crypto_acipher_rsanopad_encrypt(struct rsa_public_key *key __unused, 403 const uint8_t *src __unused, 404 size_t src_len __unused, 405 uint8_t *dst __unused, 406 size_t *dst_len __unused) 407 { 408 return TEE_ERROR_NOT_IMPLEMENTED; 409 } 410 411 TEE_Result crypto_acipher_rsaes_decrypt(uint32_t algo __unused, 412 struct rsa_keypair *key __unused, 413 const uint8_t *label __unused, 414 size_t label_len __unused, 415 const uint8_t *src __unused, 416 size_t src_len __unused, 417 uint8_t *dst __unused, 418 size_t *dst_len __unused) 419 { 420 return TEE_ERROR_NOT_IMPLEMENTED; 421 } 422 423 TEE_Result crypto_acipher_rsaes_encrypt(uint32_t algo __unused, 424 struct rsa_public_key *key __unused, 425 const uint8_t *label __unused, 426 size_t label_len __unused, 427 const uint8_t *src __unused, 428 size_t src_len __unused, 429 uint8_t *dst __unused, 430 size_t *dst_len __unused) 431 { 432 return TEE_ERROR_NOT_IMPLEMENTED; 433 } 434 435 TEE_Result crypto_acipher_rsassa_sign(uint32_t algo __unused, 436 struct rsa_keypair *key __unused, 437 int salt_len __unused, 438 const uint8_t *msg __unused, 439 size_t msg_len __unused, 440 uint8_t *sig __unused, 441 size_t *sig_len __unused) 442 { 443 return TEE_ERROR_NOT_IMPLEMENTED; 444 } 445 446 TEE_Result crypto_acipher_rsassa_verify(uint32_t algo __unused, 447 struct rsa_public_key *key __unused, 448 int salt_len __unused, 449 const uint8_t *msg __unused, 450 size_t msg_len __unused, 451 const uint8_t *sig __unused, 452 size_t sig_len __unused) 453 { 454 return TEE_ERROR_NOT_IMPLEMENTED; 455 } 456 #endif /*!CFG_CRYPTO_RSA || !_CFG_CRYPTO_WITH_ACIPHER*/ 457 458 #if !defined(CFG_CRYPTO_DSA) || !defined(_CFG_CRYPTO_WITH_ACIPHER) 459 TEE_Result crypto_acipher_alloc_dsa_keypair(struct dsa_keypair *s __unused, 460 size_t key_size_bits __unused) 461 { 462 return TEE_ERROR_NOT_IMPLEMENTED; 463 } 464 465 TEE_Result 466 crypto_acipher_alloc_dsa_public_key(struct dsa_public_key *s __unused, 467 size_t key_size_bits __unused) 468 { 469 return TEE_ERROR_NOT_IMPLEMENTED; 470 } 471 472 TEE_Result crypto_acipher_gen_dsa_key(struct dsa_keypair *key __unused, 473 size_t key_size __unused) 474 { 475 return TEE_ERROR_NOT_IMPLEMENTED; 476 } 477 478 TEE_Result crypto_acipher_dsa_sign(uint32_t algo __unused, 479 struct dsa_keypair *key __unused, 480 const uint8_t *msg __unused, 481 size_t msg_len __unused, 482 uint8_t *sig __unused, 483 size_t *sig_len __unused) 484 { 485 return TEE_ERROR_NOT_IMPLEMENTED; 486 } 487 488 TEE_Result crypto_acipher_dsa_verify(uint32_t algo __unused, 489 struct dsa_public_key *key __unused, 490 const uint8_t *msg __unused, 491 size_t msg_len __unused, 492 const uint8_t *sig __unused, 493 size_t sig_len __unused) 494 { 495 return TEE_ERROR_NOT_IMPLEMENTED; 496 } 497 #endif /*!CFG_CRYPTO_DSA || !_CFG_CRYPTO_WITH_ACIPHER*/ 498 499 #if !defined(CFG_CRYPTO_DH) || !defined(_CFG_CRYPTO_WITH_ACIPHER) 500 TEE_Result crypto_acipher_alloc_dh_keypair(struct dh_keypair *s __unused, 501 size_t key_size_bits __unused) 502 { 503 return TEE_ERROR_NOT_IMPLEMENTED; 504 } 505 506 TEE_Result crypto_acipher_gen_dh_key(struct dh_keypair *key __unused, 507 struct bignum *q __unused, 508 size_t xbits __unused) 509 { 510 return TEE_ERROR_NOT_IMPLEMENTED; 511 } 512 513 TEE_Result 514 crypto_acipher_dh_shared_secret(struct dh_keypair *private_key __unused, 515 struct bignum *public_key __unused, 516 struct bignum *secret __unused) 517 { 518 return TEE_ERROR_NOT_IMPLEMENTED; 519 } 520 #endif /*!CFG_CRYPTO_DH || !_CFG_CRYPTO_WITH_ACIPHER*/ 521 522 #if !defined(CFG_CRYPTO_ECC) || !defined(_CFG_CRYPTO_WITH_ACIPHER) 523 TEE_Result 524 crypto_acipher_alloc_ecc_public_key(struct ecc_public_key *s __unused, 525 size_t key_size_bits __unused) 526 { 527 return TEE_ERROR_NOT_IMPLEMENTED; 528 } 529 530 TEE_Result crypto_acipher_alloc_ecc_keypair(struct ecc_keypair *s __unused, 531 size_t key_size_bits __unused) 532 { 533 return TEE_ERROR_NOT_IMPLEMENTED; 534 } 535 536 void crypto_acipher_free_ecc_public_key(struct ecc_public_key *s __unused) 537 { 538 } 539 540 TEE_Result crypto_acipher_gen_ecc_key(struct ecc_keypair *key __unused) 541 { 542 return TEE_ERROR_NOT_IMPLEMENTED; 543 } 544 545 TEE_Result crypto_acipher_ecc_sign(uint32_t algo __unused, 546 struct ecc_keypair *key __unused, 547 const uint8_t *msg __unused, 548 size_t msg_len __unused, 549 uint8_t *sig __unused, 550 size_t *sig_len __unused) 551 { 552 return TEE_ERROR_NOT_IMPLEMENTED; 553 } 554 555 TEE_Result crypto_acipher_ecc_verify(uint32_t algo __unused, 556 struct ecc_public_key *key __unused, 557 const uint8_t *msg __unused, 558 size_t msg_len __unused, 559 const uint8_t *sig __unused, 560 size_t sig_len __unused) 561 { 562 return TEE_ERROR_NOT_IMPLEMENTED; 563 } 564 565 TEE_Result 566 crypto_acipher_ecc_shared_secret(struct ecc_keypair *private_key __unused, 567 struct ecc_public_key *public_key __unused, 568 void *secret __unused, 569 unsigned long *secret_len __unused) 570 { 571 return TEE_ERROR_NOT_IMPLEMENTED; 572 } 573 #endif /*!CFG_CRYPTO_ECC || !_CFG_CRYPTO_WITH_ACIPHER*/ 574