View | Details | Raw Unified | Return to bug 1118086
Collapse All | Expand All

(-)nettle-3.4/aes-set-encrypt-key.c (+1 lines)
Lines 37-42 Link Here
37
#endif
37
#endif
38
38
39
#include <assert.h>
39
#include <assert.h>
40
#include <stdlib.h>
40
41
41
#include "aes-internal.h"
42
#include "aes-internal.h"
42
43
(-)nettle-3.4/cnd-memcpy.c (+55 lines)
Line 0 Link Here
1
/* cnd-memcpy.c
2
3
   Copyright (C) 2018 Niels Möller
4
5
   This file is part of GNU Nettle.
6
7
   GNU Nettle is free software: you can redistribute it and/or
8
   modify it under the terms of either:
9
10
     * the GNU Lesser General Public License as published by the Free
11
       Software Foundation; either version 3 of the License, or (at your
12
       option) any later version.
13
14
   or
15
16
     * the GNU General Public License as published by the Free
17
       Software Foundation; either version 2 of the License, or (at your
18
       option) any later version.
19
20
   or both in parallel, as here.
21
22
   GNU Nettle is distributed in the hope that it will be useful,
23
   but WITHOUT ANY WARRANTY; without even the implied warranty of
24
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
   General Public License for more details.
26
27
   You should have received copies of the GNU General Public License and
28
   the GNU Lesser General Public License along with this program.  If
29
   not, see http://www.gnu.org/licenses/.
30
*/
31
32
#if HAVE_CONFIG_H
33
# include "config.h"
34
#endif
35
36
#include "memops.h"
37
38
void
39
cnd_memcpy(int cnd, volatile void *dst, const volatile void *src, size_t n)
40
{
41
  const volatile unsigned char *sp = src;
42
  volatile unsigned char *dp = dst;
43
  volatile unsigned char c;
44
  volatile unsigned char m;
45
  size_t i;
46
47
  m = -(unsigned char) cnd;
48
49
  for (i = 0; i < n; i++)
50
    {
51
      c = (sp[i] & m);
52
      c |= (dp[i] & ~m);
53
      dp[i] = c;
54
    }
55
}
(-)nettle-3.4/configure.ac (-2 / +2 lines)
Lines 236-244 Link Here
236
# Checks for libraries
236
# Checks for libraries
237
if test "x$enable_public_key" = "xyes" ; then
237
if test "x$enable_public_key" = "xyes" ; then
238
  if test "x$enable_mini_gmp" = "xno" ; then
238
  if test "x$enable_mini_gmp" = "xno" ; then
239
    AC_CHECK_LIB(gmp, __gmpz_powm_sec,,
239
    AC_CHECK_LIB(gmp, __gmpn_sec_div_r,,
240
        [AC_MSG_WARN(
240
        [AC_MSG_WARN(
241
    [GNU MP not found, or too old. GMP-5.0 or later is needed, see http://gmplib.org/.
241
    [GNU MP not found, or too old. GMP-6.0 or later is needed, see https://gmplib.org/.
242
    Support for public key algorithms will be unavailable.])]
242
    Support for public key algorithms will be unavailable.])]
243
        enable_public_key=no)
243
        enable_public_key=no)
244
244
(-)nettle-3.4/des-compat.c (-2 / +3 lines)
Lines 35-40 Link Here
35
# include "config.h"
35
# include "config.h"
36
#endif
36
#endif
37
37
38
#include <stdlib.h>
38
#include <string.h>
39
#include <string.h>
39
#include <assert.h>
40
#include <assert.h>
40
41
Lines 48-54 Link Here
48
49
49
static void
50
static void
50
des_compat_des3_encrypt(struct des_compat_des3 *ctx,
51
des_compat_des3_encrypt(struct des_compat_des3 *ctx,
51
			uint32_t length, uint8_t *dst, const uint8_t *src)
52
			size_t length, uint8_t *dst, const uint8_t *src)
