8 #include "sw/device/lib/crypto/drivers/entropy.h"
9 #include "sw/device/lib/crypto/impl/integrity.h"
10 #include "sw/device/lib/crypto/impl/rsa/rsa_encryption.h"
11 #include "sw/device/lib/crypto/impl/rsa/rsa_keygen.h"
12 #include "sw/device/lib/crypto/impl/rsa/rsa_signature.h"
13 #include "sw/device/lib/crypto/impl/status.h"
17 #define MODULE_ID MAKE_MODULE_ID('r', 's', 'a')
20 "RSA-2048 public key size mismatch.");
22 "RSA-3072 public key size mismatch.");
24 "RSA-4096 public key size mismatch.");
26 "RSA-2048 private key size mismatch.");
27 static_assert(kOtcryptoRsa3072PrivateKeyBytes ==
sizeof(
rsa_3072_int_t),
28 "RSA-3072 private key size mismatch.");
29 static_assert(kOtcryptoRsa4096PrivateKeyBytes ==
sizeof(
rsa_4096_int_t),
30 "RSA-4096 private key size mismatch.");
33 "RSA-2048 keyblob size mismatch.");
34 static_assert(kOtcryptoRsa3072PrivateKeyblobBytes ==
36 "RSA-3072 keyblob size mismatch.");
37 static_assert(kOtcryptoRsa4096PrivateKeyblobBytes ==
39 "RSA-4096 keyblob size mismatch.");
56 case kOtcryptoKeyModeRsaSignPkcs:
58 case kOtcryptoKeyModeRsaSignPss:
60 case kOtcryptoKeyModeRsaEncryptOaep:
63 return OTCRYPTO_BAD_ARGS;
68 return OTCRYPTO_FATAL_ERR;
74 if (modulus.data == NULL || public_key == NULL || public_key->key == NULL) {
75 return OTCRYPTO_BAD_ARGS;
77 HARDENED_TRY(rsa_mode_check(public_key->key_mode));
80 case kOtcryptoRsaSize2048: {
82 modulus.len != kRsa2048NumWords) {
83 return OTCRYPTO_BAD_ARGS;
90 case kOtcryptoRsaSize3072: {
92 modulus.len != kRsa3072NumWords) {
93 return OTCRYPTO_BAD_ARGS;
100 case kOtcryptoRsaSize4096: {
102 modulus.len != kRsa4096NumWords) {
103 return OTCRYPTO_BAD_ARGS;
111 return OTCRYPTO_BAD_ARGS;
114 public_key->checksum = integrity_unblinded_checksum(public_key);
130 static status_t private_key_structural_check(
133 HARDENED_TRY(rsa_mode_check(private_key->config.key_mode));
137 return OTCRYPTO_BAD_ARGS;
141 size_t key_length = 0;
142 size_t keyblob_length = 0;
143 switch (launder32(size)) {
144 case kOtcryptoRsaSize2048:
149 case kOtcryptoRsaSize3072:
151 key_length = kOtcryptoRsa3072PrivateKeyBytes;
152 keyblob_length = kOtcryptoRsa3072PrivateKeyblobBytes;
154 case kOtcryptoRsaSize4096:
156 key_length = kOtcryptoRsa4096PrivateKeyBytes;
157 keyblob_length = kOtcryptoRsa4096PrivateKeyblobBytes;
160 return OTCRYPTO_BAD_ARGS;
162 HARDENED_CHECK_NE(key_length, 0);
163 HARDENED_CHECK_NE(keyblob_length, 0);
165 if (private_key->config.key_length != key_length ||
166 private_key->keyblob_length != keyblob_length) {
167 return OTCRYPTO_BAD_ARGS;
177 if (modulus.data == NULL || d_share0.data == NULL || d_share1.data == NULL ||
178 private_key == NULL || private_key->keyblob == NULL) {
179 return OTCRYPTO_BAD_ARGS;
181 HARDENED_TRY(rsa_mode_check(private_key->config.key_mode));
185 if (d_share0.len != modulus.len || d_share1.len != modulus.len) {
186 return OTCRYPTO_BAD_ARGS;
191 if ((e & 1) != 1 || e >> 16 == 0) {
192 return OTCRYPTO_BAD_ARGS;
196 HARDENED_TRY(private_key_structural_check(size, private_key));
199 case kOtcryptoRsaSize2048: {
201 modulus.len != kRsa2048NumWords) {
202 return OTCRYPTO_BAD_ARGS;
209 for (
size_t i = 0; i < d_share1.len; i++) {
210 sk->d.data[i] ^= d_share1.data[i];
214 case kOtcryptoRsaSize3072: {
216 modulus.len != kRsa3072NumWords) {
217 return OTCRYPTO_BAD_ARGS;
224 for (
size_t i = 0; i < d_share1.len; i++) {
225 sk->d.data[i] ^= d_share1.data[i];
229 case kOtcryptoRsaSize4096: {
231 modulus.len != kRsa4096NumWords) {
232 return OTCRYPTO_BAD_ARGS;
239 for (
size_t i = 0; i < d_share1.len; i++) {
240 sk->d.data[i] ^= d_share1.data[i];
245 return OTCRYPTO_BAD_ARGS;
248 private_key->checksum = integrity_blinded_checksum(private_key);
258 size, modulus, e, cofactor_share0, cofactor_share1));
266 case kOtcryptoRsaSize2048: {
268 modulus.len != kRsa2048NumWords) {
269 return OTCRYPTO_RECOV_ERR;
272 modulus_eq =
hardened_memeq(modulus.data, pk->n.data, modulus.len);
275 case kOtcryptoRsaSize3072:
276 return OTCRYPTO_NOT_IMPLEMENTED;
277 case kOtcryptoRsaSize4096:
278 return OTCRYPTO_NOT_IMPLEMENTED;
280 return OTCRYPTO_BAD_ARGS;
287 return OTCRYPTO_BAD_ARGS;
308 verification_result);
337 static status_t rsa_size_from_public_key(
339 switch (launder32(public_key->key_length)) {
342 *key_size = kOtcryptoRsaSize2048;
344 case kOtcryptoRsa3072PublicKeyBytes:
346 *key_size = kOtcryptoRsaSize3072;
348 case kOtcryptoRsa4096PublicKeyBytes:
350 *key_size = kOtcryptoRsaSize4096;
354 return OTCRYPTO_BAD_ARGS;
359 return OTCRYPTO_FATAL_ERR;
369 static status_t rsa_size_from_private_key(
371 switch (launder32(private_key->config.key_length)) {
375 *key_size = kOtcryptoRsaSize2048;
377 case kOtcryptoRsa3072PrivateKeyBytes:
379 kOtcryptoRsa3072PrivateKeyBytes);
380 *key_size = kOtcryptoRsaSize3072;
382 case kOtcryptoRsa4096PrivateKeyBytes:
384 kOtcryptoRsa4096PrivateKeyBytes);
385 *key_size = kOtcryptoRsaSize4096;
389 return OTCRYPTO_BAD_ARGS;
394 return OTCRYPTO_FATAL_ERR;
407 static status_t public_key_structural_check(
410 return rsa_mode_check(public_key->key_mode);
415 HARDENED_TRY(entropy_complex_check());
418 case kOtcryptoRsaSize2048:
419 return rsa_keygen_2048_start();
420 case kOtcryptoRsaSize3072:
421 return rsa_keygen_3072_start();
422 case kOtcryptoRsaSize4096:
423 return rsa_keygen_4096_start();
425 return OTCRYPTO_BAD_ARGS;
430 return OTCRYPTO_FATAL_ERR;
436 if (public_key == NULL || public_key->key == NULL || private_key == NULL ||
437 private_key->keyblob == NULL) {
438 return OTCRYPTO_BAD_ARGS;
442 HARDENED_TRY(rsa_size_from_public_key(public_key, &size));
445 HARDENED_TRY(public_key_structural_check(public_key));
448 HARDENED_TRY(private_key_structural_check(size, private_key));
452 case kOtcryptoRsaSize2048: {
457 HARDENED_TRY(rsa_keygen_2048_finalize(pk, sk));
460 case kOtcryptoRsaSize3072: {
465 HARDENED_TRY(rsa_keygen_3072_finalize(pk, sk));
468 case kOtcryptoRsaSize4096: {
473 HARDENED_TRY(rsa_keygen_4096_finalize(pk, sk));
478 return OTCRYPTO_BAD_ARGS;
482 public_key->checksum = integrity_unblinded_checksum(public_key);
483 private_key->checksum = integrity_blinded_checksum(private_key);
492 if (modulus.data == NULL || cofactor_share0.data == NULL ||
493 cofactor_share1.data == NULL) {
494 return OTCRYPTO_BAD_ARGS;
499 if (cofactor_share0.len != modulus.len / 2 ||
500 cofactor_share1.len != modulus.len / 2) {
501 return OTCRYPTO_BAD_ARGS;
505 case kOtcryptoRsaSize2048: {
506 if (cofactor_share0.len !=
508 modulus.len != kRsa2048NumWords) {
509 return OTCRYPTO_BAD_ARGS;
513 for (
size_t i = 0; i < cofactor_share1.len; i++) {
514 cf->data[i] ^= cofactor_share1.data[i];
519 return rsa_keygen_from_cofactor_2048_start(&pk, cf);
521 case kOtcryptoRsaSize3072: {
522 return OTCRYPTO_NOT_IMPLEMENTED;
524 case kOtcryptoRsaSize4096: {
525 return OTCRYPTO_NOT_IMPLEMENTED;
528 return OTCRYPTO_BAD_ARGS;
533 return OTCRYPTO_FATAL_ERR;
539 if (public_key == NULL || public_key->key == NULL || private_key == NULL ||
540 private_key->keyblob == NULL) {
541 return OTCRYPTO_BAD_ARGS;
545 HARDENED_TRY(rsa_size_from_public_key(public_key, &size));
548 HARDENED_TRY(public_key_structural_check(public_key));
551 HARDENED_TRY(private_key_structural_check(size, private_key));
555 case kOtcryptoRsaSize2048: {
559 HARDENED_TRY(rsa_keygen_from_cofactor_2048_finalize(pk, sk));
562 case kOtcryptoRsaSize3072: {
563 return OTCRYPTO_NOT_IMPLEMENTED;
565 case kOtcryptoRsaSize4096: {
566 return OTCRYPTO_NOT_IMPLEMENTED;
570 return OTCRYPTO_BAD_ARGS;
574 public_key->checksum = integrity_unblinded_checksum(public_key);
575 private_key->checksum = integrity_blinded_checksum(private_key);
589 switch (launder32(padding_mode)) {
590 case kOtcryptoRsaPaddingPkcs:
592 if (launder32(key_mode) != kOtcryptoKeyModeRsaSignPkcs) {
593 return OTCRYPTO_BAD_ARGS;
597 case kOtcryptoRsaPaddingPss:
599 if (launder32(key_mode) != kOtcryptoKeyModeRsaSignPss) {
600 return OTCRYPTO_BAD_ARGS;
606 return OTCRYPTO_BAD_ARGS;
611 return OTCRYPTO_FATAL_ERR;
619 if (message_digest.data == NULL || private_key == NULL ||
620 private_key->keyblob == NULL) {
621 return OTCRYPTO_BAD_ARGS;
626 HARDENED_TRY(rsa_size_from_private_key(private_key, &size));
629 HARDENED_TRY(private_key_structural_check(size, private_key));
633 key_mode_padding_check(private_key->config.key_mode, padding_mode));
637 return OTCRYPTO_BAD_ARGS;
642 case kOtcryptoRsaSize2048: {
645 return rsa_signature_generate_2048_start(
646 sk, message_digest, (rsa_signature_padding_t)padding_mode);
648 case kOtcryptoRsaSize3072: {
651 return rsa_signature_generate_3072_start(
652 sk, message_digest, (rsa_signature_padding_t)padding_mode);
654 case kOtcryptoRsaSize4096: {
657 return rsa_signature_generate_4096_start(
658 sk, message_digest, (rsa_signature_padding_t)padding_mode);
663 return OTCRYPTO_FATAL_ERR;
668 return OTCRYPTO_FATAL_ERR;
674 if (signature.data == NULL) {
675 return OTCRYPTO_BAD_ARGS;
679 switch (signature.len) {
680 case kRsa2048NumWords:
681 return rsa_signature_generate_2048_finalize(
683 case kRsa3072NumWords:
684 return rsa_signature_generate_3072_finalize(
686 case kRsa4096NumWords:
687 return rsa_signature_generate_4096_finalize(
690 return OTCRYPTO_BAD_ARGS;
695 return OTCRYPTO_FATAL_ERR;
702 if (public_key == NULL || public_key->key == NULL || signature.data == NULL) {
703 return OTCRYPTO_BAD_ARGS;
707 HARDENED_TRY(public_key_structural_check(public_key));
711 return OTCRYPTO_BAD_ARGS;
716 HARDENED_TRY(rsa_size_from_public_key(public_key, &size));
719 case kOtcryptoRsaSize2048: {
720 if (signature.len != kRsa2048NumWords) {
721 return OTCRYPTO_BAD_ARGS;
725 return rsa_signature_verify_2048_start(pk, sig);
727 case kOtcryptoRsaSize3072: {
728 if (signature.len != kRsa3072NumWords) {
729 return OTCRYPTO_BAD_ARGS;
733 return rsa_signature_verify_3072_start(pk, sig);
735 case kOtcryptoRsaSize4096: {
736 if (signature.len != kRsa4096NumWords) {
737 return OTCRYPTO_BAD_ARGS;
741 return rsa_signature_verify_4096_start(pk, sig);
746 return OTCRYPTO_FATAL_ERR;
751 return OTCRYPTO_FATAL_ERR;
758 if (message_digest.data == NULL || verification_result == NULL) {
759 return OTCRYPTO_BAD_ARGS;
767 return rsa_signature_verify_finalize(message_digest,
768 (rsa_signature_padding_t)padding_mode,
769 verification_result);
777 if (public_key == NULL || public_key->key == NULL) {
778 return OTCRYPTO_BAD_ARGS;
781 if (message.data == NULL && (message.len != 0)) {
782 return OTCRYPTO_BAD_ARGS;
785 if (label.data == NULL && (label.len != 0)) {
786 return OTCRYPTO_BAD_ARGS;
790 HARDENED_TRY(public_key_structural_check(public_key));
794 return OTCRYPTO_BAD_ARGS;
798 if (launder32(public_key->key_mode) != kOtcryptoKeyModeRsaEncryptOaep) {
799 return OTCRYPTO_BAD_ARGS;
805 HARDENED_TRY(rsa_size_from_public_key(public_key, &size));
807 switch (launder32(size)) {
808 case kOtcryptoRsaSize2048: {
812 return rsa_encrypt_2048_start(pk, hash_mode, message.data, message.len,
813 label.data, label.len);
815 case kOtcryptoRsaSize3072: {
819 return rsa_encrypt_3072_start(pk, hash_mode, message.data, message.len,
820 label.data, label.len);
822 case kOtcryptoRsaSize4096: {
826 return rsa_encrypt_4096_start(pk, hash_mode, message.data, message.len,
827 label.data, label.len);
832 return OTCRYPTO_FATAL_ERR;
837 return OTCRYPTO_FATAL_ERR;
843 if (ciphertext.data == NULL) {
844 return OTCRYPTO_BAD_ARGS;
847 switch (launder32(ciphertext.len)) {
848 case kRsa2048NumWords: {
852 return rsa_encrypt_2048_finalize(ctext);
854 case kRsa3072NumWords: {
858 return rsa_encrypt_3072_finalize(ctext);
860 case kRsa4096NumWords: {
864 return rsa_encrypt_4096_finalize(ctext);
867 return OTCRYPTO_BAD_ARGS;
872 return OTCRYPTO_FATAL_ERR;
879 if (private_key == NULL || private_key->keyblob == NULL ||
880 ciphertext.data == NULL) {
881 return OTCRYPTO_BAD_ARGS;
886 HARDENED_TRY(rsa_size_from_private_key(private_key, &size));
889 HARDENED_TRY(private_key_structural_check(size, private_key));
893 return OTCRYPTO_BAD_ARGS;
897 if (launder32(private_key->config.key_mode) !=
898 kOtcryptoKeyModeRsaEncryptOaep) {
899 return OTCRYPTO_BAD_ARGS;
902 kOtcryptoKeyModeRsaEncryptOaep);
905 switch (launder32(size)) {
906 case kOtcryptoRsaSize2048: {
910 if (ciphertext.len != kRsa2048NumWords) {
911 return OTCRYPTO_BAD_ARGS;
916 return rsa_decrypt_2048_start(sk, ctext);
918 case kOtcryptoRsaSize3072: {
922 if (ciphertext.len != kRsa3072NumWords) {
923 return OTCRYPTO_BAD_ARGS;
928 return rsa_decrypt_3072_start(sk, ctext);
930 case kOtcryptoRsaSize4096: {
934 if (ciphertext.len != kRsa4096NumWords) {
935 return OTCRYPTO_BAD_ARGS;
940 return rsa_decrypt_4096_start(sk, ctext);
945 return OTCRYPTO_FATAL_ERR;
950 return OTCRYPTO_FATAL_ERR;
956 if (plaintext.data == NULL || label.data == NULL) {
957 return OTCRYPTO_BAD_ARGS;
962 HARDENED_TRY(rsa_decrypt_finalize(hash_mode, label.data, label.len,
963 plaintext.len, plaintext.data,
967 if (launder32(*plaintext_bytelen) > plaintext.len) {
969 return OTCRYPTO_FATAL_ERR;
971 HARDENED_CHECK_LE(*plaintext_bytelen, plaintext.len);