Skip to contentSkip to conversation

SM2 crypto algorithm support

PATCH Superseded Gowrishankar Muthukrishnan 2 participants 7 messages May 26, 2023 – Jun 4, 2023 Crypto API

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover SM2 crypto algorithm support

This patch series adds SM2 crypto algorithm support, along with tests verified using Openssl.

v2:
- addressed suggestions in v1.

Gowrishankar Muthukrishnan (3):
cryptodev: add SM2 asymmetric crypto algorithm test/crypto: add asymmetric SM2 test cases crypto/openssl: add SM2 asymmetric crypto support

 app/test/test_cryptodev_asym.c               | 504 +++++++++++++++++++
 app/test/test_cryptodev_sm2_test_vectors.h   | 120 +++++
 doc/guides/cryptodevs/features/default.ini   |   1 +
 doc/guides/cryptodevs/features/openssl.ini   |   1 +
 doc/guides/cryptodevs/openssl.rst            |   1 +
 doc/guides/rel_notes/release_23_07.rst       |   9 +
 drivers/crypto/openssl/openssl_pmd_private.h |   7 +
 drivers/crypto/openssl/rte_openssl_pmd.c     | 245 +++++++++
 drivers/crypto/openssl/rte_openssl_pmd_ops.c | 101 ++++
 lib/cryptodev/rte_crypto_asym.h              |  77 +++
 lib/cryptodev/rte_cryptodev.c                |   1 +
 11 files changed, 1067 insertions(+)
 create mode 100644 app/test/test_cryptodev_sm2_test_vectors.h
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/3 cryptodev: add SM2 asymmetric crypto algorithm

ShangMi 2 (SM2) is a encryption and digital signature algorithm used in the Chinese National Standard.

Added support for asymmetric SM2 in cryptodev along with prime field curve, as referenced in RFC:
https://datatracker.ietf.org/doc/html/draft-shen-sm2-ecdsa-02

---
 doc/guides/cryptodevs/features/default.ini |  1 +
 doc/guides/rel_notes/release_23_07.rst     |  5 ++
 lib/cryptodev/rte_crypto_asym.h            | 77 ++++++++++++++++++++++
 lib/cryptodev/rte_cryptodev.c              |  1 +
 4 files changed, 84 insertions(+)
doc/guides/cryptodevs/features/default.ini +1 -0
index 523da0cfa8..a69967bb9e 100644
@@ -125,6 +125,7 @@ Diffie-hellman          =
 ECDSA                   =
 ECPM                    =
 ECDH                    =
+SM2                     =
 
 ;
 ; Supported Operating systems of a default crypto driver.
doc/guides/rel_notes/release_23_07.rst +5 -0
index a9b1293689..8b8e69d619 100644
@@ -55,6 +55,11 @@ New Features
      Also, make sure to start the actual text at the margin.
      =======================================================
 
+* **Added SM2 asymmetric algorithm in cryptodev.**
+
+  Added support for ShamMi 2 (SM2) asymmetric crypto algorithm
+  along with prime field curve support.
+
 
 Removed Items
 -------------
lib/cryptodev/rte_crypto_asym.h +77 -0

104 lines not on this page · raw message

