Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "mpi.h" |
michael@0 | 6 | #include "mplogic.h" |
michael@0 | 7 | #include "ecl.h" |
michael@0 | 8 | #include "ecl-priv.h" |
michael@0 | 9 | #include <stdlib.h> |
michael@0 | 10 | |
michael@0 | 11 | /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k * P(x, |
michael@0 | 12 | * y). If x, y = NULL, then P is assumed to be the generator (base point) |
michael@0 | 13 | * of the group of points on the elliptic curve. Input and output values |
michael@0 | 14 | * are assumed to be NOT field-encoded. */ |
michael@0 | 15 | mp_err |
michael@0 | 16 | ECPoint_mul(const ECGroup *group, const mp_int *k, const mp_int *px, |
michael@0 | 17 | const mp_int *py, mp_int *rx, mp_int *ry) |
michael@0 | 18 | { |
michael@0 | 19 | mp_err res = MP_OKAY; |
michael@0 | 20 | mp_int kt; |
michael@0 | 21 | |
michael@0 | 22 | ARGCHK((k != NULL) && (group != NULL), MP_BADARG); |
michael@0 | 23 | MP_DIGITS(&kt) = 0; |
michael@0 | 24 | |
michael@0 | 25 | /* want scalar to be less than or equal to group order */ |
michael@0 | 26 | if (mp_cmp(k, &group->order) > 0) { |
michael@0 | 27 | MP_CHECKOK(mp_init(&kt)); |
michael@0 | 28 | MP_CHECKOK(mp_mod(k, &group->order, &kt)); |
michael@0 | 29 | } else { |
michael@0 | 30 | MP_SIGN(&kt) = MP_ZPOS; |
michael@0 | 31 | MP_USED(&kt) = MP_USED(k); |
michael@0 | 32 | MP_ALLOC(&kt) = MP_ALLOC(k); |
michael@0 | 33 | MP_DIGITS(&kt) = MP_DIGITS(k); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | if ((px == NULL) || (py == NULL)) { |
michael@0 | 37 | if (group->base_point_mul) { |
michael@0 | 38 | MP_CHECKOK(group->base_point_mul(&kt, rx, ry, group)); |
michael@0 | 39 | } else { |
michael@0 | 40 | MP_CHECKOK(group-> |
michael@0 | 41 | point_mul(&kt, &group->genx, &group->geny, rx, ry, |
michael@0 | 42 | group)); |
michael@0 | 43 | } |
michael@0 | 44 | } else { |
michael@0 | 45 | if (group->meth->field_enc) { |
michael@0 | 46 | MP_CHECKOK(group->meth->field_enc(px, rx, group->meth)); |
michael@0 | 47 | MP_CHECKOK(group->meth->field_enc(py, ry, group->meth)); |
michael@0 | 48 | MP_CHECKOK(group->point_mul(&kt, rx, ry, rx, ry, group)); |
michael@0 | 49 | } else { |
michael@0 | 50 | MP_CHECKOK(group->point_mul(&kt, px, py, rx, ry, group)); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | if (group->meth->field_dec) { |
michael@0 | 54 | MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth)); |
michael@0 | 55 | MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth)); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | CLEANUP: |
michael@0 | 59 | if (MP_DIGITS(&kt) != MP_DIGITS(k)) { |
michael@0 | 60 | mp_clear(&kt); |
michael@0 | 61 | } |
michael@0 | 62 | return res; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G + |
michael@0 | 66 | * k2 * P(x, y), where G is the generator (base point) of the group of |
michael@0 | 67 | * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL. |
michael@0 | 68 | * Input and output values are assumed to be NOT field-encoded. */ |
michael@0 | 69 | mp_err |
michael@0 | 70 | ec_pts_mul_basic(const mp_int *k1, const mp_int *k2, const mp_int *px, |
michael@0 | 71 | const mp_int *py, mp_int *rx, mp_int *ry, |
michael@0 | 72 | const ECGroup *group) |
michael@0 | 73 | { |
michael@0 | 74 | mp_err res = MP_OKAY; |
michael@0 | 75 | mp_int sx, sy; |
michael@0 | 76 | |
michael@0 | 77 | ARGCHK(group != NULL, MP_BADARG); |
michael@0 | 78 | ARGCHK(!((k1 == NULL) |
michael@0 | 79 | && ((k2 == NULL) || (px == NULL) |
michael@0 | 80 | || (py == NULL))), MP_BADARG); |
michael@0 | 81 | |
michael@0 | 82 | /* if some arguments are not defined used ECPoint_mul */ |
michael@0 | 83 | if (k1 == NULL) { |
michael@0 | 84 | return ECPoint_mul(group, k2, px, py, rx, ry); |
michael@0 | 85 | } else if ((k2 == NULL) || (px == NULL) || (py == NULL)) { |
michael@0 | 86 | return ECPoint_mul(group, k1, NULL, NULL, rx, ry); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | MP_DIGITS(&sx) = 0; |
michael@0 | 90 | MP_DIGITS(&sy) = 0; |
michael@0 | 91 | MP_CHECKOK(mp_init(&sx)); |
michael@0 | 92 | MP_CHECKOK(mp_init(&sy)); |
michael@0 | 93 | |
michael@0 | 94 | MP_CHECKOK(ECPoint_mul(group, k1, NULL, NULL, &sx, &sy)); |
michael@0 | 95 | MP_CHECKOK(ECPoint_mul(group, k2, px, py, rx, ry)); |
michael@0 | 96 | |
michael@0 | 97 | if (group->meth->field_enc) { |
michael@0 | 98 | MP_CHECKOK(group->meth->field_enc(&sx, &sx, group->meth)); |
michael@0 | 99 | MP_CHECKOK(group->meth->field_enc(&sy, &sy, group->meth)); |
michael@0 | 100 | MP_CHECKOK(group->meth->field_enc(rx, rx, group->meth)); |
michael@0 | 101 | MP_CHECKOK(group->meth->field_enc(ry, ry, group->meth)); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | MP_CHECKOK(group->point_add(&sx, &sy, rx, ry, rx, ry, group)); |
michael@0 | 105 | |
michael@0 | 106 | if (group->meth->field_dec) { |
michael@0 | 107 | MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth)); |
michael@0 | 108 | MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth)); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | CLEANUP: |
michael@0 | 112 | mp_clear(&sx); |
michael@0 | 113 | mp_clear(&sy); |
michael@0 | 114 | return res; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G + |
michael@0 | 118 | * k2 * P(x, y), where G is the generator (base point) of the group of |
michael@0 | 119 | * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL. |
michael@0 | 120 | * Input and output values are assumed to be NOT field-encoded. Uses |
michael@0 | 121 | * algorithm 15 (simultaneous multiple point multiplication) from Brown, |
michael@0 | 122 | * Hankerson, Lopez, Menezes. Software Implementation of the NIST |
michael@0 | 123 | * Elliptic Curves over Prime Fields. */ |
michael@0 | 124 | mp_err |
michael@0 | 125 | ec_pts_mul_simul_w2(const mp_int *k1, const mp_int *k2, const mp_int *px, |
michael@0 | 126 | const mp_int *py, mp_int *rx, mp_int *ry, |
michael@0 | 127 | const ECGroup *group) |
michael@0 | 128 | { |
michael@0 | 129 | mp_err res = MP_OKAY; |
michael@0 | 130 | mp_int precomp[4][4][2]; |
michael@0 | 131 | const mp_int *a, *b; |
michael@0 | 132 | int i, j; |
michael@0 | 133 | int ai, bi, d; |
michael@0 | 134 | |
michael@0 | 135 | ARGCHK(group != NULL, MP_BADARG); |
michael@0 | 136 | ARGCHK(!((k1 == NULL) |
michael@0 | 137 | && ((k2 == NULL) || (px == NULL) |
michael@0 | 138 | || (py == NULL))), MP_BADARG); |
michael@0 | 139 | |
michael@0 | 140 | /* if some arguments are not defined used ECPoint_mul */ |
michael@0 | 141 | if (k1 == NULL) { |
michael@0 | 142 | return ECPoint_mul(group, k2, px, py, rx, ry); |
michael@0 | 143 | } else if ((k2 == NULL) || (px == NULL) || (py == NULL)) { |
michael@0 | 144 | return ECPoint_mul(group, k1, NULL, NULL, rx, ry); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | /* initialize precomputation table */ |
michael@0 | 148 | for (i = 0; i < 4; i++) { |
michael@0 | 149 | for (j = 0; j < 4; j++) { |
michael@0 | 150 | MP_DIGITS(&precomp[i][j][0]) = 0; |
michael@0 | 151 | MP_DIGITS(&precomp[i][j][1]) = 0; |
michael@0 | 152 | } |
michael@0 | 153 | } |
michael@0 | 154 | for (i = 0; i < 4; i++) { |
michael@0 | 155 | for (j = 0; j < 4; j++) { |
michael@0 | 156 | MP_CHECKOK( mp_init_size(&precomp[i][j][0], |
michael@0 | 157 | ECL_MAX_FIELD_SIZE_DIGITS) ); |
michael@0 | 158 | MP_CHECKOK( mp_init_size(&precomp[i][j][1], |
michael@0 | 159 | ECL_MAX_FIELD_SIZE_DIGITS) ); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | /* fill precomputation table */ |
michael@0 | 164 | /* assign {k1, k2} = {a, b} such that len(a) >= len(b) */ |
michael@0 | 165 | if (mpl_significant_bits(k1) < mpl_significant_bits(k2)) { |
michael@0 | 166 | a = k2; |
michael@0 | 167 | b = k1; |
michael@0 | 168 | if (group->meth->field_enc) { |
michael@0 | 169 | MP_CHECKOK(group->meth-> |
michael@0 | 170 | field_enc(px, &precomp[1][0][0], group->meth)); |
michael@0 | 171 | MP_CHECKOK(group->meth-> |
michael@0 | 172 | field_enc(py, &precomp[1][0][1], group->meth)); |
michael@0 | 173 | } else { |
michael@0 | 174 | MP_CHECKOK(mp_copy(px, &precomp[1][0][0])); |
michael@0 | 175 | MP_CHECKOK(mp_copy(py, &precomp[1][0][1])); |
michael@0 | 176 | } |
michael@0 | 177 | MP_CHECKOK(mp_copy(&group->genx, &precomp[0][1][0])); |
michael@0 | 178 | MP_CHECKOK(mp_copy(&group->geny, &precomp[0][1][1])); |
michael@0 | 179 | } else { |
michael@0 | 180 | a = k1; |
michael@0 | 181 | b = k2; |
michael@0 | 182 | MP_CHECKOK(mp_copy(&group->genx, &precomp[1][0][0])); |
michael@0 | 183 | MP_CHECKOK(mp_copy(&group->geny, &precomp[1][0][1])); |
michael@0 | 184 | if (group->meth->field_enc) { |
michael@0 | 185 | MP_CHECKOK(group->meth-> |
michael@0 | 186 | field_enc(px, &precomp[0][1][0], group->meth)); |
michael@0 | 187 | MP_CHECKOK(group->meth-> |
michael@0 | 188 | field_enc(py, &precomp[0][1][1], group->meth)); |
michael@0 | 189 | } else { |
michael@0 | 190 | MP_CHECKOK(mp_copy(px, &precomp[0][1][0])); |
michael@0 | 191 | MP_CHECKOK(mp_copy(py, &precomp[0][1][1])); |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | /* precompute [*][0][*] */ |
michael@0 | 195 | mp_zero(&precomp[0][0][0]); |
michael@0 | 196 | mp_zero(&precomp[0][0][1]); |
michael@0 | 197 | MP_CHECKOK(group-> |
michael@0 | 198 | point_dbl(&precomp[1][0][0], &precomp[1][0][1], |
michael@0 | 199 | &precomp[2][0][0], &precomp[2][0][1], group)); |
michael@0 | 200 | MP_CHECKOK(group-> |
michael@0 | 201 | point_add(&precomp[1][0][0], &precomp[1][0][1], |
michael@0 | 202 | &precomp[2][0][0], &precomp[2][0][1], |
michael@0 | 203 | &precomp[3][0][0], &precomp[3][0][1], group)); |
michael@0 | 204 | /* precompute [*][1][*] */ |
michael@0 | 205 | for (i = 1; i < 4; i++) { |
michael@0 | 206 | MP_CHECKOK(group-> |
michael@0 | 207 | point_add(&precomp[0][1][0], &precomp[0][1][1], |
michael@0 | 208 | &precomp[i][0][0], &precomp[i][0][1], |
michael@0 | 209 | &precomp[i][1][0], &precomp[i][1][1], group)); |
michael@0 | 210 | } |
michael@0 | 211 | /* precompute [*][2][*] */ |
michael@0 | 212 | MP_CHECKOK(group-> |
michael@0 | 213 | point_dbl(&precomp[0][1][0], &precomp[0][1][1], |
michael@0 | 214 | &precomp[0][2][0], &precomp[0][2][1], group)); |
michael@0 | 215 | for (i = 1; i < 4; i++) { |
michael@0 | 216 | MP_CHECKOK(group-> |
michael@0 | 217 | point_add(&precomp[0][2][0], &precomp[0][2][1], |
michael@0 | 218 | &precomp[i][0][0], &precomp[i][0][1], |
michael@0 | 219 | &precomp[i][2][0], &precomp[i][2][1], group)); |
michael@0 | 220 | } |
michael@0 | 221 | /* precompute [*][3][*] */ |
michael@0 | 222 | MP_CHECKOK(group-> |
michael@0 | 223 | point_add(&precomp[0][1][0], &precomp[0][1][1], |
michael@0 | 224 | &precomp[0][2][0], &precomp[0][2][1], |
michael@0 | 225 | &precomp[0][3][0], &precomp[0][3][1], group)); |
michael@0 | 226 | for (i = 1; i < 4; i++) { |
michael@0 | 227 | MP_CHECKOK(group-> |
michael@0 | 228 | point_add(&precomp[0][3][0], &precomp[0][3][1], |
michael@0 | 229 | &precomp[i][0][0], &precomp[i][0][1], |
michael@0 | 230 | &precomp[i][3][0], &precomp[i][3][1], group)); |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | d = (mpl_significant_bits(a) + 1) / 2; |
michael@0 | 234 | |
michael@0 | 235 | /* R = inf */ |
michael@0 | 236 | mp_zero(rx); |
michael@0 | 237 | mp_zero(ry); |
michael@0 | 238 | |
michael@0 | 239 | for (i = d - 1; i >= 0; i--) { |
michael@0 | 240 | ai = MP_GET_BIT(a, 2 * i + 1); |
michael@0 | 241 | ai <<= 1; |
michael@0 | 242 | ai |= MP_GET_BIT(a, 2 * i); |
michael@0 | 243 | bi = MP_GET_BIT(b, 2 * i + 1); |
michael@0 | 244 | bi <<= 1; |
michael@0 | 245 | bi |= MP_GET_BIT(b, 2 * i); |
michael@0 | 246 | /* R = 2^2 * R */ |
michael@0 | 247 | MP_CHECKOK(group->point_dbl(rx, ry, rx, ry, group)); |
michael@0 | 248 | MP_CHECKOK(group->point_dbl(rx, ry, rx, ry, group)); |
michael@0 | 249 | /* R = R + (ai * A + bi * B) */ |
michael@0 | 250 | MP_CHECKOK(group-> |
michael@0 | 251 | point_add(rx, ry, &precomp[ai][bi][0], |
michael@0 | 252 | &precomp[ai][bi][1], rx, ry, group)); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | if (group->meth->field_dec) { |
michael@0 | 256 | MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth)); |
michael@0 | 257 | MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth)); |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | CLEANUP: |
michael@0 | 261 | for (i = 0; i < 4; i++) { |
michael@0 | 262 | for (j = 0; j < 4; j++) { |
michael@0 | 263 | mp_clear(&precomp[i][j][0]); |
michael@0 | 264 | mp_clear(&precomp[i][j][1]); |
michael@0 | 265 | } |
michael@0 | 266 | } |
michael@0 | 267 | return res; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G + |
michael@0 | 271 | * k2 * P(x, y), where G is the generator (base point) of the group of |
michael@0 | 272 | * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL. |
michael@0 | 273 | * Input and output values are assumed to be NOT field-encoded. */ |
michael@0 | 274 | mp_err |
michael@0 | 275 | ECPoints_mul(const ECGroup *group, const mp_int *k1, const mp_int *k2, |
michael@0 | 276 | const mp_int *px, const mp_int *py, mp_int *rx, mp_int *ry) |
michael@0 | 277 | { |
michael@0 | 278 | mp_err res = MP_OKAY; |
michael@0 | 279 | mp_int k1t, k2t; |
michael@0 | 280 | const mp_int *k1p, *k2p; |
michael@0 | 281 | |
michael@0 | 282 | MP_DIGITS(&k1t) = 0; |
michael@0 | 283 | MP_DIGITS(&k2t) = 0; |
michael@0 | 284 | |
michael@0 | 285 | ARGCHK(group != NULL, MP_BADARG); |
michael@0 | 286 | |
michael@0 | 287 | /* want scalar to be less than or equal to group order */ |
michael@0 | 288 | if (k1 != NULL) { |
michael@0 | 289 | if (mp_cmp(k1, &group->order) >= 0) { |
michael@0 | 290 | MP_CHECKOK(mp_init(&k1t)); |
michael@0 | 291 | MP_CHECKOK(mp_mod(k1, &group->order, &k1t)); |
michael@0 | 292 | k1p = &k1t; |
michael@0 | 293 | } else { |
michael@0 | 294 | k1p = k1; |
michael@0 | 295 | } |
michael@0 | 296 | } else { |
michael@0 | 297 | k1p = k1; |
michael@0 | 298 | } |
michael@0 | 299 | if (k2 != NULL) { |
michael@0 | 300 | if (mp_cmp(k2, &group->order) >= 0) { |
michael@0 | 301 | MP_CHECKOK(mp_init(&k2t)); |
michael@0 | 302 | MP_CHECKOK(mp_mod(k2, &group->order, &k2t)); |
michael@0 | 303 | k2p = &k2t; |
michael@0 | 304 | } else { |
michael@0 | 305 | k2p = k2; |
michael@0 | 306 | } |
michael@0 | 307 | } else { |
michael@0 | 308 | k2p = k2; |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | /* if points_mul is defined, then use it */ |
michael@0 | 312 | if (group->points_mul) { |
michael@0 | 313 | res = group->points_mul(k1p, k2p, px, py, rx, ry, group); |
michael@0 | 314 | } else { |
michael@0 | 315 | res = ec_pts_mul_simul_w2(k1p, k2p, px, py, rx, ry, group); |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | CLEANUP: |
michael@0 | 319 | mp_clear(&k1t); |
michael@0 | 320 | mp_clear(&k2t); |
michael@0 | 321 | return res; |
michael@0 | 322 | } |