|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 wolfSSL Inc. |
| 3 | + * |
| 4 | + * This file is part of wolfHSM. |
| 5 | + * |
| 6 | + * wolfHSM is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * wolfHSM is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with wolfHSM. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "wh_bench_mod.h" |
| 21 | +#include "wolfhsm/wh_error.h" |
| 22 | +#include "wolfhsm/wh_client.h" |
| 23 | +#include "wolfhsm/wh_client_crypto.h" |
| 24 | + |
| 25 | +#include "wolfssl/wolfcrypt/cmac.h" |
| 26 | +#include "wolfssl/wolfcrypt/kdf.h" |
| 27 | + |
| 28 | +#if defined(WOLFHSM_CFG_BENCH_ENABLE) |
| 29 | + |
| 30 | +#if defined(HAVE_CMAC_KDF) && defined(WOLFSSL_CMAC) |
| 31 | + |
| 32 | +#define WH_BENCH_CMAC_KDF_OUT_SIZE 40 |
| 33 | + |
| 34 | +static int _benchCmacKdf(whClientContext* client, whBenchOpContext* ctx, int id, |
| 35 | + int devId) |
| 36 | +{ |
| 37 | + /* Derivation inputs mirror the unit test vectors to provide realistic |
| 38 | + * message sizes while keeping the benchmark deterministic. */ |
| 39 | + static const uint8_t cmacKdfSalt[] = { |
| 40 | + 0x20, 0x51, 0xaf, 0x34, 0x76, 0x2e, 0xbe, 0x55, 0x6f, 0x72, 0xa5, 0xc6, |
| 41 | + 0xed, 0xc7, 0x77, 0x1e, 0xb9, 0x24, 0x5f, 0xad, 0x76, 0xf0, 0x34, 0xbe}; |
| 42 | + static const uint8_t cmacKdfZ[] = { |
| 43 | + 0xae, 0x8e, 0x93, 0xc9, 0xc9, 0x91, 0xcf, 0x89, 0x6a, 0x49, 0x1a, |
| 44 | + 0x89, 0x07, 0xdf, 0x4e, 0x4b, 0xe5, 0x18, 0x6a, 0xe4, 0x96, 0xcd, |
| 45 | + 0x34, 0x0d, 0xc1, 0x9b, 0x23, 0x78, 0x21, 0xdb, 0x7b, 0x60}; |
| 46 | + static const uint8_t cmacKdfFixedInfo[] = { |
| 47 | + 0xa2, 0x59, 0xca, 0xe2, 0xc4, 0xa3, 0x6b, 0x89, 0x56, 0x3c, 0xb1, 0x48, |
| 48 | + 0xc7, 0x82, 0x51, 0x34, 0x3b, 0xbf, 0xab, 0xdc, 0x13, 0xca, 0x7a, 0xc2, |
| 49 | + 0x17, 0x1c, 0x2e, 0xb6, 0x02, 0x1f, 0x44, 0x77, 0xfe, 0xa3, 0x3b, 0x28, |
| 50 | + 0x72, 0x4d, 0xa7, 0x21, 0xee, 0x08, 0x7b, 0xff, 0xd7, 0x94, 0xa1, 0x56, |
| 51 | + 0x37, 0x54, 0xb4, 0x25, 0xa8, 0xd0, 0x9b, 0x3e, 0x0d, 0xa5, 0xff, 0xed}; |
| 52 | + static const uint8_t label[] = "cmac-kdf-bench"; |
| 53 | + |
| 54 | + int ret = 0; |
| 55 | + whKeyId keyId; |
| 56 | + int i; |
| 57 | + |
| 58 | + (void)devId; |
| 59 | + |
| 60 | + for (i = 0; i < WOLFHSM_CFG_BENCH_KG_ITERS && ret == 0; i++) { |
| 61 | + int benchStartRet; |
| 62 | + int benchStopRet; |
| 63 | + |
| 64 | + keyId = WH_KEYID_ERASED; |
| 65 | + |
| 66 | + benchStartRet = wh_Bench_StartOp(ctx, id); |
| 67 | + ret = wh_Client_CmacKdfMakeCacheKey( |
| 68 | + client, WH_KEYID_ERASED, cmacKdfSalt, (uint32_t)sizeof(cmacKdfSalt), |
| 69 | + WH_KEYID_ERASED, cmacKdfZ, (uint32_t)sizeof(cmacKdfZ), |
| 70 | + cmacKdfFixedInfo, (uint32_t)sizeof(cmacKdfFixedInfo), &keyId, |
| 71 | + WH_NVM_FLAGS_NONE, label, (uint32_t)sizeof(label), |
| 72 | + WH_BENCH_CMAC_KDF_OUT_SIZE); |
| 73 | + benchStopRet = wh_Bench_StopOp(ctx, id); |
| 74 | + |
| 75 | + if (benchStartRet != 0) { |
| 76 | + WH_BENCH_PRINTF("Failed to wh_Bench_StartOp %d\n", benchStartRet); |
| 77 | + ret = benchStartRet; |
| 78 | + break; |
| 79 | + } |
| 80 | + if (ret != 0) { |
| 81 | + WH_BENCH_PRINTF("Failed to wh_Client_CmacKdfMakeCacheKey %d\n", |
| 82 | + ret); |
| 83 | + break; |
| 84 | + } |
| 85 | + if (benchStopRet != 0) { |
| 86 | + WH_BENCH_PRINTF("Failed to wh_Bench_StopOp %d\n", benchStopRet); |
| 87 | + ret = benchStopRet; |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + /* Evict the cached key to free resources for next iteration */ |
| 92 | + ret = wh_Client_KeyEvict(client, keyId); |
| 93 | + if (ret != 0) { |
| 94 | + WH_BENCH_PRINTF("Failed to wh_Client_KeyEvict %d\n", ret); |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return ret; |
| 100 | +} |
| 101 | + |
| 102 | +int wh_Bench_Mod_CmacKdf(whClientContext* client, whBenchOpContext* ctx, int id, |
| 103 | + void* params) |
| 104 | +{ |
| 105 | + (void)params; |
| 106 | + return _benchCmacKdf(client, ctx, id, WH_DEV_ID); |
| 107 | +} |
| 108 | + |
| 109 | +#endif /* HAVE_CMAC_KDF && WOLFSSL_CMAC */ |
| 110 | + |
| 111 | +#endif /* WOLFHSM_CFG_BENCH_ENABLE */ |
0 commit comments