chiark / gitweb /
2fc74447d27c5e277bb56bc5b538b9c95d9d6fba
[catacomb] / pub / ed25519.h
1 /* -*-c-*-
2  *
3  * The Ed25519 signature scheme
4  *
5  * (c) 2017 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 #ifndef CATACOMB_ED25519_H
29 #define CATACOMB_ED25519_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Notes on the Ed25519 signature scheme -----------------------------*
36  *
37  * This is Ed25519, as described in Daniel J. Bernstein, Neils Duif, Tanja
38  * Lange, Peter Schwabe, and Bo-Yin Yang, `High-speed high-security
39  * signatures', CHES 2011, https://ed25519.cr.yp.to/ed25519-20110926.pdf
40  *
41  * Specifically, this code implements `PureEdDSA', according to the
42  * definition in Daniel J. Bernstein, Simon Josefsson, Tanja Lange, Peter
43  * Schwabe, and Bo-Yin Yang, `EdDSA for more curves',
44  * https://ed25519.cr.yp.to/eddsa-20150704.pdf.  HashEdEDSA can be
45  * implemented easily by presenting a hash of a message to the functions
46  * here, as the message to be signed or verified.
47  */
48
49 /*----- Header files ------------------------------------------------------*/
50
51 #include <mLib/bits.h>
52
53 #ifndef CATACOMB_KEY_H
54 #  include "key.h"
55 #endif
56
57 /*----- Important constants -----------------------------------------------*/
58
59 #define ED25519_KEYSZ 32u
60 #define ED25519_PUBSZ 32u
61 #define ED25519_SIGSZ 64u
62
63 /*----- Key fetching ------------------------------------------------------*/
64
65 typedef struct ed25519_priv { key_bin priv, pub; } ed25519_priv;
66 typedef struct ed25519_pub { key_bin pub; } ed25519_pub;
67
68 extern const key_fetchdef ed25519_pubfetch[], ed25519_privfetch[];
69 #define ED25519_PUBFETCHSZ 3
70 #define ED25519_PRIVFETCHSZ 6
71
72 /*----- Functions provided ------------------------------------------------*/
73
74 /* --- @ed25519_pubkey@ --- *
75  *
76  * Arguments:   @octet K[ED25519_PUBSZ]@ = where to put the public key
77  *              @const void *k@ = private key
78  *              @size_t ksz@ = length of private key
79  *
80  * Returns:     ---
81  *
82  * Use:         Derives the public key from a private key.
83  */
84
85 extern void ed25519_pubkey(octet /*K*/[ED25519_PUBSZ],
86                            const void */*k*/, size_t /*ksz*/);
87
88 /* --- @ed25519_sign@ --- *
89  *
90  * Arguments:   @octet sig[ED25519_SIGSZ]@ = where to put the signature
91  *              @const void *k@ = private key
92  *              @size_t ksz@ = length of private key
93  *              @const octet K[ED25519_PUBSZ]@ = public key
94  *              @const void *m@ = message to sign
95  *              @size_t msz@ = length of message
96  *
97  * Returns:     ---
98  *
99  * Use:         Signs a message.
100  */
101
102 extern void ed25519_sign(octet /*sig*/[ED25519_SIGSZ],
103                          const void */*k*/, size_t /*ksz*/,
104                          const octet /*K*/[ED25519_PUBSZ],
105                          const void */*m*/, size_t /*msz*/);
106
107 /* --- @ed25519_verify@ --- *
108  *
109  * Arguments:   @const octet K[ED25519_PUBSZ]@ = public key
110  *              @const void *m@ = message to sign
111  *              @size_t msz@ = length of message
112  *              @const octet sig[ED25519_SIGSZ]@ = signature
113  *
114  * Returns:     Zero if OK, negative on failure.
115  *
116  * Use:         Verify a signature.
117  */
118
119 extern int ed25519_verify(const octet /*K*/[ED25519_PUBSZ],
120                           const void */*m*/, size_t /*msz*/,
121                           const octet /*sig*/[ED25519_SIGSZ]);
122
123 /*----- That's all, folks -------------------------------------------------*/
124
125 #ifdef __cplusplus
126   }
127 #endif
128
129 #endif