/src/gnutls/lib/nettle/gost/gostdsa-mask.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* gostdsa-verify.c |
2 | | |
3 | | Copyright (C) 2018 Dmitry Eremin-Solenikov |
4 | | |
5 | | This file is part of GNU Nettle. |
6 | | |
7 | | GNU Nettle is free software: you can redistribute it and/or |
8 | | modify it under the terms of either: |
9 | | |
10 | | * the GNU Lesser General Public License as published by the Free |
11 | | Software Foundation; either version 3 of the License, or (at your |
12 | | option) any later version. |
13 | | |
14 | | or |
15 | | |
16 | | * the GNU General Public License as published by the Free |
17 | | Software Foundation; either version 2 of the License, or (at your |
18 | | option) any later version. |
19 | | |
20 | | or both in parallel, as here. |
21 | | |
22 | | GNU Nettle is distributed in the hope that it will be useful, |
23 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
25 | | General Public License for more details. |
26 | | |
27 | | You should have received copies of the GNU General Public License and |
28 | | the GNU Lesser General Public License along with this program. If |
29 | | not, see https://www.gnu.org/licenses/. |
30 | | */ |
31 | | |
32 | | #if HAVE_CONFIG_H |
33 | | # include "config.h" |
34 | | #endif |
35 | | |
36 | | #include <gnutls_int.h> |
37 | | |
38 | | #include <stdlib.h> |
39 | | |
40 | | #include <nettle/ecc-curve.h> |
41 | | #include "gostdsa2.h" |
42 | | |
43 | 0 | #define GOST_GC256B_Q "ffffffffffffffffffffffffffffffff" \ |
44 | 0 | "6c611070995ad10045841b09b761b893" |
45 | 0 | #define GOST_GC512A_Q "ffffffffffffffffffffffffffffffff" \ |
46 | 0 | "ffffffffffffffffffffffffffffffff" \ |
47 | 0 | "27e69532f48d89116ff22b8d4e056060" \ |
48 | 0 | "9b4b38abfad2b85dcacdb1411f10b275" |
49 | | |
50 | | /* Key comes in form .... M_2 M_1 K_0, |
51 | | unmask is K_i = K_i-1 * M_i mod Q */ |
52 | | int gostdsa_unmask_key(const struct ecc_curve *ecc, mpz_t key) |
53 | 0 | { |
54 | 0 | unsigned bits = ecc_bit_size(ecc); |
55 | 0 | unsigned keybits = mpz_sizeinbase(key, 2); |
56 | 0 | mpz_t unmasked, temp, temp2, q; |
57 | |
|
58 | 0 | if (keybits <= bits) |
59 | 0 | return 0; |
60 | | |
61 | 0 | mpz_init(unmasked); |
62 | 0 | mpz_init(temp); |
63 | 0 | mpz_init(temp2); |
64 | |
|
65 | 0 | if (ecc == nettle_get_gost_gc256b()) |
66 | 0 | mpz_init_set_str(q, GOST_GC256B_Q, 16); |
67 | 0 | else if (ecc == nettle_get_gost_gc512a()) |
68 | 0 | mpz_init_set_str(q, GOST_GC512A_Q, 16); |
69 | 0 | else |
70 | 0 | abort(); |
71 | | |
72 | 0 | mpz_tdiv_r_2exp(unmasked, key, bits); |
73 | 0 | mpz_tdiv_q_2exp(key, key, bits); |
74 | 0 | keybits -= bits; |
75 | 0 | while (keybits > bits) { |
76 | 0 | mpz_tdiv_r_2exp(temp2, key, bits); |
77 | 0 | mpz_tdiv_q_2exp(key, key, bits); |
78 | 0 | keybits -= bits; |
79 | 0 | mpz_mul(temp, unmasked, temp2); |
80 | 0 | mpz_mod(unmasked, temp, q); |
81 | 0 | } |
82 | 0 | mpz_mul(temp, unmasked, key); |
83 | 0 | mpz_mod(key, temp, q); |
84 | |
|
85 | 0 | mpz_clear(q); |
86 | 0 | mpz_clear(temp2); |
87 | 0 | mpz_clear(temp); |
88 | 0 | mpz_clear(unmasked); |
89 | |
|
90 | 0 | return 0; |
91 | 0 | } |