pkparse: parse also Weierstrass private keys using ecp_read_key()
This is to hanlde more uniformly Weierstrass and Montgomery curves.
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/library/pkparse.c b/library/pkparse.c
index 7f2816b..1091c42 100644
--- a/library/pkparse.c
+++ b/library/pkparse.c
@@ -1217,15 +1217,11 @@
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
}
+ /* Keep a reference to the position fo the private key. It will be used
+ * later in this function. */
d = p;
d_len = len;
-#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
- if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
- return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
- }
-#endif
-
p += len;
pubkey_done = 0;
@@ -1245,6 +1241,13 @@
}
}
+
+#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
+ if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
+ }
+#endif
+
if (p != end) {
/*
* Is 'publickey' present? If not, or if we can't read it (eg because it