Skip to content

Commit

Permalink
xtest: add SM2 perf test
Browse files Browse the repository at this point in the history
add perf test for SM2 algorithm

Signed-off-by: Zexi Yu <[email protected]>
Acked-by: Etienne Carriere <[email protected]>
  • Loading branch information
yuzexiyzx authored and jforissier committed Apr 24, 2024
1 parent 8a31d08 commit 50e1cbd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
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")) {
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

0 comments on commit 50e1cbd

Please sign in to comment.