2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
21 /* Based on public domain code of LibTomCrypt by Tom St Denis.
22 * Adapted to gmp and nettle by Nikos Mavrogiannopoulos.
28 @file ecc_mulmod_timing.c
29 ECC Crypto, Tom St Denis
33 Perform a point multiplication (timing resistant)
34 @param k The scalar to multiply by
35 @param G The base point
36 @param R [out] Destination for kG
37 @param a The a value of the curve
38 @param modulus The modulus of the field the ECC curve is in
39 @param map Boolean whether to map back to affine or not (1==map, 0 == leave in projective)
43 ecc_mulmod_timing (mpz_t k
, ecc_point
* G
, ecc_point
* R
, mpz_t a
, mpz_t modulus
,
51 if (k
== NULL
|| G
== NULL
|| R
== NULL
|| modulus
== NULL
)
54 /* alloc ram for window temps */
55 for (i
= 0; i
< 3; i
++)
57 M
[i
] = ecc_new_point ();
60 for (j
= 0; j
< i
; j
++)
68 /* make a copy of G incase R==G */
69 tG
= ecc_new_point ();
76 /* tG = G and convert to montgomery */
77 mpz_set (tG
->x
, G
->x
);
78 mpz_set (tG
->y
, G
->y
);
79 mpz_set (tG
->z
, G
->z
);
83 mpz_set (M
[0]->x
, tG
->x
);
84 mpz_set (M
[0]->y
, tG
->y
);
85 mpz_set (M
[0]->z
, tG
->z
);
87 if ((err
= ecc_projective_dbl_point (tG
, M
[1], a
, modulus
)) != 0)
92 /* setup sliding window */
94 bit_to_read
= mpz_size (k
) * GMP_NUMB_BITS
- 1;
99 /* grab next digit as required */
100 if (bit_to_read
== -1)
102 i
= mpz_tstbit (k
, bit_to_read
--);
104 if (mode
== 0 && i
== 0)
106 /* dummy operations */
108 ecc_projective_add_point (M
[0], M
[1], M
[2], a
,
114 ecc_projective_dbl_point (M
[1], M
[2], a
, modulus
)) != 0)
121 if (mode
== 0 && i
== 1)
124 /* dummy operations */
126 ecc_projective_add_point (M
[0], M
[1], M
[2], a
,
132 ecc_projective_dbl_point (M
[1], M
[2], a
, modulus
)) != 0)
140 ecc_projective_add_point (M
[0], M
[1], M
[i
^ 1], a
,
145 if ((err
= ecc_projective_dbl_point (M
[i
], M
[i
], a
, modulus
)) != 0)
151 /* copy result out */
152 mpz_set (R
->x
, M
[0]->x
);
153 mpz_set (R
->y
, M
[0]->y
);
154 mpz_set (R
->z
, M
[0]->z
);
156 /* map R back from projective space */
159 err
= ecc_map (R
, modulus
);
167 for (i
= 0; i
< 3; i
++)
169 ecc_del_point (M
[i
]);
174 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_mulmod_timing.c,v $ */
175 /* $Revision: 1.13 $ */
176 /* $Date: 2007/05/12 14:32:35 $ */