ed25519.h
Go to the documentation of this file.
1 /**
2  * @file ed25519.h
3  * @brief Ed25519 elliptic curve (constant-time implementation)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCrypto Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 #ifndef _ED25519_H
32 #define _ED25519_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "hash/sha512.h"
37 
38 //Length of EdDSA private keys
39 #define ED25519_PRIVATE_KEY_LEN 32
40 //Length of EdDSA public keys
41 #define ED25519_PUBLIC_KEY_LEN 32
42 //Length of EdDSA signatures
43 #define ED25519_SIGNATURE_LEN 64
44 
45 //Ed25519ph flag
46 #define ED25519_PH_FLAG 1
47 //Prehash function output size
48 #define ED25519_PH_SIZE 64
49 
50 //C++ guard
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 
56 /**
57  * @brief Extended point representation
58  **/
59 
60 typedef struct
61 {
62  uint32_t x[8];
63  uint32_t y[8];
64  uint32_t z[8];
65  uint32_t t[8];
66 } Ed25519Point;
67 
68 
69 /**
70  * @brief Ed25519 working state
71  **/
72 
73 typedef struct
74 {
76  uint8_t k[64];
77  uint8_t p[32];
78  uint8_t r[32];
79  uint8_t s[32];
85  uint32_t a[8];
86  uint32_t b[8];
87  uint32_t c[8];
88  uint32_t d[8];
89  uint32_t e[8];
90  uint32_t f[8];
91  uint32_t g[8];
92  uint32_t h[8];
93 } Ed25519State;
94 
95 
96 //Ed25519 related functions
97 error_t ed25519GenerateKeyPair(const PrngAlgo *prngAlgo, void *prngContext,
98  uint8_t *privateKey, uint8_t *publicKey);
99 
100 error_t ed25519GenerateSignature(const uint8_t *privateKey,
101  const uint8_t *publicKey, const void *message, size_t messageLen,
102  const void *context, uint8_t contextLen, uint8_t flag, uint8_t *signature);
103 
104 error_t ed25519VerifySignature(const uint8_t *publicKey, const void *message,
105  size_t messageLen, const void *context, uint8_t contextLen, uint8_t flag,
106  const uint8_t *signature);
107 
108 void ed25519Mul(Ed25519State *state, Ed25519Point *r, const uint8_t *k,
109  const Ed25519Point *p);
110 
111 void ed25519Add(Ed25519State *state, Ed25519Point *r, const Ed25519Point *p,
112  const Ed25519Point *q);
113 
114 void ed25519Double(Ed25519State *state, Ed25519Point *r, const Ed25519Point *p);
115 
116 void ed25519Encode(Ed25519Point *p, uint8_t *data);
117 uint32_t ed25519Decode(Ed25519Point *p, const uint8_t *data);
118 
119 void ed25519RedInt(uint8_t *r, const uint8_t *a);
120 
121 void ed25519AddInt(uint8_t *r, const uint8_t *a, const uint8_t *b, uint_t n);
122 uint8_t ed25519SubInt(uint8_t *r, const uint8_t *a, const uint8_t *b, uint_t n);
123 
124 void ed25519MulInt(uint8_t *rl, uint8_t *rh, const uint8_t *a,
125  const uint8_t *b, uint_t n);
126 
127 void ed25519CopyInt(uint8_t *a, const uint8_t *b, uint_t n);
128 
129 void ed25519SelectInt(uint8_t *r, const uint8_t *a, const uint8_t *b,
130  uint8_t c, uint_t n);
131 
132 uint8_t ed25519CompInt(const uint8_t *a, const uint8_t *b, uint_t n);
133 
134 //C++ guard
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif
Ed25519 working state.
Definition: ed25519.h:73
void ed25519Mul(Ed25519State *state, Ed25519Point *r, const uint8_t *k, const Ed25519Point *p)
Scalar multiplication on Ed25519 curve.
Definition: ed25519.c:409
Extended point representation.
Definition: ed25519.h:60
uint8_t a
Definition: ndp.h:410
Common interface for pseudo-random number generators.
Definition: crypto.h:1168
error_t ed25519GenerateKeyPair(const PrngAlgo *prngAlgo, void *prngContext, uint8_t *privateKey, uint8_t *publicKey)
EdDSA key pair generation.
Definition: ed25519.c:116
uint8_t ed25519SubInt(uint8_t *r, const uint8_t *a, const uint8_t *b, uint_t n)
Subtraction of two integers.
Definition: ed25519.c:687
void ed25519CopyInt(uint8_t *a, const uint8_t *b, uint_t n)
Copy an integer.
Definition: ed25519.c:770
uint8_t b[6]
Definition: dtls_misc.h:139
void ed25519Double(Ed25519State *state, Ed25519Point *r, const Ed25519Point *p)
Point doubling.
Definition: ed25519.c:499
uint8_t p
Definition: ndp.h:298
Ed25519Point rb
Definition: ed25519.h:81
uint8_t signature
Definition: tls.h:1370
SHA-512 (Secure Hash Algorithm 512)
uint32_t r
Definition: ndp.h:345
uint8_t t
Definition: llmnr_common.h:81
void ed25519RedInt(uint8_t *r, const uint8_t *a)
Reduce an integer modulo L.
Definition: ed25519.c:627
uint8_t h
Definition: ndp.h:300
uint32_t ed25519Decode(Ed25519Point *p, const uint8_t *data)
Point decoding.
Definition: ed25519.c:560
void ed25519SelectInt(uint8_t *r, const uint8_t *a, const uint8_t *b, uint8_t c, uint_t n)
Select an integer.
Definition: ed25519.c:791
error_t
Error codes.
Definition: error.h:42
Ed25519Point v
Definition: ed25519.h:84
void ed25519Encode(Ed25519Point *p, uint8_t *data)
Point encoding.
Definition: ed25519.c:535
SHA-512 algorithm context.
Definition: sha512.h:58
error_t ed25519GenerateSignature(const uint8_t *privateKey, const uint8_t *publicKey, const void *message, size_t messageLen, const void *context, uint8_t contextLen, uint8_t flag, uint8_t *signature)
EdDSA signature generation.
Definition: ed25519.c:185
General definitions for cryptographic algorithms.
Sha512Context sha512Context
Definition: ed25519.h:75
uint8_t z
Definition: dns_common.h:175
Ed25519Point u
Definition: ed25519.h:83
Ed25519Point ka
Definition: ed25519.h:80
uint8_t n
void ed25519MulInt(uint8_t *rl, uint8_t *rh, const uint8_t *a, const uint8_t *b, uint_t n)
Multiplication of two integers.
Definition: ed25519.c:715
uint8_t s
uint8_t message[]
Definition: chap.h:152
Ed25519Point sb
Definition: ed25519.h:82
void ed25519AddInt(uint8_t *r, const uint8_t *a, const uint8_t *b, uint_t n)
Addition of two integers.
Definition: ed25519.c:662
unsigned int uint_t
Definition: compiler_port.h:45
uint8_t data[]
Definition: dtls_misc.h:176
void ed25519Add(Ed25519State *state, Ed25519Point *r, const Ed25519Point *p, const Ed25519Point *q)
Point addition.
Definition: ed25519.c:456
uint8_t ed25519CompInt(const uint8_t *a, const uint8_t *b, uint_t n)
Compare integers.
Definition: ed25519.c:817
uint8_t c
Definition: ndp.h:513
error_t ed25519VerifySignature(const uint8_t *publicKey, const void *message, size_t messageLen, const void *context, uint8_t contextLen, uint8_t flag, const uint8_t *signature)
EdDSA signature verification.
Definition: ed25519.c:315