mbedtls_pk_get_psa_attributes: ECC support

Add code and unit tests for MBEDTLS_PK_ECxxx in
mbedtls_pk_get_psa_attributes().

This commit only supports built-in ECC (MBEDTLS_ECP_C). A subsequent commit
will handle driver-only ECC.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/tests/suites/test_suite_pk.function b/tests/suites/test_suite_pk.function
index d6902b4..90c5044 100644
--- a/tests/suites/test_suite_pk.function
+++ b/tests/suites/test_suite_pk.function
@@ -187,7 +187,7 @@
             } else {
                 unsigned char N[PSA_BITS_TO_BYTES(MBEDTLS_RSA_GEN_KEY_MIN_BITS)] = { 0xff };
                 N[sizeof(N) - 1] = 0x03;
-                const unsigned char E[1] = {0x03};
+                const unsigned char E[1] = { 0x03 };
                 TEST_EQUAL(mbedtls_rsa_import_raw(rsa,
                                                   N, sizeof(N),
                                                   NULL, 0, NULL, 0, NULL, 0,
@@ -198,6 +198,23 @@
         }
 #endif /* MBEDTLS_RSA_C */
 
+#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
+        case MBEDTLS_PK_ECKEY:
+        case MBEDTLS_PK_ECKEY_DH:
+        case MBEDTLS_PK_ECDSA:
+        {
+            mbedtls_ecp_group_id grp_id = mbedtls_ecp_grp_id_list()[0];
+            size_t bits;
+            *psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(mbedtls_ecc_group_to_psa(grp_id, &bits));
+            TEST_EQUAL(pk_genkey(pk, grp_id), 0);
+            if (!want_pair) {
+                mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
+                mbedtls_mpi_free(&ec->d);
+            }
+            break;
+        }
+#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
+
         default:
             TEST_FAIL("Unknown PK type in test data");
             break;
@@ -1674,6 +1691,17 @@
     }
     expected_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
 
+#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
+    /* When the resulting algorithm is ECDSA, the compile-time configuration
+     * can cause it to be either deterministic or randomized ECDSA.
+     * Rather than have two near-identical sets of test data depending on
+     * the configuration, always use randomized in the test data and
+     * tweak the expected result here. */
+    if (expected_alg == PSA_ALG_ECDSA(PSA_ALG_ANY_HASH)) {
+        expected_alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
+    }
+#endif
+
     TEST_EQUAL(mbedtls_pk_get_psa_attributes(&pk, usage, &attributes), 0);
 
     TEST_EQUAL(psa_get_key_lifetime(&attributes), lifetime);