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