diff --git a/host/xtest/aes_perf.c b/host/xtest/aes_perf.c index c2f381ed2..1f9c715f4 100644 --- a/host/xtest/aes_perf.c +++ b/host/xtest/aes_perf.c @@ -162,16 +162,39 @@ static double stddev(struct statistics *s) return sqrt(s->M2/s->n); } -static const char *mode_str(uint32_t mode) +static const char *cipher_str(uint32_t algo) { - switch (mode) { + switch (algo) { case TA_AES_ECB: + case TA_AES_CBC: + case TA_AES_CTR: + case TA_AES_XTS: + case TA_AES_GCM: + return "AES"; + case TA_SM4_ECB: + case TA_SM4_CBC: + case TA_SM4_CTR: + case TA_SM4_XTS: + return "SM4"; + default: + return "???"; + } +} + +static const char *mode_str(uint32_t algo) +{ + switch (algo) { + case TA_AES_ECB: + case TA_SM4_ECB: return "ECB"; case TA_AES_CBC: + case TA_SM4_CBC: return "CBC"; case TA_AES_CTR: + case TA_SM4_CTR: return "CTR"; case TA_AES_XTS: + case TA_SM4_XTS: return "XTS"; case TA_AES_GCM: return "GCM"; @@ -183,19 +206,19 @@ static const char *mode_str(uint32_t mode) #define _TO_STR(x) #x #define TO_STR(x) _TO_STR(x) -static void usage(const char *applet_optname, int keysize, int mode, +static void usage(const char *applet_optname, int keysize, int algo, size_t size, size_t unit, int warmup, unsigned int l, unsigned int n) { fprintf(stderr, "Usage: %s %s [-h]\n", xtest_progname, applet_optname); fprintf(stderr, "Usage: %s %s [-d] [-i] [-k SIZE]", xtest_progname, applet_optname); - fprintf(stderr, " [-l LOOP] [-m MODE] [-n LOOP] [-r|--no-inited] [-s SIZE]"); + fprintf(stderr, " [-l LOOP] [-c CIPHER] [-m MODE] [-n LOOP] [-r|--no-inited] [-s SIZE]"); fprintf(stderr, " [-v [-v]] [-w SEC]"); #ifdef CFG_SECURE_DATA_PATH fprintf(stderr, " [--sdp [-Id|-Ir|-IR] [-Od|-Or|-OR]]"); #endif fprintf(stderr, "\n"); - fprintf(stderr, "AES performance testing tool for OP-TEE\n"); + fprintf(stderr, "AES/SM4 performance testing tool for OP-TEE\n"); fprintf(stderr, "\n"); fprintf(stderr, "Options:\n"); fprintf(stderr, " -d Test AES decryption instead of encryption\n"); @@ -203,7 +226,8 @@ static void usage(const char *applet_optname, int keysize, int mode, fprintf(stderr, " -i|--in-place Use same buffer for input and output (decrypt in place)\n"); fprintf(stderr, " -k SIZE Key size in bits: 128, 192 or 256 [%u]\n", keysize); fprintf(stderr, " -l LOOP Inner loop iterations [%u]\n", l); - fprintf(stderr, " -m MODE AES mode: ECB, CBC, CTR, XTS, GCM [%s]\n", mode_str(mode)); + fprintf(stderr, " -c CIPHER cipher: AES, SM4 [%s]\n", cipher_str(algo)); + fprintf(stderr, " -m MODE mode: ECB, CBC, CTR, XTS, GCM [%s]\n", mode_str(algo)); fprintf(stderr, " -n LOOP Outer test loop iterations [%u]\n", n); fprintf(stderr, " --not-inited Do not initialize input buffer content.\n"); fprintf(stderr, " -r|--random Get input data from /dev/urandom (default: all zeros)\n"); @@ -349,7 +373,7 @@ static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end) return timespec_to_ns(end) - timespec_to_ns(start); } -static void prepare_key(int decrypt, int keysize, int mode) +static void prepare_key(int decrypt, int keysize, int ) { TEEC_Result res = TEEC_ERROR_GENERIC; uint32_t ret_origin = 0; @@ -360,7 +384,7 @@ static void prepare_key(int decrypt, int keysize, int mode) TEEC_NONE, TEEC_NONE); op.params[0].value.a = decrypt; op.params[0].value.b = keysize; - op.params[1].value.a = mode; + op.params[1].value.a = ; res = TEEC_InvokeCommand(&sess, cmd, &op, &ret_origin); check_res(res, "TEEC_InvokeCommand", &ret_origin); @@ -423,7 +447,7 @@ static void run_feed_input(void *in, size_t size, int random) } -void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size, size_t unit, +void aes_perf_run_test(int , int keysize, int decrypt, size_t size, size_t unit, unsigned int n, unsigned int l, int input_data_init, int in_place, int warmup, int verbosity) { @@ -456,7 +480,7 @@ void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size, size_t u vverbose("output test buffer: %s\n", buf_type_str(output_buffer)); open_ta(); - prepare_key(decrypt, keysize, mode); + prepare_key(decrypt, keysize, ); alloc_buffers(size, in_place, verbosity); if (input_data_init == CRYPTO_USE_ZEROS) @@ -539,7 +563,7 @@ void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size, size_t u } \ } while (0); -#define USAGE() usage(argv[0], keysize, mode, size, unit, warmup, l, n) +#define USAGE() usage(argv[0], keysize, algo, size, unit, warmup, l, n) int aes_perf_runner_cmd_parser(int argc, char *argv[]) { @@ -554,7 +578,7 @@ int aes_perf_runner_cmd_parser(int argc, char *argv[]) int verbosity = CRYPTO_DEF_VERBOSITY; /* Verbosity (-v) */ int decrypt = 0; /* Encrypt by default, -d to decrypt */ int keysize = AES_128; /* AES key size (-k) */ - int mode = TA_AES_ECB; /* AES mode (-m) */ + int algo = TA_AES_ECB; /* AES algo (-m) */ /* Get input data from /dev/urandom (-r) */ int input_data_init = CRYPTO_USE_ZEROS; /* Use same buffer for in and out (-i) */ @@ -587,21 +611,43 @@ int aes_perf_runner_cmd_parser(int argc, char *argv[]) } else if (!strcmp(argv[i], "-l")) { NEXT_ARG(i); l = atoi(argv[i]); + } else if (!strcmp(argv[i], "-c")) { + NEXT_ARG(i); + if (!strcasecmp(argv[i], "SM4")) { + NEXT_ARG(i); + if (!strcmp(argv[i], "-m")) { + NEXT_ARG(i); + if (!strcasecmp(argv[i], "ECB")) + algo = TA_SM4_ECB; + else if (!strcasecmp(argv[i], "CBC")) + algo = TA_SM4_CBC; + else if (!strcasecmp(argv[i], "CTR")) + algo = TA_SM4_CTR; + else if (!strcasecmp(argv[i], "XTS")) + algo = TA_SM4_XTS; + else { + fprintf(stderr, "%s %s, invalid mode\n", + xtest_progname, argv[0]); + USAGE(); + return 1; + } + } + } } else if (!strcmp(argv[i], "-m")) { NEXT_ARG(i); if (!strcasecmp(argv[i], "ECB")) - mode = TA_AES_ECB; + algo = TA_AES_ECB; else if (!strcasecmp(argv[i], "CBC")) - mode = TA_AES_CBC; + algo = TA_AES_CBC; else if (!strcasecmp(argv[i], "CTR")) - mode = TA_AES_CTR; + algo = TA_AES_CTR; else if (!strcasecmp(argv[i], "XTS")) - mode = TA_AES_XTS; + algo = TA_AES_XTS; else if (!strcasecmp(argv[i], "GCM")) - mode = TA_AES_GCM; + algo = TA_AES_GCM; else { - fprintf(stderr, "%s %s, invalid mode\n", - xtest_progname, argv[0]); + (stderr, "%s %s, invalid mode\n", + xtest_progname, argv[0]); USAGE(); return 1; } @@ -669,7 +715,7 @@ int aes_perf_runner_cmd_parser(int argc, char *argv[]) } - aes_perf_run_test(mode, keysize, decrypt, size, unit, n, l, + aes_perf_run_test(algo, keysize, decrypt, size, unit, n, l, input_data_init, in_place, warmup, verbosity); return 0; diff --git a/ta/crypto_perf/include/ta_crypto_perf.h b/ta/crypto_perf/include/ta_crypto_perf.h index 1d5acc85d..14b6460fd 100644 --- a/ta/crypto_perf/include/ta_crypto_perf.h +++ b/ta/crypto_perf/include/ta_crypto_perf.h @@ -36,6 +36,10 @@ #define TA_AES_CTR 2 #define TA_AES_XTS 3 #define TA_AES_GCM 4 +#define TA_SM4_ECB 5 +#define TA_SM4_CBC 6 +#define TA_SM4_CTR 7 +#define TA_SM4_XTS 8 /* * AES key sizes diff --git a/ta/crypto_perf/ta_crypto_perf.c b/ta/crypto_perf/ta_crypto_perf.c index 5240f7eb3..f48750d00 100644 --- a/ta/crypto_perf/ta_crypto_perf.c +++ b/ta/crypto_perf/ta_crypto_perf.c @@ -240,6 +240,27 @@ TEE_Result cmd_cipher_prepare_key(uint32_t param_types, TEE_Param params[4]) objectType = TEE_TYPE_AES; use_iv = 1; break; + case TA_SM4_ECB: + algo = TEE_ALG_SM4_ECB_NOPAD; + objectType = TEE_TYPE_SM4; + use_iv = 0; + break; + case TA_SM4_CBC: + algo = TEE_ALG_SM4_CBC_NOPAD; + objectType = TEE_TYPE_SM4; + use_iv = 1; + break; + case TA_SM4_CTR: + algo = TEE_ALG_SM4_CTR; + objectType = TEE_TYPE_SM4; + use_iv = 1; + break; + case TA_SM4_XTS: + algo = TEE_ALG_SM4_XTS; + objectType = TEE_TYPE_SM4; + use_iv = 1; + op_keysize *= 2; + break; default: return TEE_ERROR_BAD_PARAMETERS; } @@ -259,7 +280,7 @@ TEE_Result cmd_cipher_prepare_key(uint32_t param_types, TEE_Param params[4]) res = TEE_PopulateTransientObject(hkey, &attr, 1); CHECK(res, "TEE_PopulateTransientObject", return res;); - if (algo == TEE_ALG_AES_XTS) { + if (algo == TEE_ALG_AES_XTS || algo == TEE_ALG_SM4_XTS) { res = TEE_AllocateTransientObject(objectType, keysize, &hkey2); CHECK(res, "TEE_AllocateTransientObject", return res;);