summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/freebl/ecl/ecl.c
blob: ca87b490cd3c4fde02d8c6c731cb7fcb62c2c66f (plain)
1
2
3
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifdef FREEBL_NO_DEPEND
#include "../stubs.h"
#endif

#include "mpi.h"
#include "mplogic.h"
#include "ecl.h"
#include "ecl-priv.h"
#include "ecp.h"
#include "ecl-curve.h"
#include <stdlib.h>
#include <string.h>

/* Allocate memory for a new ECGroup object. */
ECGroup *
ECGroup_new()
{
    mp_err res = MP_OKAY;
    ECGroup *group;
    group = (ECGroup *)malloc(sizeof(ECGroup));
    if (group == NULL)
        return NULL;
    group->constructed = MP_YES;
    group->meth = NULL;
    group->text = NULL;
    MP_DIGITS(&group->curvea) = 0;
    MP_DIGITS(&group->curveb) = 0;
    MP_DIGITS(&group->genx) = 0;
    MP_DIGITS(&group->geny) = 0;
    MP_DIGITS(&group->order) = 0;
    group->base_point_mul = NULL;
    group->points_mul = NULL;
    group->validate_point = NULL;
    group->extra1 = NULL;
    group->extra2 = NULL;
    group->extra_free = NULL;
    MP_CHECKOK(mp_init(&group->curvea));
    MP_CHECKOK(mp_init(&group->curveb));
    MP_CHECKOK(mp_init(&group->genx));
    MP_CHECKOK(mp_init(&group->geny));
    MP_CHECKOK(mp_init(&group->order));

CLEANUP:
    if (res != MP_OKAY) {
        ECGroup_free(group);
        return NULL;
    }
    return group;
}

/* Construct a generic ECGroup for elliptic curves over prime fields. */
ECGroup *
ECGroup_consGFp(const mp_int *irr, const mp_int *curvea,
                const mp_int *curveb, const mp_int *genx,
                const mp_int *geny, const mp_int *order, int cofactor)
{
    mp_err res = MP_OKAY;
    ECGroup *group = NULL;

    group = ECGroup_new();
    if (group == NULL)
        return NULL;

    group->meth = GFMethod_consGFp(irr);
    if (group->meth == NULL) {
        res = MP_MEM;
        goto CLEANUP;
    }
    MP_CHECKOK(mp_copy(curvea, &group->curvea));
    MP_CHECKOK(mp_copy(curveb, &group->curveb));
    MP_CHECKOK(mp_copy(genx, &group->genx));
    MP_CHECKOK(mp_copy(geny, &group->geny));
    MP_CHECKOK(mp_copy(order, &group->order));
    group->cofactor = cofactor;
    group->point_add = &ec_GFp_pt_add_aff;
    group->point_sub = &ec_GFp_pt_sub_aff;
    group->point_dbl = &ec_GFp_pt_dbl_aff;
    group->point_mul = &ec_GFp_pt_mul_jm_wNAF;
    group->base_point_mul = NULL;
    group->points_mul = &ec_GFp_pts_mul_jac;
    group->validate_point = &ec_GFp_validate_point;

CLEANUP:
    if (res != MP_OKAY) {
        ECGroup_free(group);
        return NULL;
    }
    return group;
}

/* Construct a generic ECGroup for elliptic curves over prime fields with
 * field arithmetic implemented in Montgomery coordinates. */
ECGroup *
ECGroup_consGFp_mont(const mp_int *irr, const mp_int *curvea,
                     const mp_int *curveb, const mp_int *genx,
                     const mp_int *geny, const mp_int *order, int cofactor)
{
    mp_err res = MP_OKAY;
    ECGroup *group = NULL;

    group = ECGroup_new();
    if (group == NULL)
        return NULL;

    group->meth = GFMethod_consGFp_mont(irr);
    if (group->meth == NULL) {
        res = MP_MEM;
        goto CLEANUP;
    }
    MP_CHECKOK(group->meth->field_enc(curvea, &group->curvea, group->meth));
    MP_CHECKOK(group->meth->field_enc(curveb, &group->curveb, group->meth));
    MP_CHECKOK(group->meth->field_enc(genx, &group->genx, group->meth));
    MP_CHECKOK(group->meth->field_enc(geny, &group->geny, group->meth));
    MP_CHECKOK(mp_copy(order, &group->order));
    group->cofactor = cofactor;
    group->point_add = &ec_GFp_pt_add_aff;
    group->point_sub = &ec_GFp_pt_sub_aff;
    group->point_dbl = &ec_GFp_pt_dbl_aff;
    group->point_mul = &ec_GFp_pt_mul_jm_wNAF;
    group->base_point_mul = NULL;
    group->points_mul = &ec_GFp_pts_mul_jac;
    group->validate_point = &ec_GFp_validate_point;

CLEANUP:
    if (res != MP_OKAY) {
        ECGroup_free(group);
        return NULL;
    }
    return group;
}

