1 /*	$OpenBSD: curve25519.h,v 1.7 2022/11/13 14:05:04 tb Exp $ */
2 /*
3  * Copyright (c) 2015, Google Inc.
4  *
5  * Permission to use, copy, modify, and/or 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 ANY
12  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef HEADER_CURVE25519_H
19 #define HEADER_CURVE25519_H
20 
21 #include <stdint.h>
22 
23 #include <openssl/opensslconf.h>
24 
25 #if defined(__cplusplus)
26 extern "C" {
27 #endif
28 
29 /*
30  * Curve25519.
31  *
32  * Curve25519 is an elliptic curve. See https://tools.ietf.org/html/rfc7748.
33  */
34 
35 /*
36  * X25519.
37  *
38  * X25519 is the Diffie-Hellman primitive built from curve25519. It is
39  * sometimes referred to as curve25519, but X25519 is a more precise name.
40  * See http://cr.yp.to/ecdh.html and https://tools.ietf.org/html/rfc7748.
41  */
42 
43 #define X25519_KEY_LENGTH 32
44 
45 /*
46  * X25519_keypair sets |out_public_value| and |out_private_key| to a freshly
47  * generated, public/private key pair.
48  */
49 void X25519_keypair(uint8_t out_public_value[X25519_KEY_LENGTH],
50     uint8_t out_private_key[X25519_KEY_LENGTH]);
51 
52 /*
53  * X25519 writes a shared key to |out_shared_key| that is calculated from the
54  * given private key and the peer's public value. It returns one on success and
55  * zero on error.
56  *
57  * Don't use the shared key directly, rather use a KDF and also include the two
58  * public values as inputs.
59  */
60 int X25519(uint8_t out_shared_key[X25519_KEY_LENGTH],
61     const uint8_t private_key[X25519_KEY_LENGTH],
62     const uint8_t peers_public_value[X25519_KEY_LENGTH]);
63 
64 /*
65  * ED25519
66  *
67  * Ed25519 is a signature scheme using a twisted Edwards curve that is
68  * birationally equivalent to curve25519.
69  */
70 
71 #define ED25519_PRIVATE_KEY_LENGTH	32
72 #define ED25519_PUBLIC_KEY_LENGTH	32
73 #define ED25519_SIGNATURE_LENGTH	64
74 
75 /*
76  * ED25519_keypair sets |out_public_key| and |out_private_key| to a freshly
77  * generated, public/private key pair.
78  */
79 void ED25519_keypair(uint8_t out_public_key[ED25519_PUBLIC_KEY_LENGTH],
80     uint8_t out_private_key[ED25519_PRIVATE_KEY_LENGTH]);
81 
82 /*
83  * ED25519_sign sets |out_sig| to be a signature of |message_len| bytes from
84  * |message| using |public_key| and |private_key|. It returns one on success
85  * or zero on allocation failure.
86  */
87 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
88     const uint8_t public_key[ED25519_PUBLIC_KEY_LENGTH],
89     const uint8_t private_key_seed[ED25519_PRIVATE_KEY_LENGTH]);
90 
91 /*
92  * ED25519_verify returns one iff |signature| is a valid signature by
93  * |public_key| of |message_len| bytes from |message|. It returns zero
94  * otherwise.
95  */
96 int ED25519_verify(const uint8_t *message, size_t message_len,
97     const uint8_t signature[ED25519_SIGNATURE_LENGTH],
98     const uint8_t public_key[ED25519_PUBLIC_KEY_LENGTH]);
99 
100 #if defined(__cplusplus)
101 }  /* extern C */
102 #endif
103 
104 #endif  /* HEADER_CURVE25519_H */
105