chiark / gitweb /
Merge branch '2.4.x' into 2.5.x
[catacomb] / pub / x25519.h
1 /* -*-c-*-
2  *
3  * The X25519 key-agreement algorithm
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_X25519_H
29 #define CATACOMB_X25519_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Notes on the X25519 key-agreement algorithm -----------------------*
36  *
37  * This is X25519, as described in Daniel J. Bernstein, `Curve25519: new
38  * Diffie--Hellman speed records', PKC 2006,
39  * https://cr.yp.to/ecdh/curve25519-20060209.pdf
40  *
41  * Since then, the name `Curve25519' has shifted somewhat, to refer to the
42  * specific elliptic curve used, and the x-coordinate Diffie--Hellman
43  * operation is now named `X25519'.
44  */
45
46 /*----- Header files ------------------------------------------------------*/
47
48 #include <mLib/bits.h>
49
50 #ifndef CATACOMB_KEY_H
51 #  include "key.h"
52 #endif
53
54 /*----- Important constants -----------------------------------------------*/
55
56 #define X25519_KEYSZ 32u
57 #define X25519_PUBSZ 32u
58 #define X25519_OUTSZ 32u
59
60 extern const octet x25519_base[32];
61
62 /*----- Key fetching ------------------------------------------------------*/
63
64 typedef struct x25519_priv { key_bin priv, pub; } x25519_priv;
65 typedef struct x25519_pub { key_bin pub; } x25519_pub;
66
67 extern const key_fetchdef x25519_pubfetch[], x25519_privfetch[];
68 #define X25519_PUBFETCHSZ 3
69 #define X25519_PRIVFETCHSZ 6
70
71 /*----- Functions provided ------------------------------------------------*/
72
73 /* --- @x25519@ --- *
74  *
75  * Arguments:   @octet zz[X25519_OUTSZ]@ = where to put the result
76  *              @const octet k[X25519_KEYSZ]@ = pointer to private key
77  *              @const octet qx[X25519_PUBSZ]@ = pointer to public value
78  *
79  * Returns:     ---
80  *
81  * Use:         Calculates X25519 of @k@ and @qx@.
82  *
83  *              Note that there is disagreement over whether the most
84  *              significant bit of @qx@ (i.e., the value @qx[31]&0x80@)
85  *              should be ignored or counted towards the represented value.
86  *              Historically implementations respected the bit; later
87  *              convention seems to be to ignore it.  This implementation
88  *              honours the bit: a caller who wants to ignore the bit can
89  *              easily clear it, while caller who wants to respect it has a
90  *              difficult job if this function ignores it.
91  */
92
93 extern void x25519(octet /*zz*/[X25519_OUTSZ],
94                    const octet /*k*/[X25519_KEYSZ],
95                    const octet /*qx*/[X25519_PUBSZ]);
96
97 /*----- That's all, folks -------------------------------------------------*/
98
99 #ifdef __cplusplus
100   }
101 #endif
102
103 #endif