Skip to content

Commit c5c6e81

Browse files
authored
Merge pull request #104 from billphipps/fix_flashunit_program
Fix flashunit program
2 parents a487c53 + 1cfd36d commit c5c6e81

File tree

9 files changed

+275
-36
lines changed

9 files changed

+275
-36
lines changed

port/posix/posix_flash_file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ int posixFlashFile_Init( void* c,
7878
}
7979

8080
/* Open the storage backend */
81-
rc = open(config->filename, O_RDWR|O_CREAT|O_SYNC, S_IRUSR | S_IWUSR);
81+
/* III Recommend to add O_SYNC if realtime data consistency is a concern */
82+
rc = open(config->filename, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
8283
if (rc >= 0) {
8384
/* File is open, setup context */
8485
memset(context, 0, sizeof(*context));

src/wh_flash_unit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ int wh_FlashUnit_ProgramBytes(const whFlashCb* cb, void* context,
217217
}
218218

219219
/* Aligned programming */
220-
ret = wh_FlashUnit_Program(cb, context,
220+
if(count) {
221+
ret = wh_FlashUnit_Program(cb, context,
221222
offset, count, (whFlashUnit*)data);
223+
}
222224

223225
/* Final partial unit */
224226
if ((ret == 0) && (rem != 0)) {

src/wh_server_crypto.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,10 @@ int wh_Server_CacheImportCurve25519Key(whServerContext* server,
452452
uint8_t* cacheBuf;
453453
whNvmMetadata* cacheMeta;
454454
int ret;
455-
/* Max size of a DER encoded curve25519 keypair with SubjectPublicKeyInfo
456-
* included. Determined by experiment */
457-
const uint16_t MAX_DER_SIZE = 128;
458-
uint16_t keySz = keySz;
459455

460-
uint8_t der_buf[MAX_DER_SIZE];
456+
/* CURVE25519_MAX_KEY_TO_DER_SZ should be 82 */
457+
uint8_t der_buf[CURVE25519_MAX_KEY_TO_DER_SZ];
458+
uint16_t keySz = sizeof(der_buf);
461459

462460

463461
if ((server == NULL) || (key == NULL) || (WH_KEYID_ISERASED(keyId)) ||

test/user_settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ extern "C" {
8888

8989
/** RSA Options */
9090
/*#define NO_RSA */
91+
#define RSA_MIN_SIZE 1024
9192
#define WC_RSA_PSS
9293
#define WOLFSSL_PSS_LONG_SALT
9394
#define FP_MAX_BITS 8192

test/wh_test_crypto.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,7 @@ int whTest_CryptoClientConfig(whClientConfig* config)
22852285
i++;
22862286
}
22872287
}
2288+
22882289
#ifdef WOLFHSM_CFG_DMA
22892290
if (ret == 0) {
22902291
ret = whTestCrypto_MlDsaDmaClient(client, WH_DEV_ID_DMA, rng);

test/wh_test_nvm_flash.c

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "wolfhsm/wh_error.h"
3030
#include "wolfhsm/wh_nvm.h"
3131
#include "wolfhsm/wh_nvm_flash.h"
32+
#include "wolfhsm/wh_flash_unit.h"
3233

3334
/* NVM simulator backends to use for testing NVM module */
3435
#include "wolfhsm/wh_flash_ramsim.h"
@@ -180,6 +181,131 @@ static int destroyObjectWithReadBackCheck(const whNvmCb* cb,
180181
return 0;
181182
}
182183

184+
int whTest_Flash(const whFlashCb* fcb, void* fctx, const void* cfg)
185+
{
186+
uint8_t write_bytes[8] = { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87};
187+
uint8_t read_bytes[8] = {0};
188+
whFlashUnit write_buffer[4] = {0};
189+
whFlashUnit read_buffer[4] = {0};
190+
191+
uint32_t partition_units = 0;
192+
193+
WH_TEST_RETURN_ON_FAIL(fcb->Init(fctx, cfg));
194+
195+
partition_units = wh_FlashUnit_Bytes2Units(fcb->PartitionSize(fctx)) ;
196+
197+
/* Unlock the first partition */
198+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_WriteUnlock(fcb, fctx,
199+
0, partition_units));
200+
201+
/* Erase the first partition */
202+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Erase(fcb, fctx,
203+
0, partition_units));
204+
205+
/* Blank check the first partition */
206+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_BlankCheck(fcb, fctx,
207+
0, partition_units));
208+
209+
/* Program a few different unit sizes */
210+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Program(fcb, fctx,
211+
0, 1, write_buffer));
212+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Program(fcb, fctx,
213+
1, 2, write_buffer));
214+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Program(fcb, fctx,
215+
3, 3, write_buffer));
216+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Program(fcb, fctx,
217+
6, 4, write_buffer));
218+
219+
/* Read back and check */
220+
memset(read_buffer, 0, sizeof(read_buffer));
221+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Read(fcb, fctx,
222+
0, 1, read_buffer));
223+
WH_TEST_RETURN_ON_FAIL(memcmp(write_buffer, read_buffer,
224+
1 * WHFU_BYTES_PER_UNIT));
225+
memset(read_buffer, 0, sizeof(read_buffer));
226+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Read(fcb, fctx,
227+
1, 2, read_buffer));
228+
WH_TEST_RETURN_ON_FAIL(memcmp(write_buffer, read_buffer,
229+
2 * WHFU_BYTES_PER_UNIT));
230+
memset(read_buffer, 0, sizeof(read_buffer));
231+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Read(fcb, fctx,
232+
3, 3, read_buffer));
233+
WH_TEST_RETURN_ON_FAIL(memcmp(write_buffer, read_buffer,
234+
3 * WHFU_BYTES_PER_UNIT));
235+
memset(read_buffer, 0, sizeof(read_buffer));
236+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Read(fcb, fctx,
237+
6, 4, read_buffer));
238+
WH_TEST_RETURN_ON_FAIL(memcmp(write_buffer, read_buffer,
239+
4 * WHFU_BYTES_PER_UNIT));
240+
241+
/* Program a few different byte sizes */
242+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ProgramBytes(fcb, fctx,
243+
10 * WHFU_BYTES_PER_UNIT, 1, write_bytes));
244+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ProgramBytes(fcb, fctx,
245+
11 * WHFU_BYTES_PER_UNIT, 2, write_bytes));
246+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ProgramBytes(fcb, fctx,
247+
12 * WHFU_BYTES_PER_UNIT, 3, write_bytes));
248+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ProgramBytes(fcb, fctx,
249+
13 * WHFU_BYTES_PER_UNIT, 4, write_bytes));
250+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ProgramBytes(fcb, fctx,
251+
14 * WHFU_BYTES_PER_UNIT, 5, write_bytes));
252+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ProgramBytes(fcb, fctx,
253+
15 * WHFU_BYTES_PER_UNIT, 6, write_bytes));
254+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ProgramBytes(fcb, fctx,
255+
16 * WHFU_BYTES_PER_UNIT, 7, write_bytes));
256+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ProgramBytes(fcb, fctx,
257+
17 * WHFU_BYTES_PER_UNIT, 8, write_bytes));
258+
259+
/* Read back and compare */
260+
memset(read_bytes, 0, sizeof(read_bytes));
261+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
262+
10 * WHFU_BYTES_PER_UNIT, 1, read_bytes));
263+
WH_TEST_RETURN_ON_FAIL(memcmp(write_bytes, read_bytes, 1));
264+
memset(read_bytes, 0, sizeof(read_bytes));
265+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
266+
11 * WHFU_BYTES_PER_UNIT, 2, read_bytes));
267+
WH_TEST_RETURN_ON_FAIL(memcmp(write_bytes, read_bytes, 2));
268+
memset(read_bytes, 0, sizeof(read_bytes));
269+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
270+
12 * WHFU_BYTES_PER_UNIT, 3, read_bytes));
271+
WH_TEST_RETURN_ON_FAIL(memcmp(write_bytes, read_bytes, 3));
272+
memset(read_bytes, 0, sizeof(read_bytes));
273+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
274+
13 * WHFU_BYTES_PER_UNIT, 4, read_bytes));
275+
WH_TEST_RETURN_ON_FAIL(memcmp(write_bytes, read_bytes, 4));
276+
memset(read_bytes, 0, sizeof(read_bytes));
277+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
278+
14 * WHFU_BYTES_PER_UNIT, 5, read_bytes));
279+
WH_TEST_RETURN_ON_FAIL(memcmp(write_bytes, read_bytes, 5));
280+
memset(read_bytes, 0, sizeof(read_bytes));
281+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
282+
15 * WHFU_BYTES_PER_UNIT, 6, read_bytes));
283+
WH_TEST_RETURN_ON_FAIL(memcmp(write_bytes, read_bytes, 6));
284+
memset(read_bytes, 0, sizeof(read_bytes));
285+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
286+
16 * WHFU_BYTES_PER_UNIT, 7, read_bytes));
287+
WH_TEST_RETURN_ON_FAIL(memcmp(write_bytes, read_bytes, 7));
288+
memset(read_bytes, 0, sizeof(read_bytes));
289+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
290+
17 * WHFU_BYTES_PER_UNIT, 8, read_bytes));
291+
WH_TEST_RETURN_ON_FAIL(memcmp(write_bytes, read_bytes, 8));
292+
293+
/* Erase the first partition */
294+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Erase(fcb, fctx,
295+
0, partition_units));
296+
297+
/* Blank check the first partition */
298+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_BlankCheck(fcb, fctx,
299+
0, partition_units));
300+
301+
/* Lock the first partition */
302+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_WriteLock(fcb, fctx,
303+
0, partition_units));
304+
305+
WH_TEST_RETURN_ON_FAIL(fcb->Cleanup(fctx));
306+
307+
return 0;
308+
}
183309

