ecc-ecdsa-sign.c 2.7 KB
Newer Older
1 2
/* ecc-ecdsa-sign.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 signing */

mp_size_t
ecc_ecdsa_sign_itch (const struct ecc_curve *ecc)
{
Niels Möller's avatar
Niels Möller committed
49
  /* Needs 3*ecc->p.size + scratch for ecc->mul_g. Currently same for
50
     ecc_mul_g and ecc_mul_g_eh. */
Niels Möller's avatar
Niels Möller committed
51
  return ECC_ECDSA_SIGN_ITCH (ecc->p.size);
52 53 54 55 56 57 58 59 60
}

/* NOTE: Caller should check if r or s is zero. */
void
ecc_ecdsa_sign (const struct ecc_curve *ecc,
		const mp_limb_t *zp,
		/* Random nonce, must be invertible mod ecc group
		   order. */
		const mp_limb_t *kp,
Niels Möller's avatar
Niels Möller committed
61
		size_t length, const uint8_t *digest,
62 63 64 65
		mp_limb_t *rp, mp_limb_t *sp,
		mp_limb_t *scratch)
{
#define P	    scratch
Niels Möller's avatar
Niels Möller committed
66 67 68
#define kinv	    scratch                /* Needs 5*ecc->p.size for computation */
#define hp	    (scratch  + ecc->p.size) /* NOTE: ecc->p.size + 1 limbs! */
#define tp	    (scratch + 2*ecc->p.size)
69 70 71 72 73 74 75 76 77 78 79 80
  /* Procedure, according to RFC 6090, "KT-I". q denotes the group
     order.

     1. k <-- uniformly random, 0 < k < q

     2. R <-- (r_x, r_y) = k g

     3. s1 <-- r_x mod q

     4. s2 <-- (h + z*s1)/k mod q.
  */

Niels Möller's avatar
Niels Möller committed
81
  ecc->mul_g (ecc, P, kp, P + 3*ecc->p.size);
82
  /* x coordinate only, modulo q */
Niels Möller's avatar
Niels Möller committed
83
  ecc->h_to_a (ecc, 2, rp, P, P + 3*ecc->p.size);
84

Niels Möller's avatar
Niels Möller committed
85 86
  /* Invert k, uses 5 * ecc->p.size including scratch */
  mpn_copyi (hp, kp, ecc->p.size);
87
  ecc->q.invert (&ecc->q, kinv, hp, tp);
88 89 90 91 92 93 94 95
  
  /* Process hash digest */
  ecc_hash (ecc, hp, length, digest);

  ecc_modq_mul (ecc, tp, zp, rp);
  ecc_modq_add (ecc, hp, hp, tp);
  ecc_modq_mul (ecc, tp, hp, kinv);

Niels Möller's avatar
Niels Möller committed
96
  mpn_copyi (sp, tp, ecc->p.size);
97 98 99 100 101
#undef P
#undef hp
#undef kinv
#undef tp
}