52
{
53
{
53
  nettle_des_encrypt(ctx->keys[0], length, dst, src);
54
  nettle_des_encrypt(ctx->keys[0], length, dst, src);
54
  nettle_des_decrypt(ctx->keys[1], length, dst, dst);
55
  nettle_des_decrypt(ctx->keys[1], length, dst, dst);
Lines 57-63 Link Here
57
58
58
static void
59
static void
59
des_compat_des3_decrypt(struct des_compat_des3 *ctx,
60
des_compat_des3_decrypt(struct des_compat_des3 *ctx,
60
			uint32_t length, uint8_t *dst, const uint8_t *src)
61
			size_t length, uint8_t *dst, const uint8_t *src)
61
{
62
{
62
  nettle_des_decrypt(ctx->keys[2], length, dst, src);
63
  nettle_des_decrypt(ctx->keys[2], length, dst, src);
63
  nettle_des_encrypt(ctx->keys[1], length, dst, dst);
64
  nettle_des_encrypt(ctx->keys[1], length, dst, dst);
(-)nettle-3.4/gmp-glue.c (+31 lines)
Lines 247-252 Link Here
247
}
247
}
248
248
249
void
249
void
250
mpn_get_base256 (uint8_t *rp, size_t rn,
251
		 const mp_limb_t *xp, mp_size_t xn)
252
{
253
  unsigned bits;
254
  mp_limb_t in;
255
  for (bits = in = 0; xn > 0 && rn > 0; )
256
    {
257
      if (bits >= 8)
258
	{
259
	  rp[--rn] = in;
260
	  in >>= 8;
261
	  bits -= 8;
262
	}
263
      else
264
	{
265
	  uint8_t old = in;
266
	  in = *xp++;
267
	  xn--;
268
	  rp[--rn] = old | (in << bits);
269
	  in >>= (8 - bits);
270
	  bits += GMP_NUMB_BITS - 8;
271
	}
272
    }
273
  while (rn > 0)
274
    {
275
      rp[--rn] = in;
276
      in >>= 8;
277
    }
278
}
279
280
void
250
mpn_get_base256_le (uint8_t *rp, size_t rn,
281
mpn_get_base256_le (uint8_t *rp, size_t rn,
251
		    const mp_limb_t *xp, mp_size_t xn)
282
		    const mp_limb_t *xp, mp_size_t xn)
252
{
283
{
(-)nettle-3.4/gmp-glue.h (+8 lines)
Lines 57-62 Link Here
57
#define mpz_set_n _nettle_mpz_set_n
57
#define mpz_set_n _nettle_mpz_set_n
58
#define mpn_set_base256 _nettle_mpn_set_base256
58
#define mpn_set_base256 _nettle_mpn_set_base256
59
#define mpn_set_base256_le _nettle_mpn_set_base256_le
59
#define mpn_set_base256_le _nettle_mpn_set_base256_le
60
#define mpn_get_base256 _nettle_mpn_get_base256
60
#define mpn_get_base256_le _nettle_mpn_get_base256_le
61
#define mpn_get_base256_le _nettle_mpn_get_base256_le
61
#define gmp_alloc_limbs _nettle_gmp_alloc_limbs
62
#define gmp_alloc_limbs _nettle_gmp_alloc_limbs
62
#define gmp_free_limbs _nettle_gmp_free_limbs
63
#define gmp_free_limbs _nettle_gmp_free_limbs
Lines 81-86 Link Here
81
# define cnd_sub_n(cnd, rp, ap, n) mpn_submul_1 ((rp), (ap), (n), (cnd) != 0)
82
# define cnd_sub_n(cnd, rp, ap, n) mpn_submul_1 ((rp), (ap), (n), (cnd) != 0)
82
#endif
83
#endif
83
84
85
#define NETTLE_OCTET_SIZE_TO_LIMB_SIZE(n) \
86
  (((n) * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS)
87
84
/* Some functions for interfacing between mpz and mpn code. Signs of
88
/* Some functions for interfacing between mpz and mpn code. Signs of
85
   the mpz numbers are generally ignored. */
89
   the mpz numbers are generally ignored. */
86
90
Lines 148-153 Link Here
148
		    const uint8_t *xp, size_t xn);
152
		    const uint8_t *xp, size_t xn);
149
153
150
void
154
void
155
mpn_get_base256 (uint8_t *rp, size_t rn,
156
	         const mp_limb_t *xp, mp_size_t xn);
157
158
void
151
mpn_get_base256_le (uint8_t *rp, size_t rn,
159
mpn_get_base256_le (uint8_t *rp, size_t rn,
152
		    const mp_limb_t *xp, mp_size_t xn);
160
		    const mp_limb_t *xp, mp_size_t xn);
153
161
(-)nettle-3.4/Makefile.in (-1 / +6 lines)
Lines 92-97 Link Here
92
		 camellia256-meta.c \
92
		 camellia256-meta.c \
93
		 cast128.c cast128-meta.c cbc.c \
93
		 cast128.c cast128-meta.c cbc.c \
94
		 ccm.c ccm-aes128.c ccm-aes192.c ccm-aes256.c cfb.c \
94
		 ccm.c ccm-aes128.c ccm-aes192.c ccm-aes256.c cfb.c \
95
		 cnd-memcpy.c \
95
		 chacha-crypt.c chacha-core-internal.c \
96
		 chacha-crypt.c chacha-core-internal.c \
96
		 chacha-poly1305.c chacha-poly1305-meta.c \
97
		 chacha-poly1305.c chacha-poly1305-meta.c \
97
		 chacha-set-key.c chacha-set-nonce.c \
98
		 chacha-set-key.c chacha-set-nonce.c \
Lines 143-152 Link Here
143
		  bignum.c bignum-random.c bignum-random-prime.c \
144
		  bignum.c bignum-random.c bignum-random-prime.c \
144
		  sexp2bignum.c \
145
		  sexp2bignum.c \
145
		  pkcs1.c pkcs1-encrypt.c pkcs1-decrypt.c \
146
		  pkcs1.c pkcs1-encrypt.c pkcs1-decrypt.c \
147
		  pkcs1-sec-decrypt.c \
146
		  pkcs1-rsa-digest.c pkcs1-rsa-md5.c pkcs1-rsa-sha1.c \
148
		  pkcs1-rsa-digest.c pkcs1-rsa-md5.c pkcs1-rsa-sha1.c \
147
		  pkcs1-rsa-sha256.c pkcs1-rsa-sha512.c \
149
		  pkcs1-rsa-sha256.c pkcs1-rsa-sha512.c \
148
		  pss.c pss-mgf1.c \
150
		  pss.c pss-mgf1.c \
149
		  rsa.c rsa-sign.c rsa-sign-tr.c rsa-verify.c \
151
		  rsa.c rsa-sign.c rsa-sign-tr.c rsa-verify.c \
152
		  rsa-sec-compute-root.c \
150
		  rsa-pkcs1-sign.c rsa-pkcs1-sign-tr.c rsa-pkcs1-verify.c \
153
		  rsa-pkcs1-sign.c rsa-pkcs1-sign-tr.c rsa-pkcs1-verify.c \
151
		  rsa-md5-sign.c rsa-md5-sign-tr.c rsa-md5-verify.c \
154
		  rsa-md5-sign.c rsa-md5-sign-tr.c rsa-md5-verify.c \
152
		  rsa-sha1-sign.c rsa-sha1-sign-tr.c rsa-sha1-verify.c \
155
		  rsa-sha1-sign.c rsa-sha1-sign-tr.c rsa-sha1-verify.c \
Lines 154-160 Link Here
154
		  rsa-sha512-sign.c rsa-sha512-sign-tr.c rsa-sha512-verify.c \
157
		  rsa-sha512-sign.c rsa-sha512-sign-tr.c rsa-sha512-verify.c \
155
		  rsa-pss-sha256-sign-tr.c rsa-pss-sha256-verify.c \
158
		  rsa-pss-sha256-sign-tr.c rsa-pss-sha256-verify.c \
156
		  rsa-pss-sha512-sign-tr.c rsa-pss-sha512-verify.c \
159
		  rsa-pss-sha512-sign-tr.c rsa-pss-sha512-verify.c \
157
		  rsa-encrypt.c rsa-decrypt.c rsa-decrypt-tr.c \
160
		  rsa-encrypt.c rsa-decrypt.c \
161
		  rsa-sec-decrypt.c rsa-decrypt-tr.c \
158
		  rsa-keygen.c rsa-blind.c \
162
		  rsa-keygen.c rsa-blind.c \
159
		  rsa2sexp.c sexp2rsa.c \
163
		  rsa2sexp.c sexp2rsa.c \
160
		  dsa.c dsa-compat.c dsa-compat-keygen.c dsa-gen-params.c \
164
		  dsa.c dsa-compat.c dsa-compat-keygen.c dsa-gen-params.c \
Lines 228-233 Link Here
228
	aes-internal.h camellia-internal.h serpent-internal.h \
232
	aes-internal.h camellia-internal.h serpent-internal.h \
229
	cast128_sboxes.h desinfo.h desCode.h \
233
	cast128_sboxes.h desinfo.h desCode.h \
230
	memxor-internal.h nettle-internal.h nettle-write.h \
234
	memxor-internal.h nettle-internal.h nettle-write.h \
235
	rsa-internal.h \
231
	gmp-glue.h ecc-internal.h fat-setup.h \
236
	gmp-glue.h ecc-internal.h fat-setup.h \
232
	mini-gmp.h asm.m4 \
237
	mini-gmp.h asm.m4 \
233
	nettle.texinfo nettle.info nettle.html nettle.pdf sha-example.c
238
	nettle.texinfo nettle.info nettle.html nettle.pdf sha-example.c
(-)nettle-3.4/memops.h (+6 lines)
Lines 39-49 Link Here
39
#endif
39
#endif
40
40
41
/* Name mangling */
41
/* Name mangling */
42
#define cnd_memcpy nettle_cnd_memcpy
42
#define memeql_sec nettle_memeql_sec
43
#define memeql_sec nettle_memeql_sec
43
44
44
int
45
int
45
memeql_sec (const void *a, const void *b, size_t n);
46
memeql_sec (const void *a, const void *b, size_t n);
46
47
48
/* Side-channel silent conditional memcpy. cnd must be 0 (nop) or 1
49
   (copy). */
50
void
51
cnd_memcpy(int cnd, volatile void *dst, const volatile void *src, size_t n);
52
47
#ifdef __cplusplus
53
#ifdef __cplusplus
48
}
54
}
49
#endif
55
#endif
(-)nettle-3.4/pkcs1-decrypt.c (-37 / +2 lines)
Lines 41-46 Link Here
41
41
42
#include "bignum.h"
42
#include "bignum.h"
43
#include "gmp-glue.h"
43
#include "gmp-glue.h"
44
#include "rsa-internal.h"
44
45
45
int
46
int
46
pkcs1_decrypt (size_t key_size,
47
pkcs1_decrypt (size_t key_size,
Lines 48-96 Link Here
48
	       size_t *length, uint8_t *message)
49
	       size_t *length, uint8_t *message)
49
{
50
{
50
  TMP_GMP_DECL(em, uint8_t);
51
  TMP_GMP_DECL(em, uint8_t);
51
  uint8_t *terminator;
52
  size_t padding;
53
  size_t message_length;
54
  int ret;
52
  int ret;
55
53
56
  TMP_GMP_ALLOC(em, key_size);
54
  TMP_GMP_ALLOC(em, key_size);
57
  nettle_mpz_get_str_256(key_size, em, m);
55
  nettle_mpz_get_str_256(key_size, em, m);
58
56
59
  /* Check format */
57
  ret = _pkcs1_sec_decrypt_variable (length, message, key_size, em);
60
  if (em[0] || em[1] != 2)
61
    {
62
      ret = 0;
63
      goto cleanup;
64
    }
65
58
66
  terminator = memchr(em + 2, 0, key_size - 2);
67
68
  if (!terminator)
69
    {
70
      ret = 0;
71
      goto cleanup;
72
    }
73
  
74
  padding = terminator - (em + 2);
75
  if (padding < 8)
76
    {
77
      ret = 0;
78
      goto cleanup;
79
    }
80
81
  message_length = key_size - 3 - padding;
82
83
  if (*length < message_length)
84
    {
85
      ret = 0;
86
      goto cleanup;
87
    }
88
  
89
  memcpy(message, terminator + 1, message_length);
90
  *length = message_length;
91
92
  ret = 1;
93
cleanup:
94
  TMP_GMP_FREE(em);
59
  TMP_GMP_FREE(em);
95
  return ret;
60
  return ret;
96
}
61
}
(-)nettle-3.4/pkcs1-sec-decrypt.c (+149 lines)
Line 0 Link Here
1
/* pkcs1-sec-decrypt.c
2
3
   The RSA publickey algorithm. Side channel resistant PKCS#1 decryption.
4
5
   Copyright (C) 2001, 2012 Niels Möller
6
   Copyright (C) 2018 Red Hat, Inc.
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 <assert.h>
40
41
#include <string.h>
42
43
#include "memops.h"
44
45
#include "gmp-glue.h"
46
#include "rsa.h"
47
#include "rsa-internal.h"
48
49
/* Inputs are always cast to uint32_t values. But all values used in this
50
 * function should never exceed the maximum value of a uint32_t anyway.
51
 * these macros returns 1 on success, 0 on failure */
52
#define NOT_EQUAL(a, b) \
53
    ((0U - ((uint32_t)(a) ^ (uint32_t)(b))) >> 31)
54
#define EQUAL(a, b) \
55
    ((((uint32_t)(a) ^ (uint32_t)(b)) - 1U) >> 31)
56
#define GREATER_OR_EQUAL(a, b) \
57
    (1U - (((uint32_t)(a) - (uint32_t)(b)) >> 31))
58
59
int
60
_pkcs1_sec_decrypt (size_t length, uint8_t *message,
61
                    size_t padded_message_length,
62
                    const volatile uint8_t *padded_message)
63
{
64
  volatile int ok;
65
  size_t i, t;
66
67
  assert (padded_message_length >= length);
68
69
  t = padded_message_length - length - 1;
70
71
  /* Check format, padding, message_size */
72
  ok = EQUAL(padded_message[0], 0);       /* ok if padded_message[0] == 0 */
73
  ok &= EQUAL(padded_message[1], 2);      /* ok if padded_message[1] == 2 */
74
  for (i = 2; i < t; i++)      /* check padding has no zeros */
75
    {
76
      ok &= NOT_EQUAL(padded_message[i], 0);
77
    }
78
  ok &= EQUAL(padded_message[t], 0);      /* ok if terminator == 0 */
79
80
  /* fill destination buffer regardless of outcome */
81
  cnd_memcpy(ok, message, padded_message + t + 1, length);
82
83
  return ok;
84
}
85
86
int
87
_pkcs1_sec_decrypt_variable(size_t *length, uint8_t *message,
88
                            size_t padded_message_length,
89
                            const volatile uint8_t *padded_message)
90
{
91
  volatile int not_found = 1;
92
  volatile int ok;
93
  volatile size_t offset;
94
  size_t buflen, msglen;
95
  size_t shift, i;
96
97
  /* Check format, padding, message_size */
98
  ok = EQUAL(padded_message[0], 0);
99
  ok &= EQUAL(padded_message[1], 2);
100
101
  /* length is discovered in a side-channel silent way.
102
   * not_found goes to 0 when the terminator is found.
103
   * offset strts at 3 as it includes the terminator and
104
   * the fomat bytes already */
105
  offset = 3;
106
  for (i = 2; i < padded_message_length; i++)
107
    {
108
      not_found &= NOT_EQUAL(padded_message[i], 0);
109
      offset += not_found;
110
    }
111
  /* check if we ran out of buffer */
112
  ok &= NOT_EQUAL(not_found, 1);
113
  /* padding must be >= 11 (2 format bytes + 8 pad bytes min. + terminator) */
114
  ok &= GREATER_OR_EQUAL(offset, 11);
115
116
  /* offset can vary between 3 and padded_message_length, due to the loop
117
   * above, therefore msglen can't underflow */
118
  msglen = padded_message_length - offset;
119
120
  /* we always fill the whole buffer but only up to
121
   * padded_message_length length */
122
  buflen = *length;
123
  if (buflen > padded_message_length) { /* input independent branch */
124
    buflen = padded_message_length;
125
  }
126
127
  /* if the message length is larger than the buffer we must fail */
128
  ok &= GREATER_OR_EQUAL(buflen, msglen);
129
130
  /* fill destination buffer fully regardless of outcome. Copies the message
131
   * in a memory access independent way. The destination message buffer will
132
   * be clobbered past the message length. */
133
  shift = padded_message_length - buflen;
134
  cnd_memcpy(ok, message, padded_message + shift, buflen);
135
  offset -= shift;
136
  /* In this loop, the bits of the 'offset' variable are used as shifting
137
   * conditions, starting from the least significant bit. The end result is
138
   * that the buffer is shifted left exactly 'offset' bytes. */
139
  for (shift = 1; shift < buflen; shift <<= 1, offset >>= 1)
140
    {
141
      /* 'ok' is both a least significant bit mask and a condition */
142
      cnd_memcpy(offset & ok, message, message + shift, buflen - shift);
143
    }
144
145
  /* update length only if we succeeded, otherwise leave unchanged */
146
  *length = (msglen & (-(size_t) ok)) + (*length & ((size_t) ok - 1));
147
148
  return ok;
149
}
(-)nettle-3.4/rsa-decrypt-tr.c (-8 / +18 lines)
Lines 37-45 Link Here
37
#endif
37
#endif
38
38
39
#include "rsa.h"
39
#include "rsa.h"
40
40
#include "rsa-internal.h"
41
#include "bignum.h"
41
#include "gmp-glue.h"
42
#include "pkcs1.h"
43
42
44
int
43
int
45
rsa_decrypt_tr(const struct rsa_public_key *pub,
44
rsa_decrypt_tr(const struct rsa_public_key *pub,
Lines 48-61 Link Here
48
	       size_t *length, uint8_t *message,
47
	       size_t *length, uint8_t *message,
49
	       const mpz_t gibberish)
48
	       const mpz_t gibberish)
50
{
49
{
51
  mpz_t m;
50
  TMP_GMP_DECL (m, mp_limb_t);
51
  TMP_GMP_DECL (em, uint8_t);
52
  mp_size_t key_limb_size;
52
  int res;
53
  int res;
53
54
54
  mpz_init_set(m, gibberish);
55
  key_limb_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
56
57
  TMP_GMP_ALLOC (m, key_limb_size);
58
  TMP_GMP_ALLOC (em, key->size);
59
60
  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m,
61
				  mpz_limbs_read(gibberish),
62
				  mpz_size(gibberish));
63
64
  mpn_get_base256 (em, key->size, m, key_limb_size);
55
65
56
  res = (rsa_compute_root_tr (pub, key, random_ctx, random, m, gibberish)
66
  res &= _pkcs1_sec_decrypt_variable (length, message, key->size, em);
57
	 && pkcs1_decrypt (key->size, m, length, message));
58
67
59
  mpz_clear(m);
68
  TMP_GMP_FREE (em);
69
  TMP_GMP_FREE (m);
60
  return res;
70
  return res;
61
}
71
}
(-)nettle-3.4/rsa.h (+10 lines)
Lines 88-93 Link Here
88
#define rsa_encrypt nettle_rsa_encrypt
88
#define rsa_encrypt nettle_rsa_encrypt
89
#define rsa_decrypt nettle_rsa_decrypt
89
#define rsa_decrypt nettle_rsa_decrypt
90
#define rsa_decrypt_tr nettle_rsa_decrypt_tr
90
#define rsa_decrypt_tr nettle_rsa_decrypt_tr
91
#define rsa_sec_decrypt nettle_rsa_sec_decrypt
91
#define rsa_compute_root nettle_rsa_compute_root
92
#define rsa_compute_root nettle_rsa_compute_root
92
#define rsa_compute_root_tr nettle_rsa_compute_root_tr
93
#define rsa_compute_root_tr nettle_rsa_compute_root_tr
93
#define rsa_generate_keypair nettle_rsa_generate_keypair
94
#define rsa_generate_keypair nettle_rsa_generate_keypair
Lines 423-428 Link Here
423
	       size_t *length, uint8_t *message,
