-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUID_identity.c
More file actions
277 lines (240 loc) · 7.4 KB
/
UID_identity.c
File metadata and controls
277 lines (240 loc) · 7.4 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (c) 2016-2018. Uniquid Inc. or its affiliates. All Rights Reserved.
*
* License is in the "LICENSE" file accompanying this file.
* See the License for the specific language governing permissions and limitations under the License.
*/
/*
* @file UID_identity.c
*
* @date 1/aug/2016
* @author M. Palumbi
*/
/**
* @file UID_identity.h
*
* identity functions
*
*/
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include <inttypes.h>
#include "curves.h"
#include "secp256k1.h"
#include "bip32.h"
#include "UID_utils.h"
#include "UID_identity.h"
#include "rand.h"
#ifdef UID_EMBEDDED
#include "UID_persistence.h"
#else
#include <fcntl.h>
char *identityDB = UID_DEFAULT_IDENTITY_FILE;
static char lbuffer[1024];
static char *load_tprv(char *privateKey, size_t size)
{
FILE *id;
char format[64];
char *tprv = NULL;
if ((id = fopen(identityDB, "r")) != NULL)
{
while(fgets(lbuffer, sizeof(lbuffer), id) != NULL)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
snprintf(format, sizeof(format), "privateKey: %%%zus\n", size - 1);
if (sscanf(lbuffer, format, privateKey) == 1) tprv = privateKey; // if read OK assign to tprv
#pragma GCC diagnostic pop
}
fclose(id);
}
return tprv;
}
static void store_tprv(char *privateKey)
{
FILE *id;
if ((id = fopen(identityDB, "w")) != NULL)
{
fprintf(id, "privateKey: %s\n", privateKey);
fclose(id);
}
}
#endif
static HDNode node_m;
static HDNode node_m_44H_0H; // imprinting
static char tpub[120];
static HDNode node_m_44H_0H_0; // orchestrator
static HDNode node_m_44H_0H_0_x[2][2];
// ^ ^
// provider/user________| |
// intern/extern___________|
static HDNode *UID_deriveAtfull(UID_Bip32Path *path, HDNode *node)
{
memcpy( node, &node_m_44H_0H_0, sizeof(HDNode) );
hdnode_private_ckd(node, path->p_u);
hdnode_private_ckd(node, path->account);
hdnode_private_ckd(node, path->n);
return node;
}
static HDNode *UID_deriveAt(UID_Bip32Path *path, HDNode *node)
{
if (path->p_u > 1 )
return UID_deriveAtfull(path, node);
if (path->account > 1 )
return UID_deriveAtfull(path, node);
memcpy( node, &node_m_44H_0H_0_x[path->p_u][path->account], sizeof(HDNode));
hdnode_private_ckd(node, path->n);
// hdnode_fill_public_key(node);
return node;
}
static void derive_m_44H_0H_x(void)
{
uint32_t fingerprint;
// [Chain m/44']
memcpy(&node_m_44H_0H, &node_m, sizeof(HDNode));
hdnode_private_ckd_prime(&node_m_44H_0H, 44);
// [Chain m/44'/0']
fingerprint = hdnode_fingerprint(&node_m_44H_0H);
//memcpy(&node_m_44H_0H, &node_m_44H_0H, sizeof(HDNode));
hdnode_private_ckd_prime(&node_m_44H_0H, 0);
hdnode_fill_public_key(&node_m_44H_0H);
hdnode_serialize_public(&node_m_44H_0H, fingerprint, tpub, sizeof(tpub));
// [Chain m/44'/0'/0]
memcpy(&node_m_44H_0H_0, &node_m_44H_0H, sizeof(HDNode));
hdnode_private_ckd(&node_m_44H_0H_0, 0);
// [Chain m/44'/0'/0/0]
memcpy(&node_m_44H_0H_0_x[0][0], &node_m_44H_0H_0, sizeof(HDNode));
hdnode_private_ckd(&node_m_44H_0H_0_x[0][0], 0);
memcpy(&node_m_44H_0H_0_x[0][1], &node_m_44H_0H_0_x[0][0], sizeof(HDNode));
// [Chain m/44'/0'/0/0/0]
hdnode_private_ckd(&node_m_44H_0H_0_x[0][0], 0);
// [Chain m/44'/0'/0/0/1]
hdnode_private_ckd(&node_m_44H_0H_0_x[0][1], 1);
// [Chain m/44'/0'/0/1]
memcpy(&node_m_44H_0H_0_x[1][0], &node_m_44H_0H_0, sizeof(HDNode));
hdnode_private_ckd(&node_m_44H_0H_0_x[1][0], 1);
memcpy(&node_m_44H_0H_0_x[1][1], &node_m_44H_0H_0_x[1][0], sizeof(HDNode));
// [Chain m/44'/0'/0/1/0]
hdnode_private_ckd(&node_m_44H_0H_0_x[1][0], 0);
// [Chain m/44'/0'/0/1/1]
hdnode_private_ckd(&node_m_44H_0H_0_x[1][1], 1);
}
/**
* load/create and store the identity of the Entity (xprv @ node m).<br>
* this function must be called before all other library functions.
*
* if the file named identityDB exists, the identity is loaded from it
* else a new identity is created from random.<br>
* the identity is then saved in the file named identityDB.
*
* @param[in] tprv if != NULL the value is used to force the identity (identityDB take precedence).
*
*/
void UID_getLocalIdentity(char *tprv)
{
char privateKey[256];
if (load_tprv(privateKey, sizeof(privateKey)) != NULL)
tprv = privateKey; // if read OK assign to tprv
if(tprv == NULL)
{
uint8_t seed[32];
random_buffer(seed, sizeof(seed));
hdnode_from_seed(seed, sizeof(seed), SECP256K1_NAME, &node_m);
}
else
{
hdnode_deserialize(tprv, &node_m);
}
derive_m_44H_0H_x();
if (tprv != privateKey) {
memset(privateKey, 0, sizeof(privateKey));
hdnode_serialize_private(&node_m, 0 /*uint32_t fingerprint*/, privateKey, sizeof(privateKey));
store_tprv(privateKey);
}
}
/**
* Returns the tprv string from seed
*
* @param[in] seed 32 byte buffer containing the seed (binary form)
* @param[out] tprv string buffer to be filled with the tprv
* @param[in] tprvsize size of the tprv buffer
* @return string containin the tprv
*/
char *UID_tprvFromSeed(uint8_t seed[32], char *tprv, int tprvsize)
{
HDNode node;
hdnode_from_seed(seed, 32, SECP256K1_NAME, &node);
hdnode_serialize_private(&node, 0 /*uint32_t fingerprint*/, tprv, tprvsize);
return tprv;
}
/**
* Returns the xpub for imprinting
*
* @return xpub @ m/44'/0'
*/
char *UID_getTpub(void)
{
return tpub;
}
/**
* Signs a 32 byte digest with the key @ a given bip32 path
* of the identity.
*
* @param[in] path bip32 path of the private-key to use
* @param[in] hash 32 bytes long buffer holding the digest to be signed
* @param[out] sig pointer to a 64 bytes long buffer to be filled with the signature
* @param[out] pby pointer to signature recovery byte - it can be NULL
* @return 0 == no error
*
* \todo improve error handling
*/
int UID_signAt(UID_Bip32Path *path, uint8_t hash[32], uint8_t sig[64], uint8_t *pby)
{
//uint8_t pby = 0;
HDNode node;
if(pby) *pby = 0;
UID_deriveAt(path, &node);
ecdsa_sign_digest(&secp256k1, node.private_key, hash, sig, pby);
return 0;
}
/**
* Returns the public key @ a given bip32 path
* of the identity.
*
* @param[in] path bip32 path of the private-key to use
* @param[out] public_key pointer to a 33 bytes long buffer
* to be filled with the public key
* @return 0 == no error
*
* \todo improve error handling
*/
int UID_getPubkeyAt(UID_Bip32Path *path, uint8_t public_key[33])
{
HDNode node;
UID_deriveAt(path, &node);
hdnode_fill_public_key(&node);
memcpy(public_key, node.public_key, 33);
return 0;
}
/**
* Returns the address @ a given bip32 path
* of the identity.
*
* @param[in] path bip32 path of the private-key to use
* @param[out] b58addr pointer to a buffer to be filled with
* the address
* @param[in] size size of the buffer pointed by b58addr
* @return 0 == no error
*
* \todo improve error handling
*/
int UID_getAddressAt(UID_Bip32Path *path, char *b58addr, size_t size)
{
HDNode node;
UID_deriveAt(path, &node);
hdnode_fill_public_key(&node);
ecdsa_get_address(node.public_key, /*version*/ NETWORK_BYTE, b58addr, size);
return 0;
}