lib/cryptodev/rte_cryptodev.c +1 -0
index a96114b2da..21cabc3ffd 100644
@@ -299,6 +299,7 @@ crypto_asym_xform_strings[] = {
 	[RTE_CRYPTO_ASYM_XFORM_DSA]	= "dsa",
 	[RTE_CRYPTO_ASYM_XFORM_ECDSA]	= "ecdsa",
 	[RTE_CRYPTO_ASYM_XFORM_ECPM]	= "ecpm",
+	[RTE_CRYPTO_ASYM_XFORM_SM2]	= "sm2",
 };
 
 /**
Signature · 1 line
2.25.1
#2 lore raw

RE: [PATCH v2 1/3] cryptodev: add SM2 asymmetric crypto algorithm

Hi Gowrishankar,

Quoted 11 lines

-----Original Message-----
From: Gowrishankar Muthukrishnan <[email protected]>
Sent: Friday, May 26, 2023 11:12 AM
To: [email protected]
Cc: [email protected]; Akhil Goyal <[email protected]>; Fan Zhang <[email protected]>; Gowrishankar Muthukrishnan <[email protected]>
Subject: [PATCH v2 1/3] cryptodev: add SM2 asymmetric crypto algorithm

ShangMi 2 (SM2) is a encryption and digital signature algorithm used in the Chinese National Standard.

It is more of a set of public-key cryptography algorithms based on elliptic curves.

Quoted 83 lines

Added support for asymmetric SM2 in cryptodev along with prime field curve, as referenced in RFC:
https://datatracker.ietf.org/doc/html/draft-shen-sm2-ecdsa-02

---
 doc/guides/cryptodevs/features/default.ini |  1 +
 doc/guides/rel_notes/release_23_07.rst     |  5 ++
 lib/cryptodev/rte_crypto_asym.h            | 77 ++++++++++++++++++++++
 lib/cryptodev/rte_cryptodev.c              |  1 +
 4 files changed, 84 insertions(+)
doc/guides/cryptodevs/features/default.ini +1 -0
index 523da0cfa8..a69967bb9e 100644
@@ -125,6 +125,7 @@ Diffie-hellman          =
 ECDSA                   =
 ECPM                    =
 ECDH                    =
+SM2                     =
 
 ;
 ; Supported Operating systems of a default crypto driver.
doc/guides/rel_notes/release_23_07.rst +5 -0
index a9b1293689..8b8e69d619 100644
@@ -55,6 +55,11 @@ New Features
      Also, make sure to start the actual text at the margin.
      =======================================================
 
+* **Added SM2 asymmetric algorithm in cryptodev.**
+
+  Added support for ShamMi 2 (SM2) asymmetric crypto algorithm  along
+ with prime field curve support.
+
 
 Removed Items
 -------------
lib/cryptodev/rte_crypto_asym.h +18 -0
index 989f38323f..35fa2c0a6d 100644
@@ -119,6 +119,11 @@ enum rte_crypto_asym_xform_type {
 	/**< Elliptic Curve Point Multiplication */
 	RTE_CRYPTO_ASYM_XFORM_ECFPM,
 	/**< Elliptic Curve Fixed Point Multiplication */
+	RTE_CRYPTO_ASYM_XFORM_SM2,
+	/**< ShangMi 2
+	 * Performs Encrypt, Decrypt, Sign and Verify.
+	 * Refer to rte_crypto_asym_op_type.
+	 */
 	RTE_CRYPTO_ASYM_XFORM_TYPE_LIST_END
 	/**< End of list */
 };
@@ -382,6 +387,20 @@ struct rte_crypto_ec_xform {
 	/**< Pre-defined ec groups */
 };
 
+/**
+ * Asymmetric SM2 transform data
+ *
+ * Structure describing SM2 xform params
+ *
+ */
+struct rte_crypto_sm2_xform {
+	rte_crypto_uint pkey;
+	/**< Private key of the signer for signature generation. */
+
+	struct rte_crypto_ec_point q;
+	/**< Public key of the signer for verification. */ };
+
 /**
  * Operations params for modular operations:
  * exponentiation and multiplicative inverse @@ -637,9 +656,66 @@ struct

rte_crypto_asym_xform {

lib/cryptodev/rte_crypto_asym.h +0 -0
 		/**< EC xform parameters, used by elliptic curve based
 		 * operations.
 		 */

lib/cryptodev/rte_crypto_asym.h struct rte_crypto_asym_xform { · line 668

Quoted 10 lines
lib/cryptodev/rte_crypto_asym.h +7 -0
+
+		struct rte_crypto_sm2_xform sm2;
+		/**< SM2 xform parameters */
 	};
 };
 
