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/ecdh_p256.h"
10 #include "sw/device/lib/crypto/impl/ecc/ecdh_p384.h"
11 #include "sw/device/lib/crypto/impl/ecc/ecdsa_p256.h"
13 #include "sw/device/lib/crypto/impl/integrity.h"
14 #include "sw/device/lib/crypto/impl/keyblob.h"
18 #define MODULE_ID MAKE_MODULE_ID('e', 'c', 'c')
45 signature, elliptic_curve));
70 return OTCRYPTO_NOT_IMPLEMENTED;
78 return OTCRYPTO_NOT_IMPLEMENTED;
87 return OTCRYPTO_NOT_IMPLEMENTED;
93 return OTCRYPTO_NOT_IMPLEMENTED;
100 return OTCRYPTO_NOT_IMPLEMENTED;
115 keyblob_to_keymgr_diversification(private_key, &diversification));
116 return keymgr_generate_key_otbn(diversification);
122 if (elliptic_curve == NULL || private_key == NULL ||
123 private_key->keyblob == NULL) {
124 return OTCRYPTO_BAD_ARGS;
128 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdsa) {
129 return OTCRYPTO_BAD_ARGS;
134 HARDENED_TRY(entropy_complex_check());
137 switch (launder32(elliptic_curve->curve_type)) {
138 case kOtcryptoEccCurveTypeNistP256:
140 kOtcryptoEccCurveTypeNistP256);
143 HARDENED_TRY(sideload_key_seed(private_key));
144 return ecdsa_p256_sideload_keygen_start();
145 }
else if (launder32(private_key->config.hw_backed) ==
148 return ecdsa_p256_keygen_start();
150 return OTCRYPTO_BAD_ARGS;
153 case kOtcryptoEccCurveTypeNistP384:
155 kOtcryptoEccCurveTypeNistP384);
158 HARDENED_TRY(sideload_key_seed(private_key));
159 return ecdsa_p384_sideload_keygen_start();
160 }
else if (launder32(private_key->config.hw_backed) ==
163 return ecdsa_p384_keygen_start();
165 return OTCRYPTO_BAD_ARGS;
168 case kEccCurveTypeBrainpoolP256R1:
170 case kOtcryptoEccCurveTypeCustom:
172 return OTCRYPTO_NOT_IMPLEMENTED;
174 return OTCRYPTO_BAD_ARGS;
179 return OTCRYPTO_FATAL_ERR;
196 static status_t p256_private_key_length_check(
198 if (private_key->keyblob == NULL) {
199 return OTCRYPTO_BAD_ARGS;
210 if (launder32(private_key->config.key_length) != kP256ScalarBytes) {
211 return OTCRYPTO_BAD_ARGS;
216 if (launder32(keyblob_share_num_words(private_key->config)) !=
217 kP256MaskedScalarShareWords) {
218 return OTCRYPTO_BAD_ARGS;
221 kP256MaskedScalarShareWords);
225 return OTCRYPTO_BAD_ARGS;
246 static status_t p256_public_key_length_check(
248 if (launder32(public_key->key_length) !=
sizeof(
p256_point_t)) {
249 return OTCRYPTO_BAD_ARGS;
269 static status_t p384_private_key_length_check(
271 if (private_key->keyblob == NULL) {
272 return OTCRYPTO_BAD_ARGS;
283 if (launder32(private_key->config.key_length) != kP384ScalarBytes) {
284 return OTCRYPTO_BAD_ARGS;
289 if (launder32(keyblob_share_num_words(private_key->config)) !=
290 kP384MaskedScalarShareWords) {
291 return OTCRYPTO_BAD_ARGS;
294 kP384MaskedScalarShareWords);
298 return OTCRYPTO_BAD_ARGS;
319 static status_t p384_public_key_length_check(
321 if (launder32(public_key->key_length) !=
sizeof(
p384_point_t)) {
322 return OTCRYPTO_BAD_ARGS;
340 static status_t internal_ecdsa_p256_keygen_finalize(
343 HARDENED_TRY(p256_private_key_length_check(private_key));
344 HARDENED_TRY(p256_public_key_length_check(public_key));
355 HARDENED_TRY(ecdsa_p256_sideload_keygen_finalize(pk));
363 HARDENED_TRY(ecdsa_p256_keygen_finalize(sk, pk));
364 private_key->checksum = integrity_blinded_checksum(private_key);
366 return OTCRYPTO_BAD_ARGS;
369 public_key->checksum = integrity_unblinded_checksum(public_key);
385 static status_t internal_ecdsa_p384_keygen_finalize(
388 HARDENED_TRY(p384_private_key_length_check(private_key));
389 HARDENED_TRY(p384_public_key_length_check(public_key));
400 HARDENED_TRY(ecdsa_p384_sideload_keygen_finalize(pk));
408 HARDENED_TRY(ecdsa_p384_keygen_finalize(sk, pk));
409 private_key->checksum = integrity_blinded_checksum(private_key);
411 return OTCRYPTO_BAD_ARGS;
414 public_key->checksum = integrity_unblinded_checksum(public_key);
422 if (elliptic_curve == NULL || private_key == NULL || public_key == NULL ||
423 private_key->keyblob == NULL || public_key->key == NULL) {
424 return OTCRYPTO_BAD_ARGS;
428 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdsa ||
429 launder32(public_key->key_mode) != kOtcryptoKeyModeEcdsa) {
430 return OTCRYPTO_BAD_ARGS;
436 switch (launder32(elliptic_curve->curve_type)) {
437 case kOtcryptoEccCurveTypeNistP256:
439 kOtcryptoEccCurveTypeNistP256);
441 internal_ecdsa_p256_keygen_finalize(private_key, public_key));
443 case kOtcryptoEccCurveTypeNistP384:
445 kOtcryptoEccCurveTypeNistP384);
447 internal_ecdsa_p384_keygen_finalize(private_key, public_key));
449 case kEccCurveTypeBrainpoolP256R1:
451 case kOtcryptoEccCurveTypeCustom:
453 return OTCRYPTO_NOT_IMPLEMENTED;
455 return OTCRYPTO_BAD_ARGS;
459 return keymgr_sideload_clear_otbn();
470 static status_t internal_ecdsa_p256_sign_start(
474 if (launder32(message_digest.len) != kP256ScalarWords) {
475 return OTCRYPTO_BAD_ARGS;
480 HARDENED_TRY(p256_private_key_length_check(private_key));
486 return ecdsa_p256_sign_start(message_digest.data, sk);
490 HARDENED_TRY(sideload_key_seed(private_key));
491 return ecdsa_p256_sideload_sign_start(message_digest.data);
495 return OTCRYPTO_BAD_ARGS;
506 static status_t internal_ecdsa_p384_sign_start(
510 if (launder32(message_digest.len) != kP384ScalarWords) {
511 return OTCRYPTO_BAD_ARGS;
516 HARDENED_TRY(p384_private_key_length_check(private_key));
522 return ecdsa_p384_sign_start(message_digest.data, sk);
526 HARDENED_TRY(sideload_key_seed(private_key));
527 return ecdsa_p384_sideload_sign_start(message_digest.data);
531 return OTCRYPTO_BAD_ARGS;
538 if (private_key == NULL || private_key->keyblob == NULL ||
539 elliptic_curve == NULL || message_digest.data == NULL) {
540 return OTCRYPTO_BAD_ARGS;
544 if (launder32(integrity_blinded_key_check(private_key)) !=
546 return OTCRYPTO_BAD_ARGS;
552 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdsa) {
553 return OTCRYPTO_BAD_ARGS;
558 HARDENED_TRY(entropy_complex_check());
561 switch (launder32(elliptic_curve->curve_type)) {
562 case kOtcryptoEccCurveTypeNistP256:
564 kOtcryptoEccCurveTypeNistP256);
565 HARDENED_TRY(internal_ecdsa_p256_sign_start(private_key, message_digest));
567 case kOtcryptoEccCurveTypeNistP384:
569 kOtcryptoEccCurveTypeNistP384);
570 HARDENED_TRY(internal_ecdsa_p384_sign_start(private_key, message_digest));
572 case kEccCurveTypeBrainpoolP256R1:
574 case kOtcryptoEccCurveTypeCustom:
576 return OTCRYPTO_NOT_IMPLEMENTED;
578 return OTCRYPTO_BAD_ARGS;
583 return OTCRYPTO_FATAL_ERR;
596 static status_t p256_signature_length_check(
size_t len) {
597 if (launder32(len) > UINT32_MAX /
sizeof(uint32_t) ||
599 return OTCRYPTO_BAD_ARGS;
616 static status_t p384_signature_length_check(
size_t len) {
617 if (launder32(len) > UINT32_MAX /
sizeof(uint32_t) ||
619 return OTCRYPTO_BAD_ARGS;
629 if (elliptic_curve == NULL || signature.data == NULL) {
630 return OTCRYPTO_BAD_ARGS;
634 switch (launder32(elliptic_curve->curve_type)) {
635 case kOtcryptoEccCurveTypeNistP256:
637 kOtcryptoEccCurveTypeNistP256);
638 HARDENED_TRY(p256_signature_length_check(signature.len));
644 HARDENED_TRY(ecdsa_p256_sign_finalize(sig_p256));
646 case kOtcryptoEccCurveTypeNistP384:
648 kOtcryptoEccCurveTypeNistP384);
649 HARDENED_TRY(p384_signature_length_check(signature.len));
655 HARDENED_TRY(ecdsa_p384_sign_finalize(sig_p384));
657 case kEccCurveTypeBrainpoolP256R1:
659 case kOtcryptoEccCurveTypeCustom:
661 return OTCRYPTO_NOT_IMPLEMENTED;
663 return OTCRYPTO_BAD_ARGS;
667 return keymgr_sideload_clear_otbn();
679 static status_t internal_ecdsa_p256_verify_start(
684 HARDENED_TRY(p256_public_key_length_check(public_key));
688 if (launder32(message_digest.len) != kP256ScalarWords) {
689 return OTCRYPTO_BAD_ARGS;
694 HARDENED_TRY(p256_signature_length_check(signature.len));
698 return ecdsa_p256_verify_start(sig, message_digest.data, pk);
710 static status_t internal_ecdsa_p384_verify_start(
715 HARDENED_TRY(p384_public_key_length_check(public_key));
719 if (launder32(message_digest.len) != kP384ScalarWords) {
720 return OTCRYPTO_BAD_ARGS;
725 HARDENED_TRY(p384_signature_length_check(signature.len));
729 return ecdsa_p384_verify_start(sig, message_digest.data, pk);
737 if (public_key == NULL || elliptic_curve == NULL || signature.data == NULL ||
738 message_digest.data == NULL || public_key->key == NULL) {
739 return OTCRYPTO_BAD_ARGS;
743 if (launder32(public_key->key_mode) != kOtcryptoKeyModeEcdsa) {
744 return OTCRYPTO_BAD_ARGS;
749 if (launder32(integrity_unblinded_key_check(public_key)) !=
751 return OTCRYPTO_BAD_ARGS;
757 switch (launder32(elliptic_curve->curve_type)) {
758 case kOtcryptoEccCurveTypeNistP256:
760 kOtcryptoEccCurveTypeNistP256);
761 HARDENED_TRY(internal_ecdsa_p256_verify_start(public_key, message_digest,
764 case kOtcryptoEccCurveTypeNistP384:
766 kOtcryptoEccCurveTypeNistP384);
767 HARDENED_TRY(internal_ecdsa_p384_verify_start(public_key, message_digest,
770 case kEccCurveTypeBrainpoolP256R1:
772 case kOtcryptoEccCurveTypeCustom:
774 return OTCRYPTO_NOT_IMPLEMENTED;
776 return OTCRYPTO_BAD_ARGS;
781 return OTCRYPTO_FATAL_ERR;
788 if (elliptic_curve == NULL || verification_result == NULL) {
789 return OTCRYPTO_BAD_ARGS;
793 switch (launder32(elliptic_curve->curve_type)) {
794 case kOtcryptoEccCurveTypeNistP256:
796 kOtcryptoEccCurveTypeNistP256);
797 HARDENED_TRY(p256_signature_length_check(signature.len));
800 return ecdsa_p256_verify_finalize(sig_p256, verification_result);
801 case kOtcryptoEccCurveTypeNistP384:
803 kOtcryptoEccCurveTypeNistP384);
804 HARDENED_TRY(p384_signature_length_check(signature.len));
807 return ecdsa_p384_verify_finalize(sig_p384, verification_result);
808 case kEccCurveTypeBrainpoolP256R1:
810 case kOtcryptoEccCurveTypeCustom:
812 return OTCRYPTO_NOT_IMPLEMENTED;
814 return OTCRYPTO_BAD_ARGS;
819 return OTCRYPTO_FATAL_ERR;
825 if (elliptic_curve == NULL || private_key == NULL ||
826 private_key->keyblob == NULL) {
827 return OTCRYPTO_BAD_ARGS;
831 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdh) {
832 return OTCRYPTO_BAD_ARGS;
837 HARDENED_TRY(entropy_complex_check());
840 switch (launder32(elliptic_curve->curve_type)) {
841 case kOtcryptoEccCurveTypeNistP256:
843 kOtcryptoEccCurveTypeNistP256);
846 HARDENED_TRY(sideload_key_seed(private_key));
847 return ecdh_p256_sideload_keypair_start();
848 }
else if (launder32(private_key->config.hw_backed) ==
850 return ecdh_p256_keypair_start();
852 return OTCRYPTO_BAD_ARGS;
853 case kOtcryptoEccCurveTypeNistP384:
855 kOtcryptoEccCurveTypeNistP384);
858 HARDENED_TRY(sideload_key_seed(private_key));
859 return ecdh_p384_sideload_keypair_start();
860 }
else if (launder32(private_key->config.hw_backed) ==
863 return ecdh_p384_keypair_start();
865 return OTCRYPTO_BAD_ARGS;
866 case kEccCurveTypeBrainpoolP256R1:
868 case kOtcryptoEccCurveTypeCustom:
870 return OTCRYPTO_NOT_IMPLEMENTED;
872 return OTCRYPTO_BAD_ARGS;
877 return OTCRYPTO_FATAL_ERR;
892 static status_t internal_ecdh_p256_keygen_finalize(
895 HARDENED_TRY(p256_private_key_length_check(private_key));
896 HARDENED_TRY(p256_public_key_length_check(public_key));
906 HARDENED_TRY(ecdh_p256_sideload_keypair_finalize(pk));
910 HARDENED_TRY(ecdh_p256_keypair_finalize(sk, pk));
911 private_key->checksum = integrity_blinded_checksum(private_key);
913 return OTCRYPTO_BAD_ARGS;
917 public_key->checksum = integrity_unblinded_checksum(public_key);
920 return keymgr_sideload_clear_otbn();
935 static status_t internal_ecdh_p384_keygen_finalize(
938 HARDENED_TRY(p384_private_key_length_check(private_key));
939 HARDENED_TRY(p384_public_key_length_check(public_key));
949 HARDENED_TRY(ecdh_p384_sideload_keypair_finalize(pk));
953 HARDENED_TRY(ecdh_p384_keypair_finalize(sk, pk));
954 private_key->checksum = integrity_blinded_checksum(private_key);
956 return OTCRYPTO_BAD_ARGS;
960 public_key->checksum = integrity_unblinded_checksum(public_key);
963 return keymgr_sideload_clear_otbn();
970 if (elliptic_curve == NULL || private_key == NULL || public_key == NULL ||
971 private_key->keyblob == NULL || public_key->key == NULL) {
972 return OTCRYPTO_BAD_ARGS;
976 if (launder32(public_key->key_mode) != kOtcryptoKeyModeEcdh ||
977 launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdh) {
978 return OTCRYPTO_BAD_ARGS;
984 switch (launder32(elliptic_curve->curve_type)) {
985 case kOtcryptoEccCurveTypeNistP256:
987 kOtcryptoEccCurveTypeNistP256);
988 HARDENED_TRY(internal_ecdh_p256_keygen_finalize(private_key, public_key));
990 case kOtcryptoEccCurveTypeNistP384:
992 kOtcryptoEccCurveTypeNistP384);
993 HARDENED_TRY(internal_ecdh_p384_keygen_finalize(private_key, public_key));
995 case kEccCurveTypeBrainpoolP256R1:
997 case kOtcryptoEccCurveTypeCustom:
999 return OTCRYPTO_NOT_IMPLEMENTED;
1001 return OTCRYPTO_BAD_ARGS;
1005 return keymgr_sideload_clear_otbn();
1016 static status_t internal_ecdh_p256_start(
1019 HARDENED_TRY(p256_private_key_length_check(private_key));
1020 HARDENED_TRY(p256_public_key_length_check(public_key));
1025 HARDENED_TRY(sideload_key_seed(private_key));
1026 return ecdh_p256_sideload_shared_key_start(pk);
1030 return ecdh_p256_shared_key_start(sk, pk);
1034 return OTCRYPTO_BAD_ARGS;
1045 static status_t internal_ecdh_p384_start(
1048 HARDENED_TRY(p384_private_key_length_check(private_key));
1049 HARDENED_TRY(p384_public_key_length_check(public_key));
1054 HARDENED_TRY(sideload_key_seed(private_key));
1055 return ecdh_p384_sideload_shared_key_start(pk);
1059 return ecdh_p384_shared_key_start(sk, pk);
1063 return OTCRYPTO_BAD_ARGS;
1070 if (private_key == NULL || public_key == NULL || elliptic_curve == NULL ||
1071 public_key->key == NULL || private_key->keyblob == NULL) {
1072 return OTCRYPTO_BAD_ARGS;
1076 if (launder32(integrity_blinded_key_check(private_key)) !=
1078 launder32(integrity_unblinded_key_check(public_key)) !=
1080 return OTCRYPTO_BAD_ARGS;
1088 if (launder32(private_key->config.key_mode) != kOtcryptoKeyModeEcdh ||
1089 launder32(public_key->key_mode) != kOtcryptoKeyModeEcdh) {
1090 return OTCRYPTO_BAD_ARGS;
1096 switch (launder32(elliptic_curve->curve_type)) {
1097 case kOtcryptoEccCurveTypeNistP256:
1099 kOtcryptoEccCurveTypeNistP256);
1100 HARDENED_TRY(internal_ecdh_p256_start(private_key, public_key));
1102 case kOtcryptoEccCurveTypeNistP384:
1104 kOtcryptoEccCurveTypeNistP384);
1105 HARDENED_TRY(internal_ecdh_p384_start(private_key, public_key));
1107 case kEccCurveTypeBrainpoolP256R1:
1109 case kOtcryptoEccCurveTypeCustom:
1111 return OTCRYPTO_NOT_IMPLEMENTED;
1113 return OTCRYPTO_BAD_ARGS;
1118 return OTCRYPTO_FATAL_ERR;
1128 static status_t internal_ecdh_p256_finalize(
1132 return OTCRYPTO_BAD_ARGS;
1136 if (shared_secret->keyblob == NULL) {
1137 return OTCRYPTO_BAD_ARGS;
1140 if (launder32(shared_secret->config.key_length) != kP256CoordBytes) {
1141 return OTCRYPTO_BAD_ARGS;
1145 if (launder32(shared_secret->keyblob_length) !=
1146 keyblob_num_words(shared_secret->config) *
sizeof(uint32_t)) {
1147 return OTCRYPTO_BAD_ARGS;
1150 shared_secret->keyblob_length,
1151 keyblob_num_words(shared_secret->config) *
sizeof(uint32_t));
1157 HARDENED_TRY(ecdh_p256_shared_key_finalize(&ss));
1159 keyblob_from_shares(ss.share0, ss.share1, shared_secret->config,
1160 shared_secret->keyblob);
1163 shared_secret->checksum = integrity_blinded_checksum(shared_secret);
1166 return keymgr_sideload_clear_otbn();
1176 static status_t internal_ecdh_p384_finalize(
1180 return OTCRYPTO_BAD_ARGS;
1184 if (shared_secret->keyblob == NULL) {
1185 return OTCRYPTO_BAD_ARGS;
1188 if (launder32(shared_secret->config.key_length) != kP384CoordBytes) {
1189 return OTCRYPTO_BAD_ARGS;
1193 if (launder32(shared_secret->keyblob_length) !=
1194 keyblob_num_words(shared_secret->config) *
sizeof(uint32_t)) {
1195 return OTCRYPTO_BAD_ARGS;
1198 shared_secret->keyblob_length,
1199 keyblob_num_words(shared_secret->config) *
sizeof(uint32_t));
1205 HARDENED_TRY(ecdh_p384_shared_key_finalize(&ss));
1207 keyblob_from_shares(ss.share0, ss.share1, shared_secret->config,
1208 shared_secret->keyblob);
1211 shared_secret->checksum = integrity_blinded_checksum(shared_secret);
1214 return keymgr_sideload_clear_otbn();
1220 if (shared_secret == NULL || elliptic_curve == NULL) {
1221 return OTCRYPTO_BAD_ARGS;
1225 switch (launder32(elliptic_curve->curve_type)) {
1226 case kOtcryptoEccCurveTypeNistP256:
1228 kOtcryptoEccCurveTypeNistP256);
1229 HARDENED_TRY(internal_ecdh_p256_finalize(shared_secret));
1231 case kOtcryptoEccCurveTypeNistP384:
1233 kOtcryptoEccCurveTypeNistP384);
1234 HARDENED_TRY(internal_ecdh_p384_finalize(shared_secret));
1236 case kEccCurveTypeBrainpoolP256R1:
1238 case kOtcryptoEccCurveTypeCustom:
1240 return OTCRYPTO_NOT_IMPLEMENTED;
1242 return OTCRYPTO_BAD_ARGS;
1246 return keymgr_sideload_clear_otbn();
1252 return OTCRYPTO_NOT_IMPLEMENTED;
1258 return OTCRYPTO_NOT_IMPLEMENTED;
1266 return OTCRYPTO_NOT_IMPLEMENTED;
1272 return OTCRYPTO_NOT_IMPLEMENTED;
1281 return OTCRYPTO_NOT_IMPLEMENTED;
1287 return OTCRYPTO_NOT_IMPLEMENTED;
1293 return OTCRYPTO_NOT_IMPLEMENTED;
1299 return OTCRYPTO_NOT_IMPLEMENTED;
1306 return OTCRYPTO_NOT_IMPLEMENTED;
1312 return OTCRYPTO_NOT_IMPLEMENTED;