184310
int whTest_NvmFlashCfg(whNvmFlashConfig* cfg)
185311
{
@@ -350,6 +476,8 @@ int whTest_NvmFlash_RamSim(void)
350476
.erasedByte = (uint8_t)0,
351477
}};
352478

479+
WH_TEST_RETURN_ON_FAIL(whTest_Flash(myCb, myHalFlashCtx, myHalFlashCfg));
480+
353481
/* NVM Configuration using PosixSim HAL Flash */
354482
whNvmFlashConfig myNvmCfg = {
355483
.cb = myCb,
@@ -375,6 +503,8 @@ int whTest_NvmFlash_PosixFileSim(void)
375503
.erased_byte = (~(uint8_t)0),
376504
}};
377505

506+
WH_TEST_RETURN_ON_FAIL(whTest_Flash(myCb, myHalFlashContext,
507+
myHalFlashConfig));
378508

379509
/* NVM Configuration using PosixSim HAL Flash */
380510
whNvmFlashConfig myNvmCfg = {
@@ -383,7 +513,6 @@ int whTest_NvmFlash_PosixFileSim(void)
383513
.config = myHalFlashConfig,
384514
};
385515

386-
387516
WH_TEST_ASSERT(0 == whTest_NvmFlashCfg(&myNvmCfg));
388517

