Skip to content

Commit b3e384f

Browse files
committed
keystore: move funcs
To be below the static function `_retain_seed()` that will be needed later.
1 parent db63e39 commit b3e384f

File tree

1 file changed

+80
-80
lines changed

1 file changed

+80
-80
lines changed

src/keystore.c

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -224,86 +224,6 @@ static bool _verify_seed(
224224
return true;
225225
}
226226

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-
307227
USE_RESULT static keystore_error_t _retain_seed(const uint8_t* seed, size_t seed_len)
308228
{
309229
#ifdef TESTING
@@ -385,6 +305,86 @@ static void _delete_retained_seeds(void)
385305
_retained_bip39_seed_encrypted_len = 0;
386306
}
387307

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+
return KEYSTORE_OK;
349+
}
350+
351+
keystore_error_t keystore_create_and_store_seed(
352+
const char* password,
353+
const uint8_t* host_entropy,
354+
size_t host_entropy_size)
355+
{
356+
if (host_entropy_size != 16 && host_entropy_size != 32) {
357+
return KEYSTORE_ERR_SEED_SIZE;
358+
}
359+
if (KEYSTORE_MAX_SEED_LENGTH != RANDOM_NUM_SIZE) {
360+
Abort("keystore create: size mismatch");
361+
}
362+
uint8_t seed[KEYSTORE_MAX_SEED_LENGTH];
363+
UTIL_CLEANUP_32(seed);
364+
random_32_bytes(seed);
365+
366+
// Mix in Host entropy.
367+
for (size_t i = 0; i < host_entropy_size; i++) {
368+
seed[i] ^= host_entropy[i];
369+
}
370+
371+
// Mix in entropy derived from the user password.
372+
uint8_t password_salted_hashed[KEYSTORE_MAX_SEED_LENGTH] = {0};
373+
UTIL_CLEANUP_32(password_salted_hashed);
374+
if (!salt_hash_data(
375+
(const uint8_t*)password,
376+
strlen(password),
377+
"keystore_seed_generation",
378+
password_salted_hashed)) {
379+
return KEYSTORE_ERR_SALT;
380+
}
381+
382+
for (size_t i = 0; i < host_entropy_size; i++) {
383+
seed[i] ^= password_salted_hashed[i];
384+
}
385+
return keystore_encrypt_and_store_seed(seed, host_entropy_size, password);
386+
}
387+
388388
keystore_error_t keystore_unlock(
389389
const char* password,
390390
uint8_t* remaining_attempts_out,

0 commit comments

Comments
 (0)