@@ -108,7 +108,7 @@ static uint8_t* _get_bip39_seed(void)
108
108
return _retained_bip39_seed ;
109
109
}
110
110
111
- static bool _stretch_password (const char * password , uint8_t * kdf_out )
111
+ static keystore_error_t _stretch_password (const char * password , uint8_t * kdf_out )
112
112
{
113
113
uint8_t password_salted_hashed [32 ] = {0 };
114
114
UTIL_CLEANUP_32 (password_salted_hashed );
@@ -117,7 +117,7 @@ static bool _stretch_password(const char* password, uint8_t* kdf_out)
117
117
strlen (password ),
118
118
"keystore_seed_access_in" ,
119
119
password_salted_hashed )) {
120
- return false ;
120
+ return KEYSTORE_ERR_SALT ;
121
121
}
122
122
123
123
uint8_t kdf_in [32 ] = {0 };
@@ -127,13 +127,13 @@ static bool _stretch_password(const char* password, uint8_t* kdf_out)
127
127
// First KDF on SECURECHIP_SLOT_ROLLKEY increments the monotonic
128
128
// counter. Call only once!
129
129
if (!securechip_kdf (SECURECHIP_SLOT_ROLLKEY , kdf_in , 32 , kdf_out )) {
130
- return false ;
130
+ return KEYSTORE_ERR_SC_KDF ;
131
131
}
132
132
// Second KDF does not use the counter and we call it multiple times.
133
133
for (int i = 0 ; i < KDF_NUM_ITERATIONS ; i ++ ) {
134
134
memcpy (kdf_in , kdf_out , 32 );
135
135
if (!securechip_kdf (SECURECHIP_SLOT_KDF , kdf_in , 32 , kdf_out )) {
136
- return false ;
136
+ return KEYSTORE_ERR_SC_KDF ;
137
137
}
138
138
}
139
139
@@ -142,17 +142,18 @@ static bool _stretch_password(const char* password, uint8_t* kdf_out)
142
142
strlen (password ),
143
143
"keystore_seed_access_out" ,
144
144
password_salted_hashed )) {
145
- return false ;
145
+ return KEYSTORE_ERR_SALT ;
146
146
}
147
147
if (wally_hmac_sha256 (
148
148
password_salted_hashed , sizeof (password_salted_hashed ), kdf_out , 32 , kdf_out , 32 ) !=
149
149
WALLY_OK ) {
150
- return false ;
150
+ return KEYSTORE_ERR_HASH ;
151
151
}
152
152
153
- return true ;
153
+ return KEYSTORE_OK ;
154
154
}
155
- static bool _get_and_decrypt_seed (
155
+
156
+ static keystore_error_t _get_and_decrypt_seed (
156
157
const char * password ,
157
158
uint8_t * decrypted_seed_out ,
158
159
size_t * decrypted_seed_len_out ,
@@ -162,12 +163,13 @@ static bool _get_and_decrypt_seed(
162
163
UTIL_CLEANUP_32 (encrypted_seed_and_hmac );
163
164
uint8_t encrypted_len ;
164
165
if (!memory_get_encrypted_seed_and_hmac (encrypted_seed_and_hmac , & encrypted_len )) {
165
- return false ;
166
+ return KEYSTORE_ERR_MEMORY ;
166
167
}
167
168
uint8_t secret [32 ];
168
169
UTIL_CLEANUP_32 (secret );
169
- if (!_stretch_password (password , secret )) {
170
- return false;
170
+ keystore_error_t result = _stretch_password (password , secret );
171
+ if (result != KEYSTORE_OK ) {
172
+ return result ;
171
173
}
172
174
if (encrypted_len < 49 ) {
173
175
Abort ("_get_and_decrypt_seed: underflow / zero size" );
@@ -179,13 +181,13 @@ static bool _get_and_decrypt_seed(
179
181
if (* password_correct_out ) {
180
182
if (!_validate_seed_length (decrypted_len )) {
181
183
util_zero (decrypted , sizeof (decrypted ));
182
- return false ;
184
+ return KEYSTORE_ERR_SEED_SIZE ;
183
185
}
184
186
* decrypted_seed_len_out = decrypted_len ;
185
187
memcpy (decrypted_seed_out , decrypted , decrypted_len );
186
188
}
187
189
util_zero (decrypted , sizeof (decrypted ));
188
- return true ;
190
+ return KEYSTORE_OK ;
189
191
}
190
192
191
193
static bool _verify_seed (
@@ -197,7 +199,8 @@ static bool _verify_seed(
197
199
size_t seed_len ;
198
200
UTIL_CLEANUP_32 (decrypted_seed );
199
201
bool password_correct = false;
200
- if (!_get_and_decrypt_seed (password , decrypted_seed , & seed_len , & password_correct )) {
202
+ if (_get_and_decrypt_seed (password , decrypted_seed , & seed_len , & password_correct ) !=
203
+ KEYSTORE_OK ) {
201
204
return false;
202
205
}
203
206
if (!password_correct ) {
@@ -232,7 +235,7 @@ bool keystore_encrypt_and_store_seed(
232
235
}
233
236
uint8_t secret [32 ] = {0 };
234
237
UTIL_CLEANUP_32 (secret );
235
- if (! _stretch_password (password , secret )) {
238
+ if (_stretch_password (password , secret ) != KEYSTORE_OK ) {
236
239
return false;
237
240
}
238
241
@@ -302,7 +305,7 @@ static void _free_string(char** str)
302
305
keystore_error_t keystore_unlock (const char * password , uint8_t * remaining_attempts_out )
303
306
{
304
307
if (!memory_is_seeded ()) {
305
- return KEYSTORE_ERR_GENERIC ;
308
+ return KEYSTORE_ERR_UNSEEDED ;
306
309
}
307
310
uint8_t failed_attempts = bitbox02_smarteeprom_get_unlock_attempts ();
308
311
if (failed_attempts >= MAX_UNLOCK_ATTEMPTS ) {
@@ -320,8 +323,9 @@ keystore_error_t keystore_unlock(const char* password, uint8_t* remaining_attemp
320
323
UTIL_CLEANUP_32 (seed );
321
324
size_t seed_len ;
322
325
bool password_correct = false;
323
- if (!_get_and_decrypt_seed (password , seed , & seed_len , & password_correct )) {
324
- return KEYSTORE_ERR_GENERIC ;
326
+ keystore_error_t result = _get_and_decrypt_seed (password , seed , & seed_len , & password_correct );
327
+ if (result != KEYSTORE_OK ) {
328
+ return result ;
325
329
}
326
330
if (password_correct ) {
327
331
if (_is_unlocked_device ) {
0 commit comments