]>
Commit | Line | Data |
---|---|---|
5c6bf031 | 1 | /* |
fd38836b | 2 | * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
38e3c581 | 4 | * |
a7f182b7 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
4f22f405 RS |
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 | |
38e3c581 | 9 | */ |
4f22f405 | 10 | |
37942fab NL |
11 | #include <limits.h> |
12 | ||
5c6bf031 BM |
13 | #include <openssl/err.h> |
14 | #include <openssl/obj_mac.h> | |
706457b7 | 15 | #include "ec_local.h" |
0657bf9c | 16 | |
0657bf9c | 17 | const EC_METHOD *EC_GFp_nist_method(void) |
0f113f3e MC |
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, | |
9ff9bccc | 29 | ec_group_simple_order_bits, |
0f113f3e MC |
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 */ , | |
e0033efc | 55 | ec_GFp_simple_field_inv, |
0f113f3e MC |
56 | 0 /* field_encode */ , |
57 | 0 /* field_decode */ , | |
9ff9bccc DSH |
58 | 0, /* field_set_to_one */ |
59 | ec_key_simple_priv2oct, | |
60 | ec_key_simple_oct2priv, | |
61 | 0, /* set private */ | |
62 | ec_key_simple_generate_key, | |
63 | ec_key_simple_check_key, | |
64 | ec_key_simple_generate_public_key, | |
65 | 0, /* keycopy */ | |
66 | 0, /* keyfinish */ | |
f667820c | 67 | ecdh_simple_compute_key, |
9bf682f6 PS |
68 | ecdsa_simple_sign_setup, |
69 | ecdsa_simple_sign_sig, | |
70 | ecdsa_simple_verify_sig, | |
f667820c | 71 | 0, /* field_inverse_mod_ord */ |
37124360 | 72 | ec_GFp_simple_blind_coordinates, |
9d91530d BB |
73 | ec_GFp_simple_ladder_pre, |
74 | ec_GFp_simple_ladder_step, | |
75 | ec_GFp_simple_ladder_post | |
0f113f3e MC |
76 | }; |
77 | ||
78 | return &ret; | |
79 | } | |
60428dbf | 80 | |
e2c9c91b | 81 | int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src) |
0f113f3e MC |
82 | { |
83 | dest->field_mod_func = src->field_mod_func; | |
e2c9c91b | 84 | |
0f113f3e MC |
85 | return ec_GFp_simple_group_copy(dest, src); |
86 | } | |
5c6bf031 BM |
87 | |
88 | int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p, | |
0f113f3e MC |
89 | const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) |
90 | { | |
91 | int ret = 0; | |
92 | BN_CTX *new_ctx = NULL; | |
93 | ||
94 | if (ctx == NULL) | |
a9612d6c | 95 | if ((ctx = new_ctx = BN_CTX_new_ex(group->libctx)) == NULL) |
0f113f3e MC |
96 | return 0; |
97 | ||
98 | BN_CTX_start(ctx); | |
99 | ||
100 | if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0) | |
101 | group->field_mod_func = BN_nist_mod_192; | |
102 | else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0) | |
103 | group->field_mod_func = BN_nist_mod_224; | |
104 | else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0) | |
105 | group->field_mod_func = BN_nist_mod_256; | |
106 | else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0) | |
107 | group->field_mod_func = BN_nist_mod_384; | |
108 | else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0) | |
109 | group->field_mod_func = BN_nist_mod_521; | |
110 | else { | |
111 | ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME); | |
112 | goto err; | |
113 | } | |
114 | ||
115 | ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); | |
5c6bf031 BM |
116 | |
117 | err: | |
0f113f3e | 118 | BN_CTX_end(ctx); |
23a1d5e9 | 119 | BN_CTX_free(new_ctx); |
0f113f3e MC |
120 | return ret; |
121 | } | |
a2dbcf36 | 122 | |
5c6bf031 | 123 | int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
0f113f3e MC |
124 | const BIGNUM *b, BN_CTX *ctx) |
125 | { | |
126 | int ret = 0; | |
127 | BN_CTX *ctx_new = NULL; | |
128 | ||
129 | if (!group || !r || !a || !b) { | |
130 | ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER); | |
131 | goto err; | |
132 | } | |
133 | if (!ctx) | |
a9612d6c | 134 | if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL) |
0f113f3e MC |
135 | goto err; |
136 | ||
137 | if (!BN_mul(r, a, b, ctx)) | |
138 | goto err; | |
139 | if (!group->field_mod_func(r, r, group->field, ctx)) | |
140 | goto err; | |
141 | ||
142 | ret = 1; | |
143 | err: | |
23a1d5e9 | 144 | BN_CTX_free(ctx_new); |
0f113f3e MC |
145 | return ret; |
146 | } | |
a2dbcf36 | 147 | |
5c6bf031 | 148 | int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
0f113f3e MC |
149 | BN_CTX *ctx) |
150 | { | |
151 | int ret = 0; | |
152 | BN_CTX *ctx_new = NULL; | |
153 | ||
154 | if (!group || !r || !a) { | |
155 | ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER); | |
156 | goto err; | |
157 | } | |
158 | if (!ctx) | |
a9612d6c | 159 | if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL) |
0f113f3e MC |
160 | goto err; |
161 | ||
162 | if (!BN_sqr(r, a, ctx)) | |
163 | goto err; | |
164 | if (!group->field_mod_func(r, r, group->field, ctx)) | |
165 | goto err; | |
166 | ||
167 | ret = 1; | |
168 | err: | |
23a1d5e9 | 169 | BN_CTX_free(ctx_new); |
0f113f3e MC |
170 | return ret; |
171 | } |