regression 4006: add test cases for SM2 PKE

Adds test cases for SM2 Public Key Encryption.

Signed-off-by: Jerome Forissier <jerome@forissier.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/host/xtest/regression_4000.c b/host/xtest/regression_4000.c
index 7a60ce2..441540e 100644
--- a/host/xtest/regression_4000.c
+++ b/host/xtest/regression_4000.c
@@ -3875,6 +3875,13 @@
 	/* [B-283] - GP NOT SUPPORTED */
 	/* [B-409] - GP NOT SUPPORTED */
 	/* [B-571] - GP NOT SUPPORTED */
+
+	XTEST_AC_ECC_CASE(0, TEE_ALG_SM2_PKE, TEE_MODE_ENCRYPT,
+			  gmt_0003_part5_c2_sm2_testvector),
+	XTEST_AC_ECC_CASE(0, TEE_ALG_SM2_PKE, TEE_MODE_DECRYPT,
+			  gmt_0003_part5_c2_sm2_testvector),
+	XTEST_AC_ECC_CASE(0, TEE_ALG_SM2_PKE, TEE_MODE_ENCRYPT,
+			  sm2_testvector2),
 };
 
 static bool create_key(ADBG_Case_t *c, TEEC_Session *s,
@@ -3942,6 +3949,8 @@
 	uint32_t ret_orig = 0;
 	size_t n = 0;
 	uint32_t curve = 0;
+	uint32_t pub_key_type = 0;
+	uint32_t priv_key_type = 0;
 	uint32_t hash_algo = 0;
 
 	if (!ADBG_EXPECT_TEEC_SUCCESS(c,
@@ -4126,21 +4135,37 @@
 			break;
 
 		case TEE_MAIN_ALGO_ECDSA:
+		case TEE_MAIN_ALGO_SM2_PKE:
 			switch (tv->algo) {
 			case TEE_ALG_ECDSA_P192:
 				curve = TEE_ECC_CURVE_NIST_P192;
+				pub_key_type = TEE_TYPE_ECDSA_PUBLIC_KEY;
+				priv_key_type = TEE_TYPE_ECDSA_KEYPAIR;
 				break;
 			case TEE_ALG_ECDSA_P224:
 				curve = TEE_ECC_CURVE_NIST_P224;
+				pub_key_type = TEE_TYPE_ECDSA_PUBLIC_KEY;
+				priv_key_type = TEE_TYPE_ECDSA_KEYPAIR;
 				break;
 			case TEE_ALG_ECDSA_P256:
 				curve = TEE_ECC_CURVE_NIST_P256;
+				pub_key_type = TEE_TYPE_ECDSA_PUBLIC_KEY;
+				priv_key_type = TEE_TYPE_ECDSA_KEYPAIR;
 				break;
 			case TEE_ALG_ECDSA_P384:
 				curve = TEE_ECC_CURVE_NIST_P384;
+				pub_key_type = TEE_TYPE_ECDSA_PUBLIC_KEY;
+				priv_key_type = TEE_TYPE_ECDSA_KEYPAIR;
 				break;
 			case TEE_ALG_ECDSA_P521:
 				curve = TEE_ECC_CURVE_NIST_P521;
+				pub_key_type = TEE_TYPE_ECDSA_PUBLIC_KEY;
+				priv_key_type = TEE_TYPE_ECDSA_KEYPAIR;
+				break;
+			case TEE_ALG_SM2_PKE:
+				curve = TEE_ECC_CURVE_SM2;
+				pub_key_type = TEE_TYPE_SM2_PKE_PUBLIC_KEY;
+				priv_key_type = TEE_TYPE_SM2_PKE_KEYPAIR;
 				break;
 			default:
 				curve = 0xFF;
@@ -4165,7 +4190,7 @@
 
 			if (!ADBG_EXPECT_TRUE(c,
 				create_key(c, &session, max_key_size,
-					   TEE_TYPE_ECDSA_PUBLIC_KEY, key_attrs,
+					   pub_key_type, key_attrs,
 					   num_key_attrs, &pub_key_handle)))
 				goto out;
 
@@ -4176,7 +4201,7 @@
 
 			if (!ADBG_EXPECT_TRUE(c,
 				create_key(c, &session, max_key_size,
-					   TEE_TYPE_ECDSA_KEYPAIR, key_attrs,
+					   priv_key_type, key_attrs,
 					   num_key_attrs, &priv_key_handle)))
 				goto out;
 			break;
diff --git a/host/xtest/regression_4000_data.h b/host/xtest/regression_4000_data.h
index d40a3eb..03f72de 100644
--- a/host/xtest/regression_4000_data.h
+++ b/host/xtest/regression_4000_data.h
@@ -7762,4 +7762,72 @@
 	/* [B-571] - GP NOT SUPPORTED */
 };
 
+/* G/MT 0003 (SM2) Part 5 Annex C.2 - encryption/decryption */
+static const uint8_t gmt_0003_part5_c2_sm2_testvector_ptx[19] =
+/* M */
+	"encryption standard";
+static const uint8_t gmt_0003_part5_c2_sm2_testvector_private[] = {
+/* dB */
+	0x39, 0x45, 0x20, 0x8F, 0x7B, 0x21, 0x44, 0xB1, 0x3F, 0x36, 0xE3, 0x8A,
+	0xC6, 0xD3, 0x9F, 0x95, 0x88, 0x93, 0x93, 0x69, 0x28, 0x60, 0xB5, 0x1A,
+	0x42, 0xFB, 0x81, 0xEF, 0x4D, 0xF7, 0xC5, 0xB8
+};
+static const uint8_t gmt_0003_part5_c2_sm2_testvector_public_x[] = {
+/* xB */
+	0x09, 0xF9, 0xDF, 0x31, 0x1E, 0x54, 0x21, 0xA1, 0x50, 0xDD, 0x7D, 0x16,
+	0x1E, 0x4B, 0xC5, 0xC6, 0x72, 0x17, 0x9F, 0xAD, 0x18, 0x33, 0xFC, 0x07,
+	0x6B, 0xB0, 0x8F, 0xF3, 0x56, 0xF3, 0x50, 0x20
+};
+static const uint8_t gmt_0003_part5_c2_sm2_testvector_public_y[] = {
+/* yB */
+	0xCC, 0xEA, 0x49, 0x0C, 0xE2, 0x67, 0x75, 0xA5, 0x2D, 0xC6, 0xEA, 0x71,
+	0x8C, 0xC1, 0xAA, 0x60, 0x0A, 0xED, 0x05, 0xFB, 0xF3, 0x5E, 0x08, 0x4A,
+	0x66, 0x32, 0xF6, 0x07, 0x2D, 0xA9, 0xAD, 0x13
+};
+static const uint8_t gmt_0003_part5_c2_sm2_testvector_out[] = {
+/* C */
+	/* C1 */
+	0x04,
+	0x04, 0xEB, 0xFC, 0x71, 0x8E, 0x8D, 0x17, 0x98, 0x62, 0x04, 0x32, 0x26,
+	0x8E, 0x77, 0xFE, 0xB6, 0x41, 0x5E, 0x2E, 0xDE, 0x0E, 0x07, 0x3C, 0x0F,
+	0x4F, 0x64, 0x0E, 0xCD, 0x2E, 0x14, 0x9A, 0x73, 0xE8, 0x58, 0xF9, 0xD8,
+	0x1E, 0x54, 0x30, 0xA5, 0x7B, 0x36, 0xDA, 0xAB, 0x8F, 0x95, 0x0A, 0x3C,
+	0x64, 0xE6, 0xEE, 0x6A, 0x63, 0x09, 0x4D, 0x99, 0x28, 0x3A, 0xFF, 0x76,
+	0x7E, 0x12, 0x4D, 0xF0,
+	/* C2 */
+	0x21, 0x88, 0x6C, 0xA9, 0x89, 0xCA, 0x9C, 0x7D, 0x58, 0x08, 0x73, 0x07,
+	0xCA, 0x93, 0x09, 0x2D, 0x65, 0x1E, 0xFA,
+	/* C3 */
+	0x59, 0x98, 0x3C, 0x18, 0xF8, 0x09, 0xE2, 0x62, 0x92, 0x3C, 0x53, 0xAE,
+	0xC2, 0x95, 0xD3, 0x03, 0x83, 0xB5, 0x4E, 0x39, 0xD6, 0x09, 0xD1, 0x60,
+	0xAF, 0xCB, 0x19, 0x08, 0xD0, 0xBD, 0x87, 0x66
+};
+
+/* SM2 encryption/decryption */
+static const uint8_t sm2_testvector2_ptx[] =
+/* M */
+	"This test vector is used to test encryption then decryption. The "
+	"plain text is longer than 64 characters, so it will exercise parts "
+	"of the code that are not used for shorter messages.";
+static const uint8_t sm2_testvector2_private[] = {
+/* dB */
+	0x39, 0x45, 0x20, 0x8F, 0x7B, 0x21, 0x44, 0xB1, 0x3F, 0x36, 0xE3, 0x8A,
+	0xC6, 0xD3, 0x9F, 0x95, 0x88, 0x93, 0x93, 0x69, 0x28, 0x60, 0xB5, 0x1A,
+	0x42, 0xFB, 0x81, 0xEF, 0x4D, 0xF7, 0xC5, 0xB8
+};
+static const uint8_t sm2_testvector2_public_x[] = {
+/* xB */
+	0x09, 0xF9, 0xDF, 0x31, 0x1E, 0x54, 0x21, 0xA1, 0x50, 0xDD, 0x7D, 0x16,
+	0x1E, 0x4B, 0xC5, 0xC6, 0x72, 0x17, 0x9F, 0xAD, 0x18, 0x33, 0xFC, 0x07,
+	0x6B, 0xB0, 0x8F, 0xF3, 0x56, 0xF3, 0x50, 0x20
+};
+static const uint8_t sm2_testvector2_public_y[] = {
+/* yB */
+	0xCC, 0xEA, 0x49, 0x0C, 0xE2, 0x67, 0x75, 0xA5, 0x2D, 0xC6, 0xEA, 0x71,
+	0x8C, 0xC1, 0xAA, 0x60, 0x0A, 0xED, 0x05, 0xFB, 0xF3, 0x5E, 0x08, 0x4A,
+	0x66, 0x32, 0xF6, 0x07, 0x2D, 0xA9, 0xAD, 0x13
+};
+static const uint8_t sm2_testvector2_out[] = {
+	0x00, /* Not used -- test runs in encrypt/decrypt mode */
+};
 #endif /*XTEST_4000_DATA_H*/