424
	       size_t *length, uint8_t *message,
424
	       const mpz_t gibberish);
425
	       const mpz_t gibberish);
425
426
427
/* like rsa_decrypt_tr but with additional side-channel resistance.
428
 * NOTE: the length of the final message must be known in advance. */
429
int
430
rsa_sec_decrypt(const struct rsa_public_key *pub,
431
	        const struct rsa_private_key *key,
432
	        void *random_ctx, nettle_random_func *random,
433
	        size_t length, uint8_t *message,
434
	        const mpz_t gibberish);
435
426
/* Compute x, the e:th root of m. Calling it with x == m is allowed. */
436
/* Compute x, the e:th root of m. Calling it with x == m is allowed. */
427
void
437
void
428
rsa_compute_root(const struct rsa_private_key *key,
438
rsa_compute_root(const struct rsa_private_key *key,
(-)nettle-3.4/rsa-internal.h (+75 lines)
Line 0 Link Here
1
/* rsa-internal.h
2
3
   The RSA publickey algorithm.
4
5
   Copyright (C) 2001, 2002 Niels Möller
6
7
   This file is part of GNU Nettle.
8
9
   GNU Nettle is free software: you can redistribute it and/or
10
   modify it under the terms of either:
11
12
     * the GNU Lesser General Public License as published by the Free
13
       Software Foundation; either version 3 of the License, or (at your
14
       option) any later version.
15
16
   or
17
18
     * the GNU General Public License as published by the Free
19
       Software Foundation; either version 2 of the License, or (at your
20
       option) any later version.
21
22
   or both in parallel, as here.
23
24
   GNU Nettle is distributed in the hope that it will be useful,
25
   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
   General Public License for more details.
28
29
   You should have received copies of the GNU General Public License and
30
   the GNU Lesser General Public License along with this program.  If
31
   not, see http://www.gnu.org/licenses/.
32
*/
33
34
#ifndef NETTLE_RSA_INTERNAL_H_INCLUDED
35
#define NETTLE_RSA_INTERNAL_H_INCLUDED
36
37
#include "nettle-types.h"
38
39
#include "rsa.h"
40
41
#define _rsa_sec_compute_root_itch _nettle_rsa_sec_compute_root_itch
42
#define _rsa_sec_compute_root _nettle_rsa_sec_compute_root
43
#define _rsa_sec_compute_root_tr _nettle_rsa_sec_compute_root_tr
44
#define _pkcs1_sec_decrypt _nettle_pkcs1_sec_decrypt
45
#define _pkcs1_sec_decrypt_variable _nettle_pkcs1_sec_decrypt_variable
46
47
/* side-channel silent root computation */
48
mp_size_t
49
_rsa_sec_compute_root_itch(const struct rsa_private_key *key);
50
void
51
_rsa_sec_compute_root(const struct rsa_private_key *key,
52
                      mp_limb_t *rp, const mp_limb_t *mp,
53
                      mp_limb_t *scratch);
54
55
/* Safe side-channel silent variant, using RSA blinding, and checking the
56
 * result after CRT. */
57
int
58
_rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
59
			 const struct rsa_private_key *key,
60
			 void *random_ctx, nettle_random_func *random,
61
			 mp_limb_t *x, const mp_limb_t *m, size_t mn);
62
63
/* additional resistance to memory access side-channel attacks.
64
 * Note: message buffer is returned unchanged on error */
65
int
66
_pkcs1_sec_decrypt (size_t length, uint8_t *message,
67
                    size_t padded_message_length,
68
                    const volatile uint8_t *padded_message);
69
70
int
71
_pkcs1_sec_decrypt_variable(size_t *length, uint8_t *message,
72
                            size_t padded_message_length,
73
                            const volatile uint8_t *padded_message);
74
75
#endif /* NETTLE_RSA_INTERNAL_H_INCLUDED */
(-)nettle-3.4/rsa-sec-compute-root.c (+195 lines)
Line 0 Link Here
1
/* rsa-sec-compute-root.c
2
3
   Side-channel silent RSA root computation.
4
5
   Copyright (C) 2018 Niels Möller
6
   Copyright (C) 2018 Red Hat, Inc
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 <assert.h>
40
41
#include "rsa.h"
42
#include "rsa-internal.h"
43
#include "gmp-glue.h"
44
45
#if !NETTLE_USE_MINI_GMP
46
#define MAX(a, b) ((a) > (b) ? (a) : (b))
47
48
/* Like mpn_sec_mul_itch, monotonously increasing in operand sizes. */
49
static mp_size_t
50
sec_mul_itch (mp_size_t an, mp_size_t bn)
51
{
52
  if (an >= bn)
53
    return mpn_sec_mul_itch (an, bn);
54
  else
55
    return mpn_sec_mul_itch (bn, an);
56
}
57
58
/* Writes an + bn limbs to the rp area */
59
static void
60
sec_mul (mp_limb_t *rp,
61
	 const mp_limb_t *ap, mp_size_t an,
62
	 const mp_limb_t *bp, mp_size_t bn, mp_limb_t *scratch)
63
{
64
  if (an >= bn)
65
    mpn_sec_mul (rp, ap, an, bp, bn, scratch);
66
  else
67
    mpn_sec_mul (rp, bp, bn, ap, an, scratch);
68
}
69
70
static mp_size_t
71
sec_mod_mul_itch (mp_size_t an, mp_size_t bn, mp_size_t mn)
72
{
73
  mp_size_t mul_itch = sec_mul_itch (an, bn);
74
  mp_size_t mod_itch = mpn_sec_div_r_itch (an + bn, mn);
75
  return MAX(mul_itch, mod_itch);
76
}
77
78
/* Sets r <-- a b % m. Needs space for an + bn limbs at rp. It is
79
   required than an + bn >= mn. */
80
static void
81
sec_mod_mul (mp_limb_t *rp,
82
	     const mp_limb_t *ap, mp_size_t an,
83
	     const mp_limb_t *bp, mp_size_t bn,
84
	     const mp_limb_t *mp, mp_size_t mn,
85
	     mp_limb_t *scratch)
86
{
87
  assert (an + bn >= mn);
88
  sec_mul (rp, ap, an, bp, bn, scratch);
89
  mpn_sec_div_r (rp, an + bn, mp, mn, scratch);
90
}
91
92
static mp_size_t
93
sec_powm_itch (mp_size_t bn, mp_size_t en, mp_size_t mn)
94
{
95
  mp_size_t mod_itch = bn + mpn_sec_div_r_itch (bn, mn);
96
  mp_size_t pow_itch = mn + mpn_sec_powm_itch (mn, en * GMP_NUMB_BITS, mn);
97
  return MAX (mod_itch, pow_itch);
98
}
99
100
/* Sets r <-- b ^ e % m. Performs an initial reduction b mod m, and
101
   requires bn >= mn. */
102
static void
103
sec_powm (mp_limb_t *rp,
104
	  const mp_limb_t *bp, mp_size_t bn,
105
	  const mp_limb_t *ep, mp_size_t en,
106
	  const mp_limb_t *mp, mp_size_t mn, mp_limb_t *scratch)
107
{
108
  assert (bn >= mn);
109
  assert (en <= mn);
110
  mpn_copyi (scratch, bp, bn);
111
  mpn_sec_div_r (scratch, bn, mp, mn, scratch + bn);
112
  mpn_sec_powm (rp, scratch, mn, ep, en * GMP_NUMB_BITS, mp, mn,
113
		scratch + mn);
114
}
115
116
mp_size_t
117
_rsa_sec_compute_root_itch (const struct rsa_private_key *key)
118
{
119
  mp_size_t nn = NETTLE_OCTET_SIZE_TO_LIMB_SIZE (key->size);
120
  mp_size_t pn = mpz_size (key->p);
121
  mp_size_t qn = mpz_size (key->q);
122
  mp_size_t an = mpz_size (key->a);
123
  mp_size_t bn = mpz_size (key->b);
124
  mp_size_t cn = mpz_size (key->c);
125
126
  mp_size_t powm_p_itch = sec_powm_itch (nn, an, pn);
127
  mp_size_t powm_q_itch = sec_powm_itch (nn, bn, qn);
128
  mp_size_t mod_mul_itch = cn + MAX(pn, qn) 
129
    + sec_mod_mul_itch (MAX(pn, qn), cn, pn);
130
131
  mp_size_t mul_itch = sec_mul_itch (qn, pn);
132
  mp_size_t add_1_itch = mpn_sec_add_1_itch (nn - qn);
133
134
  /* pn + qn for the product q * r_mod_p' */
135
  mp_size_t itch = pn + qn + MAX (mul_itch, add_1_itch);
136
137
  itch = MAX (itch, powm_p_itch);
138
  itch = MAX (itch, powm_q_itch);
139
  itch = MAX (itch, mod_mul_itch);
140
141
  /* pn + qn for the r_mod_p and r_mod_q temporaries. */
142
  return pn + qn + itch;
143
}
144
145
void
146
_rsa_sec_compute_root (const struct rsa_private_key *key,
147
		       mp_limb_t *rp, const mp_limb_t *mp,
148
		       mp_limb_t *scratch)
149
{
150
  mp_size_t nn = NETTLE_OCTET_SIZE_TO_LIMB_SIZE (key->size);
151
152
  /* The common case is pn = qn. This function would be simpler if we
153
   * could require that pn >= qn. */
154
  const mp_limb_t *pp = mpz_limbs_read (key->p);
155
  const mp_limb_t *qp = mpz_limbs_read (key->q);
156
157
  mp_size_t pn = mpz_size (key->p);
158
  mp_size_t qn = mpz_size (key->q);
159
  mp_size_t an = mpz_size (key->a);
160
  mp_size_t bn = mpz_size (key->b);
161
  mp_size_t cn = mpz_size (key->c);
162
163
  mp_limb_t *r_mod_p = scratch;
164
  mp_limb_t *r_mod_q = scratch + pn;
165
  mp_limb_t *scratch_out = r_mod_q + qn;
166
  mp_limb_t cy;
167
168
  assert (pn <= nn);
169
  assert (qn <= nn);
170
  assert (an <= pn);
171
  assert (bn <= qn);
172
  assert (cn <= pn);
173
174
  /* Compute r_mod_p = m^d % p = (m%p)^a % p */
175
  sec_powm (r_mod_p, mp, nn, mpz_limbs_read (key->a), an, pp, pn, scratch_out);
176
  /* Compute r_mod_q = m^d % q = (m%q)^b % q */
177
  sec_powm (r_mod_q, mp, nn, mpz_limbs_read (key->b), bn, qp, qn, scratch_out);
178
179
  /* Set r_mod_p' = r_mod_p * c % p - r_mod_q * c % p . */
180
  sec_mod_mul (scratch_out, r_mod_p, pn, mpz_limbs_read (key->c), cn, pp, pn,
181
	       scratch_out + cn + pn);
182
  mpn_copyi (r_mod_p, scratch_out, pn);
183
184
  sec_mod_mul (scratch_out, r_mod_q, qn, mpz_limbs_read (key->c), cn, pp, pn,
185
	       scratch_out + cn + qn);
186
  cy = mpn_sub_n (r_mod_p, r_mod_p, scratch_out, pn);
187
  cnd_add_n (cy, r_mod_p, pp, pn);
188
189
  /* Finally, compute x = r_mod_q + q r_mod_p' */
190
  sec_mul (scratch_out, qp, qn, r_mod_p, pn, scratch_out + pn + qn);
191
192
  cy = mpn_add_n (rp, scratch_out, r_mod_q, qn);
193
  mpn_sec_add_1 (rp + qn, scratch_out + qn, nn - qn, cy, scratch_out + pn + qn);
194
}
195
#endif
(-)nettle-3.4/rsa-sec-decrypt.c (+72 lines)
Line 0 Link Here
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 "rsa-internal.h"
43
44
#include "gmp-glue.h"
45
46
int
47
rsa_sec_decrypt(const struct rsa_public_key *pub,
48
	        const struct rsa_private_key *key,
49
	        void *random_ctx, nettle_random_func *random,
50
	        size_t length, uint8_t *message,
51
	        const mpz_t gibberish)
52
{
53
  TMP_GMP_DECL (m, mp_limb_t);
54
  TMP_GMP_DECL (em, uint8_t);
55
  int res;
56
57
  TMP_GMP_ALLOC (m, mpz_size(pub->n));
58
  TMP_GMP_ALLOC (em, key->size);
59
60
  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m,
61
				  mpz_limbs_read(gibberish),
62
				  mpz_size(gibberish));
63
64
  mpn_get_base256 (em, key->size, m, mpz_size(pub->n));
65
66
  res &= _pkcs1_sec_decrypt (length, message, key->size, em);
67
68
  TMP_GMP_FREE (em);
69
  TMP_GMP_FREE (m);
70
  return res;
71
}
72
(-)nettle-3.4/rsa-sign.c (-4 / +46 lines)
Lines 35-43 Link Here
35
# include "config.h"
35
# include "config.h"
36
#endif
36
#endif
37
37
38
#include "rsa.h"
38
#include <assert.h>
39
39
40
#include "bignum.h"
40
#include "rsa.h"
41
#include "rsa-internal.h"
42
#include "gmp-glue.h"
41
43
42
void
44
void
43
rsa_private_key_init(struct rsa_private_key *key)
45
rsa_private_key_init(struct rsa_private_key *key)
Lines 69-75 Link Here
69
rsa_private_key_prepare(struct rsa_private_key *key)
71
rsa_private_key_prepare(struct rsa_private_key *key)
70
{
72
{
71
  mpz_t n;
73
  mpz_t n;
72
  
74
75
  /* A key is invalid if the sizes of q and c are smaller than
76
   * the size of n, we rely on that property in calculations so
77
   * fail early if that happens. */
78
  if (mpz_size (key->q) + mpz_size (key->c) < mpz_size(key->p))
79
    return 0;
80
73
  /* The size of the product is the sum of the sizes of the factors,
81
  /* The size of the product is the sum of the sizes of the factors,
74
   * or sometimes one less. It's possible but tricky to compute the
82
   * or sometimes one less. It's possible but tricky to compute the
75
   * size without computing the full product. */
83
   * size without computing the full product. */
Lines 80-89 Link Here
80
  key->size = _rsa_check_size(n);
88
  key->size = _rsa_check_size(n);
81
89
82
  mpz_clear(n);
90
  mpz_clear(n);
83
  
91
84
  return (key->size > 0);
92
  return (key->size > 0);
85
}
93
}
86
94
95
#if NETTLE_USE_MINI_GMP
96
87
/* Computing an rsa root. */
97
/* Computing an rsa root. */
88
void
98
void
89
rsa_compute_root(const struct rsa_private_key *key,
99
rsa_compute_root(const struct rsa_private_key *key,
Lines 142-144 Link Here
142
152
143
  mpz_clear(xp); mpz_clear(xq);
153
  mpz_clear(xp); mpz_clear(xq);
144
}
154
}
155
156
#else /* !NETTLE_USE_MINI_GMP */
157
158
/* Computing an rsa root. */
159
void
160
rsa_compute_root(const struct rsa_private_key *key,
161
		 mpz_t x, const mpz_t m)
