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 #include <sys/libkern.h> /* karc4random_buf() */
22
23 #define CURVE25519_KEY_SIZE 32
24
25 int curve25519(uint8_t out[CURVE25519_KEY_SIZE],
26 const uint8_t scalar[CURVE25519_KEY_SIZE],
27 const uint8_t point[CURVE25519_KEY_SIZE]);
28
29 int curve25519_generate_public(uint8_t pub[CURVE25519_KEY_SIZE],
30 const uint8_t secret[CURVE25519_KEY_SIZE]);
31
32 static inline void
curve25519_clamp_secret(uint8_t secret[CURVE25519_KEY_SIZE])33 curve25519_clamp_secret(uint8_t secret[CURVE25519_KEY_SIZE])
34 {
35 secret[0] &= 248;
36 secret[31] = (secret[31] & 127) | 64;
37 }
38
39 static inline void
curve25519_generate_secret(uint8_t secret[CURVE25519_KEY_SIZE])40 curve25519_generate_secret(uint8_t secret[CURVE25519_KEY_SIZE])
41 {
42 karc4random_buf(secret, CURVE25519_KEY_SIZE);
43 curve25519_clamp_secret(secret);
44 }
45
46 #endif /* _CURVE25519_H_ */
47