+/**
+ * SM2 operation params
+ */
+struct rte_crypto_sm2_op_param {

There is no random value 'k'. And SM2 is also using it for encryption.
There is no key exchange or point multiplication option in this op, therefore, I would rather have all SM2 algorithms in separate ops. We also could abandon finally '_param' suffix, it does not add clarity but extends struct tags, which are very long already.

lib/cryptodev/rte_crypto_asym.h struct rte_crypto_asym_xform { · line 677

Quoted 9 lines
lib/cryptodev/rte_crypto_asym.h +9 -0
+	enum rte_crypto_asym_op_type op_type;
+	/**< Signature generation or verification */
+
+	rte_crypto_param message;
+	/**<
+	 * Pointer to input data
+	 * - to be encrypted for SM2 public encrypt.
+	 * - to be signed for SM2 sign generation.
+	 * - to be authenticated for SM2 sign verification.

Is 'message' in signature case here plaintext or hash?
If 'plaintext' it is inconsistent with other signature algorithms, and most likely not all HW devices will support that. Additionally or hash function should be specified as an argument by the user, or the function used should be defined in the API information. I see it is not directly said in this draft what function it ought be, although most likely SM3 would be the one.

Quoted 17 lines
lib/cryptodev/rte_crypto_asym.h +17 -0
+	 *
+	 * Pointer to output data
+	 * - for SM2 private decrypt.
+	 * In this case the underlying array should have been
+	 * allocated with enough memory to hold plaintext output
+	 * (at least encrypted text length). The message.length field
+	 * will be overwritten by the PMD with the decrypted length.
+	 */
+
+	rte_crypto_param cipher;
+	/**<
+	 * Pointer to input data
+	 * - to be decrypted for SM2 private decrypt.
+	 *
+	 * Pointer to output data
+	 * - for SM2 public encrypt.
+	 * In this case the underlying array should have been allocated

lib/cryptodev/rte_crypto_asym.h struct rte_crypto_asym_xform {

Quoted 10 lines
lib/cryptodev/rte_crypto_asym.h +10 -0
+	 * with enough memory to hold ciphertext output (at least X bytes
+	 * for prime field curve of N bytes and for message M bytes,
+	 * where X = (C1 + C2 + C3) and computed based on SM2 RFC as
+	 * C1 (1 + N + N), C2 = M, C3 = N. The cipher.length field will
+	 * be overwritten by the PMD with the encrypted length.
+	 */
+
+	rte_crypto_uint id;
+	/**< The SM2 id used by signer and verifier and is in interval (1,
+n-1). */

What is this parameter exactly, is it an ID prepended to the signature input?

Quoted 40 lines
lib/cryptodev/rte_crypto_asym.h +16 -0
+
+	rte_crypto_uint r;
+	/**< r component of elliptic curve signature
+	 *     output : for signature generation (of at least N bytes
+	 *              where prime field length is N bytes)
+	 *     input  : for signature verification
+	 */
+	rte_crypto_uint s;
+	/**< s component of elliptic curve signature
+	 *     output : for signature generation (of at least N bytes
+	 *              where prime field length is N bytes)
+	 *     input  : for signature verification
+	 */
+};
+
 /**
  * Asymmetric Cryptographic Operation.
  *
@@ -665,6 +741,7 @@ struct rte_crypto_asym_op {
 		struct rte_crypto_dsa_op_param dsa;
 		struct rte_crypto_ecdsa_op_param ecdsa;
 		struct rte_crypto_ecpm_op_param ecpm;
+		struct rte_crypto_sm2_op_param sm2;
 	};
 	uint16_t flags;
 	/**<
lib/cryptodev/rte_cryptodev.c index renamed from lib/cryptodev/rte_cryptodev.c +0 -0

a96114b2da..21cabc3ffd 100644

lib/cryptodev/rte_cryptodev.c +1 -1
@@ -299,6 +299,7 @@ crypto_asym_xform_strings[] = {
 	[RTE_CRYPTO_ASYM_XFORM_DSA]	= "dsa",
 	[RTE_CRYPTO_ASYM_XFORM_ECDSA]	= "ecdsa",
 	[RTE_CRYPTO_ASYM_XFORM_ECPM]	= "ecpm",
+	[RTE_CRYPTO_ASYM_XFORM_SM2]	= "sm2",
 };
 
 /**
--

2.25.1

#3 lore raw

RE: [PATCH v2 1/3] cryptodev: add SM2 asymmetric crypto algorithm

Hi ArkadiuszX,

Quoted 4 lines
Quoted 2 lines

ShangMi 2 (SM2) is a encryption and digital signature algorithm used in the Chinese National Standard.

It is more of a set of public-key cryptography algorithms based on elliptic curves.

Ok I ll rephrase it.

Quoted 2 lines
Quoted 2 lines

...

Quoted 83 lines
Quoted 83 lines
---
 doc/guides/cryptodevs/features/default.ini |  1 +
 doc/guides/rel_notes/release_23_07.rst     |  5 ++
 lib/cryptodev/rte_crypto_asym.h            | 77 ++++++++++++++++++++++
 lib/cryptodev/rte_cryptodev.c              |  1 +
 4 files changed, 84 insertions(+)
doc/guides/cryptodevs/features/default.ini +1 -0
index 523da0cfa8..a69967bb9e 100644
@@ -125,6 +125,7 @@ Diffie-hellman          =
 ECDSA                   =
 ECPM                    =
 ECDH                    =
+SM2                     =
 
 ;
 ; Supported Operating systems of a default crypto driver.
doc/guides/rel_notes/release_23_07.rst +5 -0
index a9b1293689..8b8e69d619 100644
@@ -55,6 +55,11 @@ New Features
      Also, make sure to start the actual text at the margin.
      =======================================================
 
+* **Added SM2 asymmetric algorithm in cryptodev.**
+
+  Added support for ShamMi 2 (SM2) asymmetric crypto algorithm  along
+ with prime field curve support.
+
 
 Removed Items
 -------------
(quoted diff) +0 -0

b/lib/cryptodev/rte_crypto_asym.h index 989f38323f..35fa2c0a6d 100644

lib/cryptodev/rte_crypto_asym.h +18 -0
@@ -119,6 +119,11 @@ enum rte_crypto_asym_xform_type {
 	/**< Elliptic Curve Point Multiplication */
 	RTE_CRYPTO_ASYM_XFORM_ECFPM,
 	/**< Elliptic Curve Fixed Point Multiplication */
+	RTE_CRYPTO_ASYM_XFORM_SM2,
+	/**< ShangMi 2
+	 * Performs Encrypt, Decrypt, Sign and Verify.
+	 * Refer to rte_crypto_asym_op_type.
+	 */
 	RTE_CRYPTO_ASYM_XFORM_TYPE_LIST_END
 	/**< End of list */
 };
