Skip to content

Commit a5911d7

Browse files
committed
Various fixes
* clang-tidy * Reduce temp key buffer on stack for CMAC operations * Minor misc changes
1 parent 977bf18 commit a5911d7

18 files changed

+207
-195
lines changed

benchmark/bench_modules/wh_bench_mod_rsa.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* You should have received a copy of the GNU General Public License
1717
* along with wolfHSM. If not, see <http://www.gnu.org/licenses/>.
1818
*/
19+
#include <string.h>
1920
#include "wh_bench_mod.h"
2021
#include "wolfhsm/wh_error.h"
2122
#include "wolfhsm/wh_client.h"
@@ -384,10 +385,10 @@ int _benchRsaCrypt(whClientContext* client, whBenchOpContext* ctx, int id,
384385
goto exit;
385386
}
386387

387-
strcpy((char*)inBuf, inStr);
388+
strncpy((char*)inBuf, inStr, sizeof(inBuf)-1);
388389

389390
/* Do an initial encryption to get the size of the output */
390-
encSz = ret = wc_RsaPublicEncrypt(inBuf, sizeof(inStr), outBuf,
391+
encSz = ret = wc_RsaPublicEncrypt(inBuf, strlen(inStr), outBuf,
391392
sizeof(outBuf), rsa, rng);
392393
if (ret < 0) {
393394
WH_BENCH_PRINTF("Failed to wc_RsaPublicEncrypt %d\n", ret);
@@ -408,7 +409,7 @@ int _benchRsaCrypt(whClientContext* client, whBenchOpContext* ctx, int id,
408409

409410
if (operation == RSA_PUBLIC_ENCRYPT) {
410411
benchStartRet = wh_Bench_StartOp(ctx, id);
411-
opRet = wc_RsaPublicEncrypt(inBuf, sizeof(inStr), outBuf,
412+
opRet = wc_RsaPublicEncrypt(inBuf, strlen(inStr), outBuf,
412413
sizeof(outBuf), rsa, rng);
413414
benchStopRet = wh_Bench_StopOp(ctx, id);
414415
}

benchmark/wh_bench.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
/* Buffer sizes for transport */
5959
/* Large enough to handle an RSA 4096 key */
6060
#define BUFFER_SIZE \
61-
sizeof(whTransportMemCsr) + sizeof(whCommHeader) + WOLFHSM_CFG_COMM_DATA_LEN
61+
(sizeof(whTransportMemCsr) + sizeof(whCommHeader) + \
62+
WOLFHSM_CFG_COMM_DATA_LEN)
6263
#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
6364

6465
typedef struct BenchModule {
@@ -826,14 +827,14 @@ static void _whBenchClientServerThreadTest(whClientConfig* c_conf,
826827
rc = pthread_create(&cthread, NULL, _whBenchClientTask, &clientData);
827828
if (rc == 0) {
828829
/* Wait for client to finish, then cancel server */
829-
pthread_join(cthread, &retval);
830-
pthread_cancel(sthread);
831-
pthread_join(sthread, &retval);
830+
(void)pthread_join(cthread, &retval);
831+
(void)pthread_cancel(sthread);
832+
(void)pthread_join(sthread, &retval);
832833
}
833834
else {
834835
/* If client thread creation failed, cancel server */
835-
pthread_cancel(sthread);
836-
pthread_join(sthread, &retval);
836+
(void)pthread_cancel(sthread);
837+
(void)pthread_join(sthread, &retval);
837838
}
838839
}
839840
}

benchmark/wh_bench_ops.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,27 +346,27 @@ int wh_Bench_PrintResults(whBenchOpContext* ctx)
346346

347347
if (throughput < 1024.0) {
348348
/* Bytes per second */
349-
WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "%.2f B/s",
350-
throughput);
349+
(void)WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "%.2f B/s",
350+
throughput);
351351
}
352352
else if (throughput < 1024.0 * 1024.0) {
353353
/* Kilobytes per second */
354-
WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "%.2f KB/s",
355-
throughput / 1024.0);
354+
(void)WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "%.2f KB/s",
355+
throughput / 1024.0);
356356
}
357357
else {
358358
/* Megabytes per second */
359-
WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "%.2f MB/s",
360-
throughput / (1024.0 * 1024.0));
359+
(void)WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "%.2f MB/s",
360+
throughput / (1024.0 * 1024.0));
361361
}
362362
}
363363
else if (ctx->ops[i].throughputType == BENCH_THROUGHPUT_OPS) {
364-
WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "%.2f ops/s",
365-
ctx->ops[i].throughput);
364+
(void)WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "%.2f ops/s",
365+
ctx->ops[i].throughput);
366366
}
367367
else {
368368
/* No throughput */
369-
WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "N/A");
369+
(void)WH_BENCH_SNPRINTF(buffer, sizeof(buffer), "N/A");
370370
}
371371
WH_BENCH_PRINTF("%-18s |\n", buffer);
372372
}

