Skip to content

Commit

Permalink
Changed several datatypes to better suit values stored
Browse files Browse the repository at this point in the history
  • Loading branch information
aido committed Nov 9, 2023
1 parent d5a076d commit 3a28df7
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 63 deletions.
12 changes: 6 additions & 6 deletions src/bc-sskr/shard.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

typedef struct sskr_shard_struct {
uint16_t identifier;
size_t group_index;
size_t group_threshold;
size_t group_count;
size_t member_index;
size_t member_threshold;
size_t value_len;
uint8_t group_index;
uint8_t group_threshold;
uint8_t group_count;
uint8_t member_index;
uint8_t member_threshold;
uint8_t value_len;
uint8_t value[32];
} sskr_shard;

Expand Down
60 changes: 30 additions & 30 deletions src/bc-sskr/sskr.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#define memzero(...) explicit_bzero(__VA_ARGS__)

static int check_secret_length(size_t len) {
static int check_secret_length(uint8_t len) {
if (len < SSKR_MIN_STRENGTH_BYTES) {
return SSKR_ERROR_SECRET_TOO_SHORT;
}
Expand Down Expand Up @@ -45,15 +45,15 @@ static int serialize_shard(const sskr_shard *shard, uint8_t *destination, size_t
// reserved (MUST be zero): 4
// member-index: 4

uint32_t id = shard->identifier & 0xffff;
uint32_t gt = (shard->group_threshold - 1) & 0xf;
uint32_t gc = (shard->group_count - 1) & 0xf;
uint32_t gi = shard->group_index & 0xf;
uint32_t mt = (shard->member_threshold - 1) & 0xf;
uint32_t mi = shard->member_index & 0xf;
uint16_t id = shard->identifier;
uint8_t gt = (shard->group_threshold - 1) & 0xf;
uint8_t gc = (shard->group_count - 1) & 0xf;
uint8_t gi = shard->group_index & 0xf;
uint8_t mt = (shard->member_threshold - 1) & 0xf;
uint8_t mi = shard->member_index & 0xf;

uint32_t id1 = id >> 8;
uint32_t id2 = id & 0xff;
uint8_t id1 = id >> 8;
uint8_t id2 = id & 0xff;

destination[0] = id1;
destination[1] = id2;
Expand All @@ -71,8 +71,8 @@ static int deserialize_shard(const uint8_t *source, size_t source_len, sskr_shar
return SSKR_ERROR_NOT_ENOUGH_SERIALIZED_BYTES;
}

size_t group_threshold = (source[2] >> 4) + 1;
size_t group_count = (source[2] & 0xf) + 1;
uint8_t group_threshold = (source[2] >> 4) + 1;
uint8_t group_count = (source[2] & 0xf) + 1;

if (group_threshold > group_count) {
return SSKR_ERROR_INVALID_GROUP_THRESHOLD;
Expand All @@ -83,7 +83,7 @@ static int deserialize_shard(const uint8_t *source, size_t source_len, sskr_shar
shard->group_count = group_count;
shard->group_index = source[3] >> 4;
shard->member_threshold = (source[3] & 0xf) + 1;
size_t reserved = source[4] >> 4;
uint8_t reserved = source[4] >> 4;
if (reserved != 0) {
return SSKR_ERROR_INVALID_RESERVED_BITS;
}
Expand All @@ -98,10 +98,10 @@ static int deserialize_shard(const uint8_t *source, size_t source_len, sskr_shar
return shard->value_len;
}

int sskr_count_shards(size_t group_threshold,
int sskr_count_shards(uint8_t group_threshold,
const sskr_group_descriptor *groups,
size_t groups_len) {
size_t shard_count = 0;
uint8_t groups_len) {
uint8_t shard_count = 0;

if (groups_len < 1) {
return SSKR_ERROR_INVALID_GROUP_LENGTH;
Expand All @@ -111,7 +111,7 @@ int sskr_count_shards(size_t group_threshold,
return SSKR_ERROR_INVALID_GROUP_THRESHOLD;
}

for (int i = 0; i < (int) groups_len; ++i) {
for (uint8_t i = 0; i < groups_len; ++i) {
if (groups[i].count < 1) {
return SSKR_ERROR_INVALID_GROUP_COUNT;
}
Expand Down Expand Up @@ -215,12 +215,12 @@ static int generate_shards(uint8_t group_threshold,
//////////////////////////////////////////////////
// generate mnemonics
//
int sskr_generate(size_t group_threshold,
int sskr_generate(uint8_t group_threshold,
const sskr_group_descriptor *groups,
size_t groups_len,
uint8_t groups_len,
const uint8_t *master_secret,
size_t master_secret_len,
size_t *shard_len,
uint8_t *shard_len,
uint8_t *output,
size_t buffer_size,
unsigned char *(*random_generator)(uint8_t *, size_t)) {
Expand All @@ -230,7 +230,7 @@ int sskr_generate(size_t group_threshold,
}

// Figure out how many shards we are dealing with
int total_shards = sskr_count_shards(group_threshold, groups, groups_len);
uint8_t total_shards = sskr_count_shards(group_threshold, groups, groups_len);
if (total_shards < 0) {
return total_shards;
}
Expand Down Expand Up @@ -299,25 +299,25 @@ typedef struct sskr_group_struct {
* in place, so it is for internal use only, however it provides the implementation
* for both combine_shards and sskr_combine.
*/
static int combine_shards_internal(sskr_shard *shards, // array of shard structures
size_t shards_count, // number of shards in array
static int combine_shards_internal(sskr_shard *shards, // array of shard structures
uint8_t shards_count, // number of shards in array
uint8_t *buffer, // working space, and place to return secret
size_t buffer_len // total amount of working space
) {
int error = 0;
uint16_t identifier = 0;
size_t group_threshold = 0;
size_t group_count = 0;
uint8_t group_threshold = 0;
uint8_t group_count = 0;

if (shards_count == 0) {
return SSKR_ERROR_EMPTY_SHARD_SET;
}

size_t next_group = 0;
uint8_t next_group = 0;
sskr_group groups[16];
size_t secret_len = 0;

for (unsigned int i = 0; i < shards_count; ++i) {
for (uint8_t i = 0; i < shards_count; ++i) {
sskr_shard *shard = &shards[i];

if (i == 0) {
Expand All @@ -336,13 +336,13 @@ static int combine_shards_internal(sskr_shard *shards, // array of shard struc

// sort shards into member groups
bool group_found = false;
for (int j = 0; j < (int) next_group; ++j) {
for (uint8_t j = 0; j < next_group; ++j) {
if (shard->group_index == groups[j].group_index) {
group_found = true;
if (shard->member_threshold != groups[j].member_threshold) {
return SSKR_ERROR_INVALID_MEMBER_THRESHOLD;
}
for (int k = 0; k < (int) groups[j].count; ++k) {
for (uint8_t k = 0; k < groups[j].count; ++k) {
if (shard->member_index == groups[j].member_index[k]) {
return SSKR_ERROR_DUPLICATE_MEMBER_INDEX;
}
Expand Down Expand Up @@ -435,8 +435,8 @@ static int combine_shards_internal(sskr_shard *shards, // array of shard struc
// sskr_combine

int sskr_combine(const uint8_t **input_shards, // array of pointers to 10-bit words
size_t shard_len, // number of bytes in each serialized shard
size_t shards_count, // total number of shards
uint8_t shard_len, // number of bytes in each serialized shard
uint8_t shards_count, // total number of shards
uint8_t *buffer, // working space, and place to return secret
size_t buffer_len // total amount of working space
) {
Expand Down
14 changes: 7 additions & 7 deletions src/bc-sskr/sskr.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "sskr-constants.h"
#include "group.h"

int sskr_count_shards(size_t group_threshold,
int sskr_count_shards(uint8_t group_threshold,
const sskr_group_descriptor *groups,
size_t groups_len);
uint8_t groups_len);

/**
* generate a set of shards that can be used to reconstruct a secret
Expand All @@ -39,12 +39,12 @@ int sskr_count_shards(size_t group_threshold,
* output[i*shard_len]..output[(i+1)*shard_len -1]
* buffer_size: maximum number of bytes to write to the output array
*/
int sskr_generate(size_t group_threshold,
int sskr_generate(uint8_t group_threshold,
const sskr_group_descriptor *groups,
size_t groups_length,
uint8_t groups_length,
const uint8_t *master_secret,
size_t master_secret_length,
size_t *shard_len,
uint8_t *shard_len,
uint8_t *output,
size_t buffer_size,
unsigned char *(*random_generator)(uint8_t *, size_t));
Expand All @@ -62,8 +62,8 @@ int sskr_generate(size_t group_threshold,
* buffer_length: maximum space available in buffer
*/
int sskr_combine(const uint8_t **input_shards, // an array of pointers to serialized shards
size_t shard_len, // number of bytes in each serialized shard
size_t shards_count, // total number of shards
uint8_t shard_len, // number of bytes in each serialized shard
uint8_t shards_count, // total number of shards
uint8_t *buffer, // working space, and place to return secret
size_t buffer_length // total amount of working space
);
Expand Down
38 changes: 19 additions & 19 deletions src/ux_common/onboarding_seed_sskr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ uint32_t cx_crc32_hw_nbo(const uint8_t *bytes, size_t len) {
#endif
}

unsigned int bolos_ux_sskr_size_get(unsigned int bip39_onboarding_kind,
unsigned int groups_threshold,
unsigned int bolos_ux_sskr_size_get(uint8_t bip39_onboarding_kind,
uint8_t groups_threshold,
unsigned int *group_descriptor,
unsigned int groups_len,
size_t *share_len) {
uint8_t groups_len,
uint8_t *share_len) {
sskr_group_descriptor groups[groups_len];
for (uint8_t i = 0; i < (uint8_t) groups_len; i++) {
for (uint8_t i = 0; i < groups_len; i++) {
groups[i].threshold = *(group_descriptor + i * sizeof(*(group_descriptor)) / groups_len);
groups[i].count = *(group_descriptor + 1 + i * sizeof(*(group_descriptor)) / groups_len);
}
Expand Down Expand Up @@ -82,16 +82,16 @@ void bolos_ux_sskr_hex_to_seed(unsigned char *mnemonic_hex,
bolos_ux_bip39_mnemonic_to_seed(words_buffer, *words_buffer_length, seed);
}

unsigned int bolos_ux_sskr_generate(unsigned int groups_threshold,
unsigned int bolos_ux_sskr_generate(uint8_t groups_threshold,
unsigned int *group_descriptor,
unsigned int groups_len,
uint8_t groups_len,
unsigned char *seed,
unsigned int seed_len,
size_t *share_len,
uint8_t *share_len,
unsigned char *share_buffer,
unsigned int share_buffer_len,
size_t share_len_expected,
unsigned int share_count_expected) {
uint8_t share_len_expected,
uint8_t share_count_expected) {
sskr_group_descriptor groups[groups_len];
for (uint8_t i = 0; i < (uint8_t) groups_len; i++) {
groups[i].threshold = *(group_descriptor + i * 2);
Expand Down Expand Up @@ -168,18 +168,18 @@ unsigned int bolos_ux_bip39_to_sskr_convert(unsigned char *bip39_words_buffer,
seed_len + 1) == 1) {
memzero(bip39_words_buffer, bip39_words_buffer_length);
bip39_words_buffer_length = 0;
size_t groups_len = 1;
size_t groups_threshold = 1;
size_t share_len_expected = 0;
size_t share_count_expected = bolos_ux_sskr_size_get(bip39_onboarding_kind,
groups_threshold,
group_descriptor,
groups_len,
&share_len_expected);
uint8_t groups_len = 1;
uint8_t groups_threshold = 1;
uint8_t share_len_expected = 0;
uint8_t share_count_expected = bolos_ux_sskr_size_get(bip39_onboarding_kind,
groups_threshold,
group_descriptor,
groups_len,
&share_len_expected);

size_t share_buffer_len = share_count_expected * share_len_expected;
uint8_t share_buffer[share_buffer_len];
size_t share_len = 0;
uint8_t share_len = 0;
*share_count = bolos_ux_sskr_generate(groups_threshold,
group_descriptor,
groups_len,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/tests/sskr.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static void test_sskr_generate(void **state) {
const size_t seed_len = sizeof(seed);
const size_t share_buffer_len = (seed_len + SSKR_METADATA_LENGTH_BYTES) * groups[0].count;
uint8_t share_buffer[share_buffer_len];
size_t share_len;
uint8_t share_len;

int share_count = sskr_generate(groups_threshold,
groups,
Expand Down

0 comments on commit 3a28df7

Please sign in to comment.