Home | History | Annotate | Line # | Download | only in test
      1 /*
      2  * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  * this file except in compliance with the License.  You can obtain a copy
      6  * in the file LICENSE in the source distribution or at
      7  * https://www.openssl.org/source/license.html
      8  */
      9 
     10 /*
     11  * Low level APIs are deprecated for public use, but still ok for internal use.
     12  */
     13 #include "internal/deprecated.h"
     14 
     15 #include "internal/nelem.h"
     16 #include "testutil.h"
     17 #include <openssl/ec.h>
     18 #include "ec_local.h"
     19 #include <crypto/bn.h>
     20 #include <openssl/objects.h>
     21 
     22 static size_t crv_len = 0;
     23 static EC_builtin_curve *curves = NULL;
     24 
     25 /* sanity checks field_inv function pointer in EC_METHOD */
     26 static int group_field_tests(const EC_GROUP *group, BN_CTX *ctx)
     27 {
     28     BIGNUM *a = NULL, *b = NULL, *c = NULL;
     29     int ret = 0;
     30 
     31     if (group->meth->field_inv == NULL || group->meth->field_mul == NULL)
     32         return 1;
     33 
     34     BN_CTX_start(ctx);
     35     a = BN_CTX_get(ctx);
     36     b = BN_CTX_get(ctx);
     37     if (!TEST_ptr(c = BN_CTX_get(ctx))
     38         /* 1/1 = 1 */
     39         || !TEST_true(group->meth->field_inv(group, b, BN_value_one(), ctx))
     40         || !TEST_true(BN_is_one(b))
     41         /* (1/a)*a = 1 */
     42         || !TEST_true(BN_rand(a, BN_num_bits(group->field) - 1,
     43             BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
     44         || !TEST_true(group->meth->field_inv(group, b, a, ctx))
     45         || (group->meth->field_encode && !TEST_true(group->meth->field_encode(group, a, a, ctx)))
     46         || (group->meth->field_encode && !TEST_true(group->meth->field_encode(group, b, b, ctx)))
     47         || !TEST_true(group->meth->field_mul(group, c, a, b, ctx))
     48         || (group->meth->field_decode && !TEST_true(group->meth->field_decode(group, c, c, ctx)))
     49         || !TEST_true(BN_is_one(c)))
     50         goto err;
     51 
     52     /* 1/0 = error */
     53     BN_zero(a);
     54     if (!TEST_false(group->meth->field_inv(group, b, a, ctx))
     55         || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)
     56         || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) == EC_R_CANNOT_INVERT)
     57         /* 1/p = error */
     58         || !TEST_false(group->meth->field_inv(group, b, group->field, ctx))
     59         || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)
     60         || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) == EC_R_CANNOT_INVERT))
     61         goto err;
     62 
     63     ERR_clear_error();
     64     ret = 1;
     65 err:
     66     BN_CTX_end(ctx);
     67     return ret;
     68 }
     69 
     70 /* wrapper for group_field_tests for explicit curve params and EC_METHOD */
     71 static int field_tests(const EC_METHOD *meth, const unsigned char *params,
     72     int len)
     73 {
     74     BN_CTX *ctx = NULL;
     75     BIGNUM *p = NULL, *a = NULL, *b = NULL;
     76     EC_GROUP *group = NULL;
     77     int ret = 0;
     78 
     79     if (!TEST_ptr(ctx = BN_CTX_new()))
     80         return 0;
     81 
     82     BN_CTX_start(ctx);
     83     p = BN_CTX_get(ctx);
     84     a = BN_CTX_get(ctx);
     85     if (!TEST_ptr(b = BN_CTX_get(ctx))
     86         || !TEST_ptr(group = EC_GROUP_new(meth))
     87         || !TEST_true(BN_bin2bn(params, len, p))
     88         || !TEST_true(BN_bin2bn(params + len, len, a))
     89         || !TEST_true(BN_bin2bn(params + 2 * len, len, b))
     90         || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
     91         || !group_field_tests(group, ctx))
     92         goto err;
     93     ret = 1;
     94 
     95 err:
     96     BN_CTX_end(ctx);
     97     BN_CTX_free(ctx);
     98     if (group != NULL)
     99         EC_GROUP_free(group);
    100     return ret;
    101 }
    102 
    103 /* NIST prime curve P-256 */
    104 static const unsigned char params_p256[] = {
    105     /* p */
    106     0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
    107     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    108     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    109     /* a */
    110     0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
    111     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    112     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC,
    113     /* b */
    114     0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7, 0xB3, 0xEB, 0xBD, 0x55,
    115     0x76, 0x98, 0x86, 0xBC, 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
    116     0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B
    117 };
    118 
    119 #ifndef OPENSSL_NO_EC2M
    120 /* NIST binary curve B-283 */
    121 static const unsigned char params_b283[] = {
    122     /* p */
    123     0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    124     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    125     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xA1,
    126     /* a */
    127     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    128     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    129     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
    130     /* b */
    131     0x02, 0x7B, 0x68, 0x0A, 0xC8, 0xB8, 0x59, 0x6D, 0xA5, 0xA4, 0xAF, 0x8A,
    132     0x19, 0xA0, 0x30, 0x3F, 0xCA, 0x97, 0xFD, 0x76, 0x45, 0x30, 0x9F, 0xA2,
    133     0xA5, 0x81, 0x48, 0x5A, 0xF6, 0x26, 0x3E, 0x31, 0x3B, 0x79, 0xA2, 0xF5
    134 };
    135 #endif
    136 
    137 /* test EC_GFp_simple_method directly */
    138 static int field_tests_ecp_simple(void)
    139 {
    140     TEST_info("Testing EC_GFp_simple_method()\n");
    141     return field_tests(EC_GFp_simple_method(), params_p256,
    142         sizeof(params_p256) / 3);
    143 }
    144 
    145 /* test EC_GFp_mont_method directly */
    146 static int field_tests_ecp_mont(void)
    147 {
    148     TEST_info("Testing EC_GFp_mont_method()\n");
    149     return field_tests(EC_GFp_mont_method(), params_p256,
    150         sizeof(params_p256) / 3);
    151 }
    152 
    153 #ifndef OPENSSL_NO_EC2M
    154 /* Test that decoding of invalid GF2m field parameters fails. */
    155 static int ec2m_field_sanity(void)
    156 {
    157     int ret = 0;
    158     BN_CTX *ctx = BN_CTX_new();
    159     BIGNUM *p, *a, *b;
    160     EC_GROUP *group1 = NULL, *group2 = NULL, *group3 = NULL;
    161 
    162     TEST_info("Testing GF2m hardening\n");
    163 
    164     BN_CTX_start(ctx);
    165     p = BN_CTX_get(ctx);
    166     a = BN_CTX_get(ctx);
    167     if (!TEST_ptr(b = BN_CTX_get(ctx))
    168         || !TEST_true(BN_one(a))
    169         || !TEST_true(BN_one(b)))
    170         goto out;
    171 
    172     /* Even pentanomial value should be rejected */
    173     if (!TEST_true(BN_set_word(p, 0xf2)))
    174         goto out;
    175     if (!TEST_ptr_null(group1 = EC_GROUP_new_curve_GF2m(p, a, b, ctx)))
    176         TEST_error("Zero constant term accepted in GF2m polynomial");
    177 
    178     /* Odd hexanomial should also be rejected */
    179     if (!TEST_true(BN_set_word(p, 0xf3)))
    180         goto out;
    181     if (!TEST_ptr_null(group2 = EC_GROUP_new_curve_GF2m(p, a, b, ctx)))
    182         TEST_error("Hexanomial accepted as GF2m polynomial");
    183 
    184     /* Excessive polynomial degree should also be rejected */
    185     if (!TEST_true(BN_set_word(p, 0x71))
    186         || !TEST_true(BN_set_bit(p, OPENSSL_ECC_MAX_FIELD_BITS + 1)))
    187         goto out;
    188     if (!TEST_ptr_null(group3 = EC_GROUP_new_curve_GF2m(p, a, b, ctx)))
    189         TEST_error("GF2m polynomial degree > %d accepted",
    190             OPENSSL_ECC_MAX_FIELD_BITS);
    191 
    192     ret = group1 == NULL && group2 == NULL && group3 == NULL;
    193 
    194 out:
    195     EC_GROUP_free(group1);
    196     EC_GROUP_free(group2);
    197     EC_GROUP_free(group3);
    198     BN_CTX_end(ctx);
    199     BN_CTX_free(ctx);
    200 
    201     return ret;
    202 }
    203 
    204 /* test EC_GF2m_simple_method directly */
    205 static int field_tests_ec2_simple(void)
    206 {
    207     TEST_info("Testing EC_GF2m_simple_method()\n");
    208     return field_tests(EC_GF2m_simple_method(), params_b283,
    209         sizeof(params_b283) / 3);
    210 }
    211 #endif
    212 
    213 /* test default method for a named curve */
    214 static int field_tests_default(int n)
    215 {
    216     BN_CTX *ctx = NULL;
    217     EC_GROUP *group = NULL;
    218     int nid = curves[n].nid;
    219     int ret = 0;
    220 
    221     TEST_info("Testing curve %s\n", OBJ_nid2sn(nid));
    222 
    223     if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
    224         || !TEST_ptr(ctx = BN_CTX_new())
    225         || !group_field_tests(group, ctx))
    226         goto err;
    227 
    228     ret = 1;
    229 err:
    230     if (group != NULL)
    231         EC_GROUP_free(group);
    232     if (ctx != NULL)
    233         BN_CTX_free(ctx);
    234     return ret;
    235 }
    236 
    237 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
    238 /*
    239  * Tests a point known to cause an incorrect underflow in an old version of
    240  * ecp_nist521.c
    241  */
    242 static int underflow_test(void)
    243 {
    244     BN_CTX *ctx = NULL;
    245     EC_GROUP *grp = NULL;
    246     EC_POINT *P = NULL, *Q = NULL, *R = NULL;
    247     BIGNUM *x1 = NULL, *y1 = NULL, *z1 = NULL, *x2 = NULL, *y2 = NULL;
    248     BIGNUM *k = NULL;
    249     int testresult = 0;
    250     const char *x1str = "1534f0077fffffe87e9adcfe000000000000000000003e05a21d2400002e031b1f4"
    251                         "b80000c6fafa4f3c1288798d624a247b5e2ffffffffffffffefe099241900004";
    252     const char *p521m1 = "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
    253                          "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe";
    254 
    255     ctx = BN_CTX_new();
    256     if (!TEST_ptr(ctx))
    257         return 0;
    258 
    259     BN_CTX_start(ctx);
    260     x1 = BN_CTX_get(ctx);
    261     y1 = BN_CTX_get(ctx);
    262     z1 = BN_CTX_get(ctx);
    263     x2 = BN_CTX_get(ctx);
    264     y2 = BN_CTX_get(ctx);
    265     k = BN_CTX_get(ctx);
    266     if (!TEST_ptr(k))
    267         goto err;
    268 
    269     grp = EC_GROUP_new_by_curve_name(NID_secp521r1);
    270     P = EC_POINT_new(grp);
    271     Q = EC_POINT_new(grp);
    272     R = EC_POINT_new(grp);
    273     if (!TEST_ptr(grp) || !TEST_ptr(P) || !TEST_ptr(Q) || !TEST_ptr(R))
    274         goto err;
    275 
    276     if (!TEST_int_gt(BN_hex2bn(&x1, x1str), 0)
    277         || !TEST_int_gt(BN_hex2bn(&y1, p521m1), 0)
    278         || !TEST_int_gt(BN_hex2bn(&z1, p521m1), 0)
    279         || !TEST_int_gt(BN_hex2bn(&k, "02"), 0)
    280         || !TEST_true(ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(grp, P, x1,
    281             y1, z1, ctx))
    282         || !TEST_true(EC_POINT_mul(grp, Q, NULL, P, k, ctx))
    283         || !TEST_true(EC_POINT_get_affine_coordinates(grp, Q, x1, y1, ctx))
    284         || !TEST_true(EC_POINT_dbl(grp, R, P, ctx))
    285         || !TEST_true(EC_POINT_get_affine_coordinates(grp, R, x2, y2, ctx)))
    286         goto err;
    287 
    288     if (!TEST_int_eq(BN_cmp(x1, x2), 0)
    289         || !TEST_int_eq(BN_cmp(y1, y2), 0))
    290         goto err;
    291 
    292     testresult = 1;
    293 
    294 err:
    295     BN_CTX_end(ctx);
    296     EC_POINT_free(P);
    297     EC_POINT_free(Q);
    298     EC_POINT_free(R);
    299     EC_GROUP_free(grp);
    300     BN_CTX_free(ctx);
    301 
    302     return testresult;
    303 }
    304 #endif
    305 
    306 /*
    307  * Tests behavior of the EC_KEY_set_private_key
    308  */
    309 static int set_private_key(void)
    310 {
    311     EC_KEY *key = NULL, *aux_key = NULL;
    312     int testresult = 0;
    313 
    314     key = EC_KEY_new_by_curve_name(NID_secp224r1);
    315     aux_key = EC_KEY_new_by_curve_name(NID_secp224r1);
    316     if (!TEST_ptr(key)
    317         || !TEST_ptr(aux_key)
    318         || !TEST_int_eq(EC_KEY_generate_key(key), 1)
    319         || !TEST_int_eq(EC_KEY_generate_key(aux_key), 1))
    320         goto err;
    321 
    322     /* Test setting a valid private key */
    323     if (!TEST_int_eq(EC_KEY_set_private_key(key, aux_key->priv_key), 1))
    324         goto err;
    325 
    326     /* Test compliance with legacy behavior for NULL private keys */
    327     if (!TEST_int_eq(EC_KEY_set_private_key(key, NULL), 0)
    328         || !TEST_ptr_null(key->priv_key))
    329         goto err;
    330 
    331     testresult = 1;
    332 
    333 err:
    334     EC_KEY_free(key);
    335     EC_KEY_free(aux_key);
    336     return testresult;
    337 }
    338 
    339 /*
    340  * Tests behavior of the decoded_from_explicit_params flag and API
    341  */
    342 static int decoded_flag_test(void)
    343 {
    344     EC_GROUP *grp;
    345     EC_GROUP *grp_copy = NULL;
    346     ECPARAMETERS *ecparams = NULL;
    347     ECPKPARAMETERS *ecpkparams = NULL;
    348     EC_KEY *key = NULL;
    349     unsigned char *encodedparams = NULL;
    350     const unsigned char *encp;
    351     int encodedlen;
    352     int testresult = 0;
    353 
    354     /* Test EC_GROUP_new not setting the flag */
    355     grp = EC_GROUP_new(EC_GFp_simple_method());
    356     if (!TEST_ptr(grp)
    357         || !TEST_int_eq(grp->decoded_from_explicit_params, 0))
    358         goto err;
    359     EC_GROUP_free(grp);
    360 
    361     /* Test EC_GROUP_new_by_curve_name not setting the flag */
    362     grp = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
    363     if (!TEST_ptr(grp)
    364         || !TEST_int_eq(grp->decoded_from_explicit_params, 0))
    365         goto err;
    366 
    367     /* Test EC_GROUP_new_from_ecparameters not setting the flag */
    368     if (!TEST_ptr(ecparams = EC_GROUP_get_ecparameters(grp, NULL))
    369         || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecparameters(ecparams))
    370         || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0))
    371         goto err;
    372     EC_GROUP_free(grp_copy);
    373     grp_copy = NULL;
    374     ECPARAMETERS_free(ecparams);
    375     ecparams = NULL;
    376 
    377     /* Test EC_GROUP_new_from_ecpkparameters not setting the flag */
    378     if (!TEST_int_eq(EC_GROUP_get_asn1_flag(grp), OPENSSL_EC_NAMED_CURVE)
    379         || !TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL))
    380         || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams))
    381         || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0)
    382         || !TEST_ptr(key = EC_KEY_new())
    383         /* Test EC_KEY_decoded_from_explicit_params on key without a group */
    384         || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), -1)
    385         || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1)
    386         /* Test EC_KEY_decoded_from_explicit_params negative case */
    387         || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 0))
    388         goto err;
    389     EC_GROUP_free(grp_copy);
    390     grp_copy = NULL;
    391     ECPKPARAMETERS_free(ecpkparams);
    392     ecpkparams = NULL;
    393 
    394     /* Test d2i_ECPKParameters with named params not setting the flag */
    395     if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0)
    396         || !TEST_ptr(encp = encodedparams)
    397         || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen))
    398         || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0))
    399         goto err;
    400     EC_GROUP_free(grp_copy);
    401     grp_copy = NULL;
    402     OPENSSL_free(encodedparams);
    403     encodedparams = NULL;
    404 
    405     /* Asn1 flag stays set to explicit with EC_GROUP_new_from_ecpkparameters */
    406     EC_GROUP_set_asn1_flag(grp, OPENSSL_EC_EXPLICIT_CURVE);
    407     if (!TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL))
    408         || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams))
    409         || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE)
    410         || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0))
    411         goto err;
    412     EC_GROUP_free(grp_copy);
    413     grp_copy = NULL;
    414 
    415     /* Test d2i_ECPKParameters with explicit params setting the flag */
    416     if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0)
    417         || !TEST_ptr(encp = encodedparams)
    418         || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen))
    419         || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE)
    420         || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 1)
    421         || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1)
    422         /* Test EC_KEY_decoded_from_explicit_params positive case */
    423         || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 1))
    424         goto err;
    425 
    426     testresult = 1;
    427 
    428 err:
    429     EC_KEY_free(key);
    430     EC_GROUP_free(grp);
    431     EC_GROUP_free(grp_copy);
    432     ECPARAMETERS_free(ecparams);
    433     ECPKPARAMETERS_free(ecpkparams);
    434     OPENSSL_free(encodedparams);
    435 
    436     return testresult;
    437 }
    438 
    439 static int ecpkparams_i2d2i_test(int n)
    440 {
    441     EC_GROUP *g1 = NULL, *g2 = NULL;
    442     FILE *fp = NULL;
    443     int nid = curves[n].nid;
    444     int testresult = 0;
    445 
    446     /* create group */
    447     if (!TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid)))
    448         goto end;
    449 
    450     /* encode params to file */
    451     if (!TEST_ptr(fp = fopen("params.der", "wb"))
    452         || !TEST_true(i2d_ECPKParameters_fp(fp, g1)))
    453         goto end;
    454 
    455     /* flush and close file */
    456     if (!TEST_int_eq(fclose(fp), 0)) {
    457         fp = NULL;
    458         goto end;
    459     }
    460     fp = NULL;
    461 
    462     /* decode params from file */
    463     if (!TEST_ptr(fp = fopen("params.der", "rb"))
    464         || !TEST_ptr(g2 = d2i_ECPKParameters_fp(fp, NULL)))
    465         goto end;
    466 
    467     testresult = 1; /* PASS */
    468 
    469 end:
    470     if (fp != NULL)
    471         fclose(fp);
    472 
    473     EC_GROUP_free(g1);
    474     EC_GROUP_free(g2);
    475 
    476     return testresult;
    477 }
    478 
    479 static int check_bn_mont_ctx(BN_MONT_CTX *mont, BIGNUM *mod, BN_CTX *ctx)
    480 {
    481     int ret = 0;
    482     BN_MONT_CTX *regenerated = BN_MONT_CTX_new();
    483 
    484     if (!TEST_ptr(regenerated))
    485         return ret;
    486     if (!TEST_ptr(mont))
    487         goto err;
    488 
    489     if (!TEST_true(BN_MONT_CTX_set(regenerated, mod, ctx)))
    490         goto err;
    491 
    492     if (!TEST_true(ossl_bn_mont_ctx_eq(regenerated, mont)))
    493         goto err;
    494 
    495     ret = 1;
    496 
    497 err:
    498     BN_MONT_CTX_free(regenerated);
    499     return ret;
    500 }
    501 
    502 static int montgomery_correctness_test(EC_GROUP *group)
    503 {
    504     int ret = 0;
    505     BN_CTX *ctx = NULL;
    506 
    507     ctx = BN_CTX_new();
    508     if (!TEST_ptr(ctx))
    509         return ret;
    510     if (!TEST_true(check_bn_mont_ctx(group->mont_data, group->order, ctx))) {
    511         TEST_error("group order issue");
    512         goto err;
    513     }
    514     if (group->field_data1 != NULL) {
    515         if (!TEST_true(check_bn_mont_ctx(group->field_data1, group->field, ctx)))
    516             goto err;
    517     }
    518     ret = 1;
    519 err:
    520     BN_CTX_free(ctx);
    521     return ret;
    522 }
    523 
    524 static int named_group_creation_test(void)
    525 {
    526     int ret = 0;
    527     EC_GROUP *group = NULL;
    528 
    529     if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1))
    530         || !TEST_true(montgomery_correctness_test(group)))
    531         goto err;
    532 
    533     ret = 1;
    534 
    535 err:
    536     EC_GROUP_free(group);
    537     return ret;
    538 }
    539 
    540 int setup_tests(void)
    541 {
    542     crv_len = EC_get_builtin_curves(NULL, 0);
    543     if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
    544         || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
    545         return 0;
    546 
    547     ADD_TEST(field_tests_ecp_simple);
    548     ADD_TEST(field_tests_ecp_mont);
    549 #ifndef OPENSSL_NO_EC2M
    550     ADD_TEST(ec2m_field_sanity);
    551     ADD_TEST(field_tests_ec2_simple);
    552 #endif
    553     ADD_ALL_TESTS(field_tests_default, crv_len);
    554 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
    555     ADD_TEST(underflow_test);
    556 #endif
    557     ADD_TEST(set_private_key);
    558     ADD_TEST(decoded_flag_test);
    559     ADD_ALL_TESTS(ecpkparams_i2d2i_test, crv_len);
    560     ADD_TEST(named_group_creation_test);
    561 
    562     return 1;
    563 }
    564 
    565 void cleanup_tests(void)
    566 {
    567     OPENSSL_free(curves);
    568 }
    569