diff options
author | tedu <> | 2014-05-06 03:56:27 +0000 |
---|---|---|
committer | tedu <> | 2014-05-06 03:56:27 +0000 |
commit | 2518b24aa4315d557b967bff48dfc9efed909569 (patch) | |
tree | ce2ee4fdddbbe61dd0ccb045a1604a3d92a86a00 /src/lib/libcrypto/ec/ecp_nist.c | |
parent | 0539604f5771dae2c3ecffa8122b5651ff283719 (diff) | |
download | openbsd-2518b24aa4315d557b967bff48dfc9efed909569.tar.gz openbsd-2518b24aa4315d557b967bff48dfc9efed909569.tar.bz2 openbsd-2518b24aa4315d557b967bff48dfc9efed909569.zip |
knf approximation
Diffstat (limited to '')
-rw-r--r-- | src/lib/libcrypto/ec/ecp_nist.c | 95 |
1 files changed, 51 insertions, 44 deletions
diff --git a/src/lib/libcrypto/ec/ecp_nist.c b/src/lib/libcrypto/ec/ecp_nist.c index 479cff8fc9..60c4a2d790 100644 --- a/src/lib/libcrypto/ec/ecp_nist.c +++ b/src/lib/libcrypto/ec/ecp_nist.c | |||
@@ -10,7 +10,7 @@ | |||
10 | * are met: | 10 | * are met: |
11 | * | 11 | * |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * | 14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in | 16 | * notice, this list of conditions and the following disclaimer in |
@@ -81,20 +81,20 @@ EC_GFp_nist_method(void) | |||
81 | .group_get_curve = ec_GFp_simple_group_get_curve, | 81 | .group_get_curve = ec_GFp_simple_group_get_curve, |
82 | .group_get_degree = ec_GFp_simple_group_get_degree, | 82 | .group_get_degree = ec_GFp_simple_group_get_degree, |
83 | .group_check_discriminant = | 83 | .group_check_discriminant = |
84 | ec_GFp_simple_group_check_discriminant, | 84 | ec_GFp_simple_group_check_discriminant, |
85 | .point_init = ec_GFp_simple_point_init, | 85 | .point_init = ec_GFp_simple_point_init, |
86 | .point_finish = ec_GFp_simple_point_finish, | 86 | .point_finish = ec_GFp_simple_point_finish, |
87 | .point_clear_finish = ec_GFp_simple_point_clear_finish, | 87 | .point_clear_finish = ec_GFp_simple_point_clear_finish, |
88 | .point_copy = ec_GFp_simple_point_copy, | 88 | .point_copy = ec_GFp_simple_point_copy, |
89 | .point_set_to_infinity = ec_GFp_simple_point_set_to_infinity, | 89 | .point_set_to_infinity = ec_GFp_simple_point_set_to_infinity, |
90 | .point_set_Jprojective_coordinates_GFp = | 90 | .point_set_Jprojective_coordinates_GFp = |
91 | ec_GFp_simple_set_Jprojective_coordinates_GFp, | 91 | ec_GFp_simple_set_Jprojective_coordinates_GFp, |
92 | .point_get_Jprojective_coordinates_GFp = | 92 | .point_get_Jprojective_coordinates_GFp = |
93 | ec_GFp_simple_get_Jprojective_coordinates_GFp, | 93 | ec_GFp_simple_get_Jprojective_coordinates_GFp, |
94 | .point_set_affine_coordinates = | 94 | .point_set_affine_coordinates = |
95 | ec_GFp_simple_point_set_affine_coordinates, | 95 | ec_GFp_simple_point_set_affine_coordinates, |
96 | .point_get_affine_coordinates = | 96 | .point_get_affine_coordinates = |
97 | ec_GFp_simple_point_get_affine_coordinates, | 97 | ec_GFp_simple_point_get_affine_coordinates, |
98 | .add = ec_GFp_simple_add, | 98 | .add = ec_GFp_simple_add, |
99 | .dbl = ec_GFp_simple_dbl, | 99 | .dbl = ec_GFp_simple_dbl, |
100 | .invert = ec_GFp_simple_invert, | 100 | .invert = ec_GFp_simple_invert, |
@@ -110,25 +110,29 @@ EC_GFp_nist_method(void) | |||
110 | return &ret; | 110 | return &ret; |
111 | } | 111 | } |
112 | 112 | ||
113 | int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src) | 113 | int |
114 | { | 114 | ec_GFp_nist_group_copy(EC_GROUP * dest, const EC_GROUP * src) |
115 | { | ||
115 | dest->field_mod_func = src->field_mod_func; | 116 | dest->field_mod_func = src->field_mod_func; |
116 | 117 | ||
117 | return ec_GFp_simple_group_copy(dest, src); | 118 | return ec_GFp_simple_group_copy(dest, src); |
118 | } | 119 | } |
119 | 120 | ||
120 | int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p, | 121 | int |
121 | const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) | 122 | ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p, |
122 | { | 123 | const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) |
124 | { | ||
123 | int ret = 0; | 125 | int ret = 0; |
124 | BN_CTX *new_ctx = NULL; | 126 | BN_CTX *new_ctx = NULL; |
125 | BIGNUM *tmp_bn; | 127 | BIGNUM *tmp_bn; |
126 | 128 | ||
127 | if (ctx == NULL) | 129 | if (ctx == NULL) |
128 | if ((ctx = new_ctx = BN_CTX_new()) == NULL) return 0; | 130 | if ((ctx = new_ctx = BN_CTX_new()) == NULL) |
131 | return 0; | ||
129 | 132 | ||
130 | BN_CTX_start(ctx); | 133 | BN_CTX_start(ctx); |
131 | if ((tmp_bn = BN_CTX_get(ctx)) == NULL) goto err; | 134 | if ((tmp_bn = BN_CTX_get(ctx)) == NULL) |
135 | goto err; | ||
132 | 136 | ||
133 | if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0) | 137 | if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0) |
134 | group->field_mod_func = BN_nist_mod_192; | 138 | group->field_mod_func = BN_nist_mod_192; |
@@ -140,69 +144,72 @@ int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p, | |||
140 | group->field_mod_func = BN_nist_mod_384; | 144 | group->field_mod_func = BN_nist_mod_384; |
141 | else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0) | 145 | else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0) |
142 | group->field_mod_func = BN_nist_mod_521; | 146 | group->field_mod_func = BN_nist_mod_521; |
143 | else | 147 | else { |
144 | { | ||
145 | ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME); | 148 | ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME); |
146 | goto err; | 149 | goto err; |
147 | } | 150 | } |
148 | 151 | ||
149 | ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); | 152 | ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); |
150 | 153 | ||
151 | err: | 154 | err: |
152 | BN_CTX_end(ctx); | 155 | BN_CTX_end(ctx); |
153 | if (new_ctx != NULL) | 156 | if (new_ctx != NULL) |
154 | BN_CTX_free(new_ctx); | 157 | BN_CTX_free(new_ctx); |
155 | return ret; | 158 | return ret; |
156 | } | 159 | } |
157 | 160 | ||
158 | 161 | ||
159 | int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | 162 | int |
160 | const BIGNUM *b, BN_CTX *ctx) | 163 | ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
161 | { | 164 | const BIGNUM *b, BN_CTX *ctx) |
162 | int ret=0; | 165 | { |
163 | BN_CTX *ctx_new=NULL; | 166 | int ret = 0; |
167 | BN_CTX *ctx_new = NULL; | ||
164 | 168 | ||
165 | if (!group || !r || !a || !b) | 169 | if (!group || !r || !a || !b) { |
166 | { | ||
167 | ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER); | 170 | ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER); |
168 | goto err; | 171 | goto err; |
169 | } | 172 | } |
170 | if (!ctx) | 173 | if (!ctx) |
171 | if ((ctx_new = ctx = BN_CTX_new()) == NULL) goto err; | 174 | if ((ctx_new = ctx = BN_CTX_new()) == NULL) |
175 | goto err; | ||
172 | 176 | ||
173 | if (!BN_mul(r, a, b, ctx)) goto err; | 177 | if (!BN_mul(r, a, b, ctx)) |
178 | goto err; | ||
174 | if (!group->field_mod_func(r, r, &group->field, ctx)) | 179 | if (!group->field_mod_func(r, r, &group->field, ctx)) |
175 | goto err; | 180 | goto err; |
176 | 181 | ||
177 | ret=1; | 182 | ret = 1; |
178 | err: | 183 | err: |
179 | if (ctx_new) | 184 | if (ctx_new) |
180 | BN_CTX_free(ctx_new); | 185 | BN_CTX_free(ctx_new); |
181 | return ret; | 186 | return ret; |
182 | } | 187 | } |
183 | 188 | ||
184 | 189 | ||
185 | int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | 190 | int |
186 | BN_CTX *ctx) | 191 | ec_GFp_nist_field_sqr(const EC_GROUP * group, BIGNUM * r, const BIGNUM * a, |
187 | { | 192 | BN_CTX * ctx) |
188 | int ret=0; | 193 | { |
189 | BN_CTX *ctx_new=NULL; | 194 | int ret = 0; |
195 | BN_CTX *ctx_new = NULL; | ||
190 | 196 | ||
191 | if (!group || !r || !a) | 197 | if (!group || !r || !a) { |
192 | { | ||
193 | ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER); | 198 | ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER); |
194 | goto err; | 199 | goto err; |
195 | } | 200 | } |
196 | if (!ctx) | 201 | if (!ctx) |
197 | if ((ctx_new = ctx = BN_CTX_new()) == NULL) goto err; | 202 | if ((ctx_new = ctx = BN_CTX_new()) == NULL) |
203 | goto err; | ||
198 | 204 | ||
199 | if (!BN_sqr(r, a, ctx)) goto err; | 205 | if (!BN_sqr(r, a, ctx)) |
206 | goto err; | ||
200 | if (!group->field_mod_func(r, r, &group->field, ctx)) | 207 | if (!group->field_mod_func(r, r, &group->field, ctx)) |
201 | goto err; | 208 | goto err; |
202 | 209 | ||
203 | ret=1; | 210 | ret = 1; |
204 | err: | 211 | err: |
205 | if (ctx_new) | 212 | if (ctx_new) |
206 | BN_CTX_free(ctx_new); | 213 | BN_CTX_free(ctx_new); |
207 | return ret; | 214 | return ret; |
208 | } | 215 | } |