162
{
163
  TMP_GMP_DECL (scratch, mp_limb_t);
164
  TMP_GMP_DECL (ml, mp_limb_t);
165
  mp_limb_t *xl;
166
  size_t key_size;
167
168
  key_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
169
  assert(mpz_size (m) <= key_size);
170
171
  /* we need a copy because m can be shorter than key_size,
172
   * but _rsa_sec_compute_root expect all inputs to be
173
   * normalized to a key_size long buffer length */
174
  TMP_GMP_ALLOC (ml, key_size);
175
  mpz_limbs_copy(ml, m, key_size);
176
177
  TMP_GMP_ALLOC (scratch, _rsa_sec_compute_root_itch(key));
178
179
  xl = mpz_limbs_write (x, key_size);
180
  _rsa_sec_compute_root (key, xl, ml, scratch);
181
  mpz_limbs_finish (x, key_size);
182
183
  TMP_GMP_FREE (ml);
184
  TMP_GMP_FREE (scratch);
185
}
186
#endif /* !NETTLE_USE_MINI_GMP */
(-)nettle-3.4/rsa-sign-tr.c (+255 lines)
Lines 4-9 Link Here
4
4
5
   Copyright (C) 2001, 2015 Niels Möller
5
   Copyright (C) 2001, 2015 Niels Möller
6
   Copyright (C) 2012 Nikos Mavrogiannopoulos
6
   Copyright (C) 2012 Nikos Mavrogiannopoulos
7
   Copyright (C) 2018 Red Hat Inc.
7
8
8
   This file is part of GNU Nettle.
9
   This file is part of GNU Nettle.
9
10
Lines 36-43 Link Here
36
# include "config.h"
37
# include "config.h"
37
#endif
38
#endif
38
39
40
#include <assert.h>
41
42
#include "gmp-glue.h"
39
#include "rsa.h"
43
#include "rsa.h"
44
#include "rsa-internal.h"
45
46
#define MAX(a, b) ((a) > (b) ? (a) : (b))
40
47
48
#if NETTLE_USE_MINI_GMP
41
/* Blinds m, by computing c = m r^e (mod n), for a random r. Also
49
/* 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. */
50
   returns the inverse (ri), for use by rsa_unblind. */
43
static void
51
static void
Lines 118-120 Link Here
118
126
119
  return res;
127
  return res;
120
}
128
}
129
130
int
131
_rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
132
			 const struct rsa_private_key *key,
133
			 void *random_ctx, nettle_random_func *random,
134
			 mp_limb_t *x, const mp_limb_t *m, size_t mn)
135
{
136
  mpz_t mz;
137
  mpz_t xz;
138
  int res;
139
140
  mpz_init(mz);
141
  mpz_init(xz);
142
143
  mpn_copyi(mpz_limbs_write(mz, mn), m, mn);
144
  mpz_limbs_finish(mz, mn);
145
146
  res = rsa_compute_root_tr(pub, key, random_ctx, random, xz, mz);
147
148
  if (res)
149
    mpz_limbs_copy(x, xz, mpz_size(pub->n));
150
151
  mpz_clear(mz);
152
  mpz_clear(xz);
153
  return res;
154
}
155
#else
156
/* Blinds m, by computing c = m r^e (mod n), for a random r. Also
157
   returns the inverse (ri), for use by rsa_unblind. */
158
static void
159
rsa_sec_blind (const struct rsa_public_key *pub,
160
               void *random_ctx, nettle_random_func *random,
161
               mp_limb_t *c, mp_limb_t *ri, const mp_limb_t *m,
162
               mp_size_t mn)
