add ECDH and CURVE25519 algorithms support for Kunpeng 930
Subsystems (10)
v9 cover add ECDH and CURVE25519 algorithms support for Kunpeng 930
1. Move curve ID from the key into the algorithm name (like 'ecdh-nist-pxxx' so we get its tfm like 'crypto_alloc_kpp("ecdh-nist-p256", 0, 0)'), in 'crypto/ecc.c' (has been verified by testmgr) and 'crypto/atmel-ecc.c' (only compiled, not do test), and modify 'testmgr.c' and 'net/bluetooth/smp.c' (only compiled, not do test) to adapt the modification;
2. Add new file 'include/crypto/ecc_curve.h', and move 'struct ecc_point' and 'struct ecc_curve' definitions to it, also add new APIs 'ecc_get_curveXXX' into it, with these APIs, users in kernel tree can get ECDH and curve25519 parameters;
3. Add ECDH and CURVE25519 algorithms support for Kunpeng 930.
v8->v9:
- patch #3: squash patches 3-5 in v8 into one in v9
- patch #4: delete ECDH curve parameters: P224, P384 and P521
- patch #5: delete ecdh-nist-p224, ecdh-nist-p384 and ecdh-nist-p521 support in HPRE
v7->v8:
- patch #3 and #5: move the curve ID from the key into the algorithm name instead
v6->v7:
- patch #4: add function interface to expose elliptic curve parameters
- patch #4: eliminate warning by 'kernel test robot'
- patch #5: add function interface to expose curve25519 parameters
v5->v6:
- patch #1: add a new patch (the first patch), which is the "depend on" patch before
v4->v5:
- patch #4: delete P-128 and P-320 curve, as the few using case in the kernel
v3 -> v4:
- patch #3: add new, and move ecc_curve params to "include/crypto"
v2 -> v3:
- patch #5: fix sparse warnings
- patch #5: add 'CRYPTO_LIB_CURVE25519_GENERIC' in 'Kconfig'
v1 -> v2:
- patch #5: delete `curve25519_null_point'
Meng Yu (7):
crypto: hisilicon/hpre - add version adapt to new algorithms
crypto: hisilicon/hpre - add algorithm type
crypto: move curve_id of ECDH from the key to algorithm name
crypto: and expose ecc curves
crypto: hisilicon/hpre - add 'ECDH' algorithm
crypto: add curve25519 params and expose them
crypto: hisilicon/hpre - add 'CURVE25519' algorithm
crypto/ecc.c | 11 +- crypto/ecc.h | 37 +- crypto/ecc_curve_defs.h | 17 + crypto/ecdh.c | 72 ++- crypto/ecdh_helper.c | 4 +- crypto/testmgr.c | 13 +- crypto/testmgr.h | 34 +- drivers/crypto/atmel-ecc.c | 14 +- drivers/crypto/hisilicon/Kconfig | 1 + drivers/crypto/hisilicon/hpre/hpre.h | 17 +- drivers/crypto/hisilicon/hpre/hpre_crypto.c | 881 +++++++++++++++++++++++++++- drivers/crypto/hisilicon/hpre/hpre_main.c | 12 +- drivers/crypto/hisilicon/qm.c | 4 +- drivers/crypto/hisilicon/qm.h | 4 +- drivers/crypto/hisilicon/sec2/sec.h | 4 +- drivers/crypto/hisilicon/sec2/sec_crypto.c | 4 +- drivers/crypto/hisilicon/sec2/sec_crypto.h | 4 +- drivers/crypto/hisilicon/zip/zip.h | 4 +- drivers/crypto/hisilicon/zip/zip_crypto.c | 4 +- include/crypto/ecc_curve.h | 60 ++ include/crypto/ecdh.h | 2 - net/bluetooth/ecdh_helper.c | 2 - net/bluetooth/selftest.c | 2 +- net/bluetooth/smp.c | 6 +- 24 files changed, 1085 insertions(+), 128 deletions(-) create mode 100644 include/crypto/ecc_curve.h
Signature · 1 line
2.8.1
PATCH v9 1/7 crypto: hisilicon/hpre - add version adapt to new algorithms
A new generation of accelerator Kunpeng930 has appeared, and the corresponding driver needs to be updated to support some new algorithms of Kunpeng930. To be compatible with Kunpeng920, we add parameter 'struct hisi_qm *qm' to sec_algs_(un)register to identify the chip's version.
--- drivers/crypto/hisilicon/hpre/hpre.h | 5 +++-- drivers/crypto/hisilicon/hpre/hpre_crypto.c | 4 ++-- drivers/crypto/hisilicon/qm.c | 4 ++-- drivers/crypto/hisilicon/qm.h | 4 ++-- drivers/crypto/hisilicon/sec2/sec.h | 4 ++-- drivers/crypto/hisilicon/sec2/sec_crypto.c | 4 ++-- drivers/crypto/hisilicon/sec2/sec_crypto.h | 4 ++-- drivers/crypto/hisilicon/zip/zip.h | 4 ++-- drivers/crypto/hisilicon/zip/zip_crypto.c | 4 ++-- 9 files changed, 19 insertions(+), 18 deletions(-)
drivers/crypto/hisilicon/hpre/hpre.h +3 -2
@@ -93,7 +93,8 @@ struct hpre_sqe { };struct hisi_qp *hpre_create_qp (void ); -int hpre_algs_register (void ); -void hpre_algs_unregister (void ); +int hpre_algs_register (struct hisi_qm *qm); +void hpre_algs_unregister (struct hisi_qm *qm); +#endif
drivers/crypto/hisilicon/hpre/hpre_crypto.c +2 -2
@@ -1154,7 +1154,7 @@ static struct kpp_alg dh = { };#endif -int hpre_algs_register (void ) +int hpre_algs_register (struct hisi_qm *qm) {int ret;
@@ -1171,7 +1171,7 @@ int hpre_algs_register(void)return ret; } -void hpre_algs_unregister (void ) +void hpre_algs_unregister (struct hisi_qm *qm) {crypto_unregister_akcipher (&rsa);#ifdef CONFIG_CRYPTO_DH
drivers/crypto/hisilicon/qm.c +2 -2
@@ -4084,7 +4084,7 @@ int hisi_qm_alg_register(struct hisi_qm *qm, struct hisi_qm_list *qm_list)mutex_unlock (&qm_list->lock);if (flag) { - ret = qm_list->register_to_crypto (); + ret = qm_list->register_to_crypto (qm);if (ret) {mutex_lock (&qm_list->lock);list_del (&qm->list);
@@ -4115,7 +4115,7 @@ void hisi_qm_alg_unregister(struct hisi_qm *qm, struct hisi_qm_list *qm_list)mutex_unlock (&qm_list->lock);if (list_empty (&qm_list->list)) - qm_list->unregister_from_crypto (); + qm_list->unregister_from_crypto (qm); }EXPORT_SYMBOL_GPL (hisi_qm_alg_unregister);
drivers/crypto/hisilicon/qm.h +2 -2
@@ -199,8 +199,8 @@ struct hisi_qm_err_ini {struct hisi_qm_list {struct mutex lock;struct list_head list; -int (*register_to_crypto)(void ); -void (*unregister_from_crypto)(void ); +int (*register_to_crypto)(struct hisi_qm *qm); +void (*unregister_from_crypto)(struct hisi_qm *qm); };struct hisi_qm {
drivers/crypto/hisilicon/sec2/sec.h +2 -2
@@ -183,6 +183,6 @@ struct sec_dev {void sec_destroy_qps (struct hisi_qp **qps,int qp_num);struct hisi_qp **sec_create_qps (void ); -int sec_register_to_crypto (void ); -void sec_unregister_from_crypto (void ); +int sec_register_to_crypto (struct hisi_qm *qm); +void sec_unregister_from_crypto (struct hisi_qm *qm);#endif
drivers/crypto/hisilicon/sec2/sec_crypto.c +2 -2
@@ -1634,7 +1634,7 @@ static struct aead_alg sec_aeads[] = { AES_BLOCK_SIZE, AES_BLOCK_SIZE, SHA512_DIGEST_SIZE), }; -int sec_register_to_crypto (void ) +int sec_register_to_crypto (struct hisi_qm *qm) {int ret;
@@ -1651,7 +1651,7 @@ int sec_register_to_crypto(void)return ret; } -void sec_unregister_from_crypto (void ) +void sec_unregister_from_crypto (struct hisi_qm *qm) {crypto_unregister_skciphers (sec_skciphers,ARRAY_SIZE (sec_skciphers));
drivers/crypto/hisilicon/sec2/sec_crypto.h +2 -2
@@ -211,6 +211,6 @@ struct sec_sqe { struct sec_sqe_type2 type2; }; -int sec_register_to_crypto (void ); -void sec_unregister_from_crypto (void ); +int sec_register_to_crypto (struct hisi_qm *qm); +void sec_unregister_from_crypto (struct hisi_qm *qm);#endif
drivers/crypto/hisilicon/zip/zip.h +2 -2
@@ -62,6 +62,6 @@ struct hisi_zip_sqe { };int zip_create_qps (struct hisi_qp **qps,int ctx_num,int node); -int hisi_zip_register_to_crypto (void ); -void hisi_zip_unregister_from_crypto (void ); +int hisi_zip_register_to_crypto (struct hisi_qm *qm); +void hisi_zip_unregister_from_crypto (struct hisi_qm *qm);#endif
drivers/crypto/hisilicon/zip/zip_crypto.c +2 -2
@@ -665,7 +665,7 @@ static struct acomp_alg hisi_zip_acomp_gzip = { } }; -int hisi_zip_register_to_crypto (void ) +int hisi_zip_register_to_crypto (struct hisi_qm *qm) {int ret;
@@ -684,7 +684,7 @@ int hisi_zip_register_to_crypto(void)return ret; } -void hisi_zip_unregister_from_crypto (void ) +void hisi_zip_unregister_from_crypto (struct hisi_qm *qm) {crypto_unregister_acomp (&hisi_zip_acomp_gzip);crypto_unregister_acomp (&hisi_zip_acomp_zlib);
Signature · 1 line
2.8.1
PATCH v9 2/7 crypto: hisilicon/hpre - add algorithm type
In reply to Meng YuAlgorithm type is brought in to get hardware HPRE queue to support different algorithms.
--- drivers/crypto/hisilicon/hpre/hpre.h | 10 +++++++++- drivers/crypto/hisilicon/hpre/hpre_crypto.c | 12 ++++++------ drivers/crypto/hisilicon/hpre/hpre_main.c | 11 +++++++++-- 3 files changed, 24 insertions(+), 9 deletions(-)
drivers/crypto/hisilicon/hpre/hpre.h +9 -1
@@ -10,6 +10,14 @@#define HPRE_PF_DEF_Q_NUM 64#define HPRE_PF_DEF_Q_BASE 0 +/* +* type used in qm sqc DW6. +* 0 - Algorithm which has been supported in V2, like RSA, DH and so on; +* 1 - ECC algorithm in V3. +*/ +#define HPRE_V2_ALG_TYPE 0 +#define HPRE_V3_ECC_ALG_TYPE 1 +enum {HPRE_CLUSTER0 ,HPRE_CLUSTER1 ,
@@ -92,7 +100,7 @@ struct hpre_sqe {__le32 rsvd1[_HPRE_SQE_ALIGN_EXT]; }; -struct hisi_qp *hpre_create_qp (void ); +struct hisi_qp *hpre_create_qp (u8 type);int hpre_algs_register (struct hisi_qm *qm);void hpre_algs_unregister (struct hisi_qm *qm);
drivers/crypto/hisilicon/hpre/hpre_crypto.c +6 -6
@@ -152,12 +152,12 @@ static void hpre_rm_req_from_ctx(struct hpre_asym_request *hpre_req) } } -static struct hisi_qp *hpre_get_qp_and_start (void ) +static struct hisi_qp *hpre_get_qp_and_start (u8 type) {struct hisi_qp *qp;int ret; - qp =hpre_create_qp (); + qp =hpre_create_qp (type);if (!qp) {pr_err ("Can not create hpre qp!\n" );return ERR_PTR (-ENODEV );
@@ -422,11 +422,11 @@ static void hpre_alg_cb(struct hisi_qp *qp, void *resp) req->cb (ctx, resp); } -static int hpre_ctx_init (struct hpre_ctx *ctx) +static int hpre_ctx_init (struct hpre_ctx *ctx,u8 type) {struct hisi_qp *qp; - qp =hpre_get_qp_and_start (); + qp =hpre_get_qp_and_start (type);if (IS_ERR (qp))return PTR_ERR (qp);
@@ -674,7 +674,7 @@ static int hpre_dh_init_tfm(struct crypto_kpp *tfm) {struct hpre_ctx *ctx =kpp_tfm_ctx (tfm); -return hpre_ctx_init (ctx); +return hpre_ctx_init (ctx,HPRE_V2_ALG_TYPE ); }static void hpre_dh_exit_tfm (struct crypto_kpp *tfm)
@@ -1100,7 +1100,7 @@ static int hpre_rsa_init_tfm(struct crypto_akcipher *tfm)return PTR_ERR (ctx->rsa.soft_tfm); } - ret =hpre_ctx_init (ctx); + ret =hpre_ctx_init (ctx,HPRE_V2_ALG_TYPE );if (ret)crypto_free_akcipher (ctx->rsa.soft_tfm);
drivers/crypto/hisilicon/hpre/hpre_main.c +9 -2
@@ -226,13 +226,20 @@ static u32 vfs_num;module_param_cb (vfs_num, &vfs_num_ops, &vfs_num,0444 );MODULE_PARM_DESC (vfs_num,"Number of VFs to enable(1-63), 0(default)" ); -struct hisi_qp *hpre_create_qp (void ) +struct hisi_qp *hpre_create_qp (u8 type) {int node =cpu_to_node (smp_processor_id ());struct hisi_qp *qp =NULL ;int ret; - ret =hisi_qm_alloc_qps_node (&hpre_devices,1 ,0 , node, &qp); +if (type !=HPRE_V2_ALG_TYPE && type !=HPRE_V3_ECC_ALG_TYPE ) +return NULL ; + +/* +* type: 0 - RSA/DH. algorithm supported in V2, +* 1 - ECC algorithm in V3. +*/ + ret =hisi_qm_alloc_qps_node (&hpre_devices,1 , type, node, &qp);if (!ret)return qp;
Signature · 1 line
2.8.1
PATCH v9 3/7 crypto: move curve_id of ECDH from the key to algorithm name
In reply to Meng Yu1. crypto and crypto/atmel-ecc:
Move curve id of ECDH from the key into the algorithm name instead in crypto and atmel-ecc, so ECDH algorithm name change form 'ecdh' to 'ecdh-nist-pxxx', and we cannot use 'curve_id' in 'struct ecdh';
2. crypto/testmgr and net/bluetooth:
Modify 'testmgr.c', 'testmgr.h' and 'net/bluetooth' to adapt the modification.
--- crypto/ecdh.c | 72 +++++++++++++++++++++++++++++++-------------- crypto/ecdh_helper.c | 4 +-- crypto/testmgr.c | 13 ++++++-- crypto/testmgr.h | 34 ++++++++++----------- drivers/crypto/atmel-ecc.c | 14 ++++----- include/crypto/ecdh.h | 2 -- net/bluetooth/ecdh_helper.c | 2 -- net/bluetooth/selftest.c | 2 +- net/bluetooth/smp.c | 6 ++-- 9 files changed, 88 insertions(+), 61 deletions(-)
crypto/ecdh.c +50 -22
@@ -23,33 +23,16 @@ static inline struct ecdh_ctx *ecdh_get_ctx(struct crypto_kpp *tfm)return kpp_tfm_ctx (tfm); } -static unsigned int ecdh_supported_curve (unsigned int curve_id) -{ -switch (curve_id) { -case ECC_CURVE_NIST_P192 :return ECC_CURVE_NIST_P192_DIGITS ; -case ECC_CURVE_NIST_P256 :return ECC_CURVE_NIST_P256_DIGITS ; -default :return 0 ; - } -} -static int ecdh_set_secret (struct crypto_kpp *tfm,const void *buf,unsigned int len) {struct ecdh_ctx *ctx =ecdh_get_ctx (tfm);struct ecdh params; -unsigned int ndigits;if (crypto_ecdh_decode_key (buf, len, ¶ms) <0 || - params.key_size >sizeof (ctx->private_key)) + params.key_size >sizeof (u64) * ctx->ndigits)return -EINVAL ; - ndigits =ecdh_supported_curve (params.curve_id); -if (!ndigits) -return -EINVAL ; - - ctx->curve_id = params.curve_id; - ctx->ndigits = ndigits; -if (!params.key || !params.key_size)return ecc_gen_privkey (ctx->curve_id, ctx->ndigits, ctx->private_key);
@@ -140,13 +123,24 @@ static unsigned int ecdh_max_size(struct crypto_kpp *tfm)return ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT +1 ); } -static struct kpp_alg ecdh = { +static int ecdh_nist_p192_init_tfm (struct crypto_kpp *tfm) +{ +struct ecdh_ctx *ctx =ecdh_get_ctx (tfm); + + ctx->curve_id =ECC_CURVE_NIST_P192 ; + ctx->ndigits =ECC_CURVE_NIST_P192_DIGITS ; + +return 0 ; +} + +static struct kpp_alg ecdh_nist_p192 = { .set_secret = ecdh_set_secret, .generate_public_key = ecdh_compute_value, .compute_shared_secret = ecdh_compute_value, .max_size = ecdh_max_size, + .init = ecdh_nist_p192_init_tfm, .base = { - .cra_name ="ecdh" , + .cra_name ="ecdh-nist-p192" , .cra_driver_name ="ecdh-generic" , .cra_priority =100 , .cra_module =THIS_MODULE ,
@@ -154,14 +148,48 @@ static struct kpp_alg ecdh = { }, }; +static int ecdh_nist_p256_init_tfm (struct crypto_kpp *tfm) +{ +struct ecdh_ctx *ctx =ecdh_get_ctx (tfm); + + ctx->curve_id =ECC_CURVE_NIST_P256 ; + ctx->ndigits =ECC_CURVE_NIST_P256_DIGITS ; + +return 0 ; +} + +static struct kpp_alg ecdh_nist_p256 = { + .set_secret = ecdh_set_secret, + .generate_public_key = ecdh_compute_value, + .compute_shared_secret = ecdh_compute_value, + .max_size = ecdh_max_size, + .init = ecdh_nist_p256_init_tfm, + .base = { + .cra_name ="ecdh-nist-p256" , + .cra_driver_name ="ecdh-generic" , + .cra_priority =100 , + .cra_module =THIS_MODULE , + .cra_ctxsize =sizeof (struct ecdh_ctx ), + }, +}; + +static bool ecdh_nist_p192_registered; +static int ecdh_init (void ) { -return crypto_register_kpp (&ecdh); +int ret; + + ret =crypto_register_kpp (&ecdh_nist_p192); + ecdh_nist_p192_registered = ret ==0 ; + +return crypto_register_kpp (&ecdh_nist_p256); }static void ecdh_exit (void ) { -crypto_unregister_kpp (&ecdh); +if (ecdh_nist_p192_registered) +crypto_unregister_kpp (&ecdh_nist_p192); +crypto_unregister_kpp (&ecdh_nist_p256); }subsys_initcall (ecdh_init);
crypto/ecdh_helper.c +1 -3
@@ -10,7 +10,7 @@#include <crypto/ecdh.h> #include <crypto/kpp.h> -#define ECDH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 2 * sizeof(short)) +#define ECDH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + sizeof(short))static inline u8 *ecdh_pack_data (void *dst,const void *src,size_t sz) {
@@ -46,7 +46,6 @@ int crypto_ecdh_encode_key(char *buf, unsigned int len,return -EINVAL ; ptr =ecdh_pack_data (ptr, &secret,sizeof (secret)); - ptr =ecdh_pack_data (ptr, ¶ms->curve_id,sizeof (params->curve_id)); ptr =ecdh_pack_data (ptr, ¶ms->key_size,sizeof (params->key_size));ecdh_pack_data (ptr, params->key, params->key_size);
@@ -70,7 +69,6 @@ int crypto_ecdh_decode_key(const char *buf, unsigned int len,if (unlikely (len < secret.len))return -EINVAL ; - ptr =ecdh_unpack_data (¶ms->curve_id, ptr,sizeof (params->curve_id)); ptr =ecdh_unpack_data (¶ms->key_size, ptr,sizeof (params->key_size));if (secret.len !=crypto_ecdh_key_len (params))return -EINVAL ;
crypto/testmgr.c +11 -2
@@ -4904,11 +4904,20 @@ static const struct alg_test_desc alg_test_descs[] = { } }, {#endif - .alg = "ecdh", +#ifndef CONFIG_CRYPTO_FIPS + .alg = "ecdh-nist-p192", .test = alg_test_kpp, .fips_allowed = 1, .suite = { - .kpp = __VECS(ecdh_tv_template) + .kpp = __VECS(ecdh_p192_tv_template) + } + }, { +#endif + .alg = "ecdh-nist-p256", + .test = alg_test_kpp, + .fips_allowed = 1, + .suite = { + .kpp = __VECS(ecdh_p256_tv_template) } }, { .alg = "ecrdsa",
crypto/testmgr.h +15 -19
@@ -2261,19 +2261,17 @@ static const struct kpp_testvec curve25519_tv_template[] = { } }; -static const struct kpp_testvec ecdh_tv_template[] = { - { #ifndef CONFIG_CRYPTO_FIPS +static const struct kpp_testvec ecdh_p192_tv_template[] = { + { .secret = #ifdef __LITTLE_ENDIAN "\x02\x00" /* type */ - "\x20\x00" /* len */ - "\x01\x00" /* curve_id */ + "\x1e\x00" /* len */ "\x18\x00" /* key_size */ #else "\x00\x02" /* type */ - "\x00\x20" /* len */ - "\x00\x01" /* curve_id */ + "\x00\x1e" /* len */ "\x00\x18" /* key_size */ #endif "\xb5\x05\xb1\x71\x1e\xbf\x8c\xda"
@@ -2301,18 +2299,20 @@ static const struct kpp_testvec ecdh_tv_template[] = { .b_public_size = 48, .expected_a_public_size = 48, .expected_ss_size =24 - }, { + } +};#endif + +static const struct kpp_testvec ecdh_p256_tv_template[] = { + { .secret = #ifdef __LITTLE_ENDIAN "\x02\x00" /* type */ -"\x28\x00" /* len */ -"\x02\x00" /* curve_id */ + "\x26\x00" /* len */ "\x20\x00" /* key_size */ #else "\x00\x02" /* type */ -"\x00\x28" /* len */ -"\x00\x02" /* curve_id */ + "\x00\x26" /* len */ "\x00\x20" /* key_size */ #endif"\x24\xd1\x21\xeb\xe5\xcf\x2d\x83"
@@ -2350,25 +2350,21 @@ static const struct kpp_testvec ecdh_tv_template[] = { .secret =#ifdef __LITTLE_ENDIAN"\x02\x00" /* type */ -"\x08\x00" /* len */ -"\x02\x00" /* curve_id */ +"\x06\x00" /* len */ "\x00\x00" ,/* key_size */ #else"\x00\x02" /* type */ -"\x00\x08" /* len */ -"\x00\x02" /* curve_id */ +"\x00\x06" /* len */ "\x00\x00" ,/* key_size */ #endif .b_secret = #ifdef __LITTLE_ENDIAN "\x02\x00" /* type */ - "\x28\x00" /* len */ - "\x02\x00" /* curve_id */ + "\x26\x00" /* len */ "\x20\x00" /* key_size */ #else "\x00\x02" /* type */ - "\x00\x28" /* len */ - "\x00\x02" /* curve_id */ + "\x00\x26" /* len */ "\x00\x20" /* key_size */ #endif"\x24\xd1\x21\xeb\xe5\xcf\x2d\x83"
drivers/crypto/atmel-ecc.c +7 -7
@@ -104,7 +104,7 @@ static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,return -EINVAL ; } - ctx->n_sz =atmel_ecdh_supported_curve (params.curve_id); + ctx->n_sz =atmel_ecdh_supported_curve (ctx->curve_id);if (!ctx->n_sz || params.key_size) {/* fallback to ecdh software implementation */ ctx->do_fallback = true;
@@ -125,7 +125,6 @@ static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, goto free_cmd; ctx->do_fallback = false; - ctx->curve_id = params.curve_id;atmel_i2c_init_genkey_cmd (cmd,DATA_SLOT_2 );
@@ -263,6 +262,7 @@ static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)struct crypto_kpp *fallback;struct atmel_ecdh_ctx *ctx =kpp_tfm_ctx (tfm); + ctx->curve_id =ECC_CURVE_NIST_P256 ; ctx->client =atmel_ecc_i2c_client_alloc ();if (IS_ERR (ctx->client)) {pr_err ("tfm - i2c_client binding failed\n" );
@@ -306,7 +306,7 @@ static unsigned int atmel_ecdh_max_size(struct crypto_kpp *tfm)return ATMEL_ECC_PUBKEY_SIZE ; } -static struct kpp_alg atmel_ecdh = { +static struct kpp_alg atmel_ecdh_nist_p256 = { .set_secret = atmel_ecdh_set_secret, .generate_public_key = atmel_ecdh_generate_public_key, .compute_shared_secret = atmel_ecdh_compute_shared_secret,
@@ -315,7 +315,7 @@ static struct kpp_alg atmel_ecdh = { .max_size = atmel_ecdh_max_size, .base = { .cra_flags = CRYPTO_ALG_NEED_FALLBACK, - .cra_name = "ecdh", + .cra_name = "ecdh-nist-p256", .cra_driver_name = "atmel-ecdh", .cra_priority = ATMEL_ECC_PRIORITY, .cra_module = THIS_MODULE,
@@ -340,14 +340,14 @@ static int atmel_ecc_probe(struct i2c_client *client, &driver_data.i2c_client_list);spin_unlock (&driver_data.i2c_list_lock); - ret =crypto_register_kpp (&atmel_ecdh); + ret =crypto_register_kpp (&atmel_ecdh_nist_p256);if (ret) {spin_lock (&driver_data.i2c_list_lock);list_del (&i2c_priv->i2c_client_list_node);spin_unlock (&driver_data.i2c_list_lock);dev_err (&client->dev,"%s alg registration failed\n" , - atmel_ecdh.base.cra_driver_name); + atmel_ecdh_nist_p256.base.cra_driver_name); }else {dev_info (&client->dev,"atmel ecc algorithms registered in /proc/crypto\n" ); }
@@ -365,7 +365,7 @@ static int atmel_ecc_remove(struct i2c_client *client)return -EBUSY ; } -crypto_unregister_kpp (&atmel_ecdh); +crypto_unregister_kpp (&atmel_ecdh_nist_p256);spin_lock (&driver_data.i2c_list_lock);list_del (&i2c_priv->i2c_client_list_node);
include/crypto/ecdh.h +0 -2
@@ -29,12 +29,10 @@/** * struct ecdh - define an ECDH private key * -* @curve_id: ECC curve the key is based on. * @key: Private ECDH key * @key_size: Size of the private ECDH key */ struct ecdh { -unsigned short curve_id;char *key;unsigned short key_size; };
net/bluetooth/ecdh_helper.c +0 -2
@@ -126,8 +126,6 @@ int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])int err;struct ecdh p = {0 }; - p.curve_id =ECC_CURVE_NIST_P256 ; -if (private_key) { tmp =kmalloc (32 ,GFP_KERNEL );if (!tmp)
net/bluetooth/selftest.c +1 -1
@@ -205,7 +205,7 @@ static int __init test_ecdh(void) calltime =ktime_get (); - tfm =crypto_alloc_kpp ("ecdh" ,0 ,0 ); + tfm =crypto_alloc_kpp ("ecdh-nist-p256" ,0 ,0 );if (IS_ERR (tfm)) {BT_ERR ("Unable to create ECDH crypto context" ); err =PTR_ERR (tfm);
net/bluetooth/smp.c +3 -3
@@ -1387,7 +1387,7 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) goto zfree_smp; } - smp->tfm_ecdh =crypto_alloc_kpp ("ecdh" ,0 ,0 ); + smp->tfm_ecdh =crypto_alloc_kpp ("ecdh-nist-p256" ,0 ,0 );if (IS_ERR (smp->tfm_ecdh)) {BT_ERR ("Unable to create ECDH crypto context" ); goto free_shash;
@@ -3282,7 +3282,7 @@ static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)return ERR_CAST (tfm_cmac); } - tfm_ecdh =crypto_alloc_kpp ("ecdh" ,0 ,0 ); + tfm_ecdh =crypto_alloc_kpp ("ecdh-nist-p256" ,0 ,0 );if (IS_ERR (tfm_ecdh)) {BT_ERR ("Unable to create ECDH crypto context" );crypto_free_shash (tfm_cmac);
@@ -3807,7 +3807,7 @@ int __init bt_selftest_smp(void)return PTR_ERR (tfm_cmac); } - tfm_ecdh =crypto_alloc_kpp ("ecdh" ,0 ,0 ); + tfm_ecdh =crypto_alloc_kpp ("ecdh-nist-p256" ,0 ,0 );if (IS_ERR (tfm_ecdh)) {BT_ERR ("Unable to create ECDH crypto context" );crypto_free_shash (tfm_cmac);
Signature · 1 line
2.8.1
Re: [PATCH v9 3/7] crypto: move curve_id of ECDH from the key to algorithm name
Hi,
drivers/crypto/atmel-ecc.c static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, · line 110
Quoted 11 lines · Meng Yu
drivers/crypto/atmel-ecc.c +1
-1@@ -104,7 +104,7 @@ static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,return -EINVAL ; } - ctx->n_sz =atmel_ecdh_supported_curve (params.curve_id); + ctx->n_sz =atmel_ecdh_supported_curve (ctx->curve_id);if (!ctx->n_sz || params.key_size) {/* fallback to ecdh software implementation */ ctx->do_fallback = true;
Now that you moved the curve id info into the alg name, and it is no longer dynamically discovered when decoding the key, does it still make sense to keep the curve id, the key size checks, and the fallback to the software implementation?
I don't have an atecc508 at hand to test the changes, but I expect your changes won't affect the functionality.
ta
Re: [PATCH v9 3/7] crypto: move curve_id of ECDH from the key to algorithm name
Quoted 9 lines · 在 2021/2/23 18:44, [email protected] 写道:
Hi,
Quoted 6 lines · Meng Yu
drivers/crypto/atmel-ecc.c +0
-0@@ -104,7 +104,7 @@ static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,return -EINVAL ; }
Quoted 11 lines
drivers/crypto/atmel-ecc.c static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, · line 110
Quoted 5 lines
drivers/crypto/atmel-ecc.c +1
-1- ctx->n_sz =atmel_ecdh_supported_curve (params.curve_id); + ctx->n_sz =atmel_ecdh_supported_curve (ctx->curve_id);if (!ctx->n_sz || params.key_size) {/* fallback to ecdh software implementation */ ctx->do_fallback = true;Now that you moved the curve id info into the alg name, and it is no longer dynamically discovered when decoding the key, does it still make sense to keep the curve id, the key size checks, and the fallback to the software implementation?
Yes, I think we can keep this code if 'atmel-ecc' may support
new other curves at future, and if you're sure P256 is the only curve 'atmel-ecc' uses, and it will never be changed, we can delete it.
Quoted 3 lines
I don't have an atecc508 at hand to test the changes, but I expect your changes won't affect the functionality.
OK, when you or your team members have an atecc508, please help test.
Re: [PATCH v9 3/7] crypto: move curve_id of ECDH from the key to algorithm name
In reply to [email protected]Quoted 9 lines · 在 2021/2/23 18:44, [email protected] 写道:
Hi,
Quoted 6 lines · Meng Yu
drivers/crypto/atmel-ecc.c +0
-0@@ -104,7 +104,7 @@ static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,return -EINVAL ; }
Quoted 11 lines
drivers/crypto/atmel-ecc.c static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, · line 110
Quoted 5 lines
drivers/crypto/atmel-ecc.c +1
-1- ctx->n_sz =atmel_ecdh_supported_curve (params.curve_id); + ctx->n_sz =atmel_ecdh_supported_curve (ctx->curve_id);if (!ctx->n_sz || params.key_size) {/* fallback to ecdh software implementation */ ctx->do_fallback = true;Now that you moved the curve id info into the alg name, and it is no longer dynamically discovered when decoding the key, does it still make sense to keep the curve id, the key size checks, and the fallback to the software implementation?
I think we can keep the curve id, the key size check if 'atmel-ecc' may
support other curves in the future, and if you're sure P256 is the only curve 'atmel-ecc' uses, and it will be changed, we can delete it.
And fallback to ecdh software implementation is needed when params.key_size is zero.
Quoted 3 lines
I don't have an atecc508 at hand to test the changes, but I expect your changes won't affect the functionality.
OK, if you or your team members have an atecc508, please help test.
Re: [PATCH v9 3/7] crypto: move curve_id of ECDH from the key to algorithm name
Quoted 23 lines · yumeng
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 9 lines · 在 2021/2/23 18:44, [email protected] 写道:
Hi,
Quoted 6 lines · Meng Yu
drivers/crypto/atmel-ecc.c +0
-0@@ -104,7 +104,7 @@ static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, return -EINVAL; }Quoted 11 lines
drivers/crypto/atmel-ecc.c static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, · line 110
Quoted 5 lines
drivers/crypto/atmel-ecc.c +1
-1- ctx->n_sz = atmel_ecdh_supported_curve(params.curve_id); + ctx->n_sz = atmel_ecdh_supported_curve(ctx->curve_id); if (!ctx->n_sz || params.key_size) { /* fallback to ecdh software implementation */ ctx->do_fallback = true;Now that you moved the curve id info into the alg name, and it is no longer dynamically discovered when decoding the key, does it still make sense to keep the curve id, the key size checks, and the fallback to the software implementation?
I think we can keep the curve id, the key size check if 'atmel-ecc' may
Quoted 6 lines
support other curves in the future, and if you're sure P256 is the only curve 'atmel-ecc' uses, and it will be changed, we can delete it.
And fallback to ecdh software implementation is needed when params.key_size is zero.
I've read the code again, now I remember. The fallback is needed indeed, but for other reason. ecdh-nist-p256 will be handled in the crypto IP only when its user provides a zero length private key: we will use a pre-provisioned private key that can't be read outside of the device. The fallback was needed for ecdh-nist-p256 when the user provides a non-zero private key, or for other curve IDs.
Since the atecc508 and atecc608 (for which there isn't support in kernel as of now) both support just NIST Standard P256 Elliptic Curve, and the curve id is now unique per alg, the ctx->curve_id handling does no longer make sense. So please remove the ctx->curve_id handling. ctx->n_sz can be removed too and use instead directly ATMEL_ECC_NIST_P256_N_SIZE, similar to how ATMEL_ECC_PUBKEY_SIZE is used.
Re: [PATCH v9 3/7] crypto: move curve_id of ECDH from the key to algorithm name
Quoted 38 lines · 在 2021/2/24 18:15, [email protected] 写道:
Quoted 23 lines · yumeng
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 9 lines · 在 2021/2/23 18:44, [email protected] 写道:
Hi,
Quoted 6 lines · Meng Yu
drivers/crypto/atmel-ecc.c +0
-0@@ -104,7 +104,7 @@ static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, return -EINVAL; }Quoted 11 lines
drivers/crypto/atmel-ecc.c static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, · line 110
Quoted 5 lines
drivers/crypto/atmel-ecc.c +1
-1- ctx->n_sz = atmel_ecdh_supported_curve(params.curve_id); + ctx->n_sz = atmel_ecdh_supported_curve(ctx->curve_id); if (!ctx->n_sz || params.key_size) { /* fallback to ecdh software implementation */ ctx->do_fallback = true;Now that you moved the curve id info into the alg name, and it is no longer dynamically discovered when decoding the key, does it still make sense to keep the curve id, the key size checks, and the fallback to the software implementation?
I think we can keep the curve id, the key size check if 'atmel-ecc' mayQuoted 6 lines
support other curves in the future, and if you're sure P256 is the only curve 'atmel-ecc' uses, and it will be changed, we can delete it.
And fallback to ecdh software implementation is needed when params.key_size is zero.
I've read the code again, now I remember. The fallback is needed indeed, but for other reason. ecdh-nist-p256 will be handled in the crypto IP only when its user provides a zero length private key: we will use a pre-provisioned private key that can't be read outside of the device. The fallback was needed for ecdh-nist-p256 when the user provides a non-zero private key, or for other curve IDs.
Quoted 7 lines
Since the atecc508 and atecc608 (for which there isn't support in kernel as of now) both support just NIST Standard P256 Elliptic Curve, and the curve id is now unique per alg, the
ctx->curve_idhandling does no longer make sense. So please remove thectx->curve_idhandling.ctx->n_szcan be removed too and use instead directlyATMEL_ECC_NIST_P256_N_SIZE, similar to howATMEL_ECC_PUBKEY_SIZEis used.
OK, thanks, I will remove it. But I'd like to wait 2 more days, as maybe the owners of other modules (like crypto or bluetooth) have any comments.
thanks.
PATCH v9 4/7 crypto: and expose ecc curves
In reply to Meng YuMove 'ecc_get_curve' to 'include/crypto/ecc_curve.h', so everyone in kernel tree can easily get ecc curve params;
--- crypto/ecc.c | 5 ++++- crypto/ecc.h | 37 ++------------------------------ include/crypto/ecc_curve.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 include/crypto/ecc_curve.h
crypto/ecc.c +4 -1
@@ -24,6 +24,7 @@* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <crypto/ecc_curve.h> #include <linux/module.h> #include <linux/random.h> #include <linux/slab.h>
@@ -42,7 +43,8 @@ typedef struct {u64 m_high; } uint128_t; -static inline const struct ecc_curve *ecc_get_curve (unsigned int curve_id) + +const struct ecc_curve *ecc_get_curve (unsigned int curve_id) {switch (curve_id) {/* In FIPS mode only allow P256 and higher */
@@ -54,6 +56,7 @@ static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)return NULL ; } } +EXPORT_SYMBOL (ecc_get_curve);static u64 *ecc_alloc_digits_space (unsigned int ndigits) {
crypto/ecc.h +2 -35
@@ -26,6 +26,8 @@#ifndef _CRYPTO_ECC_H#define _CRYPTO_ECC_H +#include <crypto/ecc_curve.h> +/* One digit is u64 qword. */ #define ECC_CURVE_NIST_P192_DIGITS 3#define ECC_CURVE_NIST_P256_DIGITS 4
@@ -33,44 +35,9 @@#define ECC_DIGITS_TO_BYTES_SHIFT 3 -/** -* struct ecc_point - elliptic curve point in affine coordinates -* -* @x: X coordinate in vli form. -* @y: Y coordinate in vli form. -* @ndigits: Length of vlis in u64 qwords. -*/ -struct ecc_point { -u64 *x; -u64 *y; -u8 ndigits; -}; -#define ECC_POINT_INIT (x, y, ndigits) (struct ecc_point) { x, y, ndigits }/** -* struct ecc_curve - definition of elliptic curve -* -* @name: Short name of the curve. -* @g: Generator point of the curve. -* @p: Prime number, if Barrett's reduction is used for this curve -* pre-calculated value 'mu' is appended to the @p after ndigits. -* Use of Barrett's reduction is heuristically determined in -* vli_mmod_fast(). -* @n: Order of the curve group. -* @a: Curve parameter a. -* @b: Curve parameter b. -*/ -struct ecc_curve { -char *name; -struct ecc_point g; -u64 *p; -u64 *n; -u64 *a; -u64 *b; -}; - -/** * ecc_is_key_valid() - Validate a given ECDH private key * * @curve_id: id representing the curve to use
include/crypto/ecc_curve.h new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
2.8.1
PATCH v9 5/7 crypto: hisilicon/hpre - add 'ECDH' algorithm
In reply to Meng Yu1. Enable 'ECDH' algorithm in Kunpeng 930;
2. HPRE ECDH Support: ecdh-nist-p192, ecdh-nist-p256.
--- drivers/crypto/hisilicon/hpre/hpre.h | 2 +- drivers/crypto/hisilicon/hpre/hpre_crypto.c | 515 +++++++++++++++++++++++++++- drivers/crypto/hisilicon/hpre/hpre_main.c | 1 + 3 files changed, 513 insertions(+), 5 deletions(-)
drivers/crypto/hisilicon/hpre/hpre.h +1 -1
@@ -83,6 +83,7 @@ enum hpre_alg_type {HPRE_ALG_KG_CRT =0x3 ,HPRE_ALG_DH_G2 =0x4 ,HPRE_ALG_DH =0x5 , + HPRE_ALG_ECC_MUL = 0xD, }; struct hpre_sqe {
@@ -104,5 +105,4 @@ struct hisi_qp *hpre_create_qp(u8 type);int hpre_algs_register (struct hisi_qm *qm);void hpre_algs_unregister (struct hisi_qm *qm); -#endif
drivers/crypto/hisilicon/hpre/hpre_crypto.c +511 -4
579 lines not on this page · raw message
drivers/crypto/hisilicon/hpre/hpre_main.c +1 -0
@@ -1082,4 +1082,5 @@ module_exit(hpre_exit);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Zaibo Xu <[email protected]>" ); +MODULE_AUTHOR ("Meng Yu <[email protected]>" );MODULE_DESCRIPTION ("Driver for HiSilicon HPRE accelerator" );
Signature · 1 line
2.8.1
PATCH v9 6/7 crypto: add curve25519 params and expose them
In reply to Meng Yu1. Add curve 25519 parameters in 'crypto/ecc_curve_defs.h';
2. Add curve25519 interface 'ecc_get_curve25519_param' in 'include/crypto/ecc_curve.h', to make its parameters be exposed to everyone in kernel tree.
--- crypto/ecc.c | 6 ++++++ crypto/ecc_curve_defs.h | 17 +++++++++++++++++ include/crypto/ecc_curve.h | 7 +++++++ 3 files changed, 30 insertions(+)
crypto/ecc.c +6 -0
@@ -43,6 +43,12 @@ typedef struct {u64 m_high; } uint128_t; +/* Returns curv25519 curve param */ +const struct ecc_curve *ecc_get_curve25519 (void ) +{ +return &ecc_25519; +} +EXPORT_SYMBOL (ecc_get_curve25519);const struct ecc_curve *ecc_get_curve (unsigned int curve_id) {
crypto/ecc_curve_defs.h +17 -0
@@ -54,4 +54,21 @@ static struct ecc_curve nist_p256 = { .b = nist_p256_b }; +/* curve25519 */ +static u64 curve25519_g_x[] = {0x0000000000000009 ,0x0000000000000000 , +0x0000000000000000 ,0x0000000000000000 }; +static u64 curve25519_p[] = {0xffffffffffffffed ,0xffffffffffffffff , +0xffffffffffffffff ,0x7fffffffffffffff }; +static u64 curve25519_a[] = {0x000000000001DB41 ,0x0000000000000000 , +0x0000000000000000 ,0x0000000000000000 }; +static const struct ecc_curve ecc_25519 = { + .name ="curve25519" , + .g = { + .x = curve25519_g_x, + .ndigits =4 , + }, + .p = curve25519_p, + .a = curve25519_a, +}; +#endif
include/crypto/ecc_curve.h +7 -0
@@ -50,4 +50,11 @@ struct ecc_curve {*/ const struct ecc_curve *ecc_get_curve (unsigned int curve_id); +/** +* ecc_get_curve25519() - get curve25519 curve; +* +* Returns curve25519 +*/ +const struct ecc_curve *ecc_get_curve25519 (void ); +#endif
Signature · 1 line
2.8.1
PATCH v9 7/7 crypto: hisilicon/hpre - add 'CURVE25519' algorithm
In reply to Meng YuEnable 'CURVE25519' algorithm in Kunpeng 930.
--- drivers/crypto/hisilicon/Kconfig | 1 + drivers/crypto/hisilicon/hpre/hpre.h | 2 + drivers/crypto/hisilicon/hpre/hpre_crypto.c | 366 +++++++++++++++++++++++++++- 3 files changed, 361 insertions(+), 8 deletions(-)
drivers/crypto/hisilicon/Kconfig +1 -0
@@ -65,6 +65,7 @@ config CRYPTO_DEV_HISI_HPRE depends on UACCE || UACCE=n depends on ARM64 || (COMPILE_TEST && 64BIT) depends on ACPI + select CRYPTO_LIB_CURVE25519_GENERIC select CRYPTO_DEV_HISI_QM select CRYPTO_DH select CRYPTO_RSA
drivers/crypto/hisilicon/hpre/hpre.h +2 -0
@@ -84,6 +84,8 @@ enum hpre_alg_type {HPRE_ALG_DH_G2 =0x4 ,HPRE_ALG_DH =0x5 ,HPRE_ALG_ECC_MUL =0xD , +/* shared by x25519 and x448, but x448 is not supported now */ + HPRE_ALG_CURVE25519_MUL = 0x10, }; struct hpre_sqe {
drivers/crypto/hisilicon/hpre/hpre_crypto.c +358 -8
429 lines not on this page · raw message
Signature · 1 line
2.8.1