~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/crypto/curve25519-generic.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php