nettle  3.7.3
About: Nettle is a low-level cryptographic library.
  Fossies Dox: nettle-3.7.3.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

rsa-decrypt-tr.c
Go to the documentation of this file.
1 /* rsa-decrypt-tr.c
2 
3  RSA decryption, using randomized RSA blinding to be more resistant
4  to timing attacks.
5 
6  Copyright (C) 2001, 2012 Niels Möller, 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-internal.h"
40 #include "pkcs1-internal.h"
41 #include "gmp-glue.h"
42 
43 int
44 rsa_decrypt_tr(const struct rsa_public_key *pub,
45  const struct rsa_private_key *key,
46  void *random_ctx, nettle_random_func *random,
47  size_t *length, uint8_t *message,
48  const mpz_t gibberish)
49 {
51  TMP_GMP_DECL (em, uint8_t);
52  mp_size_t key_limb_size;
53  int res;
54 
55  /* First check that input is in range. */
56  if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, pub->n) >= 0)
57  return 0;
58 
59  key_limb_size = mpz_size(pub->n);
60 
61  TMP_GMP_ALLOC (m, key_limb_size);
62  TMP_GMP_ALLOC (em, key->size);
63  mpz_limbs_copy(m, gibberish, key_limb_size);
64 
65  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m, m);
66 
67  mpn_get_base256 (em, key->size, m, key_limb_size);
68 
69  res &= _pkcs1_sec_decrypt_variable (length, message, key->size, em);
70 
71  TMP_GMP_FREE (em);
72  TMP_GMP_FREE (m);
73  return res;
74 }
#define TMP_GMP_DECL(name, type)
Definition: gmp-glue.h:51
#define TMP_GMP_FREE(name)
Definition: gmp-glue.h:57
#define mpn_get_base256
Definition: gmp-glue.h:44
#define TMP_GMP_ALLOC(name, size)
Definition: gmp-glue.h:53
#define mpz_limbs_copy
Definition: gmp-glue.h:40
int mpz_sgn(const mpz_t u)
Definition: mini-gmp.c:1847
size_t mpz_size(const mpz_t u)
Definition: mini-gmp.c:1622
int mpz_cmp(const mpz_t a, const mpz_t b)
Definition: mini-gmp.c:1877
__mpz_struct mpz_t[1]
Definition: mini-gmp.h:77
long mp_size_t
Definition: mini-gmp.h:61
unsigned long mp_limb_t
Definition: mini-gmp.h:60
void nettle_random_func(void *ctx, size_t length, uint8_t *dst)
Definition: nettle-types.h:75
#define _pkcs1_sec_decrypt_variable
#define _rsa_sec_compute_root_tr
Definition: rsa-internal.h:46
#define rsa_decrypt_tr
Definition: rsa.h:90
size_t size
Definition: rsa.h:129
mpz_t n
Definition: rsa.h:121