Skip to content

Commit d426556

Browse files
committed
Move devId from crypto context into server context
1 parent c1e2cbd commit d426556

21 files changed

Lines changed: 128 additions & 161 deletions

benchmark/wh_bench.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,9 +1047,7 @@ int wh_Bench_ClientServer_Posix(int transport, int moduleIndex)
10471047

10481048
#ifndef WOLFHSM_CFG_NO_CRYPTO
10491049
/* Crypto context */
1050-
whServerCryptoContext crypto[1] = {{
1051-
.devId = INVALID_DEVID,
1052-
}};
1050+
whServerCryptoContext crypto[1] = {0};
10531051
#endif
10541052

10551053
/* Set up server configuration with NVM and crypto */
@@ -1085,7 +1083,7 @@ int wh_Bench_ClientServer_Posix(int transport, int moduleIndex)
10851083
}
10861084

10871085
/* Initialize RNG */
1088-
ret = wc_InitRng_ex(crypto->rng, NULL, crypto->devId);
1086+
ret = wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID);
10891087
if (ret != 0) {
10901088
WH_BENCH_PRINTF("Failed to initialize RNG: %d\n", ret);
10911089
wolfCrypt_Cleanup();

docs/draft/crypto_affinity.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ When affinity is set:
122122

123123
| Affinity | Server Action |
124124
|----------|---------------|
125-
| `WH_CRYPTO_AFFINITY_SW` | `server->crypto->devId = INVALID_DEVID` (wolfCrypt uses software) |
126-
| `WH_CRYPTO_AFFINITY_HW` | `server->crypto->devId = server->crypto->defaultDevId` (wolfCrypt uses registered crypto callback) |
125+
| `WH_CRYPTO_AFFINITY_SW` | `server->devId = INVALID_DEVID` (wolfCrypt uses software) |
126+
| `WH_CRYPTO_AFFINITY_HW` | `server->devId = server->defaultDevId` (wolfCrypt uses registered crypto callback) |
127127

128128
When affinity is queried (Get), the server reads the current `devId` and returns the corresponding affinity value without modifying any state.
129129

docs/src-ja/chapter03.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,14 @@ whNvmContext nvmCtx = {0};
171171
wh_Nvm_Init(&nvmCtx, &whNvmConfig);
172172

173173
/* 手順3: 暗号コンテキスト構造体の割り当てと初期化 */
174-
whServerCryptoContext cryptoCtx {
175-
.devID = INVALID_DEVID; /* あるいは、カスタム暗号コールバックdevIDを設定 */
176-
};
174+
whServerCryptoContext cryptoCtx = {0};
177175

178176
/* サーバー設定の割り当てと初期化 */
179177
whServerConfig serverCfg = {
180178
.comm = commServerCfg,
181179
.nvm = nvmCtx,
182-
.crypto = cryptoCtx,
180+
.crypto = &cryptoCtx,
181+
.devId = INVALID_DEVID, /* あるいは、カスタム暗号コールバックdevIDを設定 */
183182
};
184183

185184
/* 手順4: wolfCryptの初期化 */

docs/src/chapter03.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,14 @@ whNvmContext nvmCtx = {0};
169169
wh_Nvm_Init(&nvmCtx, &whNvmConfig);
170170

171171
/* Step 3: Allocate and initialize a crypto context structure */
172-
whServerCryptoContext cryptoCtx {
173-
.devID = INVALID_DEVID; /* or set to custom crypto callback devID */
174-
};
172+
whServerCryptoContext cryptoCtx = {0};
175173

176174
/* Allocate and initialize the Server configuration*/
177175
whServerConfig serverCfg = {
178176
.comm = commServerCfg,
179177
.nvm = nvmCtx,
180-
.crypto = cryptoCtx,
178+
.crypto = &cryptoCtx,
179+
.devId = INVALID_DEVID, /* or set to custom crypto callback devID */
181180
};
182181

183182
/* Step 4: Initialize wolfCrypt*/

examples/posix/wh_posix_server/wh_posix_server.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,7 @@ int main(int argc, char** argv)
416416
}
417417
#if !defined(WOLFHSM_CFG_NO_CRYPTO)
418418
/* Crypto context */
419-
whServerCryptoContext crypto[1] = {{
420-
.devId = INVALID_DEVID,
421-
}};
419+
whServerCryptoContext crypto[1] = {0};
422420

