^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* ECDH key-agreement protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2016, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors: Salvator Benedetto <salvatore.benedetto@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <crypto/internal/kpp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/kpp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/ecdh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "ecc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct ecdh_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) unsigned int curve_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) unsigned int ndigits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u64 private_key[ECC_MAX_DIGITS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static inline struct ecdh_ctx *ecdh_get_ctx(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return kpp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static unsigned int ecdh_supported_curve(unsigned int curve_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) switch (curve_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) case ECC_CURVE_NIST_P192: return ECC_CURVE_NIST_P192_DIGITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) case ECC_CURVE_NIST_P256: return ECC_CURVE_NIST_P256_DIGITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) default: return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct ecdh params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int ndigits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (crypto_ecdh_decode_key(buf, len, ¶ms) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) params.key_size > sizeof(ctx->private_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ndigits = ecdh_supported_curve(params.curve_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!ndigits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ctx->curve_id = params.curve_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ctx->ndigits = ndigits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!params.key || !params.key_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ctx->private_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) memcpy(ctx->private_key, params.key, params.key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ctx->private_key, params.key_size) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) memzero_explicit(ctx->private_key, params.key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int ecdh_compute_value(struct kpp_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u64 *public_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u64 *shared_secret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) size_t copied, nbytes, public_key_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* Public part is a point thus it has both coordinates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) public_key_sz = 2 * nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) public_key = kmalloc(public_key_sz, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!public_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (req->src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) shared_secret = kmalloc(nbytes, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!shared_secret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) goto free_pubkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* from here on it's invalid parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* must have exactly two points to be on the curve */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (public_key_sz != req->src_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto free_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) copied = sg_copy_to_buffer(req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) sg_nents_for_len(req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) public_key_sz),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) public_key, public_key_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (copied != public_key_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto free_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ctx->private_key, public_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) shared_secret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) buf = shared_secret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ret = ecc_make_pub_key(ctx->curve_id, ctx->ndigits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ctx->private_key, public_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) buf = public_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) nbytes = public_key_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto free_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* might want less than we've got */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) nbytes = min_t(size_t, nbytes, req->dst_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) nbytes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (copied != nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) free_all:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) kfree_sensitive(shared_secret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) free_pubkey:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) kfree(public_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static unsigned int ecdh_max_size(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Public key is made of two coordinates, add one to the left shift */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static struct kpp_alg ecdh = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .set_secret = ecdh_set_secret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .generate_public_key = ecdh_compute_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .compute_shared_secret = ecdh_compute_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .max_size = ecdh_max_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .cra_name = "ecdh",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .cra_driver_name = "ecdh-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) .cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) .cra_ctxsize = sizeof(struct ecdh_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int ecdh_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return crypto_register_kpp(&ecdh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void ecdh_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) crypto_unregister_kpp(&ecdh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) subsys_initcall(ecdh_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) module_exit(ecdh_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODULE_ALIAS_CRYPTO("ecdh");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) MODULE_DESCRIPTION("ECDH generic algorithm");