xref: /dflybsd-src/sys/crypto/curve25519/curve25519.h (revision a163f8c49c78cfd65042d8addf96d7ac8c1484a8)
194a56edaSAaron LI /*	$OpenBSD: curve25519.h,v 1.2 2020/07/22 13:54:30 tobhe Exp $	*/
294a56edaSAaron LI /*
394a56edaSAaron LI  * Copyright (C) 2019-2020 Matt Dunwoodie <ncon@noconroy.net>
494a56edaSAaron LI  *
594a56edaSAaron LI  * Permission to use, copy, modify, and distribute this software for any
694a56edaSAaron LI  * purpose with or without fee is hereby granted, provided that the above
794a56edaSAaron LI  * copyright notice and this permission notice appear in all copies.
894a56edaSAaron LI  *
994a56edaSAaron LI  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1094a56edaSAaron LI  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1194a56edaSAaron LI  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1294a56edaSAaron LI  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1394a56edaSAaron LI  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1494a56edaSAaron LI  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1594a56edaSAaron LI  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1694a56edaSAaron LI  */
1794a56edaSAaron LI 
1894a56edaSAaron LI #ifndef _CURVE25519_H_
1994a56edaSAaron LI #define _CURVE25519_H_
2094a56edaSAaron LI 
21*a163f8c4SAaron LI #include <sys/libkern.h> /* karc4random_buf() */
22*a163f8c4SAaron LI 
2394a56edaSAaron LI #define CURVE25519_KEY_SIZE 32
2494a56edaSAaron LI 
2594a56edaSAaron LI int curve25519(uint8_t out[CURVE25519_KEY_SIZE],
2694a56edaSAaron LI 	       const uint8_t scalar[CURVE25519_KEY_SIZE],
2794a56edaSAaron LI 	       const uint8_t point[CURVE25519_KEY_SIZE]);
2894a56edaSAaron LI 
2994a56edaSAaron LI int curve25519_generate_public(uint8_t pub[CURVE25519_KEY_SIZE],
3094a56edaSAaron LI 			       const uint8_t secret[CURVE25519_KEY_SIZE]);
3194a56edaSAaron LI 
3294a56edaSAaron LI static inline void
curve25519_clamp_secret(uint8_t secret[CURVE25519_KEY_SIZE])3394a56edaSAaron LI curve25519_clamp_secret(uint8_t secret[CURVE25519_KEY_SIZE])
3494a56edaSAaron LI {
3594a56edaSAaron LI 	secret[0] &= 248;
3694a56edaSAaron LI 	secret[31] = (secret[31] & 127) | 64;
3794a56edaSAaron LI }
3894a56edaSAaron LI 
3994a56edaSAaron LI static inline void
curve25519_generate_secret(uint8_t secret[CURVE25519_KEY_SIZE])4094a56edaSAaron LI curve25519_generate_secret(uint8_t secret[CURVE25519_KEY_SIZE])
4194a56edaSAaron LI {
42*a163f8c4SAaron LI 	karc4random_buf(secret, CURVE25519_KEY_SIZE);
4394a56edaSAaron LI 	curve25519_clamp_secret(secret);
4494a56edaSAaron LI }
4594a56edaSAaron LI 
4694a56edaSAaron LI #endif /* _CURVE25519_H_ */
47