Fix build errors for Curve448 code on Windows (VC-WIN32 and VC-WIN64A)
[openssl.git] / crypto / ec / ecp_nist.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #include <limits.h>
12
13 #include <openssl/err.h>
14 #include <openssl/obj_mac.h>
15 #include "ec_lcl.h"
16
17 const EC_METHOD *EC_GFp_nist_method(void)
18 {
19     static const EC_METHOD ret = {
20         EC_FLAGS_DEFAULT_OCT,
21         NID_X9_62_prime_field,
22         ec_GFp_simple_group_init,
23         ec_GFp_simple_group_finish,
24         ec_GFp_simple_group_clear_finish,
25         ec_GFp_nist_group_copy,
26         ec_GFp_nist_group_set_curve,
27         ec_GFp_simple_group_get_curve,
28         ec_GFp_simple_group_get_degree,
29         ec_group_simple_order_bits,
30         ec_GFp_simple_group_check_discriminant,
31         ec_GFp_simple_point_init,
32         ec_GFp_simple_point_finish,
33         ec_GFp_simple_point_clear_finish,
34         ec_GFp_simple_point_copy,
35         ec_GFp_simple_point_set_to_infinity,
36         ec_GFp_simple_set_Jprojective_coordinates_GFp,
37         ec_GFp_simple_get_Jprojective_coordinates_GFp,
38         ec_GFp_simple_point_set_affine_coordinates,
39         ec_GFp_simple_point_get_affine_coordinates,
40         0, 0, 0,
41         ec_GFp_simple_add,
42         ec_GFp_simple_dbl,
43         ec_GFp_simple_invert,
44         ec_GFp_simple_is_at_infinity,
45         ec_GFp_simple_is_on_curve,
46         ec_GFp_simple_cmp,
47         ec_GFp_simple_make_affine,
48         ec_GFp_simple_points_make_affine,
49         0 /* mul */ ,
50         0 /* precompute_mult */ ,
51         0 /* have_precompute_mult */ ,
52         ec_GFp_nist_field_mul,
53         ec_GFp_nist_field_sqr,
54         0 /* field_div */ ,
55         0 /* field_encode */ ,
56         0 /* field_decode */ ,
57         0,                      /* field_set_to_one */
58         ec_key_simple_priv2oct,
59         ec_key_simple_oct2priv,
60         0, /* set private */
61         ec_key_simple_generate_key,
62         ec_key_simple_check_key,
63         ec_key_simple_generate_public_key,
64         0, /* keycopy */
65         0, /* keyfinish */
66         ecdh_simple_compute_key
67     };
68
69     return &ret;
70 }
71
72 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
73 {
74     dest->field_mod_func = src->field_mod_func;
75
76     return ec_GFp_simple_group_copy(dest, src);
77 }
78
79 int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
80                                 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
81 {
82     int ret = 0;
83     BN_CTX *new_ctx = NULL;
84
85     if (ctx == NULL)
86         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
87             return 0;
88
89     BN_CTX_start(ctx);
90
91     if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0)
92         group->field_mod_func = BN_nist_mod_192;
93     else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0)
94         group->field_mod_func = BN_nist_mod_224;
95     else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0)
96         group->field_mod_func = BN_nist_mod_256;
97     else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0)
98         group->field_mod_func = BN_nist_mod_384;
99     else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0)
100         group->field_mod_func = BN_nist_mod_521;
101     else {
102         ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME);
103         goto err;
104     }
105
106     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
107
108  err:
109     BN_CTX_end(ctx);
110     BN_CTX_free(new_ctx);
111     return ret;
112 }
113
114 int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
115                           const BIGNUM *b, BN_CTX *ctx)
116 {
117     int ret = 0;
118     BN_CTX *ctx_new = NULL;
119
120     if (!group || !r || !a || !b) {
121         ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER);
122         goto err;
123     }
124     if (!ctx)
125         if ((ctx_new = ctx = BN_CTX_new()) == NULL)
126             goto err;
127
128     if (!BN_mul(r, a, b, ctx))
129         goto err;
130     if (!group->field_mod_func(r, r, group->field, ctx))
131         goto err;
132
133     ret = 1;
134  err:
135     BN_CTX_free(ctx_new);
136     return ret;
137 }
138
139 int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
140                           BN_CTX *ctx)
141 {
142     int ret = 0;
143     BN_CTX *ctx_new = NULL;
144
145     if (!group || !r || !a) {
146         ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);
147         goto err;
148     }
149     if (!ctx)
150         if ((ctx_new = ctx = BN_CTX_new()) == NULL)
151             goto err;
152
153     if (!BN_sqr(r, a, ctx))
154         goto err;
155     if (!group->field_mod_func(r, r, group->field, ctx))
156         goto err;
157
158     ret = 1;
159  err:
160     BN_CTX_free(ctx_new);
161     return ret;
162 }