x25519.c
Go to the documentation of this file.
1 /**
2  * @file x25519.c
3  * @brief X25519 function implementation
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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 2.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "ecc/ec.h"
37 #include "ecc/curve25519.h"
38 #include "ecc/x25519.h"
39 #include "debug.h"
40 
41 //Check crypto library configuration
42 #if (X25519_SUPPORT == ENABLED)
43 
44 
45 /**
46  * @brief X25519 function (scalar multiplication on Curve25519)
47  * @param[out] r Output u-coordinate
48  * @param[in] k Input scalar
49  * @param[in] u Input u-coordinate
50  * @return Error code
51  **/
52 
53 __weak_func error_t x25519(uint8_t *r, const uint8_t *k, const uint8_t *u)
54 {
55  int_t i;
56  uint32_t b;
57  uint32_t swap;
58 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
59  X25519State *state;
60 #else
61  X25519State state[1];
62 #endif
63 
64  //Check parameters
65  if(r == NULL || k == NULL || u == NULL)
67 
68 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
69  //Allocate working state
70  state = cryptoAllocMem(sizeof(X25519State));
71  //Failed to allocate memory?
72  if(state == NULL)
73  return ERROR_OUT_OF_MEMORY;
74 #endif
75 
76  //Copy scalar
77  for(i = 0; i < 8; i++)
78  {
79  state->k[i] = LOAD32LE(k + i * 4);
80  }
81 
82  //Set the three least significant bits of the first byte and the most
83  //significant bit of the last to zero, set the second most significant
84  //bit of the last byte to 1
85  state->k[0] &= 0xFFFFFFF8;
86  state->k[7] &= 0x7FFFFFFF;
87  state->k[7] |= 0x40000000;
88 
89  //Copy input u-coordinate
90  curve25519Import(state->u, u);
91 
92  //Implementations must mask the most significant bit in the final byte
93  state->u[8] &= 0x007FFFFF;
94 
95  //Implementations must accept non-canonical values and process them as
96  //if they had been reduced modulo the field prime (refer to RFC 7748,
97  //section 5)
98  curve25519Canonicalize(state->u, state->u);
99 
100  //Set X1 = 1
101  curve25519SetInt(state->x1, 1);
102  //Set Z1 = 0
103  curve25519SetInt(state->z1, 0);
104  //Set X2 = U
105  curve25519Copy(state->x2, state->u);
106  //Set Z2 = 1
107  curve25519SetInt(state->z2, 1);
108 
109  //Set swap = 0
110  swap = 0;
111 
112  //Montgomery ladder
113  for(i = CURVE25519_BIT_LEN - 1; i >= 0; i--)
114  {
115  //The scalar is processed in a left-to-right fashion
116  b = (state->k[i / 32] >> (i % 32)) & 1;
117 
118  //Conditional swap
119  curve25519Swap(state->x1, state->x2, swap ^ b);
120  curve25519Swap(state->z1, state->z2, swap ^ b);
121 
122  //Save current bit value
123  swap = b;
124 
125  //Compute T1 = X2 + Z2
126  curve25519Add(state->t1, state->x2, state->z2);
127  //Compute X2 = X2 - Z2
128  curve25519Sub(state->x2, state->x2, state->z2);
129  //Compute Z2 = X1 + Z1
130  curve25519Add(state->z2, state->x1, state->z1);
131  //Compute X1 = X1 - Z1
132  curve25519Sub(state->x1, state->x1, state->z1);
133  //Compute T1 = T1 * X1
134  curve25519Mul(state->t1, state->t1, state->x1);
135  //Compute X2 = X2 * Z2
136  curve25519Mul(state->x2, state->x2, state->z2);
137  //Compute Z2 = Z2 * Z2
138  curve25519Sqr(state->z2, state->z2);
139  //Compute X1 = X1 * X1
140  curve25519Sqr(state->x1, state->x1);
141  //Compute T2 = Z2 - X1
142  curve25519Sub(state->t2, state->z2, state->x1);
143  //Compute Z1 = T2 * a24
144  curve25519MulInt(state->z1, state->t2, CURVE25519_A24);
145  //Compute Z1 = Z1 + X1
146  curve25519Add(state->z1, state->z1, state->x1);
147  //Compute Z1 = Z1 * T2
148  curve25519Mul(state->z1, state->z1, state->t2);
149  //Compute X1 = X1 * Z2
150  curve25519Mul(state->x1, state->x1, state->z2);
151  //Compute Z2 = T1 - X2
152  curve25519Sub(state->z2, state->t1, state->x2);
153  //Compute Z2 = Z2 * Z2
154  curve25519Sqr(state->z2, state->z2);
155  //Compute Z2 = Z2 * U
156  curve25519Mul(state->z2, state->z2, state->u);
157  //Compute X2 = X2 + T1
158  curve25519Add(state->x2, state->x2, state->t1);
159  //Compute X2 = X2 * X2
160  curve25519Sqr(state->x2, state->x2);
161  }
162 
163  //Conditional swap
164  curve25519Swap(state->x1, state->x2, swap);
165  curve25519Swap(state->z1, state->z2, swap);
166 
167  //Retrieve affine representation
168  curve25519Inv(state->u, state->z1);
169  curve25519Mul(state->u, state->u, state->x1);
170 
171  //Reduce non-canonical values
172  curve25519Canonicalize(state->u, state->u);
173 
174  //Copy output u-coordinate
175  curve25519Export(state->u, r);
176 
177  //Erase working state
178  osMemset(state, 0, sizeof(X25519State));
179 
180 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
181  //Release working state
182  cryptoFreeMem(state);
183 #endif
184 
185  //Successful processing
186  return NO_ERROR;
187 }
188 
189 #endif
int32_t t2[9]
Definition: x25519.h:57
int32_t z2[9]
Definition: x25519.h:55
void curve25519Add(int32_t *r, const int32_t *a, const int32_t *b)
Modular addition.
Definition: curve25519.c:79
int32_t x2[9]
Definition: x25519.h:54
void curve25519Canonicalize(int32_t *r, const int32_t *a)
Reduce non-canonical value.
Definition: curve25519.c:749
uint8_t b
Definition: nbns_common.h:104
signed int int_t
Definition: compiler_port.h:56
void curve25519Export(int32_t *a, uint8_t *data)
Export an octet string.
Definition: curve25519.c:917
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
__weak_func error_t x25519(uint8_t *r, const uint8_t *k, const uint8_t *u)
X25519 function (scalar multiplication on Curve25519)
Definition: x25519.c:53
uint8_t r
Definition: ndp.h:346
#define CURVE25519_A24
Definition: curve25519.h:50
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
int32_t t1[9]
Definition: x25519.h:56
error_t
Error codes.
Definition: error.h:43
int32_t x1[9]
Definition: x25519.h:52
General definitions for cryptographic algorithms.
__weak_func void curve25519Sqr(int32_t *r, const int32_t *a)
Modular squaring.
Definition: curve25519.c:571
void curve25519Copy(int32_t *a, const int32_t *b)
Copy an integer.
Definition: curve25519.c:798
uint8_t u
Definition: lldp_ext_med.h:213
void curve25519Swap(int32_t *a, int32_t *b, uint32_t c)
Conditional swap.
Definition: curve25519.c:817
X25519 function implementation.
uint32_t k[8]
Definition: x25519.h:50
#define CURVE25519_BIT_LEN
Definition: curve25519.h:45
void curve25519SetInt(int32_t *a, int32_t b)
Set integer value.
Definition: curve25519.c:57
void curve25519Inv(int32_t *r, const int32_t *a)
Modular multiplicative inverse.
Definition: curve25519.c:606
void curve25519Sub(int32_t *r, const int32_t *a, const int32_t *b)
Modular subtraction.
Definition: curve25519.c:173
int32_t z1[9]
Definition: x25519.h:53
#define cryptoFreeMem(p)
Definition: crypto.h:826
Curve25519 elliptic curve (constant-time implementation)
void curve25519MulInt(int32_t *r, const int32_t *a, int32_t b)
Modular multiplication.
Definition: curve25519.c:499
#define cryptoAllocMem(size)
Definition: crypto.h:821
void curve25519Import(int32_t *a, const uint8_t *data)
Import an octet string.
Definition: curve25519.c:896
#define LOAD32LE(p)
Definition: cpu_endian.h:203
X25519 working state.
Definition: x25519.h:49
__weak_func void curve25519Mul(int32_t *r, const int32_t *a, const int32_t *b)
Modular multiplication.
Definition: curve25519.c:267
#define osMemset(p, value, length)
Definition: os_port.h:138
ECC (Elliptic Curve Cryptography)
@ NO_ERROR
Success.
Definition: error.h:44
int32_t u[9]
Definition: x25519.h:51
Debugging facilities.