summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/eck_prn.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/ec/eck_prn.c290
1 files changed, 133 insertions, 157 deletions
diff --git a/src/lib/libcrypto/ec/eck_prn.c b/src/lib/libcrypto/ec/eck_prn.c
index 4e8c748bbc..86107d5142 100644
--- a/src/lib/libcrypto/ec/eck_prn.c
+++ b/src/lib/libcrypto/ec/eck_prn.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
@@ -57,7 +57,7 @@
57 */ 57 */
58/* ==================================================================== 58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * Portions originally developed by SUN MICROSYSTEMS, INC., and 60 * Portions originally developed by SUN MICROSYSTEMS, INC., and
61 * contributed to the OpenSSL project. 61 * contributed to the OpenSSL project.
62 */ 62 */
63 63
@@ -68,113 +68,112 @@
68#include <openssl/bn.h> 68#include <openssl/bn.h>
69 69
70#ifndef OPENSSL_NO_FP_API 70#ifndef OPENSSL_NO_FP_API
71int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off) 71int
72 { 72ECPKParameters_print_fp(FILE * fp, const EC_GROUP * x, int off)
73{
73 BIO *b; 74 BIO *b;
74 int ret; 75 int ret;
75 76
76 if ((b=BIO_new(BIO_s_file())) == NULL) 77 if ((b = BIO_new(BIO_s_file())) == NULL) {
77 { 78 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB);
78 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB); 79 return (0);
79 return(0); 80 }
80 }
81 BIO_set_fp(b, fp, BIO_NOCLOSE); 81 BIO_set_fp(b, fp, BIO_NOCLOSE);
82 ret = ECPKParameters_print(b, x, off); 82 ret = ECPKParameters_print(b, x, off);
83 BIO_free(b); 83 BIO_free(b);
84 return(ret); 84 return (ret);
85 } 85}
86 86
87int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off) 87int
88 { 88EC_KEY_print_fp(FILE * fp, const EC_KEY * x, int off)
89{
89 BIO *b; 90 BIO *b;
90 int ret; 91 int ret;
91 92
92 if ((b=BIO_new(BIO_s_file())) == NULL) 93 if ((b = BIO_new(BIO_s_file())) == NULL) {
93 {
94 ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB); 94 ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
95 return(0); 95 return (0);
96 } 96 }
97 BIO_set_fp(b, fp, BIO_NOCLOSE); 97 BIO_set_fp(b, fp, BIO_NOCLOSE);
98 ret = EC_KEY_print(b, x, off); 98 ret = EC_KEY_print(b, x, off);
99 BIO_free(b); 99 BIO_free(b);
100 return(ret); 100 return (ret);
101 } 101}
102 102
103int ECParameters_print_fp(FILE *fp, const EC_KEY *x) 103int
104 { 104ECParameters_print_fp(FILE * fp, const EC_KEY * x)
105{
105 BIO *b; 106 BIO *b;
106 int ret; 107 int ret;
107 108
108 if ((b=BIO_new(BIO_s_file())) == NULL) 109 if ((b = BIO_new(BIO_s_file())) == NULL) {
109 {
110 ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB); 110 ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
111 return(0); 111 return (0);
112 } 112 }
113 BIO_set_fp(b, fp, BIO_NOCLOSE); 113 BIO_set_fp(b, fp, BIO_NOCLOSE);
114 ret = ECParameters_print(b, x); 114 ret = ECParameters_print(b, x);
115 BIO_free(b); 115 BIO_free(b);
116 return(ret); 116 return (ret);
117 } 117}
118#endif 118#endif
119 119
120int EC_KEY_print(BIO *bp, const EC_KEY *x, int off) 120int
121 { 121EC_KEY_print(BIO * bp, const EC_KEY * x, int off)
122{
122 EVP_PKEY *pk; 123 EVP_PKEY *pk;
123 int ret; 124 int ret;
124 pk = EVP_PKEY_new(); 125 pk = EVP_PKEY_new();
125 if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x)) 126 if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x))
126 return 0; 127 return 0;
127 ret = EVP_PKEY_print_private(bp, pk, off, NULL); 128 ret = EVP_PKEY_print_private(bp, pk, off, NULL);
128 EVP_PKEY_free(pk); 129 EVP_PKEY_free(pk);
129 return ret; 130 return ret;
130 } 131}
131 132
132int ECParameters_print(BIO *bp, const EC_KEY *x) 133int
133 { 134ECParameters_print(BIO * bp, const EC_KEY * x)
135{
134 EVP_PKEY *pk; 136 EVP_PKEY *pk;
135 int ret; 137 int ret;
136 pk = EVP_PKEY_new(); 138 pk = EVP_PKEY_new();
137 if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x)) 139 if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x))
138 return 0; 140 return 0;
139 ret = EVP_PKEY_print_params(bp, pk, 4, NULL); 141 ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
140 EVP_PKEY_free(pk); 142 EVP_PKEY_free(pk);
141 return ret; 143 return ret;
142 } 144}
143 145
144static int print_bin(BIO *fp, const char *str, const unsigned char *num, 146static int
145 size_t len, int off); 147print_bin(BIO * fp, const char *str, const unsigned char *num,
146 148 size_t len, int off);
147int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off) 149
148 { 150int
149 unsigned char *buffer=NULL; 151ECPKParameters_print(BIO * bp, const EC_GROUP * x, int off)
150 size_t buf_len=0, i; 152{
151 int ret=0, reason=ERR_R_BIO_LIB; 153 unsigned char *buffer = NULL;
152 BN_CTX *ctx=NULL; 154 size_t buf_len = 0, i;
153 const EC_POINT *point=NULL; 155 int ret = 0, reason = ERR_R_BIO_LIB;
154 BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL, 156 BN_CTX *ctx = NULL;
155 *order=NULL, *cofactor=NULL; 157 const EC_POINT *point = NULL;
158 BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL, *order = NULL,
159 *cofactor = NULL;
156 const unsigned char *seed; 160 const unsigned char *seed;
157 size_t seed_len=0; 161 size_t seed_len = 0;
158 162
159 static const char *gen_compressed = "Generator (compressed):"; 163 static const char *gen_compressed = "Generator (compressed):";
160 static const char *gen_uncompressed = "Generator (uncompressed):"; 164 static const char *gen_uncompressed = "Generator (uncompressed):";
161 static const char *gen_hybrid = "Generator (hybrid):"; 165 static const char *gen_hybrid = "Generator (hybrid):";
162 166
163 if (!x) 167 if (!x) {
164 {
165 reason = ERR_R_PASSED_NULL_PARAMETER; 168 reason = ERR_R_PASSED_NULL_PARAMETER;
166 goto err; 169 goto err;
167 } 170 }
168
169 ctx = BN_CTX_new(); 171 ctx = BN_CTX_new();
170 if (ctx == NULL) 172 if (ctx == NULL) {
171 {
172 reason = ERR_R_MALLOC_FAILURE; 173 reason = ERR_R_MALLOC_FAILURE;
173 goto err; 174 goto err;
174 } 175 }
175 176 if (EC_GROUP_get_asn1_flag(x)) {
176 if (EC_GROUP_get_asn1_flag(x))
177 {
178 /* the curve parameter are given by an asn1 OID */ 177 /* the curve parameter are given by an asn1 OID */
179 int nid; 178 int nid;
180 179
@@ -189,9 +188,7 @@ int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
189 goto err; 188 goto err;
190 if (BIO_printf(bp, "\n") <= 0) 189 if (BIO_printf(bp, "\n") <= 0)
191 goto err; 190 goto err;
192 } 191 } else {
193 else
194 {
195 /* explicit parameters */ 192 /* explicit parameters */
196 int is_char_two = 0; 193 int is_char_two = 0;
197 point_conversion_form_t form; 194 point_conversion_form_t form;
@@ -201,84 +198,71 @@ int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
201 is_char_two = 1; 198 is_char_two = 1;
202 199
203 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL || 200 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
204 (b = BN_new()) == NULL || (order = BN_new()) == NULL || 201 (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
205 (cofactor = BN_new()) == NULL) 202 (cofactor = BN_new()) == NULL) {
206 {
207 reason = ERR_R_MALLOC_FAILURE; 203 reason = ERR_R_MALLOC_FAILURE;
208 goto err; 204 goto err;
209 } 205 }
210#ifndef OPENSSL_NO_EC2M 206#ifndef OPENSSL_NO_EC2M
211 if (is_char_two) 207 if (is_char_two) {
212 { 208 if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) {
213 if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
214 {
215 reason = ERR_R_EC_LIB; 209 reason = ERR_R_EC_LIB;
216 goto err; 210 goto err;
217 }
218 } 211 }
219 else /* prime field */ 212 } else /* prime field */
220#endif 213#endif
221 { 214 {
222 if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) 215 if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) {
223 {
224 reason = ERR_R_EC_LIB; 216 reason = ERR_R_EC_LIB;
225 goto err; 217 goto err;
226 }
227 } 218 }
219 }
228 220
229 if ((point = EC_GROUP_get0_generator(x)) == NULL) 221 if ((point = EC_GROUP_get0_generator(x)) == NULL) {
230 {
231 reason = ERR_R_EC_LIB; 222 reason = ERR_R_EC_LIB;
232 goto err; 223 goto err;
233 } 224 }
234 if (!EC_GROUP_get_order(x, order, NULL) || 225 if (!EC_GROUP_get_order(x, order, NULL) ||
235 !EC_GROUP_get_cofactor(x, cofactor, NULL)) 226 !EC_GROUP_get_cofactor(x, cofactor, NULL)) {
236 {
237 reason = ERR_R_EC_LIB; 227 reason = ERR_R_EC_LIB;
238 goto err; 228 goto err;
239 } 229 }
240
241 form = EC_GROUP_get_point_conversion_form(x); 230 form = EC_GROUP_get_point_conversion_form(x);
242 231
243 if ((gen = EC_POINT_point2bn(x, point, 232 if ((gen = EC_POINT_point2bn(x, point,
244 form, NULL, ctx)) == NULL) 233 form, NULL, ctx)) == NULL) {
245 {
246 reason = ERR_R_EC_LIB; 234 reason = ERR_R_EC_LIB;
247 goto err; 235 goto err;
248 } 236 }
249 237 buf_len = (size_t) BN_num_bytes(p);
250 buf_len = (size_t)BN_num_bytes(p); 238 if (buf_len < (i = (size_t) BN_num_bytes(a)))
251 if (buf_len < (i = (size_t)BN_num_bytes(a)))
252 buf_len = i; 239 buf_len = i;
253 if (buf_len < (i = (size_t)BN_num_bytes(b))) 240 if (buf_len < (i = (size_t) BN_num_bytes(b)))
254 buf_len = i; 241 buf_len = i;
255 if (buf_len < (i = (size_t)BN_num_bytes(gen))) 242 if (buf_len < (i = (size_t) BN_num_bytes(gen)))
256 buf_len = i; 243 buf_len = i;
257 if (buf_len < (i = (size_t)BN_num_bytes(order))) 244 if (buf_len < (i = (size_t) BN_num_bytes(order)))
258 buf_len = i; 245 buf_len = i;
259 if (buf_len < (i = (size_t)BN_num_bytes(cofactor))) 246 if (buf_len < (i = (size_t) BN_num_bytes(cofactor)))
260 buf_len = i; 247 buf_len = i;
261 248
262 if ((seed = EC_GROUP_get0_seed(x)) != NULL) 249 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
263 seed_len = EC_GROUP_get_seed_len(x); 250 seed_len = EC_GROUP_get_seed_len(x);
264 251
265 buf_len += 10; 252 buf_len += 10;
266 if ((buffer = malloc(buf_len)) == NULL) 253 if ((buffer = malloc(buf_len)) == NULL) {
267 {
268 reason = ERR_R_MALLOC_FAILURE; 254 reason = ERR_R_MALLOC_FAILURE;
269 goto err; 255 goto err;
270 } 256 }
271
272 if (!BIO_indent(bp, off, 128)) 257 if (!BIO_indent(bp, off, 128))
273 goto err; 258 goto err;
274 259
275 /* print the 'short name' of the field type */ 260 /* print the 'short name' of the field type */
276 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) 261 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
277 <= 0) 262 <= 0)
278 goto err; 263 goto err;
279 264
280 if (is_char_two) 265 if (is_char_two) {
281 {
282 /* print the 'short name' of the base type OID */ 266 /* print the 'short name' of the base type OID */
283 int basis_type = EC_GROUP_get_basis_type(x); 267 int basis_type = EC_GROUP_get_basis_type(x);
284 if (basis_type == 0) 268 if (basis_type == 0)
@@ -287,7 +271,7 @@ int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
287 if (!BIO_indent(bp, off, 128)) 271 if (!BIO_indent(bp, off, 128))
288 goto err; 272 goto err;
289 273
290 if (BIO_printf(bp, "Basis Type: %s\n", 274 if (BIO_printf(bp, "Basis Type: %s\n",
291 OBJ_nid2sn(basis_type)) <= 0) 275 OBJ_nid2sn(basis_type)) <= 0)
292 goto err; 276 goto err;
293 277
@@ -295,48 +279,43 @@ int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
295 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer, 279 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer,
296 off)) 280 off))
297 goto err; 281 goto err;
298 } 282 } else {
299 else 283 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer, off))
300 {
301 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer,off))
302 goto err; 284 goto err;
303 } 285 }
304 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off)) 286 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off))
305 goto err; 287 goto err;
306 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off)) 288 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off))
307 goto err; 289 goto err;
308 if (form == POINT_CONVERSION_COMPRESSED) 290 if (form == POINT_CONVERSION_COMPRESSED) {
309 {
310 if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen, 291 if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
311 buffer, off)) 292 buffer, off))
312 goto err; 293 goto err;
313 } 294 } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
314 else if (form == POINT_CONVERSION_UNCOMPRESSED)
315 {
316 if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen, 295 if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
317 buffer, off)) 296 buffer, off))
318 goto err; 297 goto err;
319 } 298 } else { /* form == POINT_CONVERSION_HYBRID */
320 else /* form == POINT_CONVERSION_HYBRID */
321 {
322 if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen, 299 if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
323 buffer, off)) 300 buffer, off))
324 goto err; 301 goto err;
325 } 302 }
326 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, 303 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
327 buffer, off)) goto err; 304 buffer, off))
328 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor, 305 goto err;
329 buffer, off)) goto err; 306 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
307 buffer, off))
308 goto err;
330 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off)) 309 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
331 goto err; 310 goto err;
332 } 311 }
333 ret=1; 312 ret = 1;
334err: 313err:
335 if (!ret) 314 if (!ret)
336 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason); 315 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
337 if (p) 316 if (p)
338 BN_free(p); 317 BN_free(p);
339 if (a) 318 if (a)
340 BN_free(a); 319 BN_free(a);
341 if (b) 320 if (b)
342 BN_free(b); 321 BN_free(b);
@@ -348,45 +327,42 @@ err:
348 BN_free(cofactor); 327 BN_free(cofactor);
349 if (ctx) 328 if (ctx)
350 BN_CTX_free(ctx); 329 BN_CTX_free(ctx);
351 if (buffer != NULL) 330 if (buffer != NULL)
352 free(buffer); 331 free(buffer);
353 return(ret); 332 return (ret);
354 } 333}
355 334
356static int print_bin(BIO *fp, const char *name, const unsigned char *buf, 335static int
357 size_t len, int off) 336print_bin(BIO * fp, const char *name, const unsigned char *buf,
358 { 337 size_t len, int off)
338{
359 size_t i; 339 size_t i;
360 char str[128]; 340 char str[128];
361 341
362 if (buf == NULL) 342 if (buf == NULL)
363 return 1; 343 return 1;
364 if (off) 344 if (off) {
365 {
366 if (off > 128) 345 if (off > 128)
367 off=128; 346 off = 128;
368 memset(str,' ',off); 347 memset(str, ' ', off);
369 if (BIO_write(fp, str, off) <= 0) 348 if (BIO_write(fp, str, off) <= 0)
370 return 0; 349 return 0;
371 } 350 }
372 351 if (BIO_printf(fp, "%s", name) <= 0)
373 if (BIO_printf(fp,"%s", name) <= 0)
374 return 0; 352 return 0;
375 353
376 for (i=0; i<len; i++) 354 for (i = 0; i < len; i++) {
377 { 355 if ((i % 15) == 0) {
378 if ((i%15) == 0) 356 str[0] = '\n';
379 { 357 memset(&(str[1]), ' ', off + 4);
380 str[0]='\n'; 358 if (BIO_write(fp, str, off + 1 + 4) <= 0)
381 memset(&(str[1]),' ',off+4);
382 if (BIO_write(fp, str, off+1+4) <= 0)
383 return 0; 359 return 0;
384 }
385 if (BIO_printf(fp,"%02x%s",buf[i],((i+1) == len)?"":":") <= 0)
386 return 0;
387 } 360 }
388 if (BIO_write(fp,"\n",1) <= 0) 361 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <= 0)
362 return 0;
363 }
364 if (BIO_write(fp, "\n", 1) <= 0)
389 return 0; 365 return 0;
390 366
391 return 1; 367 return 1;
392 } 368}