You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2024/09/09 15:55:42 UTC
(nuttx) 02/02: nuttx/crypto: export Diffie-Hellman algorithm
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit bcb90fab7c3a051b8cbd9358147175cbfb146cc4
Author: makejian <ma...@xiaomi.com>
AuthorDate: Tue Sep 3 21:35:28 2024 +0800
nuttx/crypto: export Diffie-Hellman algorithm
Supplementary CRK_DH_MAKE_PUBLIC and CRK_DH_COMPUTE_KEY with curve25519
Signed-off-by: makejian <ma...@xiaomi.com>
---
crypto/cryptodev.c | 7 +++++++
crypto/cryptosoft.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++
include/crypto/cryptodev.h | 13 ++++++------
3 files changed, 63 insertions(+), 6 deletions(-)
diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c
index 5bda3ccf68..2e6adcf09e 100644
--- a/crypto/cryptodev.c
+++ b/crypto/cryptodev.c
@@ -601,6 +601,13 @@ int cryptodev_key(FAR struct crypt_kop *kop)
break;
}
+ return -EINVAL;
+ case CRK_DH_MAKE_PUBLIC:
+ if (in == 2 && out == 2)
+ {
+ break;
+ }
+
return -EINVAL;
case CRK_RSA_PKCS15_VERIFY:
if (in == 5 && out == 0)
diff --git a/crypto/cryptosoft.c b/crypto/cryptosoft.c
index 2c8be17d62..a19e9d4000 100644
--- a/crypto/cryptosoft.c
+++ b/crypto/cryptosoft.c
@@ -32,6 +32,7 @@
#include <crypto/bn.h>
#include <crypto/cryptodev.h>
#include <crypto/cryptosoft.h>
+#include <crypto/curve25519.h>
#include <crypto/xform.h>
#include <sys/param.h>
@@ -1154,6 +1155,38 @@ done:
return 0;
}
+static int swcr_dh_make_public(FAR struct cryptkop *krp)
+{
+ /* Curve25519 is used for testing. In fact,
+ * the four parameters of this interface are p, g, x, gx;
+ * p: used to determine the conic curve;
+ * g: the base point of the curve;
+ * x: the private key produced by random;
+ * gx: the public key generated by the private key,
+ * which could be caculated by gx = g ^ x mod p;
+ * In curve25519, p and g are fixed.
+ */
+
+ uint8_t *secret = (uint8_t *)krp->krp_param[2].crp_p;
+ uint8_t *public = (uint8_t *)krp->krp_param[3].crp_p;
+
+ curve25519_generate_secret(secret);
+ return curve25519_generate_public(public, secret);
+}
+
+static int swcr_dh_make_common(FAR struct cryptkop *krp)
+{
+ /* Curve25519 is used for testing. In fact,
+ * the four parameters of this interface are:
+ * public key / private key / p (the conic curve) / shared key
+ */
+
+ uint8_t *public = (uint8_t *)krp->krp_param[0].crp_p;
+ uint8_t *secret = (uint8_t *)krp->krp_param[1].crp_p;
+ uint8_t *shared = (uint8_t *)krp->krp_param[3].crp_p;
+ return curve25519(shared, secret, public);
+}
+
int swcr_rsa_verify(struct cryptkop *krp)
{
uint8_t *exp = (uint8_t *)krp->krp_param[0].crp_p;
@@ -1196,6 +1229,20 @@ int swcr_kprocess(struct cryptkop *krp)
switch (krp->krp_op)
{
+ case CRK_DH_MAKE_PUBLIC:
+ if ((krp->krp_status = swcr_dh_make_public(krp) != 0))
+ {
+ goto done;
+ }
+
+ break;
+ case CRK_DH_COMPUTE_KEY:
+ if ((krp->krp_status = swcr_dh_make_common(krp)) != 0)
+ {
+ goto done;
+ }
+
+ break;
case CRK_RSA_PKCS15_VERIFY:
if ((krp->krp_status = swcr_rsa_verify(krp)) != 0)
{
@@ -1270,6 +1317,8 @@ void swcr_init(void)
crypto_register(swcr_id, algs, swcr_newsession,
swcr_freesession, swcr_process);
+ kalgs[CRK_DH_MAKE_PUBLIC] = CRYPTO_ALG_FLAG_SUPPORTED;
+ kalgs[CRK_DH_COMPUTE_KEY] = CRYPTO_ALG_FLAG_SUPPORTED;
kalgs[CRK_RSA_PKCS15_VERIFY] = CRYPTO_ALG_FLAG_SUPPORTED;
crypto_kregister(swcr_id, kalgs, swcr_kprocess);
}
diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h
index c742e2cf53..a499ba113d 100644
--- a/include/crypto/cryptodev.h
+++ b/include/crypto/cryptodev.h
@@ -253,12 +253,13 @@ struct crypt_kop
#define CRK_MOD_EXP_CRT 1
#define CRK_DSA_SIGN 2
#define CRK_DSA_VERIFY 3
-#define CRK_DH_COMPUTE_KEY 4
-#define CRK_RSA_PKCS15_VERIFY 5
-#define CRK_ECDSA_SECP256R1_SIGN 6
-#define CRK_ECDSA_SECP256R1_VERIFY 7
-#define CRK_ECDSA_SECP256R1_GENKEY 8
-#define CRK_ALGORITHM_MAX 8 /* Keep updated */
+#define CRK_DH_MAKE_PUBLIC 4
+#define CRK_DH_COMPUTE_KEY 5
+#define CRK_RSA_PKCS15_VERIFY 6
+#define CRK_ECDSA_SECP256R1_SIGN 7
+#define CRK_ECDSA_SECP256R1_VERIFY 8
+#define CRK_ECDSA_SECP256R1_GENKEY 9
+#define CRK_ALGORITHM_MAX 9 /* Keep updated */
#define CRF_MOD_EXP (1 << CRK_MOD_EXP)
#define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT)