nuttx/crypto: export Diffie-Hellman algorithm

Supplementary CRK_DH_MAKE_PUBLIC and CRK_DH_COMPUTE_KEY with curve25519
Signed-off-by: makejian <makejian@xiaomi.com>
This commit is contained in:
makejian 2024-09-03 21:35:28 +08:00 committed by Xiang Xiao
parent 882c09abb3
commit bcb90fab7c
3 changed files with 63 additions and 6 deletions

View File

@ -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)

View File

@ -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);
}

View File

@ -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)