389518
/* Remove the configured file on success*/

test/wh_test_nvm_flash.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
*/
3333
int whTest_NvmFlash(void);
3434

35+
/*
36+
* Runs low-level flash tests on a custom NVM flash configuration. Useful to
37+
* test your FLASH HAL implementation. This will erase the first partition.
38+
* Returns 0 on success, and a non-zero error code on failure
39+
*/
40+
int whTest_FlashCfg(const whFlashCb* fcb, void* fctx, const void* cfg);
41+
3542
/*
3643
* Runs NVM flash tests on a custom NVM flash configuration. Useful to test your
3744
* NVM HAL implementation

wolfhsm/wh_error.h

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,37 @@ enum WH_ERROR_ENUM {
3232
WH_ERROR_OK = 0, /* Success, no error. */
3333

3434
/* General errors */
35-
WH_ERROR_BADARGS = -400, /* No side effects. Fix args. */
36-
WH_ERROR_NOTREADY = -401, /* Retry function. */
37-
WH_ERROR_ABORTED = -402, /* Function has fatally failed. Cleanup. */
38-
WH_ERROR_CANCEL = -403, /* Operation was canceled */
39-
WH_ERROR_CANCEL_LATE = -404, /* Cancel was processed too late */
40-
WH_ERROR_CERT_VERIFY = -405, /* Certificate verification failed */
41-
WH_ERROR_BUFFER_SIZE = -406, /* Generic buffer size mismatch. Buffer
35+
WH_ERROR_BADARGS = -2000, /* No side effects. Fix args. */
36+
WH_ERROR_NOTREADY = -2001, /* Retry function. */
37+
WH_ERROR_ABORTED = -2002, /* Function has fatally failed. Cleanup. */
38+
WH_ERROR_CANCEL = -2003, /* Operation was canceled */
39+
WH_ERROR_CANCEL_LATE = -2004, /* Cancel was processed too late */
40+
WH_ERROR_CERT_VERIFY = -2005, /* Certificate verification failed */
41+
WH_ERROR_BUFFER_SIZE = -2006, /* Generic buffer size mismatch. Buffer
4242
* length is not what was expected */
43+
WH_ERROR_NOHANDLER = -2007, /* No customcb handler registered */
4344

