p_ec.c
Go to the documentation of this file.
1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2  * project 2006.
3  */
4 /* ====================================================================
5  * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  * software must display the following acknowledgment:
21  * "This product includes software developed by the OpenSSL Project
22  * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  * endorse or promote products derived from this software without
26  * prior written permission. For written permission, please contact
27  * licensing@OpenSSL.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  * nor may "OpenSSL" appear in their names without prior written
31  * permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  * acknowledgment:
35  * "This product includes software developed by the OpenSSL Project
36  * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com). This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com). */
55 
56 #include <openssl/evp.h>
57 
58 #include <string.h>
59 
60 #include <openssl/bn.h>
61 #include <openssl/digest.h>
62 #include <openssl/ec.h>
63 #include <openssl/ec_key.h>
64 #include <openssl/ecdh.h>
65 #include <openssl/ecdsa.h>
66 #include <openssl/err.h>
67 #include <openssl/mem.h>
68 #include <openssl/nid.h>
69 
70 #include "internal.h"
71 #include "../fipsmodule/ec/internal.h"
72 #include "../internal.h"
73 
74 
75 typedef struct {
76  // message digest
77  const EVP_MD *md;
79 } EC_PKEY_CTX;
80 
81 
83  EC_PKEY_CTX *dctx;
84  dctx = OPENSSL_malloc(sizeof(EC_PKEY_CTX));
85  if (!dctx) {
86  return 0;
87  }
88  OPENSSL_memset(dctx, 0, sizeof(EC_PKEY_CTX));
89 
90  ctx->data = dctx;
91 
92  return 1;
93 }
94 
96  EC_PKEY_CTX *dctx, *sctx;
97  if (!pkey_ec_init(dst)) {
98  return 0;
99  }
100  sctx = src->data;
101  dctx = dst->data;
102 
103  dctx->md = sctx->md;
104 
105  return 1;
106 }
107 
109  EC_PKEY_CTX *dctx = ctx->data;
110  if (!dctx) {
111  return;
112  }
113 
114  EC_GROUP_free(dctx->gen_group);
115  OPENSSL_free(dctx);
116 }
117 
118 static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
119  const uint8_t *tbs, size_t tbslen) {
120  unsigned int sltmp;
121  EC_KEY *ec = ctx->pkey->pkey.ec;
122 
123  if (!sig) {
124  *siglen = ECDSA_size(ec);
125  return 1;
126  } else if (*siglen < (size_t)ECDSA_size(ec)) {
128  return 0;
129  }
130 
131  if (!ECDSA_sign(0, tbs, tbslen, sig, &sltmp, ec)) {
132  return 0;
133  }
134  *siglen = (size_t)sltmp;
135  return 1;
136 }
137 
138 static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
139  const uint8_t *tbs, size_t tbslen) {
140  return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->pkey->pkey.ec);
141 }
142 
144  size_t *keylen) {
145  int ret;
146  size_t outlen;
147  const EC_POINT *pubkey = NULL;
148  EC_KEY *eckey;
149 
150  if (!ctx->pkey || !ctx->peerkey) {
152  return 0;
153  }
154 
155  eckey = ctx->pkey->pkey.ec;
156 
157  if (!key) {
158  const EC_GROUP *group;
159  group = EC_KEY_get0_group(eckey);
160  *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
161  return 1;
162  }
163  pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
164 
165  // NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is
166  // not an error, the result is truncated.
167 
168  outlen = *keylen;
169 
170  ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
171  if (ret < 0) {
172  return 0;
173  }
174  *keylen = ret;
175  return 1;
176 }
177 
178 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
179  EC_PKEY_CTX *dctx = ctx->data;
180 
181  switch (type) {
182  case EVP_PKEY_CTRL_MD:
183  if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
184  EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
185  EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
186  EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
187  EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
188  EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
190  return 0;
191  }
192  dctx->md = p2;
193  return 1;
194 
196  *(const EVP_MD **)p2 = dctx->md;
197  return 1;
198 
200  // Default behaviour is OK
201  return 1;
202 
205  if (group == NULL) {
206  return 0;
207  }
208  EC_GROUP_free(dctx->gen_group);
209  dctx->gen_group = group;
210  return 1;
211  }
212 
213  default:
215  return 0;
216  }
217 }
218 
220  EC_PKEY_CTX *dctx = ctx->data;
221  const EC_GROUP *group = dctx->gen_group;
222  if (group == NULL) {
223  if (ctx->pkey == NULL) {
225  return 0;
226  }
227  group = EC_KEY_get0_group(ctx->pkey->pkey.ec);
228  }
229  EC_KEY *ec = EC_KEY_new();
230  if (ec == NULL ||
231  !EC_KEY_set_group(ec, group) ||
232  !EC_KEY_generate_key(ec)) {
233  EC_KEY_free(ec);
234  return 0;
235  }
236  EVP_PKEY_assign_EC_KEY(pkey, ec);
237  return 1;
238 }
239 
241  EC_PKEY_CTX *dctx = ctx->data;
242  if (dctx->gen_group == NULL) {
244  return 0;
245  }
246  EC_KEY *ec = EC_KEY_new();
247  if (ec == NULL ||
248  !EC_KEY_set_group(ec, dctx->gen_group)) {
249  EC_KEY_free(ec);
250  return 0;
251  }
252  EVP_PKEY_assign_EC_KEY(pkey, ec);
253  return 1;
254 }
255 
257  EVP_PKEY_EC,
258  pkey_ec_init,
259  pkey_ec_copy,
262  pkey_ec_sign,
263  NULL /* sign_message */,
265  NULL /* verify_message */,
266  NULL /* verify_recover */,
267  NULL /* encrypt */,
268  NULL /* decrypt */,
271  pkey_ec_ctrl,
272 };
273 
277 }
278 
280  // BoringSSL only supports named curve syntax.
283  return 0;
284  }
285  return 1;
286 }
bn.h
EVP_PKEY_EC
#define EVP_PKEY_EC
Definition: evp.h:178
EVP_MD_type
#define EVP_MD_type
Definition: boringssl_prefix_symbols.h:1580
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
NID_ecdsa_with_SHA1
#define NID_ecdsa_with_SHA1
Definition: nid.h:1918
EC_KEY_new
#define EC_KEY_new
Definition: boringssl_prefix_symbols.h:1355
ctx
Definition: benchmark-async.c:30
EVP_R_COMMAND_NOT_SUPPORTED
#define EVP_R_COMMAND_NOT_SUPPORTED
Definition: evp_errors.h:61
env_md_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/internal.h:67
setup.encoding
encoding
Definition: third_party/protobuf/python/protobuf_distutils/setup.py:40
pkey_ec_derive
static int pkey_ec_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *keylen)
Definition: p_ec.c:143
EC_KEY_generate_key
#define EC_KEY_generate_key
Definition: boringssl_prefix_symbols.h:1342
evp.h
ecdsa.h
EVP_PKEY_CTRL_PEER_KEY
#define EVP_PKEY_CTRL_PEER_KEY
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:162
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
EC_KEY_set_group
#define EC_KEY_set_group
Definition: boringssl_prefix_symbols.h:1365
ecdh.h
EC_GROUP_new_by_curve_name
#define EC_GROUP_new_by_curve_name
Definition: boringssl_prefix_symbols.h:1331
pkey_ec_copy
static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
Definition: p_ec.c:95
pkey_ec_cleanup
static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
Definition: p_ec.c:108
NID_sha384
#define NID_sha384
Definition: nid.h:2998
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
EC_PKEY_CTX::md
const EVP_MD * md
Definition: p_ec.c:77
EC_KEY_get0_group
#define EC_KEY_get0_group
Definition: boringssl_prefix_symbols.h:1344
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
pkey_ec_ctrl
static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
Definition: p_ec.c:178
pkey_ec_keygen
static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
Definition: p_ec.c:219
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
EC_GROUP_free
#define EC_GROUP_free
Definition: boringssl_prefix_symbols.h:1321
EVP_R_INVALID_DIGEST_TYPE
#define EVP_R_INVALID_DIGEST_TYPE
Definition: evp_errors.h:71
evp_pkey_ctx_st
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:182
internal.h
evp_pkey_st
Definition: evp.h:1046
ECDSA_verify
#define ECDSA_verify
Definition: boringssl_prefix_symbols.h:1314
NID_sha256
#define NID_sha256
Definition: nid.h:2993
EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
#define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:180
EVP_PKEY_OP_TYPE_GEN
#define EVP_PKEY_OP_TYPE_GEN
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:137
EVP_R_INVALID_PARAMETERS
#define EVP_R_INVALID_PARAMETERS
Definition: evp_errors.h:93
EVP_PKEY_CTRL_GET_MD
#define EVP_PKEY_CTRL_GET_MD
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:151
EVP_R_NO_PARAMETERS_SET
#define EVP_R_NO_PARAMETERS_SET
Definition: evp_errors.h:84
err.h
evp_pkey_method_st
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:197
OPENSSL_EC_NAMED_CURVE
#define OPENSSL_EC_NAMED_CURVE
Definition: ec.h:347
EC_KEY_get0_public_key
#define EC_KEY_get0_public_key
Definition: boringssl_prefix_symbols.h:1346
ec_key.h
EC_GROUP_get_degree
#define EC_GROUP_get_degree
Definition: boringssl_prefix_symbols.h:1328
EVP_PKEY_CTX_ctrl
#define EVP_PKEY_CTX_ctrl
Definition: boringssl_prefix_symbols.h:1584
NID_sha1
#define NID_sha1
Definition: nid.h:372
NID_sha512
#define NID_sha512
Definition: nid.h:3003
EC_KEY_free
#define EC_KEY_free
Definition: boringssl_prefix_symbols.h:1341
nid
int nid
Definition: cipher_extra.c:71
ec_key_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:723
pkey_ec_init
static int pkey_ec_init(EVP_PKEY_CTX *ctx)
Definition: p_ec.c:82
EVP_PKEY_CTRL_MD
#define EVP_PKEY_CTRL_MD
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:150
NID_sha224
#define NID_sha224
Definition: nid.h:3008
ECDSA_sign
#define ECDSA_sign
Definition: boringssl_prefix_symbols.h:1311
nid.h
digest.h
key
const char * key
Definition: hpack_parser_table.cc:164
upload.group
group
Definition: bloaty/third_party/googletest/googlemock/scripts/upload.py:397
pkey_ec_sign
static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen, const uint8_t *tbs, size_t tbslen)
Definition: p_ec.c:118
ec_pkey_meth
const EVP_PKEY_METHOD ec_pkey_meth
Definition: p_ec.c:256
EC_PKEY_CTX
Definition: p_ec.c:75
ec_point_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:615
EVP_PKEY_CTX_set_ec_paramgen_curve_nid
int EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx, int nid)
Definition: p_ec.c:274
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
ec_group_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:573
EVP_PKEY_assign_EC_KEY
#define EVP_PKEY_assign_EC_KEY
Definition: boringssl_prefix_symbols.h:1611
EC_PKEY_CTX::gen_group
EC_GROUP * gen_group
Definition: p_ec.c:78
EVP_R_BUFFER_TOO_SMALL
#define EVP_R_BUFFER_TOO_SMALL
Definition: evp_errors.h:60
EVP_PKEY_CTX_set_ec_param_enc
int EVP_PKEY_CTX_set_ec_param_enc(EVP_PKEY_CTX *ctx, int encoding)
Definition: p_ec.c:279
pkey_ec_paramgen
static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
Definition: p_ec.c:240
evp_pkey_ctx_st::data
void * data
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:194
mem.h
ECDH_compute_key
#define ECDH_compute_key
Definition: boringssl_prefix_symbols.h:1296
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
ECDSA_size
#define ECDSA_size
Definition: boringssl_prefix_symbols.h:1313
ec.h
pkey_ec_verify
static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen, const uint8_t *tbs, size_t tbslen)
Definition: p_ec.c:138
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
EVP_R_KEYS_NOT_SET
#define EVP_R_KEYS_NOT_SET
Definition: evp_errors.h:77


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:38