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-2024 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.4.4
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_curves.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  curve448Import(state->k, k);
78 
79  //Set the two least significant bits of the first byte to 0, and the most
80  //significant bit of the last byte to 1
81  state->k[0] &= 0xFFFFFFFC;
82  state->k[13] |= 0x80000000;
83 
84  //Copy input u-coordinate
85  curve448Import(state->u, u);
86 
87  //Implementations must accept non-canonical values and process them as
88  //if they had been reduced modulo the field prime (refer to RFC 7748,
89  //section 5)
90  curve448Red(state->u, state->u, 0);
91 
92  //Set X1 = 1
93  curve448SetInt(state->x1, 1);
94  //Set Z1 = 0
95  curve448SetInt(state->z1, 0);
96  //Set X2 = U
97  curve448Copy(state->x2, state->u);
98  //Set Z2 = 1
99  curve448SetInt(state->z2, 1);
100 
101  //Set swap = 0
102  swap = 0;
103 
104  //Montgomery ladder
105  for(i = CURVE448_BIT_LEN - 1; i >= 0; i--)
106  {
107  //The scalar is processed in a left-to-right fashion
108  b = (state->k[i / 32] >> (i % 32)) & 1;
109 
110  //Conditional swap
111  curve448Swap(state->x1, state->x2, swap ^ b);
112  curve448Swap(state->z1, state->z2, swap ^ b);
113 
114  //Save current bit value
115  swap = b;
116 
117  //Compute T1 = X2 + Z2
118  curve448Add(state->t1, state->x2, state->z2);
119  //Compute X2 = X2 - Z2
120  curve448Sub(state->x2, state->x2, state->z2);
121  //Compute Z2 = X1 + Z1
122  curve448Add(state->z2, state->x1, state->z1);
123  //Compute X1 = X1 - Z1
124  curve448Sub(state->x1, state->x1, state->z1);
125  //Compute T1 = T1 * X1
126  curve448Mul(state->t1, state->t1, state->x1);
127  //Compute X2 = X2 * Z2
128  curve448Mul(state->x2, state->x2, state->z2);
129  //Compute Z2 = Z2 * Z2
130  curve448Sqr(state->z2, state->z2);
131  //Compute X1 = X1 * X1
132  curve448Sqr(state->x1, state->x1);
133  //Compute T2 = Z2 - X1
134  curve448Sub(state->t2, state->z2, state->x1);
135  //Compute Z1 = T2 * a24
136  curve448MulInt(state->z1, state->t2, CURVE448_A24);
137  //Compute Z1 = Z1 + X1
138  curve448Add(state->z1, state->z1, state->x1);
139  //Compute Z1 = Z1 * T2
140  curve448Mul(state->z1, state->z1, state->t2);
141  //Compute X1 = X1 * Z2
142  curve448Mul(state->x1, state->x1, state->z2);
143  //Compute Z2 = T1 - X2
144  curve448Sub(state->z2, state->t1, state->x2);
145  //Compute Z2 = Z2 * Z2
146  curve448Sqr(state->z2, state->z2);
147  //Compute Z2 = Z2 * U
148  curve448Mul(state->z2, state->z2, state->u);
149  //Compute X2 = X2 + T1
150  curve448Add(state->x2, state->x2, state->t1);
151  //Compute X2 = X2 * X2
152  curve448Sqr(state->x2, state->x2);
153  }
154 
155  //Conditional swap
156  curve448Swap(state->x1, state->x2, swap);
157  curve448Swap(state->z1, state->z2, swap);
158 
159  //Retrieve affine representation
160  curve448Inv(state->u, state->z1);
161  curve448Mul(state->u, state->u, state->x1);
162 
163  //Copy output u-coordinate
164  curve448Export(state->u, r);
165 
166  //Erase working state
167  osMemset(state, 0, sizeof(X448State));
168 
169 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
170  //Release working state
171  cryptoFreeMem(state);
172 #endif
173 
174  //Successful processing
175  return NO_ERROR;
176 }
177 
178 #endif
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:49
#define CURVE448_BIT_LEN
Definition: curve448.h:38
void curve448Export(uint32_t *a, uint8_t *data)
Export an octet string.
Definition: curve448.c:646
void curve448Copy(uint32_t *a, const uint32_t *b)
Copy an integer.
Definition: curve448.c:527
uint32_t u[14]
Definition: x448.h:51
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
void curve448MulInt(uint32_t *r, const uint32_t *a, uint32_t b)
Modular multiplication.
Definition: curve448.c:290
uint32_t t1[14]
Definition: x448.h:56
X448 function implementation.
uint8_t r
Definition: ndp.h:346
uint32_t t2[14]
Definition: x448.h:57
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
void curve448Import(uint32_t *a, const uint8_t *data)
Import an octet string.
Definition: curve448.c:625
void curve448Inv(uint32_t *r, const uint32_t *a)
Modular multiplicative inverse.
Definition: curve448.c:402
void curve448Sqr(uint32_t *r, const uint32_t *a)
Modular squaring.
Definition: curve448.c:331
General definitions for cryptographic algorithms.
#define CURVE448_A24
Definition: curve448.h:43
uint32_t z2[14]
Definition: x448.h:55
uint8_t u
Definition: lldp_ext_med.h:213
void curve448SetInt(uint32_t *a, uint32_t b)
Set integer value.
Definition: curve448.c:50
uint32_t k[14]
Definition: x448.h:50
void curve448Swap(uint32_t *a, uint32_t *b, uint32_t c)
Conditional swap.
Definition: curve448.c:546
X448 working state.
Definition: x448.h:49
uint32_t z1[14]
Definition: x448.h:53
#define cryptoFreeMem(p)
Definition: crypto.h:791
void curve448Add(uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular addition.
Definition: curve448.c:72
#define cryptoAllocMem(size)
Definition: crypto.h:786
void curve448Sub(uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular subtraction.
Definition: curve448.c:123
void curve448Red(uint32_t *r, const uint32_t *a, uint32_t h)
Modular reduction.
Definition: curve448.c:367
Elliptic curves.
#define osMemset(p, value, length)
Definition: os_port.h:135
__weak_func void curve448Mul(uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular multiplication.
Definition: curve448.c:199
uint32_t x2[14]
Definition: x448.h:54
@ NO_ERROR
Success.
Definition: error.h:44
uint32_t x1[14]
Definition: x448.h:52
Debugging facilities.