4445
/* NVM-specific status returns */
45-
WH_ERROR_LOCKED = -410, /* Unlock and retry if necessary */
46-
WH_ERROR_ACCESS = -411, /* Update access and retry */
47-
WH_ERROR_NOTVERIFIED = -412, /* Backing store does not match */
48-
WH_ERROR_NOTBLANK = -413, /* Area is no blank */
49-
WH_ERROR_NOTFOUND = -414, /* Matching object not found */
50-
WH_ERROR_NOSPACE = -415, /* No available space */
51-
52-
/* Custom-callback status returns */
53-
WH_ERROR_NOHANDLER = -420, /* No handler registered for action */
46+
WH_ERROR_LOCKED = -2100, /* Unlock and retry if necessary */
47+
WH_ERROR_ACCESS = -2101, /* Update access and retry */
48+
WH_ERROR_NOTVERIFIED = -2102, /* Backing store does not match */
49+
WH_ERROR_NOTBLANK = -2103, /* Area is no blank */
50+
WH_ERROR_NOTFOUND = -2104, /* Matching object not found */
51+
WH_ERROR_NOSPACE = -2105, /* No available space */
5452

5553
/* SHE-specific error codes */
56-
WH_SHE_ERC_SEQUENCE_ERROR = -500,
57-
WH_SHE_ERC_KEY_NOT_AVAILABLE = -501,
58-
WH_SHE_ERC_KEY_INVALID = -502,
59-
WH_SHE_ERC_KEY_EMPTY = -503,
60-
WH_SHE_ERC_NO_SECURE_BOOT = -504,
61-
WH_SHE_ERC_WRITE_PROTECTED = -505,
62-
WH_SHE_ERC_KEY_UPDATE_ERROR = -506,
63-
WH_SHE_ERC_RNG_SEED = -507,
64-
WH_SHE_ERC_NO_DEBUGGING = -508,
65-
WH_SHE_ERC_BUSY = -509,
66-
WH_SHE_ERC_MEMORY_FAILURE = -510,
67-
WH_SHE_ERC_GENERAL_ERROR = -511,
54+
WH_SHE_ERC_SEQUENCE_ERROR = -2200,
55+
WH_SHE_ERC_KEY_NOT_AVAILABLE = -2201,
56+
WH_SHE_ERC_KEY_INVALID = -2202,
57+
WH_SHE_ERC_KEY_EMPTY = -2203,
58+
WH_SHE_ERC_NO_SECURE_BOOT = -2204,
59+
WH_SHE_ERC_WRITE_PROTECTED = -2205,
60+
WH_SHE_ERC_KEY_UPDATE_ERROR = -2206,
61+
WH_SHE_ERC_RNG_SEED = -2207,
62+
WH_SHE_ERC_NO_DEBUGGING = -2208,
63+
WH_SHE_ERC_BUSY = -2209,
64+
WH_SHE_ERC_MEMORY_FAILURE = -2210,
65+
WH_SHE_ERC_GENERAL_ERROR = -2211,
6866
};
6967

7068
#define WH_SHE_ERC_NO_ERROR WH_ERROR_OK

0 commit comments

Comments
 (0)