forked from trezor/trezor-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzkp_ecdsa.c
403 lines (343 loc) · 11.5 KB
/
zkp_ecdsa.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* Copyright (c) SatoshiLabs
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include "memzero.h"
#include "secp256k1.h"
#include "zkp_context.h"
#include "vendor/secp256k1-zkp/include/secp256k1.h"
#include "vendor/secp256k1-zkp/include/secp256k1_extrakeys.h"
#include "vendor/secp256k1-zkp/include/secp256k1_preallocated.h"
#include "vendor/secp256k1-zkp/include/secp256k1_recovery.h"
#include "zkp_ecdsa.h"
static bool is_zero_digest(const uint8_t *digest) {
const uint8_t zeroes[32] = {0};
return memcmp(digest, zeroes, 32) == 0;
}
// ECDSA compressed public key derivation
// curve has to be &secp256k1
// private_key_bytes has 32 bytes
// public_key_bytes has 33 bytes
// returns 0 on success
int zkp_ecdsa_get_public_key33(const ecdsa_curve *curve,
const uint8_t *private_key_bytes,
uint8_t *public_key_bytes) {
assert(curve == &secp256k1);
if (curve != &secp256k1) {
return 1;
}
int result = 0;
secp256k1_context *context_writable = NULL;
if (result == 0) {
context_writable = zkp_context_acquire_writable();
if (context_writable == NULL) {
result = 1;
}
}
if (result == 0) {
if (secp256k1_context_writable_randomize(context_writable) != 0) {
result = 1;
}
}
secp256k1_pubkey public_key = {0};
if (result == 0) {
if (secp256k1_ec_pubkey_create(context_writable, &public_key,
private_key_bytes) != 1) {
result = 1;
}
}
if (context_writable) {
zkp_context_release_writable();
context_writable = NULL;
}
if (result == 0) {
size_t written = 33;
const secp256k1_context *context_read_only = zkp_context_get_read_only();
int returned = secp256k1_ec_pubkey_serialize(
context_read_only, public_key_bytes, &written, &public_key,
SECP256K1_EC_COMPRESSED);
if (returned != 1 || written != 33) {
result = 1;
}
}
memzero(&public_key, sizeof(public_key));
return result;
}
// ECDSA uncompressed public key derivation
// curve has to be &secp256k1
// private_key_bytes has 32 bytes
// public_key_bytes has 65 bytes
// returns 0 on success
int zkp_ecdsa_get_public_key65(const ecdsa_curve *curve,
const uint8_t *private_key_bytes,
uint8_t *public_key_bytes) {
assert(curve == &secp256k1);
if (curve != &secp256k1) {
return 1;
}
int result = 0;
secp256k1_context *context_writable = NULL;
if (result == 0) {
context_writable = zkp_context_acquire_writable();
if (context_writable == NULL) {
result = 1;
}
}
if (result == 0) {
if (secp256k1_context_writable_randomize(context_writable) != 0) {
result = 1;
}
}
secp256k1_pubkey public_key = {0};
if (result == 0) {
if (secp256k1_ec_pubkey_create(context_writable, &public_key,
private_key_bytes) != 1) {
result = 1;
}
}
if (context_writable) {
zkp_context_release_writable();
context_writable = NULL;
}
if (result == 0) {
size_t written = 65;
const secp256k1_context *context_read_only = zkp_context_get_read_only();
int returned = secp256k1_ec_pubkey_serialize(
context_read_only, public_key_bytes, &written, &public_key,
SECP256K1_EC_UNCOMPRESSED);
if (returned != 1 || written != 65) {
result = 1;
}
}
memzero(&public_key, sizeof(public_key));
return result;
}
// ECDSA signing
// curve has to be &secp256k1
// private_key_bytes has 32 bytes
// digest has 32 bytes
// signature_bytes has 64 bytes
// pby is one byte
// returns 0 on success
int zkp_ecdsa_sign_digest(
const ecdsa_curve *curve, const uint8_t *private_key_bytes,
const uint8_t *digest, uint8_t *signature_bytes, uint8_t *pby,
int (*is_canonical)(uint8_t by, uint8_t signature_bytes[64])) {
assert(curve == &secp256k1);
if (curve != &secp256k1) {
return 1;
}
int result = 0;
if (result == 0) {
if (is_zero_digest(digest)) {
// The probability of the digest being all-zero by chance is
// infinitesimal, so this is most likely an indication of a bug.
// Furthermore, the signature has no value, because in this case it can be
// easily forged for any public key, see zkp_ecdsa_verify_digest().
result = 1;
}
}
secp256k1_context *context_writable = NULL;
if (result == 0) {
context_writable = zkp_context_acquire_writable();
if (context_writable == NULL) {
result = 1;
}
}
uint16_t retry_count = 0;
int recid = 0;
do {
if (retry_count >= 10000) {
result = 1;
}
if (result == 0) {
if (secp256k1_context_writable_randomize(context_writable) != 0) {
result = 1;
}
}
secp256k1_ecdsa_recoverable_signature recoverable_signature = {0};
if (result == 0) {
uint8_t rfc6979_nonce_data[32] = {0};
void *rfc6979_nonce = NULL;
if (retry_count != 0) {
// If this is a retry attempt, then randomize rfc6979 with the counter.
rfc6979_nonce_data[0] = retry_count & 0xff;
rfc6979_nonce_data[1] = (retry_count >> 8) & 0xff;
rfc6979_nonce = rfc6979_nonce_data;
}
if (secp256k1_ecdsa_sign_recoverable(
context_writable, &recoverable_signature, digest,
private_key_bytes, secp256k1_nonce_function_rfc6979,
rfc6979_nonce) != 1) {
result = 1;
}
}
if (result == 0) {
const secp256k1_context *context_read_only = zkp_context_get_read_only();
if (secp256k1_ecdsa_recoverable_signature_serialize_compact(
context_read_only, signature_bytes, &recid,
&recoverable_signature) != 1) {
result = 1;
}
if (pby != NULL) {
*pby = (uint8_t)recid;
}
}
memzero(&recoverable_signature, sizeof(recoverable_signature));
retry_count += 1;
// If the signature is not acceptable then retry.
} while (result == 0 && is_canonical != NULL &&
!is_canonical(recid, signature_bytes));
if (context_writable) {
zkp_context_release_writable();
context_writable = NULL;
}
return result;
}
// ECDSA public key recovery
// public_key_bytes has 65 bytes
// signature_bytes has 64 bytes
// digest has 32 bytes
// recid is 0, 1, 2 or 3
// returns 0 on success
int zkp_ecdsa_recover_pub_from_sig(const ecdsa_curve *curve,
uint8_t *public_key_bytes,
const uint8_t *signature_bytes,
const uint8_t *digest, int recid) {
assert(curve == &secp256k1);
if (curve != &secp256k1) {
return 1;
}
int result = 0;
const secp256k1_context *context_read_only = zkp_context_get_read_only();
secp256k1_ecdsa_recoverable_signature recoverable_signature = {0};
if (result == 0) {
if (secp256k1_ecdsa_recoverable_signature_parse_compact(
context_read_only, &recoverable_signature, signature_bytes,
recid) != 1) {
result = 1;
}
}
secp256k1_pubkey public_key = {0};
if (result == 0) {
if (secp256k1_ecdsa_recover(context_read_only, &public_key,
&recoverable_signature, digest) != 1) {
result = 1;
}
}
memzero(&recoverable_signature, sizeof(recoverable_signature));
if (result == 0) {
size_t written = 65;
int returned = secp256k1_ec_pubkey_serialize(
context_read_only, public_key_bytes, &written, &public_key,
SECP256K1_EC_UNCOMPRESSED);
if (returned != 1 || written != 65) {
result = 1;
}
}
memzero(&public_key, sizeof(public_key));
return result;
}
// ECDSA verification
// curve has to be &secp256k1
// public_key_bytes has 33 or 65 bytes
// signature_bytes has 64 bytes
// digest has 32 bytes
// returns 0 if verification succeeded
int zkp_ecdsa_verify_digest(const ecdsa_curve *curve,
const uint8_t *public_key_bytes,
const uint8_t *signature_bytes,
const uint8_t *digest) {
assert(curve == &secp256k1);
if (curve != &secp256k1) {
return 1;
}
int result = 0;
int public_key_length = 0;
if (result == 0) {
if (public_key_bytes[0] == 0x04) {
public_key_length = 65;
} else if (public_key_bytes[0] == 0x02 || public_key_bytes[0] == 0x03) {
public_key_length = 33;
} else {
result = 1;
}
}
if (result == 0) {
if (is_zero_digest(digest)) {
// The digest was all-zero. The probability of this happening by chance is
// infinitesimal, but it could be induced by a fault injection. In this
// case the signature (r,s) can be forged by taking r := (t * Q).x mod n
// and s := r * t^-1 mod n for any t in [1, n-1]. We fail verification,
// because there is no guarantee that the signature was created by the
// owner of the private key.
result = 3;
}
}
const secp256k1_context *context_read_only = zkp_context_get_read_only();
secp256k1_pubkey public_key = {0};
if (result == 0) {
if (secp256k1_ec_pubkey_parse(context_read_only, &public_key,
public_key_bytes, public_key_length) != 1) {
result = 1;
}
}
secp256k1_ecdsa_signature signature = {0};
if (result == 0) {
if (secp256k1_ecdsa_signature_parse_compact(context_read_only, &signature,
signature_bytes) != 1) {
result = 2;
}
}
if (result == 0) {
secp256k1_ecdsa_signature_normalize(context_read_only, &signature,
&signature);
if (secp256k1_ecdsa_verify(context_read_only, &signature, digest,
&public_key) != 1) {
result = 5;
}
}
memzero(&public_key, sizeof(public_key));
memzero(&signature, sizeof(signature));
return result;
}
// ECDSA verification
// curve has to be &secp256k1
// public_key_bytes has 33 or 65 bytes
// signature_bytes has 64 bytes
// returns 0 if verification succeeded
int zkp_ecdsa_verify(const ecdsa_curve *curve, HasherType hasher_type,
const uint8_t *public_key_bytes,
const uint8_t *signature_bytes, const uint8_t *message,
uint32_t message_length) {
assert(curve == &secp256k1);
if (curve != &secp256k1) {
return 1;
}
uint8_t hash[32] = {0};
hasher_Raw(hasher_type, message, message_length, hash);
int result =
zkp_ecdsa_verify_digest(curve, public_key_bytes, signature_bytes, hash);
memzero(hash, sizeof(hash));
return result;
}