[331] | 1 | /*
|
---|
| 2 | * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (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 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
|
---|
| 12 | * Portions of this software developed by SUN MICROSYSTEMS, INC.,
|
---|
| 13 | * and contributed to the OpenSSL project.
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | #include <limits.h>
|
---|
| 17 |
|
---|
| 18 | #include <openssl/err.h>
|
---|
| 19 | #include <openssl/obj_mac.h>
|
---|
| 20 | #include "ec_lcl.h"
|
---|
| 21 |
|
---|
| 22 | const EC_METHOD *EC_GFp_nist_method(void)
|
---|
| 23 | {
|
---|
| 24 | static const EC_METHOD ret = {
|
---|
| 25 | EC_FLAGS_DEFAULT_OCT,
|
---|
| 26 | NID_X9_62_prime_field,
|
---|
| 27 | ec_GFp_simple_group_init,
|
---|
| 28 | ec_GFp_simple_group_finish,
|
---|
| 29 | ec_GFp_simple_group_clear_finish,
|
---|
| 30 | ec_GFp_nist_group_copy,
|
---|
| 31 | ec_GFp_nist_group_set_curve,
|
---|
| 32 | ec_GFp_simple_group_get_curve,
|
---|
| 33 | ec_GFp_simple_group_get_degree,
|
---|
| 34 | ec_group_simple_order_bits,
|
---|
| 35 | ec_GFp_simple_group_check_discriminant,
|
---|
| 36 | ec_GFp_simple_point_init,
|
---|
| 37 | ec_GFp_simple_point_finish,
|
---|
| 38 | ec_GFp_simple_point_clear_finish,
|
---|
| 39 | ec_GFp_simple_point_copy,
|
---|
| 40 | ec_GFp_simple_point_set_to_infinity,
|
---|
| 41 | ec_GFp_simple_set_Jprojective_coordinates_GFp,
|
---|
| 42 | ec_GFp_simple_get_Jprojective_coordinates_GFp,
|
---|
| 43 | ec_GFp_simple_point_set_affine_coordinates,
|
---|
| 44 | ec_GFp_simple_point_get_affine_coordinates,
|
---|
| 45 | 0, 0, 0,
|
---|
| 46 | ec_GFp_simple_add,
|
---|
| 47 | ec_GFp_simple_dbl,
|
---|
| 48 | ec_GFp_simple_invert,
|
---|
| 49 | ec_GFp_simple_is_at_infinity,
|
---|
| 50 | ec_GFp_simple_is_on_curve,
|
---|
| 51 | ec_GFp_simple_cmp,
|
---|
| 52 | ec_GFp_simple_make_affine,
|
---|
| 53 | ec_GFp_simple_points_make_affine,
|
---|
| 54 | 0 /* mul */ ,
|
---|
| 55 | 0 /* precompute_mult */ ,
|
---|
| 56 | 0 /* have_precompute_mult */ ,
|
---|
| 57 | ec_GFp_nist_field_mul,
|
---|
| 58 | ec_GFp_nist_field_sqr,
|
---|
| 59 | 0 /* field_div */ ,
|
---|
| 60 | 0 /* field_encode */ ,
|
---|
| 61 | 0 /* field_decode */ ,
|
---|
| 62 | 0, /* field_set_to_one */
|
---|
| 63 | ec_key_simple_priv2oct,
|
---|
| 64 | ec_key_simple_oct2priv,
|
---|
| 65 | 0, /* set private */
|
---|
| 66 | ec_key_simple_generate_key,
|
---|
| 67 | ec_key_simple_check_key,
|
---|
| 68 | ec_key_simple_generate_public_key,
|
---|
| 69 | 0, /* keycopy */
|
---|
| 70 | 0, /* keyfinish */
|
---|
| 71 | ecdh_simple_compute_key
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 | return &ret;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
|
---|
| 78 | {
|
---|
| 79 | dest->field_mod_func = src->field_mod_func;
|
---|
| 80 |
|
---|
| 81 | return ec_GFp_simple_group_copy(dest, src);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
|
---|
| 85 | const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
|
---|
| 86 | {
|
---|
| 87 | int ret = 0;
|
---|
| 88 | BN_CTX *new_ctx = NULL;
|
---|
| 89 |
|
---|
| 90 | if (ctx == NULL)
|
---|
| 91 | if ((ctx = new_ctx = BN_CTX_new()) == NULL)
|
---|
| 92 | return 0;
|
---|
| 93 |
|
---|
| 94 | BN_CTX_start(ctx);
|
---|
| 95 |
|
---|
| 96 | if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0)
|
---|
| 97 | group->field_mod_func = BN_nist_mod_192;
|
---|
| 98 | else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0)
|
---|
| 99 | group->field_mod_func = BN_nist_mod_224;
|
---|
| 100 | else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0)
|
---|
| 101 | group->field_mod_func = BN_nist_mod_256;
|
---|
| 102 | else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0)
|
---|
| 103 | group->field_mod_func = BN_nist_mod_384;
|
---|
| 104 | else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0)
|
---|
| 105 | group->field_mod_func = BN_nist_mod_521;
|
---|
| 106 | else {
|
---|
| 107 | ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME);
|
---|
| 108 | goto err;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
|
---|
| 112 |
|
---|
| 113 | err:
|
---|
| 114 | BN_CTX_end(ctx);
|
---|
| 115 | BN_CTX_free(new_ctx);
|
---|
| 116 | return ret;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
|
---|
| 120 | const BIGNUM *b, BN_CTX *ctx)
|
---|
| 121 | {
|
---|
| 122 | int ret = 0;
|
---|
| 123 | BN_CTX *ctx_new = NULL;
|
---|
| 124 |
|
---|
| 125 | if (!group || !r || !a || !b) {
|
---|
| 126 | ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 127 | goto err;
|
---|
| 128 | }
|
---|
| 129 | if (!ctx)
|
---|
| 130 | if ((ctx_new = ctx = BN_CTX_new()) == NULL)
|
---|
| 131 | goto err;
|
---|
| 132 |
|
---|
| 133 | if (!BN_mul(r, a, b, ctx))
|
---|
| 134 | goto err;
|
---|
| 135 | if (!group->field_mod_func(r, r, group->field, ctx))
|
---|
| 136 | goto err;
|
---|
| 137 |
|
---|
| 138 | ret = 1;
|
---|
| 139 | err:
|
---|
| 140 | BN_CTX_free(ctx_new);
|
---|
| 141 | return ret;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
|
---|
| 145 | BN_CTX *ctx)
|
---|
| 146 | {
|
---|
| 147 | int ret = 0;
|
---|
| 148 | BN_CTX *ctx_new = NULL;
|
---|
| 149 |
|
---|
| 150 | if (!group || !r || !a) {
|
---|
| 151 | ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);
|
---|
| 152 | goto err;
|
---|
| 153 | }
|
---|
| 154 | if (!ctx)
|
---|
| 155 | if ((ctx_new = ctx = BN_CTX_new()) == NULL)
|
---|
| 156 | goto err;
|
---|
| 157 |
|
---|
| 158 | if (!BN_sqr(r, a, ctx))
|
---|
| 159 | goto err;
|
---|
| 160 | if (!group->field_mod_func(r, r, group->field, ctx))
|
---|
| 161 | goto err;
|
---|
| 162 |
|
---|
| 163 | ret = 1;
|
---|
| 164 | err:
|
---|
| 165 | BN_CTX_free(ctx_new);
|
---|
| 166 | return ret;
|
---|
| 167 | }
|
---|