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

xtest: add SM2 perf test #739

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 28 additions & 6 deletions host/xtest/asym_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ static void usage(const char *progname, uint32_t width_bits, uint32_t main_algo,
fprintf(stderr, " RSASSA_PKCS1_PSS_MGF1_SHA256_SIGN, RSASSA_PKCS1_PSS_MGF1_SHA256_VERIFY\n");
fprintf(stderr, " RSASSA_PKCS1_PSS_MGF1_SHA384_SIGN, RSASSA_PKCS1_PSS_MGF1_SHA384_VERIFY\n");
fprintf(stderr, " RSASSA_PKCS1_PSS_MGF1_SHA512_SIGN, RSASSA_PKCS1_PSS_MGF1_SHA512_VERIFY\n");
fprintf(stderr, " ECDSA_SIGN, ECDSA_VERIFY, ECDH, X25519\n");
fprintf(stderr, " ECDSA_SIGN, ECDSA_VERIFY, ECDH, X25519, SM2_GENKEYPAIR, SM2_VERIFY\n");
fprintf(stderr, " SM2_ENCRYPT, SM2_DECRYPT\n");
fprintf(stderr, " -l LOOP Inner loop iterations [%u]\n", l);
fprintf(stderr, " -n LOOP Outer test loop iterations [%u]\n", n);
fprintf(stderr, " -r|--random Get input data from /dev/urandom (default: all zeros)\n");
Expand Down Expand Up @@ -232,17 +233,18 @@ static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
return timespec_to_ns(end) - timespec_to_ns(start);
}

static void prepare_obj(int width_bits, uint32_t main_algo)
static void prepare_obj(int width_bits, uint32_t main_algo, int mode)
{
uint32_t cmd = TA_CRYPTO_PERF_CMD_ASYM_PREPARE_OBJ;
TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
TEEC_Result res = TEEC_ERROR_GENERIC;
uint32_t ret_origin = 0;

op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
TEEC_NONE);
op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT,
TEEC_NONE, TEEC_NONE);
op.params[0].value.a = main_algo;
op.params[0].value.b = width_bits;
op.params[1].value.a = mode;

res = TEEC_InvokeCommand(&sess, cmd, &op, &ret_origin);
check_res(res, "TEEC_InvokeCommand()", &ret_origin);
Expand Down Expand Up @@ -467,7 +469,7 @@ static int asym_perf_run_test(int mode, size_t size, uint32_t n,
break;
}

prepare_obj(width_bits, main_algo);
prepare_obj(width_bits, main_algo, mode);

res = pack_attrs(params, param_count, &buf, &blen);
CHECK(res, "pack_attrs", goto out;);
Expand Down Expand Up @@ -904,6 +906,26 @@ int asym_perf_runner_cmd_parser(int argc, char *argv[])
main_algo = ALGO_X25519;
width_bits = 256;
mode = MODE_GENKEYPAIR;
} else if (!strcasecmp(argv[i], "SM2_GENKEYPAIR")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't you need to provide the main_algo info to generate an SM2 keypair?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

solved

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know much about SM2 but it seems the key type differs if used for ciphering and authentication (TEE_TYPE_SM2_DSA_KEYPAIR vs TEE_TYPE_SM2_PKE_KEYPAIR). Here we don't knw what the key will be used for, is that ok?
(resolved: answered by #739 (comment))

main_algo = ALGO_SM2;
width_bits = 256;
mode = MODE_GENKEYPAIR;
} else if (!strcasecmp(argv[i], "SM2_SIGN")) {
main_algo = ALGO_SM2;
width_bits = 256;
mode = MODE_SIGN;
} else if (!strcasecmp(argv[i], "SM2_VERIFY")) {
main_algo = ALGO_SM2;
width_bits = 256;
mode = MODE_VERIFY;
} else if (!strcasecmp(argv[i], "SM2_ENCRYPT")) {
main_algo = ALGO_SM2;
width_bits = 256;
mode = MODE_ENCRYPT;
} else if (!strcasecmp(argv[i], "SM2_DECRYPT")) {
main_algo = ALGO_SM2;
width_bits = 256;
mode = MODE_DECRYPT;
} else {
fprintf(stderr, "%s, invalid main_algo\n",
argv[0]);
Expand Down Expand Up @@ -957,7 +979,7 @@ int asym_perf_runner_cmd_parser(int argc, char *argv[])
}
}

if (mode == MODE_GENKEYPAIR)
if (mode == MODE_GENKEYPAIR || main_algo == ALGO_SM2)
size = BITS_TO_BYTES(width_bits);

return asym_perf_run_test(mode, size, n, l, is_random, warmup,
Expand Down
1 change: 1 addition & 0 deletions ta/crypto_perf/include/ta_crypto_perf.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ enum asym_algorithm {
ALGO_ECDSA = 3,
ALGO_ECDH = 4,
ALGO_X25519 = 5,
ALGO_SM2 = 6,
};

enum asym_cipher_mode {
Expand Down
24 changes: 21 additions & 3 deletions ta/crypto_perf/ta_crypto_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,14 +663,23 @@ static TEE_Result get_algo(uint32_t tee_type, uint32_t mode,
EMSG("ECDSA error mode");
res = TEE_ERROR_BAD_PARAMETERS;
}
} else if (tee_type == ALGO_SM2) {
if (mode == MODE_ENCRYPT || mode == MODE_DECRYPT) {
algo = TEE_ALG_SM2_PKE;
} else if (mode == MODE_SIGN || mode == MODE_VERIFY) {
algo = TEE_ALG_SM2_DSA_SM3;
} else {
EMSG("SM2 error mode");
res = TEE_ERROR_BAD_PARAMETERS;
}
} else {
res = TEE_ERROR_BAD_PARAMETERS;
}

return res;
}

static uint32_t get_keypair_type(uint32_t value)
static uint32_t get_keypair_type(uint32_t value, uint32_t mode)
{
switch (value) {
case ALGO_DH:
Expand All @@ -683,6 +692,15 @@ static uint32_t get_keypair_type(uint32_t value)
return TEE_TYPE_ECDH_KEYPAIR;
case ALGO_X25519:
return TEE_TYPE_X25519_KEYPAIR;
case ALGO_SM2:
if (mode == MODE_ENCRYPT || mode == MODE_DECRYPT) {
return TEE_TYPE_SM2_PKE_KEYPAIR;
} else if (mode == MODE_SIGN || mode == MODE_VERIFY) {
return TEE_TYPE_SM2_DSA_KEYPAIR;
} else {
EMSG("The mode[%"PRIu32"] is not valid", mode);
return TEE_TYPE_ILLEGAL_VALUE;
}
default:
EMSG("The algo[%"PRIu32"] is not valid", value);
return TEE_TYPE_ILLEGAL_VALUE;
Expand Down Expand Up @@ -817,14 +835,14 @@ TEE_Result cmd_asym_prepare_obj(uint32_t param_types,
TEE_Result res = TEE_ERROR_GENERIC;
uint32_t tee_type = TEE_TYPE_ILLEGAL_VALUE;
uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_VALUE_INPUT,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE);

if (param_types != exp_param_types)
return TEE_ERROR_BAD_PARAMETERS;

tee_type = get_keypair_type(params[0].value.a);
tee_type = get_keypair_type(params[0].value.a, params[1].value.a);
if (tee_type == TEE_TYPE_ILLEGAL_VALUE)
return TEE_ERROR_BAD_PARAMETERS;

Expand Down