已合并
add benchmarks #478
dumb创建于 2025年8月18日
add benchmarks #478
已合并
共 15 个文件变更+1824-246
| @@ -0,0 +1,151 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +static int32_t X25519SetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 25 | +{ | ||
| 26 | + (void)paraId; | ||
| 27 | + CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); | ||
| 28 | + if (pkeyCtx == NULL) { | ||
| 29 | + printf("Failed to create pkey context\n"); | ||
| 30 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 31 | + } | ||
| 32 | + int32_t ret = CRYPT_EAL_PkeyGen(pkeyCtx); | ||
| 33 | + if (ret != CRYPT_SUCCESS) { | ||
| 34 | + printf("Failed to gen curve25519 key.\n"); | ||
| 35 | + return ret; | ||
| 36 | + } | ||
| 37 | + *ctx = pkeyCtx; | ||
| 38 | + return CRYPT_SUCCESS; | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +static void X25519TearDown(void *ctx) | ||
| 42 | +{ | ||
| 43 | + CRYPT_EAL_PkeyFreeCtx(ctx); | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +static int32_t X25519KeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 47 | +{ | ||
| 48 | + int rc = CRYPT_SUCCESS; | ||
| 49 | + BENCH_TIMES(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "X25519 keyGen"); | ||
| 50 | + return rc; | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +static int32_t Ed25519SetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 54 | +{ | ||
| 55 | + return X25519SetUp(ctx, bench, ops, paraId); | ||
| 56 | +} | ||
| 57 | +static void Ed25519TearDown(void *ctx) | ||
| 58 | +{ | ||
| 59 | + X25519TearDown(ctx); | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +static int32_t Ed25519KeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 63 | +{ | ||
| 64 | + int rc = CRYPT_SUCCESS; | ||
| 65 | + BENCH_TIMES(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "Ed25519 keyGen"); | ||
| 66 | + return rc; | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +static int32_t X25519KeyDerive(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 70 | +{ | ||
| 71 | + int rc; | ||
| 72 | + CRYPT_EAL_PkeyCtx *peerCtx = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_X25519); | ||
| 73 | + if (peerCtx == NULL) { | ||
| 74 | + printf("Failed to create peer context\n"); | ||
| 75 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + rc = CRYPT_EAL_PkeyGen(peerCtx); | ||
| 79 | + if (rc != CRYPT_SUCCESS) { | ||
| 80 | + printf("Failed to gen peer key.\n"); | ||
| 81 | + CRYPT_EAL_PkeyFreeCtx(peerCtx); | ||
| 82 | + return rc; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + uint8_t sharedKey[32]; | ||
| 86 | + uint32_t sharedKeyLen = sizeof(sharedKey); | ||
| 87 | + | ||
| 88 | + BENCH_TIMES(CRYPT_EAL_PkeyComputeShareKey(ctx, peerCtx, sharedKey, &sharedKeyLen), rc, CRYPT_SUCCESS, -1, | ||
| 89 | + opts->times, "X25519 keyDerive"); | ||
| 90 | + | ||
| 91 | + CRYPT_EAL_PkeyFreeCtx(peerCtx); | ||
| 92 | + return rc; | ||
| 93 | +} | ||
| 94 | + | ||
| 95 | +static int32_t GetHashId(BenchCtx *bench, BenchOptions *opts) | ||
| 96 | +{ | ||
| 97 | + int32_t hashId = bench->ctxOps->hashId; | ||
| 98 | + if (opts->hashId != -1) { | ||
| 99 | + if (opts->hashId != CRYPT_MD_SHA512) { | ||
| 100 | + printf("Wrong Hash Algorithm Id for Ed25519. Must be SHA512."); | ||
| 101 | + return -1; | ||
| 102 | + } | ||
| 103 | + hashId = opts->hashId; | ||
| 104 | + } | ||
| 105 | + return hashId; | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +static int32_t Ed25519SignInner(void *ctx, int32_t hashId) | ||
| 109 | +{ | ||
| 110 | + uint8_t plainText[32]; | ||
| 111 | + uint8_t signature[64]; | ||
| 112 | + uint32_t signatureLen = sizeof(signature); | ||
| 113 | + return CRYPT_EAL_PkeySign(ctx, hashId, plainText, sizeof(plainText), signature, &signatureLen); | ||
| 114 | +} | ||
| 115 | + | ||
| 116 | +static int32_t Ed25519Sign(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 117 | +{ | ||
| 118 | + int rc; | ||
| 119 | + int32_t hashId = GetHashId(bench, opts); | ||
| 120 | + if (hashId == -1) { | ||
| 121 | + return -1; | ||
| 122 | + } | ||
| 123 | + BENCH_TIMES(Ed25519SignInner(ctx, hashId), rc, CRYPT_SUCCESS, -1, opts->times, "ed25519 sign"); | ||
| 124 | + return rc; | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +static int32_t Ed25519Verify(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 128 | +{ | ||
| 129 | + int rc; | ||
| 130 | + int32_t hashId = GetHashId(bench, opts); | ||
| 131 | + if (hashId == -1) { | ||
| 132 | + return -1; | ||
| 133 | + } | ||
| 134 | + uint8_t plainText[32]; | ||
| 135 | + uint8_t signature[64]; | ||
| 136 | + uint32_t signatureLen = sizeof(signature); | ||
| 137 | + rc = CRYPT_EAL_PkeySign(ctx, hashId, plainText, sizeof(plainText), signature, &signatureLen); | ||
| 138 | + if (rc != CRYPT_SUCCESS) { | ||
| 139 | + printf("Failed to sign\n"); | ||
| 140 | + return rc; | ||
| 141 | + } | ||
| 142 | + BENCH_TIMES(CRYPT_EAL_PkeyVerify(ctx, hashId, plainText, sizeof(plainText), signature, signatureLen), rc, | ||
| 143 | + CRYPT_SUCCESS, -1, opts->times, "ed25519 verify"); | ||
| 144 | + return rc; | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +DEFINE_OPS_KX(X25519, CRYPT_PKEY_X25519); | ||
| 148 | +DEFINE_BENCH_CTX_FIXLEN(X25519); | ||
| 149 | + | ||
| 150 | +DEFINE_OPS_SIGN(Ed25519, CRYPT_PKEY_ED25519, CRYPT_MD_SHA512); | ||
| 151 | +DEFINE_BENCH_CTX_FIXLEN(Ed25519); | ||
| @@ -15,9 +15,11 @@ cmake_minimum_required(VERSION 3.16 FATAL_ERROR) | |||
| 15 | PROJECT(openHiTLS_BENCHMARK) | 15 | PROJECT(openHiTLS_BENCHMARK) |
| 16 | 16 | ||
| 17 | set(OPENHITLS_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) | 17 | set(OPENHITLS_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) |
| 18 | -set(BENCHS sm2_bench.c slh_dsa_bench.c) | 18 | +set(BENCHS sm2_bench.c slh_dsa_bench.c ecdsa_bench.c md_bench.c cipher_bench.c mac_bench.c |
| 19 | + dh_bench.c ecdh_bench.c 25519_bench.c mldsa_bench.c mlkem_bench.c) | ||
| 19 | 20 | ||
| 20 | add_compile_options(-g) | 21 | add_compile_options(-g) |
| 22 | +add_link_options(-z noexecstack) | ||
| 21 | add_executable(openhitls_benchmark benchmark.c ${BENCHS}) | 23 | add_executable(openhitls_benchmark benchmark.c ${BENCHS}) |
| 22 | 24 | ||
| 23 | 25 | ||
| @@ -24,36 +24,169 @@ | |||
| 24 | 24 | ||
| 25 | extern BenchCtx Sm2BenchCtx; | 25 | extern BenchCtx Sm2BenchCtx; |
| 26 | extern BenchCtx SlhDsaBenchCtx; | 26 | extern BenchCtx SlhDsaBenchCtx; |
| 27 | +extern BenchCtx EcdsaBenchCtx; | ||
| 28 | +extern BenchCtx MdBenchCtx; | ||
| 29 | +extern BenchCtx CipherBenchCtx; | ||
| 30 | +extern BenchCtx MacBenchCtx; | ||
| 31 | +extern BenchCtx DhBenchCtx; | ||
| 32 | +extern BenchCtx EcdhBenchCtx; | ||
| 33 | +extern BenchCtx RsaBenchCtx; | ||
| 34 | +extern BenchCtx X25519BenchCtx; | ||
| 35 | +extern BenchCtx Ed25519BenchCtx; | ||
| 36 | +extern BenchCtx MldsaBenchCtx; | ||
| 37 | +extern BenchCtx MlkemBenchCtx; | ||
| 27 | 38 | ||
| 28 | BenchCtx *g_benchs[] = { | 39 | BenchCtx *g_benchs[] = { |
| 29 | - &Sm2BenchCtx, | 40 | + &Sm2BenchCtx, &SlhDsaBenchCtx, &EcdsaBenchCtx, &MdBenchCtx, &CipherBenchCtx, &MacBenchCtx, |
| 30 | - &SlhDsaBenchCtx, | 41 | + &DhBenchCtx, &EcdhBenchCtx, &X25519BenchCtx, &Ed25519BenchCtx, &MldsaBenchCtx, &MlkemBenchCtx, |
| 31 | }; | 42 | }; |
| 32 | 43 | ||
| 33 | -// 定义命令行选项结构 | ||
| 34 | typedef struct { | 44 | typedef struct { |
| 35 | - char *algorithm; // -a 选项指定的算法 | 45 | + const char *s; |
| 36 | - uint32_t times; // -t 选项指定的运行次数 | 46 | + int32_t id; |
| 37 | - uint32_t seconds; // -s 选项指定的运行时间 | 47 | +} StrIdMap; |
| 38 | - uint32_t len; | 48 | + |
| 39 | -} BenchOptions; | 49 | +static StrIdMap g_strIdMap[] = { |
| 50 | + {"md5", CRYPT_MD_MD5}, | ||
| 51 | + {"sha1", CRYPT_MD_SHA1}, | ||
| 52 | + {"sha224", CRYPT_MD_SHA224}, | ||
| 53 | + {"sha256", CRYPT_MD_SHA256}, | ||
| 54 | + {"sha384", CRYPT_MD_SHA384}, | ||
| 55 | + {"sha512", CRYPT_MD_SHA512}, | ||
| 56 | + {"sha3-224", CRYPT_MD_SHA3_224}, | ||
| 57 | + {"sha3-256", CRYPT_MD_SHA3_256}, | ||
| 58 | + {"sha3-384", CRYPT_MD_SHA3_384}, | ||
| 59 | + {"sha3-512", CRYPT_MD_SHA3_512}, | ||
| 60 | + {"shake128", CRYPT_MD_SHAKE128}, | ||
| 61 | + {"shake256", CRYPT_MD_SHAKE256}, | ||
| 62 | + {"sm3", CRYPT_MD_SM3}, | ||
| 63 | + {"SLH-DSA-SHA2-128S", CRYPT_SLH_DSA_SHA2_128S}, | ||
| 64 | + {"SLH-DSA-SHAKE-128S", CRYPT_SLH_DSA_SHAKE_128S}, | ||
| 65 | + {"SLH-DSA-SHA2-128F", CRYPT_SLH_DSA_SHA2_128F}, | ||
| 66 | + {"SLH-DSA-SHAKE-128F", CRYPT_SLH_DSA_SHAKE_128F}, | ||
| 67 | + {"SLH-DSA-SHA2-192S", CRYPT_SLH_DSA_SHA2_192S}, | ||
| 68 | + {"SLH-DSA-SHAKE-192S", CRYPT_SLH_DSA_SHAKE_192S}, | ||
| 69 | + {"SLH-DSA-SHA2-192F", CRYPT_SLH_DSA_SHA2_192F}, | ||
| 70 | + {"SLH-DSA-SHAKE-192F", CRYPT_SLH_DSA_SHAKE_192F}, | ||
| 71 | + {"SLH-DSA-SHA2-256S", CRYPT_SLH_DSA_SHA2_256S}, | ||
| 72 | + {"SLH-DSA-SHAKE-256S", CRYPT_SLH_DSA_SHAKE_256S}, | ||
| 73 | + {"SLH-DSA-SHA2-256F", CRYPT_SLH_DSA_SHA2_256F}, | ||
| 74 | + {"SLH-DSA-SHAKE-256F", CRYPT_SLH_DSA_SHAKE_256F}, | ||
| 75 | + {"nistp224", CRYPT_ECC_NISTP224}, | ||
| 76 | + {"nistp256", CRYPT_ECC_NISTP256}, | ||
| 77 | + {"nistp384", CRYPT_ECC_NISTP384}, | ||
| 78 | + {"nistp521", CRYPT_ECC_NISTP521}, | ||
| 79 | + {"brainpoolP256r1", CRYPT_ECC_BRAINPOOLP256R1}, | ||
| 80 | + {"brainpoolP384r1", CRYPT_ECC_BRAINPOOLP384R1}, | ||
| 81 | + {"brainpoolP512r1", CRYPT_ECC_BRAINPOOLP512R1}, | ||
| 82 | + {"aes128-cbc", CRYPT_CIPHER_AES128_CBC}, | ||
| 83 | + {"aes128-ctr", CRYPT_CIPHER_AES128_CTR}, | ||
| 84 | + {"aes128-ecb", CRYPT_CIPHER_AES128_ECB}, | ||
| 85 | + {"aes128-xts", CRYPT_CIPHER_AES128_XTS}, | ||
| 86 | + {"aes128-ccm", CRYPT_CIPHER_AES128_CCM}, | ||
| 87 | + {"aes128-gcm", CRYPT_CIPHER_AES128_GCM}, | ||
| 88 | + {"aes128-cfb", CRYPT_CIPHER_AES128_CFB}, | ||
| 89 | + {"aes128-ofb", CRYPT_CIPHER_AES128_OFB}, | ||
| 90 | + {"aes192-cbc", CRYPT_CIPHER_AES192_CBC}, | ||
| 91 | + {"aes192-ctr", CRYPT_CIPHER_AES192_CTR}, | ||
| 92 | + {"aes192-ecb", CRYPT_CIPHER_AES192_ECB}, | ||
| 93 | + {"aes192-ccm", CRYPT_CIPHER_AES192_CCM}, | ||
| 94 | + {"aes192-gcm", CRYPT_CIPHER_AES192_GCM}, | ||
| 95 | + {"aes192-cfb", CRYPT_CIPHER_AES192_CFB}, | ||
| 96 | + {"aes192-ofb", CRYPT_CIPHER_AES192_OFB}, | ||
| 97 | + {"aes256-cbc", CRYPT_CIPHER_AES256_CBC}, | ||
| 98 | + {"aes256-ctr", CRYPT_CIPHER_AES256_CTR}, | ||
| 99 | + {"aes256-ecb", CRYPT_CIPHER_AES256_ECB}, | ||
| 100 | + {"aes256-xts", CRYPT_CIPHER_AES256_XTS}, | ||
| 101 | + {"aes256-ccm", CRYPT_CIPHER_AES256_CCM}, | ||
| 102 | + {"aes256-gcm", CRYPT_CIPHER_AES256_GCM}, | ||
| 103 | + {"aes256-cfb", CRYPT_CIPHER_AES256_CFB}, | ||
| 104 | + {"aes256-ofb", CRYPT_CIPHER_AES256_OFB}, | ||
| 105 | + {"sm4-cbc", CRYPT_CIPHER_SM4_CBC}, | ||
| 106 | + {"sm4-ecb", CRYPT_CIPHER_SM4_ECB}, | ||
| 107 | + {"sm4-ctr", CRYPT_CIPHER_SM4_CTR}, | ||
| 108 | + {"sm4-gcm", CRYPT_CIPHER_SM4_GCM}, | ||
| 109 | + {"sm4-cfb", CRYPT_CIPHER_SM4_CFB}, | ||
| 110 | + {"sm4-ofb", CRYPT_CIPHER_SM4_OFB}, | ||
| 111 | + {"sm4-xts", CRYPT_CIPHER_SM4_XTS}, | ||
| 112 | + {"chacha20-poly1305", CRYPT_CIPHER_CHACHA20_POLY1305}, | ||
| 113 | + {"hmac-md5", CRYPT_MAC_HMAC_MD5}, | ||
| 114 | + {"hmac-sha1", CRYPT_MAC_HMAC_SHA1}, | ||
| 115 | + {"hmac-sha224", CRYPT_MAC_HMAC_SHA224}, | ||
| 116 | + {"hmac-sha256", CRYPT_MAC_HMAC_SHA256}, | ||
| 117 | + {"hmac-sha384", CRYPT_MAC_HMAC_SHA384}, | ||
| 118 | + {"hmac-sha512", CRYPT_MAC_HMAC_SHA512}, | ||
| 119 | + {"hmac-sha3-224", CRYPT_MAC_HMAC_SHA3_224}, | ||
| 120 | + {"hmac-sha3-256", CRYPT_MAC_HMAC_SHA3_256}, | ||
| 121 | + {"hmac-sha3-384", CRYPT_MAC_HMAC_SHA3_384}, | ||
| 122 | + {"hmac-sha3-512", CRYPT_MAC_HMAC_SHA3_512}, | ||
| 123 | + {"hmac-sha3-224", CRYPT_MAC_HMAC_SHA3_224}, | ||
| 124 | + {"hmac-sha3-256", CRYPT_MAC_HMAC_SHA3_256}, | ||
| 125 | + {"hmac-sm3", CRYPT_MAC_HMAC_SM3}, | ||
| 126 | + {"cmac-aes128", CRYPT_MAC_CMAC_AES128}, | ||
| 127 | + {"cmac-aes192", CRYPT_MAC_CMAC_AES192}, | ||
| 128 | + {"cmac-aes256", CRYPT_MAC_CMAC_AES256}, | ||
| 129 | + {"cmac-sm4", CRYPT_MAC_CMAC_SM4}, | ||
| 130 | + {"cbc-mac-sm4", CRYPT_MAC_CBC_MAC_SM4}, | ||
| 131 | + {"gmac-aes128", CRYPT_MAC_GMAC_AES128}, | ||
| 132 | + {"gmac-aes192", CRYPT_MAC_GMAC_AES192}, | ||
| 133 | + {"gmac-aes256", CRYPT_MAC_GMAC_AES256}, | ||
| 134 | + {"siphash64", CRYPT_MAC_SIPHASH64}, | ||
| 135 | + {"siphash128", CRYPT_MAC_SIPHASH128}, | ||
| 136 | + {"dh-rfc2409-768", CRYPT_DH_RFC2409_768}, | ||
| 137 | + {"dh-rfc2409-1024", CRYPT_DH_RFC2409_1024}, | ||
| 138 | + {"dh-rfc3526-1536", CRYPT_DH_RFC3526_1536}, | ||
| 139 | + {"dh-rfc3526-2048", CRYPT_DH_RFC3526_2048}, | ||
| 140 | + {"dh-rfc3526-3072", CRYPT_DH_RFC3526_3072}, | ||
| 141 | + {"dh-rfc3526-4096", CRYPT_DH_RFC3526_4096}, | ||
| 142 | + {"dh-rfc3526-6144", CRYPT_DH_RFC3526_6144}, | ||
| 143 | + {"dh-rfc3526-8192", CRYPT_DH_RFC3526_8192}, | ||
| 144 | + {"dh-rfc7919-2048", CRYPT_DH_RFC7919_2048}, | ||
| 145 | + {"dh-rfc7919-3072", CRYPT_DH_RFC7919_3072}, | ||
| 146 | + {"dh-rfc7919-4096", CRYPT_DH_RFC7919_4096}, | ||
| 147 | + {"dh-rfc7919-6144", CRYPT_DH_RFC7919_6144}, | ||
| 148 | + {"dh-rfc7919-8192", CRYPT_DH_RFC7919_8192}, | ||
| 149 | + | ||
| 150 | +}; | ||
| 151 | + | ||
| 152 | +static int32_t AlgStr2Id(char *str) | ||
| 153 | +{ | ||
| 154 | + for (int i = 0; i < SIZEOF(g_strIdMap); i++) { | ||
| 155 | + if (strncasecmp(g_strIdMap[i].s, str, strlen(g_strIdMap[i].s)) == 0) { | ||
| 156 | + return g_strIdMap[i].id; | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + return -1; | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +const char *GetAlgName(int32_t algId) | ||
| 163 | +{ | ||
| 164 | + for (int i = 0; i < SIZEOF(g_strIdMap); i++) { | ||
| 165 | + if (g_strIdMap[i].id == algId) { | ||
| 166 | + return g_strIdMap[i].s; | ||
| 167 | + } | ||
| 168 | + } | ||
| 169 | + return ""; | ||
| 170 | +} | ||
| 40 | 171 | ||
| 41 | static void PrintUsage(void) | 172 | static void PrintUsage(void) |
| 42 | { | 173 | { |
| 43 | printf("Usage: openhitls_benchmark [options]\n"); | 174 | printf("Usage: openhitls_benchmark [options]\n"); |
| 44 | printf("Options:\n"); | 175 | printf("Options:\n"); |
| 45 | - printf(" -a <algorithm> Specify algorithm to benchmark (e.g., sm2*, sm2-KeyGen, *KeyGen)\n"); | 176 | + printf(" -a <algorithm> Specify algorithm to benchmark (e.g., sm2*, sm2-KeyGen, *KeyGen)\n"); |
| 46 | - printf(" -t <times> Number of times to run each benchmark\n"); | 177 | + printf(" -t <times> Number of times to run each benchmark\n"); |
| 47 | - printf(" -s <seconds> Number of seconds to run each benchmark\n"); | 178 | + printf(" -s <seconds> Number of seconds to run each benchmark\n"); |
| 48 | - printf(" -l <len> Length of the payload to benchmark\n"); | 179 | + printf(" -l <len> Length of the payload to benchmark\n"); |
| 49 | - printf(" -h Show this help message\n"); | 180 | + printf(" -d <digest id> Digest algorithm id before sign\n"); |
| 181 | + printf(" -p <para id> Parameter id to benchmark\n"); | ||
| 182 | + printf(" -h Show this help message\n"); | ||
| 50 | } | 183 | } |
| 51 | 184 | ||
| 52 | static void ParseOptions(int argc, char **argv, BenchOptions *opts) | 185 | static void ParseOptions(int argc, char **argv, BenchOptions *opts) |
| 53 | { | 186 | { |
| 54 | int c; | 187 | int c; |
| 55 | 188 | ||
| 56 | - while ((c = getopt(argc, argv, "a:t:s:h")) != -1) { | 189 | + while ((c = getopt(argc, argv, "a:t:s:l:d:p:h")) != -1) { |
| 57 | switch (c) { | 190 | switch (c) { |
| 58 | case 'a': | 191 | case 'a': |
| 59 | opts->algorithm = optarg; | 192 | opts->algorithm = optarg; |
| @@ -67,6 +200,12 @@ static void ParseOptions(int argc, char **argv, BenchOptions *opts) | |||
| 67 | case 'l': | 200 | case 'l': |
| 68 | opts->len = (uint32_t)atoi(optarg); | 201 | opts->len = (uint32_t)atoi(optarg); |
| 69 | break; | 202 | break; |
| 203 | + case 'd': | ||
| 204 | + opts->hashId = AlgStr2Id(optarg); | ||
| 205 | + break; | ||
| 206 | + case 'p': | ||
| 207 | + opts->paraId = AlgStr2Id(optarg); | ||
| 208 | + break; | ||
| 70 | case 'h': | 209 | case 'h': |
| 71 | PrintUsage(); | 210 | PrintUsage(); |
| 72 | exit(0); | 211 | exit(0); |
| @@ -77,12 +216,17 @@ static void ParseOptions(int argc, char **argv, BenchOptions *opts) | |||
| 77 | } | 216 | } |
| 78 | } | 217 | } |
| 79 | 218 | ||
| 80 | -static bool MatchAlgorithm(const char *pattern, const char *name) | 219 | +bool MatchAlgorithm(const char *pattern, const char *name) |
| 81 | { | 220 | { |
| 82 | if (pattern == NULL) { | 221 | if (pattern == NULL) { |
| 83 | return true; | 222 | return true; |
| 84 | } | 223 | } |
| 85 | 224 | ||
| 225 | + // it's operation benchmark | ||
| 226 | + if (strncmp(pattern, "*-", 2) == 0) { | ||
| 227 | + return true; | ||
| 228 | + } | ||
| 229 | + | ||
| 86 | size_t patternLen = strlen(pattern); | 230 | size_t patternLen = strlen(pattern); |
| 87 | size_t nameLen = strlen(name); | 231 | size_t nameLen = strlen(name); |
| 88 | 232 | ||
| @@ -91,10 +235,9 @@ static bool MatchAlgorithm(const char *pattern, const char *name) | |||
| 91 | // Process prefix wildcard "*XXX" | 235 | // Process prefix wildcard "*XXX" |
| 92 | if (pattern[0] == '*') { | 236 | if (pattern[0] == '*') { |
| 93 | // Check if pattern is "*XXX" | 237 | // Check if pattern is "*XXX" |
| 94 | - return (nameLen >= patternLen - 1) && | 238 | + return (nameLen >= patternLen - 1) && (strcasecmp(name + nameLen - (patternLen - 1), pattern + 1) == 0); |
| 95 | - (strcasecmp(name + nameLen - (patternLen - 1), pattern + 1) == 0); | ||
| 96 | } | 239 | } |
| 97 | - | 240 | + |
| 98 | // Process suffix wildcard "XXX*" | 241 | // Process suffix wildcard "XXX*" |
| 99 | if (pattern[patternLen - 1] == '*') { | 242 | if (pattern[patternLen - 1] == '*') { |
| 100 | return strncasecmp(name, pattern, patternLen - 1) == 0; | 243 | return strncasecmp(name, pattern, patternLen - 1) == 0; |
| @@ -129,69 +272,94 @@ static uint32_t MatchOperation(const char *pattern, BenchCtx *bench) | |||
| 129 | return re; | 272 | return re; |
| 130 | } | 273 | } |
| 131 | 274 | ||
| 132 | -static void FilterBenchs(BenchOptions *opts, BenchCtx *benchs[], uint32_t *num) | 275 | +static int32_t InstantOperation(const Operation *op, void *ctx, BenchCtx *bench, BenchOptions *opts) |
| 133 | -{ | ||
| 134 | - const char *hyphen = strchr(opts->algorithm, '*'); | ||
| 135 | - for (int i = 0; i < sizeof(g_benchs) / sizeof(g_benchs[0]); i++) { | ||
| 136 | - if (!MatchAlgorithm(opts->algorithm, g_benchs[i]->name) && hyphen == NULL) { | ||
| 137 | - continue; | ||
| 138 | - } | ||
| 139 | - uint32_t ops = MatchOperation(opts->algorithm, g_benchs[i]); | ||
| 140 | - if (ops != 0) { | ||
| 141 | - uint32_t n = *num; | ||
| 142 | - benchs[n] = DupBenchCtx(g_benchs[i]); | ||
| 143 | - benchs[n]->seconds = opts->seconds; | ||
| 144 | - benchs[n]->len = opts->len; | ||
| 145 | - benchs[n]->filteredOps = ops; | ||
| 146 | - (*num)++; | ||
| 147 | - } | ||
| 148 | - } | ||
| 149 | -} | ||
| 150 | - | ||
| 151 | -static int32_t InstantOperation(const Operation *op, void *ctx, BenchCtx *bench) | ||
| 152 | { | 276 | { |
| 153 | if (op->id & KEY_GEN_ID) { | 277 | if (op->id & KEY_GEN_ID) { |
| 154 | - return ((KeyGen)op->oper)(ctx, bench); | 278 | + return ((KeyGen)op->oper)(ctx, bench, opts); |
| 155 | } | 279 | } |
| 156 | if (op->id & KEY_DERIVE_ID) { | 280 | if (op->id & KEY_DERIVE_ID) { |
| 157 | - return ((KeyDerive)op->oper)(ctx, bench); | 281 | + return ((KeyDerive)op->oper)(ctx, bench, opts); |
| 158 | } | 282 | } |
| 159 | if (op->id & ENC_ID) { | 283 | if (op->id & ENC_ID) { |
| 160 | - return ((Enc)op->oper)(ctx, bench); | 284 | + return ((Enc)op->oper)(ctx, bench, opts); |
| 161 | } | 285 | } |
| 162 | if (op->id & DEC_ID) { | 286 | if (op->id & DEC_ID) { |
| 163 | - return ((Dec)op->oper)(ctx, bench); | 287 | + return ((Dec)op->oper)(ctx, bench, opts); |
| 164 | } | 288 | } |
| 165 | if (op->id & SIGN_ID) { | 289 | if (op->id & SIGN_ID) { |
| 166 | - return ((Sign)op->oper)(ctx, bench); | 290 | + return ((Sign)op->oper)(ctx, bench, opts); |
| 167 | } | 291 | } |
| 168 | if (op->id & VERIFY_ID) { | 292 | if (op->id & VERIFY_ID) { |
| 169 | - return ((Verify)op->oper)(ctx, bench); | 293 | + return ((Verify)op->oper)(ctx, bench, opts); |
| 294 | + } | ||
| 295 | + if (op->id & ONESHOT_ID) { | ||
| 296 | + return ((OneShot)op->oper)(ctx, bench, opts); | ||
| 297 | + } | ||
| 298 | + if (op->id & ENCAPS_ID) { | ||
| 299 | + return ((Encaps)op->oper)(ctx, bench, opts); | ||
| 300 | + } | ||
| 301 | + if (op->id & DECAPS_ID) { | ||
| 302 | + return ((Decaps)op->oper)(ctx, bench, opts); | ||
| 303 | + } | ||
| 304 | + return CRYPT_NOT_SUPPORT; | ||
| 305 | +} | ||
| 306 | + | ||
| 307 | +static void ResetOptions(BenchOptions *opts, BenchCtx *benchCtx) | ||
| 308 | +{ | ||
| 309 | + if (opts->seconds == 0) { | ||
| 310 | + opts->seconds = benchCtx->seconds; | ||
| 311 | + } | ||
| 312 | + if (opts->times == 0) { | ||
| 313 | + opts->times = benchCtx->times; | ||
| 170 | } | 314 | } |
| 171 | } | 315 | } |
| 172 | 316 | ||
| 173 | -BenchCtx *DupBenchCtx(BenchCtx *bench) | 317 | +static void DoBenchTest(BenchCtx *benchs, const CtxOps *ctxOps, const Operation *op, BenchOptions *algOpts) |
| 174 | { | 318 | { |
| 175 | - BenchCtx *re = malloc(sizeof(BenchCtx)); | 319 | + void *ctx = NULL; |
| 176 | - if (re == NULL) { | 320 | + BENCH_SETUP(ctx, benchs, ctxOps, algOpts->paraId); |
| 177 | - printf("malloc failed\n"); | 321 | + if (op->id & KEY_GEN_ID || op->id & KEY_DERIVE_ID) { |
| 178 | - return NULL; | 322 | + // keygen and keyderive just do one fixed len. |
| 323 | + int32_t ret = InstantOperation(op, ctx, benchs, algOpts); | ||
| 324 | + if (ret != CRYPT_SUCCESS) { | ||
| 325 | + printf("Failed to %s, ret = %08x\n", op->name, ret); | ||
| 326 | + } | ||
| 327 | + } else { | ||
| 328 | + bool is_match = false; | ||
| 329 | + for (int i = 0; i < benchs->lensNum; i++) { | ||
| 330 | + if (algOpts->len != -1 && algOpts->len != benchs->lens[i]) { | ||
| 331 | + continue; | ||
| 332 | + } | ||
| 333 | + BENCH_SETUP(ctx, benchs, ctxOps, algOpts->paraId); | ||
| 334 | + BenchOptions tmpOpts = *algOpts; | ||
| 335 | + tmpOpts.len = benchs->lens[i]; | ||
| 336 | + int32_t ret = InstantOperation(op, ctx, benchs, &tmpOpts); | ||
| 337 | + if (ret != CRYPT_SUCCESS) { | ||
| 338 | + printf("Failed to %s, ret = %08x\n", op->name, ret); | ||
| 339 | + } | ||
| 340 | + is_match = true; | ||
| 341 | + } | ||
| 342 | + if (algOpts->len != -1 && !is_match) { | ||
| 343 | + int32_t ret = InstantOperation(op, ctx, benchs, algOpts); | ||
| 344 | + if (ret != CRYPT_SUCCESS) { | ||
| 345 | + printf("Failed to %s, ret = %08x\n", op->name, ret); | ||
| 346 | + } | ||
| 347 | + } | ||
| 179 | } | 348 | } |
| 180 | - *re = *bench; | 349 | + BENCH_TEARDOWN(ctx, ctxOps); |
| 181 | - | ||
| 182 | - return re; | ||
| 183 | } | 350 | } |
| 184 | 351 | ||
| 185 | int main(int argc, char **argv) | 352 | int main(int argc, char **argv) |
| 186 | { | 353 | { |
| 187 | int32_t ret; | 354 | int32_t ret; |
| 188 | - BenchOptions opts; | 355 | + BenchOptions opts = {0}; |
| 189 | - memset(&opts, 0, sizeof(BenchOptions)); | ||
| 190 | 356 | ||
| 191 | // default options | 357 | // default options |
| 192 | - opts.times = 10000; | 358 | + opts.algorithm = "*"; // all algorithms |
| 193 | - opts.seconds = 3; | 359 | + opts.filteredOps = 0x3F; // all operations |
| 194 | - opts.len = 1024; | 360 | + opts.paraId = -1; // fake hash id |
| 361 | + opts.hashId = -1; // fake hash id | ||
| 362 | + opts.len = -1; // fake len | ||
| 195 | ParseOptions(argc, argv, &opts); | 363 | ParseOptions(argc, argv, &opts); |
| 196 | 364 | ||
| 197 | ret = CRYPT_EAL_ProviderRandInitCtx(NULL, CRYPT_RAND_SHA256, "provider=default", NULL, 0, NULL); | 365 | ret = CRYPT_EAL_ProviderRandInitCtx(NULL, CRYPT_RAND_SHA256, "provider=default", NULL, 0, NULL); |
| @@ -200,37 +368,35 @@ int main(int argc, char **argv) | |||
| 200 | return -1; | 368 | return -1; |
| 201 | } | 369 | } |
| 202 | 370 | ||
| 203 | - BenchCtx *benchs[sizeof(g_benchs) / sizeof(g_benchs[0])] = {0}; | 371 | + printf("%-35s, %10s, %15s, %15s, %20s\n", "algorithm operation", "len", "run times", "time elapsed(ms)", "ops/s"); |
| 204 | - uint32_t num = 0; | ||
| 205 | - FilterBenchs(&opts, benchs, &num); | ||
| 206 | 372 | ||
| 207 | - if (num > 0) { | 373 | + for (int i = 0; i < SIZEOF(g_benchs); i++) { |
| 208 | - printf("%-25s, %15s, %20s, %20s\n", "algorithm operation", "time elapsed(s)", "run times", "ops/s"); | 374 | + const CtxOps *ctxOps = g_benchs[i]->ctxOps; |
| 209 | - } | 375 | + BenchOptions algOpts = opts; |
| 376 | + ResetOptions(&algOpts, g_benchs[i]); | ||
| 210 | 377 | ||
| 211 | - for (int i = 0; i < num; i++) { | 378 | + // filtering benchmark test |
| 212 | - const CtxOps *ctxOps = benchs[i]->ctxOps; | 379 | + if (!MatchAlgorithm(opts.algorithm, g_benchs[i]->name)) { |
| 213 | - void *ctx = NULL; | 380 | + continue; |
| 381 | + } | ||
| 382 | + opts.filteredOps = MatchOperation(opts.algorithm, g_benchs[i]); | ||
| 214 | 383 | ||
| 215 | - BENCH_SETUP(ctx, ctxOps); | 384 | + for (int j = 0; j < g_benchs[i]->opsNum; j++) { |
| 216 | - | ||
| 217 | - for (int j = 0; j < benchs[i]->opsNum; j++) { | ||
| 218 | const Operation *op = &ctxOps->ops[j]; | 385 | const Operation *op = &ctxOps->ops[j]; |
| 219 | - if ((uint32_t)(op->id & benchs[i]->filteredOps) == 0U) { | 386 | + if ((uint32_t)(op->id & opts.filteredOps) == 0U) { |
| 220 | continue; | 387 | continue; |
| 221 | } | 388 | } |
| 222 | - ret = InstantOperation(op, ctx, benchs[i]); | 389 | + BenchOptions tmpOpts = algOpts; |
| 223 | - if (ret != CRYPT_SUCCESS) { | 390 | + if (tmpOpts.paraId != -1 || g_benchs[i]->paraIdsNum == 0) { |
| 224 | - printf("Failed to %s, ret = %08x\n", op->name, ret); | 391 | + DoBenchTest(g_benchs[i], ctxOps, op, &tmpOpts); |
| 392 | + } else { | ||
| 393 | + for (int k = 0; k < g_benchs[i]->paraIdsNum; k++) { | ||
| 394 | + tmpOpts.paraId = g_benchs[i]->paraIds[k]; | ||
| 395 | + DoBenchTest(g_benchs[i], ctxOps, op, &tmpOpts); | ||
| 396 | + } | ||
| 225 | } | 397 | } |
| 226 | } | 398 | } |
| 227 | - | ||
| 228 | - BENCH_TEARDOWN(ctx, ctxOps); | ||
| 229 | - } | ||
| 230 | - | ||
| 231 | - for (int i = 0; i < num; i++) { | ||
| 232 | - free(benchs[i]); | ||
| 233 | } | 399 | } |
| 234 | 400 | ||
| 235 | return 0; | 401 | return 0; |
| 236 | -} | 402 | +} |
| @@ -22,7 +22,7 @@ | |||
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | -#define BENCH_TIMES(func, rc, ok, times, header) \ | 25 | +#define BENCH_TIMES(func, rc, ok, len, times, header) \ |
| 26 | { \ | 26 | { \ |
| 27 | struct timespec start, end; \ | 27 | struct timespec start, end; \ |
| 28 | clock_gettime(CLOCK_REALTIME, &start); \ | 28 | clock_gettime(CLOCK_REALTIME, &start); \ |
| @@ -35,18 +35,18 @@ | |||
| 35 | } \ | 35 | } \ |
| 36 | clock_gettime(CLOCK_REALTIME, &end); \ | 36 | clock_gettime(CLOCK_REALTIME, &end); \ |
| 37 | uint64_t elapsedTime = (end.tv_sec - start.tv_sec) * 1000000000 + (end.tv_nsec - start.tv_nsec); \ | 37 | uint64_t elapsedTime = (end.tv_sec - start.tv_sec) * 1000000000 + (end.tv_nsec - start.tv_nsec); \ |
| 38 | - printf("%-25s, %15.2f, %20d, %20.2f\n", header, (double)elapsedTime / 1000000000, times, \ | 38 | + printf("%-35s, %10d, %15d, %16.2f, %20.2f\n", header, len, times, (double)elapsedTime / 1000000, \ |
| 39 | ((double)times * 1000000000) / elapsedTime); \ | 39 | ((double)times * 1000000000) / elapsedTime); \ |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | -#define BENCH_TIMES_VA(func, rc, ok, times, headerFmt, ...) \ | 42 | +#define BENCH_TIMES_VA(func, rc, ok, len, times, headerFmt, ...) \ |
| 43 | - { \ | 43 | + { \ |
| 44 | - char header[256] = {0}; \ | 44 | + char header[256] = {0}; \ |
| 45 | - sprintf(header, headerFmt, __VA_ARGS__); \ | 45 | + snprintf(header, sizeof(header), headerFmt, ##__VA_ARGS__); \ |
| 46 | - BENCH_TIMES(func, rc, ok, times, header); \ | 46 | + BENCH_TIMES(func, rc, ok, len, times, header); \ |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | -#define BENCH_SECONDS(func, rc, ok, secs, header) \ | 49 | +#define BENCH_SECONDS(func, rc, ok, len, secs, header) \ |
| 50 | { \ | 50 | { \ |
| 51 | struct timespec start, end; \ | 51 | struct timespec start, end; \ |
| 52 | uint64_t totalTime = secs * 1000000000; \ | 52 | uint64_t totalTime = secs * 1000000000; \ |
| @@ -63,25 +63,28 @@ | |||
| 63 | elapsedTime += (end.tv_sec - start.tv_sec) * 1000000000 + (end.tv_nsec - start.tv_nsec); \ | 63 | elapsedTime += (end.tv_sec - start.tv_sec) * 1000000000 + (end.tv_nsec - start.tv_nsec); \ |
| 64 | cnt++; \ | 64 | cnt++; \ |
| 65 | } \ | 65 | } \ |
| 66 | - printf("%-25s, %15ld, %20d, %20.2f\n", header, elapsedTime / 1000000000, cnt, \ | 66 | + printf("%-35s, %10d, %15d, %16.2f, %20.2f\n", header, len, cnt, elapsedTime / 1000000, \ |
| 67 | ((double)times * 1000000000) / elapsedTime); \ | 67 | ((double)times * 1000000000) / elapsedTime); \ |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | -#define BENCH_SETUP(ctx, ops) \ | 70 | +#define BENCH_SETUP(ctx, bench, ops, id) \ |
| 71 | - do { \ | 71 | + do { \ |
| 72 | - int32_t ret; \ | 72 | + int32_t ret; \ |
| 73 | - ret = ops->newCtx(&ctx, ops); \ | 73 | + ret = ops->setUp(&ctx, bench, ops, id); \ |
| 74 | - if (ret != CRYPT_SUCCESS) { \ | 74 | + if (ret != CRYPT_SUCCESS) { \ |
| 75 | - printf("Failed to create context\n"); \ | 75 | + printf("Failed to setup benchmark testcase: %08x\n", ret); \ |
| 76 | - return ret; \ | 76 | + return; \ |
| 77 | - } \ | 77 | + } \ |
| 78 | } while (0) | 78 | } while (0) |
| 79 | 79 | ||
| 80 | 80 | ||
| 81 | do { \ | 81 | do { \ |
| 82 | - ops->freeCtx(ctx); \ | 82 | + ops->tearDown(ctx); \ |
| 83 | } while (0) | 83 | } while (0) |
| 84 | 84 | ||
| 85 | +// sizeof array | ||
| 86 | + | ||
| 87 | + | ||
| 85 | static inline void Hex2Bin(const char *hex, uint8_t *bin, uint32_t *len) | 88 | static inline void Hex2Bin(const char *hex, uint8_t *bin, uint32_t *len) |
| 86 | { | 89 | { |
| 87 | *len = strlen(hex) / 2; | 90 | *len = strlen(hex) / 2; |
| @@ -90,17 +93,34 @@ static inline void Hex2Bin(const char *hex, uint8_t *bin, uint32_t *len) | |||
| 90 | } | 93 | } |
| 91 | } | 94 | } |
| 92 | 95 | ||
| 96 | +// 定义命令行选项结构 | ||
| 97 | +typedef struct { | ||
| 98 | + char *algorithm; // -a 选项指定的算法 | ||
| 99 | + uint32_t filteredOps; | ||
| 100 | + uint32_t times; // -t 选项指定的运行次数 | ||
| 101 | + uint32_t seconds; // -s 选项指定的运行时间 | ||
| 102 | + uint32_t len; | ||
| 103 | + int32_t paraId; | ||
| 104 | + int32_t hashId; | ||
| 105 | +} BenchOptions; | ||
| 106 | + | ||
| 93 | typedef struct BenchCtx_ BenchCtx; | 107 | typedef struct BenchCtx_ BenchCtx; |
| 94 | typedef struct CtxOps_ CtxOps; | 108 | typedef struct CtxOps_ CtxOps; |
| 95 | // every benchmark testcase should define "NewCtx" and "FreeCtx" | 109 | // every benchmark testcase should define "NewCtx" and "FreeCtx" |
| 96 | -typedef int32_t (*NewCtx)(void **ctx, const CtxOps *ops); | 110 | +typedef int32_t (*SetUp)(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t id); |
| 97 | -typedef void (*FreeCtx)(void *ctx); | 111 | +typedef void (*TearDown)(void *ctx); |
| 98 | -typedef int32_t (*KeyGen)(void *ctx, BenchCtx *bench); | 112 | +typedef int32_t (*KeyGen)(void *ctx, BenchCtx *bench, BenchOptions *opts); |
| 99 | -typedef int32_t (*KeyDerive)(void *ctx, BenchCtx *bench); | 113 | +typedef int32_t (*KeyDerive)(void *ctx, BenchCtx *bench, BenchOptions *opts); |
| 100 | -typedef int32_t (*Enc)(void *ctx, BenchCtx *bench); | 114 | +typedef int32_t (*Enc)(void *ctx, BenchCtx *bench, BenchOptions *opts); |
| 101 | -typedef int32_t (*Dec)(void *ctx, BenchCtx *bench); | 115 | +typedef int32_t (*Dec)(void *ctx, BenchCtx *bench, BenchOptions *opts); |
| 102 | -typedef int32_t (*Sign)(void *ctx, BenchCtx *bench); | 116 | +typedef int32_t (*Sign)(void *ctx, BenchCtx *bench, BenchOptions *opts); |
| 103 | -typedef int32_t (*Verify)(void *ctx, BenchCtx *bench); | 117 | +typedef int32_t (*Verify)(void *ctx, BenchCtx *bench, BenchOptions *opts); |
| 118 | +typedef int32_t (*OneShot)(void *ctx, BenchCtx *bench, BenchOptions *opts); | ||
| 119 | +typedef int32_t (*Encaps)(void *ctx, BenchCtx *bench, BenchOptions *opts); | ||
| 120 | +typedef int32_t (*Decaps)(void *ctx, BenchCtx *bench, BenchOptions *opts); | ||
| 121 | + | ||
| 122 | +// return true if not be filetered; else return false. | ||
| 123 | +typedef bool (*ParaFilterCb)(BenchOptions *opts, int32_t paraId); | ||
| 104 | 124 | ||
| 105 | typedef struct { | 125 | typedef struct { |
| 106 | uint32_t id; | 126 | uint32_t id; |
| @@ -110,90 +130,151 @@ typedef struct { | |||
| 110 | 130 | ||
| 111 | struct CtxOps_ { | 131 | struct CtxOps_ { |
| 112 | int32_t algId; | 132 | int32_t algId; |
| 113 | - int32_t subAlgId; | 133 | + int32_t hashId; |
| 114 | - NewCtx newCtx; | 134 | + SetUp setUp; |
| 115 | - FreeCtx freeCtx; | 135 | + TearDown tearDown; |
| 116 | Operation ops[]; | 136 | Operation ops[]; |
| 117 | }; | 137 | }; |
| 118 | 138 | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - static const CtxOps alg##CtxOps = { \ | ||
| 122 | - .algId = id, \ | ||
| 123 | - .subAlgId = id, \ | ||
| 124 | - .newCtx = alg##NewCtx, \ | ||
| 125 | - .freeCtx = alg##FreeCtx, \ | ||
| 126 | - .ops = \ | ||
| 127 | - { \ | ||
| 128 | - DEFINE_OPER(1, alg##KeyGen), \ | ||
| 129 | - DEFINE_OPER(2, alg##KeyDerive), \ | ||
| 130 | - DEFINE_OPER(4, alg##Enc), \ | ||
| 131 | - DEFINE_OPER(8, alg##Dec), \ | ||
| 132 | - DEFINE_OPER(16, alg##Sign), \ | ||
| 133 | - DEFINE_OPER(32, alg##Verify), \ | ||
| 134 | - }, \ | ||
| 135 | - } | ||
| 136 | - | ||
| 137 | - | ||
| 138 | - static const CtxOps alg##CtxOps = { \ | ||
| 139 | - .algId = id1, \ | ||
| 140 | - .subAlgId = id2, \ | ||
| 141 | - .newCtx = alg##NewCtx, \ | ||
| 142 | - .freeCtx = alg##FreeCtx, \ | ||
| 143 | - .ops = \ | ||
| 144 | - { \ | ||
| 145 | - DEFINE_OPER(1, alg##KeyGen), \ | ||
| 146 | - DEFINE_OPER(16, alg##Sign), \ | ||
| 147 | - DEFINE_OPER(32, alg##Verify), \ | ||
| 148 | - }, \ | ||
| 149 | - } | ||
| 150 | - | ||
| 151 | - | ||
| 152 | - | ||
| 153 | - static const CtxOps alg##CtxOps = { \ | ||
| 154 | - .algId = id, \ | ||
| 155 | - .subAlgId = id, \ | ||
| 156 | - .newCtx = alg##NewCtx, \ | ||
| 157 | - .freeCtx = alg##FreeCtx, \ | ||
| 158 | - .ops = \ | ||
| 159 | - { \ | ||
| 160 | - DEFINE_OPER(4, alg##Enc), \ | ||
| 161 | - DEFINE_OPER(8, alg##Dec), \ | ||
| 162 | - }, \ | ||
| 163 | - } | ||
| 164 | - | ||
| 165 | 139 | ||
| 166 | 140 | ||
| 167 | 141 | ||
| 168 | 142 | ||
| 169 | 143 | ||
| 170 | 144 | ||
| 145 | + | ||
| 146 | + | ||
| 147 | + | ||
| 148 | + | ||
| 149 | +static int32_t g_lens[] = {16, 64, 256, 1024, 8192, 16384}; | ||
| 150 | +static uint8_t g_plain[16384]; | ||
| 151 | +static uint8_t g_out[16384]; | ||
| 152 | +static uint8_t g_key[32] = {1}; | ||
| 153 | +static uint8_t g_iv[16]; | ||
| 154 | + | ||
| 155 | + | ||
| 156 | + | ||
| 157 | + | ||
| 158 | + static const CtxOps alg##CtxOps = { \ | ||
| 159 | + .algId = id, \ | ||
| 160 | + .hashId = hId, \ | ||
| 161 | + .setUp = alg##SetUp, \ | ||
| 162 | + .tearDown = alg##TearDown, \ | ||
| 163 | + .ops = \ | ||
| 164 | + { \ | ||
| 165 | + DEFINE_OPER(KEY_GEN_ID, alg##KeyGen), \ | ||
| 166 | + DEFINE_OPER(KEY_DERIVE_ID, alg##KeyDerive), \ | ||
| 167 | + DEFINE_OPER(ENC_ID, alg##Enc), \ | ||
| 168 | + DEFINE_OPER(DEC_ID, alg##Dec), \ | ||
| 169 | + DEFINE_OPER(SIGN_ID, alg##Sign), \ | ||
| 170 | + DEFINE_OPER(VERIFY_ID, alg##Verify), \ | ||
| 171 | + }, \ | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + | ||
| 175 | + static const CtxOps alg##CtxOps = { \ | ||
| 176 | + .algId = id, \ | ||
| 177 | + .hashId = hId, \ | ||
| 178 | + .setUp = alg##SetUp, \ | ||
| 179 | + .tearDown = alg##TearDown, \ | ||
| 180 | + .ops = \ | ||
| 181 | + { \ | ||
| 182 | + DEFINE_OPER(KEY_GEN_ID, alg##KeyGen), \ | ||
| 183 | + DEFINE_OPER(SIGN_ID, alg##Sign), \ | ||
| 184 | + DEFINE_OPER(VERIFY_ID, alg##Verify), \ | ||
| 185 | + }, \ | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + | ||
| 189 | + static const CtxOps alg##CtxOps = { \ | ||
| 190 | + .algId = id, \ | ||
| 191 | + .hashId = id, \ | ||
| 192 | + .setUp = alg##SetUp, \ | ||
| 193 | + .tearDown = alg##TearDown, \ | ||
| 194 | + .ops = \ | ||
| 195 | + { \ | ||
| 196 | + DEFINE_OPER(ENC_ID, alg##Enc), \ | ||
| 197 | + DEFINE_OPER(DEC_ID, alg##Dec), \ | ||
| 198 | + }, \ | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + | ||
| 202 | + static const CtxOps alg##CtxOps = { \ | ||
| 203 | + .algId = CRYPT_MD_MAX, \ | ||
| 204 | + .hashId = CRYPT_MD_MAX, \ | ||
| 205 | + .setUp = alg##SetUp, \ | ||
| 206 | + .tearDown = alg##TearDown, \ | ||
| 207 | + .ops = \ | ||
| 208 | + { \ | ||
| 209 | + DEFINE_OPER(ONESHOT_ID, alg##OneShot), \ | ||
| 210 | + }, \ | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + | ||
| 214 | + static const CtxOps alg##CtxOps = { \ | ||
| 215 | + .algId = id, \ | ||
| 216 | + .hashId = CRYPT_MD_MAX, \ | ||
| 217 | + .setUp = alg##SetUp, \ | ||
| 218 | + .tearDown = alg##TearDown, \ | ||
| 219 | + .ops = \ | ||
| 220 | + { \ | ||
| 221 | + DEFINE_OPER(KEY_GEN_ID, alg##KeyGen), \ | ||
| 222 | + DEFINE_OPER(KEY_DERIVE_ID, alg##KeyDerive), \ | ||
| 223 | + }, \ | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + | ||
| 227 | + static const CtxOps alg##CtxOps = { \ | ||
| 228 | + .algId = id, \ | ||
| 229 | + .hashId = CRYPT_MD_MAX, \ | ||
| 230 | + .setUp = alg##SetUp, \ | ||
| 231 | + .tearDown = alg##TearDown, \ | ||
| 232 | + .ops = \ | ||
| 233 | + { \ | ||
| 234 | + DEFINE_OPER(KEY_GEN_ID, alg##KeyGen), \ | ||
| 235 | + DEFINE_OPER(ENCAPS_ID, alg##Encaps), \ | ||
| 236 | + DEFINE_OPER(DECAPS_ID, alg##Decaps), \ | ||
| 237 | + }, \ | ||
| 238 | + } | ||
| 171 | 239 | ||
| 172 | typedef struct BenchCtx_ { | 240 | typedef struct BenchCtx_ { |
| 173 | const char *name; | 241 | const char *name; |
| 174 | const char *desc; | 242 | const char *desc; |
| 175 | const CtxOps *ctxOps; | 243 | const CtxOps *ctxOps; |
| 176 | int32_t opsNum; | 244 | int32_t opsNum; |
| 177 | - uint32_t filteredOps; | 245 | + int32_t *paraIds; |
| 246 | + uint32_t paraIdsNum; | ||
| 247 | + int32_t *lens; | ||
| 248 | + uint32_t lensNum; | ||
| 178 | int32_t times; | 249 | int32_t times; |
| 179 | int32_t seconds; | 250 | int32_t seconds; |
| 180 | - int32_t len; | ||
| 181 | } BenchCtx; | 251 | } BenchCtx; |
| 182 | 252 | ||
| 183 | -#define DEFINE_BENCH_CTX_TIMES(alg, ts) \ | 253 | +#define DEFINE_BENCH_CTX_PARA_TIMES_LEN(alg, pId, pIdNum, ts, l, ln) \ |
| 184 | - BenchCtx alg##BenchCtx = { \ | 254 | + BenchCtx alg##BenchCtx = { \ |
| 185 | - .name = #alg, \ | 255 | + .name = #alg, \ |
| 186 | - .desc = #alg " benchmark", \ | 256 | + .desc = #alg " benchmark", \ |
| 187 | - .ctxOps = &alg##CtxOps, \ | 257 | + .ctxOps = &alg##CtxOps, \ |
| 188 | - .opsNum = sizeof(alg##CtxOps.ops) / sizeof(alg##CtxOps.ops[0]), \ | 258 | + .opsNum = SIZEOF(alg##CtxOps.ops), \ |
| 189 | - .filteredOps = 0, \ | 259 | + .paraIds = pId, \ |
| 190 | - .times = ts, \ | 260 | + .paraIdsNum = pIdNum, \ |
| 191 | - .seconds = -1, \ | 261 | + .lens = l, \ |
| 262 | + .lensNum = ln, \ | ||
| 263 | + .times = ts, \ | ||
| 264 | + .seconds = 0, \ | ||
| 192 | } | 265 | } |
| 266 | + | ||
| 267 | + DEFINE_BENCH_CTX_PARA_TIMES_LEN(alg, pId, pIdNum, ts, g_lens, SIZEOF(g_lens)) | ||
| 193 | 268 | ||
| 269 | + | ||
| 270 | + DEFINE_BENCH_CTX_PARA_TIMES_LEN(alg, pId, pIdNum, ts, g_lens, 1) | ||
| 194 | // default to run 10000 times | 271 | // default to run 10000 times |
| 195 | -#define DEFINE_BENCH_CTX(alg) DEFINE_BENCH_CTX_TIMES(alg, 10000) | 272 | +#define DEFINE_BENCH_CTX_PARA(alg, pId, pIdNum) DEFINE_BENCH_CTX_PARA_TIMES(alg, pId, pIdNum, 10000) |
| 273 | + | ||
| 274 | + | ||
| 275 | + | ||
| 196 | 276 | ||
| 197 | -BenchCtx *DupBenchCtx(BenchCtx *bench); | 277 | +bool MatchAlgorithm(const char *pattern, const char *name); |
| 278 | +const char *GetAlgName(int32_t hashId); | ||
| 198 | 279 | ||
| 199 | 280 | ||
| @@ -0,0 +1,283 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | +static int32_t CipherSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 24 | +{ | ||
| 25 | + *ctx = CRYPT_EAL_CipherNewCtx(paraId); | ||
| 26 | + if (*ctx == NULL) { | ||
| 27 | + printf("Failed to new cipher ctx\n"); | ||
| 28 | + return CRYPT_ERR_ALGID; | ||
| 29 | + } | ||
| 30 | + return CRYPT_SUCCESS; | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +static void CipherTearDown(void *ctx) | ||
| 34 | +{ | ||
| 35 | + if (ctx != NULL) { | ||
| 36 | + CRYPT_EAL_CipherFreeCtx(ctx); | ||
| 37 | + } | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +static int32_t DoCipherEnc(void *ctx, BenchOptions *opts) | ||
| 41 | +{ | ||
| 42 | + uint8_t out[16384]; // Maximum output size | ||
| 43 | + uint32_t outLen = sizeof(out); | ||
| 44 | + | ||
| 45 | + return CRYPT_EAL_CipherUpdate(ctx, g_plain, opts->len, out, &outLen); | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +static CRYPT_EAL_CipherCtx *InitCipherCtx(int32_t paraId, uint32_t keyLen, uint32_t ivLen, bool isEnc) | ||
| 49 | +{ | ||
| 50 | + int rc; | ||
| 51 | + | ||
| 52 | + CRYPT_EAL_CipherCtx *cipher = CRYPT_EAL_CipherNewCtx(paraId); | ||
| 53 | + if (cipher == NULL) { | ||
| 54 | + return NULL; | ||
| 55 | + } | ||
| 56 | + // the iv len of ccm is in [7, 13] | ||
| 57 | + rc = CRYPT_EAL_CipherInit(cipher, g_key, keyLen, g_iv, ivLen, isEnc); | ||
| 58 | + if (rc != CRYPT_SUCCESS) { | ||
| 59 | + printf("init ccm cipher failed\n"); | ||
| 60 | + return NULL; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + return cipher; | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +static int32_t DoCcmEnc(void *ctx, BenchOptions *opts, uint32_t keyLen, uint32_t ivLen) | ||
| 67 | +{ | ||
| 68 | + // aead do a complete init->ctrl->update->final process. | ||
| 69 | + (void)ctx; | ||
| 70 | + | ||
| 71 | + int rc; | ||
| 72 | + int32_t paraId = opts->paraId; | ||
| 73 | + uint32_t aad[32] = {1, 2, 3}; | ||
| 74 | + uint64_t msgLen = opts->len; | ||
| 75 | + uint32_t outLen = sizeof(g_out); | ||
| 76 | + uint8_t tag[16]; | ||
| 77 | + uint32_t tagLen = sizeof(tag); | ||
| 78 | + | ||
| 79 | + CRYPT_EAL_CipherCtx *cipher = InitCipherCtx(paraId, keyLen, ivLen, true); | ||
| 80 | + if (cipher == NULL) { | ||
| 81 | + return CRYPT_ERR_ALGID; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + if ((rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_SET_TAGLEN, &tagLen, sizeof(tagLen))) != CRYPT_SUCCESS || | ||
| 85 | + (rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_SET_MSGLEN, &msgLen, sizeof(msgLen))) != CRYPT_SUCCESS || | ||
| 86 | + (rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_SET_AAD, aad, sizeof(aad))) != CRYPT_SUCCESS || | ||
| 87 | + (rc = CRYPT_EAL_CipherUpdate(cipher, g_plain, opts->len, g_out, &outLen)) != CRYPT_SUCCESS || | ||
| 88 | + (rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_GET_TAG, tag, tagLen)) != CRYPT_SUCCESS) { | ||
| 89 | + printf("do ccm enc failed\n"); | ||
| 90 | + goto ERR; | ||
| 91 | + } | ||
| 92 | +ERR: | ||
| 93 | + CRYPT_EAL_CipherFreeCtx(cipher); | ||
| 94 | + return rc; | ||
| 95 | +} | ||
| 96 | + | ||
| 97 | +static int32_t DoCcmDec(void *ctx, BenchOptions *opts, uint32_t keyLen, uint32_t ivLen) | ||
| 98 | +{ | ||
| 99 | + // aead do a complete init->ctrl->update->final process. | ||
| 100 | + (void)ctx; | ||
| 101 | + | ||
| 102 | + int rc; | ||
| 103 | + int32_t paraId = opts->paraId; | ||
| 104 | + uint32_t aad[32] = {1, 2, 3}; | ||
| 105 | + uint64_t msgLen = opts->len; | ||
| 106 | + uint32_t outLen = sizeof(g_out); | ||
| 107 | + | ||
| 108 | + CRYPT_EAL_CipherCtx *cipher = InitCipherCtx(paraId, keyLen, ivLen, false); | ||
| 109 | + if (cipher == NULL) { | ||
| 110 | + return CRYPT_ERR_ALGID; | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + if ((rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_SET_MSGLEN, &msgLen, sizeof(msgLen))) != CRYPT_SUCCESS || | ||
| 114 | + (rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_SET_AAD, aad, sizeof(aad))) != CRYPT_SUCCESS || | ||
| 115 | + (rc = CRYPT_EAL_CipherUpdate(cipher, g_plain, opts->len, g_out, &outLen)) != CRYPT_SUCCESS) { | ||
| 116 | + printf("do ccm dec failed\n"); | ||
| 117 | + goto ERR; | ||
| 118 | + } | ||
| 119 | +ERR: | ||
| 120 | + CRYPT_EAL_CipherFreeCtx(cipher); | ||
| 121 | + return rc; | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +static int32_t DoGcmEnc(void *ctx, BenchOptions *opts, uint32_t keyLen, uint32_t ivLen) | ||
| 125 | +{ | ||
| 126 | + // aead do a complete init->ctrl->update->final process. | ||
| 127 | + (void)ctx; | ||
| 128 | + | ||
| 129 | + int rc; | ||
| 130 | + int32_t paraId = opts->paraId; | ||
| 131 | + uint32_t aad[32] = {1, 2, 3}; | ||
| 132 | + uint8_t tag[16]; | ||
| 133 | + uint32_t tagLen = sizeof(tag); | ||
| 134 | + uint32_t outLen = sizeof(g_out); | ||
| 135 | + | ||
| 136 | + CRYPT_EAL_CipherCtx *cipher = InitCipherCtx(paraId, keyLen, ivLen, true); | ||
| 137 | + if (cipher == NULL) { | ||
| 138 | + return CRYPT_ERR_ALGID; | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + if ((rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_SET_TAGLEN, &tagLen, sizeof(tagLen))) != CRYPT_SUCCESS || | ||
| 142 | + (rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_SET_AAD, aad, sizeof(aad))) != CRYPT_SUCCESS || | ||
| 143 | + (rc = CRYPT_EAL_CipherUpdate(cipher, g_plain, opts->len, g_out, &outLen)) != CRYPT_SUCCESS || | ||
| 144 | + (rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_GET_TAG, tag, sizeof(tag))) != CRYPT_SUCCESS) { | ||
| 145 | + printf("do gcm enc failed\n"); | ||
| 146 | + goto ERR; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | +ERR: | ||
| 150 | + CRYPT_EAL_CipherFreeCtx(cipher); | ||
| 151 | + return rc; | ||
| 152 | +} | ||
| 153 | + | ||
| 154 | +static int32_t DoGcmDec(void *ctx, BenchOptions *opts, uint32_t keyLen, uint32_t ivLen) | ||
| 155 | +{ | ||
| 156 | + // aead do a complete init->ctrl->update->final process. | ||
| 157 | + (void)ctx; | ||
| 158 | + | ||
| 159 | + int rc; | ||
| 160 | + int32_t paraId = opts->paraId; | ||
| 161 | + uint32_t aad[32] = {1, 2, 3}; | ||
| 162 | + uint8_t tag[16]; | ||
| 163 | + uint32_t tagLen = sizeof(tag); | ||
| 164 | + uint32_t outLen = sizeof(g_out); | ||
| 165 | + | ||
| 166 | + CRYPT_EAL_CipherCtx *cipher = InitCipherCtx(paraId, keyLen, ivLen, false); | ||
| 167 | + if (cipher == NULL) { | ||
| 168 | + return CRYPT_ERR_ALGID; | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + if ((rc = CRYPT_EAL_CipherCtrl(cipher, CRYPT_CTRL_SET_AAD, aad, sizeof(aad))) != CRYPT_SUCCESS || | ||
| 172 | + (rc = CRYPT_EAL_CipherUpdate(cipher, g_plain, opts->len, g_out, &outLen)) != CRYPT_SUCCESS) { | ||
| 173 | + printf("do gcm enc failed\n"); | ||
| 174 | + goto ERR; | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | +ERR: | ||
| 178 | + CRYPT_EAL_CipherFreeCtx(cipher); | ||
| 179 | + return rc; | ||
| 180 | +} | ||
| 181 | + | ||
| 182 | +static int32_t CipherEnc(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 183 | +{ | ||
| 184 | + int rc; | ||
| 185 | + int32_t paraId = opts->paraId; | ||
| 186 | + const char *cipherName = GetAlgName(paraId); | ||
| 187 | + uint32_t keyLen = 16; | ||
| 188 | + uint32_t ivLen = 16; | ||
| 189 | + | ||
| 190 | + if ((rc = CRYPT_EAL_CipherGetInfo(paraId, CRYPT_INFO_KEY_LEN, &keyLen)) != CRYPT_SUCCESS || | ||
| 191 | + (rc = CRYPT_EAL_CipherGetInfo(paraId, CRYPT_INFO_IV_LEN, &ivLen)) != CRYPT_SUCCESS || | ||
| 192 | + (rc = CRYPT_EAL_CipherInit(ctx, g_key, keyLen, g_iv, ivLen, true)) != CRYPT_SUCCESS) { | ||
| 193 | + return rc; | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + // aead | ||
| 197 | + if (paraId == CRYPT_CIPHER_AES128_CCM || paraId == CRYPT_CIPHER_AES192_CCM || paraId == CRYPT_CIPHER_AES256_CCM) { | ||
| 198 | + BENCH_TIMES_VA(DoCcmEnc(ctx, opts, keyLen, ivLen), rc, CRYPT_SUCCESS, opts->len, opts->times, "%s encrypt", | ||
| 199 | + cipherName); | ||
| 200 | + } else if (paraId == CRYPT_CIPHER_AES128_GCM || paraId == CRYPT_CIPHER_AES192_GCM || | ||
| 201 | + paraId == CRYPT_CIPHER_AES256_GCM || paraId == CRYPT_CIPHER_SM4_GCM) { | ||
| 202 | + BENCH_TIMES_VA(DoGcmEnc(ctx, opts, keyLen, ivLen), rc, CRYPT_SUCCESS, opts->len, opts->times, "%s encrypt", | ||
| 203 | + cipherName); | ||
| 204 | + } else { | ||
| 205 | + BENCH_TIMES_VA(DoCipherEnc(ctx, opts), rc, CRYPT_SUCCESS, opts->len, opts->times, "%s encrypt", cipherName); | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + return rc; | ||
| 209 | +} | ||
| 210 | + | ||
| 211 | +static int32_t CipherDec(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 212 | +{ | ||
| 213 | + int rc; | ||
| 214 | + int32_t paraId = opts->paraId; | ||
| 215 | + const char *cipherName = GetAlgName(paraId); | ||
| 216 | + uint32_t keyLen = 16; | ||
| 217 | + uint32_t ivLen = 16; | ||
| 218 | + | ||
| 219 | + if ((rc = CRYPT_EAL_CipherGetInfo(paraId, CRYPT_INFO_KEY_LEN, &keyLen)) != CRYPT_SUCCESS || | ||
| 220 | + (rc = CRYPT_EAL_CipherGetInfo(paraId, CRYPT_INFO_IV_LEN, &ivLen)) != CRYPT_SUCCESS || | ||
| 221 | + (rc = CRYPT_EAL_CipherInit(ctx, g_key, keyLen, g_iv, ivLen, false)) != CRYPT_SUCCESS) { | ||
| 222 | + return rc; | ||
| 223 | + } | ||
| 224 | + | ||
| 225 | + // aead | ||
| 226 | + if (paraId == CRYPT_CIPHER_AES128_CCM || paraId == CRYPT_CIPHER_AES192_CCM || paraId == CRYPT_CIPHER_AES256_CCM) { | ||
| 227 | + BENCH_TIMES_VA(DoCcmDec(ctx, opts, keyLen, ivLen), rc, CRYPT_SUCCESS, opts->len, opts->times, "%s decrypt", | ||
| 228 | + cipherName); | ||
| 229 | + } else if (paraId == CRYPT_CIPHER_AES128_GCM || paraId == CRYPT_CIPHER_AES192_GCM || | ||
| 230 | + paraId == CRYPT_CIPHER_AES256_GCM || paraId == CRYPT_CIPHER_SM4_GCM) { | ||
| 231 | + BENCH_TIMES_VA(DoGcmDec(ctx, opts, keyLen, ivLen), rc, CRYPT_SUCCESS, opts->len, opts->times, "%s decrypt", | ||
| 232 | + cipherName); | ||
| 233 | + } else { | ||
| 234 | + BENCH_TIMES_VA(DoCipherEnc(ctx, opts), rc, CRYPT_SUCCESS, opts->len, opts->times, "%s decrypt", cipherName); | ||
| 235 | + } | ||
| 236 | + return rc; | ||
| 237 | +} | ||
| 238 | + | ||
| 239 | +static int32_t g_paraIds[] = { | ||
| 240 | + // AES-128 modes | ||
| 241 | + CRYPT_CIPHER_AES128_CBC, | ||
| 242 | + CRYPT_CIPHER_AES128_CTR, | ||
| 243 | + CRYPT_CIPHER_AES128_ECB, | ||
| 244 | + CRYPT_CIPHER_AES128_XTS, | ||
| 245 | + CRYPT_CIPHER_AES128_CCM, | ||
| 246 | + CRYPT_CIPHER_AES128_GCM, | ||
| 247 | + CRYPT_CIPHER_AES128_CFB, | ||
| 248 | + CRYPT_CIPHER_AES128_OFB, | ||
| 249 | + | ||
| 250 | + // AES-192 modes | ||
| 251 | + CRYPT_CIPHER_AES192_CBC, | ||
| 252 | + CRYPT_CIPHER_AES192_CTR, | ||
| 253 | + CRYPT_CIPHER_AES192_ECB, | ||
| 254 | + CRYPT_CIPHER_AES192_CCM, | ||
| 255 | + CRYPT_CIPHER_AES192_GCM, | ||
| 256 | + CRYPT_CIPHER_AES192_CFB, | ||
| 257 | + CRYPT_CIPHER_AES192_OFB, | ||
| 258 | + | ||
| 259 | + // AES-256 modes | ||
| 260 | + CRYPT_CIPHER_AES256_CBC, | ||
| 261 | + CRYPT_CIPHER_AES256_CTR, | ||
| 262 | + CRYPT_CIPHER_AES256_ECB, | ||
| 263 | + CRYPT_CIPHER_AES256_XTS, | ||
| 264 | + CRYPT_CIPHER_AES256_CCM, | ||
| 265 | + CRYPT_CIPHER_AES256_GCM, | ||
| 266 | + CRYPT_CIPHER_AES256_CFB, | ||
| 267 | + CRYPT_CIPHER_AES256_OFB, | ||
| 268 | + | ||
| 269 | + // SM4 modes | ||
| 270 | + CRYPT_CIPHER_SM4_XTS, | ||
| 271 | + CRYPT_CIPHER_SM4_CBC, | ||
| 272 | + CRYPT_CIPHER_SM4_ECB, | ||
| 273 | + CRYPT_CIPHER_SM4_CTR, | ||
| 274 | + CRYPT_CIPHER_SM4_GCM, | ||
| 275 | + CRYPT_CIPHER_SM4_CFB, | ||
| 276 | + CRYPT_CIPHER_SM4_OFB, | ||
| 277 | + | ||
| 278 | + // ChaCha20-Poly1305 | ||
| 279 | + CRYPT_CIPHER_CHACHA20_POLY1305, | ||
| 280 | +}; | ||
| 281 | + | ||
| 282 | +DEFINE_OPS_CIPHER(Cipher, CRYPT_PKEY_MAX); | ||
| 283 | +DEFINE_BENCH_CTX_PARA(Cipher, g_paraIds, SIZEOF(g_paraIds)); | ||
| @@ -0,0 +1,106 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +static int32_t DhSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 25 | +{ | ||
| 26 | + int32_t ret; | ||
| 27 | + CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); | ||
| 28 | + if (pkeyCtx == NULL) { | ||
| 29 | + printf("Failed to create pkey context\n"); | ||
| 30 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 31 | + } | ||
| 32 | + ret = CRYPT_EAL_PkeySetParaById(pkeyCtx, paraId); | ||
| 33 | + if (ret != CRYPT_SUCCESS) { | ||
| 34 | + printf("Failed to set para: %d.\n", paraId); | ||
| 35 | + return ret; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + ret = CRYPT_EAL_PkeyGen(pkeyCtx); | ||
| 39 | + if (ret != CRYPT_SUCCESS) { | ||
| 40 | + printf("Failed to gen dh key.\n"); | ||
| 41 | + return ret; | ||
| 42 | + } | ||
| 43 | + *ctx = pkeyCtx; | ||
| 44 | + return CRYPT_SUCCESS; | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +static void DhTearDown(void *ctx) | ||
| 48 | +{ | ||
| 49 | + CRYPT_EAL_PkeyFreeCtx(ctx); | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +static int32_t DhKeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 53 | +{ | ||
| 54 | + int rc = CRYPT_SUCCESS; | ||
| 55 | + int32_t paraId = opts->paraId; | ||
| 56 | + const char *group = GetAlgName(paraId); | ||
| 57 | + | ||
| 58 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "%s dh keyGen", group); | ||
| 59 | + return rc; | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +static int32_t DhKeyDerive(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 63 | +{ | ||
| 64 | + int rc; | ||
| 65 | + int32_t paraId = opts->paraId; | ||
| 66 | + const char *group = GetAlgName(paraId); | ||
| 67 | + | ||
| 68 | + // Create a peer context for key exchange | ||
| 69 | + CRYPT_EAL_PkeyCtx *peerCtx = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_DH); | ||
| 70 | + if (peerCtx == NULL) { | ||
| 71 | + printf("Failed to create peer context\n"); | ||
| 72 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + rc = CRYPT_EAL_PkeySetParaById(peerCtx, paraId); | ||
| 76 | + if (rc != CRYPT_SUCCESS) { | ||
| 77 | + printf("Failed to set peer para: %d.\n", paraId); | ||
| 78 | + CRYPT_EAL_PkeyFreeCtx(peerCtx); | ||
| 79 | + return rc; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + rc = CRYPT_EAL_PkeyGen(peerCtx); | ||
| 83 | + if (rc != CRYPT_SUCCESS) { | ||
| 84 | + printf("Failed to gen peer key.\n"); | ||
| 85 | + CRYPT_EAL_PkeyFreeCtx(peerCtx); | ||
| 86 | + return rc; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + uint8_t sharedKey[4096]; // DH can have larger key sizes | ||
| 90 | + uint32_t sharedKeyLen = sizeof(sharedKey); | ||
| 91 | + | ||
| 92 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyComputeShareKey(ctx, peerCtx, sharedKey, &sharedKeyLen), rc, CRYPT_SUCCESS, -1, | ||
| 93 | + opts->times, "%s dh keyDervie", group); | ||
| 94 | + | ||
| 95 | + CRYPT_EAL_PkeyFreeCtx(peerCtx); | ||
| 96 | + return rc; | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +static int32_t g_paraIds[] = { | ||
| 100 | + CRYPT_DH_RFC2409_768, CRYPT_DH_RFC2409_1024, CRYPT_DH_RFC3526_1536, CRYPT_DH_RFC3526_2048, CRYPT_DH_RFC3526_3072, | ||
| 101 | + CRYPT_DH_RFC3526_4096, CRYPT_DH_RFC3526_6144, CRYPT_DH_RFC3526_8192, CRYPT_DH_RFC7919_2048, CRYPT_DH_RFC7919_3072, | ||
| 102 | + CRYPT_DH_RFC7919_4096, CRYPT_DH_RFC7919_6144, CRYPT_DH_RFC7919_8192, | ||
| 103 | +}; | ||
| 104 | + | ||
| 105 | +DEFINE_OPS_KX(Dh, CRYPT_PKEY_DH); | ||
| 106 | +DEFINE_BENCH_CTX_PARA_TIMES_FIXLEN(Dh, g_paraIds, SIZEOF(g_paraIds), 1000); | ||
| @@ -0,0 +1,105 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +static int32_t EcdhSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 25 | +{ | ||
| 26 | + int32_t ret; | ||
| 27 | + CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); | ||
| 28 | + if (pkeyCtx == NULL) { | ||
| 29 | + printf("Failed to create pkey context\n"); | ||
| 30 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 31 | + } | ||
| 32 | + ret = CRYPT_EAL_PkeySetParaById(pkeyCtx, paraId); | ||
| 33 | + if (ret != CRYPT_SUCCESS) { | ||
| 34 | + printf("Failed to set para: %d.\n", paraId); | ||
| 35 | + return ret; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + ret = CRYPT_EAL_PkeyGen(pkeyCtx); | ||
| 39 | + if (ret != CRYPT_SUCCESS) { | ||
| 40 | + printf("Failed to gen ecdh key.\n"); | ||
| 41 | + return ret; | ||
| 42 | + } | ||
| 43 | + *ctx = pkeyCtx; | ||
| 44 | + return CRYPT_SUCCESS; | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +static void EcdhTearDown(void *ctx) | ||
| 48 | +{ | ||
| 49 | + CRYPT_EAL_PkeyFreeCtx(ctx); | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +static int32_t EcdhKeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 53 | +{ | ||
| 54 | + int rc = CRYPT_SUCCESS; | ||
| 55 | + int32_t paraId = opts->paraId; | ||
| 56 | + const char *curve = GetAlgName(paraId); | ||
| 57 | + | ||
| 58 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "%s ecdh keyGen", curve); | ||
| 59 | + return rc; | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +static int32_t EcdhKeyDerive(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 63 | +{ | ||
| 64 | + int rc; | ||
| 65 | + int32_t paraId = opts->paraId; | ||
| 66 | + const char *curve = GetAlgName(paraId); | ||
| 67 | + | ||
| 68 | + // Create a peer context for key exchange | ||
| 69 | + CRYPT_EAL_PkeyCtx *peerCtx = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_ECDH); | ||
| 70 | + if (peerCtx == NULL) { | ||
| 71 | + printf("Failed to create peer context\n"); | ||
| 72 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + rc = CRYPT_EAL_PkeySetParaById(peerCtx, paraId); | ||
| 76 | + if (rc != CRYPT_SUCCESS) { | ||
| 77 | + printf("Failed to set peer para: %d.\n", paraId); | ||
| 78 | + CRYPT_EAL_PkeyFreeCtx(peerCtx); | ||
| 79 | + return rc; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + rc = CRYPT_EAL_PkeyGen(peerCtx); | ||
| 83 | + if (rc != CRYPT_SUCCESS) { | ||
| 84 | + printf("Failed to gen peer key.\n"); | ||
| 85 | + CRYPT_EAL_PkeyFreeCtx(peerCtx); | ||
| 86 | + return rc; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + uint8_t sharedKey[256]; | ||
| 90 | + uint32_t sharedKeyLen = sizeof(sharedKey); | ||
| 91 | + | ||
| 92 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyComputeShareKey(ctx, peerCtx, sharedKey, &sharedKeyLen), rc, CRYPT_SUCCESS, -1, | ||
| 93 | + opts->times, "%s ecdh keyDerive", curve); | ||
| 94 | + | ||
| 95 | + CRYPT_EAL_PkeyFreeCtx(peerCtx); | ||
| 96 | + return rc; | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +static int32_t g_paraIds[] = { | ||
| 100 | + CRYPT_ECC_NISTP224, CRYPT_ECC_NISTP256, CRYPT_ECC_NISTP384, CRYPT_ECC_NISTP521, | ||
| 101 | + CRYPT_ECC_BRAINPOOLP256R1, CRYPT_ECC_BRAINPOOLP384R1, CRYPT_ECC_BRAINPOOLP512R1, | ||
| 102 | +}; | ||
| 103 | + | ||
| 104 | +DEFINE_OPS_KX(Ecdh, CRYPT_PKEY_ECDH); | ||
| 105 | +DEFINE_BENCH_CTX_PARA_FIXLEN(Ecdh, g_paraIds, SIZEOF(g_paraIds)); | ||
| @@ -0,0 +1,124 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +static int32_t EcdsaSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 25 | +{ | ||
| 26 | + int32_t ret; | ||
| 27 | + CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); | ||
| 28 | + if (pkeyCtx == NULL) { | ||
| 29 | + printf("Failed to create pkey context\n"); | ||
| 30 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 31 | + } | ||
| 32 | + ret = CRYPT_EAL_PkeySetParaById(pkeyCtx, paraId); | ||
| 33 | + if (ret != CRYPT_SUCCESS) { | ||
| 34 | + printf("Failed to set para: %d.\n", paraId); | ||
| 35 | + return ret; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + ret = CRYPT_EAL_PkeyGen(pkeyCtx); | ||
| 39 | + if (ret != CRYPT_SUCCESS) { | ||
| 40 | + printf("Failed to gen ecdsa key.\n"); | ||
| 41 | + return ret; | ||
| 42 | + } | ||
| 43 | + *ctx = pkeyCtx; | ||
| 44 | + return CRYPT_SUCCESS; | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +static void EcdsaTearDown(void *ctx) | ||
| 48 | +{ | ||
| 49 | + CRYPT_EAL_PkeyFreeCtx(ctx); | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +static int32_t EcdsaKeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 53 | +{ | ||
| 54 | + int rc = CRYPT_SUCCESS; | ||
| 55 | + int32_t paraId = opts->paraId; | ||
| 56 | + const char *curve = GetAlgName(paraId); | ||
| 57 | + | ||
| 58 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "%s keyGen", curve); | ||
| 59 | + return rc; | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +static int32_t GetHashId(BenchCtx *bench, BenchOptions *opts) | ||
| 63 | +{ | ||
| 64 | + int32_t hashId = bench->ctxOps->hashId; | ||
| 65 | + if (opts->hashId != -1) { | ||
| 66 | + hashId = opts->hashId; | ||
| 67 | + } | ||
| 68 | + return hashId; | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +static int32_t EcdsaSignInner(void *ctx, int32_t hashId, int32_t len) | ||
| 72 | +{ | ||
| 73 | + uint8_t signature[256]; | ||
| 74 | + uint32_t signatureLen = sizeof(signature); | ||
| 75 | + return CRYPT_EAL_PkeySign(ctx, hashId, g_plain, len, signature, &signatureLen); | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +static int32_t EcdsaSign(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 79 | +{ | ||
| 80 | + int rc; | ||
| 81 | + int32_t paraId = opts->paraId; | ||
| 82 | + const char *curve = GetAlgName(paraId); | ||
| 83 | + int32_t hashId = GetHashId(bench, opts); | ||
| 84 | + if (hashId == -1) { | ||
| 85 | + return -1; | ||
| 86 | + } | ||
| 87 | + const char *mdName = GetAlgName(hashId); | ||
| 88 | + | ||
| 89 | + BENCH_TIMES_VA(EcdsaSignInner(ctx, hashId, opts->len), rc, CRYPT_SUCCESS, -1, opts->times, "%s-%s sign", curve, | ||
| 90 | + mdName); | ||
| 91 | + return rc; | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +static int32_t EcdsaVerify(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 95 | +{ | ||
| 96 | + int rc; | ||
| 97 | + int32_t paraId = opts->paraId; | ||
| 98 | + const char *curve = GetAlgName(paraId); | ||
| 99 | + int32_t hashId = GetHashId(bench, opts); | ||
| 100 | + if (hashId == -1) { | ||
| 101 | + return -1; | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + uint8_t signature[256]; | ||
| 105 | + uint32_t signatureLen = sizeof(signature); | ||
| 106 | + rc = CRYPT_EAL_PkeySign(ctx, hashId, g_plain, opts->len, signature, &signatureLen); | ||
| 107 | + if (rc != CRYPT_SUCCESS) { | ||
| 108 | + printf("Failed to sign\n"); | ||
| 109 | + return rc; | ||
| 110 | + } | ||
| 111 | + const char *mdName = GetAlgName(hashId); | ||
| 112 | + | ||
| 113 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyVerify(ctx, hashId, g_plain, opts->len, signature, signatureLen), rc, CRYPT_SUCCESS, | ||
| 114 | + -1, opts->times, "%s-%s verify", curve, mdName); | ||
| 115 | + return rc; | ||
| 116 | +} | ||
| 117 | + | ||
| 118 | +static int32_t g_paraIds[] = { | ||
| 119 | + CRYPT_ECC_NISTP224, CRYPT_ECC_NISTP256, CRYPT_ECC_NISTP384, CRYPT_ECC_NISTP521, | ||
| 120 | + CRYPT_ECC_BRAINPOOLP256R1, CRYPT_ECC_BRAINPOOLP384R1, CRYPT_ECC_BRAINPOOLP512R1, | ||
| 121 | +}; | ||
| 122 | + | ||
| 123 | +DEFINE_OPS_SIGN(Ecdsa, CRYPT_PKEY_ECDSA, CRYPT_MD_SHA256); | ||
| 124 | +DEFINE_BENCH_CTX_PARA_FIXLEN(Ecdsa, g_paraIds, SIZEOF(g_paraIds)); | ||
| @@ -0,0 +1,104 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | +static int32_t MacSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 24 | +{ | ||
| 25 | + (void)ctx; | ||
| 26 | + (void)bench; | ||
| 27 | + (void)ops; | ||
| 28 | + (void)paraId; | ||
| 29 | + return CRYPT_SUCCESS; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +static void MacTearDown(void *ctx) | ||
| 33 | +{ | ||
| 34 | + (void)ctx; | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +static int32_t DoMacCtrl(CRYPT_EAL_MacCtx *mac, int32_t paraId) | ||
| 38 | +{ | ||
| 39 | + if (paraId == CRYPT_MAC_CBC_MAC_SM4) { | ||
| 40 | + // cbc-mac-sm4 only support zeros padding | ||
| 41 | + CRYPT_PaddingType padType = CRYPT_PADDING_ZEROS; | ||
| 42 | + return CRYPT_EAL_MacCtrl(mac, CRYPT_CTRL_SET_CBC_MAC_PADDING, &padType, sizeof(padType)); | ||
| 43 | + } | ||
| 44 | + return CRYPT_SUCCESS; | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +static int32_t DoMac(void *ctx, BenchCtx *bench, BenchOptions *opts, uint32_t keyLen, uint32_t digestLen) | ||
| 48 | +{ | ||
| 49 | + (void)ctx; | ||
| 50 | + (void)bench; | ||
| 51 | + | ||
| 52 | + int32_t rc = CRYPT_SUCCESS; | ||
| 53 | + int32_t paraId = opts->paraId; | ||
| 54 | + uint8_t digest[256]; | ||
| 55 | + CRYPT_EAL_MacCtx *mac = CRYPT_EAL_MacNewCtx(paraId); | ||
| 56 | + if (mac == NULL) { | ||
| 57 | + return CRYPT_ERR_ALGID; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + if ((rc = CRYPT_EAL_MacInit(mac, g_key, keyLen)) != CRYPT_SUCCESS || | ||
| 61 | + (rc = DoMacCtrl(mac, paraId)) != CRYPT_SUCCESS || | ||
| 62 | + (rc = CRYPT_EAL_MacUpdate(mac, g_plain, opts->len)) != CRYPT_SUCCESS || | ||
| 63 | + (rc = CRYPT_EAL_MacFinal(mac, digest, &digestLen)) != CRYPT_SUCCESS) { | ||
| 64 | + printf("do mac init failed\n"); | ||
| 65 | + goto ERR; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | +ERR: | ||
| 69 | + CRYPT_EAL_MacFreeCtx(mac); | ||
| 70 | + return rc; | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +static int32_t MacOneShot(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 74 | +{ | ||
| 75 | + int rc; | ||
| 76 | + int32_t paraId = opts->paraId; | ||
| 77 | + const char *macName = GetAlgName(paraId); | ||
| 78 | + uint32_t keyLen = 16; | ||
| 79 | + uint32_t digestLen = 256; | ||
| 80 | + if (paraId == CRYPT_MAC_CMAC_AES192 || paraId == CRYPT_MAC_GMAC_AES192) { | ||
| 81 | + keyLen = 24; | ||
| 82 | + } | ||
| 83 | + if (paraId == CRYPT_MAC_CMAC_AES256 || paraId == CRYPT_MAC_GMAC_AES256) { | ||
| 84 | + keyLen = 32; | ||
| 85 | + } | ||
| 86 | + if (paraId == CRYPT_MAC_GMAC_AES128 || paraId == CRYPT_MAC_GMAC_AES192 || paraId == CRYPT_MAC_GMAC_AES256) { | ||
| 87 | + digestLen = 16; | ||
| 88 | + } | ||
| 89 | + BENCH_TIMES_VA(DoMac(ctx, bench, opts, keyLen, digestLen), rc, CRYPT_SUCCESS, opts->len, opts->times, "%s mac", | ||
| 90 | + macName); | ||
| 91 | + return rc; | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +static int32_t g_paraIds[] = { | ||
| 95 | + CRYPT_MAC_HMAC_MD5, CRYPT_MAC_HMAC_SHA1, CRYPT_MAC_HMAC_SHA224, CRYPT_MAC_HMAC_SHA256, | ||
| 96 | + CRYPT_MAC_HMAC_SHA384, CRYPT_MAC_HMAC_SHA512, CRYPT_MAC_HMAC_SHA3_224, CRYPT_MAC_HMAC_SHA3_256, | ||
| 97 | + CRYPT_MAC_HMAC_SHA3_384, CRYPT_MAC_HMAC_SHA3_512, CRYPT_MAC_HMAC_SM3, CRYPT_MAC_CMAC_AES128, | ||
| 98 | + CRYPT_MAC_CMAC_AES192, CRYPT_MAC_CMAC_AES256, CRYPT_MAC_CMAC_SM4, CRYPT_MAC_CBC_MAC_SM4, | ||
| 99 | + CRYPT_MAC_GMAC_AES128, CRYPT_MAC_GMAC_AES192, CRYPT_MAC_GMAC_AES256, CRYPT_MAC_SIPHASH64, | ||
| 100 | + CRYPT_MAC_SIPHASH128, | ||
| 101 | +}; | ||
| 102 | + | ||
| 103 | +DEFINE_OPS_MD(Mac); | ||
| 104 | +DEFINE_BENCH_CTX_PARA(Mac, g_paraIds, SIZEOF(g_paraIds)); | ||
| @@ -0,0 +1,58 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | +static int32_t MdSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 24 | +{ | ||
| 25 | + (void)ctx; | ||
| 26 | + (void)bench; | ||
| 27 | + (void)ops; | ||
| 28 | + (void)paraId; | ||
| 29 | + return CRYPT_SUCCESS; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +static void MdTearDown(void *ctx) | ||
| 33 | +{ | ||
| 34 | + (void)ctx; | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +static int32_t MdOneShot(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 38 | +{ | ||
| 39 | + (void)ctx; | ||
| 40 | + int rc; | ||
| 41 | + int32_t paraId = opts->paraId; | ||
| 42 | + const char *mdName = GetAlgName(paraId); | ||
| 43 | + uint8_t digest[64]; // Maximum digest size for supported algorithms | ||
| 44 | + uint32_t digestLen = sizeof(digest); | ||
| 45 | + | ||
| 46 | + BENCH_TIMES_VA(CRYPT_EAL_Md(paraId, g_plain, opts->len, digest, &digestLen), rc, CRYPT_SUCCESS, opts->len, | ||
| 47 | + opts->times, "%s digest", mdName); | ||
| 48 | + return rc; | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +static int32_t g_paraIds[] = { | ||
| 52 | + CRYPT_MD_MD5, CRYPT_MD_SHA1, CRYPT_MD_SHA224, CRYPT_MD_SHA256, CRYPT_MD_SHA384, | ||
| 53 | + CRYPT_MD_SHA512, CRYPT_MD_SHA3_224, CRYPT_MD_SHA3_256, CRYPT_MD_SHA3_384, CRYPT_MD_SHA3_512, | ||
| 54 | + CRYPT_MD_SHAKE128, CRYPT_MD_SHAKE256, CRYPT_MD_SM3, | ||
| 55 | +}; | ||
| 56 | + | ||
| 57 | +DEFINE_OPS_MD(Md); | ||
| 58 | +DEFINE_BENCH_CTX_PARA(Md, g_paraIds, SIZEOF(g_paraIds)); | ||
| @@ -0,0 +1,129 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +const char *GetParaName(int32_t paraId) | ||
| 25 | +{ | ||
| 26 | + switch (paraId) { | ||
| 27 | + case CRYPT_MLDSA_TYPE_MLDSA_44: | ||
| 28 | + return "mldsa-44"; | ||
| 29 | + case CRYPT_MLDSA_TYPE_MLDSA_65: | ||
| 30 | + return "mldsa-65"; | ||
| 31 | + case CRYPT_MLDSA_TYPE_MLDSA_87: | ||
| 32 | + return "mldsa-87"; | ||
| 33 | + default: | ||
| 34 | + break; | ||
| 35 | + } | ||
| 36 | + return ""; | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +static int32_t MldsaSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 40 | +{ | ||
| 41 | + (void)paraId; | ||
| 42 | + CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); | ||
| 43 | + if (pkeyCtx == NULL) { | ||
| 44 | + printf("Failed to create pkey context\n"); | ||
| 45 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 46 | + } | ||
| 47 | + int32_t ret = CRYPT_EAL_PkeyCtrl(pkeyCtx, CRYPT_CTRL_SET_PARA_BY_ID, ¶Id, sizeof(paraId)); | ||
| 48 | + if (ret != CRYPT_SUCCESS) { | ||
| 49 | + printf("Failed to set mldsa alg info.\n"); | ||
| 50 | + return ret; | ||
| 51 | + } | ||
| 52 | + ret = CRYPT_EAL_PkeyGen(pkeyCtx); | ||
| 53 | + if (ret != CRYPT_SUCCESS) { | ||
| 54 | + printf("Failed to gen mldsa key.\n"); | ||
| 55 | + return ret; | ||
| 56 | + } | ||
| 57 | + *ctx = pkeyCtx; | ||
| 58 | + return CRYPT_SUCCESS; | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +static void MldsaTearDown(void *ctx) | ||
| 62 | +{ | ||
| 63 | + CRYPT_EAL_PkeyFreeCtx(ctx); | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +static int32_t MldsaKeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 67 | +{ | ||
| 68 | + int rc = CRYPT_SUCCESS; | ||
| 69 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "%s keyGen", GetParaName(opts->paraId)); | ||
| 70 | + return rc; | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +static int32_t GetHashId(BenchCtx *bench, BenchOptions *opts) | ||
| 74 | +{ | ||
| 75 | + int32_t hashId = bench->ctxOps->hashId; | ||
| 76 | + if (opts->hashId != -1) { | ||
| 77 | + hashId = opts->hashId; | ||
| 78 | + } | ||
| 79 | + return hashId; | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +static int32_t MldsaSignInner(void *ctx, int32_t hashId) | ||
| 83 | +{ | ||
| 84 | + uint8_t plainText[32]; | ||
| 85 | + uint8_t signature[5120]; // ML-DSA can have larger signatures | ||
| 86 | + uint32_t signatureLen = sizeof(signature); | ||
| 87 | + return CRYPT_EAL_PkeySign(ctx, hashId, plainText, sizeof(plainText), signature, &signatureLen); | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +static int32_t MldsaSign(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 91 | +{ | ||
| 92 | + int rc; | ||
| 93 | + int32_t hashId = GetHashId(bench, opts); | ||
| 94 | + if (hashId == -1) { | ||
| 95 | + return -1; | ||
| 96 | + } | ||
| 97 | + BENCH_TIMES_VA(MldsaSignInner(ctx, hashId), rc, CRYPT_SUCCESS, -1, opts->times, "%s sign", | ||
| 98 | + GetParaName(opts->paraId)); | ||
| 99 | + return rc; | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +static int32_t MldsaVerify(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 103 | +{ | ||
| 104 | + int rc; | ||
| 105 | + int32_t hashId = GetHashId(bench, opts); | ||
| 106 | + if (hashId == -1) { | ||
| 107 | + return -1; | ||
| 108 | + } | ||
| 109 | + uint8_t plainText[32]; | ||
| 110 | + uint8_t signature[5120]; // ML-DSA can have larger signatures | ||
| 111 | + uint32_t signatureLen = sizeof(signature); | ||
| 112 | + rc = CRYPT_EAL_PkeySign(ctx, hashId, plainText, sizeof(plainText), signature, &signatureLen); | ||
| 113 | + if (rc != CRYPT_SUCCESS) { | ||
| 114 | + printf("Failed to sign\n"); | ||
| 115 | + return rc; | ||
| 116 | + } | ||
| 117 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyVerify(ctx, hashId, plainText, sizeof(plainText), signature, signatureLen), rc, | ||
| 118 | + CRYPT_SUCCESS, -1, opts->times, "%s verify", GetParaName(opts->paraId)); | ||
| 119 | + return rc; | ||
| 120 | +} | ||
| 121 | + | ||
| 122 | +static int32_t g_paraIds[] = { | ||
| 123 | + CRYPT_MLDSA_TYPE_MLDSA_44, | ||
| 124 | + CRYPT_MLDSA_TYPE_MLDSA_65, | ||
| 125 | + CRYPT_MLDSA_TYPE_MLDSA_87, | ||
| 126 | +}; | ||
| 127 | + | ||
| 128 | +DEFINE_OPS_SIGN(Mldsa, CRYPT_PKEY_ML_DSA, CRYPT_MD_SHA256); | ||
| 129 | +DEFINE_BENCH_CTX_PARA_FIXLEN(Mldsa, g_paraIds, SIZEOF(g_paraIds)); | ||
| @@ -0,0 +1,113 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +static const char *GetParaName(int32_t paraId) | ||
| 25 | +{ | ||
| 26 | + switch (paraId) { | ||
| 27 | + case CRYPT_KEM_TYPE_MLKEM_512: | ||
| 28 | + return "mlkem-512"; | ||
| 29 | + case CRYPT_KEM_TYPE_MLKEM_768: | ||
| 30 | + return "mlkem-768"; | ||
| 31 | + case CRYPT_KEM_TYPE_MLKEM_1024: | ||
| 32 | + return "mlkem-1024"; | ||
| 33 | + default: | ||
| 34 | + return "unknown"; | ||
| 35 | + } | ||
| 36 | + return ""; | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +static int32_t MlkemSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 40 | +{ | ||
| 41 | + (void)paraId; | ||
| 42 | + CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); | ||
| 43 | + if (pkeyCtx == NULL) { | ||
| 44 | + printf("Failed to create pkey context\n"); | ||
| 45 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 46 | + } | ||
| 47 | + int32_t ret = CRYPT_EAL_PkeyCtrl(pkeyCtx, CRYPT_CTRL_SET_PARA_BY_ID, ¶Id, sizeof(paraId)); | ||
| 48 | + if (ret != CRYPT_SUCCESS) { | ||
| 49 | + printf("Failed to set mldsa alg info.\n"); | ||
| 50 | + return ret; | ||
| 51 | + } | ||
| 52 | + ret = CRYPT_EAL_PkeyGen(pkeyCtx); | ||
| 53 | + if (ret != CRYPT_SUCCESS) { | ||
| 54 | + printf("Failed to gen mlkem key.\n"); | ||
| 55 | + return ret; | ||
| 56 | + } | ||
| 57 | + *ctx = pkeyCtx; | ||
| 58 | + return CRYPT_SUCCESS; | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +static void MlkemTearDown(void *ctx) | ||
| 62 | +{ | ||
| 63 | + CRYPT_EAL_PkeyFreeCtx(ctx); | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +static int32_t MlkemKeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 67 | +{ | ||
| 68 | + int rc = CRYPT_SUCCESS; | ||
| 69 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "%s keyGen", GetParaName(opts->paraId)); | ||
| 70 | + return rc; | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +static int32_t MlkemEncaps(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 74 | +{ | ||
| 75 | + int rc; | ||
| 76 | + uint8_t ciphertext[2048]; // ML-KEM can have larger ciphertexts | ||
| 77 | + uint32_t ciphertextLen = sizeof(ciphertext); | ||
| 78 | + uint8_t sharedKey[32]; | ||
| 79 | + uint32_t sharedKeyLen = sizeof(sharedKey); | ||
| 80 | + | ||
| 81 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyEncaps(ctx, ciphertext, &ciphertextLen, sharedKey, &sharedKeyLen), rc, CRYPT_SUCCESS, | ||
| 82 | + -1, opts->times, "%s encaps", GetParaName(opts->paraId)); | ||
| 83 | + return rc; | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +static int32_t MlkemDecaps(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 87 | +{ | ||
| 88 | + int rc; | ||
| 89 | + uint8_t ciphertext[2048]; // ML-KEM can have larger ciphertexts | ||
| 90 | + uint32_t ciphertextLen = sizeof(ciphertext); | ||
| 91 | + uint8_t sharedKey[32]; | ||
| 92 | + uint32_t sharedKeyLen = sizeof(sharedKey); | ||
| 93 | + | ||
| 94 | + // First encap to get a valid ciphertext | ||
| 95 | + rc = CRYPT_EAL_PkeyEncaps(ctx, ciphertext, &ciphertextLen, sharedKey, &sharedKeyLen); | ||
| 96 | + if (rc != CRYPT_SUCCESS) { | ||
| 97 | + printf("Failed to encap\n"); | ||
| 98 | + return rc; | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyDecaps(ctx, ciphertext, ciphertextLen, sharedKey, &sharedKeyLen), rc, CRYPT_SUCCESS, | ||
| 102 | + -1, opts->times, "%s decaps", GetParaName(opts->paraId)); | ||
| 103 | + return rc; | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +static int32_t g_paraIds[] = { | ||
| 107 | + CRYPT_KEM_TYPE_MLKEM_512, | ||
| 108 | + CRYPT_KEM_TYPE_MLKEM_768, | ||
| 109 | + CRYPT_KEM_TYPE_MLKEM_1024, | ||
| 110 | +}; | ||
| 111 | + | ||
| 112 | +DEFINE_OPS_KEM(Mlkem, CRYPT_PKEY_ML_KEM); | ||
| 113 | +DEFINE_BENCH_CTX_PARA_FIXLEN(Mlkem, g_paraIds, SIZEOF(g_paraIds)); | ||
| @@ -0,0 +1,134 @@ | |||
| 1 | +/* | ||
| 2 | + * This file is part of the openHiTLS project. | ||
| 3 | + * | ||
| 4 | + * openHiTLS is licensed under the Mulan PSL v2. | ||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||
| 7 | + * | ||
| 8 | + * http://license.coscl.org.cn/MulanPSL2 | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
| 11 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
| 12 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 13 | + * See the Mulan PSL v2 for more details. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +static int32_t RsaSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) | ||
| 25 | +{ | ||
| 26 | + (void)paraId; | ||
| 27 | + CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); | ||
| 28 | + if (pkeyCtx == NULL) { | ||
| 29 | + printf("Failed to create pkey context\n"); | ||
| 30 | + return CRYPT_MEM_ALLOC_FAIL; | ||
| 31 | + } | ||
| 32 | + int32_t ret = CRYPT_EAL_PkeyGen(pkeyCtx); | ||
| 33 | + if (ret != CRYPT_SUCCESS) { | ||
| 34 | + printf("Failed to gen rsa key.\n"); | ||
| 35 | + return ret; | ||
| 36 | + } | ||
| 37 | + *ctx = pkeyCtx; | ||
| 38 | + return CRYPT_SUCCESS; | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +static void RsaTearDown(void *ctx) | ||
| 42 | +{ | ||
| 43 | + CRYPT_EAL_PkeyFreeCtx(ctx); | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +static int32_t RsaKeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 47 | +{ | ||
| 48 | + int rc = CRYPT_SUCCESS; | ||
| 49 | + BENCH_TIMES(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "rsa keyGen"); | ||
| 50 | + return rc; | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +static int32_t RsaEncInner(void *ctx) | ||
| 54 | +{ | ||
| 55 | + uint8_t plainText[32]; | ||
| 56 | + uint8_t cipherText[512]; // RSA can have larger output | ||
| 57 | + uint32_t outLen = sizeof(cipherText); | ||
| 58 | + return CRYPT_EAL_PkeyEncrypt(ctx, plainText, sizeof(plainText), cipherText, &outLen); | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +static int32_t RsaEnc(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 62 | +{ | ||
| 63 | + int rc = CRYPT_SUCCESS; | ||
| 64 | + BENCH_TIMES(RsaEncInner(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "rsa enc"); | ||
| 65 | + return rc; | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +static int32_t RsaDec(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 69 | +{ | ||
| 70 | + int rc; | ||
| 71 | + uint8_t plainText[32]; | ||
| 72 | + uint32_t plainTextLen = sizeof(plainText); | ||
| 73 | + uint8_t cipherText[512]; // RSA can have larger output | ||
| 74 | + uint32_t outLen = sizeof(cipherText); | ||
| 75 | + rc = CRYPT_EAL_PkeyEncrypt(ctx, plainText, sizeof(plainText), cipherText, &outLen); | ||
| 76 | + if (rc != CRYPT_SUCCESS) { | ||
| 77 | + printf("Failed to encrypt\n"); | ||
| 78 | + return rc; | ||
| 79 | + } | ||
| 80 | + BENCH_TIMES(CRYPT_EAL_PkeyDecrypt(ctx, cipherText, outLen, plainText, &plainTextLen), rc, CRYPT_SUCCESS, -1, | ||
| 81 | + opts->times, "rsa dec"); | ||
| 82 | + return rc; | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +static int32_t GetHashId(BenchCtx *bench, BenchOptions *opts) | ||
| 86 | +{ | ||
| 87 | + int32_t hashId = bench->ctxOps->hashId; | ||
| 88 | + if (opts->hashId != -1) { | ||
| 89 | + hashId = opts->hashId; | ||
| 90 | + } | ||
| 91 | + return hashId; | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +static int32_t RsaSignInner(void *ctx, int32_t hashId) | ||
| 95 | +{ | ||
| 96 | + uint8_t plainText[32]; | ||
| 97 | + uint8_t signature[512]; // RSA can have larger signatures | ||
| 98 | + uint32_t signatureLen = sizeof(signature); | ||
| 99 | + return CRYPT_EAL_PkeySign(ctx, hashId, plainText, sizeof(plainText), signature, &signatureLen); | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +static int32_t RsaSign(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 103 | +{ | ||
| 104 | + int rc; | ||
| 105 | + int32_t hashId = GetHashId(bench, opts); | ||
| 106 | + if (hashId == -1) { | ||
| 107 | + return -1; | ||
| 108 | + } | ||
| 109 | + BENCH_TIMES(RsaSignInner(ctx, hashId), rc, CRYPT_SUCCESS, -1, opts->times, "rsa sign"); | ||
| 110 | + return rc; | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +static int32_t RsaVerify(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 114 | +{ | ||
| 115 | + int rc; | ||
| 116 | + int32_t hashId = GetHashId(bench, opts); | ||
| 117 | + if (hashId == -1) { | ||
| 118 | + return -1; | ||
| 119 | + } | ||
| 120 | + uint8_t plainText[32]; | ||
| 121 | + uint8_t signature[512]; // RSA can have larger signatures | ||
| 122 | + uint32_t signatureLen = sizeof(signature); | ||
| 123 | + rc = CRYPT_EAL_PkeySign(ctx, hashId, plainText, sizeof(plainText), signature, &signatureLen); | ||
| 124 | + if (rc != CRYPT_SUCCESS) { | ||
| 125 | + printf("Failed to sign\n"); | ||
| 126 | + return rc; | ||
| 127 | + } | ||
| 128 | + BENCH_TIMES(CRYPT_EAL_PkeyVerify(ctx, hashId, plainText, sizeof(plainText), signature, signatureLen), rc, | ||
| 129 | + CRYPT_SUCCESS, -1, opts->times, "rsa verify"); | ||
| 130 | + return rc; | ||
| 131 | +} | ||
| 132 | + | ||
| 133 | +DEFINE_OPS(Rsa, CRYPT_PKEY_RSA, CRYPT_MD_SHA256); | ||
| 134 | +DEFINE_BENCH_CTX_FIXLEN(Rsa); | ||
| @@ -20,97 +20,92 @@ | |||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | -static const char *SlhDsaHeader(BenchCtx *bench) | 23 | +static int32_t SlhDsaSetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) |
| 24 | -{ | ||
| 25 | - switch (bench->ctxOps->subAlgId) { | ||
| 26 | - case CRYPT_SLH_DSA_SHA2_128S: | ||
| 27 | - return "SLH-DSA-SHA2-128S"; | ||
| 28 | - case CRYPT_SLH_DSA_SHAKE_128S: | ||
| 29 | - return "SLH-DSA-SHAKE-128S"; | ||
| 30 | - case CRYPT_SLH_DSA_SHA2_128F: | ||
| 31 | - return "SLH-DSA-SHA2-128F"; | ||
| 32 | - case CRYPT_SLH_DSA_SHAKE_128F: | ||
| 33 | - return "SLH-DSA-SHAKE-128F"; | ||
| 34 | - case CRYPT_SLH_DSA_SHA2_192S: | ||
| 35 | - return "SLH-DSA-SHA2-192S"; | ||
| 36 | - case CRYPT_SLH_DSA_SHAKE_192S: | ||
| 37 | - return "SLH-DSA-SHAKE-192S"; | ||
| 38 | - case CRYPT_SLH_DSA_SHA2_192F: | ||
| 39 | - return "SLH-DSA-SHA2-192F"; | ||
| 40 | - case CRYPT_SLH_DSA_SHAKE_192F: | ||
| 41 | - return "SLH-DSA-SHAKE-192F"; | ||
| 42 | - case CRYPT_SLH_DSA_SHA2_256S: | ||
| 43 | - return "SLH-DSA-SHA2-256S"; | ||
| 44 | - case CRYPT_SLH_DSA_SHAKE_256S: | ||
| 45 | - return "SLH-DSA-SHAKE-256S"; | ||
| 46 | - case CRYPT_SLH_DSA_SHA2_256F: | ||
| 47 | - return "SLH-DSA-SHA2-256F"; | ||
| 48 | - case CRYPT_SLH_DSA_SHAKE_256F: | ||
| 49 | - return "SLH-DSA-SHAKE_256F"; | ||
| 50 | - case CRYPT_SLH_DSA_ALG_ID_MAX: | ||
| 51 | - break; | ||
| 52 | - } | ||
| 53 | - return ""; | ||
| 54 | -} | ||
| 55 | - | ||
| 56 | -static int32_t SlhDsaNewCtx(void **ctx, const CtxOps *ops) | ||
| 57 | { | 24 | { |
| 58 | CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); | 25 | CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); |
| 59 | if (pkeyCtx == NULL) { | 26 | if (pkeyCtx == NULL) { |
| 60 | printf("Failed to create pkey context\n"); | 27 | printf("Failed to create pkey context\n"); |
| 61 | return CRYPT_MEM_ALLOC_FAIL; | 28 | return CRYPT_MEM_ALLOC_FAIL; |
| 62 | } | 29 | } |
| 30 | + int rc = CRYPT_SUCCESS; | ||
| 31 | + CRYPT_SLH_DSA_AlgId algId = paraId; | ||
| 32 | + rc = CRYPT_EAL_PkeyCtrl(pkeyCtx, CRYPT_CTRL_SET_PARA_BY_ID, (void *)&algId, sizeof(algId)); | ||
| 33 | + if (rc != CRYPT_SUCCESS) { | ||
| 34 | + return rc; | ||
| 35 | + } | ||
| 36 | + rc = CRYPT_EAL_PkeyGen(pkeyCtx); | ||
| 37 | + if (rc != CRYPT_SUCCESS) { | ||
| 38 | + printf("Failed to gen slhdsa key.\n"); | ||
| 39 | + return rc; | ||
| 40 | + } | ||
| 63 | *ctx = pkeyCtx; | 41 | *ctx = pkeyCtx; |
| 64 | return CRYPT_SUCCESS; | 42 | return CRYPT_SUCCESS; |
| 65 | } | 43 | } |
| 66 | 44 | ||
| 67 | -static void SlhDsaFreeCtx(void *ctx) | 45 | +static void SlhDsaTearDown(void *ctx) |
| 68 | { | 46 | { |
| 69 | CRYPT_EAL_PkeyFreeCtx(ctx); | 47 | CRYPT_EAL_PkeyFreeCtx(ctx); |
| 70 | } | 48 | } |
| 71 | 49 | ||
| 72 | -static int32_t SlhDsaKeyGen(void *ctx, BenchCtx *bench) | 50 | +static int32_t SlhDsaKeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) |
| 73 | { | 51 | { |
| 74 | int rc = CRYPT_SUCCESS; | 52 | int rc = CRYPT_SUCCESS; |
| 75 | - CRYPT_SLH_DSA_AlgId algId = bench->ctxOps->subAlgId; | 53 | + CRYPT_SLH_DSA_AlgId paraId = opts->paraId; |
| 76 | - rc = CRYPT_EAL_PkeyCtrl(ctx, CRYPT_CTRL_SET_SLH_DSA_ALG_ID, (void *)&algId, sizeof(algId)); | 54 | + |
| 55 | + rc = CRYPT_EAL_PkeyCtrl(ctx, CRYPT_CTRL_SET_PARA_BY_ID, (void *)¶Id, sizeof(paraId)); | ||
| 77 | if (rc != CRYPT_SUCCESS) { | 56 | if (rc != CRYPT_SUCCESS) { |
| 78 | return rc; | 57 | return rc; |
| 79 | } | 58 | } |
| 80 | - BENCH_TIMES_VA(rc = CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, bench->times, "%s keyGen", SlhDsaHeader(bench)); | 59 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "%s keyGen", GetAlgName(paraId)); |
| 81 | return rc; | 60 | return rc; |
| 82 | } | 61 | } |
| 83 | 62 | ||
| 84 | -static int32_t SlhDsaSignInner(void *ctx) | 63 | +static int32_t GetHashId(BenchCtx *bench, BenchOptions *opts) |
| 85 | { | 64 | { |
| 86 | - uint8_t plainText[32] = {0}; | 65 | + int32_t hashId = bench->ctxOps->hashId; |
| 87 | - uint8_t signature[51200]; // maximum len is 49856 | 66 | + if (opts->hashId != -1) { |
| 88 | - uint32_t signatureLen = sizeof(signature); | 67 | + hashId = opts->hashId; |
| 89 | - return CRYPT_EAL_PkeySign(ctx, CRYPT_MD_SHA256, plainText, sizeof(plainText), signature, &signatureLen); | 68 | + } |
| 69 | + return hashId; | ||
| 90 | } | 70 | } |
| 91 | 71 | ||
| 92 | -static int32_t SlhDsaSign(void *ctx, BenchCtx *bench) | 72 | +static int32_t SlhDsaSignInner(void *ctx, int32_t hashId, int32_t len) |
| 73 | +{ | ||
| 74 | + static uint8_t sign[51200]; // maximum len is 49856 | ||
| 75 | + uint32_t signLen = sizeof(sign); | ||
| 76 | + return CRYPT_EAL_PkeySign(ctx, hashId, g_plain, len, sign, &signLen); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +static int32_t SlhDsaSign(void *ctx, BenchCtx *bench, BenchOptions *opts) | ||
| 93 | { | 80 | { |
| 94 | int rc; | 81 | int rc; |
| 95 | - BENCH_TIMES_VA(SlhDsaSignInner(ctx), rc, CRYPT_SUCCESS, bench->times, "%s sign", SlhDsaHeader(bench)); | 82 | + int32_t hashId = GetHashId(bench, opts); |
| 83 | + BENCH_TIMES_VA(SlhDsaSignInner(ctx, hashId, opts->len), rc, CRYPT_SUCCESS, opts->len, opts->times, "%s sign", | ||
| 84 | + GetAlgName(opts->paraId)); | ||
| 96 | return rc; | 85 | return rc; |
| 97 | } | 86 | } |
| 98 | 87 | ||
| 99 | -static int32_t SlhDsaVerify(void *ctx, BenchCtx *bench) | 88 | +static int32_t SlhDsaVerify(void *ctx, BenchCtx *bench, BenchOptions *opts) |
| 100 | { | 89 | { |
| 101 | int rc; | 90 | int rc; |
| 102 | - uint8_t plainText[32] = {0}; | 91 | + static uint8_t sign[51200]; // maximum len is 49856 |
| 103 | - uint8_t signature[51200]; // maximum len is 49856 | 92 | + uint32_t signLen = sizeof(sign); |
| 104 | - uint32_t signatureLen = sizeof(signature); | 93 | + int32_t hashId = GetHashId(bench, opts); |
| 105 | - rc = CRYPT_EAL_PkeySign(ctx, CRYPT_MD_SHA256, plainText, sizeof(plainText), signature, &signatureLen); | 94 | + rc = CRYPT_EAL_PkeySign(ctx, hashId, g_plain, opts->len, sign, &signLen); |
| 106 | if (rc != CRYPT_SUCCESS) { | 95 | if (rc != CRYPT_SUCCESS) { |
| 107 | printf("Failed to sign\n"); | 96 | printf("Failed to sign\n"); |
| 108 | return rc; | 97 | return rc; |
| 109 | } | 98 | } |
| 110 | - BENCH_TIMES_VA(CRYPT_EAL_PkeyVerify(ctx, CRYPT_MD_SHA256, plainText, sizeof(plainText), signature, signatureLen), rc, | 99 | + BENCH_TIMES_VA(CRYPT_EAL_PkeyVerify(ctx, hashId, g_plain, opts->len, sign, signLen), rc, CRYPT_SUCCESS, opts->len, |
| 111 | - CRYPT_SUCCESS, bench->times, "%s verify", SlhDsaHeader(bench)); | 100 | + opts->times, "%s verify", GetAlgName(opts->paraId)); |
| 112 | return rc; | 101 | return rc; |
| 113 | } | 102 | } |
| 114 | 103 | ||
| 115 | -DEFINE_OPS_SIGN_ALG(SlhDsa, CRYPT_PKEY_SLH_DSA, CRYPT_SLH_DSA_SHA2_128S); | 104 | +static int32_t g_paraIds[] = { |
| 116 | -DEFINE_BENCH_CTX_TIMES(SlhDsa, 100); | 105 | + CRYPT_SLH_DSA_SHA2_128S, CRYPT_SLH_DSA_SHAKE_128S, CRYPT_SLH_DSA_SHA2_128F, CRYPT_SLH_DSA_SHAKE_128F, |
| 106 | + CRYPT_SLH_DSA_SHA2_192S, CRYPT_SLH_DSA_SHAKE_192S, CRYPT_SLH_DSA_SHA2_192F, CRYPT_SLH_DSA_SHAKE_192F, | ||
| 107 | + CRYPT_SLH_DSA_SHA2_256S, CRYPT_SLH_DSA_SHAKE_256S, CRYPT_SLH_DSA_SHA2_256F, CRYPT_SLH_DSA_SHAKE_256F, | ||
| 108 | +}; | ||
| 109 | + | ||
| 110 | +DEFINE_OPS_SIGN(SlhDsa, CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHA256); | ||
| 111 | +DEFINE_BENCH_CTX_PARA_FIXLEN(SlhDsa, g_paraIds, SIZEOF(g_paraIds)); | ||
| @@ -18,29 +18,35 @@ | |||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | + | ||
| 21 | 22 | ||
| 22 | 23 | ||
| 23 | - | 24 | +static int32_t Sm2SetUp(void **ctx, BenchCtx *bench, const CtxOps *ops, int32_t paraId) |
| 24 | -static int32_t Sm2NewCtx(void **ctx, const CtxOps *ops) | ||
| 25 | { | 25 | { |
| 26 | + (void)paraId; | ||
| 26 | CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); | 27 | CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(ops->algId); |
| 27 | if (pkeyCtx == NULL) { | 28 | if (pkeyCtx == NULL) { |
| 28 | printf("Failed to create pkey context\n"); | 29 | printf("Failed to create pkey context\n"); |
| 29 | return CRYPT_MEM_ALLOC_FAIL; | 30 | return CRYPT_MEM_ALLOC_FAIL; |
| 30 | } | 31 | } |
| 32 | + int32_t ret = CRYPT_EAL_PkeyGen(pkeyCtx); | ||
| 33 | + if (ret != CRYPT_SUCCESS) { | ||
| 34 | + printf("Failed to gen sm2 key.\n"); | ||
| 35 | + return ret; | ||
| 36 | + } | ||
| 31 | *ctx = pkeyCtx; | 37 | *ctx = pkeyCtx; |
| 32 | return CRYPT_SUCCESS; | 38 | return CRYPT_SUCCESS; |
| 33 | } | 39 | } |
| 34 | 40 | ||
| 35 | -static void Sm2FreeCtx(void *ctx) | 41 | +static void Sm2TearDown(void *ctx) |
| 36 | { | 42 | { |
| 37 | CRYPT_EAL_PkeyFreeCtx(ctx); | 43 | CRYPT_EAL_PkeyFreeCtx(ctx); |
| 38 | } | 44 | } |
| 39 | 45 | ||
| 40 | -static int32_t Sm2KeyGen(void *ctx, BenchCtx *bench) | 46 | +static int32_t Sm2KeyGen(void *ctx, BenchCtx *bench, BenchOptions *opts) |
| 41 | { | 47 | { |
| 42 | int rc = CRYPT_SUCCESS; | 48 | int rc = CRYPT_SUCCESS; |
| 43 | - BENCH_TIMES(rc = CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, bench->times, "sm2 keyGen"); | 49 | + BENCH_TIMES(CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "sm2 keyGen"); |
| 44 | return rc; | 50 | return rc; |
| 45 | } | 51 | } |
| 46 | 52 | ||
| @@ -63,7 +69,7 @@ static int32_t Sm2KeyDeriveInner(void *ctx, void *peerCtx) | |||
| 63 | return CRYPT_SUCCESS; | 69 | return CRYPT_SUCCESS; |
| 64 | } | 70 | } |
| 65 | 71 | ||
| 66 | -static int32_t Sm2KeyDerive(void *ctx, BenchCtx *bench) | 72 | +static int32_t Sm2KeyDerive(void *ctx, BenchCtx *bench, BenchOptions *opts) |
| 67 | { | 73 | { |
| 68 | int rc = CRYPT_SUCCESS; | 74 | int rc = CRYPT_SUCCESS; |
| 69 | CRYPT_EAL_PkeyCtx *peerCtx = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_SM2); | 75 | CRYPT_EAL_PkeyCtx *peerCtx = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_SM2); |
| @@ -84,7 +90,7 @@ static int32_t Sm2KeyDerive(void *ctx, BenchCtx *bench) | |||
| 84 | printf("Failed to set R\n"); | 90 | printf("Failed to set R\n"); |
| 85 | goto ERR_OUT; | 91 | goto ERR_OUT; |
| 86 | } | 92 | } |
| 87 | - BENCH_TIMES(Sm2KeyDeriveInner(ctx, peerCtx), rc, CRYPT_SUCCESS, bench->times, "sm2 keyDerive"); | 93 | + BENCH_TIMES(Sm2KeyDeriveInner(ctx, peerCtx), rc, CRYPT_SUCCESS, -1, opts->times, "sm2 keyDerive"); |
| 88 | ERR_OUT: | 94 | ERR_OUT: |
| 89 | CRYPT_EAL_PkeyFreeCtx(peerCtx); | 95 | CRYPT_EAL_PkeyFreeCtx(peerCtx); |
| 90 | return rc; | 96 | return rc; |
| @@ -98,14 +104,14 @@ static int32_t Sm2EncInner(void *ctx) | |||
| 98 | return CRYPT_EAL_PkeyEncrypt(ctx, plainText, sizeof(plainText), cipherText, &outLen); | 104 | return CRYPT_EAL_PkeyEncrypt(ctx, plainText, sizeof(plainText), cipherText, &outLen); |
| 99 | } | 105 | } |
| 100 | 106 | ||
| 101 | -static int32_t Sm2Enc(void *ctx, BenchCtx *bench) | 107 | +static int32_t Sm2Enc(void *ctx, BenchCtx *bench, BenchOptions *opts) |
| 102 | { | 108 | { |
| 103 | int rc = CRYPT_SUCCESS; | 109 | int rc = CRYPT_SUCCESS; |
| 104 | - BENCH_TIMES(Sm2EncInner(ctx), rc, CRYPT_SUCCESS, bench->times, "sm2 enc"); | 110 | + BENCH_TIMES(Sm2EncInner(ctx), rc, CRYPT_SUCCESS, -1, opts->times, "sm2 enc"); |
| 105 | return rc; | 111 | return rc; |
| 106 | } | 112 | } |
| 107 | 113 | ||
| 108 | -static int32_t Sm2Dec(void *ctx, BenchCtx *bench) | 114 | +static int32_t Sm2Dec(void *ctx, BenchCtx *bench, BenchOptions *opts) |
| 109 | { | 115 | { |
| 110 | int rc; | 116 | int rc; |
| 111 | uint8_t plainText[32]; | 117 | uint8_t plainText[32]; |
| @@ -117,41 +123,62 @@ static int32_t Sm2Dec(void *ctx, BenchCtx *bench) | |||
| 117 | printf("Failed to encrypt\n"); | 123 | printf("Failed to encrypt\n"); |
| 118 | return rc; | 124 | return rc; |
| 119 | } | 125 | } |
| 120 | - BENCH_TIMES(CRYPT_EAL_PkeyDecrypt(ctx, cipherText, outLen, plainText, &plainTextLen), rc, CRYPT_SUCCESS, bench->times, | 126 | + BENCH_TIMES(CRYPT_EAL_PkeyDecrypt(ctx, cipherText, outLen, plainText, &plainTextLen), rc, CRYPT_SUCCESS, -1, |
| 121 | - "sm2 dec"); | 127 | + opts->times, "sm2 dec"); |
| 122 | return rc; | 128 | return rc; |
| 123 | } | 129 | } |
| 124 | 130 | ||
| 125 | -static int32_t Sm2SignInner(void *ctx) | 131 | +static int32_t GetHashId(BenchCtx *bench, BenchOptions *opts) |
| 132 | +{ | ||
| 133 | + int32_t hashId = bench->ctxOps->hashId; | ||
| 134 | + if (opts->hashId != -1) { | ||
| 135 | + if (CRYPT_EAL_MdGetDigestSize(opts->hashId) != 32) { | ||
| 136 | + printf("Wrong Hash Algorithm Id for Sm2."); | ||
| 137 | + return -1; | ||
| 138 | + } | ||
| 139 | + hashId = opts->hashId; | ||
| 140 | + } | ||
| 141 | + return hashId; | ||
| 142 | +} | ||
| 143 | + | ||
| 144 | +static int32_t Sm2SignInner(void *ctx, int32_t hashId) | ||
| 126 | { | 145 | { |
| 127 | uint8_t plainText[32]; | 146 | uint8_t plainText[32]; |
| 128 | uint8_t signature[256]; | 147 | uint8_t signature[256]; |
| 129 | uint32_t signatureLen = sizeof(signature); | 148 | uint32_t signatureLen = sizeof(signature); |
| 130 | - return CRYPT_EAL_PkeySign(ctx, CRYPT_MD_SM3, plainText, sizeof(plainText), signature, &signatureLen); | 149 | + return CRYPT_EAL_PkeySign(ctx, hashId, plainText, sizeof(plainText), signature, &signatureLen); |
| 131 | } | 150 | } |
| 132 | 151 | ||
| 133 | -static int32_t Sm2Sign(void *ctx, BenchCtx *bench) | 152 | +static int32_t Sm2Sign(void *ctx, BenchCtx *bench, BenchOptions *opts) |
| 134 | { | 153 | { |
| 135 | int rc; | 154 | int rc; |
| 136 | - BENCH_TIMES(Sm2SignInner(ctx), rc, CRYPT_SUCCESS, bench->times, "sm2 sign"); | 155 | + int32_t hashId = GetHashId(bench, opts); |
| 156 | + if (hashId == -1) { | ||
| 157 | + return -1; | ||
| 158 | + } | ||
| 159 | + BENCH_TIMES(Sm2SignInner(ctx, hashId), rc, CRYPT_SUCCESS, -1, opts->times, "sm2 sign"); | ||
| 137 | return rc; | 160 | return rc; |
| 138 | } | 161 | } |
| 139 | 162 | ||
| 140 | -static int32_t Sm2Verify(void *ctx, BenchCtx *bench) | 163 | +static int32_t Sm2Verify(void *ctx, BenchCtx *bench, BenchOptions *opts) |
| 141 | { | 164 | { |
| 142 | int rc; | 165 | int rc; |
| 166 | + int32_t hashId = GetHashId(bench, opts); | ||
| 167 | + if (hashId == -1) { | ||
| 168 | + return -1; | ||
| 169 | + } | ||
| 143 | uint8_t plainText[32]; | 170 | uint8_t plainText[32]; |
| 144 | uint8_t signature[256]; | 171 | uint8_t signature[256]; |
| 145 | uint32_t signatureLen = sizeof(signature); | 172 | uint32_t signatureLen = sizeof(signature); |
| 146 | - rc = CRYPT_EAL_PkeySign(ctx, CRYPT_MD_SM3, plainText, sizeof(plainText), signature, &signatureLen); | 173 | + rc = CRYPT_EAL_PkeySign(ctx, hashId, plainText, sizeof(plainText), signature, &signatureLen); |
| 147 | if (rc != CRYPT_SUCCESS) { | 174 | if (rc != CRYPT_SUCCESS) { |
| 148 | printf("Failed to sign\n"); | 175 | printf("Failed to sign\n"); |
| 149 | return rc; | 176 | return rc; |
| 150 | } | 177 | } |
| 151 | - BENCH_TIMES(CRYPT_EAL_PkeyVerify(ctx, CRYPT_MD_SM3, plainText, sizeof(plainText), signature, signatureLen), rc, | 178 | + BENCH_TIMES(CRYPT_EAL_PkeyVerify(ctx, hashId, plainText, sizeof(plainText), signature, signatureLen), rc, |
| 152 | - CRYPT_SUCCESS, bench->times, "sm2 verify"); | 179 | + CRYPT_SUCCESS, -1, opts->times, "sm2 verify"); |
| 153 | return rc; | 180 | return rc; |
| 154 | } | 181 | } |
| 155 | 182 | ||
| 156 | -DEFINE_OPS(Sm2, CRYPT_PKEY_SM2); | 183 | +DEFINE_OPS(Sm2, CRYPT_PKEY_SM2, CRYPT_MD_SM3); |
| 157 | -DEFINE_BENCH_CTX(Sm2); | 184 | +DEFINE_BENCH_CTX_FIXLEN(Sm2); |