^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <crypto/curve25519.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <crypto/internal/kpp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <crypto/kpp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) static int curve25519_set_secret(struct crypto_kpp *tfm, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) u8 *secret = kpp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) curve25519_generate_secret(secret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) else if (len == CURVE25519_KEY_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) crypto_memneq(buf, curve25519_null_point, CURVE25519_KEY_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) memcpy(secret, buf, CURVE25519_KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int curve25519_compute_value(struct kpp_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) const u8 *secret = kpp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u8 public_key[CURVE25519_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u8 buf[CURVE25519_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int copied, nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u8 const *bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (req->src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) copied = sg_copy_to_buffer(req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) sg_nents_for_len(req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) CURVE25519_KEY_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) public_key, CURVE25519_KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (copied != CURVE25519_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) bp = public_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) bp = curve25519_base_point;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) curve25519_generic(buf, secret, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* might want less than we've got */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) nbytes = min_t(size_t, CURVE25519_KEY_SIZE, req->dst_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) nbytes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (copied != nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static unsigned int curve25519_max_size(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return CURVE25519_KEY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static struct kpp_alg curve25519_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .base.cra_name = "curve25519",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .base.cra_driver_name = "curve25519-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .base.cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .base.cra_ctxsize = CURVE25519_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .set_secret = curve25519_set_secret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .generate_public_key = curve25519_compute_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .compute_shared_secret = curve25519_compute_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .max_size = curve25519_max_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static int curve25519_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return crypto_register_kpp(&curve25519_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static void curve25519_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) crypto_unregister_kpp(&curve25519_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) subsys_initcall(curve25519_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) module_exit(curve25519_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) MODULE_ALIAS_CRYPTO("curve25519");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) MODULE_ALIAS_CRYPTO("curve25519-generic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) MODULE_LICENSE("GPL");