|
| 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 "wolfhsm/wh_settings.h" |
| 21 | +#ifdef WOLFHSM_CFG_WRAPKEY |
| 22 | +#include <stdint.h> |
| 23 | +#include <stdio.h> |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +#include "wolfhsm/wh_common.h" |
| 27 | +#include "wolfhsm/wh_error.h" |
| 28 | +#include "wolfhsm/wh_client.h" |
| 29 | +#include "wolfhsm/wh_client_crypto.h" |
| 30 | +#include "wolfhsm/wh_client_wrapkey.h" |
| 31 | + |
| 32 | +#include "wolfssl/wolfcrypt/settings.h" |
| 33 | +#include "wolfssl/wolfcrypt/aes.h" |
| 34 | +#include "wolfssl/wolfcrypt/random.h" |
| 35 | + |
| 36 | +#include "wh_demo_client_wrapkey.h" |
| 37 | + |
| 38 | +#ifndef NO_AES |
| 39 | +#define HAVE_AESGCM |
| 40 | +#ifdef HAVE_AESGCM |
| 41 | +int wh_DemoClient_AesGcmWrapKeyBasic(whClientContext* ctx, WC_RNG* rng) |
| 42 | +{ |
| 43 | + |
| 44 | +#define WH_TEST_AES_KEYSIZE 16 |
| 45 | +#define WH_TEST_AES_TEXTSIZE 16 |
| 46 | +#define WH_TEST_AES_AUTHSIZE 16 |
| 47 | +#define WH_TEST_AES_TAGSIZE 16 |
| 48 | +#define WH_TEST_AES_WRAPPED_KEYSIZE \ |
| 49 | + (WH_TEST_AES_AUTHSIZE + WH_TEST_AES_TAGSIZE + WH_TEST_AES_KEYSIZE + \ |
| 50 | + sizeof(whNvmMetadata)) |
| 51 | + |
| 52 | + int ret = 0; |
| 53 | + uint8_t iv[AES_BLOCK_SIZE]; |
| 54 | + uint8_t key[WH_TEST_AES_KEYSIZE]; |
| 55 | + uint8_t plainKey[WH_TEST_AES_KEYSIZE]; |
| 56 | + uint8_t tmpPlainKey[WH_TEST_AES_KEYSIZE]; |
| 57 | + uint8_t wrappedKey[WH_TEST_AES_WRAPPED_KEYSIZE]; |
| 58 | + uint8_t label[WH_NVM_LABEL_LEN] = "Server AES Key Label"; |
| 59 | + whKeyId serverKeyId; |
| 60 | + whKeyId wrappedKeyId; |
| 61 | + whNvmMetadata metadata = {.label = "AES Key Label", |
| 62 | + .access = WH_NVM_ACCESS_ANY, |
| 63 | + .len = WH_TEST_AES_KEYSIZE}; |
| 64 | + whNvmMetadata tmpMetadata; |
| 65 | + |
| 66 | + /* Randomize inputs */ |
| 67 | + ret = wc_RNG_GenerateBlock(rng, key, sizeof(key)); |
| 68 | + if (ret != 0) { |
| 69 | + printf("Failed to wc_RNG_GenerateBlock for key %d\n", ret); |
| 70 | + return ret; |
| 71 | + } |
| 72 | + |
| 73 | + ret = wc_RNG_GenerateBlock(rng, plainKey, sizeof(plainKey)); |
| 74 | + if (ret != 0) { |
| 75 | + printf("Failed to wc_RNG_GenerateBlock for key data %d\n", ret); |
| 76 | + return ret; |
| 77 | + } |
| 78 | + |
| 79 | + ret = wc_RNG_GenerateBlock(rng, iv, sizeof(iv)); |
| 80 | + if (ret != 0) { |
| 81 | + printf("Failed to wc_RNG_GenerateBlock for IV %d\n", ret); |
| 82 | + return ret; |
| 83 | + } |
| 84 | + |
| 85 | + /* Initialize the AES GCM Server key */ |
| 86 | + ret = wh_Client_KeyCache(ctx, 0, label, sizeof(label), key, sizeof(key), |
| 87 | + &serverKeyId); |
| 88 | + if (ret != 0) { |
| 89 | + printf("Failed to wh_Client_KeyCache %d\n", ret); |
| 90 | + return ret; |
| 91 | + } |
| 92 | + |
| 93 | + ret = wh_Client_AesGcmWrapKey(ctx, serverKeyId, plainKey, sizeof(plainKey), |
| 94 | + &metadata, wrappedKey, sizeof(wrappedKey)); |
| 95 | + if (ret != 0) { |
| 96 | + printf("Failed to wh_Client_AesGcmWrapKey %d\n", ret); |
| 97 | + return ret; |
| 98 | + } |
| 99 | + |
| 100 | + ret = wh_Client_AesGcmWrapKeyCache(ctx, serverKeyId, wrappedKey, |
| 101 | + sizeof(wrappedKey), &wrappedKeyId); |
| 102 | + if (ret != 0) { |
| 103 | + printf("Failed to wh_Client_AesGcmWrapKeyCache %d\n", ret); |
| 104 | + return ret; |
| 105 | + } |
| 106 | + |
| 107 | + ret = wh_Client_AesGcmUnwrapKey(ctx, serverKeyId, wrappedKey, |
| 108 | + sizeof(wrappedKey), &tmpMetadata, |
| 109 | + tmpPlainKey, sizeof(tmpPlainKey)); |
| 110 | + if (ret != 0) { |
| 111 | + printf("Failed to wh_Client_AesGcmUnwrapKeyCache %d\n", ret); |
| 112 | + return ret; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + if (memcmp(plainKey, tmpPlainKey, sizeof(plainKey)) != 0) { |
| 117 | + printf("AES GCM wrap/unwrap key failed to match\n"); |
| 118 | + return ret; |
| 119 | + } |
| 120 | + |
| 121 | + if (memcmp(&metadata, &tmpMetadata, sizeof(metadata)) != 0) { |
| 122 | + printf("AES GCM wrap/unwrap metadata failed to match\n"); |
| 123 | + return ret; |
| 124 | + } |
| 125 | + |
| 126 | + return ret; |
| 127 | +} |
| 128 | + |
| 129 | +#endif /* HAVE_AESGCM */ |
| 130 | + |
| 131 | +int wh_DemoClient_AesWrapKeyBasic(whClientContext* clientContext, WC_RNG* rng) |
| 132 | +{ |
| 133 | + int ret = WH_ERROR_OK; |
| 134 | + |
| 135 | +#ifdef HAVE_AESGCM |
| 136 | + ret = wh_DemoClient_AesGcmWrapKeyBasic(clientContext, rng); |
| 137 | +#endif |
| 138 | + |
| 139 | + return ret; |
| 140 | +} |
| 141 | + |
| 142 | +#endif /* !NO_AES */ |
| 143 | +int wh_DemoClient_WrapKeyBasic(whClientContext* clientContext) |
| 144 | +{ |
| 145 | + |
| 146 | + int ret; |
| 147 | + WC_RNG rng[1]; |
| 148 | + |
| 149 | + ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID); |
| 150 | + if (ret != 0) { |
| 151 | + printf("Failed to wc_InitRng_ex %d\n", ret); |
| 152 | + return ret; |
| 153 | + } |
| 154 | + |
| 155 | +#ifndef NO_AES |
| 156 | + ret = wh_DemoClient_AesWrapKeyBasic(clientContext, rng); |
| 157 | +#endif |
| 158 | + |
| 159 | + wc_FreeRng(rng); |
| 160 | + return ret; |
| 161 | +} |
| 162 | + |
| 163 | +#endif /* WOLFHSM_CFG_WRAPKEY */ |
0 commit comments