163
{
164
  const mp_limb_t *ep = mpz_limbs_read (pub->e);
165
  const mp_limb_t *np = mpz_limbs_read (pub->n);
166
  mp_bitcnt_t ebn = mpz_sizeinbase (pub->e, 2);
167
  mp_size_t nn = mpz_size (pub->n);
168
  size_t itch;
169
  size_t i2;
170
  mp_limb_t *scratch;
171
  TMP_GMP_DECL (tp, mp_limb_t);
172
  TMP_GMP_DECL (rp, mp_limb_t);
173
  TMP_GMP_DECL (r, uint8_t);
174
175
  TMP_GMP_ALLOC (rp, nn);
176
  TMP_GMP_ALLOC (r, nn * sizeof(mp_limb_t));
177
178
  /* c = m*(r^e) mod n */
179
  itch = mpn_sec_powm_itch(nn, ebn, nn);
180
  i2 = mpn_sec_mul_itch(nn, mn);
181
  itch = MAX(itch, i2);
182
  i2 = mpn_sec_div_r_itch(nn + mn, nn);
183
  itch = MAX(itch, i2);
184
  i2 = mpn_sec_invert_itch(nn);
185
  itch = MAX(itch, i2);
186
187
  TMP_GMP_ALLOC (tp, nn + mn + itch);
188
  scratch = tp + nn + mn;
189
190
  /* ri = r^(-1) */
191
  do
192
    {
193
      random(random_ctx, nn * sizeof(mp_limb_t), (uint8_t *)r);
194
      mpn_set_base256(rp, nn, r, nn * sizeof(mp_limb_t));
195
      mpn_copyi(tp, rp, nn);
196
      /* invert r */
197
    }
198
  while (!mpn_sec_invert (ri, tp, np, nn, 2 * nn * GMP_NUMB_BITS, scratch));
199
200
  mpn_sec_powm (c, rp, nn, ep, ebn, np, nn, scratch);
201
  /* normally mn == nn, but m can be smaller in some cases */
202
  mpn_sec_mul (tp, c, nn, m, mn, scratch);
203
  mpn_sec_div_r (tp, nn + mn, np, nn, scratch);
204
  mpn_copyi(c, tp, nn);
205
206
  TMP_GMP_FREE (r);
207
  TMP_GMP_FREE (rp);
208
  TMP_GMP_FREE (tp);
209
}
210
211
/* m = c ri mod n */
212
static void
213
rsa_sec_unblind (const struct rsa_public_key *pub,
214
                 mp_limb_t *x, mp_limb_t *ri, const mp_limb_t *c)
215
{
216
  const mp_limb_t *np = mpz_limbs_read (pub->n);
217
  mp_size_t nn = mpz_size (pub->n);
218
219
  size_t itch;
220
  size_t i2;
221
  mp_limb_t *scratch;
222
  TMP_GMP_DECL(tp, mp_limb_t);
223
224
  itch = mpn_sec_mul_itch(nn, nn);
225
  i2 = mpn_sec_div_r_itch(nn + nn, nn);
226
  itch = MAX(itch, i2);
227
228
  TMP_GMP_ALLOC (tp, nn + nn + itch);
229
  scratch = tp + nn + nn;
230
231
  mpn_sec_mul (tp, c, nn, ri, nn, scratch);
232
  mpn_sec_div_r (tp, nn + nn, np, nn, scratch);
233
  mpn_copyi(x, tp, nn);
234
235
  TMP_GMP_FREE (tp);
236
}
237
238
static int
239
sec_equal(const mp_limb_t *a, const mp_limb_t *b, size_t limbs)
240
{
241
  volatile mp_limb_t z = 0;
242
243
  for (size_t i = 0; i < limbs; i++)
244
    {
245
      z |= (a[i] ^ b[i]);
246
    }
247
248
  /* FIXME: Might compile to a branch instruction on some platforms. */
249
  return z == 0;
250
}
251
252
static int
253
rsa_sec_check_root(const struct rsa_public_key *pub,
254
                   const mp_limb_t *x, const mp_limb_t *m)
255
{
256
  mp_size_t nn = mpz_size (pub->n);
257
  mp_size_t ebn = mpz_sizeinbase (pub->e, 2);
258
  const mp_limb_t *np = mpz_limbs_read (pub->n);
259
  const mp_limb_t *ep = mpz_limbs_read (pub->e);
260
  int ret;
261
262
  mp_size_t itch;
263
264
  mp_limb_t *scratch;
265
  TMP_GMP_DECL(tp, mp_limb_t);
266
267
  itch = mpn_sec_powm_itch (nn, ebn, nn);
268
  TMP_GMP_ALLOC (tp, nn + itch);
269
  scratch = tp + nn;
270
271
  mpn_sec_powm(tp, x, nn, ep, ebn, np, nn, scratch);
272
  ret = sec_equal(tp, m, nn);
273
274
  TMP_GMP_FREE (tp);
275
  return ret;
276
}
277
278
static void
279
cnd_mpn_zero (int cnd, volatile mp_ptr rp, mp_size_t n)
280
{
281
  volatile mp_limb_t c;
282
  volatile mp_limb_t mask = (mp_limb_t) cnd - 1;
283
284
  while (--n >= 0)
285
    {
286
      c = rp[n];
287
      c &= mask;
288
      rp[n] = c;
289
    }
290
}
291
292
/* Checks for any errors done in the RSA computation. That avoids
293
 * attacks which rely on faults on hardware, or even software MPI
294
 * implementation.
295
 * This version is side-channel silent even in case of error,
296
 * the destination buffer is always overwritten */
297
int
298
_rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
299
			 const struct rsa_private_key *key,
300
			 void *random_ctx, nettle_random_func *random,
301
			 mp_limb_t *x, const mp_limb_t *m, size_t mn)
302
{
303
  TMP_GMP_DECL (c, mp_limb_t);
304
  TMP_GMP_DECL (ri, mp_limb_t);
305
  TMP_GMP_DECL (scratch, mp_limb_t);
306
  size_t key_limb_size;
307
  int ret;
308
309
  key_limb_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
310
311
  /* mpz_powm_sec handles only odd moduli. If p, q or n is even, the
312
     key is invalid and rejected by rsa_private_key_prepare. However,
313
     some applications, notably gnutls, don't use this function, and
314
     we don't want an invalid key to lead to a crash down inside
315
     mpz_powm_sec. So do an additional check here. */
316
  if (mpz_even_p (pub->n) || mpz_even_p (key->p) || mpz_even_p (key->q))
317
    {
318
      mpn_zero(x, key_limb_size);
319
      return 0;
320
    }
321
322
  assert(mpz_size(pub->n) == key_limb_size);
323
  assert(mn <= key_limb_size);
324
325
  TMP_GMP_ALLOC (c, key_limb_size);
326
  TMP_GMP_ALLOC (ri, key_limb_size);
327
  TMP_GMP_ALLOC (scratch, _rsa_sec_compute_root_itch(key));
328
329
  rsa_sec_blind (pub, random_ctx, random, x, ri, m, mn);
330
331
  _rsa_sec_compute_root(key, c, x, scratch);
332
333
  ret = rsa_sec_check_root(pub, c, x);
334
335
  rsa_sec_unblind(pub, x, ri, c);
336
337
  cnd_mpn_zero(1 - ret, x, key_limb_size);
338
339
  TMP_GMP_FREE (scratch);
340
  TMP_GMP_FREE (ri);
341
  TMP_GMP_FREE (c);
342
  return ret;
343
}
344
345
/* Checks for any errors done in the RSA computation. That avoids
346
 * attacks which rely on faults on hardware, or even software MPI
347
 * implementation.
348
 * This version is maintained for API compatibility reasons. It
349
 * is not completely side-channel silent. There are conditionals
350
 * in buffer copying both in case of success or error.
351
 */
352
int
353
rsa_compute_root_tr(const struct rsa_public_key *pub,
354
		    const struct rsa_private_key *key,
355
		    void *random_ctx, nettle_random_func *random,
356
		    mpz_t x, const mpz_t m)
357
{
358
  TMP_GMP_DECL (l, mp_limb_t);
359
  int res;
360
361
  mp_size_t l_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
362
  TMP_GMP_ALLOC (l, l_size);
363
364
  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, l,
365
				  mpz_limbs_read(m), mpz_size(m));
366
  if (res) {
367
    mp_limb_t *xp = mpz_limbs_write (x, l_size);
368
    mpn_copyi (xp, l, l_size);
369
    mpz_limbs_finish (x, l_size);
370
  }
371
372
  TMP_GMP_FREE (l);
373
  return res;
374
}
375
#endif
(-)nettle-3.4/testsuite/cnd-memcpy-test.c (+47 lines)
Line 0 Link Here
1
#include "testutils.h"
2
#include "knuth-lfib.h"
3
#include "memops.h"
4
5
#if HAVE_VALGRIND_MEMCHECK_H
6
# include <valgrind/memcheck.h>
7
static void
8
cnd_memcpy_for_test(int cnd, void *dst, const void *src, size_t n)
9
{
10
  /* Makes valgrind trigger on any branches depending on the input
11
     data. */
12
  VALGRIND_MAKE_MEM_UNDEFINED (dst, n);
13
  VALGRIND_MAKE_MEM_UNDEFINED (src, n);
14
  VALGRIND_MAKE_MEM_UNDEFINED (&cnd, sizeof(cnd));
15
16
  cnd_memcpy (cnd, dst, src, n);
17
  VALGRIND_MAKE_MEM_DEFINED (src, n);
18
  VALGRIND_MAKE_MEM_DEFINED (dst, n);
19
}
20
#else
21
#define cnd_memcpy_for_test cnd_memcpy
22
#endif
23
24
#define MAX_SIZE 50
25
void
26
test_main(void)
27
{
28
  uint8_t src[MAX_SIZE];
29
  uint8_t dst[MAX_SIZE];
30
  uint8_t res[MAX_SIZE];
31
  struct knuth_lfib_ctx random_ctx;
32
33
  knuth_lfib_init (&random_ctx, 11);
34
35
  size_t size;
36
  for (size = 1; size < 50; size++)
37
    {
38
      knuth_lfib_random (&random_ctx, size, src);
39
      knuth_lfib_random (&random_ctx, size, dst);
40
      memcpy (res, dst, size);
41
      cnd_memcpy_for_test (0, res, src, size);
42
43
      ASSERT (memcmp (res, dst, size) == 0);
44
      cnd_memcpy_for_test (1, res, src, size);
45
      ASSERT (memcmp (res, src, size) == 0);
46
    }
47
}
(-)nettle-3.4/testsuite/Makefile.in (-3 / +7 lines)
Lines 14-19 Link Here
14
		    blowfish-test.c cast128-test.c \
14
		    blowfish-test.c cast128-test.c \
15
	            base16-test.c base64-test.c \
15
	            base16-test.c base64-test.c \
16
		    camellia-test.c chacha-test.c \
16
		    camellia-test.c chacha-test.c \
17
		    cnd-memcpy-test.c \
17
		    des-test.c des3-test.c des-compat-test.c \
18
		    des-test.c des3-test.c des-compat-test.c \
18
		    md2-test.c md4-test.c md5-test.c md5-compat-test.c \
19
		    md2-test.c md4-test.c md5-test.c md5-compat-test.c \
19
		    memeql-test.c memxor-test.c gosthash94-test.c \
20
		    memeql-test.c memxor-test.c gosthash94-test.c \
Lines 30-43 Link Here
30
		    hmac-test.c umac-test.c \
31
		    hmac-test.c umac-test.c \
31
		    meta-hash-test.c meta-cipher-test.c\
