@@ -224,86 +224,6 @@ static bool _verify_seed(
224
224
return true;
225
225
}
226
226
227
- keystore_error_t keystore_encrypt_and_store_seed (
228
- const uint8_t * seed ,
229
- size_t seed_length ,
230
- const char * password )
231
- {
232
- if (memory_is_initialized ()) {
233
- return KEYSTORE_ERR_MEMORY ;
234
- }
235
- keystore_lock ();
236
- if (!_validate_seed_length (seed_length )) {
237
- return KEYSTORE_ERR_SEED_SIZE ;
238
- }
239
- if (securechip_init_new_password (password )) {
240
- return KEYSTORE_ERR_SECURECHIP ;
241
- }
242
- uint8_t secret [32 ] = {0 };
243
- UTIL_CLEANUP_32 (secret );
244
- if (securechip_stretch_password (password , secret )) {
245
- return KEYSTORE_ERR_SECURECHIP ;
246
- }
247
-
248
- size_t encrypted_seed_len = seed_length + 64 ;
249
- uint8_t encrypted_seed [encrypted_seed_len ];
250
- UTIL_CLEANUP_32 (encrypted_seed );
251
- if (!cipher_aes_hmac_encrypt (seed , seed_length , encrypted_seed , & encrypted_seed_len , secret )) {
252
- return KEYSTORE_ERR_ENCRYPT ;
253
- }
254
- if (encrypted_seed_len > 255 ) { // sanity check, can't happen
255
- Abort ("keystore_encrypt_and_store_seed" );
256
- }
257
- uint8_t encrypted_seed_len_u8 = (uint8_t )encrypted_seed_len ;
258
- if (!memory_set_encrypted_seed_and_hmac (encrypted_seed , encrypted_seed_len_u8 )) {
259
- return KEYSTORE_ERR_MEMORY ;
260
- }
261
- if (!_verify_seed (password , seed , seed_length )) {
262
- if (!memory_reset_hww ()) {
263
- return KEYSTORE_ERR_MEMORY ;
264
- }
265
- return KEYSTORE_ERR_MEMORY ;
266
- }
267
- return KEYSTORE_OK ;
268
- }
269
-
270
- keystore_error_t keystore_create_and_store_seed (
271
- const char * password ,
272
- const uint8_t * host_entropy ,
273
- size_t host_entropy_size )
274
- {
275
- if (host_entropy_size != 16 && host_entropy_size != 32 ) {
276
- return KEYSTORE_ERR_SEED_SIZE ;
277
- }
278
- if (KEYSTORE_MAX_SEED_LENGTH != RANDOM_NUM_SIZE ) {
279
- Abort ("keystore create: size mismatch" );
280
- }
281
- uint8_t seed [KEYSTORE_MAX_SEED_LENGTH ];
282
- UTIL_CLEANUP_32 (seed );
283
- random_32_bytes (seed );
284
-
285
- // Mix in Host entropy.
286
- for (size_t i = 0 ; i < host_entropy_size ; i ++ ) {
287
- seed [i ] ^= host_entropy [i ];
288
- }
289
-
290
- // Mix in entropy derived from the user password.
291
- uint8_t password_salted_hashed [KEYSTORE_MAX_SEED_LENGTH ] = {0 };
292
- UTIL_CLEANUP_32 (password_salted_hashed );
293
- if (!salt_hash_data (
294
- (const uint8_t * )password ,
295
- strlen (password ),
296
- "keystore_seed_generation" ,
297
- password_salted_hashed )) {
298
- return KEYSTORE_ERR_SALT ;
299
- }
300
-
301
- for (size_t i = 0 ; i < host_entropy_size ; i ++ ) {
302
- seed [i ] ^= password_salted_hashed [i ];
303
- }
304
- return keystore_encrypt_and_store_seed (seed , host_entropy_size , password );
305
- }
306
-
307
227
USE_RESULT static keystore_error_t _retain_seed (const uint8_t * seed , size_t seed_len )
308
228
{
309
229
#ifdef TESTING
@@ -385,6 +305,93 @@ static void _delete_retained_seeds(void)
385
305
_retained_bip39_seed_encrypted_len = 0 ;
386
306
}
387
307
308
+ keystore_error_t keystore_encrypt_and_store_seed (
309
+ const uint8_t * seed ,
310
+ size_t seed_length ,
311
+ const char * password )
312
+ {
313
+ if (memory_is_initialized ()) {
314
+ return KEYSTORE_ERR_MEMORY ;
315
+ }
316
+ keystore_lock ();
317
+ if (!_validate_seed_length (seed_length )) {
318
+ return KEYSTORE_ERR_SEED_SIZE ;
319
+ }
320
+ if (securechip_init_new_password (password )) {
321
+ return KEYSTORE_ERR_SECURECHIP ;
322
+ }
323
+ uint8_t secret [32 ] = {0 };
324
+ UTIL_CLEANUP_32 (secret );
325
+ if (securechip_stretch_password (password , secret )) {
326
+ return KEYSTORE_ERR_SECURECHIP ;
327
+ }
328
+
329
+ size_t encrypted_seed_len = seed_length + 64 ;
330
+ uint8_t encrypted_seed [encrypted_seed_len ];
331
+ UTIL_CLEANUP_32 (encrypted_seed );
332
+ if (!cipher_aes_hmac_encrypt (seed , seed_length , encrypted_seed , & encrypted_seed_len , secret )) {
333
+ return KEYSTORE_ERR_ENCRYPT ;
334
+ }
335
+ if (encrypted_seed_len > 255 ) { // sanity check, can't happen
336
+ Abort ("keystore_encrypt_and_store_seed" );
337
+ }
338
+ uint8_t encrypted_seed_len_u8 = (uint8_t )encrypted_seed_len ;
339
+ if (!memory_set_encrypted_seed_and_hmac (encrypted_seed , encrypted_seed_len_u8 )) {
340
+ return KEYSTORE_ERR_MEMORY ;
341
+ }
342
+ if (!_verify_seed (password , seed , seed_length )) {
343
+ if (!memory_reset_hww ()) {
344
+ return KEYSTORE_ERR_MEMORY ;
345
+ }
346
+ return KEYSTORE_ERR_MEMORY ;
347
+ }
348
+
349
+ keystore_error_t retain_seed_result = _retain_seed (seed , seed_length );
350
+ if (retain_seed_result != KEYSTORE_OK ) {
351
+ return retain_seed_result ;
352
+ }
353
+ _is_unlocked_device = true;
354
+
355
+ return KEYSTORE_OK ;
356
+ }
357
+
358
+ keystore_error_t keystore_create_and_store_seed (
359
+ const char * password ,
360
+ const uint8_t * host_entropy ,
361
+ size_t host_entropy_size )
362
+ {
363
+ if (host_entropy_size != 16 && host_entropy_size != 32 ) {
364
+ return KEYSTORE_ERR_SEED_SIZE ;
365
+ }
366
+ if (KEYSTORE_MAX_SEED_LENGTH != RANDOM_NUM_SIZE ) {
367
+ Abort ("keystore create: size mismatch" );
368
+ }
369
+ uint8_t seed [KEYSTORE_MAX_SEED_LENGTH ];
370
+ UTIL_CLEANUP_32 (seed );
371
+ random_32_bytes (seed );
372
+
373
+ // Mix in Host entropy.
374
+ for (size_t i = 0 ; i < host_entropy_size ; i ++ ) {
375
+ seed [i ] ^= host_entropy [i ];
376
+ }
377
+
378
+ // Mix in entropy derived from the user password.
379
+ uint8_t password_salted_hashed [KEYSTORE_MAX_SEED_LENGTH ] = {0 };
380
+ UTIL_CLEANUP_32 (password_salted_hashed );
381
+ if (!salt_hash_data (
382
+ (const uint8_t * )password ,
383
+ strlen (password ),
384
+ "keystore_seed_generation" ,
385
+ password_salted_hashed )) {
386
+ return KEYSTORE_ERR_SALT ;
387
+ }
388
+
389
+ for (size_t i = 0 ; i < host_entropy_size ; i ++ ) {
390
+ seed [i ] ^= password_salted_hashed [i ];
391
+ }
392
+ return keystore_encrypt_and_store_seed (seed , host_entropy_size , password );
393
+ }
394
+
388
395
keystore_error_t keystore_unlock (
389
396
const char * password ,
390
397
uint8_t * remaining_attempts_out ,
0 commit comments