|
17 | 17 | #include <stddef.h>
|
18 | 18 | #include <cmocka.h>
|
19 | 19 |
|
20 |
| -#include <cipher/cipher.h> |
21 | 20 | #include <keystore.h>
|
22 |
| -#include <memory/bitbox02_smarteeprom.h> |
23 |
| -#include <memory/memory.h> |
24 |
| -#include <memory/smarteeprom.h> |
25 |
| -#include <mock_memory.h> |
26 |
| -#include <secp256k1_ecdsa_s2c.h> |
27 |
| -#include <secp256k1_recovery.h> |
28 | 21 | #include <secp256k1_schnorrsig.h>
|
29 |
| -#include <securechip/securechip.h> |
30 |
| -#include <util.h> |
31 | 22 |
|
32 | 23 | #include <stdint.h>
|
33 |
| -#include <stdio.h> |
34 |
| -#include <string.h> |
35 |
| - |
36 |
| -#define PASSWORD ("password") |
37 |
| - |
38 |
| -static uint8_t _salt_root[KEYSTORE_MAX_SEED_LENGTH] = { |
39 |
| - 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, |
40 |
| - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, |
41 |
| -}; |
42 |
| - |
43 |
| -// Same as Python: |
44 |
| -// import hmac, hashlib; hmac.digest(b"unit-test", b"password", hashlib.sha256).hex() |
45 |
| -// See also: securechip_mock.c |
46 |
| -static uint8_t _expected_secret[32] = |
47 |
| - "\xe5\x6d\xe4\x48\xf5\xf1\xd2\x9c\xdc\xc0\xe0\x09\x90\x07\x30\x9a\xfe\x4d\x5a\x3e\xf2\x34\x9e" |
48 |
| - "\x99\xdc\xc4\x18\x40\xad\x98\x40\x9e"; |
49 |
| - |
50 |
| -int __real_secp256k1_anti_exfil_sign( |
51 |
| - const secp256k1_context* ctx, |
52 |
| - secp256k1_ecdsa_signature* sig, |
53 |
| - const unsigned char* msg32, |
54 |
| - const unsigned char* seckey, |
55 |
| - const unsigned char* host_data32, |
56 |
| - int* recid); |
57 |
| - |
58 |
| -static const unsigned char* _sign_expected_msg = NULL; |
59 |
| -static const unsigned char* _sign_expected_seckey = NULL; |
60 |
| -int __wrap_secp256k1_anti_exfil_sign( |
61 |
| - const secp256k1_context* ctx, |
62 |
| - secp256k1_ecdsa_signature* sig, |
63 |
| - const unsigned char* msg32, |
64 |
| - const unsigned char* seckey, |
65 |
| - const unsigned char* host_data32, |
66 |
| - int* recid) |
67 |
| -{ |
68 |
| - if (_sign_expected_msg != NULL) { |
69 |
| - assert_memory_equal(_sign_expected_msg, msg32, 32); |
70 |
| - _sign_expected_msg = NULL; |
71 |
| - } |
72 |
| - if (_sign_expected_seckey != NULL) { |
73 |
| - assert_memory_equal(_sign_expected_seckey, seckey, 32); |
74 |
| - _sign_expected_seckey = NULL; |
75 |
| - } |
76 |
| - return __real_secp256k1_anti_exfil_sign(ctx, sig, msg32, seckey, host_data32, recid); |
77 |
| -} |
78 |
| - |
79 |
| -static bool _reset_reset_called = false; |
80 |
| -void __wrap_reset_reset(void) |
81 |
| -{ |
82 |
| - _reset_reset_called = true; |
83 |
| -} |
84 |
| - |
85 |
| -void __wrap_random_32_bytes(uint8_t* buf) |
86 |
| -{ |
87 |
| - memcpy(buf, (const void*)mock(), 32); |
88 |
| -} |
89 |
| - |
90 |
| -void _mock_unlocked(const uint8_t* seed, size_t seed_len, const uint8_t* bip39_seed) |
91 |
| -{ |
92 |
| - keystore_mock_unlocked(seed, seed_len, bip39_seed); |
93 |
| -} |
94 |
| - |
95 |
| -static void _expect_encrypt_and_store_seed(void) |
96 |
| -{ |
97 |
| - will_return(__wrap_memory_is_initialized, false); |
98 |
| -} |
99 |
| - |
100 |
| -static void _test_keystore_create_and_store_seed(void** state) |
101 |
| -{ |
102 |
| - const uint8_t seed_random[32] = |
103 |
| - "\x98\xef\xa1\xb6\x0a\x83\x39\x16\x61\xa2\x4d\xc7\x4a\x80\x4f\x34\x36\xe8\x33\xe0\xaa\xbe" |
104 |
| - "\x75\xe9\x71\x1e\x5d\xef\x3a\x8f\x9f\x7c"; |
105 |
| - const uint8_t host_entropy[32] = |
106 |
| - "\x25\x56\x9b\x9a\x11\xf9\xdb\x65\x60\x45\x9e\x8e\x48\xb4\x72\x7a\x4c\x93\x53\x00\x14\x3d" |
107 |
| - "\x97\x89\x89\xed\x55\xdb\x1d\x1b\x9c\xbe"; |
108 |
| - // expected_seed = seed_random ^ host_entropy ^ password_salted_hashed |
109 |
| - const uint8_t expected_seed[32] = |
110 |
| - "\x55\x7e\x30\x0c\xc2\x6a\x6d\xc8\x95\xb3\x62\xf1\xe0\xe3\x0a\x70\x02\xb0\xcf\x7d\x5e\xa6" |
111 |
| - "\x49\x4d\xb7\xbe\x34\x4e\x40\x85\x6a\x8e"; |
112 |
| - |
113 |
| - // Invalid seed lengths. |
114 |
| - assert_int_equal( |
115 |
| - keystore_create_and_store_seed(PASSWORD, host_entropy, 8), KEYSTORE_ERR_SEED_SIZE); |
116 |
| - assert_int_equal( |
117 |
| - keystore_create_and_store_seed(PASSWORD, host_entropy, 24), KEYSTORE_ERR_SEED_SIZE); |
118 |
| - assert_int_equal( |
119 |
| - keystore_create_and_store_seed(PASSWORD, host_entropy, 40), KEYSTORE_ERR_SEED_SIZE); |
120 |
| - |
121 |
| - size_t test_sizes[2] = {16, 32}; |
122 |
| - for (size_t i = 0; i < sizeof(test_sizes) / sizeof(test_sizes[0]); i++) { |
123 |
| - size_t seed_len = test_sizes[i]; |
124 |
| - // Seed random is xored with host entropy and the salted/hashed user password. |
125 |
| - will_return(__wrap_random_32_bytes, seed_random); |
126 |
| - _expect_encrypt_and_store_seed(); |
127 |
| - assert_int_equal( |
128 |
| - keystore_create_and_store_seed(PASSWORD, host_entropy, seed_len), KEYSTORE_OK); |
129 |
| - |
130 |
| - // Decrypt and check seed. |
131 |
| - uint8_t encrypted_seed_and_hmac[96] = {0}; |
132 |
| - uint8_t len = 0; |
133 |
| - assert_true(memory_get_encrypted_seed_and_hmac(encrypted_seed_and_hmac, &len)); |
134 |
| - size_t decrypted_len = len - 48; |
135 |
| - uint8_t out[decrypted_len]; |
136 |
| - assert_true(cipher_aes_hmac_decrypt( |
137 |
| - encrypted_seed_and_hmac, len, out, &decrypted_len, _expected_secret)); |
138 |
| - assert_int_equal(decrypted_len, seed_len); |
139 |
| - assert_memory_equal(expected_seed, out, seed_len); |
140 |
| - } |
141 |
| -} |
142 | 24 |
|
143 | 25 | // This tests that `secp256k1_schnorrsig_sign()` is the correct function to be used for schnorr sigs
|
144 | 26 | // in taproot. It is a separate test because there are test vectors available for this which cannot
|
@@ -200,10 +82,7 @@ static void _test_secp256k1_schnorr_sign(void** state)
|
200 | 82 |
|
201 | 83 | int main(void)
|
202 | 84 | {
|
203 |
| - mock_memory_set_salt_root(_salt_root); |
204 |
| - |
205 | 85 | const struct CMUnitTest tests[] = {
|
206 |
| - cmocka_unit_test(_test_keystore_create_and_store_seed), |
207 | 86 | cmocka_unit_test(_test_secp256k1_schnorr_sign),
|
208 | 87 | };
|
209 | 88 | return cmocka_run_group_tests(tests, NULL, NULL);
|
|
0 commit comments