forked from dsprenkels/sss
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
slip39_buffer.c
64 lines (53 loc) · 1.72 KB
/
slip39_buffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "slip39.h"
int decode_binary_shard(
slip39_shard *shard,
const uint8_t *buffer,
uint32_t buffer_length
) {
if(buffer_length < 12 || buffer[0] != 0x48 || buffer[1] != 0xbd || buffer[2] != 0xfd) {
return ERROR_INVALID_SHARD_BUFFER;
}
shard->identifier = (buffer[3] << 8) | buffer[4];
shard->iteration_exponent = buffer[5];
shard->group_index = buffer[6];
shard->group_threshold = buffer[7];
shard->group_count = buffer[8];
shard->member_index = buffer[9];
shard->member_threshold = buffer[10];
shard->value_length= buffer[11];
if(shard->value_length < 16) {
return ERROR_SECRET_TOO_SHORT;
}
if(shard->value_length > 32) {
return ERROR_SECRET_TOO_LONG;
}
if(buffer_length < (uint32_t) shard->value_length + 12) {
return ERROR_INVALID_SHARD_BUFFER;
}
memset(shard->value, 0, 32);
memcpy(shard->value, buffer+12, shard->value_length);
return shard->value_length + 12;
}
int encode_binary_shard(
uint8_t *buffer,
uint32_t buffer_length,
const slip39_shard *shard
) {
if(buffer_length < (uint32_t) shard->value_length + 12) {
return ERROR_INVALID_SHARD_BUFFER;
}
buffer[0] = 0x48;
buffer[1] = 0xbd;
buffer[2] = 0xfd;
buffer[3] = (uint8_t) (shard->identifier >> 8);
buffer[4] = (uint8_t) (shard->identifier & 0xff);
buffer[5] = shard->iteration_exponent;
buffer[6] = shard->group_index;
buffer[7] = shard->group_threshold;
buffer[8] = shard->group_count;
buffer[9] = shard->member_index;
buffer[10] = shard->member_threshold;
buffer[11] = shard->value_length;
memcpy(buffer+12, shard->value, shard->value_length);
return shard->value_length + 12;
}