Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

driver: crypto: hisilicon: add ECC gen_keypair and ECDH #6807

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yuzexiyzx
Copy link
Contributor

add ECC gen_keypair and shared secret

};

/* NID_X9_62_prime192v1 */
static uint8_t g_prime192v1_p[] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static const?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
cannot add const

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to change struct hpre_ecc_curve to use const pointers instead or is that struct sometimes used for output too?

It would be advantageous to make this data const since it will be protected from accidental overwrite.

return false;
}

EMSG("Error: %s all zero", p_name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like all these EMSG() should be DMSG() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is error msg, and we use EMSG() in other files such as dh.c

struct hpre_ecc_msg *msg = (struct hpre_ecc_msg *)info;
struct hpre_sqe *sqe = (struct hpre_sqe *)bd;

sqe->w0 = msg->alg_type | (0x1 << HPRE_DONE_SHIFT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use SHIFT_U32(0x1, HPRE_DONE_SHIFT)


if (msg->alg_type == HPRE_ALG_SM2_SIGN ||
msg->alg_type == HPRE_ALG_SM2_ENC)
sqe->ext1 |= (0x1 << HPRE_SQE_SM2_KSEL_SHIFT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHIFT_U32()

return TEE_SUCCESS;
}

static bool is_size_support(size_t key_bits)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key_size_is_supported()?

return TEE_ERROR_BAD_PARAMETERS;
}

rand_k = (uint8_t *)malloc(size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to cast void *

return TEE_ERROR_BAD_PARAMETERS;
}

ecc_key = (struct ecc_keypair *)sdata->key_priv;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to cast void *, same below.

uint32_t sm2_mlen;
bool sm2_sp;
union {
struct hpre_ecc_dh ecc_dh;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indent these.

#ifndef __HPRE_ECC_H__
#define __HPRE_ECC_H__

#define ECC_DH_IN_PARAM_NUM 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you expect these to be used outside hpre_ecc.c or could they be moved into that C file?


struct hpre_ecc_msg {
uint8_t *key;
uintptr_t key_dma;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddr_t? Same below.


#ifndef __HPRE_ECC_H__
#define __HPRE_ECC_H__

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include <stdbool.h>
#include <stdint.h>
#include <tee_api_type.h>
#include <types_ext.h>    // for paddr_t suggested by @jenswi-linaro 

.x = g_prime192v1_gx,
.y = g_prime192v1_gy,
.n = g_prime192v1_n,
}, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	},
	{

uint32_t i = 0;

for (i = 0; i < size; i++) {
if (data[i] > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking: if (data[i])

err1 = HPRE_TASK_ETYPE1(sqe->w0);
done = HPRE_TASK_DONE(sqe->w0);
if (done != HPRE_HW_TASK_DONE || err || err1) {
EMSG("HPRE do ecc fail! done=0x%x, etype=0x%x, etype1=0x%x",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EMSG("HPRE do ecc fail! done=0x%"PRIx16", etype=0x%"PRIx16", etype1=0x%"RPIx16,

static TEE_Result hpre_do_ecc_task(void *msg)
{
struct hisi_qp *ecc_qp = NULL;
int32_t ret = TEE_SUCCESS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum hisi_drv_status res= HISI_QM_DRVCRYPT_<smoething>;

if (!msg->key_bytes)
return TEE_ERROR_BAD_PARAMETERS;

ret = hpre_ecc_dh_params_alloc(msg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function return a enum hisi_drv_status value.


curve = get_curve_from_list(key->curve);
if (!curve) {
EMSG("Fail to get valid curve, id %u.", key->curve);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... %"PRIu32, key->curve);

Ditto at line 914.

}

if (BITS_TO_BYTES(size_bits) != BITS_TO_BYTES(curve->key_bits)) {
EMSG("Invalid size_bits %lu.", size_bits);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... %zu.", size_bits);

Ditto at line 919.


ret = hpre_do_ecc_task(&msg);
if (ret) {
EMSG("Fail to do ecc key pair task! ret = 0x%x.", ret);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... ret = 0x%"PRIx32, ret);

ditto at line 931.

if (ret != TEE_SUCCESS)
EMSG("Hpre ecc register to crypto fail");

return ret;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of error, OP-TEE will only print a message and continue initialization. If you want the platform to refuse to boot (and not fallback to a software ECC implementation, you should explicitly panic here.

@yuzexiyzx yuzexiyzx force-pushed the master branch 3 times, most recently from 7f718ff to 63a2a14 Compare May 10, 2024 01:48
@yuzexiyzx
Copy link
Contributor Author

@etienne-lms Could you please tell me how to deal with warning like this?
image
Should I move struct hpre_ecc_curve to next line? I don't know how to deal with alignment like this.

add ECC gen_keypair and shared secret

Signed-off-by: yuzexi <yuzexi@hisilicon.com>
@etienne-lms
Copy link
Contributor

Hi @yuzexiyzx,
My suggestion:

-static enum hisi_drv_status hpre_ecc_dh_params_fill(struct hpre_ecc_curve *curve,
-						    struct hpre_ecc_msg *msg,
-						    struct bignum *privkey,
-						    struct ecc_public_key *pubkey)
+static enum hisi_drv_status
+hpre_ecc_dh_params_fill(struct hpre_ecc_curve *curve, struct hpre_ecc_msg *msg,
+                        struct bignum *privkey, struct ecc_public_key *pubkey)
 {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants