Apply %restore_fcommon macro for Address Sanitizer
[platform/upstream/nettle.git] / rsa-sign-tr.c
1 /* rsa-sign-tr.c
2
3    Creating RSA signatures, with some additional checks.
4
5    Copyright (C) 2001, 2015 Niels Möller
6    Copyright (C) 2012 Nikos Mavrogiannopoulos
7
8    This file is part of GNU Nettle.
9
10    GNU Nettle is free software: you can redistribute it and/or
11    modify it under the terms of either:
12
13      * the GNU Lesser General Public License as published by the Free
14        Software Foundation; either version 3 of the License, or (at your
15        option) any later version.
16
17    or
18
19      * the GNU General Public License as published by the Free
20        Software Foundation; either version 2 of the License, or (at your
21        option) any later version.
22
23    or both in parallel, as here.
24
25    GNU Nettle is distributed in the hope that it will be useful,
26    but WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28    General Public License for more details.
29
30    You should have received copies of the GNU General Public License and
31    the GNU Lesser General Public License along with this program.  If
32    not, see http://www.gnu.org/licenses/.
33 */
34
35 #if HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include "rsa.h"
40
41 /* Blinds m, by computing c = m r^e (mod n), for a random r. Also
42    returns the inverse (ri), for use by rsa_unblind. */
43 static void
44 rsa_blind (const struct rsa_public_key *pub,
45            void *random_ctx, nettle_random_func *random,
46            mpz_t c, mpz_t ri, const mpz_t m)
47 {
48   mpz_t r;
49
50   mpz_init(r);
51
52   /* c = m*(r^e)
53    * ri = r^(-1)
54    */
55   do
56     {
57       nettle_mpz_random(r, random_ctx, random, pub->n);
58       /* invert r */
59     }
60   while (!mpz_invert (ri, r, pub->n));
61
62   /* c = c*(r^e) mod n */
63   mpz_powm(r, r, pub->e, pub->n);
64   mpz_mul(c, m, r);
65   mpz_fdiv_r(c, c, pub->n);
66
67   mpz_clear(r);
68 }
69
70 /* m = c ri mod n */
71 static void
72 rsa_unblind (const struct rsa_public_key *pub,
73              mpz_t m, const mpz_t ri, const mpz_t c)
74 {
75   mpz_mul(m, c, ri);
76   mpz_fdiv_r(m, m, pub->n);
77 }
78
79 /* Checks for any errors done in the RSA computation. That avoids
80  * attacks which rely on faults on hardware, or even software MPI
81  * implementation. */
82 int
83 rsa_compute_root_tr(const struct rsa_public_key *pub,
84                     const struct rsa_private_key *key,
85                     void *random_ctx, nettle_random_func *random,
86                     mpz_t x, const mpz_t m)
87 {
88   int res;
89   mpz_t t, mb, xb, ri;
90
91   mpz_init (mb);
92   mpz_init (xb);
93   mpz_init (ri);
94   mpz_init (t);
95
96   rsa_blind (pub, random_ctx, random, mb, ri, m);
97
98   rsa_compute_root (key, xb, mb);
99
100   mpz_powm(t, xb, pub->e, pub->n);
101   res = (mpz_cmp(mb, t) == 0);
102
103   if (res)
104     rsa_unblind (pub, x, ri, xb);
105
106   mpz_clear (mb);
107   mpz_clear (xb);
108   mpz_clear (ri);
109   mpz_clear (t);
110
111   return res;
112 }