ec_derive.c
Go to the documentation of this file.
1 /* Copyright (c) 2019, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/ec_key.h>
16 
17 #include <string.h>
18 
19 #include <openssl/ec.h>
20 #include <openssl/err.h>
21 #include <openssl/digest.h>
22 #include <openssl/hkdf.h>
23 #include <openssl/mem.h>
24 
25 #include "../fipsmodule/ec/internal.h"
26 
27 
29  size_t secret_len) {
30 #define EC_KEY_DERIVE_MAX_NAME_LEN 16
32  if (name == NULL || strlen(name) > EC_KEY_DERIVE_MAX_NAME_LEN) {
34  return NULL;
35  }
36 
37  // Assemble a label string to provide some key separation in case |secret| is
38  // misused, but ultimately it's on the caller to ensure |secret| is suitably
39  // separated.
40  static const char kLabel[] = "derive EC key ";
41  char info[sizeof(kLabel) + EC_KEY_DERIVE_MAX_NAME_LEN];
42  OPENSSL_strlcpy(info, kLabel, sizeof(info));
43  OPENSSL_strlcat(info, name, sizeof(info));
44 
45  // Generate 128 bits beyond the group order so the bias is at most 2^-128.
46 #define EC_KEY_DERIVE_EXTRA_BITS 128
47 #define EC_KEY_DERIVE_EXTRA_BYTES (EC_KEY_DERIVE_EXTRA_BITS / 8)
48 
50  // The reduction strategy below requires the group order be large enough.
51  // (The actual bound is a bit tighter, but our curves are much larger than
52  // 128-bit.)
54  return NULL;
55  }
56 
58  size_t derived_len = BN_num_bytes(&group->order) + EC_KEY_DERIVE_EXTRA_BYTES;
59  assert(derived_len <= sizeof(derived));
60  if (!HKDF(derived, derived_len, EVP_sha256(), secret, secret_len,
61  /*salt=*/NULL, /*salt_len=*/0, (const uint8_t *)info,
62  strlen(info))) {
63  return NULL;
64  }
65 
66  EC_KEY *key = EC_KEY_new();
67  BN_CTX *ctx = BN_CTX_new();
68  BIGNUM *priv = BN_bin2bn(derived, derived_len, NULL);
69  EC_POINT *pub = EC_POINT_new(group);
70  if (key == NULL || ctx == NULL || priv == NULL || pub == NULL ||
71  // Reduce |priv| with Montgomery reduction. First, convert "from"
72  // Montgomery form to compute |priv| * R^-1 mod |order|. This requires
73  // |priv| be under order * R, which is true if the group order is large
74  // enough. 2^(num_bytes(order)) < 2^8 * order, so:
75  //
76  // priv < 2^8 * order * 2^128 < order * order < order * R
77  !BN_from_montgomery(priv, priv, group->order_mont, ctx) ||
78  // Multiply by R^2 and do another Montgomery reduction to compute
79  // priv * R^-1 * R^2 * R^-1 = priv mod order.
80  !BN_to_montgomery(priv, priv, group->order_mont, ctx) ||
81  !EC_POINT_mul(group, pub, priv, NULL, NULL, ctx) ||
83  !EC_KEY_set_private_key(key, priv)) {
85  key = NULL;
86  goto err;
87  }
88 
89 err:
90  OPENSSL_cleanse(derived, sizeof(derived));
92  BN_free(priv);
93  EC_POINT_free(pub);
94  return key;
95 }
EC_POINT_new
#define EC_POINT_new
Definition: boringssl_prefix_symbols.h:1384
EC_KEY_DERIVE_MAX_NAME_LEN
#define EC_KEY_DERIVE_MAX_NAME_LEN
EC_KEY_new
#define EC_KEY_new
Definition: boringssl_prefix_symbols.h:1355
ctx
Definition: benchmark-async.c:30
EC_KEY_DERIVE_EXTRA_BITS
#define EC_KEY_DERIVE_EXTRA_BITS
OPENSSL_cleanse
#define OPENSSL_cleanse
Definition: boringssl_prefix_symbols.h:1864
BN_bin2bn
#define BN_bin2bn
Definition: boringssl_prefix_symbols.h:900
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
EC_KEY_set_public_key
#define EC_KEY_set_public_key
Definition: boringssl_prefix_symbols.h:1367
error_ref_leak.err
err
Definition: error_ref_leak.py:35
EC_KEY_DERIVE_EXTRA_BYTES
#define EC_KEY_DERIVE_EXTRA_BYTES
EC_R_UNKNOWN_GROUP
#define EC_R_UNKNOWN_GROUP
Definition: ec.h:430
setup.name
name
Definition: setup.py:542
BN_free
#define BN_free
Definition: boringssl_prefix_symbols.h:923
bignum_ctx
Definition: ctx.c:91
BN_num_bytes
#define BN_num_bytes
Definition: boringssl_prefix_symbols.h:976
EVP_sha256
const OPENSSL_EXPORT EVP_MD * EVP_sha256(void)
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
BN_to_montgomery
#define BN_to_montgomery
Definition: boringssl_prefix_symbols.h:999
EC_POINT_mul
#define EC_POINT_mul
Definition: boringssl_prefix_symbols.h:1383
EC_MAX_BYTES
#define EC_MAX_BYTES
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:91
EC_KEY_derive_from_secret
EC_KEY * EC_KEY_derive_from_secret(const EC_GROUP *group, const uint8_t *secret, size_t secret_len)
Definition: ec_derive.c:28
EC_GROUP_get_curve_name
#define EC_GROUP_get_curve_name
Definition: boringssl_prefix_symbols.h:1327
err.h
EC_POINT_free
#define EC_POINT_free
Definition: boringssl_prefix_symbols.h:1377
ERR_R_INTERNAL_ERROR
#define ERR_R_INTERNAL_ERROR
Definition: err.h:374
ec_key.h
BN_CTX_new
#define BN_CTX_new
Definition: boringssl_prefix_symbols.h:885
EC_KEY_free
#define EC_KEY_free
Definition: boringssl_prefix_symbols.h:1341
OPENSSL_strlcat
#define OPENSSL_strlcat
Definition: boringssl_prefix_symbols.h:1893
ec_key_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:723
EC_curve_nid2nist
#define EC_curve_nid2nist
Definition: boringssl_prefix_symbols.h:1392
HKDF
#define HKDF
Definition: boringssl_prefix_symbols.h:1780
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
OPENSSL_strlcpy
#define OPENSSL_strlcpy
Definition: boringssl_prefix_symbols.h:1894
hkdf.h
bignum_st
Definition: bn.h:957
ec_point_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:615
BN_CTX_free
#define BN_CTX_free
Definition: boringssl_prefix_symbols.h:883
ec_group_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:573
EC_GROUP_order_bits
#define EC_GROUP_order_bits
Definition: boringssl_prefix_symbols.h:1333
EC_KEY_set_private_key
#define EC_KEY_set_private_key
Definition: boringssl_prefix_symbols.h:1366
mem.h
ec.h
BN_from_montgomery
#define BN_from_montgomery
Definition: boringssl_prefix_symbols.h:924


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:18