1313#include "hal.h"
1414#include "wolfboot/wolfhsm_flash_hal.h"
1515
16+ #include "wolfssl/wolfcrypt/settings.h"
17+ #include "wolfssl/wolfcrypt/misc.h"
18+
1619#include "wolfhsm/wh_error.h"
1720#include "wolfhsm/wh_flash.h"
1821
@@ -28,20 +31,22 @@ static uint8_t cached_sector[WHFH5_SECTOR_SIZE];
2831
2932static int _Init (void * context , const void * config )
3033{
31- whFlashH5Ctx * ctx = (whFlashH5Ctx * )context ;
34+ const whFlashH5Ctx * cfg = (const whFlashH5Ctx * )config ;
35+ whFlashH5Ctx * ctx = (whFlashH5Ctx * )context ;
3236
33- if (ctx == NULL || config == NULL ) {
37+ if (ctx == NULL || cfg == NULL ) {
3438 return WH_ERROR_BADARGS ;
3539 }
36- * ctx = * ((const whFlashH5Ctx * )config );
3740
38- if (ctx -> base == 0U || ctx -> size == 0U || ctx -> partition_size == 0U ||
39- (ctx -> base % WHFH5_SECTOR_SIZE ) != 0U ||
40- (ctx -> size % WHFH5_SECTOR_SIZE ) != 0U ||
41- (ctx -> partition_size % WHFH5_SECTOR_SIZE ) != 0U ||
42- ctx -> size < ( uint32_t ) 2 * ctx -> partition_size ) {
41+ if (cfg -> base == 0U || cfg -> size == 0U || cfg -> partition_size == 0U ||
42+ (cfg -> base % WHFH5_SECTOR_SIZE ) != 0U ||
43+ (cfg -> size % WHFH5_SECTOR_SIZE ) != 0U ||
44+ (cfg -> partition_size % WHFH5_SECTOR_SIZE ) != 0U ||
45+ cfg -> partition_size > cfg -> size / 2U ) {
4346 return WH_ERROR_BADARGS ;
4447 }
48+
49+ * ctx = * cfg ;
4550 return WH_ERROR_OK ;
4651}
4752
@@ -96,6 +101,7 @@ static int _Program(void *context, uint32_t offset, uint32_t size,
96101{
97102 whFlashH5Ctx * ctx = (whFlashH5Ctx * )context ;
98103 uint32_t written = 0U ;
104+ int hrc = 0 ;
99105
100106 if (ctx == NULL || (size != 0U && data == NULL )) {
101107 return WH_ERROR_BADARGS ;
@@ -107,28 +113,38 @@ static int _Program(void *context, uint32_t offset, uint32_t size,
107113 return WH_ERROR_OK ;
108114 }
109115
116+ hal_flash_unlock ();
110117 while (written < size ) {
111118 uint32_t in_sector_off = (offset + written ) % WHFH5_SECTOR_SIZE ;
112- uint32_t sector_base = (offset + written ) - in_sector_off ;
119+ uint32_t sector_offset = (offset + written ) - in_sector_off ;
113120 uint32_t chunk = WHFH5_SECTOR_SIZE - in_sector_off ;
114121 if (chunk > size - written ) {
115122 chunk = size - written ;
116123 }
117124
118125 memcpy (cached_sector ,
119- (const uint8_t * )(ctx -> base + sector_base ),
126+ (const uint8_t * )(ctx -> base + sector_offset ),
120127 WHFH5_SECTOR_SIZE );
121128 memcpy (cached_sector + in_sector_off , data + written , chunk );
122129
123- hal_flash_unlock ();
124- hal_flash_erase (ctx -> base + sector_base , WHFH5_SECTOR_SIZE );
125- hal_flash_write (ctx -> base + sector_base , cached_sector ,
126- WHFH5_SECTOR_SIZE );
127- hal_flash_lock ();
130+ hrc = hal_flash_erase (ctx -> base + sector_offset , WHFH5_SECTOR_SIZE );
131+ if (hrc == 0 ) {
132+ hrc = hal_flash_write (ctx -> base + sector_offset , cached_sector ,
133+ WHFH5_SECTOR_SIZE );
134+ }
135+
136+ /* Per-iteration wipe so a fault between sectors doesn't strand
137+ * plaintext keystore bytes in the static cache. */
138+ wc_ForceZero (cached_sector , sizeof (cached_sector ));
128139
140+ if (hrc != 0 ) {
141+ break ;
142+ }
129143 written += chunk ;
130144 }
131- return WH_ERROR_OK ;
145+ hal_flash_lock ();
146+
147+ return (hrc == 0 ) ? WH_ERROR_OK : WH_ERROR_ABORTED ;
132148}
133149
134150static int _Erase (void * context , uint32_t offset , uint32_t size )
0 commit comments