examples/demo/client/wh_demo_client_crypto.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int wh_DemoClient_CryptoRsa(whClientContext* clientContext)
6767
WC_RNG rng[1];
6868

6969
/* set the plainText to the test string */
70-
strcpy((char*)plainText, plainString);
70+
strncpy((char*)plainText, plainString, sizeof(plainText)-1);
7171

7272
/* initialize rng to make the rsa key */
7373
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);
@@ -144,7 +144,7 @@ int wh_DemoClient_CryptoRsaImport(whClientContext* clientContext)
144144
WC_RNG rng[1];
145145

146146
/* set the plainText to the test string */
147-
strcpy((char*)plainText, plainString);
147+
strncpy((char*)plainText, plainString, sizeof(plainText)-1);
148148

149149
/* initialize rng to encrypt with the rsa key */
150150
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);
@@ -497,7 +497,8 @@ int wh_DemoClient_CryptoEcc(whClientContext* clientContext)
497497
byte signature[128];
498498

499499
/* Set the message to the test string */
500-
strcpy((char*)message, plainMessage);
500+
strncpy((char*)message, plainMessage, sizeof(message)-1);
501+
message[sizeof(message)-1] = '\0';
501502

502503
/* Initialize the rng to make the ecc keys */
503504
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);
@@ -627,7 +628,8 @@ int wh_DemoClient_CryptoEccImport(whClientContext* clientContext)
627628
uint8_t keyBuf[256];
628629

629630
/* Set the message to the test string */
630-
strcpy((char*)message, plainMessage);
631+
strncpy((char*)message, plainMessage, sizeof(message)-1);
632+
message[sizeof(message)-1] = '\0';
631633

632634
/* Initialize the rng for signature signing */
633635
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);

examples/posix/wh_posix_server/wh_posix_server_cfg.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ static void parseNvmInitFile(const char* filePath)
478478
fclose(file);
479479
exit(EXIT_FAILURE);
480480
}
481-
snprintf(label, sizeof(label), "%s", token);
481+
(void)snprintf(label, sizeof(label), "%s", token);
482482

483483
/* Parse the file path */
484484
token = strtok(NULL, " ");
@@ -508,9 +508,9 @@ static void processEntry(Entry* entry, int isKey, whNvmContext* nvmContext)
508508
}
509509

510510
/* Get the file size */
511-
fseek(file, 0, SEEK_END);
511+
(void)fseek(file, 0, SEEK_END);
512512
long fileSize = ftell(file);
513-
fseek(file, 0, SEEK_SET);
513+
(void)fseek(file, 0, SEEK_SET);
514514

515515
/* Allocate memory for the file data */
516516
uint8_t* buffer = (uint8_t*)malloc(fileSize);
@@ -553,7 +553,7 @@ static void processEntry(Entry* entry, int isKey, whNvmContext* nvmContext)
553553
meta.access = entry->access;
554554
meta.flags = entry->flags;
555555
meta.len = fileSize;
556-
snprintf((char*)meta.label, WH_NVM_LABEL_LEN, "%s", entry->label);
556+
(void)snprintf((char*)meta.label, WH_NVM_LABEL_LEN, "%s", entry->label);
557557

558558
int rc = wh_Nvm_AddObject(nvmContext, &meta, fileSize, buffer);
559559
if (rc != 0) {

port/posix/posix_flash_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ enum {
4141
};
4242

4343
/** Local declarations */
44-
#define MAX_OFFSET(_context) (_context->partition_size * 2)
44+
#define MAX_OFFSET(_context) ((_context)->partition_size * 2)
4545

4646
/* Helper for pwrite like memset. Write the byte in c to filedes for size
4747
* bytes starting at offset */

0 commit comments

Comments
 (0)