diff options
Diffstat (limited to 'src/regress/lib/libcrypto/ecdsa')
| -rw-r--r-- | src/regress/lib/libcrypto/ecdsa/ecdsatest.c | 138 |
1 files changed, 64 insertions, 74 deletions
diff --git a/src/regress/lib/libcrypto/ecdsa/ecdsatest.c b/src/regress/lib/libcrypto/ecdsa/ecdsatest.c index 810fef61bb..a7fcf52bee 100644 --- a/src/regress/lib/libcrypto/ecdsa/ecdsatest.c +++ b/src/regress/lib/libcrypto/ecdsa/ecdsatest.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 |
| @@ -58,13 +58,13 @@ | |||
| 58 | /* ==================================================================== | 58 | /* ==================================================================== |
| 59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | 59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 60 | * | 60 | * |
| 61 | * Portions of the attached software ("Contribution") are developed by | 61 | * Portions of the attached software ("Contribution") are developed by |
| 62 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. | 62 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 63 | * | 63 | * |
| 64 | * The Contribution is licensed pursuant to the OpenSSL open source | 64 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 65 | * license provided above. | 65 | * license provided above. |
| 66 | * | 66 | * |
| 67 | * The elliptic curve binary polynomial software is originally written by | 67 | * The elliptic curve binary polynomial software is originally written by |
| 68 | * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. | 68 | * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. |
| 69 | * | 69 | * |
| 70 | */ | 70 | */ |
| @@ -89,7 +89,7 @@ int test_builtin(BIO *); | |||
| 89 | 89 | ||
| 90 | /* some tests from the X9.62 draft */ | 90 | /* some tests from the X9.62 draft */ |
| 91 | int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in) | 91 | int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in) |
| 92 | { | 92 | { |
| 93 | int ret = 0; | 93 | int ret = 0; |
| 94 | const char message[] = "abc"; | 94 | const char message[] = "abc"; |
| 95 | unsigned char digest[20]; | 95 | unsigned char digest[20]; |
| @@ -150,10 +150,10 @@ x962_int_err: | |||
| 150 | BN_free(s); | 150 | BN_free(s); |
| 151 | EVP_MD_CTX_cleanup(&md_ctx); | 151 | EVP_MD_CTX_cleanup(&md_ctx); |
| 152 | return ret; | 152 | return ret; |
| 153 | } | 153 | } |
| 154 | 154 | ||
| 155 | int test_builtin(BIO *out) | 155 | int test_builtin(BIO *out) |
| 156 | { | 156 | { |
| 157 | EC_builtin_curve *curves = NULL; | 157 | EC_builtin_curve *curves = NULL; |
| 158 | size_t crv_len = 0, n = 0; | 158 | size_t crv_len = 0, n = 0; |
| 159 | EC_KEY *eckey = NULL, *wrong_eckey = NULL; | 159 | EC_KEY *eckey = NULL, *wrong_eckey = NULL; |
| @@ -166,13 +166,12 @@ int test_builtin(BIO *out) | |||
| 166 | unsigned char *raw_buf = NULL; | 166 | unsigned char *raw_buf = NULL; |
| 167 | unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len; | 167 | unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len; |
| 168 | int nid, ret = 0; | 168 | int nid, ret = 0; |
| 169 | 169 | ||
| 170 | /* fill digest values with some random data */ | 170 | /* fill digest values with some random data */ |
| 171 | arc4random_buf(digest, 20); | 171 | arc4random_buf(digest, 20); |
| 172 | arc4random_buf(wrong_digest, 20); | 172 | arc4random_buf(wrong_digest, 20); |
| 173 | 173 | ||
| 174 | /* create and verify a ecdsa signature with every availble curve | 174 | /* create and verify a ecdsa signature with every available curve */ |
| 175 | * (with ) */ | ||
| 176 | BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() " | 175 | BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() " |
| 177 | "with some internal curves:\n"); | 176 | "with some internal curves:\n"); |
| 178 | 177 | ||
| @@ -181,21 +180,18 @@ int test_builtin(BIO *out) | |||
| 181 | 180 | ||
| 182 | curves = reallocarray(NULL, sizeof(EC_builtin_curve), crv_len); | 181 | curves = reallocarray(NULL, sizeof(EC_builtin_curve), crv_len); |
| 183 | 182 | ||
| 184 | if (curves == NULL) | 183 | if (curves == NULL) { |
| 185 | { | ||
| 186 | BIO_printf(out, "malloc error\n"); | 184 | BIO_printf(out, "malloc error\n"); |
| 187 | goto builtin_err; | 185 | goto builtin_err; |
| 188 | } | 186 | } |
| 189 | 187 | ||
| 190 | if (!EC_get_builtin_curves(curves, crv_len)) | 188 | if (!EC_get_builtin_curves(curves, crv_len)) { |
| 191 | { | ||
| 192 | BIO_printf(out, "unable to get internal curves\n"); | 189 | BIO_printf(out, "unable to get internal curves\n"); |
| 193 | goto builtin_err; | 190 | goto builtin_err; |
| 194 | } | 191 | } |
| 195 | 192 | ||
| 196 | /* now create and verify a signature for every curve */ | 193 | /* now create and verify a signature for every curve */ |
| 197 | for (n = 0; n < crv_len; n++) | 194 | for (n = 0; n < crv_len; n++) { |
| 198 | { | ||
| 199 | unsigned char dirt, offset; | 195 | unsigned char dirt, offset; |
| 200 | 196 | ||
| 201 | nid = curves[n].nid; | 197 | nid = curves[n].nid; |
| @@ -211,20 +207,18 @@ int test_builtin(BIO *out) | |||
| 211 | goto builtin_err; | 207 | goto builtin_err; |
| 212 | EC_GROUP_free(group); | 208 | EC_GROUP_free(group); |
| 213 | degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey)); | 209 | degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey)); |
| 214 | if (degree < 160) | 210 | if (degree < 160) { |
| 215 | /* drop the curve */ | 211 | /* drop the curve */ |
| 216 | { | ||
| 217 | EC_KEY_free(eckey); | 212 | EC_KEY_free(eckey); |
| 218 | eckey = NULL; | 213 | eckey = NULL; |
| 219 | continue; | 214 | continue; |
| 220 | } | 215 | } |
| 221 | BIO_printf(out, "%s: ", OBJ_nid2sn(nid)); | 216 | BIO_printf(out, "%s: ", OBJ_nid2sn(nid)); |
| 222 | /* create key */ | 217 | /* create key */ |
| 223 | if (!EC_KEY_generate_key(eckey)) | 218 | if (!EC_KEY_generate_key(eckey)) { |
| 224 | { | ||
| 225 | BIO_printf(out, " failed\n"); | 219 | BIO_printf(out, " failed\n"); |
| 226 | goto builtin_err; | 220 | goto builtin_err; |
| 227 | } | 221 | } |
| 228 | /* create second key */ | 222 | /* create second key */ |
| 229 | if ((wrong_eckey = EC_KEY_new()) == NULL) | 223 | if ((wrong_eckey = EC_KEY_new()) == NULL) |
| 230 | goto builtin_err; | 224 | goto builtin_err; |
| @@ -234,88 +228,83 @@ int test_builtin(BIO *out) | |||
| 234 | if (EC_KEY_set_group(wrong_eckey, group) == 0) | 228 | if (EC_KEY_set_group(wrong_eckey, group) == 0) |
| 235 | goto builtin_err; | 229 | goto builtin_err; |
| 236 | EC_GROUP_free(group); | 230 | EC_GROUP_free(group); |
| 237 | if (!EC_KEY_generate_key(wrong_eckey)) | 231 | if (!EC_KEY_generate_key(wrong_eckey)) { |
| 238 | { | ||
| 239 | BIO_printf(out, " failed\n"); | 232 | BIO_printf(out, " failed\n"); |
| 240 | goto builtin_err; | 233 | goto builtin_err; |
| 241 | } | 234 | } |
| 242 | 235 | ||
| 243 | BIO_printf(out, "."); | 236 | BIO_printf(out, "."); |
| 244 | (void)BIO_flush(out); | 237 | (void)BIO_flush(out); |
| 245 | /* check key */ | 238 | /* check key */ |
| 246 | if (!EC_KEY_check_key(eckey)) | 239 | if (!EC_KEY_check_key(eckey)) { |
| 247 | { | ||
| 248 | BIO_printf(out, " failed\n"); | 240 | BIO_printf(out, " failed\n"); |
| 249 | goto builtin_err; | 241 | goto builtin_err; |
| 250 | } | 242 | } |
| 251 | BIO_printf(out, "."); | 243 | BIO_printf(out, "."); |
| 252 | (void)BIO_flush(out); | 244 | (void)BIO_flush(out); |
| 253 | /* create signature */ | 245 | /* create signature */ |
| 254 | sig_len = ECDSA_size(eckey); | 246 | sig_len = ECDSA_size(eckey); |
| 255 | if ((signature = malloc(sig_len)) == NULL) | 247 | if ((signature = malloc(sig_len)) == NULL) |
| 256 | goto builtin_err; | 248 | goto builtin_err; |
| 257 | if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey)) | 249 | if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey)) { |
| 258 | { | ||
| 259 | BIO_printf(out, " failed\n"); | 250 | BIO_printf(out, " failed\n"); |
| 260 | goto builtin_err; | 251 | goto builtin_err; |
| 261 | } | 252 | } |
| 262 | BIO_printf(out, "."); | 253 | BIO_printf(out, "."); |
| 263 | (void)BIO_flush(out); | 254 | (void)BIO_flush(out); |
| 264 | /* verify signature */ | 255 | /* verify signature */ |
| 265 | if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1) | 256 | if (ECDSA_verify(0, digest, 20, signature, sig_len, |
| 266 | { | 257 | eckey) != 1) { |
| 267 | BIO_printf(out, " failed\n"); | 258 | BIO_printf(out, " failed\n"); |
| 268 | goto builtin_err; | 259 | goto builtin_err; |
| 269 | } | 260 | } |
| 270 | BIO_printf(out, "."); | 261 | BIO_printf(out, "."); |
| 271 | (void)BIO_flush(out); | 262 | (void)BIO_flush(out); |
| 272 | /* verify signature with the wrong key */ | 263 | /* verify signature with the wrong key */ |
| 273 | if (ECDSA_verify(0, digest, 20, signature, sig_len, | 264 | if (ECDSA_verify(0, digest, 20, signature, sig_len, |
| 274 | wrong_eckey) == 1) | 265 | wrong_eckey) == 1) { |
| 275 | { | ||
| 276 | BIO_printf(out, " failed\n"); | 266 | BIO_printf(out, " failed\n"); |
| 277 | goto builtin_err; | 267 | goto builtin_err; |
| 278 | } | 268 | } |
| 279 | BIO_printf(out, "."); | 269 | BIO_printf(out, "."); |
| 280 | (void)BIO_flush(out); | 270 | (void)BIO_flush(out); |
| 281 | /* wrong digest */ | 271 | /* wrong digest */ |
| 282 | if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len, | 272 | if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len, |
| 283 | eckey) == 1) | 273 | eckey) == 1) { |
| 284 | { | ||
| 285 | BIO_printf(out, " failed\n"); | 274 | BIO_printf(out, " failed\n"); |
| 286 | goto builtin_err; | 275 | goto builtin_err; |
| 287 | } | 276 | } |
| 288 | BIO_printf(out, "."); | 277 | BIO_printf(out, "."); |
| 289 | (void)BIO_flush(out); | 278 | (void)BIO_flush(out); |
| 290 | /* wrong length */ | 279 | /* wrong length */ |
| 291 | if (ECDSA_verify(0, digest, 20, signature, sig_len - 1, | 280 | if (ECDSA_verify(0, digest, 20, signature, sig_len - 1, |
| 292 | eckey) == 1) | 281 | eckey) == 1) { |
| 293 | { | ||
| 294 | BIO_printf(out, " failed\n"); | 282 | BIO_printf(out, " failed\n"); |
| 295 | goto builtin_err; | 283 | goto builtin_err; |
| 296 | } | 284 | } |
| 297 | BIO_printf(out, "."); | 285 | BIO_printf(out, "."); |
| 298 | (void)BIO_flush(out); | 286 | (void)BIO_flush(out); |
| 299 | 287 | ||
| 300 | /* Modify a single byte of the signature: to ensure we don't | 288 | /* |
| 289 | * Modify a single byte of the signature: to ensure we don't | ||
| 301 | * garble the ASN1 structure, we read the raw signature and | 290 | * garble the ASN1 structure, we read the raw signature and |
| 302 | * modify a byte in one of the bignums directly. */ | 291 | * modify a byte in one of the bignums directly. |
| 292 | */ | ||
| 303 | sig_ptr = signature; | 293 | sig_ptr = signature; |
| 304 | if ((ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)) == NULL) | 294 | if ((ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, |
| 305 | { | 295 | sig_len)) == NULL) { |
| 306 | BIO_printf(out, " failed\n"); | 296 | BIO_printf(out, " failed\n"); |
| 307 | goto builtin_err; | 297 | goto builtin_err; |
| 308 | } | 298 | } |
| 309 | 299 | ||
| 310 | /* Store the two BIGNUMs in raw_buf. */ | 300 | /* Store the two BIGNUMs in raw_buf. */ |
| 311 | r_len = BN_num_bytes(ecdsa_sig->r); | 301 | r_len = BN_num_bytes(ecdsa_sig->r); |
| 312 | s_len = BN_num_bytes(ecdsa_sig->s); | 302 | s_len = BN_num_bytes(ecdsa_sig->s); |
| 313 | bn_len = (degree + 7) / 8; | 303 | bn_len = (degree + 7) / 8; |
| 314 | if ((r_len > bn_len) || (s_len > bn_len)) | 304 | if ((r_len > bn_len) || (s_len > bn_len)) { |
| 315 | { | ||
| 316 | BIO_printf(out, " failed\n"); | 305 | BIO_printf(out, " failed\n"); |
| 317 | goto builtin_err; | 306 | goto builtin_err; |
| 318 | } | 307 | } |
| 319 | buf_len = 2 * bn_len; | 308 | buf_len = 2 * bn_len; |
| 320 | if ((raw_buf = calloc(1, buf_len)) == NULL) | 309 | if ((raw_buf = calloc(1, buf_len)) == NULL) |
| 321 | goto builtin_err; | 310 | goto builtin_err; |
| @@ -328,32 +317,32 @@ int test_builtin(BIO *out) | |||
| 328 | raw_buf[offset] ^= dirt; | 317 | raw_buf[offset] ^= dirt; |
| 329 | /* Now read the BIGNUMs back in from raw_buf. */ | 318 | /* Now read the BIGNUMs back in from raw_buf. */ |
| 330 | if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) || | 319 | if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) || |
| 331 | (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL)) | 320 | (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL)) |
| 332 | goto builtin_err; | 321 | goto builtin_err; |
| 333 | 322 | ||
| 334 | sig_ptr2 = signature; | 323 | sig_ptr2 = signature; |
| 335 | sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2); | 324 | sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2); |
| 336 | if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1) | 325 | if (ECDSA_verify(0, digest, 20, signature, sig_len, |
| 337 | { | 326 | eckey) == 1) { |
| 338 | BIO_printf(out, " failed\n"); | 327 | BIO_printf(out, " failed\n"); |
| 339 | goto builtin_err; | 328 | goto builtin_err; |
| 340 | } | 329 | } |
| 341 | /* Sanity check: undo the modification and verify signature. */ | 330 | /* Sanity check: undo the modification and verify signature. */ |
| 342 | raw_buf[offset] ^= dirt; | 331 | raw_buf[offset] ^= dirt; |
| 343 | if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) || | 332 | if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) || |
| 344 | (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL)) | 333 | (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL)) |
| 345 | goto builtin_err; | 334 | goto builtin_err; |
| 346 | 335 | ||
| 347 | sig_ptr2 = signature; | 336 | sig_ptr2 = signature; |
| 348 | sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2); | 337 | sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2); |
| 349 | if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1) | 338 | if (ECDSA_verify(0, digest, 20, signature, sig_len, |
| 350 | { | 339 | eckey) != 1) { |
| 351 | BIO_printf(out, " failed\n"); | 340 | BIO_printf(out, " failed\n"); |
| 352 | goto builtin_err; | 341 | goto builtin_err; |
| 353 | } | 342 | } |
| 354 | BIO_printf(out, "."); | 343 | BIO_printf(out, "."); |
| 355 | (void)BIO_flush(out); | 344 | (void)BIO_flush(out); |
| 356 | 345 | ||
| 357 | BIO_printf(out, " ok\n"); | 346 | BIO_printf(out, " ok\n"); |
| 358 | /* cleanup */ | 347 | /* cleanup */ |
| 359 | /* clean bogus errors */ | 348 | /* clean bogus errors */ |
| @@ -368,9 +357,9 @@ int test_builtin(BIO *out) | |||
| 368 | ecdsa_sig = NULL; | 357 | ecdsa_sig = NULL; |
| 369 | free(raw_buf); | 358 | free(raw_buf); |
| 370 | raw_buf = NULL; | 359 | raw_buf = NULL; |
| 371 | } | 360 | } |
| 372 | 361 | ||
| 373 | ret = 1; | 362 | ret = 1; |
| 374 | builtin_err: | 363 | builtin_err: |
| 375 | if (eckey) | 364 | if (eckey) |
| 376 | EC_KEY_free(eckey); | 365 | EC_KEY_free(eckey); |
| @@ -383,10 +372,10 @@ builtin_err: | |||
| 383 | free(curves); | 372 | free(curves); |
| 384 | 373 | ||
| 385 | return ret; | 374 | return ret; |
| 386 | } | 375 | } |
| 387 | 376 | ||
| 388 | int main(void) | 377 | int main(void) |
| 389 | { | 378 | { |
| 390 | int ret = 1; | 379 | int ret = 1; |
| 391 | BIO *out; | 380 | BIO *out; |
| 392 | 381 | ||
| @@ -395,13 +384,14 @@ int main(void) | |||
| 395 | ERR_load_crypto_strings(); | 384 | ERR_load_crypto_strings(); |
| 396 | 385 | ||
| 397 | /* the tests */ | 386 | /* the tests */ |
| 398 | if (!test_builtin(out)) goto err; | 387 | if (!test_builtin(out)) |
| 399 | 388 | goto err; | |
| 389 | |||
| 400 | ret = 0; | 390 | ret = 0; |
| 401 | err: | 391 | err: |
| 402 | if (ret) | 392 | if (ret) |
| 403 | BIO_printf(out, "\nECDSA test failed\n"); | 393 | BIO_printf(out, "\nECDSA test failed\n"); |
| 404 | else | 394 | else |
| 405 | BIO_printf(out, "\nECDSA test passed\n"); | 395 | BIO_printf(out, "\nECDSA test passed\n"); |
| 406 | if (ret) | 396 | if (ret) |
| 407 | ERR_print_errors(out); | 397 | ERR_print_errors(out); |
| @@ -412,4 +402,4 @@ err: | |||
| 412 | if (out != NULL) | 402 | if (out != NULL) |
| 413 | BIO_free(out); | 403 | BIO_free(out); |
| 414 | return ret; | 404 | return ret; |
| 415 | } | 405 | } |
