xref: /openbsd/sys/crypto/curve25519.h (revision 6a126883)
1 /*	$OpenBSD: curve25519.h,v 1.2 2020/07/22 13:54:30 tobhe Exp $	*/
2 /*
3  * Copyright (C) 2019-2020 Matt Dunwoodie <ncon@noconroy.net>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _CURVE25519_H_
19 #define _CURVE25519_H_
20 
21 #define CURVE25519_KEY_SIZE 32
22 
23 int curve25519(uint8_t out[CURVE25519_KEY_SIZE],
24                const uint8_t scalar[CURVE25519_KEY_SIZE],
25                const uint8_t point[CURVE25519_KEY_SIZE]);
26 
27 int curve25519_generate_public(uint8_t pub[CURVE25519_KEY_SIZE],
28 			       const uint8_t secret[CURVE25519_KEY_SIZE]);
29 
30 static inline void
curve25519_clamp_secret(uint8_t secret[CURVE25519_KEY_SIZE])31 curve25519_clamp_secret(uint8_t secret[CURVE25519_KEY_SIZE])
32 {
33 	secret[0] &= 248;
34 	secret[31] = (secret[31] & 127) | 64;
35 }
36 
37 static inline void
curve25519_generate_secret(uint8_t secret[CURVE25519_KEY_SIZE])38 curve25519_generate_secret(uint8_t secret[CURVE25519_KEY_SIZE])
39 {
40 	arc4random_buf(secret, CURVE25519_KEY_SIZE);
41 	curve25519_clamp_secret(secret);
42 }
43 
44 #endif /* _CURVE25519_H_ */
45