Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
0002 /*
0003  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
0004  */
0005 
0006 #ifndef CURVE25519_H
0007 #define CURVE25519_H
0008 
0009 #include <crypto/algapi.h> // For crypto_memneq.
0010 #include <linux/types.h>
0011 #include <linux/random.h>
0012 
0013 enum curve25519_lengths {
0014     CURVE25519_KEY_SIZE = 32
0015 };
0016 
0017 extern const u8 curve25519_null_point[];
0018 extern const u8 curve25519_base_point[];
0019 
0020 void curve25519_generic(u8 out[CURVE25519_KEY_SIZE],
0021             const u8 scalar[CURVE25519_KEY_SIZE],
0022             const u8 point[CURVE25519_KEY_SIZE]);
0023 
0024 void curve25519_arch(u8 out[CURVE25519_KEY_SIZE],
0025              const u8 scalar[CURVE25519_KEY_SIZE],
0026              const u8 point[CURVE25519_KEY_SIZE]);
0027 
0028 void curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE],
0029               const u8 secret[CURVE25519_KEY_SIZE]);
0030 
0031 bool curve25519_selftest(void);
0032 
0033 static inline
0034 bool __must_check curve25519(u8 mypublic[CURVE25519_KEY_SIZE],
0035                  const u8 secret[CURVE25519_KEY_SIZE],
0036                  const u8 basepoint[CURVE25519_KEY_SIZE])
0037 {
0038     if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CURVE25519))
0039         curve25519_arch(mypublic, secret, basepoint);
0040     else
0041         curve25519_generic(mypublic, secret, basepoint);
0042     return crypto_memneq(mypublic, curve25519_null_point,
0043                  CURVE25519_KEY_SIZE);
0044 }
0045 
0046 static inline bool
0047 __must_check curve25519_generate_public(u8 pub[CURVE25519_KEY_SIZE],
0048                     const u8 secret[CURVE25519_KEY_SIZE])
0049 {
0050     if (unlikely(!crypto_memneq(secret, curve25519_null_point,
0051                     CURVE25519_KEY_SIZE)))
0052         return false;
0053 
0054     if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CURVE25519))
0055         curve25519_base_arch(pub, secret);
0056     else
0057         curve25519_generic(pub, secret, curve25519_base_point);
0058     return crypto_memneq(pub, curve25519_null_point, CURVE25519_KEY_SIZE);
0059 }
0060 
0061 static inline void curve25519_clamp_secret(u8 secret[CURVE25519_KEY_SIZE])
0062 {
0063     secret[0] &= 248;
0064     secret[31] = (secret[31] & 127) | 64;
0065 }
0066 
0067 static inline void curve25519_generate_secret(u8 secret[CURVE25519_KEY_SIZE])
0068 {
0069     get_random_bytes_wait(secret, CURVE25519_KEY_SIZE);
0070     curve25519_clamp_secret(secret);
0071 }
0072 
0073 #endif /* CURVE25519_H */