x448.c
Go to the documentation of this file.
1 /**
2  * @file x448.c
3  * @brief X448 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/curve448.h"
38 #include "ecc/x448.h"
39 #include "debug.h"
40 
41 //Check crypto library configuration
42 #if (X448_SUPPORT == ENABLED)
43 
44 
45 /**
46  * @brief X448 function (scalar multiplication on Curve448)
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 error_t x448(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  X448State *state;
60 #else
61  X448State 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(X448State));
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 < 14; i++)
78  {
79  state->k[i] = LOAD32LE(k + i * 4);
80  }
81 
82  //Set the two least significant bits of the first byte to 0, and the most
83  //significant bit of the last byte to 1
84  state->k[0] &= 0xFFFFFFFC;
85  state->k[13] |= 0x80000000;
86 
87  //Copy input u-coordinate
88  curve448Import(state->u, u);
89 
90  //Implementations must accept non-canonical values and process them as
91  //if they had been reduced modulo the field prime (refer to RFC 7748,
92  //section 5)
93  curve448Canonicalize(state->u, state->u);
94 
95  //Set X1 = 1
96  curve448SetInt(state->x1, 1);
97  //Set Z1 = 0
98  curve448SetInt(state->z1, 0);
99  //Set X2 = U
100  curve448Copy(state->x2, state->u);
101  //Set Z2 = 1
102  curve448SetInt(state->z2, 1);
103 
104  //Set swap = 0
105  swap = 0;
106 
107  //Montgomery ladder
108  for(i = CURVE448_BIT_LEN - 1; i >= 0; i--)
109  {
110  //The scalar is processed in a left-to-right fashion
111  b = (state->k[i / 32] >> (i % 32)) & 1;
112 
113  //Conditional swap
114  curve448Swap(state->x1, state->x2, swap ^ b);
115  curve448Swap(state->z1, state->z2, swap ^ b);
116 
117  //Save current bit value
118  swap = b;
119 
120  //Compute T1 = X2 + Z2
121  curve448Add(state->t1, state->x2, state->z2);
122  //Compute X2 = X2 - Z2
123  curve448Sub(state->x2, state->x2, state->z2);
124  //Compute Z2 = X1 + Z1
125  curve448Add(state->z2, state->x1, state->z1);
126  //Compute X1 = X1 - Z1
127  curve448Sub(state->x1, state->x1, state->z1);
128  //Compute T1 = T1 * X1
129  curve448Mul(state->t1, state->t1, state->x1);
130  //Compute X2 = X2 * Z2
131  curve448Mul(state->x2, state->x2, state->z2);
132  //Compute Z2 = Z2 * Z2
133  curve448Sqr(state->z2, state->z2);
134  //Compute X1 = X1 * X1
135  curve448Sqr(state->x1, state->x1);
136  //Compute T2 = Z2 - X1
137  curve448Sub(state->t2, state->z2, state->x1);
138  //Compute Z1 = T2 * a24
139  curve448MulInt(state->z1, state->t2, CURVE448_A24);
140  //Compute Z1 = Z1 + X1
141  curve448Add(state->z1, state->z1, state->x1);
142  //Compute Z1 = Z1 * T2
143  curve448Mul(state->z1, state->z1, state->t2);
144  //Compute X1 = X1 * Z2
145  curve448Mul(state->x1, state->x1, state->z2);
146  //Compute Z2 = T1 - X2
147  curve448Sub(state->z2, state->t1, state->x2);
148  //Compute Z2 = Z2 * Z2
149  curve448Sqr(state->z2, state->z2);
150  //Compute Z2 = Z2 * U
151  curve448Mul(state->z2, state->z2, state->u);
152  //Compute X2 = X2 + T1
153  curve448Add(state->x2, state->x2, state->t1);
154  //Compute X2 = X2 * X2
155  curve448Sqr(state->x2, state->x2);
156  }
157 
158  //Conditional swap
159  curve448Swap(state->x1, state->x2, swap);
160  curve448Swap(state->z1, state->z2, swap);
161 
162  //Retrieve affine representation
163  curve448Inv(state->u, state->z1);
164  curve448Mul(state->u, state->u, state->x1);
165 
166  //Reduce non-canonical values
167  curve448Canonicalize(state->u, state->u);
168 
169  //Copy output u-coordinate
170  curve448Export(state->u, r);
171 
172  //Erase working state
173  osMemset(state, 0, sizeof(X448State));
174 
175 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
176  //Release working state
177  cryptoFreeMem(state);
178 #endif
179 
180  //Successful processing
181  return NO_ERROR;
182 }
183 
184 #endif
int32_t z2[16]
Definition: x448.h:55
int32_t t1[16]
Definition: x448.h:56
Curve448 elliptic curve (constant-time implementation)
uint8_t b
Definition: nbns_common.h:104
error_t x448(uint8_t *r, const uint8_t *k, const uint8_t *u)
X448 function (scalar multiplication on Curve448)
Definition: x448.c:53
signed int int_t
Definition: compiler_port.h:56
int32_t x2[16]
Definition: x448.h:54
#define CURVE448_BIT_LEN
Definition: curve448.h:45
void curve448Swap(int32_t *a, int32_t *b, uint32_t c)
Conditional swap.
Definition: curve448.c:931
int32_t u[16]
Definition: x448.h:51
__weak_func void curve448Sqr(int32_t *r, const int32_t *a)
Modular squaring.
Definition: curve448.c:696
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
void curve448Inv(int32_t *r, const int32_t *a)
Modular multiplicative inverse.
Definition: curve448.c:731
X448 function implementation.
uint8_t r
Definition: ndp.h:346
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
void curve448Canonicalize(int32_t *r, const int32_t *a)
Reduce non-canonical value.
Definition: curve448.c:861
error_t
Error codes.
Definition: error.h:43
int32_t z1[16]
Definition: x448.h:53
__weak_func void curve448Mul(int32_t *r, const int32_t *a, const int32_t *b)
Modular multiplication.
Definition: curve448.c:444
void curve448Copy(int32_t *a, const int32_t *b)
Copy an integer.
Definition: curve448.c:912
General definitions for cryptographic algorithms.
#define CURVE448_A24
Definition: curve448.h:50
uint8_t u
Definition: lldp_ext_med.h:213
uint32_t k[14]
Definition: x448.h:50
int32_t t2[16]
Definition: x448.h:57
void curve448Add(int32_t *r, const int32_t *a, const int32_t *b)
Modular addition.
Definition: curve448.c:72
int32_t x1[16]
Definition: x448.h:52
X448 working state.
Definition: x448.h:49
void curve448MulInt(int32_t *r, const int32_t *a, int32_t b)
Modular multiplication.
Definition: curve448.c:671
void curve448Export(int32_t *a, uint8_t *data)
Export an octet string.
Definition: curve448.c:1038
#define cryptoFreeMem(p)
Definition: crypto.h:826
void curve448Sub(int32_t *r, const int32_t *a, const int32_t *b)
Modular subtraction.
Definition: curve448.c:182
#define cryptoAllocMem(size)
Definition: crypto.h:821
void curve448SetInt(int32_t *a, int32_t b)
Set integer value.
Definition: curve448.c:50
#define LOAD32LE(p)
Definition: cpu_endian.h:203
#define osMemset(p, value, length)
Definition: os_port.h:138
ECC (Elliptic Curve Cryptography)
void curve448Import(int32_t *a, const uint8_t *data)
Import an octet string.
Definition: curve448.c:1010
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.