diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/ec/eck_prn.c | 271 |
1 files changed, 150 insertions, 121 deletions
diff --git a/src/lib/libcrypto/ec/eck_prn.c b/src/lib/libcrypto/ec/eck_prn.c index 04eccad061..9bf05bb38c 100644 --- a/src/lib/libcrypto/ec/eck_prn.c +++ b/src/lib/libcrypto/ec/eck_prn.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: eck_prn.c,v 1.22 2023/07/01 08:15:31 tb Exp $ */ | 1 | /* $OpenBSD: eck_prn.c,v 1.23 2023/07/02 13:48:47 tb Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Written by Nils Larsch for the OpenSSL project. | 3 | * Written by Nils Larsch for the OpenSSL project. |
| 4 | */ | 4 | */ |
| @@ -157,147 +157,176 @@ static int | |||
| 157 | print_bin(BIO *fp, const char *str, const unsigned char *num, | 157 | print_bin(BIO *fp, const char *str, const unsigned char *num, |
| 158 | size_t len, int off); | 158 | size_t len, int off); |
| 159 | 159 | ||
| 160 | int | 160 | static int |
| 161 | ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off) | 161 | ecpk_print_asn1_parameters(BIO *bp, const EC_GROUP *group, int off) |
| 162 | { | 162 | { |
| 163 | unsigned char *buffer = NULL; | 163 | const char *nist_name; |
| 164 | size_t buf_len = 0, i; | ||
| 165 | int ret = 0, reason = ERR_R_BIO_LIB; | ||
| 166 | int nid; | 164 | int nid; |
| 167 | BN_CTX *ctx = NULL; | 165 | int ret = 0; |
| 168 | const EC_POINT *point = NULL; | 166 | |
| 169 | BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL, *order = NULL, | 167 | if (!BIO_indent(bp, off, 128)) { |
| 170 | *cofactor = NULL; | 168 | ECerror(ERR_R_BIO_LIB); |
| 171 | const unsigned char *seed; | ||
| 172 | size_t seed_len = 0; | ||
| 173 | const char *nname; | ||
| 174 | |||
| 175 | if (!x) { | ||
| 176 | reason = ERR_R_PASSED_NULL_PARAMETER; | ||
| 177 | goto err; | 169 | goto err; |
| 178 | } | 170 | } |
| 179 | ctx = BN_CTX_new(); | 171 | |
| 180 | if (ctx == NULL) { | 172 | if ((nid = EC_GROUP_get_curve_name(group)) == NID_undef) { |
| 181 | reason = ERR_R_MALLOC_FAILURE; | 173 | ECerror(ERR_R_INTERNAL_ERROR); |
| 182 | goto err; | 174 | goto err; |
| 183 | } | 175 | } |
| 184 | if (EC_GROUP_get_asn1_flag(x)) { | ||
| 185 | /* the curve parameter are given by an asn1 OID */ | ||
| 186 | if (!BIO_indent(bp, off, 128)) | ||
| 187 | goto err; | ||
| 188 | 176 | ||
| 189 | nid = EC_GROUP_get_curve_name(x); | 177 | if (BIO_printf(bp, "ASN1 OID: %s\n", OBJ_nid2sn(nid)) <= 0) { |
| 190 | if (nid == 0) | 178 | ECerror(ERR_R_BIO_LIB); |
| 191 | goto err; | 179 | goto err; |
| 192 | 180 | } | |
| 193 | if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0) | ||
| 194 | goto err; | ||
| 195 | if (BIO_printf(bp, "\n") <= 0) | ||
| 196 | goto err; | ||
| 197 | 181 | ||
| 198 | nname = EC_curve_nid2nist(nid); | 182 | if ((nist_name = EC_curve_nid2nist(nid)) != NULL) { |
| 199 | if (nname) { | 183 | if (!BIO_indent(bp, off, 128)) { |
| 200 | if (!BIO_indent(bp, off, 128)) | 184 | ECerror(ERR_R_BIO_LIB); |
| 201 | goto err; | ||
| 202 | if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0) | ||
| 203 | goto err; | ||
| 204 | } | ||
| 205 | } else { | ||
| 206 | /* explicit parameters */ | ||
| 207 | point_conversion_form_t form; | ||
| 208 | const char *conversion; | ||
| 209 | |||
| 210 | if ((p = BN_new()) == NULL || (a = BN_new()) == NULL || | ||
| 211 | (b = BN_new()) == NULL || (order = BN_new()) == NULL || | ||
| 212 | (cofactor = BN_new()) == NULL) { | ||
| 213 | reason = ERR_R_MALLOC_FAILURE; | ||
| 214 | goto err; | 185 | goto err; |
| 215 | } | 186 | } |
| 216 | if (!EC_GROUP_get_curve(x, p, a, b, ctx)) { | 187 | if (BIO_printf(bp, "NIST CURVE: %s\n", nist_name) <= 0) { |
| 217 | reason = ERR_R_EC_LIB; | 188 | ECerror(ERR_R_BIO_LIB); |
| 218 | goto err; | 189 | goto err; |
| 219 | } | 190 | } |
| 191 | } | ||
| 220 | 192 | ||
| 221 | if ((point = EC_GROUP_get0_generator(x)) == NULL) { | 193 | ret = 1; |
| 222 | reason = ERR_R_EC_LIB; | 194 | err: |
| 223 | goto err; | ||
| 224 | } | ||
| 225 | if (!EC_GROUP_get_order(x, order, NULL) || | ||
| 226 | !EC_GROUP_get_cofactor(x, cofactor, NULL)) { | ||
| 227 | reason = ERR_R_EC_LIB; | ||
| 228 | goto err; | ||
| 229 | } | ||
| 230 | form = EC_GROUP_get_point_conversion_form(x); | ||
| 231 | 195 | ||
| 232 | if ((gen = EC_POINT_point2bn(x, point, | 196 | return ret; |
| 233 | form, NULL, ctx)) == NULL) { | 197 | } |
| 234 | reason = ERR_R_EC_LIB; | ||
| 235 | goto err; | ||
| 236 | } | ||
| 237 | buf_len = (size_t) BN_num_bytes(p); | ||
| 238 | if (buf_len < (i = (size_t) BN_num_bytes(a))) | ||
| 239 | buf_len = i; | ||
| 240 | if (buf_len < (i = (size_t) BN_num_bytes(b))) | ||
| 241 | buf_len = i; | ||
| 242 | if (buf_len < (i = (size_t) BN_num_bytes(gen))) | ||
| 243 | buf_len = i; | ||
| 244 | if (buf_len < (i = (size_t) BN_num_bytes(order))) | ||
| 245 | buf_len = i; | ||
| 246 | if (buf_len < (i = (size_t) BN_num_bytes(cofactor))) | ||
| 247 | buf_len = i; | ||
| 248 | |||
| 249 | if ((seed = EC_GROUP_get0_seed(x)) != NULL) | ||
| 250 | seed_len = EC_GROUP_get_seed_len(x); | ||
| 251 | |||
| 252 | buf_len += 10; | ||
| 253 | if ((buffer = malloc(buf_len)) == NULL) { | ||
| 254 | reason = ERR_R_MALLOC_FAILURE; | ||
| 255 | goto err; | ||
| 256 | } | ||
| 257 | if (!BIO_indent(bp, off, 128)) | ||
| 258 | goto err; | ||
| 259 | 198 | ||
| 260 | nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x)); | 199 | static int |
| 261 | /* print the 'short name' of the field type */ | 200 | ecpk_print_explicit_parameters(BIO *bp, const EC_GROUP *group, int off) |
| 262 | if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(nid)) <= 0) | 201 | { |
| 263 | goto err; | 202 | BN_CTX *ctx = NULL; |
| 203 | BIGNUM *p, *a, *b, *order, *cofactor; | ||
| 204 | BIGNUM *gen = NULL; | ||
| 205 | const EC_POINT *generator; | ||
| 206 | const unsigned char *conversion, *seed; | ||
| 207 | size_t seed_len; | ||
| 208 | unsigned char *buffer = NULL; | ||
| 209 | size_t buf_len, i; | ||
| 210 | point_conversion_form_t form; | ||
| 211 | int nid; | ||
| 212 | int ret = 0; | ||
| 264 | 213 | ||
| 265 | if (!ASN1_bn_print(bp, "Prime:", p, buffer, off)) | 214 | if ((ctx = BN_CTX_new()) == NULL) { |
| 266 | goto err; | 215 | ECerror(ERR_R_MALLOC_FAILURE); |
| 267 | if (!ASN1_bn_print(bp, "A: ", a, buffer, off)) | 216 | goto err; |
| 268 | goto err; | 217 | } |
| 269 | if (!ASN1_bn_print(bp, "B: ", b, buffer, off)) | 218 | |
| 270 | goto err; | 219 | BN_CTX_start(ctx); |
| 271 | if (form == POINT_CONVERSION_COMPRESSED) | 220 | |
| 272 | conversion = "Generator (compressed):"; | 221 | if ((p = BN_CTX_get(ctx)) == NULL) |
| 273 | else if (form == POINT_CONVERSION_UNCOMPRESSED) | 222 | goto err; |
| 274 | conversion = "Generator (uncompressed):"; | 223 | if ((a = BN_CTX_get(ctx)) == NULL) |
| 275 | else if (form == POINT_CONVERSION_HYBRID) | 224 | goto err; |
| 276 | conversion = "Generator (hybrid):"; | 225 | if ((b = BN_CTX_get(ctx)) == NULL) |
| 277 | else | 226 | goto err; |
| 278 | conversion = "Generator (unknown):"; | 227 | if ((order = BN_CTX_get(ctx)) == NULL) |
| 279 | if (!ASN1_bn_print(bp, conversion, gen, buffer, off)) | 228 | goto err; |
| 280 | goto err; | 229 | if ((cofactor = BN_CTX_get(ctx)) == NULL) |
| 281 | if (!ASN1_bn_print(bp, "Order: ", order, buffer, off)) | 230 | goto err; |
| 282 | goto err; | 231 | if ((gen = BN_CTX_get(ctx)) == NULL) |
| 283 | if (!ASN1_bn_print(bp, "Cofactor: ", cofactor, buffer, off)) | 232 | goto err; |
| 284 | goto err; | 233 | |
| 285 | if (seed && !print_bin(bp, "Seed:", seed, seed_len, off)) | 234 | if (!EC_GROUP_get_curve(group, p, a, b, ctx)) { |
| 235 | ECerror(ERR_R_EC_LIB); | ||
| 236 | goto err; | ||
| 237 | } | ||
| 238 | if (!EC_GROUP_get_order(group, order, NULL)) { | ||
| 239 | ECerror(ERR_R_EC_LIB); | ||
| 240 | goto err; | ||
| 241 | } | ||
| 242 | if (!EC_GROUP_get_cofactor(group, cofactor, NULL)) { | ||
| 243 | ECerror(ERR_R_EC_LIB); | ||
| 244 | goto err; | ||
| 245 | } | ||
| 246 | |||
| 247 | if ((generator = EC_GROUP_get0_generator(group)) == NULL) { | ||
| 248 | ECerror(ERR_R_EC_LIB); | ||
| 249 | goto err; | ||
| 250 | } | ||
| 251 | form = EC_GROUP_get_point_conversion_form(group); | ||
| 252 | if (EC_POINT_point2bn(group, generator, form, gen, ctx) == NULL) { | ||
| 253 | ECerror(ERR_R_EC_LIB); | ||
| 254 | goto err; | ||
| 255 | } | ||
| 256 | |||
| 257 | buf_len = (size_t) BN_num_bytes(p); | ||
| 258 | if (buf_len < (i = (size_t) BN_num_bytes(a))) | ||
| 259 | buf_len = i; | ||
| 260 | if (buf_len < (i = (size_t) BN_num_bytes(b))) | ||
| 261 | buf_len = i; | ||
| 262 | if (buf_len < (i = (size_t) BN_num_bytes(gen))) | ||
| 263 | buf_len = i; | ||
| 264 | if (buf_len < (i = (size_t) BN_num_bytes(order))) | ||
| 265 | buf_len = i; | ||
| 266 | if (buf_len < (i = (size_t) BN_num_bytes(cofactor))) | ||
| 267 | buf_len = i; | ||
| 268 | |||
| 269 | buf_len += 10; | ||
| 270 | if ((buffer = calloc(1, buf_len)) == NULL) { | ||
| 271 | ECerror(ERR_R_MALLOC_FAILURE); | ||
| 272 | goto err; | ||
| 273 | } | ||
| 274 | if (!BIO_indent(bp, off, 128)) | ||
| 275 | goto err; | ||
| 276 | |||
| 277 | nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); | ||
| 278 | if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(nid)) <= 0) | ||
| 279 | goto err; | ||
| 280 | |||
| 281 | if (!ASN1_bn_print(bp, "Prime:", p, buffer, off)) | ||
| 282 | goto err; | ||
| 283 | if (!ASN1_bn_print(bp, "A: ", a, buffer, off)) | ||
| 284 | goto err; | ||
| 285 | if (!ASN1_bn_print(bp, "B: ", b, buffer, off)) | ||
| 286 | goto err; | ||
| 287 | |||
| 288 | if (form == POINT_CONVERSION_COMPRESSED) | ||
| 289 | conversion = "Generator (compressed):"; | ||
| 290 | else if (form == POINT_CONVERSION_UNCOMPRESSED) | ||
| 291 | conversion = "Generator (uncompressed):"; | ||
| 292 | else if (form == POINT_CONVERSION_HYBRID) | ||
| 293 | conversion = "Generator (hybrid):"; | ||
| 294 | else | ||
| 295 | conversion = "Generator (unknown):"; | ||
| 296 | if (!ASN1_bn_print(bp, conversion, gen, buffer, off)) | ||
| 297 | goto err; | ||
| 298 | |||
| 299 | if (!ASN1_bn_print(bp, "Order: ", order, buffer, off)) | ||
| 300 | goto err; | ||
| 301 | if (!ASN1_bn_print(bp, "Cofactor: ", cofactor, buffer, off)) | ||
| 302 | goto err; | ||
| 303 | if ((seed = EC_GROUP_get0_seed(group)) != NULL) { | ||
| 304 | seed_len = EC_GROUP_get_seed_len(group); | ||
| 305 | if (!print_bin(bp, "Seed:", seed, seed_len, off)) | ||
| 286 | goto err; | 306 | goto err; |
| 287 | } | 307 | } |
| 308 | |||
| 288 | ret = 1; | 309 | ret = 1; |
| 289 | err: | 310 | err: |
| 290 | if (!ret) | 311 | BN_CTX_end(ctx); |
| 291 | ECerror(reason); | ||
| 292 | BN_free(p); | ||
| 293 | BN_free(a); | ||
| 294 | BN_free(b); | ||
| 295 | BN_free(gen); | ||
| 296 | BN_free(order); | ||
| 297 | BN_free(cofactor); | ||
| 298 | BN_CTX_free(ctx); | 312 | BN_CTX_free(ctx); |
| 299 | free(buffer); | 313 | free(buffer); |
| 300 | return (ret); | 314 | |
| 315 | return ret; | ||
| 316 | } | ||
| 317 | |||
| 318 | int | ||
| 319 | ECPKParameters_print(BIO *bp, const EC_GROUP *group, int off) | ||
| 320 | { | ||
| 321 | if (group == NULL) { | ||
| 322 | ECerror(ERR_R_PASSED_NULL_PARAMETER); | ||
| 323 | return 0; | ||
| 324 | } | ||
| 325 | |||
| 326 | if (EC_GROUP_get_asn1_flag(group)) | ||
| 327 | return ecpk_print_asn1_parameters(bp, group, off); | ||
| 328 | |||
| 329 | return ecpk_print_explicit_parameters(bp, group, off); | ||
| 301 | } | 330 | } |
| 302 | 331 | ||
| 303 | static int | 332 | static int |
