]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ecp_nist.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / ec / ecp_nist.c
1 /*
2 * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 *
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
9 */
10
11 /*
12 * ECDSA low level APIs are deprecated for public use, but still ok for
13 * internal use.
14 */
15 #include "internal/deprecated.h"
16
17 #include <limits.h>
18
19 #include <openssl/err.h>
20 #include <openssl/obj_mac.h>
21 #include "ec_local.h"
22
23 const EC_METHOD *EC_GFp_nist_method(void)
24 {
25 static const EC_METHOD ret = {
26 EC_FLAGS_DEFAULT_OCT,
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,
44 0, 0, 0,
45 ec_GFp_simple_add,
46 ec_GFp_simple_dbl,
47 ec_GFp_simple_invert,
48 ec_GFp_simple_is_at_infinity,
49 ec_GFp_simple_is_on_curve,
50 ec_GFp_simple_cmp,
51 ec_GFp_simple_make_affine,
52 ec_GFp_simple_points_make_affine,
53 0 /* mul */ ,
54 0 /* precompute_mult */ ,
55 0 /* have_precompute_mult */ ,
56 ec_GFp_nist_field_mul,
57 ec_GFp_nist_field_sqr,
58 0 /* field_div */ ,
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,
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 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
80 };
81
82 return &ret;
83 }
84
85 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
86 {
87 dest->field_mod_func = src->field_mod_func;
88
89 return ec_GFp_simple_group_copy(dest, src);
90 }
91
92 int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
93 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
94 {
95 int ret = 0;
96 BN_CTX *new_ctx = NULL;
97
98 if (ctx == NULL)
99 if ((ctx = new_ctx = BN_CTX_new_ex(group->libctx)) == NULL)
100 return 0;
101
102 BN_CTX_start(ctx);
103
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;
114 else {
115 ERR_raise(ERR_LIB_EC, EC_R_NOT_A_NIST_PRIME);
116 goto err;
117 }
118
119 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
120
121 err:
122 BN_CTX_end(ctx);
123 BN_CTX_free(new_ctx);
124 return ret;
125 }
126
127 int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
128 const BIGNUM *b, BN_CTX *ctx)
129 {
130 int ret = 0;
131 BN_CTX *ctx_new = NULL;
132
133 if (!group || !r || !a || !b) {
134 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
135 goto err;
136 }
137 if (!ctx)
138 if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
139 goto err;
140
141 if (!BN_mul(r, a, b, ctx))
142 goto err;
143 if (!group->field_mod_func(r, r, group->field, ctx))
144 goto err;
145
146 ret = 1;
147 err:
148 BN_CTX_free(ctx_new);
149 return ret;
150 }
151
152 int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
153 BN_CTX *ctx)
154 {
155 int ret = 0;
156 BN_CTX *ctx_new = NULL;
157
158 if (!group || !r || !a) {
159 ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
160 goto err;
161 }
162 if (!ctx)
163 if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
164 goto err;
165
166 if (!BN_sqr(r, a, ctx))
167 goto err;
168 if (!group->field_mod_func(r, r, group->field, ctx))
169 goto err;
170
171 ret = 1;
172 err:
173 BN_CTX_free(ctx_new);
174 return ret;
175 }