@@ -382,6 +387,20 @@ struct rte_crypto_ec_xform {
 	/**< Pre-defined ec groups */
 };
 
+/**
+ * Asymmetric SM2 transform data
+ *
+ * Structure describing SM2 xform params
+ *
+ */
+struct rte_crypto_sm2_xform {
+	rte_crypto_uint pkey;
+	/**< Private key of the signer for signature generation. */
+
+	struct rte_crypto_ec_point q;
+	/**< Public key of the signer for verification. */ };
+
 /**
  * Operations params for modular operations:
  * exponentiation and multiplicative inverse @@ -637,9 +656,66 @@

struct rte_crypto_asym_xform {

lib/cryptodev/rte_crypto_asym.h +3 -0
 		/**< EC xform parameters, used by elliptic curve based
 		 * operations.
 		 */
+
+		struct rte_crypto_sm2_xform sm2;
+		/**< SM2 xform parameters */
 	};
 };
Quoted 6 lines

lib/cryptodev/rte_crypto_asym.h struct rte_crypto_asym_xform { · line 668

Quoted 4 lines
lib/cryptodev/rte_crypto_asym.h +4 -0
+/**
+ * SM2 operation params
+ */
+struct rte_crypto_sm2_op_param {

There is no random value 'k'. And SM2 is also using it for encryption.

Agreed. But if we see openSSL kind of SW library, it does not take this param from app. (for eg sign/encrypt), instead it generates on its own (still through RNG as I see). so, shall we support either way meaning extern_scalar or inbuilt_scalar ? I am thinking we can add enum like below:
- RTE_EC_SCALAR_INBUILT
- RTE_EC_SCALAR_EXTERNAL

With inbuilt scalar, this k value in param can be ignored by PMD, while in external case, pass it to HW.

Quoted 2 lines

There is no key exchange or point multiplication option in this op, therefore, I would rather have all SM2 algorithms in separate ops.

You mean, we shall have structs rte_crypto_sm2_sign_op, rte_crypto_sm2_verify_op etc dedicated for each op ?

Quoted 2 lines

We also could abandon finally '_param' suffix, it does not add clarity but extends struct tags, which are very long already.

Only for SM2 or for all algorithms ?

Quoted 3 lines

lib/cryptodev/rte_crypto_asym.h struct rte_crypto_asym_xform { · line 670

Quoted 3 lines
lib/cryptodev/rte_crypto_asym.h +2 -0
+	enum rte_crypto_asym_op_type op_type;
+	/**< Signature generation or verification */

I need to add correction here. Op type can also be encrypt or decrypt.

Quoted 9 lines

lib/cryptodev/rte_crypto_asym.h struct rte_crypto_asym_xform { · line 677

Quoted 7 lines
lib/cryptodev/rte_crypto_asym.h +7 -0
+
+	rte_crypto_param message;
+	/**<
+	 * Pointer to input data
+	 * - to be encrypted for SM2 public encrypt.
+	 * - to be signed for SM2 sign generation.
+	 * - to be authenticated for SM2 sign verification.

Is 'message' in signature case here plaintext or hash?

As per RFC draft, it seems plain text, because we add prefix to it and then H256().

Quoted 2 lines

If 'plaintext' it is inconsistent with other signature algorithms, and most likely not all HW devices will support that.

It is just another EC right ? Even in EC op param, we don't distinguish plaintext/hash message.

Quoted 4 lines

Additionally or hash function should be specified as an argument by the user, or the function used should be defined in the API information. I see it is not directly said in this draft what function it ought be, although most likely
SM3 would be the one.

Agree, SM2 sign implementation would need to know which hash algo to pick. Hence, we can add hash function as param. RFC does not list options but quotes that Algorithm should have been approved by Chinese Commercial Cryptography Administration Office. So I am not too sure if we could add SHA3/SHAKE apart from SHA256 if it is approved.
- SM3
- SHA256
- SHA3_256
- SHAKE_256

May be PMD could evaluate it and return -ENOTSUP accordingly ?

Quoted 22 lines
Quoted 22 lines
lib/cryptodev/rte_crypto_asym.h +21 -0
+	 *
+	 * Pointer to output data
+	 * - for SM2 private decrypt.
+	 * In this case the underlying array should have been
+	 * allocated with enough memory to hold plaintext output
+	 * (at least encrypted text length). The message.length field
+	 * will be overwritten by the PMD with the decrypted length.
+	 */
+
+	rte_crypto_param cipher;
+	/**<
+	 * Pointer to input data
+	 * - to be decrypted for SM2 private decrypt.
+	 *
+	 * Pointer to output data
+	 * - for SM2 public encrypt.
+	 * In this case the underlying array should have been allocated
+	 * with enough memory to hold ciphertext output (at least X bytes
+	 * for prime field curve of N bytes and for message M bytes,
+	 * where X = (C1 + C2 + C3) and computed based on SM2 RFC as
+	 * C1 (1 + N + N), C2 = M, C3 = N. The cipher.length field will
Quoted 9 lines

lib/cryptodev/rte_crypto_asym.h struct rte_crypto_asym_xform {

Quoted 6 lines
lib/cryptodev/rte_crypto_asym.h +6 -0
+	 * be overwritten by the PMD with the encrypted length.
+	 */
+
+	rte_crypto_uint id;
+	/**< The SM2 id used by signer and verifier and is in interval (1,
+n-1). */

What is this parameter exactly, is it an ID prepended to the signature input?

This is signer identifier as per RFC. This gets concatenated with other EC params:
ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)

ZA is then once again added with message before one more hash on entire thing.

Quoted 37 lines
Quoted 37 lines
lib/cryptodev/rte_crypto_asym.h +16 -0
+
+	rte_crypto_uint r;
+	/**< r component of elliptic curve signature
+	 *     output : for signature generation (of at least N bytes
+	 *              where prime field length is N bytes)
+	 *     input  : for signature verification
+	 */
+	rte_crypto_uint s;
+	/**< s component of elliptic curve signature
+	 *     output : for signature generation (of at least N bytes
+	 *              where prime field length is N bytes)
+	 *     input  : for signature verification
+	 */
+};
+
 /**
  * Asymmetric Cryptographic Operation.
  *
@@ -665,6 +741,7 @@ struct rte_crypto_asym_op {
 		struct rte_crypto_dsa_op_param dsa;
 		struct rte_crypto_ecdsa_op_param ecdsa;
 		struct rte_crypto_ecpm_op_param ecpm;
+		struct rte_crypto_sm2_op_param sm2;
 	};
 	uint16_t flags;
 	/**<
(quoted diff) +0 -0

b/lib/cryptodev/rte_cryptodev.c index a96114b2da..21cabc3ffd 100644

lib/cryptodev/rte_cryptodev.c +1 -0
@@ -299,6 +299,7 @@ crypto_asym_xform_strings[] = {
 	[RTE_CRYPTO_ASYM_XFORM_DSA]	= "dsa",
 	[RTE_CRYPTO_ASYM_XFORM_ECDSA]	= "ecdsa",
 	[RTE_CRYPTO_ASYM_XFORM_ECPM]	= "ecpm",
+	[RTE_CRYPTO_ASYM_XFORM_SM2]	= "sm2",
 };
 
Quoted 3 lines
Quoted 3 lines
lib/cryptodev/rte_cryptodev.c +0 -1
 /**
--

2.25.1

Thanks for the review,
Gowrishankar

#4 lore raw

RE: [PATCH v2 1/3] cryptodev: add SM2 asymmetric crypto algorithm

Hi,
I have simplified sm2 xform to only have hash algo and rest in crypto op. Please review v3 and let me know if changes are good. Also, I need to test plain scalar k value but I don't have support in openSSL to do that. So, I will meanwhile get back once I am done with that part as well, but v3 is good to go for openSSL pmd.

Thanks,
Gowrishankar

#5 lore raw

PATCH v2 2/3 test/crypto: add asymmetric SM2 test cases

In reply to Gowrishankar Muthukrishnan

Added test cases and test vectors for asymmetric SM2 crypto verification. Cases are added for sign/verify/encrypt/decrypt.

---
 app/test/test_cryptodev_asym.c             | 504 +++++++++++++++++++++
 app/test/test_cryptodev_sm2_test_vectors.h | 120 +++++
 2 files changed, 624 insertions(+)
 create mode 100644 app/test/test_cryptodev_sm2_test_vectors.h
app/test/test_cryptodev_asym.c +504 -0
index 9236817650..68029c9910 100644
@@ -21,6 +21,7 @@
 #include "test_cryptodev_ecpm_test_vectors.h"
 #include "test_cryptodev_mod_test_vectors.h"
 #include "test_cryptodev_rsa_test_vectors.h"
+#include "test_cryptodev_sm2_test_vectors.h"
 #include "test_cryptodev_asym_util.h"
 #include "test.h"
 
@@ -2196,6 +2197,505 @@ test_ecpm_all_curve(void)
 	return overall_status;
 }
 
+static int
+test_sm2_sign(void)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	struct crypto_testsuite_sm2_params input_params = sm2_param_fp256;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_op *result_op = NULL;
+	uint8_t output_buf_r[TEST_DATA_SIZE];
+	uint8_t output_buf_s[TEST_DATA_SIZE];
+	struct rte_crypto_asym_xform xform;
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_SM2;
+	xform.sm2.pkey.data = input_params.pkey.data;
+	xform.sm2.pkey.length = input_params.pkey.length;
+	xform.sm2.q.x.data = input_params.pubkey_qx.data;
+	xform.sm2.q.x.length = input_params.pubkey_qx.length;
+	xform.sm2.q.y.data = input_params.pubkey_qy.data;
+	xform.sm2.q.y.length = input_params.pubkey_qy.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Compute sign */
+
+	/* Populate op with operational details */
+	op->asym->sm2.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
+	op->asym->sm2.message.data = input_params.message.data;
+	op->asym->sm2.message.length = input_params.message.length;
+	op->asym->sm2.id.data = input_params.id.data;
+	op->asym->sm2.id.length = input_params.id.length;
+
+	/* Init out buf */
+	op->asym->sm2.r.data = output_buf_r;
+	op->asym->sm2.s.data = output_buf_s;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "r:",
+			asym_op->sm2.r.data, asym_op->sm2.r.length);
+	debug_hexdump(stdout, "s:",
+			asym_op->sm2.s.data, asym_op->sm2.s.length);
+
+	/* Verify sign (in roundtrip).
+	 * Due to random number used per message, sign op
+	 * would produce different output for same message
+	 * everytime. Hence, we can't have expected output
+	 * to match, instead reverse op to verify.
+	 */
+
+	/* Populate op with operational details */
+	op->asym->sm2.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
+
+	/* Enqueue sign result for verify */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		status = TEST_FAILED;
+		goto exit;
+	}
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"SM2 verify failed.\n");
+		goto exit;
+	}
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+};
+
+static int
+test_sm2_verify(void)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	struct crypto_testsuite_sm2_params input_params = sm2_param_fp256;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_op *result_op = NULL;
+	struct rte_crypto_asym_xform xform;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_SM2;
+	xform.sm2.q.x.data = input_params.pubkey_qx.data;
+	xform.sm2.q.x.length = input_params.pubkey_qx.length;
+	xform.sm2.q.y.data = input_params.pubkey_qy.data;
+	xform.sm2.q.y.length = input_params.pubkey_qy.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Verify given sign */
+
+	/* Populate op with operational details */
+	op->asym->sm2.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
+	op->asym->sm2.message.data = input_params.message.data;
+	op->asym->sm2.message.length = input_params.message.length;
+	op->asym->sm2.r.data = input_params.sign_r.data;
+	op->asym->sm2.r.length = input_params.sign_r.length;
+	op->asym->sm2.s.data = input_params.sign_s.data;
+	op->asym->sm2.s.length = input_params.sign_s.length;
+	op->asym->sm2.id.data = input_params.id.data;
+	op->asym->sm2.id.length = input_params.id.length;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+};
+
+static int
+test_sm2_encrypt(void)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	struct crypto_testsuite_sm2_params input_params = sm2_param_fp256;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	uint8_t output_buf[TEST_DATA_SIZE], *pbuf = NULL;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_op *result_op = NULL;
+	struct rte_crypto_asym_xform xform;
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_SM2;
+	xform.sm2.pkey.data = input_params.pkey.data;
+	xform.sm2.pkey.length = input_params.pkey.length;
+	xform.sm2.q.x.data = input_params.pubkey_qx.data;
+	xform.sm2.q.x.length = input_params.pubkey_qx.length;
+	xform.sm2.q.y.data = input_params.pubkey_qy.data;
+	xform.sm2.q.y.length = input_params.pubkey_qy.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Compute encrypt */
+
+	/* Populate op with operational details */
+	op->asym->sm2.op_type = RTE_CRYPTO_ASYM_OP_ENCRYPT;
+	op->asym->sm2.message.data = input_params.message.data;
+	op->asym->sm2.message.length = input_params.message.length;
+
+	/* Init out buf */
+	op->asym->sm2.cipher.data = output_buf;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "cipher:",
+			asym_op->sm2.cipher.data, asym_op->sm2.cipher.length);
+
+	/* Verify cipher (in roundtrip).
+	 * Due to random number used per message, encrypt op
+	 * would produce different output for same message
+	 * everytime. Hence, we can't have expected output
+	 * to match, instead reverse op to decrypt.
+	 */
+
+	/* Populate op with operational details */
+	op->asym->sm2.op_type = RTE_CRYPTO_ASYM_OP_DECRYPT;
+	pbuf = rte_malloc(NULL, TEST_DATA_SIZE, 0);
+	op->asym->sm2.message.data = pbuf;
+	op->asym->sm2.message.length = TEST_DATA_SIZE;
+
+	/* Enqueue cipher result for decrypt */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		status = TEST_FAILED;
+		goto exit;
+	}
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"SM2 encrypt failed.\n");
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+	if (memcmp(input_params.message.data, asym_op->sm2.message.data,
+			   asym_op->sm2.message.length) != 0) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"SM2 encrypt failed.\n");
+		goto exit;
+	}
+exit:
+	if (pbuf != NULL)
+		rte_free(pbuf);
+
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+};
+
+static int
+test_sm2_decrypt(void)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	struct crypto_testsuite_sm2_params input_params = sm2_param_fp256;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_op *result_op = NULL;
+	uint8_t output_buf_m[TEST_DATA_SIZE];
+	struct rte_crypto_asym_xform xform;
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_SM2;
+	xform.sm2.pkey.data = input_params.pkey.data;
+	xform.sm2.pkey.length = input_params.pkey.length;
+	xform.sm2.q.x.data = input_params.pubkey_qx.data;
+	xform.sm2.q.x.length = input_params.pubkey_qx.length;
+	xform.sm2.q.y.data = input_params.pubkey_qy.data;
+	xform.sm2.q.y.length = input_params.pubkey_qy.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Compute decrypt */
+
+	/* Populate op with operational details */
+	op->asym->sm2.op_type = RTE_CRYPTO_ASYM_OP_DECRYPT;
+	op->asym->sm2.cipher.data = input_params.cipher.data;
+	op->asym->sm2.cipher.length = input_params.cipher.length;
+
+	/* Init out buf */
+	op->asym->sm2.message.data = output_buf_m;
+	op->asym->sm2.message.length = RTE_DIM(output_buf_m);
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "message:",
+			asym_op->sm2.message.data, asym_op->sm2.message.length);
+
+	if (memcmp(input_params.message.data, asym_op->sm2.message.data,
+			op->asym->sm2.message.length)) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"SM2 decrypt failed.\n");
+		goto exit;
+	}
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+};
+
 static struct unit_test_suite cryptodev_openssl_asym_testsuite  = {
 	.suite_name = "Crypto Device OPENSSL ASYM Unit Test Suite",
 	.setup = testsuite_setup,
@@ -2205,6 +2705,10 @@ static struct unit_test_suite cryptodev_openssl_asym_testsuite  = {
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_dsa),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
 				test_dh_keygenration),
