xref: /openbsd-src/lib/libcrypto/curve25519/curve25519.h (revision d6a7ea16f442ea4ed6f656c93d1c223ecbde99a2)
1*d6a7ea16Stb /*	$OpenBSD: curve25519.h,v 1.7 2022/11/13 14:05:04 tb Exp $ */
25f5d09a5Sjsing /*
35f5d09a5Sjsing  * Copyright (c) 2015, Google Inc.
45f5d09a5Sjsing  *
55f5d09a5Sjsing  * Permission to use, copy, modify, and/or distribute this software for any
65f5d09a5Sjsing  * purpose with or without fee is hereby granted, provided that the above
75f5d09a5Sjsing  * copyright notice and this permission notice appear in all copies.
85f5d09a5Sjsing  *
95f5d09a5Sjsing  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
105f5d09a5Sjsing  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
115f5d09a5Sjsing  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
125f5d09a5Sjsing  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
135f5d09a5Sjsing  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
145f5d09a5Sjsing  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
155f5d09a5Sjsing  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
165f5d09a5Sjsing  */
175f5d09a5Sjsing 
185f5d09a5Sjsing #ifndef HEADER_CURVE25519_H
195f5d09a5Sjsing #define HEADER_CURVE25519_H
205f5d09a5Sjsing 
215f5d09a5Sjsing #include <stdint.h>
225f5d09a5Sjsing 
235f5d09a5Sjsing #include <openssl/opensslconf.h>
245f5d09a5Sjsing 
255f5d09a5Sjsing #if defined(__cplusplus)
265f5d09a5Sjsing extern "C" {
275f5d09a5Sjsing #endif
285f5d09a5Sjsing 
295f5d09a5Sjsing /*
305f5d09a5Sjsing  * Curve25519.
315f5d09a5Sjsing  *
325f5d09a5Sjsing  * Curve25519 is an elliptic curve. See https://tools.ietf.org/html/rfc7748.
335f5d09a5Sjsing  */
345f5d09a5Sjsing 
355f5d09a5Sjsing /*
365f5d09a5Sjsing  * X25519.
375f5d09a5Sjsing  *
385f5d09a5Sjsing  * X25519 is the Diffie-Hellman primitive built from curve25519. It is
395f5d09a5Sjsing  * sometimes referred to as curve25519, but X25519 is a more precise name.
405f5d09a5Sjsing  * See http://cr.yp.to/ecdh.html and https://tools.ietf.org/html/rfc7748.
415f5d09a5Sjsing  */
425f5d09a5Sjsing 
43a878ae04Sjsing #define X25519_KEY_LENGTH 32
44a878ae04Sjsing 
455f5d09a5Sjsing /*
465f5d09a5Sjsing  * X25519_keypair sets |out_public_value| and |out_private_key| to a freshly
475f5d09a5Sjsing  * generated, public/private key pair.
485f5d09a5Sjsing  */
49a878ae04Sjsing void X25519_keypair(uint8_t out_public_value[X25519_KEY_LENGTH],
50a878ae04Sjsing     uint8_t out_private_key[X25519_KEY_LENGTH]);
515f5d09a5Sjsing 
525f5d09a5Sjsing /*
535f5d09a5Sjsing  * X25519 writes a shared key to |out_shared_key| that is calculated from the
545f5d09a5Sjsing  * given private key and the peer's public value. It returns one on success and
555f5d09a5Sjsing  * zero on error.
565f5d09a5Sjsing  *
575f5d09a5Sjsing  * Don't use the shared key directly, rather use a KDF and also include the two
585f5d09a5Sjsing  * public values as inputs.
595f5d09a5Sjsing  */
60a878ae04Sjsing int X25519(uint8_t out_shared_key[X25519_KEY_LENGTH],
61a878ae04Sjsing     const uint8_t private_key[X25519_KEY_LENGTH],
62a878ae04Sjsing     const uint8_t peers_public_value[X25519_KEY_LENGTH]);
635f5d09a5Sjsing 
64adc9c116Sjsing /*
65adc9c116Sjsing  * ED25519
66adc9c116Sjsing  *
67adc9c116Sjsing  * Ed25519 is a signature scheme using a twisted Edwards curve that is
68adc9c116Sjsing  * birationally equivalent to curve25519.
69adc9c116Sjsing  */
70adc9c116Sjsing 
715b0711d2Sjsing #define ED25519_PRIVATE_KEY_LENGTH	32
725b0711d2Sjsing #define ED25519_PUBLIC_KEY_LENGTH	32
735b0711d2Sjsing #define ED25519_SIGNATURE_LENGTH	64
74adc9c116Sjsing 
75adc9c116Sjsing /*
76adc9c116Sjsing  * ED25519_keypair sets |out_public_key| and |out_private_key| to a freshly
77adc9c116Sjsing  * generated, public/private key pair.
78adc9c116Sjsing  */
795b0711d2Sjsing void ED25519_keypair(uint8_t out_public_key[ED25519_PUBLIC_KEY_LENGTH],
805b0711d2Sjsing     uint8_t out_private_key[ED25519_PRIVATE_KEY_LENGTH]);
81adc9c116Sjsing 
82adc9c116Sjsing /*
83adc9c116Sjsing  * ED25519_sign sets |out_sig| to be a signature of |message_len| bytes from
845b0711d2Sjsing  * |message| using |public_key| and |private_key|. It returns one on success
855b0711d2Sjsing  * or zero on allocation failure.
86adc9c116Sjsing  */
87adc9c116Sjsing int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
885b0711d2Sjsing     const uint8_t public_key[ED25519_PUBLIC_KEY_LENGTH],
895b0711d2Sjsing     const uint8_t private_key_seed[ED25519_PRIVATE_KEY_LENGTH]);
90adc9c116Sjsing 
91adc9c116Sjsing /*
92adc9c116Sjsing  * ED25519_verify returns one iff |signature| is a valid signature by
93adc9c116Sjsing  * |public_key| of |message_len| bytes from |message|. It returns zero
94adc9c116Sjsing  * otherwise.
95adc9c116Sjsing  */
96adc9c116Sjsing int ED25519_verify(const uint8_t *message, size_t message_len,
975b0711d2Sjsing     const uint8_t signature[ED25519_SIGNATURE_LENGTH],
985b0711d2Sjsing     const uint8_t public_key[ED25519_PUBLIC_KEY_LENGTH]);
99adc9c116Sjsing 
1005f5d09a5Sjsing #if defined(__cplusplus)
1015f5d09a5Sjsing }  /* extern C */
1025f5d09a5Sjsing #endif
1035f5d09a5Sjsing 
1045f5d09a5Sjsing #endif  /* HEADER_CURVE25519_H */
105