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
EC_KEY_derive_from_secret(const EC_GROUP * group,const uint8_t * secret,size_t secret_len)28 EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group, const uint8_t *secret,
29 size_t secret_len) {
30 #define EC_KEY_DERIVE_MAX_NAME_LEN 16
31 const char *name = EC_curve_nid2nist(EC_GROUP_get_curve_name(group));
32 if (name == NULL || strlen(name) > EC_KEY_DERIVE_MAX_NAME_LEN) {
33 OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
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
49 if (EC_GROUP_order_bits(group) <= EC_KEY_DERIVE_EXTRA_BITS + 8) {
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.)
53 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
54 return NULL;
55 }
56
57 uint8_t derived[EC_KEY_DERIVE_EXTRA_BYTES + EC_MAX_BYTES];
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) ||
82 !EC_KEY_set_group(key, group) || !EC_KEY_set_public_key(key, pub) ||
83 !EC_KEY_set_private_key(key, priv)) {
84 EC_KEY_free(key);
85 key = NULL;
86 goto err;
87 }
88
89 err:
90 OPENSSL_cleanse(derived, sizeof(derived));
91 BN_CTX_free(ctx);
92 BN_free(priv);
93 EC_POINT_free(pub);
94 return key;
95 }
96