@@ -197,6 +197,34 @@ static ATCAIfaceCfg cfg = {
197197 .rx_retries = I2C_ECC_RETRIES ,
198198 .cfg_data = NULL };
199199
200+ /**
201+ * Check if a slot is individually locked.
202+ */
203+ static bool _slot_is_locked (securechip_slot_t slot )
204+ {
205+ bool is_locked = false;
206+ ATCA_STATUS result = atcab_is_slot_locked (slot , & is_locked );
207+ if (result != ATCA_SUCCESS ) {
208+ return false;
209+ }
210+ return is_locked ;
211+ }
212+
213+ /**
214+ * Check if a zone is locked.
215+ * @param[in] zone LOCK_ZONE_CONFIG or LOCK_ZONE_DATA.
216+ */
217+ static bool _zone_is_locked (uint8_t zone )
218+ {
219+ bool is_locked = false;
220+ ATCA_STATUS result = atcab_is_locked (zone , & is_locked );
221+ if (result != ATCA_SUCCESS ) {
222+ return false;
223+ }
224+ return is_locked ;
225+ }
226+
227+ #if defined(FACTORYSETUP )
200228/**
201229 * Individually locks a slot. Used to lock the io protection and auth key so
202230 * they can never change.
@@ -214,18 +242,13 @@ static ATCA_STATUS _lock_slot(securechip_slot_t slot)
214242 return ATCA_SUCCESS ;
215243}
216244
217- bool securechip_setup ( securechip_interface_functions_t * ifs )
245+ static bool _factory_setup ( void )
218246{
219- if (ifs == NULL ) {
220- return false;
221- }
222- _interface_functions = ifs ;
223- ATCA_STATUS result = atcab_init (& cfg );
224- if (result != ATCA_SUCCESS ) {
247+ if (_interface_functions == NULL ) {
225248 return false;
226249 }
227250 bool is_config_locked = false;
228- result = atcab_is_locked (LOCK_ZONE_CONFIG , & is_config_locked );
251+ ATCA_STATUS result = atcab_is_locked (LOCK_ZONE_CONFIG , & is_config_locked );
229252 if (result != ATCA_SUCCESS ) {
230253 return false;
231254 }
@@ -309,20 +332,59 @@ bool securechip_setup(securechip_interface_functions_t* ifs)
309332 return false;
310333 }
311334 }
335+ return true;
336+ }
337+ #endif
312338
313- // Validate config.
314- if (is_config_locked && is_data_locked ) {
315- bool same_config = false;
316- if (atcab_cmp_config_zone (_configuration , & same_config ) != ATCA_SUCCESS ) {
317- return false;
318- }
319- if (!same_config ) {
320- return false;
321- }
339+ static bool _verify_config (void )
340+ {
341+ if (!_zone_is_locked (LOCK_ZONE_CONFIG )) {
342+ return false;
343+ }
344+ if (!_zone_is_locked (LOCK_ZONE_DATA )) {
345+ return false;
346+ }
347+
348+ bool same_config = false;
349+ if (atcab_cmp_config_zone (_configuration , & same_config ) != ATCA_SUCCESS ) {
350+ return false;
351+ }
352+ if (!same_config ) {
353+ return false;
354+ }
355+
356+ if (!_slot_is_locked (SECURECHIP_SLOT_IO_PROTECTION_KEY )) {
357+ return false;
358+ }
359+ if (!_slot_is_locked (SECURECHIP_SLOT_AUTHKEY )) {
360+ return false;
361+ }
362+ if (!_slot_is_locked (SECURECHIP_SLOT_ENCRYPTION_KEY )) {
363+ return false;
322364 }
323365 return true;
324366}
325367
368+ bool securechip_setup (securechip_interface_functions_t * ifs )
369+ {
370+ if (ifs == NULL ) {
371+ return false;
372+ }
373+ _interface_functions = ifs ;
374+ ATCA_STATUS result = atcab_init (& cfg );
375+ if (result != ATCA_SUCCESS ) {
376+ return false;
377+ }
378+
379+ #if defined(FACTORYSETUP )
380+ if (!_factory_setup ()) {
381+ return false;
382+ }
383+ #endif
384+
385+ return _verify_config ();
386+ }
387+
326388/**
327389 * This performs the CheckMac command on SECURECHIP_SLOT_AUTHKEY. This needs to
328390 * be called before using any slot requiring auth and whose KeyConfig.AuthKey is
0 commit comments