423421
#if defined(WOLFHSM_CFG_SHE_EXTENSION)
424422
whServerSheContext she[1] = {{0}};
@@ -452,11 +450,11 @@ int main(int argc, char** argv)
452450
wh_Utils_Hexdump("Context 4: Server HW RNG:\n", buffer, sizeof(buffer));
453451

454452
/* Context 5: Set default server crypto to use cryptocb */
455-
crypto->devId = HW_DEV_ID;
453+
s_conf->devId = HW_DEV_ID;
456454
WOLFHSM_CFG_PRINTF("Context 5: Setting up default server crypto with devId=%d\n",
457-
crypto->devId);
455+
s_conf->devId);
458456

459-
rc = wc_InitRng_ex(crypto->rng, NULL, crypto->devId);
457+
rc = wc_InitRng_ex(crypto->rng, NULL, s_conf->devId);
460458
if (rc != 0) {
461459
WOLFHSM_CFG_PRINTF("Failed to wc_InitRng_ex: %d\n", rc);
462460
return rc;

src/wh_server.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ int wh_Server_Init(whServerContext* server, whServerConfig* config)
7878

7979
#ifndef WOLFHSM_CFG_NO_CRYPTO
8080
server->crypto = config->crypto;
81-
if (server->crypto != NULL) {
82-
server->crypto->devId = config->devId;
83-
server->crypto->defaultDevId = config->devId;
84-
}
81+
server->devId = config->devId;
82+
server->defaultDevId = config->devId;
8583
#ifdef WOLFHSM_CFG_SHE_EXTENSION
8684
server->she = config->she;
8785
#endif
@@ -268,15 +266,15 @@ static int _wh_Server_HandleCommRequest(whServerContext* server,
268266
}
269267
else {
270268
if (req.affinity == WH_CRYPTO_AFFINITY_SW) {
271-
server->crypto->devId = INVALID_DEVID;
269+
server->devId = INVALID_DEVID;
272270
resp.rc = WH_ERROR_OK;
273271
}
274272
else if (req.affinity == WH_CRYPTO_AFFINITY_HW) {
275273
/* If the devId the server was configured with is valid then
276274
* switch back to it. The devId is used to call the appropriate
277275
* callback to utilize HW crypto */
278-
if (server->crypto->defaultDevId != INVALID_DEVID) {
279-
server->crypto->devId = server->crypto->defaultDevId;
276+
if (server->defaultDevId != INVALID_DEVID) {
277+
server->devId = server->defaultDevId;
280278
resp.rc = WH_ERROR_OK;
281279
}
282280
/* If the server was not configured with a valid devId
@@ -289,7 +287,7 @@ static int _wh_Server_HandleCommRequest(whServerContext* server,
289287
else {
290288
resp.rc = WH_ERROR_BADARGS;
291289
}
292-
resp.affinity = (server->crypto->devId == INVALID_DEVID)
290+
resp.affinity = (server->devId == INVALID_DEVID)
293291
? WH_CRYPTO_AFFINITY_SW
294292
: WH_CRYPTO_AFFINITY_HW;
295293
}
@@ -313,7 +311,7 @@ static int _wh_Server_HandleCommRequest(whServerContext* server,
313311
}
314312
else {
315313
resp.rc = WH_ERROR_OK;
316-
resp.affinity = (server->crypto->devId == INVALID_DEVID)
314+
resp.affinity = (server->devId == INVALID_DEVID)
317315
? WH_CRYPTO_AFFINITY_SW
318316
: WH_CRYPTO_AFFINITY_HW;
319317
}

0 commit comments

Comments
 (0)