| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 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 |
| 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 = |
| 59 | BN_num_bytes(EC_GROUP_get0_order(group)) + EC_KEY_DERIVE_EXTRA_BYTES; |
| 60 | assert(derived_len <= sizeof(derived)); |
| 61 | if (!HKDF(derived, derived_len, EVP_sha256(), secret, secret_len, |
| 62 | /*salt=*/NULL, /*salt_len=*/0, (const uint8_t *)info, |
| 63 | strlen(info))) { |
| 64 | return NULL; |
| 65 | } |
| 66 |
| 67 | EC_KEY *key = EC_KEY_new(); |
| 68 | BN_CTX *ctx = BN_CTX_new(); |
| 69 | BIGNUM *priv = BN_bin2bn(derived, derived_len, NULL); |
| 70 | EC_POINT *pub = EC_POINT_new(group); |
| 71 | if (key == NULL || ctx == NULL || priv == NULL || pub == NULL || |
| 72 | // Reduce |priv| with Montgomery reduction. First, convert "from" |
| 73 | // Montgomery form to compute |priv| * R^-1 mod |order|. This requires |
| 74 | // |priv| be under order * R, which is true if the group order is large |
| 75 | // enough. 2^(num_bytes(order)) < 2^8 * order, so: |
| 76 | // |
| 77 | // priv < 2^8 * order * 2^128 < order * order < order * R |
| 78 | !BN_from_montgomery(priv, priv, &group->order, ctx) || |
| 79 | // Multiply by R^2 and do another Montgomery reduction to compute |
| 80 | // priv * R^-1 * R^2 * R^-1 = priv mod order. |
| 81 | !BN_to_montgomery(priv, priv, &group->order, ctx) || |
| 82 | !EC_POINT_mul(group, pub, priv, NULL, NULL, ctx) || |
| 83 | !EC_KEY_set_group(key, group) || !EC_KEY_set_public_key(key, pub) || |
| 84 | !EC_KEY_set_private_key(key, priv)) { |
| 85 | EC_KEY_free(key); |
| 86 | key = NULL; |
| 87 | goto err; |
| 88 | } |
| 89 |
| 90 | err: |
| 91 | OPENSSL_cleanse(derived, sizeof(derived)); |
| 92 | BN_CTX_free(ctx); |
| 93 | BN_free(priv); |
| 94 | EC_POINT_free(pub); |
| 95 | return key; |
| 96 | } |
| 97 |