32
		    meta-hash-test.c meta-cipher-test.c\
32
		    meta-aead-test.c meta-armor-test.c \
33
		    meta-aead-test.c meta-armor-test.c \
33
		    buffer-test.c yarrow-test.c pbkdf2-test.c pss-mgf1-test.c
34
		    buffer-test.c yarrow-test.c pbkdf2-test.c
34
35
35
TS_HOGWEED_SOURCES = sexp-test.c sexp-format-test.c \
36
TS_HOGWEED_SOURCES = sexp-test.c sexp-format-test.c \
36
		     rsa2sexp-test.c sexp2rsa-test.c \
37
		     rsa2sexp-test.c sexp2rsa-test.c \
37
		     bignum-test.c random-prime-test.c \
38
		     bignum-test.c random-prime-test.c \
38
		     pkcs1-test.c pss-test.c rsa-sign-tr-test.c \
39
		     pkcs1-test.c pkcs1-sec-decrypt-test.c \
39
		     rsa-pss-sign-tr-test.c \
40
		     pss-test.c rsa-sign-tr-test.c \
41
		     pss-mgf1-test.c rsa-pss-sign-tr-test.c \
40
		     rsa-test.c rsa-encrypt-test.c rsa-keygen-test.c \
42
		     rsa-test.c rsa-encrypt-test.c rsa-keygen-test.c \
43
		     rsa-sec-decrypt-test.c \
44
		     rsa-compute-root-test.c \
41
		     dsa-test.c dsa-keygen-test.c \
45
		     dsa-test.c dsa-keygen-test.c \
42
		     curve25519-dh-test.c \
46
		     curve25519-dh-test.c \
43
		     ecc-mod-test.c ecc-modinv-test.c ecc-redc-test.c \
47
		     ecc-mod-test.c ecc-modinv-test.c ecc-redc-test.c \
(-)nettle-3.4/testsuite/pkcs1-sec-decrypt-test.c (+77 lines)
Line 0 Link Here
1
#include "testutils.h"
2
3
#include "rsa.h"
4
#include "rsa-internal.h"
5
6
#if HAVE_VALGRIND_MEMCHECK_H
7
# include <valgrind/memcheck.h>
8
static int
9
pkcs1_decrypt_for_test(size_t msg_len, uint8_t *msg,
10
                       size_t pad_len, uint8_t *pad)
11
{
12
  int ret;
13
14
  VALGRIND_MAKE_MEM_UNDEFINED (msg, msg_len);
15
  VALGRIND_MAKE_MEM_UNDEFINED (pad, pad_len);
16
17
  ret = _pkcs1_sec_decrypt (msg_len, msg, pad_len, pad);
18
19
  VALGRIND_MAKE_MEM_DEFINED (msg, msg_len);
20
  VALGRIND_MAKE_MEM_DEFINED (pad, pad_len);
21
  VALGRIND_MAKE_MEM_DEFINED (&ret, sizeof (ret));
22
23
  return ret;
24
}
25
#else
26
#define pkcs1_decrypt_for_test _pkcs1_sec_decrypt
27
#endif
28
29
void
30
test_main(void)
31
{
32
  uint8_t pad[128];
33
  uint8_t buffer[] =
34
    "\x00\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
35
    "\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20"
36
    "\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30"
37
    "\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40"
38
    "\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50"
39
    "\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60"
40
    "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70"
41
    "\x00\x53\x49\x47\x4e\x45\x44\x20\x4d\x45\x53\x53\x41\x47\x45\x2e";
42
  uint8_t message[15];
43
44
  memcpy(pad, buffer, 128);
45
46
  memset (message, 'A', 15);
47
  ASSERT (pkcs1_decrypt_for_test(15, message, 128, pad) == 1);
48
  ASSERT (memcmp (message, "SIGNED MESSAGE.", 15) == 0);
49
50
  /* break format byte 1 */
51
  memcpy(pad, buffer, 128);
52
  pad[0] = 1;
53
  memset (message, 'B', 15);
54
  ASSERT (pkcs1_decrypt_for_test(15, message, 128, pad) == 0);
55
  ASSERT (memcmp (message, "BBBBBBBBBBBBBBB", 15) == 0);
56
57
  /* break format byte 2 */
58
  memcpy(pad, buffer, 128);
59
  pad[1] = 1;
60
  memset (message, 'C', 15);
61
  ASSERT (pkcs1_decrypt_for_test(15, message, 128, pad) == 0);
62
  ASSERT (memcmp (message, "CCCCCCCCCCCCCCC", 15) == 0);
63
64
  /* break padding */
65
  memcpy(pad, buffer, 128);
66
  pad[24] = 0;
67
  memset (message, 'D', 15);
68
  ASSERT (pkcs1_decrypt_for_test(15, message, 128, pad) == 0);
69
  ASSERT (memcmp (message, "DDDDDDDDDDDDDDD", 15) == 0);
70
71
  /* break terminator */
72
  memcpy(pad, buffer, 128);
73
  pad[112] = 1;
74
  memset (message, 'E', 15);
75
  ASSERT (pkcs1_decrypt_for_test(15, message, 128, pad) == 0);
76
  ASSERT (memcmp (message, "EEEEEEEEEEEEEEE", 15) == 0);
77
}
(-)nettle-3.4/testsuite/rsa-compute-root-test.c (+234 lines)
Line 0 Link Here
1
#include "testutils.h"
2
3
#include <assert.h>
4
#include <errno.h>
5
#include <limits.h>
6
#include <sys/time.h>
7
8
#include "rsa.h"
9
10
#define KEY_COUNT 20
11
#define COUNT 100
12
13
static void
14
random_fn (void *ctx, size_t n, uint8_t *dst)
15
{
16
  gmp_randstate_t *rands = (gmp_randstate_t *)ctx;
17
  mpz_t r;
18
19
  mpz_init (r);
20
  mpz_urandomb (r, *rands, n*8);
21
  nettle_mpz_get_str_256 (n, dst, r);
22
  mpz_clear (r);
23
}
24
25
static void
26
test_one (gmp_randstate_t *rands, struct rsa_public_key *pub,
27
          struct rsa_private_key *key, mpz_t plaintext)
28
{
29
  mpz_t ciphertext;
30
  mpz_t decrypted;
31
32
  mpz_init (ciphertext);
33
  mpz_init (decrypted);
34
35
  mpz_powm (ciphertext, plaintext, pub->e, pub->n);
36
  rsa_compute_root_tr (pub, key, rands, random_fn, decrypted, ciphertext);
37
  if (mpz_cmp (plaintext, decrypted)) {
38
    fprintf (stderr, "rsa_compute_root_tr failed\n");
39
40
    fprintf(stderr, "Public key: size=%lu\n n:", pub->size);
41
    mpz_out_str (stderr, 10, pub->n);
42
    fprintf(stderr, "\n e:");
43
    mpz_out_str (stderr, 10, pub->e);
44
    fprintf(stderr, "\nPrivate key: size=%lu\n p:", key->size);
45
    mpz_out_str (stderr, 10, key->p);
46
    fprintf(stderr, "\n q:");
47
    mpz_out_str (stderr, 10, key->q);
48
    fprintf(stderr, "\n a:");
49
    mpz_out_str (stderr, 10, key->a);
50
    fprintf(stderr, "\n b:");
51
    mpz_out_str (stderr, 10, key->b);
52
    fprintf(stderr, "\n c:");
53
    mpz_out_str (stderr, 10, key->c);
54
    fprintf(stderr, "\n d:");
55
    mpz_out_str (stderr, 10, key->d);
56
    fprintf(stderr, "\n");
57
58
    fprintf (stderr, "plaintext(%lu) = ", mpz_sizeinbase (plaintext, 2));
59
    mpz_out_str (stderr, 10, plaintext);
60
    fprintf (stderr, "\n");
61
    fprintf (stderr, "ciphertext(%lu) = ", mpz_sizeinbase (ciphertext, 2));
62
    mpz_out_str (stderr, 10, ciphertext);
63
    fprintf (stderr, "\n");
64
    fprintf (stderr, "decrypted(%lu) = ", mpz_sizeinbase (decrypted, 2));
65
    mpz_out_str (stderr, 10, decrypted);
66
    fprintf (stderr, "\n");
67
    abort();
68
  }
69
70
  mpz_clear (ciphertext);
71
  mpz_clear (decrypted);
72
}
73
74
#if !NETTLE_USE_MINI_GMP
75
/* We want to generate keypairs that are not "standard" but have more size
76
 * variance between q and p.
77
 * Function is otherwise the same as standard rsa_generate_keypair()
78
 */
79
static void
80
generate_keypair (gmp_randstate_t rands,
81
                  struct rsa_public_key *pub, struct rsa_private_key *key)