+		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_sm2_encrypt),
+		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_sm2_sign),
+		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_sm2_verify),
+		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_sm2_decrypt),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_rsa_enc_dec),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
 				test_rsa_sign_verify),
app/test/test_cryptodev_sm2_test_vectors.h new file +120 -0

120 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw

PATCH v2 3/3 crypto/openssl: add SM2 asymmetric crypto support

In reply to Gowrishankar Muthukrishnan

Add SM2 asymmetric algorithm support in openssl PMD.

---
 doc/guides/cryptodevs/features/openssl.ini   |   1 +
 doc/guides/cryptodevs/openssl.rst            |   1 +
 doc/guides/rel_notes/release_23_07.rst       |   4 +
 drivers/crypto/openssl/openssl_pmd_private.h |   7 +
 drivers/crypto/openssl/rte_openssl_pmd.c     | 245 +++++++++++++++++++
 drivers/crypto/openssl/rte_openssl_pmd_ops.c | 101 ++++++++
 6 files changed, 359 insertions(+)
doc/guides/cryptodevs/features/openssl.ini +1 -0
index 4b0f9b162e..b64c8ec4a5 100644
@@ -65,6 +65,7 @@ DSA = Y
 Modular Exponentiation = Y
 Modular Inversion = Y
 Diffie-hellman = Y
