rsa-sec-compute-root.c 5.71 KB
Newer Older
1 2
/* rsa-sec-compute-root.c

3
   Side-channel silent RSA root computation.
4 5

   Copyright (C) 2018 Niels Möller
6
   Copyright (C) 2018 Red Hat, Inc
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>

#include "rsa.h"
#include "rsa-internal.h"
#include "gmp-glue.h"

#if !NETTLE_USE_MINI_GMP
#define MAX(a, b) ((a) > (b) ? (a) : (b))

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/* Like mpn_sec_mul_itch, monotonously increasing in operand sizes. */
static mp_size_t
sec_mul_itch (mp_size_t an, mp_size_t bn)
{
  if (an >= bn)
    return mpn_sec_mul_itch (an, bn);
  else
    return mpn_sec_mul_itch (bn, an);
}

/* Writes an + bn limbs to the rp area */
static void
sec_mul (mp_limb_t *rp,
	 const mp_limb_t *ap, mp_size_t an,
	 const mp_limb_t *bp, mp_size_t bn, mp_limb_t *scratch)
{
  if (an >= bn)
    mpn_sec_mul (rp, ap, an, bp, bn, scratch);
  else
    mpn_sec_mul (rp, bp, bn, ap, an, scratch);
}

static mp_size_t
sec_mod_mul_itch (mp_size_t an, mp_size_t bn, mp_size_t mn)
{
  mp_size_t mul_itch = sec_mul_itch (an, bn);
  mp_size_t mod_itch = mpn_sec_div_r_itch (an + bn, mn);
  return MAX(mul_itch, mod_itch);
}

/* Sets r <-- a b % m. Needs space for an + bn limbs at rp. It is
   required than an + bn >= mn. */
static void
sec_mod_mul (mp_limb_t *rp,
	     const mp_limb_t *ap, mp_size_t an,
	     const mp_limb_t *bp, mp_size_t bn,
	     const mp_limb_t *mp, mp_size_t mn,
	     mp_limb_t *scratch)
{
  assert (an + bn >= mn);
  sec_mul (rp, ap, an, bp, bn, scratch);
  mpn_sec_div_r (rp, an + bn, mp, mn, scratch);
}

static mp_size_t
sec_powm_itch (mp_size_t bn, mp_size_t en, mp_size_t mn)
{
  mp_size_t mod_itch = bn + mpn_sec_div_r_itch (bn, mn);
  mp_size_t pow_itch = mn + mpn_sec_powm_itch (mn, en * GMP_NUMB_BITS, mn);
  return MAX (mod_itch, pow_itch);
}

/* Sets r <-- b ^ e % m. Performs an initial reduction b mod m, and
   requires bn >= mn. */
static void
sec_powm (mp_limb_t *rp,
	  const mp_limb_t *bp, mp_size_t bn,
	  const mp_limb_t *ep, mp_size_t en,
	  const mp_limb_t *mp, mp_size_t mn, mp_limb_t *scratch)
{
  assert (bn >= mn);
  assert (en <= mn);
  mpn_copyi (scratch, bp, bn);
  mpn_sec_div_r (scratch, bn, mp, mn, scratch + bn);
  mpn_sec_powm (rp, scratch, mn, ep, en * GMP_NUMB_BITS, mp, mn,
		scratch + mn);
}

116 117 118 119 120 121
mp_size_t
_rsa_sec_compute_root_itch (const struct rsa_private_key *key)
{
  mp_size_t nn = NETTLE_OCTET_SIZE_TO_LIMB_SIZE (key->size);
  mp_size_t pn = mpz_size (key->p);
  mp_size_t qn = mpz_size (key->q);
122 123
  mp_size_t an = mpz_size (key->a);
  mp_size_t bn = mpz_size (key->b);
124 125
  mp_size_t cn = mpz_size (key->c);

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  mp_size_t powm_p_itch = sec_powm_itch (nn, an, pn);
  mp_size_t powm_q_itch = sec_powm_itch (nn, bn, qn);
  mp_size_t mod_mul_itch = cn + MAX(pn, qn) 
    + sec_mod_mul_itch (MAX(pn, qn), cn, pn);

  mp_size_t mul_itch = sec_mul_itch (qn, pn);
  mp_size_t add_1_itch = mpn_sec_add_1_itch (nn - qn);

  /* pn + qn for the product q * r_mod_p' */
  mp_size_t itch = pn + qn + MAX (mul_itch, add_1_itch);

  itch = MAX (itch, powm_p_itch);
  itch = MAX (itch, powm_q_itch);
  itch = MAX (itch, mod_mul_itch);

  /* pn + qn for the r_mod_p and r_mod_q temporaries. */
  return pn + qn + itch;
143 144 145 146
}

void
_rsa_sec_compute_root (const struct rsa_private_key *key,
147 148
		       mp_limb_t *rp, const mp_limb_t *mp,
		       mp_limb_t *scratch)
149 150 151 152 153 154 155 156 157 158
{
  mp_size_t nn = NETTLE_OCTET_SIZE_TO_LIMB_SIZE (key->size);

  /* The common case is pn = qn. This function would be simpler if we
   * could require that pn >= qn. */
  const mp_limb_t *pp = mpz_limbs_read (key->p);
  const mp_limb_t *qp = mpz_limbs_read (key->q);

  mp_size_t pn = mpz_size (key->p);
  mp_size_t qn = mpz_size (key->q);
159 160 161
  mp_size_t an = mpz_size (key->a);
  mp_size_t bn = mpz_size (key->b);
  mp_size_t cn = mpz_size (key->c);
162

163 164 165
  mp_limb_t *r_mod_p = scratch;
  mp_limb_t *r_mod_q = scratch + pn;
  mp_limb_t *scratch_out = r_mod_q + qn;
166 167 168 169
  mp_limb_t cy;

  assert (pn <= nn);
  assert (qn <= nn);
170 171
  assert (an <= pn);
  assert (bn <= qn);
172 173 174
  assert (cn <= pn);

  /* Compute r_mod_p = m^d % p = (m%p)^a % p */
175 176 177
  sec_powm (r_mod_p, mp, nn, mpz_limbs_read (key->a), an, pp, pn, scratch_out);
  /* Compute r_mod_q = m^d % q = (m%q)^b % q */
  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 181 182 183 184 185 186
  sec_mod_mul (scratch_out, r_mod_p, pn, mpz_limbs_read (key->c), cn, pp, pn,
	       scratch_out + cn + pn);
  mpn_copyi (r_mod_p, scratch_out, pn);

  sec_mod_mul (scratch_out, r_mod_q, qn, mpz_limbs_read (key->c), cn, pp, pn,
	       scratch_out + cn + qn);
  cy = mpn_sub_n (r_mod_p, r_mod_p, scratch_out, pn);
187 188 189
  cnd_add_n (cy, r_mod_p, pp, pn);

  /* 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 193
  cy = mpn_add_n (rp, scratch_out, r_mod_q, qn);
  mpn_sec_add_1 (rp + qn, scratch_out + qn, nn - qn, cy, scratch_out + pn + qn);
194 195
}
#endif