ecdh.c 4.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* ECDH key-agreement protocol
 *
 * Copyright (c) 2016, Intel Corporation
 * Authors: Salvator Benedetto <salvatore.benedetto@intel.com>
 */

#include <linux/module.h>
#include <crypto/internal/kpp.h>
#include <crypto/kpp.h>
#include <crypto/ecdh.h>
#include <linux/scatterlist.h>
#include "ecc.h"

struct ecdh_ctx {
	unsigned int curve_id;
	unsigned int ndigits;
	u64 private_key[ECC_MAX_DIGITS];
};

static inline struct ecdh_ctx *ecdh_get_ctx(struct crypto_kpp *tfm)
{
	return kpp_tfm_ctx(tfm);
}

static unsigned int ecdh_supported_curve(unsigned int curve_id)
{
	switch (curve_id) {
29 30
	case ECC_CURVE_NIST_P192: return ECC_CURVE_NIST_P192_DIGITS;
	case ECC_CURVE_NIST_P256: return ECC_CURVE_NIST_P256_DIGITS;
31 32 33 34
	default: return 0;
	}
}

35 36
static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
			   unsigned int len)
37 38 39 40 41
{
	struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
	struct ecdh params;
	unsigned int ndigits;

42 43
	if (crypto_ecdh_decode_key(buf, len, &params) < 0 ||
	    params.key_size > sizeof(ctx->private_key))
44 45 46 47 48 49 50 51 52
		return -EINVAL;

	ndigits = ecdh_supported_curve(params.curve_id);
	if (!ndigits)
		return -EINVAL;

	ctx->curve_id = params.curve_id;
	ctx->ndigits = ndigits;

53 54 55 56
	if (!params.key || !params.key_size)
		return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
				       ctx->private_key);

57 58
	memcpy(ctx->private_key, params.key, params.key_size);

59 60 61 62 63
	if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
			     ctx->private_key, params.key_size) < 0) {
		memzero_explicit(ctx->private_key, params.key_size);
		return -EINVAL;
	}
64 65 66 67 68 69 70
	return 0;
}

static int ecdh_compute_value(struct kpp_request *req)
{
	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
	struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
71 72
	u64 *public_key;
	u64 *shared_secret = NULL;
73
	void *buf;
74 75
	size_t copied, nbytes, public_key_sz;
	int ret = -ENOMEM;
76 77

	nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
78 79 80 81 82 83
	/* Public part is a point thus it has both coordinates */
	public_key_sz = 2 * nbytes;

	public_key = kmalloc(public_key_sz, GFP_KERNEL);
	if (!public_key)
		return -ENOMEM;
84 85

	if (req->src) {
86 87 88 89
		shared_secret = kmalloc(nbytes, GFP_KERNEL);
		if (!shared_secret)
			goto free_pubkey;

90 91 92 93 94 95 96 97 98 99 100 101
		/* from here on it's invalid parameters */
		ret = -EINVAL;

		/* must have exactly two points to be on the curve */
		if (public_key_sz != req->src_len)
			goto free_all;

		copied = sg_copy_to_buffer(req->src,
					   sg_nents_for_len(req->src,
							    public_key_sz),
					   public_key, public_key_sz);
		if (copied != public_key_sz)
102
			goto free_all;
103

104
		ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
105 106
						ctx->private_key, public_key,
						shared_secret);
107

108
		buf = shared_secret;
109
	} else {
110
		ret = ecc_make_pub_key(ctx->curve_id, ctx->ndigits,
111 112 113
				       ctx->private_key, public_key);
		buf = public_key;
		nbytes = public_key_sz;
114 115 116
	}

	if (ret < 0)
117
		goto free_all;
118

119 120 121 122 123
	/* might want less than we've got */
	nbytes = min_t(size_t, nbytes, req->dst_len);
	copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
								nbytes),
				     buf, nbytes);
124
	if (copied != nbytes)
125
		ret = -EINVAL;
126

127 128
	/* fall through */
free_all:
129
	kfree_sensitive(shared_secret);
130 131
free_pubkey:
	kfree(public_key);
132 133 134
	return ret;
}

135
static unsigned int ecdh_max_size(struct crypto_kpp *tfm)
136 137 138
{
	struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);

139 140
	/* Public key is made of two coordinates, add one to the left shift */
	return ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT + 1);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
}

static struct kpp_alg ecdh = {
	.set_secret = ecdh_set_secret,
	.generate_public_key = ecdh_compute_value,
	.compute_shared_secret = ecdh_compute_value,
	.max_size = ecdh_max_size,
	.base = {
		.cra_name = "ecdh",
		.cra_driver_name = "ecdh-generic",
		.cra_priority = 100,
		.cra_module = THIS_MODULE,
		.cra_ctxsize = sizeof(struct ecdh_ctx),
	},
};

static int ecdh_init(void)
{
	return crypto_register_kpp(&ecdh);
}

static void ecdh_exit(void)
{
	crypto_unregister_kpp(&ecdh);
}

167
subsys_initcall(ecdh_init);
168 169 170 171
module_exit(ecdh_exit);
MODULE_ALIAS_CRYPTO("ecdh");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ECDH generic algorithm");