tizen 2.4 release
[external/nettle.git] / rsa-decrypt-tr.c
1 /* rsa-decrypt-tr.c
2  *
3  * RSA decryption, using randomized RSA blinding to be more resistant
4  * to timing attacks.
5  */
6
7 /* nettle, low-level cryptographics library
8  *
9  * Copyright (C) 2001, 2012 Niels Möller, Nikos Mavrogiannopoulos
10  *  
11  * The nettle library is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or (at your
14  * option) any later version.
15  * 
16  * The nettle library is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19  * License for more details.
20  * 
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with the nettle library; see the file COPYING.LIB.  If not, write to
23  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24  * MA 02111-1301, USA.
25  */
26
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "rsa.h"
32
33 #include "bignum.h"
34 #include "pkcs1.h"
35
36 int
37 rsa_decrypt_tr(const struct rsa_public_key *pub,
38                const struct rsa_private_key *key,
39                void *random_ctx, nettle_random_func *random,
40                unsigned *length, uint8_t *message,
41                const mpz_t gibberish)
42 {
43   mpz_t m, ri;
44   int res;
45
46   mpz_init_set(m, gibberish);
47   mpz_init (ri);
48
49   _rsa_blind (pub, random_ctx, random, m, ri);
50   rsa_compute_root(key, m, m);
51   _rsa_unblind (pub, m, ri);
52   mpz_clear (ri);
53
54   res = pkcs1_decrypt (key->size, m, length, message);
55   mpz_clear(m);
56   return res;
57 }