Skip to content

Commit

Permalink
[add] Fetch RNG from env at startup then stick with it
Browse files Browse the repository at this point in the history
  • Loading branch information
lpascal-ledger committed Dec 19, 2023
1 parent f347988 commit dc9fe08
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 47 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.5.0] - 2024-??-??

### Added
- Seed is fetched from the environment during the Speculos launch and stored internally for further
use. This avoids several Speculos instances from messing up with each other's seeds.
- Seed, RNG are fetched from the environment during the Speculos launch and stored internally for
further use. This avoids several Speculos instances from messing up with each other's environment
variables.

## [0.4.1] - 2023-12-19

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ add_library(emu
emulate_blue_2.2.5.c
emulate_lnsp_1.0.c
emulate_unified_sdk.c
seed.c
environment.c
svc.c)

add_dependencies(emu openssl)
Expand Down
16 changes: 2 additions & 14 deletions src/bolos/cx.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,20 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "emulate.h"
#include "environment.h"

static bool initialized = false;

static unsigned int get_rng_seed_from_env(const char *name)
{
char *p;

p = getenv(name);
if (p != NULL) {
return atoi(p);
} else {
return time(NULL);
}
}

/* not secure, but this is clearly not the goal of this emulator */
unsigned long sys_cx_rng(uint8_t *buffer, unsigned int length)
{
unsigned int i;

if (!initialized) {
srand(get_rng_seed_from_env("RNG_SEED"));
srand(get_env_rng());
initialized = true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/bolos/os_bip32.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "cx.h"
#include "cx_utils.h"
#include "emulate.h"
#include "seed.h"
#include "environment.h"

#define BIP32_HARDEN_MASK 0x80000000
#define BIP32_SECP_SEED_LENGTH 12
Expand Down Expand Up @@ -444,7 +444,7 @@ unsigned long sys_os_perso_derive_node_with_seed_key(
sk_length = seed_key_length;
}

seed_size = get_seed(seed, sizeof(seed));
seed_size = get_env_seed(seed, sizeof(seed));

if (mode == HDW_SLIP21) {
ret = hdw_slip21(sk, sk_length, seed, seed_size, (const uint8_t *)path,
Expand Down
4 changes: 2 additions & 2 deletions src/bolos/os_eip2333.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "cx.h"
#include "cx_utils.h"
#include "emulate.h"
#include "environment.h"
#include "exception.h"
#include "seed.h"

#define MAX_SEED_SIZE 64
#define CX_SHA256_SIZE 32
Expand Down Expand Up @@ -142,7 +142,7 @@ unsigned long sys_os_perso_derive_eip2333(cx_curve_t curve,
THROW(EXCEPTION);
}

seed_size = get_seed(seed, sizeof(seed));
seed_size = get_env_seed(seed, sizeof(seed));

cx_derive_master_sk(seed, seed_size, sk);
if (privateKey != NULL) {
Expand Down
32 changes: 26 additions & 6 deletions src/seed.c → src/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include "seed.h"
#include "environment.h"

/* glory promote mansion idle axis finger extra february uncover one trip
* resource lawn turtle enact monster seven myth punch hobby comfort wild raise
Expand All @@ -16,11 +17,13 @@ static uint8_t default_seed[MAX_SEED_SIZE] =
"\x11\x44\x13\x2f\x35\xe2\x06\x87\x35\x64";

const char *SEED_ENV_NAME = "SPECULOS_SEED";
typedef struct {
const char *RNG_ENV_NAME = "RNG_SEED";

static struct {
size_t size;
uint8_t seed[MAX_SEED_SIZE];
} seed_t;
static seed_t actual_seed = { 0 };
} actual_seed = { 0 };
static unsigned int actual_rng = 0;

static int unhex(uint8_t *dst, size_t dst_size, const char *src,
size_t src_size)
Expand Down Expand Up @@ -59,7 +62,7 @@ static int unhex(uint8_t *dst, size_t dst_size, const char *src,
return src_size / 2;
}

void init_seed()
void init_env_seed()
{
ssize_t size;
char *p;
Expand All @@ -86,8 +89,25 @@ void init_seed()
actual_seed.size = size;
}

size_t get_seed(uint8_t *seed, size_t max_size)
size_t get_env_seed(uint8_t *seed, size_t max_size)
{
memcpy(seed, actual_seed.seed, max_size);
return (actual_seed.size < max_size) ? actual_seed.size : max_size;
}

void init_env_rng()
{
char *p;
p = getenv(RNG_ENV_NAME);
if (p != NULL) {
actual_rng = atoi(p);
fprintf(stderr, "[*] Seed initialized from environment: '%ud'\n", actual_rng);
} else {
actual_rng = time(NULL);
fprintf(stderr, "[*] Seed initialized by default: '%ud'\n", actual_rng);
}
}

unsigned int get_env_rng() {
return actual_rng;
}
11 changes: 11 additions & 0 deletions src/environment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <stdint.h>

#define MAX_SEED_SIZE 64

void init_env_seed();
size_t get_env_seed(uint8_t *seed, size_t max_size);

void init_env_rng();
unsigned int get_env_rng();
5 changes: 3 additions & 2 deletions src/launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include <unistd.h>

#include "emulate.h"
#include "environment.h"
#include "fonts.h"
#include "seed.h"
#include "svc.h"

#define LOAD_ADDR ((void *)0x40000000)
Expand Down Expand Up @@ -723,7 +723,8 @@ int main(int argc, char *argv[])
extra_rampage_size = 0;

fprintf(stderr, "[*] speculos launcher revision: " GIT_REVISION "\n");
init_seed();
init_env_seed();
init_env_rng();

while ((opt = getopt(argc, argv, "c:tr:s:m:k:a:f:")) != -1) {
switch (opt) {
Expand Down
8 changes: 0 additions & 8 deletions src/seed.h

This file was deleted.

8 changes: 4 additions & 4 deletions tests/syscalls/test_bip32.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "bolos/cx_utils.h"
#include "bolos/os_bip32.h"
#include "emulate.h"
#include "seed.h"
#include "environment.h"
#include "utils.h"

#define MAX_CHAIN_LEN 5
Expand Down Expand Up @@ -579,7 +579,7 @@ static void test_bip32_vector(const bip32_test_vector *v)
memset(&extkey, 0, sizeof(extkey));

assert_int_equal(setenv("SPECULOS_SEED", v->seed, 1), 0);
init_seed();
init_env_seed();

for (i = 0; i < v->chain_len; i++) {
path[i] = v->chain[i].index;
Expand All @@ -601,7 +601,7 @@ static void test_bip32_vector(const bip32_test_vector *v)
static void test_bip32(void **state __attribute__((unused)))
{
size_t i;
init_seed();
init_env_seed();
for (i = 0; i < ARRAY_SIZE(test_vectors); i++) {
test_bip32_vector(&test_vectors[i]);
}
Expand All @@ -616,7 +616,7 @@ static void test_bolos_vector(const struct bolos_vector *v)
size_t sk_length;
ssize_t path_len;
uint8_t *p;
init_seed();
init_env_seed();

switch (v->mode) {
case 0:
Expand Down
8 changes: 4 additions & 4 deletions tests/syscalls/test_eip2333.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "bolos/cx.h"
#include "bolos/cx_utils.h"
#include "emulate.h"
#include "seed.h"
#include "environment.h"
#include "utils.h"

#define MAX_PATH_LEN 10
Expand Down Expand Up @@ -229,7 +229,7 @@ static void test_eip_vector(const eip2333_test_vector *v)
int path_len;

assert_int_equal(setenv("SPECULOS_SEED", v->seed, 1), 0);
init_seed();
init_env_seed();

path_len = get_path(v->path, path, MAX_PATH_LEN);
assert_int_equal(path_len, 1);
Expand All @@ -247,7 +247,7 @@ static void test_eip2333_derive(void **state __attribute__((unused)))
unsigned int i;

assert_int_equal(setenv("SPECULOS_SEED", default_seed, 1), 0);
init_seed();
init_env_seed();

for (i = 0; i < ARRAY_SIZE(test_vectors); i++) {
test_eip_vector(&test_vectors[i]);
Expand All @@ -260,7 +260,7 @@ static void test_bolos_vector(const bolos_test_vector *v)
unsigned int path[10];
int path_len;

init_seed();
init_env_seed();
path_len = get_path(v->path, path, MAX_PATH_LEN);
assert_int_equal(path_len, v->path_len);

Expand Down
4 changes: 2 additions & 2 deletions tests/syscalls/test_slip21.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "bolos/cx.h"
#include "bolos/os_bip32.h"
#include "emulate.h"
#include "seed.h"
#include "environment.h"

void test_slip21(void **state __attribute__((unused)))
{
Expand All @@ -31,7 +31,7 @@ void test_slip21(void **state __attribute__((unused)))
1),
0);

init_seed();
init_env_seed();

sys_os_perso_derive_node_with_seed_key(HDW_SLIP21, CX_CURVE_SECP256K1,
(uint32_t *)SLIP77_LABEL, 10, key,
Expand Down

0 comments on commit dc9fe08

Please sign in to comment.