2 * Elliptic curves over GF(p): curve-specific data and functions
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #if defined(MBEDTLS_ECP_C)
24 #include "mbedtls/ecp.h"
25 #include "mbedtls/platform_util.h"
26 #include "mbedtls/error.h"
30 #if !defined(MBEDTLS_ECP_ALT)
32 /* Parameter validation macros based on platform_util.h */
33 #define ECP_VALIDATE_RET( cond ) \
34 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
35 #define ECP_VALIDATE( cond ) \
36 MBEDTLS_INTERNAL_VALIDATE( cond )
38 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
39 !defined(inline) && !defined(__cplusplus)
40 #define inline __inline
44 * Conversion macros for embedded constants:
45 * build lists of mbedtls_mpi_uint's from lists of unsigned char's grouped by 8, 4 or 2
47 #if defined(MBEDTLS_HAVE_INT32)
49 #define BYTES_TO_T_UINT_4( a, b, c, d ) \
50 ( (mbedtls_mpi_uint) (a) << 0 ) | \
51 ( (mbedtls_mpi_uint) (b) << 8 ) | \
52 ( (mbedtls_mpi_uint) (c) << 16 ) | \
53 ( (mbedtls_mpi_uint) (d) << 24 )
55 #define BYTES_TO_T_UINT_2( a, b ) \
56 BYTES_TO_T_UINT_4( a, b, 0, 0 )
58 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
59 BYTES_TO_T_UINT_4( a, b, c, d ), \
60 BYTES_TO_T_UINT_4( e, f, g, h )
64 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
65 ( (mbedtls_mpi_uint) (a) << 0 ) | \
66 ( (mbedtls_mpi_uint) (b) << 8 ) | \
67 ( (mbedtls_mpi_uint) (c) << 16 ) | \
68 ( (mbedtls_mpi_uint) (d) << 24 ) | \
69 ( (mbedtls_mpi_uint) (e) << 32 ) | \
70 ( (mbedtls_mpi_uint) (f) << 40 ) | \
71 ( (mbedtls_mpi_uint) (g) << 48 ) | \
72 ( (mbedtls_mpi_uint) (h) << 56 )
74 #define BYTES_TO_T_UINT_4( a, b, c, d ) \
75 BYTES_TO_T_UINT_8( a, b, c, d, 0, 0, 0, 0 )
77 #define BYTES_TO_T_UINT_2( a, b ) \
78 BYTES_TO_T_UINT_8( a, b, 0, 0, 0, 0, 0, 0 )
80 #endif /* bits in mbedtls_mpi_uint */
83 * Note: the constants are in little-endian order
84 * to be directly usable in MPIs
88 * Domain parameters for secp128r1
90 #if defined(MBEDTLS_ECP_DP_SECP128R1_ENABLED)
91 static const mbedtls_mpi_uint secp128r1_p
[] = {
92 // 2^128 - 2^97 - 1 // TODO
93 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
94 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFD, 0xFF, 0xFF, 0xFF),
96 static const mbedtls_mpi_uint secp128r1_a
[] = {
97 // FFFFFFFDFFFFFFFF FFFFFFFFFFFFFFFC
98 BYTES_TO_T_UINT_8(0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
99 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFD, 0xFF, 0xFF, 0xFF),
101 static const mbedtls_mpi_uint secp128r1_b
[] = {
102 // E87579C11079F43D D824993C2CEE5ED3
103 BYTES_TO_T_UINT_8(0xD3, 0x5E, 0xEE, 0x2C, 0x3C, 0x99, 0x24, 0xD8),
104 BYTES_TO_T_UINT_8(0x3D, 0xF4, 0x79, 0x10, 0xC1, 0x79, 0x75, 0xE8),
106 static const mbedtls_mpi_uint secp128r1_gx
[] = {
107 // 161FF7528B899B2D 0C28607CA52C5B86
108 BYTES_TO_T_UINT_8(0x86, 0x5B, 0x2C, 0xA5, 0x7C, 0x60, 0x28, 0x0C),
109 BYTES_TO_T_UINT_8(0x2D, 0x9B, 0x89, 0x8B, 0x52, 0xF7, 0x1F, 0x16),
111 static const mbedtls_mpi_uint secp128r1_gy
[] = {
112 // CF5AC8395BAFEB13 C02DA292DDED7A83
113 BYTES_TO_T_UINT_8(0x83, 0x7A, 0xED, 0xDD, 0x92, 0xA2, 0x2D, 0xC0),
114 BYTES_TO_T_UINT_8(0x13, 0xEB, 0xAF, 0x5B, 0x39, 0xC8, 0x5A, 0xCF),
116 static const mbedtls_mpi_uint secp128r1_n
[] = {
117 // FFFFFFFE00000000 75A30D1B9038A115
118 BYTES_TO_T_UINT_8(0x15, 0xA1, 0x38, 0x90, 0x1B, 0x0D, 0xA3, 0x75),
119 BYTES_TO_T_UINT_8(0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0xFF),
121 #endif /* MBEDTLS_ECP_DP_SECP128R1_ENABLED */
124 * Domain parameters for secp192r1
126 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
127 static const mbedtls_mpi_uint secp192r1_p
[] = {
128 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
129 BYTES_TO_T_UINT_8(0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
130 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
132 static const mbedtls_mpi_uint secp192r1_b
[] = {
133 BYTES_TO_T_UINT_8(0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE),
134 BYTES_TO_T_UINT_8(0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F),
135 BYTES_TO_T_UINT_8(0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64),
137 static const mbedtls_mpi_uint secp192r1_gx
[] = {
138 BYTES_TO_T_UINT_8(0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4),
139 BYTES_TO_T_UINT_8(0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C),
140 BYTES_TO_T_UINT_8(0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18),
142 static const mbedtls_mpi_uint secp192r1_gy
[] = {
143 BYTES_TO_T_UINT_8(0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73),
144 BYTES_TO_T_UINT_8(0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63),
145 BYTES_TO_T_UINT_8(0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07),
147 static const mbedtls_mpi_uint secp192r1_n
[] = {
148 BYTES_TO_T_UINT_8(0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14),
149 BYTES_TO_T_UINT_8(0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF),
150 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
152 #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
155 * Domain parameters for secp224r1
157 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
158 static const mbedtls_mpi_uint secp224r1_p
[] = {
159 BYTES_TO_T_UINT_8(0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
160 BYTES_TO_T_UINT_8(0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF),
161 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
162 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00),
164 static const mbedtls_mpi_uint secp224r1_b
[] = {
165 BYTES_TO_T_UINT_8(0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27),
166 BYTES_TO_T_UINT_8(0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50),
167 BYTES_TO_T_UINT_8(0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C),
168 BYTES_TO_T_UINT_4(0x85, 0x0A, 0x05, 0xB4),
170 static const mbedtls_mpi_uint secp224r1_gx
[] = {
171 BYTES_TO_T_UINT_8(0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34),
172 BYTES_TO_T_UINT_8(0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A),
173 BYTES_TO_T_UINT_8(0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B),
174 BYTES_TO_T_UINT_4(0xBD, 0x0C, 0x0E, 0xB7),
176 static const mbedtls_mpi_uint secp224r1_gy
[] = {
177 BYTES_TO_T_UINT_8(0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44),
178 BYTES_TO_T_UINT_8(0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD),
179 BYTES_TO_T_UINT_8(0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5),
180 BYTES_TO_T_UINT_4(0x88, 0x63, 0x37, 0xBD),
182 static const mbedtls_mpi_uint secp224r1_n
[] = {
183 BYTES_TO_T_UINT_8(0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13),
184 BYTES_TO_T_UINT_8(0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF),
185 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
186 BYTES_TO_T_UINT_4(0xFF, 0xFF, 0xFF, 0xFF),
188 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
191 * Domain parameters for secp256r1
193 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
194 static const mbedtls_mpi_uint secp256r1_p
[] = {
195 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
196 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00),
197 BYTES_TO_T_UINT_8(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
198 BYTES_TO_T_UINT_8(0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF),
200 static const mbedtls_mpi_uint secp256r1_b
[] = {
201 BYTES_TO_T_UINT_8(0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B),
202 BYTES_TO_T_UINT_8(0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65),
203 BYTES_TO_T_UINT_8(0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3),
204 BYTES_TO_T_UINT_8(0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A),
206 static const mbedtls_mpi_uint secp256r1_gx
[] = {
207 BYTES_TO_T_UINT_8(0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4),
208 BYTES_TO_T_UINT_8(0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77),
209 BYTES_TO_T_UINT_8(0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8),
210 BYTES_TO_T_UINT_8(0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B),
212 static const mbedtls_mpi_uint secp256r1_gy
[] = {
213 BYTES_TO_T_UINT_8(0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB),
214 BYTES_TO_T_UINT_8(0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B),
215 BYTES_TO_T_UINT_8(0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E),
216 BYTES_TO_T_UINT_8(0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F),
218 static const mbedtls_mpi_uint secp256r1_n
[] = {
219 BYTES_TO_T_UINT_8(0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3),
220 BYTES_TO_T_UINT_8(0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC),
221 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
222 BYTES_TO_T_UINT_8(0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF),
224 #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
227 * Domain parameters for secp384r1
229 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
230 static const mbedtls_mpi_uint secp384r1_p
[] = {
231 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00),
232 BYTES_TO_T_UINT_8(0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF),
233 BYTES_TO_T_UINT_8(0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
234 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
235 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
236 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
238 static const mbedtls_mpi_uint secp384r1_b
[] = {
239 BYTES_TO_T_UINT_8(0xEF, 0x2A, 0xEC, 0xD3, 0xED, 0xC8, 0x85, 0x2A),
240 BYTES_TO_T_UINT_8(0x9D, 0xD1, 0x2E, 0x8A, 0x8D, 0x39, 0x56, 0xC6),
241 BYTES_TO_T_UINT_8(0x5A, 0x87, 0x13, 0x50, 0x8F, 0x08, 0x14, 0x03),
242 BYTES_TO_T_UINT_8(0x12, 0x41, 0x81, 0xFE, 0x6E, 0x9C, 0x1D, 0x18),
243 BYTES_TO_T_UINT_8(0x19, 0x2D, 0xF8, 0xE3, 0x6B, 0x05, 0x8E, 0x98),
244 BYTES_TO_T_UINT_8(0xE4, 0xE7, 0x3E, 0xE2, 0xA7, 0x2F, 0x31, 0xB3),
246 static const mbedtls_mpi_uint secp384r1_gx
[] = {
247 BYTES_TO_T_UINT_8(0xB7, 0x0A, 0x76, 0x72, 0x38, 0x5E, 0x54, 0x3A),
248 BYTES_TO_T_UINT_8(0x6C, 0x29, 0x55, 0xBF, 0x5D, 0xF2, 0x02, 0x55),
249 BYTES_TO_T_UINT_8(0x38, 0x2A, 0x54, 0x82, 0xE0, 0x41, 0xF7, 0x59),
250 BYTES_TO_T_UINT_8(0x98, 0x9B, 0xA7, 0x8B, 0x62, 0x3B, 0x1D, 0x6E),
251 BYTES_TO_T_UINT_8(0x74, 0xAD, 0x20, 0xF3, 0x1E, 0xC7, 0xB1, 0x8E),
252 BYTES_TO_T_UINT_8(0x37, 0x05, 0x8B, 0xBE, 0x22, 0xCA, 0x87, 0xAA),
254 static const mbedtls_mpi_uint secp384r1_gy
[] = {
255 BYTES_TO_T_UINT_8(0x5F, 0x0E, 0xEA, 0x90, 0x7C, 0x1D, 0x43, 0x7A),
256 BYTES_TO_T_UINT_8(0x9D, 0x81, 0x7E, 0x1D, 0xCE, 0xB1, 0x60, 0x0A),
257 BYTES_TO_T_UINT_8(0xC0, 0xB8, 0xF0, 0xB5, 0x13, 0x31, 0xDA, 0xE9),
258 BYTES_TO_T_UINT_8(0x7C, 0x14, 0x9A, 0x28, 0xBD, 0x1D, 0xF4, 0xF8),
259 BYTES_TO_T_UINT_8(0x29, 0xDC, 0x92, 0x92, 0xBF, 0x98, 0x9E, 0x5D),
260 BYTES_TO_T_UINT_8(0x6F, 0x2C, 0x26, 0x96, 0x4A, 0xDE, 0x17, 0x36),
262 static const mbedtls_mpi_uint secp384r1_n
[] = {
263 BYTES_TO_T_UINT_8(0x73, 0x29, 0xC5, 0xCC, 0x6A, 0x19, 0xEC, 0xEC),
264 BYTES_TO_T_UINT_8(0x7A, 0xA7, 0xB0, 0x48, 0xB2, 0x0D, 0x1A, 0x58),
265 BYTES_TO_T_UINT_8(0xDF, 0x2D, 0x37, 0xF4, 0x81, 0x4D, 0x63, 0xC7),
266 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
267 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
268 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
270 #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
273 * Domain parameters for secp521r1
275 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
276 static const mbedtls_mpi_uint secp521r1_p
[] = {
277 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
278 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
279 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
280 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
281 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
282 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
283 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
284 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
285 BYTES_TO_T_UINT_2(0xFF, 0x01),
287 static const mbedtls_mpi_uint secp521r1_b
[] = {
288 BYTES_TO_T_UINT_8(0x00, 0x3F, 0x50, 0x6B, 0xD4, 0x1F, 0x45, 0xEF),
289 BYTES_TO_T_UINT_8(0xF1, 0x34, 0x2C, 0x3D, 0x88, 0xDF, 0x73, 0x35),
290 BYTES_TO_T_UINT_8(0x07, 0xBF, 0xB1, 0x3B, 0xBD, 0xC0, 0x52, 0x16),
291 BYTES_TO_T_UINT_8(0x7B, 0x93, 0x7E, 0xEC, 0x51, 0x39, 0x19, 0x56),
292 BYTES_TO_T_UINT_8(0xE1, 0x09, 0xF1, 0x8E, 0x91, 0x89, 0xB4, 0xB8),
293 BYTES_TO_T_UINT_8(0xF3, 0x15, 0xB3, 0x99, 0x5B, 0x72, 0xDA, 0xA2),
294 BYTES_TO_T_UINT_8(0xEE, 0x40, 0x85, 0xB6, 0xA0, 0x21, 0x9A, 0x92),
295 BYTES_TO_T_UINT_8(0x1F, 0x9A, 0x1C, 0x8E, 0x61, 0xB9, 0x3E, 0x95),
296 BYTES_TO_T_UINT_2(0x51, 0x00),
298 static const mbedtls_mpi_uint secp521r1_gx
[] = {
299 BYTES_TO_T_UINT_8(0x66, 0xBD, 0xE5, 0xC2, 0x31, 0x7E, 0x7E, 0xF9),
300 BYTES_TO_T_UINT_8(0x9B, 0x42, 0x6A, 0x85, 0xC1, 0xB3, 0x48, 0x33),
301 BYTES_TO_T_UINT_8(0xDE, 0xA8, 0xFF, 0xA2, 0x27, 0xC1, 0x1D, 0xFE),
302 BYTES_TO_T_UINT_8(0x28, 0x59, 0xE7, 0xEF, 0x77, 0x5E, 0x4B, 0xA1),
303 BYTES_TO_T_UINT_8(0xBA, 0x3D, 0x4D, 0x6B, 0x60, 0xAF, 0x28, 0xF8),
304 BYTES_TO_T_UINT_8(0x21, 0xB5, 0x3F, 0x05, 0x39, 0x81, 0x64, 0x9C),
305 BYTES_TO_T_UINT_8(0x42, 0xB4, 0x95, 0x23, 0x66, 0xCB, 0x3E, 0x9E),
306 BYTES_TO_T_UINT_8(0xCD, 0xE9, 0x04, 0x04, 0xB7, 0x06, 0x8E, 0x85),
307 BYTES_TO_T_UINT_2(0xC6, 0x00),
309 static const mbedtls_mpi_uint secp521r1_gy
[] = {
310 BYTES_TO_T_UINT_8(0x50, 0x66, 0xD1, 0x9F, 0x76, 0x94, 0xBE, 0x88),
311 BYTES_TO_T_UINT_8(0x40, 0xC2, 0x72, 0xA2, 0x86, 0x70, 0x3C, 0x35),
312 BYTES_TO_T_UINT_8(0x61, 0x07, 0xAD, 0x3F, 0x01, 0xB9, 0x50, 0xC5),
313 BYTES_TO_T_UINT_8(0x40, 0x26, 0xF4, 0x5E, 0x99, 0x72, 0xEE, 0x97),
314 BYTES_TO_T_UINT_8(0x2C, 0x66, 0x3E, 0x27, 0x17, 0xBD, 0xAF, 0x17),
315 BYTES_TO_T_UINT_8(0x68, 0x44, 0x9B, 0x57, 0x49, 0x44, 0xF5, 0x98),
316 BYTES_TO_T_UINT_8(0xD9, 0x1B, 0x7D, 0x2C, 0xB4, 0x5F, 0x8A, 0x5C),
317 BYTES_TO_T_UINT_8(0x04, 0xC0, 0x3B, 0x9A, 0x78, 0x6A, 0x29, 0x39),
318 BYTES_TO_T_UINT_2(0x18, 0x01),
320 static const mbedtls_mpi_uint secp521r1_n
[] = {
321 BYTES_TO_T_UINT_8(0x09, 0x64, 0x38, 0x91, 0x1E, 0xB7, 0x6F, 0xBB),
322 BYTES_TO_T_UINT_8(0xAE, 0x47, 0x9C, 0x89, 0xB8, 0xC9, 0xB5, 0x3B),
323 BYTES_TO_T_UINT_8(0xD0, 0xA5, 0x09, 0xF7, 0x48, 0x01, 0xCC, 0x7F),
324 BYTES_TO_T_UINT_8(0x6B, 0x96, 0x2F, 0xBF, 0x83, 0x87, 0x86, 0x51),
325 BYTES_TO_T_UINT_8(0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
326 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
327 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
328 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
329 BYTES_TO_T_UINT_2(0xFF, 0x01),
331 #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
333 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
334 static const mbedtls_mpi_uint secp192k1_p
[] = {
335 BYTES_TO_T_UINT_8(0x37, 0xEE, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF),
336 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
337 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
339 static const mbedtls_mpi_uint secp192k1_a
[] = {
340 BYTES_TO_T_UINT_2(0x00, 0x00),
342 static const mbedtls_mpi_uint secp192k1_b
[] = {
343 BYTES_TO_T_UINT_2(0x03, 0x00),
345 static const mbedtls_mpi_uint secp192k1_gx
[] = {
346 BYTES_TO_T_UINT_8(0x7D, 0x6C, 0xE0, 0xEA, 0xB1, 0xD1, 0xA5, 0x1D),
347 BYTES_TO_T_UINT_8(0x34, 0xF4, 0xB7, 0x80, 0x02, 0x7D, 0xB0, 0x26),
348 BYTES_TO_T_UINT_8(0xAE, 0xE9, 0x57, 0xC0, 0x0E, 0xF1, 0x4F, 0xDB),
350 static const mbedtls_mpi_uint secp192k1_gy
[] = {
351 BYTES_TO_T_UINT_8(0x9D, 0x2F, 0x5E, 0xD9, 0x88, 0xAA, 0x82, 0x40),
352 BYTES_TO_T_UINT_8(0x34, 0x86, 0xBE, 0x15, 0xD0, 0x63, 0x41, 0x84),
353 BYTES_TO_T_UINT_8(0xA7, 0x28, 0x56, 0x9C, 0x6D, 0x2F, 0x2F, 0x9B),
355 static const mbedtls_mpi_uint secp192k1_n
[] = {
356 BYTES_TO_T_UINT_8(0x8D, 0xFD, 0xDE, 0x74, 0x6A, 0x46, 0x69, 0x0F),
357 BYTES_TO_T_UINT_8(0x17, 0xFC, 0xF2, 0x26, 0xFE, 0xFF, 0xFF, 0xFF),
358 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
360 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
362 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
363 static const mbedtls_mpi_uint secp224k1_p
[] = {
364 BYTES_TO_T_UINT_8(0x6D, 0xE5, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF),
365 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
366 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
367 BYTES_TO_T_UINT_4(0xFF, 0xFF, 0xFF, 0xFF),
369 static const mbedtls_mpi_uint secp224k1_a
[] = {
370 BYTES_TO_T_UINT_2(0x00, 0x00),
372 static const mbedtls_mpi_uint secp224k1_b
[] = {
373 BYTES_TO_T_UINT_2(0x05, 0x00),
375 static const mbedtls_mpi_uint secp224k1_gx
[] = {
376 BYTES_TO_T_UINT_8(0x5C, 0xA4, 0xB7, 0xB6, 0x0E, 0x65, 0x7E, 0x0F),
377 BYTES_TO_T_UINT_8(0xA9, 0x75, 0x70, 0xE4, 0xE9, 0x67, 0xA4, 0x69),
378 BYTES_TO_T_UINT_8(0xA1, 0x28, 0xFC, 0x30, 0xDF, 0x99, 0xF0, 0x4D),
379 BYTES_TO_T_UINT_4(0x33, 0x5B, 0x45, 0xA1),
381 static const mbedtls_mpi_uint secp224k1_gy
[] = {
382 BYTES_TO_T_UINT_8(0xA5, 0x61, 0x6D, 0x55, 0xDB, 0x4B, 0xCA, 0xE2),
383 BYTES_TO_T_UINT_8(0x59, 0xBD, 0xB0, 0xC0, 0xF7, 0x19, 0xE3, 0xF7),
384 BYTES_TO_T_UINT_8(0xD6, 0xFB, 0xCA, 0x82, 0x42, 0x34, 0xBA, 0x7F),
385 BYTES_TO_T_UINT_4(0xED, 0x9F, 0x08, 0x7E),
387 static const mbedtls_mpi_uint secp224k1_n
[] = {
388 BYTES_TO_T_UINT_8(0xF7, 0xB1, 0x9F, 0x76, 0x71, 0xA9, 0xF0, 0xCA),
389 BYTES_TO_T_UINT_8(0x84, 0x61, 0xEC, 0xD2, 0xE8, 0xDC, 0x01, 0x00),
390 BYTES_TO_T_UINT_8(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
391 BYTES_TO_T_UINT_8(0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
393 #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
395 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
396 static const mbedtls_mpi_uint secp256k1_p
[] = {
397 BYTES_TO_T_UINT_8(0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF),
398 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
399 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
400 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
402 static const mbedtls_mpi_uint secp256k1_a
[] = {
403 BYTES_TO_T_UINT_2(0x00, 0x00),
405 static const mbedtls_mpi_uint secp256k1_b
[] = {
406 BYTES_TO_T_UINT_2(0x07, 0x00),
408 static const mbedtls_mpi_uint secp256k1_gx
[] = {
409 BYTES_TO_T_UINT_8(0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59),
410 BYTES_TO_T_UINT_8(0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02),
411 BYTES_TO_T_UINT_8(0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55),
412 BYTES_TO_T_UINT_8(0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79),
414 static const mbedtls_mpi_uint secp256k1_gy
[] = {
415 BYTES_TO_T_UINT_8(0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C),
416 BYTES_TO_T_UINT_8(0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD),
417 BYTES_TO_T_UINT_8(0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D),
418 BYTES_TO_T_UINT_8(0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48),
420 static const mbedtls_mpi_uint secp256k1_n
[] = {
421 BYTES_TO_T_UINT_8(0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF),
422 BYTES_TO_T_UINT_8(0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA),
423 BYTES_TO_T_UINT_8(0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
424 BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
426 #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
429 * Domain parameters for brainpoolP256r1 (RFC 5639 3.4)
431 #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
432 static const mbedtls_mpi_uint brainpoolP256r1_p
[] = {
433 BYTES_TO_T_UINT_8(0x77, 0x53, 0x6E, 0x1F, 0x1D, 0x48, 0x13, 0x20),
434 BYTES_TO_T_UINT_8(0x28, 0x20, 0x26, 0xD5, 0x23, 0xF6, 0x3B, 0x6E),
435 BYTES_TO_T_UINT_8(0x72, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E),
436 BYTES_TO_T_UINT_8(0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9),
438 static const mbedtls_mpi_uint brainpoolP256r1_a
[] = {
439 BYTES_TO_T_UINT_8(0xD9, 0xB5, 0x30, 0xF3, 0x44, 0x4B, 0x4A, 0xE9),
440 BYTES_TO_T_UINT_8(0x6C, 0x5C, 0xDC, 0x26, 0xC1, 0x55, 0x80, 0xFB),
441 BYTES_TO_T_UINT_8(0xE7, 0xFF, 0x7A, 0x41, 0x30, 0x75, 0xF6, 0xEE),
442 BYTES_TO_T_UINT_8(0x57, 0x30, 0x2C, 0xFC, 0x75, 0x09, 0x5A, 0x7D),
444 static const mbedtls_mpi_uint brainpoolP256r1_b
[] = {
445 BYTES_TO_T_UINT_8(0xB6, 0x07, 0x8C, 0xFF, 0x18, 0xDC, 0xCC, 0x6B),
446 BYTES_TO_T_UINT_8(0xCE, 0xE1, 0xF7, 0x5C, 0x29, 0x16, 0x84, 0x95),
447 BYTES_TO_T_UINT_8(0xBF, 0x7C, 0xD7, 0xBB, 0xD9, 0xB5, 0x30, 0xF3),
448 BYTES_TO_T_UINT_8(0x44, 0x4B, 0x4A, 0xE9, 0x6C, 0x5C, 0xDC, 0x26),
450 static const mbedtls_mpi_uint brainpoolP256r1_gx
[] = {
451 BYTES_TO_T_UINT_8(0x62, 0x32, 0xCE, 0x9A, 0xBD, 0x53, 0x44, 0x3A),
452 BYTES_TO_T_UINT_8(0xC2, 0x23, 0xBD, 0xE3, 0xE1, 0x27, 0xDE, 0xB9),
453 BYTES_TO_T_UINT_8(0xAF, 0xB7, 0x81, 0xFC, 0x2F, 0x48, 0x4B, 0x2C),
454 BYTES_TO_T_UINT_8(0xCB, 0x57, 0x7E, 0xCB, 0xB9, 0xAE, 0xD2, 0x8B),
456 static const mbedtls_mpi_uint brainpoolP256r1_gy
[] = {
457 BYTES_TO_T_UINT_8(0x97, 0x69, 0x04, 0x2F, 0xC7, 0x54, 0x1D, 0x5C),
458 BYTES_TO_T_UINT_8(0x54, 0x8E, 0xED, 0x2D, 0x13, 0x45, 0x77, 0xC2),
459 BYTES_TO_T_UINT_8(0xC9, 0x1D, 0x61, 0x14, 0x1A, 0x46, 0xF8, 0x97),
460 BYTES_TO_T_UINT_8(0xFD, 0xC4, 0xDA, 0xC3, 0x35, 0xF8, 0x7E, 0x54),
462 static const mbedtls_mpi_uint brainpoolP256r1_n
[] = {
463 BYTES_TO_T_UINT_8(0xA7, 0x56, 0x48, 0x97, 0x82, 0x0E, 0x1E, 0x90),
464 BYTES_TO_T_UINT_8(0xF7, 0xA6, 0x61, 0xB5, 0xA3, 0x7A, 0x39, 0x8C),
465 BYTES_TO_T_UINT_8(0x71, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E),
466 BYTES_TO_T_UINT_8(0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9),
468 #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
471 * Domain parameters for brainpoolP384r1 (RFC 5639 3.6)
473 #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
474 static const mbedtls_mpi_uint brainpoolP384r1_p
[] = {
475 BYTES_TO_T_UINT_8(0x53, 0xEC, 0x07, 0x31, 0x13, 0x00, 0x47, 0x87),
476 BYTES_TO_T_UINT_8(0x71, 0x1A, 0x1D, 0x90, 0x29, 0xA7, 0xD3, 0xAC),
477 BYTES_TO_T_UINT_8(0x23, 0x11, 0xB7, 0x7F, 0x19, 0xDA, 0xB1, 0x12),
478 BYTES_TO_T_UINT_8(0xB4, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15),
479 BYTES_TO_T_UINT_8(0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F),
480 BYTES_TO_T_UINT_8(0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C),
482 static const mbedtls_mpi_uint brainpoolP384r1_a
[] = {
483 BYTES_TO_T_UINT_8(0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04),
484 BYTES_TO_T_UINT_8(0xEB, 0xD4, 0x3A, 0x50, 0x4A, 0x81, 0xA5, 0x8A),
485 BYTES_TO_T_UINT_8(0x0F, 0xF9, 0x91, 0xBA, 0xEF, 0x65, 0x91, 0x13),
486 BYTES_TO_T_UINT_8(0x87, 0x27, 0xB2, 0x4F, 0x8E, 0xA2, 0xBE, 0xC2),
487 BYTES_TO_T_UINT_8(0xA0, 0xAF, 0x05, 0xCE, 0x0A, 0x08, 0x72, 0x3C),
488 BYTES_TO_T_UINT_8(0x0C, 0x15, 0x8C, 0x3D, 0xC6, 0x82, 0xC3, 0x7B),
490 static const mbedtls_mpi_uint brainpoolP384r1_b
[] = {
491 BYTES_TO_T_UINT_8(0x11, 0x4C, 0x50, 0xFA, 0x96, 0x86, 0xB7, 0x3A),
492 BYTES_TO_T_UINT_8(0x94, 0xC9, 0xDB, 0x95, 0x02, 0x39, 0xB4, 0x7C),
493 BYTES_TO_T_UINT_8(0xD5, 0x62, 0xEB, 0x3E, 0xA5, 0x0E, 0x88, 0x2E),
494 BYTES_TO_T_UINT_8(0xA6, 0xD2, 0xDC, 0x07, 0xE1, 0x7D, 0xB7, 0x2F),
495 BYTES_TO_T_UINT_8(0x7C, 0x44, 0xF0, 0x16, 0x54, 0xB5, 0x39, 0x8B),
496 BYTES_TO_T_UINT_8(0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04),
498 static const mbedtls_mpi_uint brainpoolP384r1_gx
[] = {
499 BYTES_TO_T_UINT_8(0x1E, 0xAF, 0xD4, 0x47, 0xE2, 0xB2, 0x87, 0xEF),
500 BYTES_TO_T_UINT_8(0xAA, 0x46, 0xD6, 0x36, 0x34, 0xE0, 0x26, 0xE8),
501 BYTES_TO_T_UINT_8(0xE8, 0x10, 0xBD, 0x0C, 0xFE, 0xCA, 0x7F, 0xDB),
502 BYTES_TO_T_UINT_8(0xE3, 0x4F, 0xF1, 0x7E, 0xE7, 0xA3, 0x47, 0x88),
503 BYTES_TO_T_UINT_8(0x6B, 0x3F, 0xC1, 0xB7, 0x81, 0x3A, 0xA6, 0xA2),
504 BYTES_TO_T_UINT_8(0xFF, 0x45, 0xCF, 0x68, 0xF0, 0x64, 0x1C, 0x1D),
506 static const mbedtls_mpi_uint brainpoolP384r1_gy
[] = {
507 BYTES_TO_T_UINT_8(0x15, 0x53, 0x3C, 0x26, 0x41, 0x03, 0x82, 0x42),
508 BYTES_TO_T_UINT_8(0x11, 0x81, 0x91, 0x77, 0x21, 0x46, 0x46, 0x0E),
509 BYTES_TO_T_UINT_8(0x28, 0x29, 0x91, 0xF9, 0x4F, 0x05, 0x9C, 0xE1),
510 BYTES_TO_T_UINT_8(0x64, 0x58, 0xEC, 0xFE, 0x29, 0x0B, 0xB7, 0x62),
511 BYTES_TO_T_UINT_8(0x52, 0xD5, 0xCF, 0x95, 0x8E, 0xEB, 0xB1, 0x5C),
512 BYTES_TO_T_UINT_8(0xA4, 0xC2, 0xF9, 0x20, 0x75, 0x1D, 0xBE, 0x8A),
514 static const mbedtls_mpi_uint brainpoolP384r1_n
[] = {
515 BYTES_TO_T_UINT_8(0x65, 0x65, 0x04, 0xE9, 0x02, 0x32, 0x88, 0x3B),
516 BYTES_TO_T_UINT_8(0x10, 0xC3, 0x7F, 0x6B, 0xAF, 0xB6, 0x3A, 0xCF),
517 BYTES_TO_T_UINT_8(0xA7, 0x25, 0x04, 0xAC, 0x6C, 0x6E, 0x16, 0x1F),
518 BYTES_TO_T_UINT_8(0xB3, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15),
519 BYTES_TO_T_UINT_8(0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F),
520 BYTES_TO_T_UINT_8(0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C),
522 #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
525 * Domain parameters for brainpoolP512r1 (RFC 5639 3.7)
527 #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
528 static const mbedtls_mpi_uint brainpoolP512r1_p
[] = {
529 BYTES_TO_T_UINT_8(0xF3, 0x48, 0x3A, 0x58, 0x56, 0x60, 0xAA, 0x28),
530 BYTES_TO_T_UINT_8(0x85, 0xC6, 0x82, 0x2D, 0x2F, 0xFF, 0x81, 0x28),
531 BYTES_TO_T_UINT_8(0xE6, 0x80, 0xA3, 0xE6, 0x2A, 0xA1, 0xCD, 0xAE),
532 BYTES_TO_T_UINT_8(0x42, 0x68, 0xC6, 0x9B, 0x00, 0x9B, 0x4D, 0x7D),
533 BYTES_TO_T_UINT_8(0x71, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6),
534 BYTES_TO_T_UINT_8(0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB),
535 BYTES_TO_T_UINT_8(0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F),
536 BYTES_TO_T_UINT_8(0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA),
538 static const mbedtls_mpi_uint brainpoolP512r1_a
[] = {
539 BYTES_TO_T_UINT_8(0xCA, 0x94, 0xFC, 0x77, 0x4D, 0xAC, 0xC1, 0xE7),
540 BYTES_TO_T_UINT_8(0xB9, 0xC7, 0xF2, 0x2B, 0xA7, 0x17, 0x11, 0x7F),
541 BYTES_TO_T_UINT_8(0xB5, 0xC8, 0x9A, 0x8B, 0xC9, 0xF1, 0x2E, 0x0A),
542 BYTES_TO_T_UINT_8(0xA1, 0x3A, 0x25, 0xA8, 0x5A, 0x5D, 0xED, 0x2D),
543 BYTES_TO_T_UINT_8(0xBC, 0x63, 0x98, 0xEA, 0xCA, 0x41, 0x34, 0xA8),
544 BYTES_TO_T_UINT_8(0x10, 0x16, 0xF9, 0x3D, 0x8D, 0xDD, 0xCB, 0x94),
545 BYTES_TO_T_UINT_8(0xC5, 0x4C, 0x23, 0xAC, 0x45, 0x71, 0x32, 0xE2),
546 BYTES_TO_T_UINT_8(0x89, 0x3B, 0x60, 0x8B, 0x31, 0xA3, 0x30, 0x78),
548 static const mbedtls_mpi_uint brainpoolP512r1_b
[] = {
549 BYTES_TO_T_UINT_8(0x23, 0xF7, 0x16, 0x80, 0x63, 0xBD, 0x09, 0x28),
550 BYTES_TO_T_UINT_8(0xDD, 0xE5, 0xBA, 0x5E, 0xB7, 0x50, 0x40, 0x98),
551 BYTES_TO_T_UINT_8(0x67, 0x3E, 0x08, 0xDC, 0xCA, 0x94, 0xFC, 0x77),
552 BYTES_TO_T_UINT_8(0x4D, 0xAC, 0xC1, 0xE7, 0xB9, 0xC7, 0xF2, 0x2B),
553 BYTES_TO_T_UINT_8(0xA7, 0x17, 0x11, 0x7F, 0xB5, 0xC8, 0x9A, 0x8B),
554 BYTES_TO_T_UINT_8(0xC9, 0xF1, 0x2E, 0x0A, 0xA1, 0x3A, 0x25, 0xA8),
555 BYTES_TO_T_UINT_8(0x5A, 0x5D, 0xED, 0x2D, 0xBC, 0x63, 0x98, 0xEA),
556 BYTES_TO_T_UINT_8(0xCA, 0x41, 0x34, 0xA8, 0x10, 0x16, 0xF9, 0x3D),
558 static const mbedtls_mpi_uint brainpoolP512r1_gx
[] = {
559 BYTES_TO_T_UINT_8(0x22, 0xF8, 0xB9, 0xBC, 0x09, 0x22, 0x35, 0x8B),
560 BYTES_TO_T_UINT_8(0x68, 0x5E, 0x6A, 0x40, 0x47, 0x50, 0x6D, 0x7C),
561 BYTES_TO_T_UINT_8(0x5F, 0x7D, 0xB9, 0x93, 0x7B, 0x68, 0xD1, 0x50),
562 BYTES_TO_T_UINT_8(0x8D, 0xD4, 0xD0, 0xE2, 0x78, 0x1F, 0x3B, 0xFF),
563 BYTES_TO_T_UINT_8(0x8E, 0x09, 0xD0, 0xF4, 0xEE, 0x62, 0x3B, 0xB4),
564 BYTES_TO_T_UINT_8(0xC1, 0x16, 0xD9, 0xB5, 0x70, 0x9F, 0xED, 0x85),
565 BYTES_TO_T_UINT_8(0x93, 0x6A, 0x4C, 0x9C, 0x2E, 0x32, 0x21, 0x5A),
566 BYTES_TO_T_UINT_8(0x64, 0xD9, 0x2E, 0xD8, 0xBD, 0xE4, 0xAE, 0x81),
568 static const mbedtls_mpi_uint brainpoolP512r1_gy
[] = {
569 BYTES_TO_T_UINT_8(0x92, 0x08, 0xD8, 0x3A, 0x0F, 0x1E, 0xCD, 0x78),
570 BYTES_TO_T_UINT_8(0x06, 0x54, 0xF0, 0xA8, 0x2F, 0x2B, 0xCA, 0xD1),
571 BYTES_TO_T_UINT_8(0xAE, 0x63, 0x27, 0x8A, 0xD8, 0x4B, 0xCA, 0x5B),
572 BYTES_TO_T_UINT_8(0x5E, 0x48, 0x5F, 0x4A, 0x49, 0xDE, 0xDC, 0xB2),
573 BYTES_TO_T_UINT_8(0x11, 0x81, 0x1F, 0x88, 0x5B, 0xC5, 0x00, 0xA0),
574 BYTES_TO_T_UINT_8(0x1A, 0x7B, 0xA5, 0x24, 0x00, 0xF7, 0x09, 0xF2),
575 BYTES_TO_T_UINT_8(0xFD, 0x22, 0x78, 0xCF, 0xA9, 0xBF, 0xEA, 0xC0),
576 BYTES_TO_T_UINT_8(0xEC, 0x32, 0x63, 0x56, 0x5D, 0x38, 0xDE, 0x7D),
578 static const mbedtls_mpi_uint brainpoolP512r1_n
[] = {
579 BYTES_TO_T_UINT_8(0x69, 0x00, 0xA9, 0x9C, 0x82, 0x96, 0x87, 0xB5),
580 BYTES_TO_T_UINT_8(0xDD, 0xDA, 0x5D, 0x08, 0x81, 0xD3, 0xB1, 0x1D),
581 BYTES_TO_T_UINT_8(0x47, 0x10, 0xAC, 0x7F, 0x19, 0x61, 0x86, 0x41),
582 BYTES_TO_T_UINT_8(0x19, 0x26, 0xA9, 0x4C, 0x41, 0x5C, 0x3E, 0x55),
583 BYTES_TO_T_UINT_8(0x70, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6),
584 BYTES_TO_T_UINT_8(0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB),
585 BYTES_TO_T_UINT_8(0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F),
586 BYTES_TO_T_UINT_8(0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA),
588 #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
590 #if defined(MBEDTLS_ECP_DP_SECP128R1_ENABLED) || \
591 defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
592 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
593 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
594 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
595 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
596 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
597 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
598 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
599 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
600 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
601 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
602 /* For these curves, we build the group parameters dynamically. */
603 #define ECP_LOAD_GROUP
606 #if defined(ECP_LOAD_GROUP)
608 * Create an MPI from embedded constants
609 * (assumes len is an exact multiple of sizeof mbedtls_mpi_uint)
611 static inline void ecp_mpi_load(mbedtls_mpi
*X
, const mbedtls_mpi_uint
*p
, size_t len
) {
613 X
->n
= len
/ sizeof(mbedtls_mpi_uint
);
614 X
->p
= (mbedtls_mpi_uint
*) p
;
618 * Set an MPI to static value 1
620 static inline void ecp_mpi_set1(mbedtls_mpi
*X
) {
621 static mbedtls_mpi_uint one
[] = { 1 };
628 * Make group available from embedded constants
630 static int ecp_group_load(mbedtls_ecp_group
*grp
,
631 const mbedtls_mpi_uint
*p
, size_t plen
,
632 const mbedtls_mpi_uint
*a
, size_t alen
,
633 const mbedtls_mpi_uint
*b
, size_t blen
,
634 const mbedtls_mpi_uint
*gx
, size_t gxlen
,
635 const mbedtls_mpi_uint
*gy
, size_t gylen
,
636 const mbedtls_mpi_uint
*n
, size_t nlen
) {
637 ecp_mpi_load(&grp
->P
, p
, plen
);
639 ecp_mpi_load(&grp
->A
, a
, alen
);
640 ecp_mpi_load(&grp
->B
, b
, blen
);
641 ecp_mpi_load(&grp
->N
, n
, nlen
);
643 ecp_mpi_load(&grp
->G
.X
, gx
, gxlen
);
644 ecp_mpi_load(&grp
->G
.Y
, gy
, gylen
);
645 ecp_mpi_set1(&grp
->G
.Z
);
647 grp
->pbits
= mbedtls_mpi_bitlen(&grp
->P
);
648 grp
->nbits
= mbedtls_mpi_bitlen(&grp
->N
);
654 #endif /* ECP_LOAD_GROUP */
656 #if defined(MBEDTLS_ECP_NIST_OPTIM)
657 /* Forward declarations */
658 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
659 static int ecp_mod_p192(mbedtls_mpi
*);
661 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
662 static int ecp_mod_p224(mbedtls_mpi
*);
664 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
665 static int ecp_mod_p256(mbedtls_mpi
*);
667 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
668 static int ecp_mod_p384(mbedtls_mpi
*);
670 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
671 static int ecp_mod_p521(mbedtls_mpi
*);
674 #define NIST_MODP( P ) grp->modp = ecp_mod_ ## P;
676 #define NIST_MODP( P )
677 #endif /* MBEDTLS_ECP_NIST_OPTIM */
679 /* Additional forward declarations */
680 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
681 static int ecp_mod_p255(mbedtls_mpi
*);
683 #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
684 static int ecp_mod_p448(mbedtls_mpi
*);
686 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
687 static int ecp_mod_p192k1(mbedtls_mpi
*);
689 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
690 static int ecp_mod_p224k1(mbedtls_mpi
*);
692 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
693 static int ecp_mod_p256k1(mbedtls_mpi
*);
696 #if defined(ECP_LOAD_GROUP)
697 #define LOAD_GROUP_A( G ) ecp_group_load( grp, \
698 G ## _p, sizeof( G ## _p ), \
699 G ## _a, sizeof( G ## _a ), \
700 G ## _b, sizeof( G ## _b ), \
701 G ## _gx, sizeof( G ## _gx ), \
702 G ## _gy, sizeof( G ## _gy ), \
703 G ## _n, sizeof( G ## _n ) )
705 #define LOAD_GROUP( G ) ecp_group_load( grp, \
706 G ## _p, sizeof( G ## _p ), \
708 G ## _b, sizeof( G ## _b ), \
709 G ## _gx, sizeof( G ## _gx ), \
710 G ## _gy, sizeof( G ## _gy ), \
711 G ## _n, sizeof( G ## _n ) )
712 #endif /* ECP_LOAD_GROUP */
714 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
716 * Specialized function for creating the Curve25519 group
718 static int ecp_use_curve25519(mbedtls_ecp_group
*grp
) {
719 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
721 /* Actually ( A + 2 ) / 4 */
722 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&grp
->A
, 16, "01DB42"));
725 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp
->P
, 1));
726 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&grp
->P
, 255));
727 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&grp
->P
, &grp
->P
, 19));
728 grp
->pbits
= mbedtls_mpi_bitlen(&grp
->P
);
730 /* N = 2^252 + 27742317777372353535851937790883648493 */
731 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&grp
->N
, 16,
732 "14DEF9DEA2F79CD65812631A5CF5D3ED"));
733 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&grp
->N
, 252, 1));
735 /* Y intentionally not set, since we use x/z coordinates.
736 * This is used as a marker to identify Montgomery curves! */
737 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp
->G
.X
, 9));
738 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp
->G
.Z
, 1));
739 mbedtls_mpi_free(&grp
->G
.Y
);
741 /* Actually, the required msb for private keys */
746 mbedtls_ecp_group_free(grp
);
750 #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
752 #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
754 * Specialized function for creating the Curve448 group
756 static int ecp_use_curve448(mbedtls_ecp_group
*grp
) {
758 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
760 mbedtls_mpi_init(&Ns
);
762 /* Actually ( A + 2 ) / 4 */
763 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&grp
->A
, 16, "98AA"));
765 /* P = 2^448 - 2^224 - 1 */
766 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp
->P
, 1));
767 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&grp
->P
, 224));
768 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&grp
->P
, &grp
->P
, 1));
769 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&grp
->P
, 224));
770 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&grp
->P
, &grp
->P
, 1));
771 grp
->pbits
= mbedtls_mpi_bitlen(&grp
->P
);
773 /* Y intentionally not set, since we use x/z coordinates.
774 * This is used as a marker to identify Montgomery curves! */
775 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp
->G
.X
, 5));
776 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp
->G
.Z
, 1));
777 mbedtls_mpi_free(&grp
->G
.Y
);
779 /* N = 2^446 - 13818066809895115352007386748515426880336692474882178609894547503885 */
780 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&grp
->N
, 446, 1));
781 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&Ns
, 16,
782 "8335DC163BB124B65129C96FDE933D8D723A70AADC873D6D54A7BB0D"));
783 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&grp
->N
, &grp
->N
, &Ns
));
785 /* Actually, the required msb for private keys */
789 mbedtls_mpi_free(&Ns
);
791 mbedtls_ecp_group_free(grp
);
795 #endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
798 * Set a group using well-known domain parameters
800 int mbedtls_ecp_group_load(mbedtls_ecp_group
*grp
, mbedtls_ecp_group_id id
) {
801 ECP_VALIDATE_RET(grp
!= NULL
);
802 mbedtls_ecp_group_free(grp
);
807 #if defined(MBEDTLS_ECP_DP_SECP128R1_ENABLED)
808 case MBEDTLS_ECP_DP_SECP128R1
:
810 return (LOAD_GROUP_A(secp128r1
));
811 #endif /* MBEDTLS_ECP_DP_SECP128R1_ENABLED */
813 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
814 case MBEDTLS_ECP_DP_SECP192R1
:
816 return (LOAD_GROUP(secp192r1
));
817 #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
819 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
820 case MBEDTLS_ECP_DP_SECP224R1
:
822 return (LOAD_GROUP(secp224r1
));
823 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
825 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
826 case MBEDTLS_ECP_DP_SECP256R1
:
828 return (LOAD_GROUP(secp256r1
));
829 #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
831 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
832 case MBEDTLS_ECP_DP_SECP384R1
:
834 return (LOAD_GROUP(secp384r1
));
835 #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
837 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
838 case MBEDTLS_ECP_DP_SECP521R1
:
840 return (LOAD_GROUP(secp521r1
));
841 #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
843 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
844 case MBEDTLS_ECP_DP_SECP192K1
:
845 grp
->modp
= ecp_mod_p192k1
;
846 return (LOAD_GROUP_A(secp192k1
));
847 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
849 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
850 case MBEDTLS_ECP_DP_SECP224K1
:
851 grp
->modp
= ecp_mod_p224k1
;
852 return (LOAD_GROUP_A(secp224k1
));
853 #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
855 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
856 case MBEDTLS_ECP_DP_SECP256K1
:
857 grp
->modp
= ecp_mod_p256k1
;
858 return (LOAD_GROUP_A(secp256k1
));
859 #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
861 #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
862 case MBEDTLS_ECP_DP_BP256R1
:
863 return (LOAD_GROUP_A(brainpoolP256r1
));
864 #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
866 #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
867 case MBEDTLS_ECP_DP_BP384R1
:
868 return (LOAD_GROUP_A(brainpoolP384r1
));
869 #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
871 #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
872 case MBEDTLS_ECP_DP_BP512R1
:
873 return (LOAD_GROUP_A(brainpoolP512r1
));
874 #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
876 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
877 case MBEDTLS_ECP_DP_CURVE25519
:
878 grp
->modp
= ecp_mod_p255
;
879 return (ecp_use_curve25519(grp
));
880 #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
882 #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
883 case MBEDTLS_ECP_DP_CURVE448
:
884 grp
->modp
= ecp_mod_p448
;
885 return (ecp_use_curve448(grp
));
886 #endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
889 grp
->id
= MBEDTLS_ECP_DP_NONE
;
890 return (MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE
);
894 #if defined(MBEDTLS_ECP_NIST_OPTIM)
896 * Fast reduction modulo the primes used by the NIST curves.
898 * These functions are critical for speed, but not needed for correct
899 * operations. So, we make the choice to heavily rely on the internals of our
900 * bignum library, which creates a tight coupling between these functions and
901 * our MPI implementation. However, the coupling between the ECP module and
902 * MPI remains loose, since these functions can be deactivated at will.
905 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
907 * Compared to the way things are presented in FIPS 186-3 D.2,
908 * we proceed in columns, from right (least significant chunk) to left,
909 * adding chunks to N in place, and keeping a carry for the next chunk.
910 * This avoids moving things around in memory, and uselessly adding zeros,
911 * compared to the more straightforward, line-oriented approach.
913 * For this prime we need to handle data in chunks of 64 bits.
914 * Since this is always a multiple of our basic mbedtls_mpi_uint, we can
915 * use a mbedtls_mpi_uint * to designate such a chunk, and small loops to handle it.
918 /* Add 64-bit chunks (dst += src) and update carry */
919 static inline void add64(mbedtls_mpi_uint
*dst
, mbedtls_mpi_uint
*src
, mbedtls_mpi_uint
*carry
) {
921 mbedtls_mpi_uint c
= 0;
922 for (i
= 0; i
< 8 / sizeof(mbedtls_mpi_uint
); i
++, dst
++, src
++) {
931 /* Add carry to a 64-bit chunk and update carry */
932 static inline void carry64(mbedtls_mpi_uint
*dst
, mbedtls_mpi_uint
*carry
) {
934 for (i
= 0; i
< 8 / sizeof(mbedtls_mpi_uint
); i
++, dst
++) {
936 *carry
= (*dst
< *carry
);
940 #define WIDTH 8 / sizeof( mbedtls_mpi_uint )
941 #define A( i ) N->p + (i) * WIDTH
942 #define ADD( i ) add64( p, A( i ), &c )
943 #define NEXT p += WIDTH; carry64( p, &c )
944 #define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0
947 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
949 static int ecp_mod_p192(mbedtls_mpi
*N
) {
950 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
951 mbedtls_mpi_uint c
= 0;
952 mbedtls_mpi_uint
*p
, *end
;
954 /* Make sure we have enough blocks so that A(5) is legal */
955 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N
, 6 * WIDTH
));
962 NEXT
; // A0 += A3 + A5
966 NEXT
; // A1 += A3 + A4 + A5
969 LAST
; // A2 += A4 + A5
980 #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
982 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
983 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
984 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
986 * The reader is advised to first understand ecp_mod_p192() since the same
987 * general structure is used here, but with additional complications:
988 * (1) chunks of 32 bits, and (2) subtractions.
992 * For these primes, we need to handle data in chunks of 32 bits.
993 * This makes it more complicated if we use 64 bits limbs in MPI,
994 * which prevents us from using a uniform access method as for p192.
996 * So, we define a mini abstraction layer to access 32 bit chunks,
997 * load them in 'cur' for work, and store them back from 'cur' when done.
999 * While at it, also define the size of N in terms of 32-bit chunks.
1001 #define LOAD32 cur = A( i );
1003 #if defined(MBEDTLS_HAVE_INT32) /* 32 bit */
1006 #define A( j ) N->p[j]
1007 #define STORE32 N->p[i] = cur;
1011 #define MAX32 N->n * 2
1012 #define A( j ) (j) % 2 ? (uint32_t)( N->p[(j)/2] >> 32 ) : \
1013 (uint32_t)( N->p[(j)/2] )
1016 N->p[i/2] &= 0x00000000FFFFFFFF; \
1017 N->p[i/2] |= ((mbedtls_mpi_uint) cur) << 32; \
1019 N->p[i/2] &= 0xFFFFFFFF00000000; \
1020 N->p[i/2] |= (mbedtls_mpi_uint) cur; \
1023 #endif /* sizeof( mbedtls_mpi_uint ) */
1026 * Helpers for addition and subtraction of chunks, with signed carry.
1028 static inline void add32(uint32_t *dst
, uint32_t src
, signed char *carry
) {
1030 *carry
+= (*dst
< src
);
1033 static inline void sub32(uint32_t *dst
, uint32_t src
, signed char *carry
) {
1034 *carry
-= (*dst
< src
);
1038 #define ADD( j ) add32( &cur, A( j ), &c );
1039 #define SUB( j ) sub32( &cur, A( j ), &c );
1042 * Helpers for the main 'loop'
1043 * (see fix_negative for the motivation of C)
1046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; \
1047 signed char c = 0, cc; \
1049 size_t i = 0, bits = (b); \
1051 mbedtls_mpi_uint Cp[ (b) / 8 / sizeof( mbedtls_mpi_uint) + 1 ]; \
1054 C.n = (b) / 8 / sizeof( mbedtls_mpi_uint) + 1; \
1056 memset( Cp, 0, C.n * sizeof( mbedtls_mpi_uint ) ); \
1058 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( N, (b) * 2 / 8 / \
1059 sizeof( mbedtls_mpi_uint ) ) ); \
1063 STORE32; i++; LOAD32; \
1066 sub32( &cur, -cc, &c ); \
1068 add32( &cur, cc, &c ); \
1072 cur = c > 0 ? c : 0; STORE32; \
1073 cur = 0; while( ++i < MAX32 ) { STORE32; } \
1074 if( c < 0 ) MBEDTLS_MPI_CHK( fix_negative( N, c, &C, bits ) );
1077 * If the result is negative, we get it in the form
1078 * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
1080 static inline int fix_negative(mbedtls_mpi
*N
, signed char c
, mbedtls_mpi
*C
, size_t bits
) {
1081 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
1083 /* C = - c * 2^(bits + 32) */
1084 #if !defined(MBEDTLS_HAVE_INT64)
1088 C
->p
[ C
->n
- 1 ] = ((mbedtls_mpi_uint
) - c
) << 32;
1091 C
->p
[ C
->n
- 1 ] = (mbedtls_mpi_uint
) - c
;
1093 /* N = - ( C - N ) */
1094 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(N
, C
, N
));
1102 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
1104 * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
1106 static int ecp_mod_p224(mbedtls_mpi
*N
) {
1111 NEXT
; // A0 += -A7 - A11
1114 NEXT
; // A1 += -A8 - A12
1117 NEXT
; // A2 += -A9 - A13
1121 NEXT
; // A3 += -A10 + A7 + A11
1125 NEXT
; // A4 += -A11 + A8 + A12
1129 NEXT
; // A5 += -A12 + A9 + A13
1132 LAST
; // A6 += -A13 + A10
1137 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
1139 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
1141 * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
1143 static int ecp_mod_p256(mbedtls_mpi
*N
) {
1220 #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
1222 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
1224 * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
1226 static int ecp_mod_p384(mbedtls_mpi
*N
) {
1322 #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
1332 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED ||
1333 MBEDTLS_ECP_DP_SECP256R1_ENABLED ||
1334 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
1336 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
1338 * Here we have an actual Mersenne prime, so things are more straightforward.
1339 * However, chunks are aligned on a 'weird' boundary (521 bits).
1342 /* Size of p521 in terms of mbedtls_mpi_uint */
1343 #define P521_WIDTH ( 521 / 8 / sizeof( mbedtls_mpi_uint ) + 1 )
1345 /* Bits to keep in the most significant mbedtls_mpi_uint */
1346 #define P521_MASK 0x01FF
1349 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
1350 * Write N as A1 + 2^521 A0, return A0 + A1
1352 static int ecp_mod_p521(mbedtls_mpi
*N
) {
1353 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
1356 mbedtls_mpi_uint Mp
[P521_WIDTH
+ 1];
1357 /* Worst case for the size of M is when mbedtls_mpi_uint is 16 bits:
1358 * we need to hold bits 513 to 1056, which is 34 limbs, that is
1359 * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
1361 if (N
->n
< P521_WIDTH
)
1366 M
.n
= N
->n
- (P521_WIDTH
- 1);
1367 if (M
.n
> P521_WIDTH
+ 1)
1368 M
.n
= P521_WIDTH
+ 1;
1370 memcpy(Mp
, N
->p
+ P521_WIDTH
- 1, M
.n
* sizeof(mbedtls_mpi_uint
));
1371 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&M
, 521 % (8 * sizeof(mbedtls_mpi_uint
))));
1374 N
->p
[P521_WIDTH
- 1] &= P521_MASK
;
1375 for (i
= P521_WIDTH
; i
< N
->n
; i
++)
1379 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(N
, N
, &M
));
1387 #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
1389 #endif /* MBEDTLS_ECP_NIST_OPTIM */
1391 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
1393 /* Size of p255 in terms of mbedtls_mpi_uint */
1394 #define P255_WIDTH ( 255 / 8 / sizeof( mbedtls_mpi_uint ) + 1 )
1397 * Fast quasi-reduction modulo p255 = 2^255 - 19
1398 * Write N as A0 + 2^255 A1, return A0 + 19 * A1
1400 static int ecp_mod_p255(mbedtls_mpi
*N
) {
1401 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
1404 mbedtls_mpi_uint Mp
[P255_WIDTH
+ 2];
1406 if (N
->n
< P255_WIDTH
)
1411 M
.n
= N
->n
- (P255_WIDTH
- 1);
1412 if (M
.n
> P255_WIDTH
+ 1)
1413 return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1415 memset(Mp
, 0, sizeof Mp
);
1416 memcpy(Mp
, N
->p
+ P255_WIDTH
- 1, M
.n
* sizeof(mbedtls_mpi_uint
));
1417 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&M
, 255 % (8 * sizeof(mbedtls_mpi_uint
))));
1418 M
.n
++; /* Make room for multiplication by 19 */
1421 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(N
, 255, 0));
1422 for (i
= P255_WIDTH
; i
< N
->n
; i
++)
1425 /* N = A0 + 19 * A1 */
1426 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&M
, &M
, 19));
1427 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(N
, N
, &M
));
1432 #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
1434 #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
1436 /* Size of p448 in terms of mbedtls_mpi_uint */
1437 #define P448_WIDTH ( 448 / 8 / sizeof( mbedtls_mpi_uint ) )
1439 /* Number of limbs fully occupied by 2^224 (max), and limbs used by it (min) */
1440 #define DIV_ROUND_UP( X, Y ) ( ( ( X ) + ( Y ) - 1 ) / ( Y ) )
1441 #define P224_WIDTH_MIN ( 28 / sizeof( mbedtls_mpi_uint ) )
1442 #define P224_WIDTH_MAX DIV_ROUND_UP( 28, sizeof( mbedtls_mpi_uint ) )
1443 #define P224_UNUSED_BITS ( ( P224_WIDTH_MAX * sizeof( mbedtls_mpi_uint ) * 8 ) - 224 )
1446 * Fast quasi-reduction modulo p448 = 2^448 - 2^224 - 1
1447 * Write N as A0 + 2^448 A1 and A1 as B0 + 2^224 B1, and return
1448 * A0 + A1 + B1 + (B0 + B1) * 2^224. This is different to the reference
1449 * implementation of Curve448, which uses its own special 56-bit limbs rather
1450 * than a generic bignum library. We could squeeze some extra speed out on
1451 * 32-bit machines by splitting N up into 32-bit limbs and doing the
1452 * arithmetic using the limbs directly as we do for the NIST primes above,
1453 * but for 64-bit targets it should use half the number of operations if we do
1454 * the reduction with 224-bit limbs, since mpi_add_mpi will then use 64-bit adds.
1456 static int ecp_mod_p448(mbedtls_mpi
*N
) {
1457 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
1460 mbedtls_mpi_uint Mp
[P448_WIDTH
+ 1], Qp
[P448_WIDTH
];
1462 if (N
->n
<= P448_WIDTH
)
1467 M
.n
= N
->n
- (P448_WIDTH
);
1468 if (M
.n
> P448_WIDTH
)
1469 /* Shouldn't be called with N larger than 2^896! */
1470 return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1472 memset(Mp
, 0, sizeof(Mp
));
1473 memcpy(Mp
, N
->p
+ P448_WIDTH
, M
.n
* sizeof(mbedtls_mpi_uint
));
1476 for (i
= P448_WIDTH
; i
< N
->n
; i
++)
1480 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N
, N
, &M
));
1482 /* Q = B1, N += B1 */
1485 memcpy(Qp
, Mp
, sizeof(Qp
));
1486 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Q
, 224));
1487 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N
, N
, &Q
));
1489 /* M = (B0 + B1) * 2^224, N += M */
1490 if (sizeof(mbedtls_mpi_uint
) > 4)
1491 Mp
[P224_WIDTH_MIN
] &= ((mbedtls_mpi_uint
) - 1) >> (P224_UNUSED_BITS
);
1492 for (i
= P224_WIDTH_MAX
; i
< M
.n
; ++i
)
1494 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&M
, &M
, &Q
));
1495 M
.n
= P448_WIDTH
+ 1; /* Make room for shifted carry bit from the addition */
1496 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&M
, 224));
1497 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N
, N
, &M
));
1502 #endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
1504 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
1505 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
1506 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
1508 * Fast quasi-reduction modulo P = 2^s - R,
1509 * with R about 33 bits, used by the Koblitz curves.
1511 * Write N as A0 + 2^224 A1, return A0 + R * A1.
1512 * Actually do two passes, since R is big.
1514 #define P_KOBLITZ_MAX ( 256 / 8 / sizeof( mbedtls_mpi_uint ) ) // Max limbs in P
1515 #define P_KOBLITZ_R ( 8 / sizeof( mbedtls_mpi_uint ) ) // Limbs in R
1516 static inline int ecp_mod_koblitz(mbedtls_mpi
*N
, mbedtls_mpi_uint
*Rp
, size_t p_limbs
,
1517 size_t adjust
, size_t shift
, mbedtls_mpi_uint mask
) {
1518 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
1521 mbedtls_mpi_uint Mp
[P_KOBLITZ_MAX
+ P_KOBLITZ_R
+ 1];
1531 /* Common setup for M */
1536 M
.n
= N
->n
- (p_limbs
- adjust
);
1537 if (M
.n
> p_limbs
+ adjust
)
1538 M
.n
= p_limbs
+ adjust
;
1539 memset(Mp
, 0, sizeof Mp
);
1540 memcpy(Mp
, N
->p
+ p_limbs
- adjust
, M
.n
* sizeof(mbedtls_mpi_uint
));
1542 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&M
, shift
));
1543 M
.n
+= R
.n
; /* Make room for multiplication by R */
1547 N
->p
[p_limbs
- 1] &= mask
;
1548 for (i
= p_limbs
; i
< N
->n
; i
++)
1551 /* N = A0 + R * A1 */
1552 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&M
, &M
, &R
));
1553 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(N
, N
, &M
));
1558 M
.n
= N
->n
- (p_limbs
- adjust
);
1559 if (M
.n
> p_limbs
+ adjust
)
1560 M
.n
= p_limbs
+ adjust
;
1561 memset(Mp
, 0, sizeof Mp
);
1562 memcpy(Mp
, N
->p
+ p_limbs
- adjust
, M
.n
* sizeof(mbedtls_mpi_uint
));
1564 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&M
, shift
));
1565 M
.n
+= R
.n
; /* Make room for multiplication by R */
1569 N
->p
[p_limbs
- 1] &= mask
;
1570 for (i
= p_limbs
; i
< N
->n
; i
++)
1573 /* N = A0 + R * A1 */
1574 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&M
, &M
, &R
));
1575 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(N
, N
, &M
));
1580 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED) ||
1581 MBEDTLS_ECP_DP_SECP224K1_ENABLED) ||
1582 MBEDTLS_ECP_DP_SECP256K1_ENABLED) */
1584 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
1586 * Fast quasi-reduction modulo p192k1 = 2^192 - R,
1587 * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119
1589 static int ecp_mod_p192k1(mbedtls_mpi
*N
) {
1590 static mbedtls_mpi_uint Rp
[] = {
1591 BYTES_TO_T_UINT_8(0xC9, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)
1594 return (ecp_mod_koblitz(N
, Rp
, 192 / 8 / sizeof(mbedtls_mpi_uint
), 0, 0, 0));
1596 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
1598 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
1600 * Fast quasi-reduction modulo p224k1 = 2^224 - R,
1601 * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
1603 static int ecp_mod_p224k1(mbedtls_mpi
*N
) {
1604 static mbedtls_mpi_uint Rp
[] = {
1605 BYTES_TO_T_UINT_8(0x93, 0x1A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)
1608 #if defined(MBEDTLS_HAVE_INT64)
1609 return (ecp_mod_koblitz(N
, Rp
, 4, 1, 32, 0xFFFFFFFF));
1611 return (ecp_mod_koblitz(N
, Rp
, 224 / 8 / sizeof(mbedtls_mpi_uint
), 0, 0, 0));
1615 #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
1617 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
1619 * Fast quasi-reduction modulo p256k1 = 2^256 - R,
1620 * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
1622 static int ecp_mod_p256k1(mbedtls_mpi
*N
) {
1623 static mbedtls_mpi_uint Rp
[] = {
1624 BYTES_TO_T_UINT_8(0xD1, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)
1626 return (ecp_mod_koblitz(N
, Rp
, 256 / 8 / sizeof(mbedtls_mpi_uint
), 0, 0, 0));
1628 #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
1630 #endif /* !MBEDTLS_ECP_ALT */
1632 #endif /* MBEDTLS_ECP_C */