Further separation of CMAC per-message state from subkeys.
[gd/nettle] / rsa-sec-decrypt.c
1 /* rsa-sec-decrypt.c
2
3    RSA decryption, using randomized RSA blinding to be more resistant
4    to side-channel attacks like timing attacks or cache based memory
5    access measurements.
6
7    Copyright (C) 2001, 2012 Niels Möller, Nikos Mavrogiannopoulos
8    Copyright (C) 2018 Red Hat, Inc.
9
10    This file is part of GNU Nettle.
11
12    GNU Nettle is free software: you can redistribute it and/or
13    modify it under the terms of either:
14
15      * the GNU Lesser General Public License as published by the Free
16        Software Foundation; either version 3 of the License, or (at your
17        option) any later version.
18
19    or
20
21      * the GNU General Public License as published by the Free
22        Software Foundation; either version 2 of the License, or (at your
23        option) any later version.
24
25    or both in parallel, as here.
26
27    GNU Nettle is distributed in the hope that it will be useful,
28    but WITHOUT ANY WARRANTY; without even the implied warranty of
29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30    General Public License for more details.
31
32    You should have received copies of the GNU General Public License and
33    the GNU Lesser General Public License along with this program.  If
34    not, see http://www.gnu.org/licenses/.
35 */
36
37 #if HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40
41 #include "rsa.h"
42 #include "pkcs1-internal.h"
43 #include "rsa-internal.h"
44
45 #include "gmp-glue.h"
46
47 int
48 rsa_sec_decrypt(const struct rsa_public_key *pub,
49                 const struct rsa_private_key *key,
50                 void *random_ctx, nettle_random_func *random,
51                 size_t length, uint8_t *message,
52                 const mpz_t gibberish)
53 {
54   TMP_GMP_DECL (m, mp_limb_t);
55   TMP_GMP_DECL (em, uint8_t);
56   int res;
57
58   TMP_GMP_ALLOC (m, mpz_size(pub->n));
59   TMP_GMP_ALLOC (em, key->size);
60
61   res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m,
62                                   mpz_limbs_read(gibberish),
63                                   mpz_size(gibberish));
64
65   mpn_get_base256 (em, key->size, m, mpz_size(pub->n));
66
67   res &= _pkcs1_sec_decrypt (length, message, key->size, em);
68
69   TMP_GMP_FREE (em);
70   TMP_GMP_FREE (m);
71   return res;
72 }
73