7 #include "sw/device/lib/crypto/drivers/entropy.h"
8 #include "sw/device/lib/crypto/drivers/hmac.h"
9 #include "sw/device/lib/crypto/impl/ecc/p256.h"
10 #include "sw/device/lib/crypto/impl/ecc/p384.h"
11 #include "sw/device/lib/crypto/impl/integrity.h"
12 #include "sw/device/lib/crypto/impl/keyblob.h"
16 #define MODULE_ID MAKE_MODULE_ID('e', 'c', 'c')
43 signature, elliptic_curve));
68 return OTCRYPTO_NOT_IMPLEMENTED;
76 return OTCRYPTO_NOT_IMPLEMENTED;
85 return OTCRYPTO_NOT_IMPLEMENTED;
91 return OTCRYPTO_NOT_IMPLEMENTED;
98 return OTCRYPTO_NOT_IMPLEMENTED;
113 keyblob_to_keymgr_diversification(private_key, &diversification));
114 return keymgr_generate_key_otbn(diversification);
120 if (elliptic_curve == NULL || private_key == NULL ||
121 private_key->keyblob == NULL) {
122 return OTCRYPTO_BAD_ARGS;
126 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdsa) {
127 return OTCRYPTO_BAD_ARGS;
132 HARDENED_TRY(entropy_complex_check());
135 switch (launder32(elliptic_curve->curve_type)) {
136 case kOtcryptoEccCurveTypeNistP256:
138 kOtcryptoEccCurveTypeNistP256);
141 HARDENED_TRY(sideload_key_seed(private_key));
142 return p256_sideload_keygen_start();
143 }
else if (launder32(private_key->config.hw_backed) ==
146 return p256_keygen_start();
148 return OTCRYPTO_BAD_ARGS;
151 case kOtcryptoEccCurveTypeNistP384:
153 kOtcryptoEccCurveTypeNistP384);
156 HARDENED_TRY(sideload_key_seed(private_key));
157 return p384_sideload_keygen_start();
158 }
else if (launder32(private_key->config.hw_backed) ==
161 return p384_keygen_start();
163 return OTCRYPTO_BAD_ARGS;
166 case kEccCurveTypeBrainpoolP256R1:
168 case kOtcryptoEccCurveTypeCustom:
170 return OTCRYPTO_NOT_IMPLEMENTED;
172 return OTCRYPTO_BAD_ARGS;
177 return OTCRYPTO_FATAL_ERR;
194 static status_t p256_private_key_length_check(
196 if (private_key->keyblob == NULL) {
197 return OTCRYPTO_BAD_ARGS;
208 if (launder32(private_key->config.key_length) != kP256ScalarBytes) {
209 return OTCRYPTO_BAD_ARGS;
214 if (launder32(keyblob_share_num_words(private_key->config)) !=
215 kP256MaskedScalarShareWords) {
216 return OTCRYPTO_BAD_ARGS;
219 kP256MaskedScalarShareWords);
223 return OTCRYPTO_BAD_ARGS;
244 static status_t p256_public_key_length_check(
246 if (launder32(public_key->key_length) !=
sizeof(
p256_point_t)) {
247 return OTCRYPTO_BAD_ARGS;
267 static status_t p384_private_key_length_check(
269 if (private_key->keyblob == NULL) {
270 return OTCRYPTO_BAD_ARGS;
281 if (launder32(private_key->config.key_length) != kP384ScalarBytes) {
282 return OTCRYPTO_BAD_ARGS;
287 if (launder32(keyblob_share_num_words(private_key->config)) !=
288 kP384MaskedScalarShareWords) {
289 return OTCRYPTO_BAD_ARGS;
292 kP384MaskedScalarShareWords);
296 return OTCRYPTO_BAD_ARGS;
317 static status_t p384_public_key_length_check(
319 if (launder32(public_key->key_length) !=
sizeof(
p384_point_t)) {
320 return OTCRYPTO_BAD_ARGS;
339 static status_t internal_p256_keygen_finalize(
342 HARDENED_TRY(p256_private_key_length_check(private_key));
343 HARDENED_TRY(p256_public_key_length_check(public_key));
353 HARDENED_TRY(p256_sideload_keygen_finalize(pk));
357 HARDENED_TRY(p256_keygen_finalize(sk, pk));
358 private_key->checksum = integrity_blinded_checksum(private_key);
360 return OTCRYPTO_BAD_ARGS;
364 public_key->checksum = integrity_unblinded_checksum(public_key);
367 return keymgr_sideload_clear_otbn();
383 static status_t internal_p384_keygen_finalize(
386 HARDENED_TRY(p384_private_key_length_check(private_key));
387 HARDENED_TRY(p384_public_key_length_check(public_key));
398 HARDENED_TRY(p384_sideload_keygen_finalize(pk));
406 HARDENED_TRY(p384_keygen_finalize(sk, pk));
407 private_key->checksum = integrity_blinded_checksum(private_key);
409 return OTCRYPTO_BAD_ARGS;
412 public_key->checksum = integrity_unblinded_checksum(public_key);
420 if (elliptic_curve == NULL || private_key == NULL || public_key == NULL ||
421 private_key->keyblob == NULL || public_key->key == NULL) {
422 return OTCRYPTO_BAD_ARGS;
426 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdsa ||
427 launder32(public_key->key_mode) != kOtcryptoKeyModeEcdsa) {
428 return OTCRYPTO_BAD_ARGS;
434 switch (launder32(elliptic_curve->curve_type)) {
435 case kOtcryptoEccCurveTypeNistP256:
437 kOtcryptoEccCurveTypeNistP256);
438 HARDENED_TRY(internal_p256_keygen_finalize(private_key, public_key));
440 case kOtcryptoEccCurveTypeNistP384:
442 kOtcryptoEccCurveTypeNistP384);
443 HARDENED_TRY(internal_p384_keygen_finalize(private_key, public_key));
445 case kEccCurveTypeBrainpoolP256R1:
447 case kOtcryptoEccCurveTypeCustom:
449 return OTCRYPTO_NOT_IMPLEMENTED;
451 return OTCRYPTO_BAD_ARGS;
455 return keymgr_sideload_clear_otbn();
466 static status_t internal_ecdsa_p256_sign_start(
470 if (launder32(message_digest.len) != kP256ScalarWords) {
471 return OTCRYPTO_BAD_ARGS;
476 HARDENED_TRY(p256_private_key_length_check(private_key));
482 return p256_ecdsa_sign_start(message_digest.data, sk);
486 HARDENED_TRY(sideload_key_seed(private_key));
487 return p256_ecdsa_sideload_sign_start(message_digest.data);
491 return OTCRYPTO_BAD_ARGS;
502 static status_t internal_ecdsa_p384_sign_start(
506 if (launder32(message_digest.len) != kP384ScalarWords) {
507 return OTCRYPTO_BAD_ARGS;
512 HARDENED_TRY(p384_private_key_length_check(private_key));
518 return p384_ecdsa_sign_start(message_digest.data, sk);
522 HARDENED_TRY(sideload_key_seed(private_key));
523 return p384_ecdsa_sideload_sign_start(message_digest.data);
527 return OTCRYPTO_BAD_ARGS;
534 if (private_key == NULL || private_key->keyblob == NULL ||
535 elliptic_curve == NULL || message_digest.data == NULL) {
536 return OTCRYPTO_BAD_ARGS;
540 if (launder32(integrity_blinded_key_check(private_key)) !=
542 return OTCRYPTO_BAD_ARGS;
548 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdsa) {
549 return OTCRYPTO_BAD_ARGS;
554 HARDENED_TRY(entropy_complex_check());
557 switch (launder32(elliptic_curve->curve_type)) {
558 case kOtcryptoEccCurveTypeNistP256:
560 kOtcryptoEccCurveTypeNistP256);
561 HARDENED_TRY(internal_ecdsa_p256_sign_start(private_key, message_digest));
563 case kOtcryptoEccCurveTypeNistP384:
565 kOtcryptoEccCurveTypeNistP384);
566 HARDENED_TRY(internal_ecdsa_p384_sign_start(private_key, message_digest));
568 case kEccCurveTypeBrainpoolP256R1:
570 case kOtcryptoEccCurveTypeCustom:
572 return OTCRYPTO_NOT_IMPLEMENTED;
574 return OTCRYPTO_BAD_ARGS;
579 return OTCRYPTO_FATAL_ERR;
592 static status_t p256_signature_length_check(
size_t len) {
593 if (launder32(len) > UINT32_MAX /
sizeof(uint32_t) ||
595 return OTCRYPTO_BAD_ARGS;
612 static status_t p384_signature_length_check(
size_t len) {
613 if (launder32(len) > UINT32_MAX /
sizeof(uint32_t) ||
615 return OTCRYPTO_BAD_ARGS;
625 if (elliptic_curve == NULL || signature.data == NULL) {
626 return OTCRYPTO_BAD_ARGS;
630 switch (launder32(elliptic_curve->curve_type)) {
631 case kOtcryptoEccCurveTypeNistP256:
633 kOtcryptoEccCurveTypeNistP256);
634 HARDENED_TRY(p256_signature_length_check(signature.len));
640 HARDENED_TRY(p256_ecdsa_sign_finalize(sig_p256));
642 case kOtcryptoEccCurveTypeNistP384:
644 kOtcryptoEccCurveTypeNistP384);
645 HARDENED_TRY(p384_signature_length_check(signature.len));
651 HARDENED_TRY(p384_ecdsa_sign_finalize(sig_p384));
653 case kEccCurveTypeBrainpoolP256R1:
655 case kOtcryptoEccCurveTypeCustom:
657 return OTCRYPTO_NOT_IMPLEMENTED;
659 return OTCRYPTO_BAD_ARGS;
663 return keymgr_sideload_clear_otbn();
675 static status_t internal_ecdsa_p256_verify_start(
680 HARDENED_TRY(p256_public_key_length_check(public_key));
684 if (launder32(message_digest.len) != kP256ScalarWords) {
685 return OTCRYPTO_BAD_ARGS;
690 HARDENED_TRY(p256_signature_length_check(signature.len));
694 return p256_ecdsa_verify_start(sig, message_digest.data, pk);
706 static status_t internal_ecdsa_p384_verify_start(
711 HARDENED_TRY(p384_public_key_length_check(public_key));
715 if (launder32(message_digest.len) != kP384ScalarWords) {
716 return OTCRYPTO_BAD_ARGS;
721 HARDENED_TRY(p384_signature_length_check(signature.len));
725 return p384_ecdsa_verify_start(sig, message_digest.data, pk);
733 if (public_key == NULL || elliptic_curve == NULL || signature.data == NULL ||
734 message_digest.data == NULL || public_key->key == NULL) {
735 return OTCRYPTO_BAD_ARGS;
739 if (launder32(public_key->key_mode) != kOtcryptoKeyModeEcdsa) {
740 return OTCRYPTO_BAD_ARGS;
745 if (launder32(integrity_unblinded_key_check(public_key)) !=
747 return OTCRYPTO_BAD_ARGS;
753 switch (launder32(elliptic_curve->curve_type)) {
754 case kOtcryptoEccCurveTypeNistP256:
756 kOtcryptoEccCurveTypeNistP256);
757 HARDENED_TRY(internal_ecdsa_p256_verify_start(public_key, message_digest,
760 case kOtcryptoEccCurveTypeNistP384:
762 kOtcryptoEccCurveTypeNistP384);
763 HARDENED_TRY(internal_ecdsa_p384_verify_start(public_key, message_digest,
766 case kEccCurveTypeBrainpoolP256R1:
768 case kOtcryptoEccCurveTypeCustom:
770 return OTCRYPTO_NOT_IMPLEMENTED;
772 return OTCRYPTO_BAD_ARGS;
777 return OTCRYPTO_FATAL_ERR;
784 if (elliptic_curve == NULL || verification_result == NULL) {
785 return OTCRYPTO_BAD_ARGS;
789 switch (launder32(elliptic_curve->curve_type)) {
790 case kOtcryptoEccCurveTypeNistP256:
792 kOtcryptoEccCurveTypeNistP256);
793 HARDENED_TRY(p256_signature_length_check(signature.len));
796 return p256_ecdsa_verify_finalize(sig_p256, verification_result);
797 case kOtcryptoEccCurveTypeNistP384:
799 kOtcryptoEccCurveTypeNistP384);
800 HARDENED_TRY(p384_signature_length_check(signature.len));
803 return p384_ecdsa_verify_finalize(sig_p384, verification_result);
804 case kEccCurveTypeBrainpoolP256R1:
806 case kOtcryptoEccCurveTypeCustom:
808 return OTCRYPTO_NOT_IMPLEMENTED;
810 return OTCRYPTO_BAD_ARGS;
815 return OTCRYPTO_FATAL_ERR;
821 if (elliptic_curve == NULL || private_key == NULL ||
822 private_key->keyblob == NULL) {
823 return OTCRYPTO_BAD_ARGS;
827 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdh) {
828 return OTCRYPTO_BAD_ARGS;
833 HARDENED_TRY(entropy_complex_check());
836 switch (launder32(elliptic_curve->curve_type)) {
837 case kOtcryptoEccCurveTypeNistP256:
839 kOtcryptoEccCurveTypeNistP256);
842 HARDENED_TRY(sideload_key_seed(private_key));
843 return p256_sideload_keygen_start();
844 }
else if (launder32(private_key->config.hw_backed) ==
846 return p256_keygen_start();
848 return OTCRYPTO_BAD_ARGS;
849 case kOtcryptoEccCurveTypeNistP384:
851 kOtcryptoEccCurveTypeNistP384);
854 HARDENED_TRY(sideload_key_seed(private_key));
855 return p384_sideload_keygen_start();
856 }
else if (launder32(private_key->config.hw_backed) ==
859 return p384_keygen_start();
861 return OTCRYPTO_BAD_ARGS;
862 case kEccCurveTypeBrainpoolP256R1:
864 case kOtcryptoEccCurveTypeCustom:
866 return OTCRYPTO_NOT_IMPLEMENTED;
868 return OTCRYPTO_BAD_ARGS;
873 return OTCRYPTO_FATAL_ERR;
880 if (elliptic_curve == NULL || private_key == NULL || public_key == NULL ||
881 private_key->keyblob == NULL || public_key->key == NULL) {
882 return OTCRYPTO_BAD_ARGS;
886 if (launder32(public_key->key_mode) != kOtcryptoKeyModeEcdh ||
887 launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdh) {
888 return OTCRYPTO_BAD_ARGS;
894 switch (launder32(elliptic_curve->curve_type)) {
895 case kOtcryptoEccCurveTypeNistP256:
897 kOtcryptoEccCurveTypeNistP256);
898 HARDENED_TRY(internal_p256_keygen_finalize(private_key, public_key));
900 case kOtcryptoEccCurveTypeNistP384:
902 kOtcryptoEccCurveTypeNistP384);
903 HARDENED_TRY(internal_p384_keygen_finalize(private_key, public_key));
905 case kEccCurveTypeBrainpoolP256R1:
907 case kOtcryptoEccCurveTypeCustom:
909 return OTCRYPTO_NOT_IMPLEMENTED;
911 return OTCRYPTO_BAD_ARGS;
915 return keymgr_sideload_clear_otbn();
926 static status_t internal_ecdh_p256_start(
929 HARDENED_TRY(p256_private_key_length_check(private_key));
930 HARDENED_TRY(p256_public_key_length_check(public_key));
935 HARDENED_TRY(sideload_key_seed(private_key));
936 return p256_sideload_ecdh_start(pk);
940 return p256_ecdh_start(sk, pk);
944 return OTCRYPTO_BAD_ARGS;
955 static status_t internal_ecdh_p384_start(
958 HARDENED_TRY(p384_private_key_length_check(private_key));
959 HARDENED_TRY(p384_public_key_length_check(public_key));
964 HARDENED_TRY(sideload_key_seed(private_key));
965 return p384_sideload_ecdh_start(pk);
969 return p384_ecdh_start(sk, pk);
973 return OTCRYPTO_BAD_ARGS;
980 if (private_key == NULL || public_key == NULL || elliptic_curve == NULL ||
981 public_key->key == NULL || private_key->keyblob == NULL) {
982 return OTCRYPTO_BAD_ARGS;
986 if (launder32(integrity_blinded_key_check(private_key)) !=
988 launder32(integrity_unblinded_key_check(public_key)) !=
990 return OTCRYPTO_BAD_ARGS;
998 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdh ||
999 launder32(public_key->key_mode) != kOtcryptoKeyModeEcdh) {
1000 return OTCRYPTO_BAD_ARGS;
1006 switch (launder32(elliptic_curve->curve_type)) {
1007 case kOtcryptoEccCurveTypeNistP256:
1009 kOtcryptoEccCurveTypeNistP256);
1010 HARDENED_TRY(internal_ecdh_p256_start(private_key, public_key));
1012 case kOtcryptoEccCurveTypeNistP384:
1014 kOtcryptoEccCurveTypeNistP384);
1015 HARDENED_TRY(internal_ecdh_p384_start(private_key, public_key));
1017 case kEccCurveTypeBrainpoolP256R1:
1019 case kOtcryptoEccCurveTypeCustom:
1021 return OTCRYPTO_NOT_IMPLEMENTED;
1023 return OTCRYPTO_BAD_ARGS;
1028 return OTCRYPTO_FATAL_ERR;
1038 static status_t internal_ecdh_p256_finalize(
1042 return OTCRYPTO_BAD_ARGS;
1046 if (shared_secret->keyblob == NULL) {
1047 return OTCRYPTO_BAD_ARGS;
1050 if (launder32(shared_secret->config.key_length) != kP256CoordBytes) {
1051 return OTCRYPTO_BAD_ARGS;
1055 if (launder32(shared_secret->keyblob_length) !=
1056 keyblob_num_words(shared_secret->config) *
sizeof(uint32_t)) {
1057 return OTCRYPTO_BAD_ARGS;
1060 shared_secret->keyblob_length,
1061 keyblob_num_words(shared_secret->config) *
sizeof(uint32_t));
1067 HARDENED_TRY(p256_ecdh_finalize(&ss));
1069 keyblob_from_shares(ss.share0, ss.share1, shared_secret->config,
1070 shared_secret->keyblob);
1073 shared_secret->checksum = integrity_blinded_checksum(shared_secret);
1076 return keymgr_sideload_clear_otbn();
1086 static status_t internal_ecdh_p384_finalize(
1090 return OTCRYPTO_BAD_ARGS;
1094 if (shared_secret->keyblob == NULL) {
1095 return OTCRYPTO_BAD_ARGS;
1098 if (launder32(shared_secret->config.key_length) != kP384CoordBytes) {
1099 return OTCRYPTO_BAD_ARGS;
1103 if (launder32(shared_secret->keyblob_length) !=
1104 keyblob_num_words(shared_secret->config) *
sizeof(uint32_t)) {
1105 return OTCRYPTO_BAD_ARGS;
1108 shared_secret->keyblob_length,
1109 keyblob_num_words(shared_secret->config) *
sizeof(uint32_t));
1115 HARDENED_TRY(p384_ecdh_finalize(&ss));
1117 keyblob_from_shares(ss.share0, ss.share1, shared_secret->config,
1118 shared_secret->keyblob);
1121 shared_secret->checksum = integrity_blinded_checksum(shared_secret);
1124 return keymgr_sideload_clear_otbn();
1130 if (shared_secret == NULL || elliptic_curve == NULL) {
1131 return OTCRYPTO_BAD_ARGS;
1135 switch (launder32(elliptic_curve->curve_type)) {
1136 case kOtcryptoEccCurveTypeNistP256:
1138 kOtcryptoEccCurveTypeNistP256);
1139 HARDENED_TRY(internal_ecdh_p256_finalize(shared_secret));
1141 case kOtcryptoEccCurveTypeNistP384:
1143 kOtcryptoEccCurveTypeNistP384);
1144 HARDENED_TRY(internal_ecdh_p384_finalize(shared_secret));
1146 case kEccCurveTypeBrainpoolP256R1:
1148 case kOtcryptoEccCurveTypeCustom:
1150 return OTCRYPTO_NOT_IMPLEMENTED;
1152 return OTCRYPTO_BAD_ARGS;
1156 return keymgr_sideload_clear_otbn();
1162 return OTCRYPTO_NOT_IMPLEMENTED;
1168 return OTCRYPTO_NOT_IMPLEMENTED;
1176 return OTCRYPTO_NOT_IMPLEMENTED;
1182 return OTCRYPTO_NOT_IMPLEMENTED;
1191 return OTCRYPTO_NOT_IMPLEMENTED;
1197 return OTCRYPTO_NOT_IMPLEMENTED;
1203 return OTCRYPTO_NOT_IMPLEMENTED;
1209 return OTCRYPTO_NOT_IMPLEMENTED;
1216 return OTCRYPTO_NOT_IMPLEMENTED;
1222 return OTCRYPTO_NOT_IMPLEMENTED;