Debian Codegraph @

src/crypto/crypto.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <unistd.h>
32 #include <cassert>
33 #include <cstddef>
34 #include <cstdint>
35 #include <cstdlib>
36 #include <cstring>
37 #include <boost/thread/mutex.hpp>
38 #include <boost/thread/lock_guard.hpp>
39 #include <boost/shared_ptr.hpp>
40
41 #include "common/varint.h"
42 #include "warnings.h"
43 #include "crypto.h"
44 #include "hash.h"
45
46 #include "cryptonote_config.h"
47
48 namespace {
49 static void local_abort(const char *msg)
50 {
51 fprintf(stderr, "%s\n", msg);
52 #ifdef NDEBUG
53 _exit(1);
54 #else
55 abort();
56 #endif
57 }
58 }
59
60 namespace crypto {
61
62 using std::abort;
63 using std::int32_t;
64 using std::int64_t;
65 using std::size_t;
66 using std::uint32_t;
67 using std::uint64_t;
68
69 extern "C" {
70 #include "crypto-ops.h"
71 #include "random.h"
72 }
73
74 const crypto::public_key null_pkey = crypto::public_key{};
75 const crypto::secret_key null_skey = crypto::secret_key{};
76
77 static inline unsigned char *operator &(ec_point &point) {
78 return &reinterpret_cast<unsigned char &>(point);
79 }
80
81 static inline const unsigned char *operator &(const ec_point &point) {
82 return &reinterpret_cast<const unsigned char &>(point);
83 }
84
85 static inline unsigned char *operator &(ec_scalar &scalar) {
86 return &reinterpret_cast<unsigned char &>(scalar);
87 }
88
89 static inline const unsigned char *operator &(const ec_scalar &scalar) {
90 return &reinterpret_cast<const unsigned char &>(scalar);
91 }
92
93 boost::mutex &get_random_lock()
94 {
95 static boost::mutex random_lock;
96 return random_lock;
97 }
98
99 void generate_random_bytes_thread_safe(size_t N, uint8_t *bytes)
100 {
101 boost::lock_guard<boost::mutex> lock(get_random_lock());
102 generate_random_bytes_not_thread_safe(N, bytes);
103 }
104
105 void add_extra_entropy_thread_safe(const void *ptr, size_t bytes)
106 {
107 boost::lock_guard<boost::mutex> lock(get_random_lock());
108 add_extra_entropy_not_thread_safe(ptr, bytes);
109 }
110
111 static inline bool less32(const unsigned char *k0, const unsigned char *k1)
112 {
113 for (int n = 31; n >= 0; --n)
114 {
115 if (k0[n] < k1[n])
116 return true;
117 if (k0[n] > k1[n])
118 return false;
119 }
120 return false;
121 }
122
123 void random32_unbiased(unsigned char *bytes)
124 {
125 // l = 2^252 + 27742317777372353535851937790883648493.
126 // l fits 15 times in 32 bytes (iow, 15 l is the highest multiple of l that fits in 32 bytes)
127 static const unsigned char limit[32] = { 0xe3, 0x6a, 0x67, 0x72, 0x8b, 0xce, 0x13, 0x29, 0x8f, 0x30, 0x82, 0x8c, 0x0b, 0xa4, 0x10, 0x39, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0 };
128 while(1)
129 {
130 generate_random_bytes_thread_safe(32, bytes);
131 if (!less32(bytes, limit))
132 continue;
133 sc_reduce32(bytes);
134 if (sc_isnonzero(bytes))
135 break;
136 }
137 }
138 /* generate a random 32-byte (256-bit) integer and copy it to res */
139 static inline void random_scalar(ec_scalar &res) {
140 random32_unbiased((unsigned char*)res.data);
141 }
142
143 void hash_to_scalar(const void *data, size_t length, ec_scalar &res) {
144 cn_fast_hash(data, length, reinterpret_cast<hash &>(res));
145 sc_reduce32(&res);
146 }
147
148 /*
149 * generate public and secret keys from a random 256-bit integer
150 * TODO: allow specifying random value (for wallet recovery)
151 *
152 */
153 secret_key crypto_ops::generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key, bool recover) {
154 ge_p3 point;
155
156 secret_key rng;
157
158 if (recover)
159 {
160 rng = recovery_key;
161 }
162 else
163 {
164 random_scalar(rng);
165 }
166 sec = rng;
167 sc_reduce32(&unwrap(sec)); // reduce in case second round of keys (sendkeys)
168
169 ge_scalarmult_base(&point, &unwrap(sec));
170 ge_p3_tobytes(&pub, &point);
171
172 return rng;
173 }
174
175 bool crypto_ops::check_key(const public_key &key) {
176 ge_p3 point;
177 return ge_frombytes_vartime(&point, &key) == 0;
178 }
179
180 bool crypto_ops::secret_key_to_public_key(const secret_key &sec, public_key &pub) {
181 ge_p3 point;
182 if (sc_check(&unwrap(sec)) != 0) {
183 return false;
184 }
185 ge_scalarmult_base(&point, &unwrap(sec));
186 ge_p3_tobytes(&pub, &point);
187 return true;
188 }
189
190 bool crypto_ops::generate_key_derivation(const public_key &key1, const secret_key &key2, key_derivation &derivation) {
191 ge_p3 point;
192 ge_p2 point2;
193 ge_p1p1 point3;
194 assert(sc_check(&key2) == 0);
195 if (ge_frombytes_vartime(&point, &key1) != 0) {
196 return false;
197 }
198 ge_scalarmult(&point2, &unwrap(key2), &point);
199 ge_mul8(&point3, &point2);
200 ge_p1p1_to_p2(&point2, &point3);
201 ge_tobytes(&derivation, &point2);
202 return true;
203 }
204
205 void crypto_ops::derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res) {
206 struct {
207 key_derivation derivation;
208 char output_index[(sizeof(size_t) * 8 + 6) / 7];
209 } buf;
210 char *end = buf.output_index;
211 buf.derivation = derivation;
212 tools::write_varint(end, output_index);
213 assert(end <= buf.output_index + sizeof buf.output_index);
214 hash_to_scalar(&buf, end - reinterpret_cast<char *>(&buf), res);
215 }
216
217 bool crypto_ops::derive_public_key(const key_derivation &derivation, size_t output_index,
218 const public_key &base, public_key &derived_key) {
219 ec_scalar scalar;
220 ge_p3 point1;
221 ge_p3 point2;
222 ge_cached point3;
223 ge_p1p1 point4;
224 ge_p2 point5;
225 if (ge_frombytes_vartime(&point1, &base) != 0) {
226 return false;
227 }
228 derivation_to_scalar(derivation, output_index, scalar);
229 ge_scalarmult_base(&point2, &scalar);
230 ge_p3_to_cached(&point3, &point2);
231 ge_add(&point4, &point1, &point3);
232 ge_p1p1_to_p2(&point5, &point4);
233 ge_tobytes(&derived_key, &point5);
234 return true;
235 }
236
237 void crypto_ops::derive_secret_key(const key_derivation &derivation, size_t output_index,
238 const secret_key &base, secret_key &derived_key) {
239 ec_scalar scalar;
240 assert(sc_check(&base) == 0);
241 derivation_to_scalar(derivation, output_index, scalar);
242 sc_add(&unwrap(derived_key), &unwrap(base), &scalar);
243 }
244
245 bool crypto_ops::derive_subaddress_public_key(const public_key &out_key, const key_derivation &derivation, std::size_t output_index, public_key &derived_key) {
246 ec_scalar scalar;
247 ge_p3 point1;
248 ge_p3 point2;
249 ge_cached point3;
250 ge_p1p1 point4;
251 ge_p2 point5;
252 if (ge_frombytes_vartime(&point1, &out_key) != 0) {
253 return false;
254 }
255 derivation_to_scalar(derivation, output_index, scalar);
256 ge_scalarmult_base(&point2, &scalar);
257 ge_p3_to_cached(&point3, &point2);
258 ge_sub(&point4, &point1, &point3);
259 ge_p1p1_to_p2(&point5, &point4);
260 ge_tobytes(&derived_key, &point5);
261 return true;
262 }
263
264 struct s_comm {
265 hash h;
266 ec_point key;
267 ec_point comm;
268 };
269
270 // Used in v1 tx proofs
271 struct s_comm_2_v1 {
272 hash msg;
273 ec_point D;
274 ec_point X;
275 ec_point Y;
276 };
277
278 // Used in v1/v2 tx proofs
279 struct s_comm_2 {
280 hash msg;
281 ec_point D;
282 ec_point X;
283 ec_point Y;
284 hash sep; // domain separation
285 ec_point R;
286 ec_point A;
287 ec_point B;
288 };
289
290 void crypto_ops::generate_signature(const hash &prefix_hash, const public_key &pub, const secret_key &sec, signature &sig) {
291 ge_p3 tmp3;
292 ec_scalar k;
293 s_comm buf;
294 #if !defined(NDEBUG)
295 {
296 ge_p3 t;
297 public_key t2;
298 assert(sc_check(&sec) == 0);
299 ge_scalarmult_base(&t, &sec);
300 ge_p3_tobytes(&t2, &t);
301 assert(pub == t2);
302 }
303 #endif
304 buf.h = prefix_hash;
305 buf.key = pub;
306 try_again:
307 random_scalar(k);
308 ge_scalarmult_base(&tmp3, &k);
309 ge_p3_tobytes(&buf.comm, &tmp3);
310 hash_to_scalar(&buf, sizeof(s_comm), sig.c);
311 if (!sc_isnonzero((const unsigned char*)sig.c.data))
312 goto try_again;
313 sc_mulsub(&sig.r, &sig.c, &unwrap(sec), &k);
314 if (!sc_isnonzero((const unsigned char*)sig.r.data))
315 goto try_again;
316 memwipe(&k, sizeof(k));
317 }
318
319 bool crypto_ops::check_signature(const hash &prefix_hash, const public_key &pub, const signature &sig) {
320 ge_p2 tmp2;
321 ge_p3 tmp3;
322 ec_scalar c;
323 s_comm buf;
324 assert(check_key(pub));
325 buf.h = prefix_hash;
326 buf.key = pub;
327 if (ge_frombytes_vartime(&tmp3, &pub) != 0) {
328 return false;
329 }
330 if (sc_check(&sig.c) != 0 || sc_check(&sig.r) != 0 || !sc_isnonzero(&sig.c)) {
331 return false;
332 }
333 ge_double_scalarmult_base_vartime(&tmp2, &sig.c, &tmp3, &sig.r);
334 ge_tobytes(&buf.comm, &tmp2);
335 static const ec_point infinity = {{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
336 if (memcmp(&buf.comm, &infinity, 32) == 0)
337 return false;
338 hash_to_scalar(&buf, sizeof(s_comm), c);
339 sc_sub(&c, &c, &sig.c);
340 return sc_isnonzero(&c) == 0;
341 }
342
343 // Generate a proof of knowledge of `r` such that (`R = rG` and `D = rA`) or (`R = rB` and `D = rA`) via a Schnorr proof
344 // This handles use cases for both standard addresses and subaddresses
345 //
346 // NOTE: This generates old v1 proofs, and is for TESTING ONLY
347 void crypto_ops::generate_tx_proof_v1(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const secret_key &r, signature &sig) {
348 // sanity check
349 ge_p3 R_p3;
350 ge_p3 A_p3;
351 ge_p3 B_p3;
352 ge_p3 D_p3;
353 if (ge_frombytes_vartime(&R_p3, &R) != 0) throw std::runtime_error("tx pubkey is invalid");
354 if (ge_frombytes_vartime(&A_p3, &A) != 0) throw std::runtime_error("recipient view pubkey is invalid");
355 if (B && ge_frombytes_vartime(&B_p3, &*B) != 0) throw std::runtime_error("recipient spend pubkey is invalid");
356 if (ge_frombytes_vartime(&D_p3, &D) != 0) throw std::runtime_error("key derivation is invalid");
357 #if !defined(NDEBUG)
358 {
359 assert(sc_check(&r) == 0);
360 // check R == r*G or R == r*B
361 public_key dbg_R;
362 if (B)
363 {
364 ge_p2 dbg_R_p2;
365 ge_scalarmult(&dbg_R_p2, &r, &B_p3);
366 ge_tobytes(&dbg_R, &dbg_R_p2);
367 }
368 else
369 {
370 ge_p3 dbg_R_p3;
371 ge_scalarmult_base(&dbg_R_p3, &r);
372 ge_p3_tobytes(&dbg_R, &dbg_R_p3);
373 }
374 assert(R == dbg_R);
375 // check D == r*A
376 ge_p2 dbg_D_p2;
377 ge_scalarmult(&dbg_D_p2, &r, &A_p3);
378 public_key dbg_D;
379 ge_tobytes(&dbg_D, &dbg_D_p2);
380 assert(D == dbg_D);
381 }
382 #endif
383
384 // pick random k
385 ec_scalar k;
386 random_scalar(k);
387
388 s_comm_2_v1 buf;
389 buf.msg = prefix_hash;
390 buf.D = D;
391
392 if (B)
393 {
394 // compute X = k*B
395 ge_p2 X_p2;
396 ge_scalarmult(&X_p2, &k, &B_p3);
397 ge_tobytes(&buf.X, &X_p2);
398 }
399 else
400 {
401 // compute X = k*G
402 ge_p3 X_p3;
403 ge_scalarmult_base(&X_p3, &k);
404 ge_p3_tobytes(&buf.X, &X_p3);
405 }
406
407 // compute Y = k*A
408 ge_p2 Y_p2;
409 ge_scalarmult(&Y_p2, &k, &A_p3);
410 ge_tobytes(&buf.Y, &Y_p2);
411
412 // sig.c = Hs(Msg || D || X || Y)
413 hash_to_scalar(&buf, sizeof(buf), sig.c);
414
415 // sig.r = k - sig.c*r
416 sc_mulsub(&sig.r, &sig.c, &unwrap(r), &k);
417 }
418
419 // Generate a proof of knowledge of `r` such that (`R = rG` and `D = rA`) or (`R = rB` and `D = rA`) via a Schnorr proof
420 // This handles use cases for both standard addresses and subaddresses
421 //
422 // Generates only proofs for InProofV2 and OutProofV2
423 void crypto_ops::generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const secret_key &r, signature &sig) {
424 // sanity check
425 ge_p3 R_p3;
426 ge_p3 A_p3;
427 ge_p3 B_p3;
428 ge_p3 D_p3;
429 if (ge_frombytes_vartime(&R_p3, &R) != 0) throw std::runtime_error("tx pubkey is invalid");
430 if (ge_frombytes_vartime(&A_p3, &A) != 0) throw std::runtime_error("recipient view pubkey is invalid");
431 if (B && ge_frombytes_vartime(&B_p3, &*B) != 0) throw std::runtime_error("recipient spend pubkey is invalid");
432 if (ge_frombytes_vartime(&D_p3, &D) != 0) throw std::runtime_error("key derivation is invalid");
433 #if !defined(NDEBUG)
434 {
435 assert(sc_check(&r) == 0);
436 // check R == r*G or R == r*B
437 public_key dbg_R;
438 if (B)
439 {
440 ge_p2 dbg_R_p2;
441 ge_scalarmult(&dbg_R_p2, &r, &B_p3);
442 ge_tobytes(&dbg_R, &dbg_R_p2);
443 }
444 else
445 {
446 ge_p3 dbg_R_p3;
447 ge_scalarmult_base(&dbg_R_p3, &r);
448 ge_p3_tobytes(&dbg_R, &dbg_R_p3);
449 }
450 assert(R == dbg_R);
451 // check D == r*A
452 ge_p2 dbg_D_p2;
453 ge_scalarmult(&dbg_D_p2, &r, &A_p3);
454 public_key dbg_D;
455 ge_tobytes(&dbg_D, &dbg_D_p2);
456 assert(D == dbg_D);
457 }
458 #endif
459
460 // pick random k
461 ec_scalar k;
462 random_scalar(k);
463
464 // if B is not present
465 static const ec_point zero = {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }};
466
467 s_comm_2 buf;
468 buf.msg = prefix_hash;
469 buf.D = D;
470 buf.R = R;
471 buf.A = A;
472 if (B)
473 buf.B = *B;
474 else
475 buf.B = zero;
476 cn_fast_hash(config::HASH_KEY_TXPROOF_V2, sizeof(config::HASH_KEY_TXPROOF_V2)-1, buf.sep);
477
478 if (B)
479 {
480 // compute X = k*B
481 ge_p2 X_p2;
482 ge_scalarmult(&X_p2, &k, &B_p3);
483 ge_tobytes(&buf.X, &X_p2);
484 }
485 else
486 {
487 // compute X = k*G
488 ge_p3 X_p3;
489 ge_scalarmult_base(&X_p3, &k);
490 ge_p3_tobytes(&buf.X, &X_p3);
491 }
492
493 // compute Y = k*A
494 ge_p2 Y_p2;
495 ge_scalarmult(&Y_p2, &k, &A_p3);
496 ge_tobytes(&buf.Y, &Y_p2);
497
498 // sig.c = Hs(Msg || D || X || Y || sep || R || A || B)
499 hash_to_scalar(&buf, sizeof(buf), sig.c);
500
501 // sig.r = k - sig.c*r
502 sc_mulsub(&sig.r, &sig.c, &unwrap(r), &k);
503
504 memwipe(&k, sizeof(k));
505 }
506
507 // Verify a proof: either v1 (version == 1) or v2 (version == 2)
508 bool crypto_ops::check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const signature &sig, const int version) {
509 // sanity check
510 ge_p3 R_p3;
511 ge_p3 A_p3;
512 ge_p3 B_p3;
513 ge_p3 D_p3;
514 if (ge_frombytes_vartime(&R_p3, &R) != 0) return false;
515 if (ge_frombytes_vartime(&A_p3, &A) != 0) return false;
516 if (B && ge_frombytes_vartime(&B_p3, &*B) != 0) return false;
517 if (ge_frombytes_vartime(&D_p3, &D) != 0) return false;
518 if (sc_check(&sig.c) != 0 || sc_check(&sig.r) != 0) return false;
519
520 // compute sig.c*R
521 ge_p3 cR_p3;
522 {
523 ge_p2 cR_p2;
524 ge_scalarmult(&cR_p2, &sig.c, &R_p3);
525 public_key cR;
526 ge_tobytes(&cR, &cR_p2);
527 if (ge_frombytes_vartime(&cR_p3, &cR) != 0) return false;
528 }
529
530 ge_p1p1 X_p1p1;
531 if (B)
532 {
533 // compute X = sig.c*R + sig.r*B
534 ge_p2 rB_p2;
535 ge_scalarmult(&rB_p2, &sig.r, &B_p3);
536 public_key rB;
537 ge_tobytes(&rB, &rB_p2);
538 ge_p3 rB_p3;
539 if (ge_frombytes_vartime(&rB_p3, &rB) != 0) return false;
540 ge_cached rB_cached;
541 ge_p3_to_cached(&rB_cached, &rB_p3);
542 ge_add(&X_p1p1, &cR_p3, &rB_cached);
543 }
544 else
545 {
546 // compute X = sig.c*R + sig.r*G
547 ge_p3 rG_p3;
548 ge_scalarmult_base(&rG_p3, &sig.r);
549 ge_cached rG_cached;
550 ge_p3_to_cached(&rG_cached, &rG_p3);
551 ge_add(&X_p1p1, &cR_p3, &rG_cached);
552 }
553 ge_p2 X_p2;
554 ge_p1p1_to_p2(&X_p2, &X_p1p1);
555
556 // compute sig.c*D
557 ge_p2 cD_p2;
558 ge_scalarmult(&cD_p2, &sig.c, &D_p3);
559
560 // compute sig.r*A
561 ge_p2 rA_p2;
562 ge_scalarmult(&rA_p2, &sig.r, &A_p3);
563
564 // compute Y = sig.c*D + sig.r*A
565 public_key cD;
566 public_key rA;
567 ge_tobytes(&cD, &cD_p2);
568 ge_tobytes(&rA, &rA_p2);
569 ge_p3 cD_p3;
570 ge_p3 rA_p3;
571 if (ge_frombytes_vartime(&cD_p3, &cD) != 0) return false;
572 if (ge_frombytes_vartime(&rA_p3, &rA) != 0) return false;
573 ge_cached rA_cached;
574 ge_p3_to_cached(&rA_cached, &rA_p3);
575 ge_p1p1 Y_p1p1;
576 ge_add(&Y_p1p1, &cD_p3, &rA_cached);
577 ge_p2 Y_p2;
578 ge_p1p1_to_p2(&Y_p2, &Y_p1p1);
579
580 // Compute hash challenge
581 // for v1, c2 = Hs(Msg || D || X || Y)
582 // for v2, c2 = Hs(Msg || D || X || Y || sep || R || A || B)
583
584 // if B is not present
585 static const ec_point zero = {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }};
586
587 s_comm_2 buf;
588 buf.msg = prefix_hash;
589 buf.D = D;
590 buf.R = R;
591 buf.A = A;
592 if (B)
593 buf.B = *B;
594 else
595 buf.B = zero;
596 cn_fast_hash(config::HASH_KEY_TXPROOF_V2, sizeof(config::HASH_KEY_TXPROOF_V2)-1, buf.sep);
597 ge_tobytes(&buf.X, &X_p2);
598 ge_tobytes(&buf.Y, &Y_p2);
599 ec_scalar c2;
600
601 // Hash depends on version
602 if (version == 1) hash_to_scalar(&buf, sizeof(s_comm_2) - 3*sizeof(ec_point) - sizeof(hash), c2);
603 else if (version == 2) hash_to_scalar(&buf, sizeof(s_comm_2), c2);
604 else return false;
605
606 // test if c2 == sig.c
607 sc_sub(&c2, &c2, &sig.c);
608 return sc_isnonzero(&c2) == 0;
609 }
610
611 static void hash_to_ec(const public_key &key, ge_p3 &res) {
612 hash h;
613 ge_p2 point;
614 ge_p1p1 point2;
615 cn_fast_hash(std::addressof(key), sizeof(public_key), h);
616 ge_fromfe_frombytes_vartime(&point, reinterpret_cast<const unsigned char *>(&h));
617 ge_mul8(&point2, &point);
618 ge_p1p1_to_p3(&res, &point2);
619 }
620
621 void crypto_ops::generate_key_image(const public_key &pub, const secret_key &sec, key_image &image) {
622 ge_p3 point;
623 ge_p2 point2;
624 assert(sc_check(&sec) == 0);
625 hash_to_ec(pub, point);
626 ge_scalarmult(&point2, &unwrap(sec), &point);
627 ge_tobytes(&image, &point2);
628 }
629
630 PUSH_WARNINGS
631 DISABLE_VS_WARNINGS(4200)
632 struct ec_point_pair {
633 ec_point a, b;
634 };
635 struct rs_comm {
636 hash h;
637 struct ec_point_pair ab[];
638 };
639 POP_WARNINGS
640
641 static inline size_t rs_comm_size(size_t pubs_count) {
642 return sizeof(rs_comm) + pubs_count * sizeof(ec_point_pair);
643 }
644
645 void crypto_ops::generate_ring_signature(const hash &prefix_hash, const key_image &image,
646 const public_key *const *pubs, size_t pubs_count,
647 const secret_key &sec, size_t sec_index,
648 signature *sig) {
649 size_t i;
650 ge_p3 image_unp;
651 ge_dsmp image_pre;
652 ec_scalar sum, k, h;
653 boost::shared_ptr<rs_comm> buf(reinterpret_cast<rs_comm *>(malloc(rs_comm_size(pubs_count))), free);
654 if (!buf)
655 local_abort("malloc failure");
656 assert(sec_index < pubs_count);
657 #if !defined(NDEBUG)
658 {
659 ge_p3 t;
660 public_key t2;
661 key_image t3;
662 assert(sc_check(&sec) == 0);
663 ge_scalarmult_base(&t, &sec);
664 ge_p3_tobytes(&t2, &t);
665 assert(*pubs[sec_index] == t2);
666 generate_key_image(*pubs[sec_index], sec, t3);
667 assert(image == t3);
668 for (i = 0; i < pubs_count; i++) {
669 assert(check_key(*pubs[i]));
670 }
671 }
672 #endif
673 if (ge_frombytes_vartime(&image_unp, &image) != 0) {
674 local_abort("invalid key image");
675 }
676 ge_dsm_precomp(image_pre, &image_unp);
677 sc_0(&sum);
678 buf->h = prefix_hash;
679 for (i = 0; i < pubs_count; i++) {
680 ge_p2 tmp2;
681 ge_p3 tmp3;
682 if (i == sec_index) {
683 random_scalar(k);
684 ge_scalarmult_base(&tmp3, &k);
685 ge_p3_tobytes(&buf->ab[i].a, &tmp3);
686 hash_to_ec(*pubs[i], tmp3);
687 ge_scalarmult(&tmp2, &k, &tmp3);
688 ge_tobytes(&buf->ab[i].b, &tmp2);
689 } else {
690 random_scalar(sig[i].c);
691 random_scalar(sig[i].r);
692 if (ge_frombytes_vartime(&tmp3, &*pubs[i]) != 0) {
693 memwipe(&k, sizeof(k));
694 local_abort("invalid pubkey");
695 }
696 ge_double_scalarmult_base_vartime(&tmp2, &sig[i].c, &tmp3, &sig[i].r);
697 ge_tobytes(&buf->ab[i].a, &tmp2);
698 hash_to_ec(*pubs[i], tmp3);
699 ge_double_scalarmult_precomp_vartime(&tmp2, &sig[i].r, &tmp3, &sig[i].c, image_pre);
700 ge_tobytes(&buf->ab[i].b, &tmp2);
701 sc_add(&sum, &sum, &sig[i].c);
702 }
703 }
704 hash_to_scalar(buf.get(), rs_comm_size(pubs_count), h);
705 sc_sub(&sig[sec_index].c, &h, &sum);
706 sc_mulsub(&sig[sec_index].r, &sig[sec_index].c, &unwrap(sec), &k);
707
708 memwipe(&k, sizeof(k));
709 }
710
711 bool crypto_ops::check_ring_signature(const hash &prefix_hash, const key_image &image,
712 const public_key *const *pubs, size_t pubs_count,
713 const signature *sig) {
714 size_t i;
715 ge_p3 image_unp;
716 ge_dsmp image_pre;
717 ec_scalar sum, h;
718 boost::shared_ptr<rs_comm> buf(reinterpret_cast<rs_comm *>(malloc(rs_comm_size(pubs_count))), free);
719 if (!buf)
720 return false;
721 #if !defined(NDEBUG)
722 for (i = 0; i < pubs_count; i++) {
723 assert(check_key(*pubs[i]));
724 }
725 #endif
726 if (ge_frombytes_vartime(&image_unp, &image) != 0) {
727 return false;
728 }
729 ge_dsm_precomp(image_pre, &image_unp);
730 sc_0(&sum);
731 buf->h = prefix_hash;
732 for (i = 0; i < pubs_count; i++) {
733 ge_p2 tmp2;
734 ge_p3 tmp3;
735 if (sc_check(&sig[i].c) != 0 || sc_check(&sig[i].r) != 0) {
736 return false;
737 }
738 if (ge_frombytes_vartime(&tmp3, &*pubs[i]) != 0) {
739 return false;
740 }
741 ge_double_scalarmult_base_vartime(&tmp2, &sig[i].c, &tmp3, &sig[i].r);
742 ge_tobytes(&buf->ab[i].a, &tmp2);
743 hash_to_ec(*pubs[i], tmp3);
744 ge_double_scalarmult_precomp_vartime(&tmp2, &sig[i].r, &tmp3, &sig[i].c, image_pre);
745 ge_tobytes(&buf->ab[i].b, &tmp2);
746 sc_add(&sum, &sum, &sig[i].c);
747 }
748 hash_to_scalar(buf.get(), rs_comm_size(pubs_count), h);
749 sc_sub(&h, &h, &sum);
750 return sc_isnonzero(&h) == 0;
751 }
752
753 void crypto_ops::derive_view_tag(const key_derivation &derivation, size_t output_index, view_tag &view_tag) {
754 #pragma pack(push, 1)
755 struct {
756 char salt[8]; // view tag domain-separator
757 key_derivation derivation;
758 char output_index[(sizeof(size_t) * 8 + 6) / 7];
759 } buf;
760 #pragma pack(pop)
761
762 char *end = buf.output_index;
763 memcpy(buf.salt, "view_tag", 8); // leave off null terminator
764 buf.derivation = derivation;
765 tools::write_varint(end, output_index);
766 assert(end <= buf.output_index + sizeof buf.output_index);
767
768 // view_tag_full = H[salt|derivation|output_index]
769 hash view_tag_full;
770 cn_fast_hash(&buf, end - reinterpret_cast<char *>(&buf), view_tag_full);
771
772 // only need a slice of view_tag_full to realize optimal perf/space efficiency
773 static_assert(sizeof(crypto::view_tag) <= sizeof(view_tag_full), "view tag should not be larger than hash result");
774 memcpy(&view_tag, &view_tag_full, sizeof(crypto::view_tag));
775 }
776 }