/* Construct an ECGroup. */
ECGroup *
construct_ecgroup(const ECCurveName name, mp_int irr, mp_int curvea,
                  mp_int curveb, mp_int genx, mp_int geny, mp_int order,
                  int cofactor, ECField field, const char *text)
{
    int bits;
    ECGroup *group = NULL;
    mp_err res = MP_OKAY;

    /* determine number of bits */
    bits = mpl_significant_bits(&irr) - 1;
    if (bits < MP_OKAY) {
        res = bits;
        goto CLEANUP;
    }

    /* determine which optimizations (if any) to use */
    if (field == ECField_GFp) {
        switch (name) {
            case ECCurve_SECG_PRIME_256R1:
                group =
                    ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
                                    &order, cofactor);
                if (group == NULL) {
                    res = MP_UNDEF;
                    goto CLEANUP;
                }
                MP_CHECKOK(ec_group_set_gfp256(group, name));
                MP_CHECKOK(ec_group_set_gfp256_32(group, name));
                break;
            case ECCurve_SECG_PRIME_521R1:
                group =
                    ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
                                    &order, cofactor);
                if (group == NULL) {
                    res = MP_UNDEF;
                    goto CLEANUP;
                }
                MP_CHECKOK(ec_group_set_gfp521(group, name));
                break;
            default:
                /* use generic arithmetic */
                group =
                    ECGroup_consGFp_mont(&irr, &curvea, &curveb, &genx, &geny,
                                         &order, cofactor);
                if (group == NULL) {
                    res = MP_UNDEF;
                    goto CLEANUP;
                }
        }
    } else {
        res = MP_UNDEF;
        goto CLEANUP;
    }

    /* set name, if any */
    if ((group != NULL) && (text != NULL)) {
        group->text = strdup(text);
        if (group->text == NULL) {
            res = MP_MEM;
        }
    }

CLEANUP:
    if (group && res != MP_OKAY) {
        ECGroup_free(group);
        return NULL;
    }
    return group;
}

/* Construct ECGroup from parameters and name, if any. */
ECGroup *
ecgroup_fromName(const ECCurveName name,
                 const ECCurveBytes *params)
{
    mp_int irr, curvea, curveb, genx, geny, order;
    ECGroup *group = NULL;
    mp_err res = MP_OKAY;

    /* initialize values */
    MP_DIGITS(&irr) = 0;
    MP_DIGITS(&curvea) = 0;
    MP_DIGITS(&curveb) = 0;
    MP_DIGITS(&genx) = 0;
    MP_DIGITS(&geny) = 0;
    MP_DIGITS(&order) = 0;
    MP_CHECKOK(mp_init(&irr));
    MP_CHECKOK(mp_init(&curvea));
    MP_CHECKOK(mp_init(&curveb));
    MP_CHECKOK(mp_init(&genx));
    MP_CHECKOK(mp_init(&geny));
    MP_CHECKOK(mp_init(&order));
    MP_CHECKOK(mp_read_unsigned_octets(&irr, params->irr, params->scalarSize));
    MP_CHECKOK(mp_read_unsigned_octets(&curvea, params->curvea, params->scalarSize));
    MP_CHECKOK(mp_read_unsigned_octets(&curveb, params->curveb, params->scalarSize));
    MP_CHECKOK(mp_read_unsigned_octets(&genx, params->genx, params->scalarSize));
    MP_CHECKOK(mp_read_unsigned_octets(&geny, params->geny, params->scalarSize));
    MP_CHECKOK(mp_read_unsigned_octets(&order, params->order, params->scalarSize));

    group = construct_ecgroup(name, irr, curvea, curveb, genx, geny, order,
                              params->cofactor, params->field, params->text);

CLEANUP:
    mp_clear(&irr);
    mp_clear(&curvea);
    mp_clear(&curveb);
    mp_clear(&genx);
    mp_clear(&geny);
    mp_clear(&order);
    if (group && res != MP_OKAY) {
        ECGroup_free(group);
        return NULL;
    }
    return group;
}

/* Construct ECCurveBytes from an ECCurveName */
const ECCurveBytes *
ec_GetNamedCurveParams(const ECCurveName name)
{
    if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) ||
        (ecCurve_map[name] == NULL)) {
        return NULL;
    } else {
        return ecCurve_map[name];
    }
}

/* Construct ECGroup from named parameters. */
ECGroup *
ECGroup_fromName(const ECCurveName name)
{
    const ECCurveBytes *params = NULL;

    /* This doesn't work with Curve25519 but it's not necessary to. */
    PORT_Assert(name != ECCurve25519);

    params = ec_GetNamedCurveParams(name);
    if (params == NULL) {
        return NULL;
    }

    /* construct actual group */
    return ecgroup_fromName(name, params);
}

/* Validates an EC public key as described in Section 5.2.2 of X9.62. */
mp_err
ECPoint_validate(const ECGroup *group, const mp_int *px, const mp_int *py)
{
    /* 1: Verify that publicValue is not the point at infinity */
    /* 2: Verify that the coordinates of publicValue are elements
     *    of the field.
     */
    /* 3: Verify that publicValue is on the curve. */
    /* 4: Verify that the order of the curve times the publicValue
     *    is the point at infinity.
     */
    return group->validate_point(px, py, group);
}

/* Free the memory allocated (if any) to an ECGroup object. */
void
ECGroup_free(ECGroup *group)
{
    if (group == NULL)
        return;
    GFMethod_free(group->meth);
    if (group->constructed == MP_NO)
        return;
    mp_clear(&group->curvea);
    mp_clear(&group->curveb);
    mp_clear(&group->genx);
    mp_clear(&group->geny);
    mp_clear(&group->order);
    if (group->text != NULL)
        free(group->text);
    if (group->extra_free != NULL)
        group->extra_free(group);
    free(group);
}