82
{
83
  unsigned long int psize;
84
  unsigned long int qsize;
85
  mpz_t p1;
86
  mpz_t q1;
87
  mpz_t phi;
88
  mpz_t tmp;
89
90
  mpz_init (p1);
91
  mpz_init (q1);
92
  mpz_init (phi);
93
  mpz_init (tmp);
94
95
  psize = 100 + gmp_urandomm_ui (rands, 400);
96
  qsize = 100 + gmp_urandomm_ui (rands, 400);
97
98
  mpz_set_ui (pub->e, 65537);
99
100
  for (;;)
101
    {
102
      for (;;)
103
        {
104
          mpz_rrandomb (key->p, rands, psize);
105
          mpz_nextprime (key->p, key->p);
106
          mpz_sub_ui (p1, key->p, 1);
107
          mpz_gcd (tmp, pub->e, p1);
108
          if (mpz_cmp_ui (tmp, 1) == 0)
109
            break;
110
        }
111
112
      for (;;)
113
        {
114
          mpz_rrandomb (key->q, rands, qsize);
115
          mpz_nextprime (key->q, key->q);
116
          mpz_sub_ui (q1, key->q, 1);
117
          mpz_gcd (tmp, pub->e, q1);
118
          if (mpz_cmp_ui (tmp, 1) == 0)
119
            break;
120
        }
121
122
      if (mpz_invert (key->c, key->q, key->p))
123
        break;
124
    }
125
126
  mpz_mul(phi, p1, q1);
127
  assert (mpz_invert(key->d, pub->e, phi));
128
129
  mpz_fdiv_r (key->a, key->d, p1);
130
  mpz_fdiv_r (key->b, key->d, q1);
131
132
  mpz_mul (pub->n, key->p, key->q);
133
134
  pub->size = key->size = mpz_size(pub->n) * sizeof(mp_limb_t);
135
136
  mpz_clear (tmp);
137
  mpz_clear (phi);
138
  mpz_clear (q1);
139
  mpz_clear (p1);
140
}
141
#endif
142
143
#if !NETTLE_USE_MINI_GMP
144
static void
145
get_random_seed(mpz_t seed)
146
{
147
  struct timeval tv;
148
  FILE *f;
149
  f = fopen ("/dev/urandom", "rb");
150
  if (f)
151
    {
152
      uint8_t buf[8];
153
      size_t res;
154
155
      setbuf (f, NULL);
156
      res = fread (&buf, sizeof(buf), 1, f);
157
      fclose(f);
158
      if (res == 1)
159
	{
160
	  nettle_mpz_set_str_256_u (seed, sizeof(buf), buf);
161
	  return;
162
	}
163
      fprintf (stderr, "Read of /dev/urandom failed: %s\n",
164
	       strerror (errno));
165
    }
166
  gettimeofday(&tv, NULL);
167
  mpz_set_ui (seed, tv.tv_sec);
168
  mpz_mul_ui (seed, seed, 1000000UL);
169
  mpz_add_ui (seed, seed, tv.tv_usec);
170
}
171
#endif /* !NETTLE_USE_MINI_GMP */
172
173
void
174
test_main (void)
175
{
176
  const char *nettle_test_seed;
177
  gmp_randstate_t rands;
178
  struct rsa_public_key pub;
179
  struct rsa_private_key key;
180
  mpz_t plaintext;
181
  unsigned i, j;
182
183
  rsa_private_key_init(&key);
184
  rsa_public_key_init(&pub);
185
  mpz_init (plaintext);
186
187
  gmp_randinit_default (rands);
188
189
#if !NETTLE_USE_MINI_GMP
190
  nettle_test_seed = getenv ("NETTLE_TEST_SEED");
191
  if (nettle_test_seed && *nettle_test_seed)
192
    {
193
      mpz_t seed;
194
      mpz_init (seed);
195
      if (mpz_set_str (seed, nettle_test_seed, 0) < 0
196
	  || mpz_sgn (seed) < 0)
197
	die ("Invalid NETTLE_TEST_SEED: %s\n",
198
	     nettle_test_seed);
199
      if (mpz_sgn (seed) == 0)
200
	get_random_seed (seed);
201
      fprintf (stderr, "Using NETTLE_TEST_SEED=");
202
      mpz_out_str (stderr, 10, seed);
203
      fprintf (stderr, "\n");
204
205
      gmp_randseed (rands, seed);
206
      mpz_clear (seed);
207
    }
208
#endif
209
210
  for (j = 0; j < KEY_COUNT; j++)
211
    {
212
#if !NETTLE_USE_MINI_GMP
213
      generate_keypair(rands, &pub, &key);
214
#else
215
      rsa_generate_keypair(&pub, &key, &rands, random_fn, NULL, NULL, 512, 16);
216
#endif /* !NETTLE_USE_MINI_GMP */
217
218
      for (i = 0; i < COUNT; i++)
219
	{
220
	  mpz_urandomb(plaintext, rands, mpz_sizeinbase(pub.n, 2) - 1);
221
	  test_one(&rands, &pub, &key, plaintext);
222
	}
223
      for (i = 0; i < COUNT; i++)
224
	{
225
	  mpz_rrandomb(plaintext, rands, mpz_sizeinbase(pub.n, 2) - 1);
226
	  test_one(&rands, &pub, &key, plaintext);
227
	}
228
    }
229
  mpz_clear (plaintext);
230
  rsa_public_key_clear (&pub);
231
  rsa_private_key_clear (&key);
232
233
  gmp_randclear (rands);
234
}
(-)nettle-3.4/testsuite/rsa-encrypt-test.c (-4 / +30 lines)
Lines 30-35 Link Here
30
30
31
  if (verbose)
31
  if (verbose)
32
    fprintf(stderr, "msg: `%s', length = %d\n", msg, (int) msg_length);
32
    fprintf(stderr, "msg: `%s', length = %d\n", msg, (int) msg_length);
33
34
  ASSERT(msg_length <= key.size);
33
  
35
  
