diff options
author | tb <> | 2022-11-24 16:29:09 +0000 |
---|---|---|
committer | tb <> | 2022-11-24 16:29:09 +0000 |
commit | 702a8c6becedac4bb6fda1ca729df5ab91006fa4 (patch) | |
tree | fc64aff0c48f371c4388c55b5eb7ad70fc6131d1 | |
parent | 8a7c8abfd4f8805f2a5101e89356e9411d908a0c (diff) | |
download | openbsd-702a8c6becedac4bb6fda1ca729df5ab91006fa4.tar.gz openbsd-702a8c6becedac4bb6fda1ca729df5ab91006fa4.tar.bz2 openbsd-702a8c6becedac4bb6fda1ca729df5ab91006fa4.zip |
Clean up EC_METHOD and EC_GROUP definitions
Remove obvious comments, wrap long lines and general KNF cleanup. Format
and rephrase the more important comments.
Discussed with jsing
-rw-r--r-- | src/lib/libcrypto/ec/ec_lcl.h | 213 |
1 files changed, 111 insertions, 102 deletions
diff --git a/src/lib/libcrypto/ec/ec_lcl.h b/src/lib/libcrypto/ec/ec_lcl.h index 3a4f6ad791..e8bac0ac0e 100644 --- a/src/lib/libcrypto/ec/ec_lcl.h +++ b/src/lib/libcrypto/ec/ec_lcl.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: ec_lcl.h,v 1.22 2022/11/23 02:13:24 jsing Exp $ */ | 1 | /* $OpenBSD: ec_lcl.h,v 1.23 2022/11/24 16:29:09 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Originally written by Bodo Moeller for the OpenSSL project. | 3 | * Originally written by Bodo Moeller for the OpenSSL project. |
4 | */ | 4 | */ |
@@ -89,101 +89,102 @@ __BEGIN_HIDDEN_DECLS | |||
89 | /* Use default functions for poin2oct, oct2point and compressed coordinates */ | 89 | /* Use default functions for poin2oct, oct2point and compressed coordinates */ |
90 | #define EC_FLAGS_DEFAULT_OCT 0x1 | 90 | #define EC_FLAGS_DEFAULT_OCT 0x1 |
91 | 91 | ||
92 | /* Structure details are not part of the exported interface, | ||
93 | * so all this may change in future versions. */ | ||
94 | |||
95 | struct ec_method_st { | 92 | struct ec_method_st { |
96 | /* Various method flags */ | 93 | |
94 | /* | ||
95 | * Methods and members exposed directly by the public API. | ||
96 | */ | ||
97 | |||
97 | int flags; | 98 | int flags; |
98 | /* used by EC_METHOD_get_field_type: */ | ||
99 | int field_type; /* a NID */ | ||
100 | 99 | ||
101 | /* used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free, EC_GROUP_copy: */ | 100 | int field_type; |
101 | |||
102 | int (*group_init)(EC_GROUP *); | 102 | int (*group_init)(EC_GROUP *); |
103 | void (*group_finish)(EC_GROUP *); | 103 | void (*group_finish)(EC_GROUP *); |
104 | void (*group_clear_finish)(EC_GROUP *); | 104 | void (*group_clear_finish)(EC_GROUP *); |
105 | int (*group_copy)(EC_GROUP *, const EC_GROUP *); | 105 | int (*group_copy)(EC_GROUP *, const EC_GROUP *); |
106 | 106 | ||
107 | /* used by EC_GROUP_{get,set}_curve */ | 107 | int (*group_set_curve)(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, |
108 | int (*group_set_curve)(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *); | 108 | const BIGNUM *b, BN_CTX *); |
109 | int (*group_get_curve)(const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *); | 109 | int (*group_get_curve)(const EC_GROUP *, BIGNUM *p, BIGNUM *a, |
110 | BIGNUM *b, BN_CTX *); | ||
110 | 111 | ||
111 | /* used by EC_GROUP_get_degree: */ | ||
112 | int (*group_get_degree)(const EC_GROUP *); | 112 | int (*group_get_degree)(const EC_GROUP *); |
113 | /* used by EC_GROUP_order_bits: */ | ||
114 | int (*group_order_bits)(const EC_GROUP *); | 113 | int (*group_order_bits)(const EC_GROUP *); |
115 | /* used by EC_GROUP_check: */ | ||
116 | int (*group_check_discriminant)(const EC_GROUP *, BN_CTX *); | 114 | int (*group_check_discriminant)(const EC_GROUP *, BN_CTX *); |
117 | 115 | ||
118 | /* used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free, EC_POINT_copy: */ | ||
119 | int (*point_init)(EC_POINT *); | 116 | int (*point_init)(EC_POINT *); |
120 | void (*point_finish)(EC_POINT *); | 117 | void (*point_finish)(EC_POINT *); |
121 | void (*point_clear_finish)(EC_POINT *); | 118 | void (*point_clear_finish)(EC_POINT *); |
122 | int (*point_copy)(EC_POINT *, const EC_POINT *); | 119 | int (*point_copy)(EC_POINT *, const EC_POINT *); |
123 | 120 | ||
124 | /* | ||
125 | * used by EC_POINT_set_to_infinity, | ||
126 | * EC_POINT_set_Jprojective_coordinates, | ||
127 | * EC_POINT_get_Jprojective_coordinates, | ||
128 | * EC_POINT_set_affine_coordinates, | ||
129 | * EC_POINT_get_affine_coordinates, | ||
130 | * EC_POINT_set_compressed_coordinates: | ||
131 | */ | ||
132 | int (*point_set_to_infinity)(const EC_GROUP *, EC_POINT *); | 121 | int (*point_set_to_infinity)(const EC_GROUP *, EC_POINT *); |
133 | int (*point_set_Jprojective_coordinates)(const EC_GROUP *, EC_POINT *, | 122 | int (*point_set_Jprojective_coordinates)(const EC_GROUP *, EC_POINT *, |
134 | const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *); | 123 | const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *); |
135 | int (*point_get_Jprojective_coordinates)(const EC_GROUP *, const EC_POINT *, | 124 | int (*point_get_Jprojective_coordinates)(const EC_GROUP *, |
136 | BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *); | 125 | const EC_POINT *, BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *); |
137 | int (*point_set_affine_coordinates)(const EC_GROUP *, EC_POINT *, | 126 | int (*point_set_affine_coordinates)(const EC_GROUP *, EC_POINT *, |
138 | const BIGNUM *x, const BIGNUM *y, BN_CTX *); | 127 | const BIGNUM *x, const BIGNUM *y, BN_CTX *); |
139 | int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_POINT *, | 128 | int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_POINT *, |
140 | BIGNUM *x, BIGNUM *y, BN_CTX *); | 129 | BIGNUM *x, BIGNUM *y, BN_CTX *); |
141 | int (*point_set_compressed_coordinates)(const EC_GROUP *, EC_POINT *, | 130 | int (*point_set_compressed_coordinates)(const EC_GROUP *, EC_POINT *, |
142 | const BIGNUM *x, int y_bit, BN_CTX *); | 131 | const BIGNUM *x, int y_bit, BN_CTX *); |
143 | 132 | ||
144 | /* used by EC_POINT_point2oct, EC_POINT_oct2point: */ | 133 | size_t (*point2oct)(const EC_GROUP *, const EC_POINT *, |
145 | size_t (*point2oct)(const EC_GROUP *, const EC_POINT *, point_conversion_form_t form, | 134 | point_conversion_form_t form, unsigned char *buf, size_t len, |
146 | unsigned char *buf, size_t len, BN_CTX *); | 135 | BN_CTX *); |
147 | int (*oct2point)(const EC_GROUP *, EC_POINT *, | 136 | int (*oct2point)(const EC_GROUP *, EC_POINT *, const unsigned char *buf, |
148 | const unsigned char *buf, size_t len, BN_CTX *); | 137 | size_t len, BN_CTX *); |
149 | 138 | ||
150 | /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */ | 139 | int (*add)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, |
151 | int (*add)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *); | 140 | const EC_POINT *b, BN_CTX *); |
152 | int (*dbl)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *); | 141 | int (*dbl)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *); |
153 | int (*invert)(const EC_GROUP *, EC_POINT *, BN_CTX *); | 142 | int (*invert)(const EC_GROUP *, EC_POINT *, BN_CTX *); |
154 | 143 | ||
155 | /* used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp: */ | ||
156 | int (*is_at_infinity)(const EC_GROUP *, const EC_POINT *); | 144 | int (*is_at_infinity)(const EC_GROUP *, const EC_POINT *); |
157 | int (*is_on_curve)(const EC_GROUP *, const EC_POINT *, BN_CTX *); | 145 | int (*is_on_curve)(const EC_GROUP *, const EC_POINT *, BN_CTX *); |
158 | int (*point_cmp)(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *); | 146 | int (*point_cmp)(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, |
147 | BN_CTX *); | ||
159 | 148 | ||
160 | /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */ | ||
161 | int (*make_affine)(const EC_GROUP *, EC_POINT *, BN_CTX *); | 149 | int (*make_affine)(const EC_GROUP *, EC_POINT *, BN_CTX *); |
162 | int (*points_make_affine)(const EC_GROUP *, size_t num, EC_POINT *[], BN_CTX *); | 150 | int (*points_make_affine)(const EC_GROUP *, size_t num, EC_POINT *[], |
163 | 151 | BN_CTX *); | |
164 | /* used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult, EC_POINT_have_precompute_mult */ | 152 | |
165 | int (*mul_generator_ct)(const EC_GROUP *, EC_POINT *r, const BIGNUM *scalar, BN_CTX *); | 153 | int (*mul_generator_ct)(const EC_GROUP *, EC_POINT *r, |
166 | int (*mul_single_ct)(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, | 154 | const BIGNUM *scalar, BN_CTX *); |
167 | const EC_POINT *point, BN_CTX *); | 155 | int (*mul_single_ct)(const EC_GROUP *group, EC_POINT *r, |
168 | int (*mul_double_nonct)(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, | 156 | const BIGNUM *scalar, const EC_POINT *point, BN_CTX *); |
169 | const BIGNUM *p_scalar, const EC_POINT *point, BN_CTX *); | 157 | int (*mul_double_nonct)(const EC_GROUP *group, EC_POINT *r, |
158 | const BIGNUM *g_scalar, const BIGNUM *p_scalar, | ||
159 | const EC_POINT *point, BN_CTX *); | ||
170 | int (*precompute_mult)(EC_GROUP *group, BN_CTX *); | 160 | int (*precompute_mult)(EC_GROUP *group, BN_CTX *); |
171 | int (*have_precompute_mult)(const EC_GROUP *group); | 161 | int (*have_precompute_mult)(const EC_GROUP *group); |
172 | 162 | ||
163 | /* | ||
164 | * Internal methods. | ||
165 | */ | ||
173 | 166 | ||
174 | /* internal functions */ | 167 | /* |
175 | 168 | * These can be used by 'add' and 'dbl' so that the same implementations | |
176 | /* 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and 'dbl' so that | 169 | * of point operations can be used with different optimized versions of |
177 | * the same implementations of point operations can be used with different | 170 | * expensive field operations. |
178 | * optimized implementations of expensive field operations: */ | 171 | */ |
179 | int (*field_mul)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *); | 172 | int (*field_mul)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, |
180 | int (*field_sqr)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); | 173 | const BIGNUM *b, BN_CTX *); |
181 | int (*field_div)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *); | 174 | int (*field_sqr)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, |
175 | BN_CTX *); | ||
176 | int (*field_div)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, | ||
177 | const BIGNUM *b, BN_CTX *); | ||
178 | |||
179 | /* Encode to and decode from other forms (e.g. Montgomery). */ | ||
180 | int (*field_encode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, | ||
181 | BN_CTX *); | ||
182 | int (*field_decode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, | ||
183 | BN_CTX *); | ||
182 | 184 | ||
183 | int (*field_encode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); /* e.g. to Montgomery */ | ||
184 | int (*field_decode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); /* e.g. from Montgomery */ | ||
185 | int (*field_set_to_one)(const EC_GROUP *, BIGNUM *r, BN_CTX *); | 185 | int (*field_set_to_one)(const EC_GROUP *, BIGNUM *r, BN_CTX *); |
186 | int (*blind_coordinates)(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx); | 186 | int (*blind_coordinates)(const EC_GROUP *group, EC_POINT *p, |
187 | BN_CTX *ctx); | ||
187 | } /* EC_METHOD */; | 188 | } /* EC_METHOD */; |
188 | 189 | ||
189 | typedef struct ec_extra_data_st { | 190 | typedef struct ec_extra_data_st { |
@@ -195,57 +196,65 @@ typedef struct ec_extra_data_st { | |||
195 | } EC_EXTRA_DATA; /* used in EC_GROUP */ | 196 | } EC_EXTRA_DATA; /* used in EC_GROUP */ |
196 | 197 | ||
197 | struct ec_group_st { | 198 | struct ec_group_st { |
199 | /* | ||
200 | * Methods and members exposed via the public API. | ||
201 | */ | ||
202 | |||
198 | const EC_METHOD *meth; | 203 | const EC_METHOD *meth; |
199 | 204 | ||
200 | EC_POINT *generator; /* optional */ | 205 | EC_POINT *generator; /* Optional */ |
201 | BIGNUM order, cofactor; | 206 | BIGNUM order; |
207 | BIGNUM cofactor; | ||
202 | 208 | ||
203 | int curve_name;/* optional NID for named curve */ | 209 | int curve_name; /* Optional NID for named curve. */ |
204 | int asn1_flag; /* flag to control the asn1 encoding */ | 210 | |
211 | /* ASN.1 encoding controls. */ | ||
212 | int asn1_flag; | ||
205 | point_conversion_form_t asn1_form; | 213 | point_conversion_form_t asn1_form; |
206 | 214 | ||
207 | unsigned char *seed; /* optional seed for parameters (appears in ASN1) */ | 215 | /* Optional seed for parameters (appears in ASN.1). */ |
216 | unsigned char *seed; | ||
208 | size_t seed_len; | 217 | size_t seed_len; |
209 | 218 | ||
210 | EC_EXTRA_DATA *extra_data; /* linked list */ | 219 | /* |
211 | 220 | * Internal methods and members. Handled by the method functions, even | |
212 | /* The following members are handled by the method functions, | 221 | * if they appear to be generic. |
213 | * even if they appear generic */ | 222 | */ |
214 | 223 | ||
215 | BIGNUM field; /* | 224 | EC_EXTRA_DATA *extra_data; |
216 | * Field specification. | 225 | |
217 | * For curves over GF(p), this is the modulus; | 226 | /* |
218 | * for curves over GF(2^m), this is the | 227 | * Field specification. For GF(p) this is the modulus; for GF(2^m), |
219 | * irreducible polynomial defining the field. | 228 | * this is the irreducible polynomial defining the field. |
220 | */ | 229 | */ |
221 | 230 | BIGNUM field; | |
222 | int poly[6]; /* | 231 | |
223 | * Field specification for curves over GF(2^m). | 232 | /* |
224 | * The irreducible f(t) is then of the form: | 233 | * Field specification for GF(2^m). The irreducible polynomial is |
225 | * t^poly[0] + t^poly[1] + ... + t^poly[k] | 234 | * f(t) = t^poly[0] + t^poly[1] + ... + t^poly[k], |
226 | * where m = poly[0] > poly[1] > ... > poly[k] = 0. | 235 | * where |
227 | * The array is terminated with poly[k+1]=-1. | 236 | * m = poly[0] > poly[1] > ... > poly[k] = 0, |
228 | * All elliptic curve irreducibles have at most 5 | 237 | * and the array is terminated with poly[k+1] = -1. All elliptic curve |
229 | * non-zero terms. | 238 | * irreducibles have at most 5 non-zero terms. |
230 | */ | 239 | */ |
231 | 240 | int poly[6]; | |
232 | BIGNUM a, b; /* | 241 | |
233 | * Curve coefficients. | 242 | /* |
234 | * (Here the assumption is that BIGNUMs can be used | 243 | * Curve coefficients. In characteristic > 3, the curve is defined by a |
235 | * or abused for all kinds of fields, not just GF(p).) | 244 | * Weierstrass equation of the form |
236 | * For characteristic > 3, the curve is defined | 245 | * y^2 = x^3 + a*x + b. |
237 | * by a Weierstrass equation of the form | 246 | * For characteristic 2, the curve is defined by an equation of the form |
238 | * y^2 = x^3 + a*x + b. | 247 | * y^2 + x*y = x^3 + a*x^2 + b. |
239 | * For characteristic 2, the curve is defined by | 248 | */ |
240 | * an equation of the form | 249 | BIGNUM a, b; |
241 | * y^2 + x*y = x^3 + a*x^2 + b. | 250 | |
242 | */ | 251 | /* Enables optimized point arithmetics for special case. */ |
243 | 252 | int a_is_minus3; | |
244 | int a_is_minus3; /* enable optimized point arithmetics for special case */ | 253 | |
245 | 254 | void *field_data1; | |
246 | void *field_data1; /* method-specific (e.g., Montgomery structure) */ | 255 | void *field_data2; |
247 | void *field_data2; /* method-specific */ | 256 | int (*field_mod_func)(BIGNUM *, const BIGNUM *, const BIGNUM *, |
248 | int (*field_mod_func)(BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); /* method-specific */ | 257 | BN_CTX *); |
249 | } /* EC_GROUP */; | 258 | } /* EC_GROUP */; |
250 | 259 | ||
251 | struct ec_key_st { | 260 | struct ec_key_st { |