22
22
#include <memory/bitbox02_smarteeprom.h>
23
23
#include <memory/memory.h>
24
24
#include <memory/smarteeprom.h>
25
+ #include <mock_memory.h>
25
26
#include <secp256k1_ecdsa_s2c.h>
26
27
#include <secp256k1_recovery.h>
27
28
#include <secp256k1_schnorrsig.h>
34
35
35
36
#define PASSWORD ("password")
36
37
38
+ int __real_securechip_kdf (securechip_slot_t slot , const uint8_t * msg , size_t len , uint8_t * kdf_out );
37
39
int __wrap_securechip_kdf (securechip_slot_t slot , const uint8_t * msg , size_t len , uint8_t * kdf_out )
38
40
{
39
41
check_expected (slot );
40
- check_expected (msg );
41
- memcpy (kdf_out , (const uint8_t * )mock (), 32 );
42
- return 0 ;
42
+ return __real_securechip_kdf (slot , msg , len , kdf_out );
43
43
}
44
44
45
+ static uint8_t _salt_root [KEYSTORE_MAX_SEED_LENGTH ] = {
46
+ 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x44 , 0x44 , 0x44 , 0x44 , 0x44 , 0x44 , 0x44 , 0x44 ,
47
+ 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 ,
48
+ };
49
+
45
50
static uint8_t _mock_seed [32 ] = {
46
51
0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 ,
47
52
0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x44 , 0x44 , 0x44 , 0x44 , 0x44 , 0x44 , 0x44 , 0x44 ,
@@ -74,42 +79,9 @@ static const uint8_t _expected_seckey[32] = {
74
79
0x58 , 0x92 , 0x32 , 0x9d , 0x67 , 0xdf , 0xd4 , 0xad , 0x05 , 0xe9 , 0xc3 , 0xd0 , 0x6e , 0xdf , 0x74 , 0xfb ,
75
80
};
76
81
77
- static uint8_t _password_salted_hashed_stretch_in [32 ] = {
78
- 0x5e , 0x88 , 0x48 , 0x98 , 0xda , 0x28 , 0x04 , 0x71 , 0x51 , 0xd0 , 0xe5 , 0x6f , 0x8d , 0xc6 , 0x29 , 0x27 ,
79
- 0x73 , 0x60 , 0x3d , 0x0d , 0x6a , 0xab , 0xbd , 0xd6 , 0x2a , 0x11 , 0xef , 0x72 , 0x1d , 0x15 , 0x42 , 0xd8 ,
80
- };
81
-
82
- static uint8_t _password_salted_hashed_stretch_out [32 ] = {
83
- 0x73 , 0x60 , 0x3d , 0x0d , 0x6a , 0xab , 0xbd , 0xd6 , 0x2a , 0x11 , 0xef , 0x72 , 0x1d , 0x15 , 0x42 , 0xd8 ,
84
- 0x5e , 0x88 , 0x48 , 0x98 , 0xda , 0x28 , 0x04 , 0x71 , 0x51 , 0xd0 , 0xe5 , 0x6f , 0x8d , 0xc6 , 0x29 , 0x27 ,
85
- };
86
-
87
- static uint8_t _password_salted_hashed_stretch_out_invalid [32 ] = {
88
- 0x72 , 0x60 , 0x3d , 0x0d , 0x6a , 0xab , 0xbd , 0xd6 , 0x2a , 0x11 , 0xef , 0x72 , 0x1d , 0x15 , 0x42 , 0xd8 ,
89
- 0x5e , 0x88 , 0x48 , 0x98 , 0xda , 0x28 , 0x04 , 0x71 , 0x51 , 0xd0 , 0xe5 , 0x6f , 0x8d , 0xc6 , 0x29 , 0x27 ,
90
- };
91
-
92
- static uint8_t _kdf_out_1 [32 ] = {
93
- 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 ,
94
- 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 , 0x11 ,
95
- };
96
-
97
- static uint8_t _kdf_out_2 [32 ] = {
98
- 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 ,
99
- 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 , 0x22 ,
100
- };
101
-
102
- static uint8_t _kdf_out_3 [32 ] = {
103
- 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 ,
104
- 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 , 0x33 ,
105
- };
106
-
107
- // Fixture: hmac.new(_password_salted_hashed_stretch_out, _kdf_out_3,
108
- // hashlib.sha256).hexdigest()
109
- static uint8_t _expected_secret [32 ] = {
110
- 0x39 , 0xa7 , 0x4f , 0x75 , 0xb6 , 0x9d , 0x6c , 0x84 , 0x5e , 0x18 , 0x91 , 0x5b , 0xae , 0x29 , 0xd1 , 0x06 ,
111
- 0x12 , 0x12 , 0x40 , 0x37 , 0x7a , 0x79 , 0x97 , 0x55 , 0xd7 , 0xcc , 0xe9 , 0x26 , 0x1e , 0x16 , 0x91 , 0x71 ,
112
- };
82
+ static uint8_t _expected_secret [32 ] =
83
+ "\xa8\xf4\xfe\x54\x33\x0e\x1a\xb7\xa0\xe3\xbe\x8a\x8d\x75\xd2\x22\xb2\xae\xc2\xb3\xab\x41\xca"
84
+ "\x2a\x04\x0e\xa0\x08\x60\x6b\xaf\xce" ;
113
85
114
86
int __real_secp256k1_anti_exfil_sign (
115
87
const secp256k1_context * ctx ,
@@ -140,6 +112,11 @@ int __wrap_secp256k1_anti_exfil_sign(
140
112
return __real_secp256k1_anti_exfil_sign (ctx , sig , msg32 , seckey , host_data32 , recid );
141
113
}
142
114
115
+ bool __real_salt_hash_data (
116
+ const uint8_t * data ,
117
+ size_t data_len ,
118
+ const char * purpose ,
119
+ uint8_t * hash_out );
143
120
bool __wrap_salt_hash_data (
144
121
const uint8_t * data ,
145
122
size_t data_len ,
@@ -149,8 +126,7 @@ bool __wrap_salt_hash_data(
149
126
check_expected (data );
150
127
check_expected (data_len );
151
128
check_expected (purpose );
152
- memcpy (hash_out , (const void * )mock (), 32 );
153
- return true;
129
+ return __real_salt_hash_data (data , data_len , purpose , hash_out );
154
130
}
155
131
156
132
bool __real_cipher_aes_hmac_encrypt (
@@ -324,42 +300,32 @@ static void _test_keystore_secp256k1_sign(void** state)
324
300
}
325
301
}
326
302
327
- static void _expect_stretch (bool valid )
303
+ static void _expect_stretch (const char * password )
328
304
{
329
- expect_memory (__wrap_salt_hash_data , data , PASSWORD , strlen (PASSWORD ));
330
- expect_value (__wrap_salt_hash_data , data_len , strlen (PASSWORD ));
305
+ expect_memory (__wrap_salt_hash_data , data , password , strlen (password ));
306
+ expect_value (__wrap_salt_hash_data , data_len , strlen (password ));
331
307
expect_string (__wrap_salt_hash_data , purpose , "keystore_seed_access_in" );
332
- will_return (__wrap_salt_hash_data , _password_salted_hashed_stretch_in );
333
308
334
309
// KDF 1
335
310
expect_value (__wrap_securechip_kdf , slot , SECURECHIP_SLOT_ROLLKEY );
336
- expect_memory (__wrap_securechip_kdf , msg , _password_salted_hashed_stretch_in , 32 );
337
- will_return (__wrap_securechip_kdf , _kdf_out_1 );
338
311
339
312
// KDF 2
340
313
expect_value (__wrap_securechip_kdf , slot , SECURECHIP_SLOT_KDF );
341
- expect_memory (__wrap_securechip_kdf , msg , _kdf_out_1 , 32 );
342
- will_return (__wrap_securechip_kdf , _kdf_out_2 );
343
314
344
315
// KDF 3
345
316
expect_value (__wrap_securechip_kdf , slot , SECURECHIP_SLOT_KDF );
346
- expect_memory (__wrap_securechip_kdf , msg , _kdf_out_2 , 32 );
347
- will_return (__wrap_securechip_kdf , _kdf_out_3 );
348
317
349
- expect_memory (__wrap_salt_hash_data , data , PASSWORD , strlen (PASSWORD ));
350
- expect_value (__wrap_salt_hash_data , data_len , strlen (PASSWORD ));
318
+ expect_memory (__wrap_salt_hash_data , data , password , strlen (password ));
319
+ expect_value (__wrap_salt_hash_data , data_len , strlen (password ));
351
320
expect_string (__wrap_salt_hash_data , purpose , "keystore_seed_access_out" );
352
- will_return (
353
- __wrap_salt_hash_data ,
354
- valid ? _password_salted_hashed_stretch_out : _password_salted_hashed_stretch_out_invalid );
355
321
}
356
322
357
323
static void _expect_encrypt_and_store_seed (void )
358
324
{
359
325
will_return (__wrap_memory_is_initialized , false);
360
326
361
- _expect_stretch (true ); // first stretch to encrypt
362
- _expect_stretch (true ); // second stretch to verify
327
+ _expect_stretch (PASSWORD ); // first stretch to encrypt
328
+ _expect_stretch (PASSWORD ); // second stretch to verify
363
329
364
330
expect_memory (__wrap_cipher_aes_hmac_encrypt , secret , _expected_secret , 32 );
365
331
// For the AES IV:
@@ -384,15 +350,15 @@ static void _test_keystore_create_and_unlock_twice(void** state)
384
350
_smarteeprom_reset ();
385
351
386
352
will_return (__wrap_memory_is_seeded , true);
387
- _expect_stretch (true );
353
+ _expect_stretch (PASSWORD );
388
354
assert_int_equal (KEYSTORE_OK , keystore_unlock (PASSWORD , & remaining_attempts , NULL ));
389
355
390
356
// Create new (different) seed.
391
357
_expect_encrypt_and_store_seed ();
392
358
assert_int_equal (keystore_encrypt_and_store_seed (_mock_seed_2 , 32 , PASSWORD ), KEYSTORE_OK );
393
359
394
360
will_return (__wrap_memory_is_seeded , true);
395
- _expect_stretch (true );
361
+ _expect_stretch (PASSWORD );
396
362
assert_int_equal (KEYSTORE_OK , keystore_unlock (PASSWORD , & remaining_attempts , NULL ));
397
363
}
398
364
@@ -410,7 +376,7 @@ static void _perform_some_unlocks(void)
410
376
for (int i = 0 ; i < 3 ; i ++ ) {
411
377
_reset_reset_called = false;
412
378
will_return (__wrap_memory_is_seeded , true);
413
- _expect_stretch (true );
379
+ _expect_stretch (PASSWORD );
414
380
assert_int_equal (KEYSTORE_OK , keystore_unlock (PASSWORD , & remaining_attempts , NULL ));
415
381
assert_int_equal (remaining_attempts , MAX_UNLOCK_ATTEMPTS );
416
382
assert_false (_reset_reset_called );
@@ -437,11 +403,11 @@ static void _test_keystore_unlock(void** state)
437
403
for (int i = 1 ; i <= MAX_UNLOCK_ATTEMPTS ; i ++ ) {
438
404
_reset_reset_called = false;
439
405
will_return (__wrap_memory_is_seeded , true);
440
- _expect_stretch (false );
406
+ _expect_stretch ("invalid password" );
441
407
assert_int_equal (
442
408
i >= MAX_UNLOCK_ATTEMPTS ? KEYSTORE_ERR_MAX_ATTEMPTS_EXCEEDED
443
409
: KEYSTORE_ERR_INCORRECT_PASSWORD ,
444
- keystore_unlock (PASSWORD , & remaining_attempts , NULL ));
410
+ keystore_unlock ("invalid password" , & remaining_attempts , NULL ));
445
411
assert_int_equal (remaining_attempts , MAX_UNLOCK_ATTEMPTS - i );
446
412
// Wrong password does not lock the keystore again if already unlocked.
447
413
_expect_seeded (true);
@@ -567,13 +533,10 @@ static void _test_keystore_create_and_store_seed(void** state)
567
533
const uint8_t host_entropy [32 ] =
568
534
"\x25\x56\x9b\x9a\x11\xf9\xdb\x65\x60\x45\x9e\x8e\x48\xb4\x72\x7a\x4c\x93\x53\x00\x14\x3d"
569
535
"\x97\x89\x89\xed\x55\xdb\x1d\x1b\x9c\xbe" ;
570
- const uint8_t password_salted_hashed [32 ] =
571
- "\xad\xee\x84\x29\xf5\xb6\x70\xa9\xd7\x34\x17\x1b\x70\x87\xf3\x8f\x86\x6a\x7e\x26\x5f\x9d"
572
- "\x7d\x06\xf0\x0e\x6f\xa4\x17\x54\xac\x77" ;
573
536
// expected_seed = seed_random ^ host_entropy ^ password_salted_hashed
574
537
const uint8_t expected_seed [32 ] =
575
- "\x10\x57\xbe\x05\xee\xcc\x92\xda\xd6\xd3\xc4\x52\x72\xb3\xce\xc1\xfc\x11\x1e\xc6\xe1\x1e "
576
- "\x9f\x66\x08\xfd\x67\x90\x30\xc0\xaf\xb5 " ;
538
+ "\x55\x7e\x30\x0c\xc2\x6a\x6d\xc8\x95\xb3\x62\xf1\xe0\xe3\x0a\x70\x02\xb0\xcf\x7d\x5e\xa6 "
539
+ "\x49\x4d\xb7\xbe\x34\x4e\x40\x85\x6a\x8e " ;
577
540
578
541
// Invalid seed lengths.
579
542
assert_int_equal (
@@ -591,7 +554,6 @@ static void _test_keystore_create_and_store_seed(void** state)
591
554
expect_memory (__wrap_salt_hash_data , data , PASSWORD , strlen (PASSWORD ));
592
555
expect_value (__wrap_salt_hash_data , data_len , strlen (PASSWORD ));
593
556
expect_string (__wrap_salt_hash_data , purpose , "keystore_seed_generation" );
594
- will_return (__wrap_salt_hash_data , password_salted_hashed );
595
557
_expect_encrypt_and_store_seed ();
596
558
assert_int_equal (
597
559
keystore_create_and_store_seed (PASSWORD , host_entropy , seed_len ), KEYSTORE_OK );
@@ -604,8 +566,7 @@ static void _test_keystore_create_and_store_seed(void** state)
604
566
uint8_t out [decrypted_len ];
605
567
assert_true (cipher_aes_hmac_decrypt (
606
568
encrypted_seed_and_hmac , len , out , & decrypted_len , _expected_secret ));
607
- assert_true (decrypted_len == seed_len );
608
-
569
+ assert_int_equal (decrypted_len , seed_len );
609
570
assert_memory_equal (expected_seed , out , seed_len );
610
571
}
611
572
}
@@ -818,6 +779,8 @@ static void _test_keystore_secp256k1_schnorr_bip86_sign(void** state)
818
779
819
780
int main (void )
820
781
{
782
+ mock_memory_set_salt_root (_salt_root );
783
+
821
784
const struct CMUnitTest tests [] = {
822
785
cmocka_unit_test (_test_keystore_get_xpub ),
823
786
cmocka_unit_test (_test_keystore_get_root_fingerprint ),
0 commit comments