+SM2 = Y
 
 ;
 ; Supported Operating systems of the 'openssl' crypto driver.
doc/guides/cryptodevs/openssl.rst +1 -0
index 03041ceda1..ff21d21b23 100644
@@ -53,6 +53,7 @@ Supported Asymmetric Crypto algorithms:
 * ``RTE_CRYPTO_ASYM_XFORM_DH``
 * ``RTE_CRYPTO_ASYM_XFORM_MODINV``
 * ``RTE_CRYPTO_ASYM_XFORM_MODEX``
+* ``RTE_CRYPTO_ASYM_XFORM_SM2``
 
 
 Installation
doc/guides/rel_notes/release_23_07.rst +4 -0
index 8b8e69d619..aeebcffb60 100644
@@ -61,6 +61,10 @@ New Features
   along with prime field curve support.
 
 
+* **Updated OpenSSL crypto driver for SM2 support.**
+
+  Added SM2 algorithm support in asymmetric crypto operations.
+
 Removed Items
 -------------
 
drivers/crypto/openssl/openssl_pmd_private.h +7 -0
index ed6841e460..d7990c8333 100644
@@ -12,6 +12,7 @@
 #include <openssl/rsa.h>
 #include <openssl/dh.h>
 #include <openssl/dsa.h>
+#include <openssl/ec.h>
 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
 #include <openssl/provider.h>
 #include <openssl/core_names.h>
@@ -200,6 +201,12 @@ struct openssl_asym_session {
 			OSSL_PARAM_BLD * param_bld;
 #endif
 		} s;
+		struct {
+#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
+			OSSL_PARAM *key_params;
+			OSSL_PARAM *enc_params;
+#endif
+		} sm2;
 	} u;
 } __rte_cache_aligned;
 /** Set and validate OPENSSL crypto session parameters */
drivers/crypto/openssl/rte_openssl_pmd.c +245 -0

269 lines not on this page · raw message

drivers/crypto/openssl/rte_openssl_pmd_ops.c +101 -0

113 lines not on this page · raw message

Signature · 1 line
2.25.1