3 Creating RSA signatures, with some additional checks.
5 Copyright (C) 2001, 2015 Niels Möller
6 Copyright (C) 2012 Nikos Mavrogiannopoulos
8 This file is part of GNU Nettle.
10 GNU Nettle is free software: you can redistribute it and/or
11 modify it under the terms of either:
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.
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.
23 or both in parallel, as here.
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.
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/.
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. */
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)
57 nettle_mpz_random(r, random_ctx, random, pub->n);
60 while (!mpz_invert (ri, r, pub->n));
62 /* c = c*(r^e) mod n */
63 mpz_powm(r, r, pub->e, pub->n);
65 mpz_fdiv_r(c, c, pub->n);
72 rsa_unblind (const struct rsa_public_key *pub,
73 mpz_t m, const mpz_t ri, const mpz_t c)
76 mpz_fdiv_r(m, m, pub->n);
79 /* Checks for any errors done in the RSA computation. That avoids
80 * attacks which rely on faults on hardware, or even software MPI
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)
96 rsa_blind (pub, random_ctx, random, mb, ri, m);
98 rsa_compute_root (key, xb, mb);
100 mpz_powm(t, xb, pub->e, pub->n);
101 res = (mpz_cmp(mb, t) == 0);
104 rsa_unblind (pub, x, ri, xb);