@@ -160,69 +160,6 @@ static bool _copy_bip39_seed(uint8_t* bip39_seed_out)
160160 return true;
161161}
162162
163- /**
164- * Stretch the user password using the securechip, putting the result in `kdf_out`, which must be 32
165- * bytes. `securechip_result_out`, if not NULL, will contain the error code from `securechip_kdf()`
166- * if there was a secure chip error, and 0 otherwise.
167- */
168- static keystore_error_t _stretch_password (
169- const char * password ,
170- uint8_t * kdf_out ,
171- int * securechip_result_out )
172- {
173- if (securechip_result_out != NULL ) {
174- * securechip_result_out = 0 ;
175- }
176- uint8_t password_salted_hashed [32 ] = {0 };
177- UTIL_CLEANUP_32 (password_salted_hashed );
178- if (!salt_hash_data (
179- (const uint8_t * )password ,
180- strlen (password ),
181- "keystore_seed_access_in" ,
182- password_salted_hashed )) {
183- return KEYSTORE_ERR_SALT ;
184- }
185-
186- uint8_t kdf_in [32 ] = {0 };
187- UTIL_CLEANUP_32 (kdf_in );
188- memcpy (kdf_in , password_salted_hashed , 32 );
189-
190- // First KDF on rollkey increments the monotonic counter. Call only once!
191- int securechip_result = securechip_kdf_rollkey (kdf_in , 32 , kdf_out );
192- if (securechip_result ) {
193- if (securechip_result_out != NULL ) {
194- * securechip_result_out = securechip_result ;
195- }
196- return KEYSTORE_ERR_SECURECHIP ;
197- }
198- // Second KDF does not use the counter and we call it multiple times.
199- for (int i = 0 ; i < KDF_NUM_ITERATIONS ; i ++ ) {
200- memcpy (kdf_in , kdf_out , 32 );
201- securechip_result = securechip_kdf (kdf_in , 32 , kdf_out );
202- if (securechip_result ) {
203- if (securechip_result_out != NULL ) {
204- * securechip_result_out = securechip_result ;
205- }
206- return KEYSTORE_ERR_SECURECHIP ;
207- }
208- }
209-
210- if (!salt_hash_data (
211- (const uint8_t * )password ,
212- strlen (password ),
213- "keystore_seed_access_out" ,
214- password_salted_hashed )) {
215- return KEYSTORE_ERR_SALT ;
216- }
217- if (wally_hmac_sha256 (
218- password_salted_hashed , sizeof (password_salted_hashed ), kdf_out , 32 , kdf_out , 32 ) !=
219- WALLY_OK ) {
220- return KEYSTORE_ERR_HASH ;
221- }
222-
223- return KEYSTORE_OK ;
224- }
225-
226163/**
227164 * Retrieves the encrypted seed and attempts to decrypt it using the password.
228165 *
@@ -243,9 +180,12 @@ static keystore_error_t _get_and_decrypt_seed(
243180 }
244181 uint8_t secret [32 ];
245182 UTIL_CLEANUP_32 (secret );
246- keystore_error_t result = _stretch_password (password , secret , securechip_result_out );
247- if (result != KEYSTORE_OK ) {
248- return result ;
183+ int stretch_result = securechip_stretch_password (password , secret );
184+ if (securechip_result_out != NULL ) {
185+ * securechip_result_out = stretch_result ;
186+ }
187+ if (stretch_result ) {
188+ return KEYSTORE_ERR_SECURECHIP ;
249189 }
250190 if (encrypted_len < 49 ) {
251191 Abort ("_get_and_decrypt_seed: underflow / zero size" );
@@ -307,9 +247,8 @@ keystore_error_t keystore_encrypt_and_store_seed(
307247 }
308248 uint8_t secret [32 ] = {0 };
309249 UTIL_CLEANUP_32 (secret );
310- keystore_error_t res = _stretch_password (password , secret , NULL );
311- if (res != KEYSTORE_OK ) {
312- return res ;
250+ if (securechip_stretch_password (password , secret )) {
251+ return KEYSTORE_ERR_SECURECHIP ;
313252 }
314253
315254 size_t encrypted_seed_len = seed_length + 64 ;
0 commit comments