2 * Copyright 2001-2020 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 ec_GFp_simple_group_init
,
29 ec_GFp_simple_group_finish
,
30 ec_GFp_simple_group_clear_finish
,
31 ec_GFp_nist_group_copy
,
32 ec_GFp_nist_group_set_curve
,
33 ec_GFp_simple_group_get_curve
,
34 ec_GFp_simple_group_get_degree
,
35 ec_group_simple_order_bits
,
36 ec_GFp_simple_group_check_discriminant
,
37 ec_GFp_simple_point_init
,
38 ec_GFp_simple_point_finish
,
39 ec_GFp_simple_point_clear_finish
,
40 ec_GFp_simple_point_copy
,
41 ec_GFp_simple_point_set_to_infinity
,
42 ec_GFp_simple_point_set_affine_coordinates
,
43 ec_GFp_simple_point_get_affine_coordinates
,
48 ec_GFp_simple_is_at_infinity
,
49 ec_GFp_simple_is_on_curve
,
51 ec_GFp_simple_make_affine
,
52 ec_GFp_simple_points_make_affine
,
54 0 /* precompute_mult */ ,
55 0 /* have_precompute_mult */ ,
56 ec_GFp_nist_field_mul
,
57 ec_GFp_nist_field_sqr
,
59 ec_GFp_simple_field_inv
,
60 0 /* field_encode */ ,
61 0 /* field_decode */ ,
62 0, /* field_set_to_one */
63 ec_key_simple_priv2oct
,
64 ec_key_simple_oct2priv
,
66 ec_key_simple_generate_key
,
67 ec_key_simple_check_key
,
68 ec_key_simple_generate_public_key
,
71 ecdh_simple_compute_key
,
72 ecdsa_simple_sign_setup
,
73 ecdsa_simple_sign_sig
,
74 ecdsa_simple_verify_sig
,
75 0, /* field_inverse_mod_ord */
76 ec_GFp_simple_blind_coordinates
,
77 ec_GFp_simple_ladder_pre
,
78 ec_GFp_simple_ladder_step
,
79 ec_GFp_simple_ladder_post
85 int ec_GFp_nist_group_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
87 dest
->field_mod_func
= src
->field_mod_func
;
89 return ec_GFp_simple_group_copy(dest
, src
);
92 int ec_GFp_nist_group_set_curve(EC_GROUP
*group
, const BIGNUM
*p
,
93 const BIGNUM
*a
, const BIGNUM
*b
, BN_CTX
*ctx
)
96 BN_CTX
*new_ctx
= NULL
;
99 if ((ctx
= new_ctx
= BN_CTX_new_ex(group
->libctx
)) == NULL
)
104 if (BN_ucmp(BN_get0_nist_prime_192(), p
) == 0)
105 group
->field_mod_func
= BN_nist_mod_192
;
106 else if (BN_ucmp(BN_get0_nist_prime_224(), p
) == 0)
107 group
->field_mod_func
= BN_nist_mod_224
;
108 else if (BN_ucmp(BN_get0_nist_prime_256(), p
) == 0)
109 group
->field_mod_func
= BN_nist_mod_256
;
110 else if (BN_ucmp(BN_get0_nist_prime_384(), p
) == 0)
111 group
->field_mod_func
= BN_nist_mod_384
;
112 else if (BN_ucmp(BN_get0_nist_prime_521(), p
) == 0)
113 group
->field_mod_func
= BN_nist_mod_521
;
115 ERR_raise(ERR_LIB_EC
, EC_R_NOT_A_NIST_PRIME
);
119 ret
= ec_GFp_simple_group_set_curve(group
, p
, a
, b
, ctx
);
123 BN_CTX_free(new_ctx
);
127 int ec_GFp_nist_field_mul(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
128 const BIGNUM
*b
, BN_CTX
*ctx
)
131 BN_CTX
*ctx_new
= NULL
;
133 if (!group
|| !r
|| !a
|| !b
) {
134 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
138 if ((ctx_new
= ctx
= BN_CTX_new_ex(group
->libctx
)) == NULL
)
141 if (!BN_mul(r
, a
, b
, ctx
))
143 if (!group
->field_mod_func(r
, r
, group
->field
, ctx
))
148 BN_CTX_free(ctx_new
);
152 int ec_GFp_nist_field_sqr(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
,
156 BN_CTX
*ctx_new
= NULL
;
158 if (!group
|| !r
|| !a
) {
159 ERR_raise(ERR_LIB_EC
, EC_R_PASSED_NULL_PARAMETER
);
163 if ((ctx_new
= ctx
= BN_CTX_new_ex(group
->libctx
)) == NULL
)
166 if (!BN_sqr(r
, a
, ctx
))
168 if (!group
->field_mod_func(r
, r
, group
->field
, ctx
))
173 BN_CTX_free(ctx_new
);