ecc-ecdsa-verify.c 4.09 KB
Newer Older
1 2
/* ecc-ecdsa-verify.c

3
   Copyright (C) 2013, 2014 Niels Möller
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

   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/.
*/
31

32
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

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

#include <assert.h>
#include <stdlib.h>

#include "ecdsa.h"
#include "ecc-internal.h"

/* Low-level ECDSA verify */

static int
ecdsa_in_range (const struct ecc_curve *ecc, const mp_limb_t *xp)
{
49
  return !mpn_zero_p (xp, ecc->p.size)
Niels Möller's avatar
Niels Möller committed
50
    && mpn_cmp (xp, ecc->q.m, ecc->p.size) < 0;
51 52 53 54 55
}

mp_size_t
ecc_ecdsa_verify_itch (const struct ecc_curve *ecc)
{
56
  /* Largest storage need is for the ecc->mul call. */
Niels Möller's avatar
Niels Möller committed
57
  return 5*ecc->p.size + ecc->mul_itch;
58 59 60 61 62 63
}

/* FIXME: Use faster primitives, not requiring side-channel silence. */
int
ecc_ecdsa_verify (const struct ecc_curve *ecc,
		  const mp_limb_t *pp, /* Public key */
Niels Möller's avatar
Niels Möller committed
64
		  size_t length, const uint8_t *digest,
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		  const mp_limb_t *rp, const mp_limb_t *sp,
		  mp_limb_t *scratch)
{
  /* Procedure, according to RFC 6090, "KT-I". q denotes the group
     order.

     1. Check 0 < r, s < q.

     2. s' <-- s^{-1}  (mod q)

     3. u1  <-- h * s' (mod q)

     4. u2  <-- r * s' (mod q)

     5. R = u1 G + u2 Y

     6. Signature is valid if R_x = r (mod q).
  */

#define P2 scratch
85
#define u1 (scratch + 3*ecc->p.size)
Niels Möller's avatar
Niels Möller committed
86
#define u2 (scratch + 4*ecc->p.size)
87 88 89 90

#define P1 (scratch + 4*ecc->p.size)
#define sinv (scratch)
#define hp (scratch + ecc->p.size)
91 92 93 94 95 96 97 98

  if (! (ecdsa_in_range (ecc, rp)
	 && ecdsa_in_range (ecc, sp)))
    return 0;

  /* FIXME: Micro optimizations: Either simultaneous multiplication.
     Or convert to projective coordinates (can be done without
     division, I think), and write an ecc_add_ppp. */
99 100 101 102 103

  /* Compute sinv */
  ecc->q.invert (&ecc->q, sinv, sp, sinv + 2*ecc->p.size);

  /* u1 = h / s, P1 = u1 * G */
104
  ecc_hash (&ecc->q, hp, length, digest);
105
  ecc_mod_mul (&ecc->q, u1, hp, sinv);
106 107

  /* u2 = r / s, P2 = u2 * Y */
108
  ecc_mod_mul (&ecc->q, u2, rp, sinv);
109

Niels Möller's avatar
Niels Möller committed
110 111
   /* Total storage: 5*ecc->p.size + ecc->mul_itch */
  ecc->mul (ecc, P2, u2, pp, u2 + ecc->p.size);
112 113 114

  /* u = 0 can happen only if h = 0 or h = q, which is extremely
     unlikely. */
115
  if (!mpn_zero_p (u1, ecc->p.size))
116
    {
117 118
      /* Total storage: 7*ecc->p.size + ecc->mul_g_itch (ecc->p.size) */
      ecc->mul_g (ecc, P1, u1, P1 + 3*ecc->p.size);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

      /* NOTE: ecc_add_jjj and/or ecc_j_to_a will produce garbage in
	 case u1 G = +/- u2 V. However, anyone who gets his or her
	 hands on a signature where this happens during verification,
	 can also get the private key as z = +/- u1 / u_2 (mod q). And
	 then it doesn't matter very much if verification of
	 signatures with that key succeeds or fails.

	 u1 G = - u2 V can never happen for a correctly generated
	 signature, since it implies k = 0.

	 u1 G = u2 V is possible, if we are unlucky enough to get h /
	 s_1 = z. Hitting that is about as unlikely as finding the
	 private key by guessing.
       */
Niels Möller's avatar
Niels Möller committed
134
      /* Total storage: 6*ecc->p.size + ecc->add_hhh_itch */
135
      ecc->add_hhh (ecc, P1, P1, P2, P1 + 3*ecc->p.size);
136
    }
137
  /* x coordinate only, modulo q */
138
  ecc->h_to_a (ecc, 2, P2, P1, P1 + 3*ecc->p.size);
139

Niels Möller's avatar
Niels Möller committed
140
  return (mpn_cmp (rp, P2, ecc->p.size) == 0);
141 142 143 144 145 146 147
#undef P2
#undef P1
#undef sinv
#undef u2
#undef hp
#undef u1
}