34
  ASSERT(rsa_encrypt(&pub,
36
  ASSERT(rsa_encrypt(&pub,
35
		     &lfib, (nettle_random_func *) knuth_lfib_random,
37
		     &lfib, (nettle_random_func *) knuth_lfib_random,
Lines 42-48 Link Here
42
      mpz_out_str(stderr, 10, gibberish);
44
      mpz_out_str(stderr, 10, gibberish);
43
    }
45
    }
44
  
46
  
45
  decrypted = xalloc(msg_length + 1);
47
  decrypted = xalloc(key.size + 1);
46
48
47
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
49
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
48
  after = decrypted[msg_length];
50
  after = decrypted[msg_length];
Lines 56-69 Link Here
56
  ASSERT(MEMEQ(msg_length, msg, decrypted));
58
  ASSERT(MEMEQ(msg_length, msg, decrypted));
57
  ASSERT(decrypted[msg_length] == after);
59
  ASSERT(decrypted[msg_length] == after);
58
60
59
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
61
  knuth_lfib_random (&lfib, key.size + 1, decrypted);
60
  after = decrypted[msg_length];
62
  after = decrypted[key.size];
61
63
62
  decrypted_length = key.size;
64
  decrypted_length = key.size;
63
  ASSERT(rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
65
  ASSERT(rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
64
  ASSERT(decrypted_length == msg_length);
66
  ASSERT(decrypted_length == msg_length);
65
  ASSERT(MEMEQ(msg_length, msg, decrypted));
67
  ASSERT(MEMEQ(msg_length, msg, decrypted));
66
  ASSERT(decrypted[msg_length] == after);
68
  ASSERT(decrypted[key.size] == after);
67
  
69
  
68
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
70
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
69
  after = decrypted[msg_length];
71
  after = decrypted[msg_length];
Lines 76-81 Link Here
76
  ASSERT(MEMEQ(msg_length, msg, decrypted));
78
  ASSERT(MEMEQ(msg_length, msg, decrypted));
77
  ASSERT(decrypted[msg_length] == after);
79
  ASSERT(decrypted[msg_length] == after);
78
80
81
  /* test side channel resistant variant */
82
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
83
  after = decrypted[msg_length];
84
  decrypted_length = msg_length;
85
86
  ASSERT(rsa_sec_decrypt(&pub, &key,
87
                         &lfib, (nettle_random_func *) knuth_lfib_random,
88
                         decrypted_length, decrypted, gibberish));
89
  ASSERT(MEMEQ(msg_length, msg, decrypted));
90
  ASSERT(decrypted[msg_length] == after);
91
92
  /* test invalid length to rsa_sec_decrypt */
93
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
94
  decrypted_length = msg_length - 1;
95
  after = decrypted[decrypted_length] = 'X';
96
  decrypted[0] = 'A';
97
98
  ASSERT(!rsa_sec_decrypt(&pub, &key,
99
                          &lfib, (nettle_random_func *) knuth_lfib_random,
100
                          decrypted_length, decrypted, gibberish));
101
  ASSERT(decrypted[decrypted_length] == after);
102
  ASSERT(decrypted[0] == 'A');
103
104
79
  /* Test invalid key. */
105
  /* Test invalid key. */
80
  mpz_add_ui (key.q, key.q, 2);
106
  mpz_add_ui (key.q, key.q, 2);
81
  decrypted_length = key.size;
107
  decrypted_length = key.size;
(-)nettle-3.4/testsuite/rsa-sec-decrypt-test.c (+118 lines)
Line 0 Link Here
1
#include "testutils.h"
2
3
#include "rsa.h"
4
#include "knuth-lfib.h"
5
6
#if HAVE_VALGRIND_MEMCHECK_H
7
# include <valgrind/memcheck.h>
8
9
#define MARK_MPZ_LIMBS_UNDEFINED(parm) \
10
  VALGRIND_MAKE_MEM_UNDEFINED (mpz_limbs_read (parm), \
11
                               mpz_size (parm) * sizeof (mp_limb_t))
12
#define MARK_MPZ_LIMBS_DEFINED(parm) \
13
  VALGRIND_MAKE_MEM_DEFINED (mpz_limbs_read (parm), \
14
                               mpz_size (parm) * sizeof (mp_limb_t))
15
static int
16
rsa_decrypt_for_test(const struct rsa_public_key *pub,
17
                     const struct rsa_private_key *key,
18
                     void *random_ctx, nettle_random_func *random,
19
                     size_t length, uint8_t *message,
20
                     const mpz_t gibberish)
21
{
22
  int ret;
23
  /* Makes valgrind trigger on any branches depending on the input
24
     data. Except that (i) we have to allow rsa_sec_compute_root_tr to
25
     check that p and q are odd, (ii) mpn_sec_div_r may leak
26
     information about the most significant bits of p and q, due to
27
     normalization check and table lookup in invert_limb, and (iii)
28
     mpn_sec_powm may leak information about the least significant
29
     bits of p and q, due to table lookup in binvert_limb. */
30
  VALGRIND_MAKE_MEM_UNDEFINED (message, length);
31
  MARK_MPZ_LIMBS_UNDEFINED(gibberish);
32
  MARK_MPZ_LIMBS_UNDEFINED(key->a);
33
  MARK_MPZ_LIMBS_UNDEFINED(key->b);
34
  MARK_MPZ_LIMBS_UNDEFINED(key->c);
35
  VALGRIND_MAKE_MEM_UNDEFINED(mpz_limbs_read (key->p) + 1,
36
			      (mpz_size (key->p) - 3) * sizeof(mp_limb_t));
37
  VALGRIND_MAKE_MEM_UNDEFINED(mpz_limbs_read (key->q) + 1,
38
			      (mpz_size (key->q) - 3) * sizeof(mp_limb_t));
39
40
  ret = rsa_sec_decrypt (pub, key, random_ctx, random, length, message, gibberish);
41
42
  VALGRIND_MAKE_MEM_DEFINED (message, length);
43
  VALGRIND_MAKE_MEM_DEFINED (&ret, sizeof(ret));
44
  MARK_MPZ_LIMBS_DEFINED(gibberish);
45
  MARK_MPZ_LIMBS_DEFINED(key->a);
46
  MARK_MPZ_LIMBS_DEFINED(key->b);
47
  MARK_MPZ_LIMBS_DEFINED(key->c);
48
  MARK_MPZ_LIMBS_DEFINED(key->p);
49
  MARK_MPZ_LIMBS_DEFINED(key->q);
50
51
  return ret;
52
}
53
#else
54
#define rsa_decrypt_for_test rsa_sec_decrypt
55
#endif
56
57
#define PAYLOAD_SIZE 50
58
void
59
test_main(void)
60
{
61
  struct rsa_public_key pub;
62
  struct rsa_private_key key;
63
  struct knuth_lfib_ctx random_ctx;
64
65
  uint8_t plaintext[PAYLOAD_SIZE];
66
  uint8_t decrypted[PAYLOAD_SIZE];
67
  uint8_t verifybad[PAYLOAD_SIZE];
68
  unsigned n_size = 1024;
69
  mpz_t gibberish;
70
  mpz_t garbage;
71
72
  rsa_private_key_init(&key);
73
  rsa_public_key_init(&pub);
74
  mpz_init(gibberish);
75
  mpz_init(garbage);
76
77
  knuth_lfib_init (&random_ctx, 19);
78
79
  memset(verifybad, 'A', PAYLOAD_SIZE);
80
81
  for (size_t size = 1; size < 51; size++)
82
    {
83
      ASSERT (rsa_generate_keypair(&pub, &key, &random_ctx,
84
			           (nettle_random_func *) knuth_lfib_random,
85
			           NULL, NULL, n_size, 17));
86
87
      /* the next key will be 19 bits larger */
88
      n_size += 19;
89
90
      knuth_lfib_random (&random_ctx, PAYLOAD_SIZE, plaintext);
91
      ASSERT(rsa_encrypt(&pub, &random_ctx,
92
                         (nettle_random_func *) knuth_lfib_random,
93
                         PAYLOAD_SIZE, plaintext, gibberish));
94
95
      /* good decryption */
96
      ASSERT (rsa_decrypt_for_test (&pub, &key, &random_ctx,
97
                                    (nettle_random_func *) knuth_lfib_random,
98
                                    PAYLOAD_SIZE, decrypted, gibberish) == 1);
99
      ASSERT (MEMEQ (PAYLOAD_SIZE, plaintext, decrypted));
100
101
      /* bad one */
102
      memcpy(decrypted, verifybad, PAYLOAD_SIZE);
103
      nettle_mpz_random_size(garbage, &random_ctx,
104
                             (nettle_random_func *) knuth_lfib_random,
105
                             mpz_sizeinbase(gibberish, 2));
106
107
      ASSERT (rsa_decrypt_for_test (&pub, &key, &random_ctx,
108
                                    (nettle_random_func *) knuth_lfib_random,
109
                                    PAYLOAD_SIZE, decrypted, garbage) == 0);
110
      ASSERT (MEMEQ (PAYLOAD_SIZE, verifybad, decrypted));
111
    }
112
113
  rsa_private_key_clear(&key);
114
  rsa_public_key_clear(&pub);
115
  mpz_clear(gibberish);
116
  mpz_clear(garbage);
117
}
118
(-)nettle-3.4/testsuite/.test-rules.make (-3 / +15 lines)
Lines 25-30 Link Here
25
chacha-test$(EXEEXT): chacha-test.$(OBJEXT)
25
chacha-test$(EXEEXT): chacha-test.$(OBJEXT)
26
	$(LINK) chacha-test.$(OBJEXT) $(TEST_OBJS) -o chacha-test$(EXEEXT)
26
	$(LINK) chacha-test.$(OBJEXT) $(TEST_OBJS) -o chacha-test$(EXEEXT)
27
27
28
cnd-memcpy-test$(EXEEXT): cnd-memcpy-test.$(OBJEXT)
29
	$(LINK) cnd-memcpy-test.$(OBJEXT) $(TEST_OBJS) -o cnd-memcpy-test$(EXEEXT)
30
28
des-test$(EXEEXT): des-test.$(OBJEXT)
31
des-test$(EXEEXT): des-test.$(OBJEXT)
29
	$(LINK) des-test.$(OBJEXT) $(TEST_OBJS) -o des-test$(EXEEXT)
32
	$(LINK) des-test.$(OBJEXT) $(TEST_OBJS) -o des-test$(EXEEXT)
30
33
Lines 163-171 Link Here
163
pbkdf2-test$(EXEEXT): pbkdf2-test.$(OBJEXT)
166
pbkdf2-test$(EXEEXT): pbkdf2-test.$(OBJEXT)
164
	$(LINK) pbkdf2-test.$(OBJEXT) $(TEST_OBJS) -o pbkdf2-test$(EXEEXT)
167
	$(LINK) pbkdf2-test.$(OBJEXT) $(TEST_OBJS) -o pbkdf2-test$(EXEEXT)
165
168
166
pss-mgf1-test$(EXEEXT): pss-mgf1-test.$(OBJEXT)
167
	$(LINK) pss-mgf1-test.$(OBJEXT) $(TEST_OBJS) -o pss-mgf1-test$(EXEEXT)
168
169
sexp-test$(EXEEXT): sexp-test.$(OBJEXT)
169
sexp-test$(EXEEXT): sexp-test.$(OBJEXT)
170
	$(LINK) sexp-test.$(OBJEXT) $(TEST_OBJS) -o sexp-test$(EXEEXT)
170
	$(LINK) sexp-test.$(OBJEXT) $(TEST_OBJS) -o sexp-test$(EXEEXT)
171
171
Lines 187-198 Link Here
187
pkcs1-test$(EXEEXT): pkcs1-test.$(OBJEXT)
187
pkcs1-test$(EXEEXT): pkcs1-test.$(OBJEXT)
188
	$(LINK) pkcs1-test.$(OBJEXT) $(TEST_OBJS) -o pkcs1-test$(EXEEXT)
188
	$(LINK) pkcs1-test.$(OBJEXT) $(TEST_OBJS) -o pkcs1-test$(EXEEXT)
189
189
190
pkcs1-sec-decrypt-test$(EXEEXT): pkcs1-sec-decrypt-test.$(OBJEXT)
191
	$(LINK) pkcs1-sec-decrypt-test.$(OBJEXT) $(TEST_OBJS) -o pkcs1-sec-decrypt-test$(EXEEXT)
192
190
pss-test$(EXEEXT): pss-test.$(OBJEXT)
193
pss-test$(EXEEXT): pss-test.$(OBJEXT)
191
	$(LINK) pss-test.$(OBJEXT) $(TEST_OBJS) -o pss-test$(EXEEXT)
194
	$(LINK) pss-test.$(OBJEXT) $(TEST_OBJS) -o pss-test$(EXEEXT)
192
195
193
rsa-sign-tr-test$(EXEEXT): rsa-sign-tr-test.$(OBJEXT)
196
rsa-sign-tr-test$(EXEEXT): rsa-sign-tr-test.$(OBJEXT)
194
	$(LINK) rsa-sign-tr-test.$(OBJEXT) $(TEST_OBJS) -o rsa-sign-tr-test$(EXEEXT)
197
	$(LINK) rsa-sign-tr-test.$(OBJEXT) $(TEST_OBJS) -o rsa-sign-tr-test$(EXEEXT)
195
198
199
pss-mgf1-test$(EXEEXT): pss-mgf1-test.$(OBJEXT)
200
	$(LINK) pss-mgf1-test.$(OBJEXT) $(TEST_OBJS) -o pss-mgf1-test$(EXEEXT)
201
196
rsa-pss-sign-tr-test$(EXEEXT): rsa-pss-sign-tr-test.$(OBJEXT)
202
rsa-pss-sign-tr-test$(EXEEXT): rsa-pss-sign-tr-test.$(OBJEXT)
197
	$(LINK) rsa-pss-sign-tr-test.$(OBJEXT) $(TEST_OBJS) -o rsa-pss-sign-tr-test$(EXEEXT)
203
	$(LINK) rsa-pss-sign-tr-test.$(OBJEXT) $(TEST_OBJS) -o rsa-pss-sign-tr-test$(EXEEXT)
198
204
Lines 205-210 Link Here
205
rsa-keygen-test$(EXEEXT): rsa-keygen-test.$(OBJEXT)
211
rsa-keygen-test$(EXEEXT): rsa-keygen-test.$(OBJEXT)
206
	$(LINK) rsa-keygen-test.$(OBJEXT) $(TEST_OBJS) -o rsa-keygen-test$(EXEEXT)
212
	$(LINK) rsa-keygen-test.$(OBJEXT) $(TEST_OBJS) -o rsa-keygen-test$(EXEEXT)
207
213
214
rsa-sec-decrypt-test$(EXEEXT): rsa-sec-decrypt-test.$(OBJEXT)
215
	$(LINK) rsa-sec-decrypt-test.$(OBJEXT) $(TEST_OBJS) -o rsa-sec-decrypt-test$(EXEEXT)
216
217
rsa-compute-root-test$(EXEEXT): rsa-compute-root-test.$(OBJEXT)
218
	$(LINK) rsa-compute-root-test.$(OBJEXT) $(TEST_OBJS) -o rsa-compute-root-test$(EXEEXT)
219
208
dsa-test$(EXEEXT): dsa-test.$(OBJEXT)
220
dsa-test$(EXEEXT): dsa-test.$(OBJEXT)
209
	$(LINK) dsa-test.$(OBJEXT) $(TEST_OBJS) -o dsa-test$(EXEEXT)
221
	$(LINK) dsa-test.$(OBJEXT) $(TEST_OBJS) -o dsa-test$(EXEEXT)
210
222
(-)nettle-3.4/testsuite/testutils.c (-1 / +1 lines)
Lines 818-824 Link Here
818
  uint8_t *buf = xalloc (bytes);
818
  uint8_t *buf = xalloc (bytes);
819
819
820
  knuth_lfib_random (ctx, bytes, buf);
820
  knuth_lfib_random (ctx, bytes, buf);
821
  buf[bytes-1] &= 0xff >> (8*bytes - bits);
821
  buf[0] &= 0xff >> (8*bytes - bits);
822
  nettle_mpz_set_str_256_u (r, bytes, buf);
822
  nettle_mpz_set_str_256_u (r, bytes, buf);
823
  free (buf);
823
  free (buf);
824
}
824
}
(-)nettle-3.4/tools/pkcs1-conv.c (-17 / +9 lines)
Lines 563-590 Link Here
563
	    {
563
	    {
564
	    case 10:
564
	    case 10:
565
	      if (memcmp(marker, "PUBLIC KEY", 10) == 0)
565
	      if (memcmp(marker, "PUBLIC KEY", 10) == 0)
566
		{
566
		type = GENERAL_PUBLIC_KEY;
567
		  type = GENERAL_PUBLIC_KEY;
567
	      break;
568
		  break;
568
569
		}
570
	    case 14:
569
	    case 14:
571
	      if (memcmp(marker, "RSA PUBLIC KEY", 14) == 0)
570
	      if (memcmp(marker, "RSA PUBLIC KEY", 14) == 0)
572
		{
571
		type = RSA_PUBLIC_KEY;
573
		  type = RSA_PUBLIC_KEY;
572
	      break;
574
		  break;
575
		}
576
573
577
	    case 15:
574
	    case 15:
578
	      if (memcmp(marker, "RSA PRIVATE KEY", 15) == 0)
575
	      if (memcmp(marker, "RSA PRIVATE KEY", 15) == 0)
579
		{
576
		type = RSA_PRIVATE_KEY;
580
		  type = RSA_PRIVATE_KEY;
577
	      else if (memcmp(marker, "DSA PRIVATE KEY", 15) == 0)
581
		  break;
578
		type = DSA_PRIVATE_KEY;
582
		}
579
	      break;
583
	      if (memcmp(marker, "DSA PRIVATE KEY", 15) == 0)
584
		{
585
		  type = DSA_PRIVATE_KEY;
586
		  break;
587
		}
588
	    }
580
	    }
589
	  
581
	  
590
	  if (!type)
582
	  if (!type)

Return to bug 1118086