10c6274a8SJohn Baldwin /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
30c6274a8SJohn Baldwin *
40c6274a8SJohn Baldwin * Copyright (c) 2021 The FreeBSD Foundation
50c6274a8SJohn Baldwin *
60c6274a8SJohn Baldwin * This software was developed by Ararat River Consulting, LLC under
70c6274a8SJohn Baldwin * sponsorship from the FreeBSD Foundation.
80c6274a8SJohn Baldwin *
90c6274a8SJohn Baldwin * Redistribution and use in source and binary forms, with or without
100c6274a8SJohn Baldwin * modification, are permitted provided that the following conditions
110c6274a8SJohn Baldwin * are met:
120c6274a8SJohn Baldwin * 1. Redistributions of source code must retain the above copyright
130c6274a8SJohn Baldwin * notice, this list of conditions and the following disclaimer.
140c6274a8SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright
150c6274a8SJohn Baldwin * notice, this list of conditions and the following disclaimer in the
160c6274a8SJohn Baldwin * documentation and/or other materials provided with the distribution.
170c6274a8SJohn Baldwin *
180c6274a8SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
190c6274a8SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
200c6274a8SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
210c6274a8SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
220c6274a8SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
230c6274a8SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
240c6274a8SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
250c6274a8SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
260c6274a8SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
270c6274a8SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
280c6274a8SJohn Baldwin * SUCH DAMAGE.
290c6274a8SJohn Baldwin */
300c6274a8SJohn Baldwin
310c6274a8SJohn Baldwin #ifndef __CRYPTO_CURVE25519_H__
320c6274a8SJohn Baldwin #define __CRYPTO_CURVE25519_H__
330c6274a8SJohn Baldwin
340c6274a8SJohn Baldwin #include <sys/libkern.h>
350c6274a8SJohn Baldwin
360c6274a8SJohn Baldwin #define CURVE25519_KEY_SIZE 32
370c6274a8SJohn Baldwin
380c6274a8SJohn Baldwin bool curve25519(uint8_t *public, const uint8_t *secret,
390c6274a8SJohn Baldwin const uint8_t *basepoint);
400c6274a8SJohn Baldwin bool curve25519_generate_public(uint8_t *public,
410c6274a8SJohn Baldwin const uint8_t *secret);
420c6274a8SJohn Baldwin
430c6274a8SJohn Baldwin static __inline void
curve25519_clamp_secret(uint8_t * secret)440c6274a8SJohn Baldwin curve25519_clamp_secret(uint8_t *secret)
450c6274a8SJohn Baldwin {
460c6274a8SJohn Baldwin secret[0] &= 248;
470c6274a8SJohn Baldwin secret[31] &= 127;
480c6274a8SJohn Baldwin secret[31] |= 64;
490c6274a8SJohn Baldwin }
500c6274a8SJohn Baldwin
510c6274a8SJohn Baldwin static __inline void
curve25519_generate_secret(uint8_t * secret)520c6274a8SJohn Baldwin curve25519_generate_secret(uint8_t *secret)
530c6274a8SJohn Baldwin {
540c6274a8SJohn Baldwin arc4random_buf(secret, CURVE25519_KEY_SIZE);
550c6274a8SJohn Baldwin curve25519_clamp_secret(secret);
560c6274a8SJohn Baldwin }
570c6274a8SJohn Baldwin
580c6274a8SJohn Baldwin #endif /* __CRYPTO_CURVE25519_H__ */
59