2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 * ECDSA low level APIs are deprecated for public use, but still ok for
15 #include "internal/deprecated.h"
19 #include <openssl/err.h>
20 #include <openssl/obj_mac.h>
23 const EC_METHOD
*EC_GFp_nist_method(void)
25 static const EC_METHOD ret
= {
27 NID_X9_62_prime_field
,
28 ossl_ec_GFp_simple_group_init
,
29 ossl_ec_GFp_simple_group_finish
,
30 ossl_ec_GFp_simple_group_clear_finish
,
31 ossl_ec_GFp_nist_group_copy
,
32 ossl_ec_GFp_nist_group_set_curve
,
33 ossl_ec_GFp_simple_group_get_curve
,
34 ossl_ec_GFp_simple_group_get_degree
,
35 ossl_ec_group_simple_order_bits
,
36 ossl_ec_GFp_simple_group_check_discriminant
,
37 ossl_ec_GFp_simple_point_init
,
38 ossl_ec_GFp_simple_point_finish
,
39 ossl_ec_GFp_simple_point_clear_finish
,
40 ossl_ec_GFp_simple_point_copy
,
41 ossl_ec_GFp_simple_point_set_to_infinity
,
42 ossl_ec_GFp_simple_point_set_affine_coordinates
,
43 ossl_ec_GFp_simple_point_get_affine_coordinates
,
45 ossl_ec_GFp_simple_add
,
46 ossl_ec_GFp_simple_dbl
,
47 ossl_ec_GFp_simple_invert
,
48 ossl_ec_GFp_simple_is_at_infinity
,
49 ossl_ec_GFp_simple_is_on_curve
,
50 ossl_ec_GFp_simple_cmp
,
51 ossl_ec_GFp_simple_make_affine
,
52 ossl_ec_GFp_simple_points_make_affine
,
54 0 /* precompute_mult */ ,
55 0 /* have_precompute_mult */ ,
56 ossl_ec_GFp_nist_field_mul
,
57 ossl_ec_GFp_nist_field_sqr
,
59 ossl_ec_GFp_simple_field_inv
,
60 0 /* field_encode */ ,
61 0 /* field_decode */ ,
62 0, /* field_set_to_one */
63 ossl_ec_key_simple_priv2oct
,
64 ossl_ec_key_simple_oct2priv
,
66 ossl_ec_key_simple_generate_key
,
67 ossl_ec_key_simple_check_key
,
68 ossl_ec_key_simple_generate_public_key
,
71 ossl_ecdh_simple_compute_key
,
72 ossl_ecdsa_simple_sign_setup
,
73 ossl_ecdsa_simple_sign_sig
,
74 ossl_ecdsa_simple_verify_sig
,
75 0, /* field_inverse_mod_ord */
76 ossl_ec_GFp_simple_blind_coordinates
,
77 ossl_ec_GFp_simple_ladder_pre
,
78 ossl_ec_GFp_simple_ladder_step
,
79 ossl_ec_GFp_simple_ladder_post
85 int ossl_ec_GFp_nist_group_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
87 dest
->field_mod_func
= src
->field_mod_func
;
89 return ossl_ec_GFp_simple_group_copy(dest
, src
);
92 int ossl_ec_GFp_nist_group_set_curve(EC_GROUP
*group
, const BIGNUM
*p
,
93 const BIGNUM
*a
, const BIGNUM
*b
,
97 BN_CTX
*new_ctx
= NULL
;
100 if ((ctx
= new_ctx
= BN_CTX_new_ex(group
->libctx
)) == NULL
)
105 if (BN_ucmp(BN_get0_nist_prime_192(), p
) == 0)
106 group
->field_mod_func
= BN_nist_mod_192
;
107 else if (BN_ucmp(BN_get0_nist_prime_224(), p
) == 0)
108 group
->field_mod_func
= BN_nist_mod_224
;
109 else if (BN_ucmp(BN_get0_nist_prime_256(), p
) == 0)
110 group
->field_mod_func
= BN_nist_mod_256
;
111 else if (BN_ucmp(BN_get0_nist_prime_384(), p
) == 0)
112 group
->field_mod_func
= BN_nist_mod_384
;
113 else if (BN_ucmp(BN_get0_nist_prime_521(), p
) == 0)
114 group
->field_mod_func
= BN_nist_mod_521
;
116 ERR_raise(ERR_LIB_EC
, EC_R_NOT_A_NIST_PRIME
);
120 ret
= ossl_ec_GFp_simple_group_set_curve(group
, p
, a
, b
, ctx
);
124 BN_CTX_free(new_ctx
);
128 int ossl_ec_GFp_nist_field_mul(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
129 const BIGNUM
*b
, BN_CTX
*ctx
)
132 BN_CTX
*ctx_new
= NULL
;
134 if (!group
|| !r
|| !a
|| !b
) {
135 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
139 if ((ctx_new
= ctx
= BN_CTX_new_ex(group
->libctx
)) == NULL
)
142 if (!BN_mul(r
, a
, b
, ctx
))
144 if (!group
->field_mod_func(r
, r
, group
->field
, ctx
))
149 BN_CTX_free(ctx_new
);
153 int ossl_ec_GFp_nist_field_sqr(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
157 BN_CTX
*ctx_new
= NULL
;
159 if (!group
|| !r
|| !a
) {
160 ERR_raise(ERR_LIB_EC
, EC_R_PASSED_NULL_PARAMETER
);
164 if ((ctx_new
= ctx
= BN_CTX_new_ex(group
->libctx
)) == NULL
)
167 if (!BN_sqr(r
, a
, ctx
))
169 if (!group
->field_mod_func(r
, r
, group
->field
, ctx
))
174 BN_CTX_free(ctx_new
);