1 /* 2 * Elliptic curves over GF(p): curve-specific data and functions 3 * 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 6 * 7 * This file is provided under the Apache License 2.0, or the 8 * GNU General Public License v2.0 or later. 9 * 10 * ********** 11 * Apache License 2.0: 12 * 13 * Licensed under the Apache License, Version 2.0 (the "License"); you may 14 * not use this file except in compliance with the License. 15 * You may obtain a copy of the License at 16 * 17 * http://www.apache.org/licenses/LICENSE-2.0 18 * 19 * Unless required by applicable law or agreed to in writing, software 20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 * See the License for the specific language governing permissions and 23 * limitations under the License. 24 * 25 * ********** 26 * 27 * ********** 28 * GNU General Public License v2.0 or later: 29 * 30 * This program is free software; you can redistribute it and/or modify 31 * it under the terms of the GNU General Public License as published by 32 * the Free Software Foundation; either version 2 of the License, or 33 * (at your option) any later version. 34 * 35 * This program is distributed in the hope that it will be useful, 36 * but WITHOUT ANY WARRANTY; without even the implied warranty of 37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 38 * GNU General Public License for more details. 39 * 40 * You should have received a copy of the GNU General Public License along 41 * with this program; if not, write to the Free Software Foundation, Inc., 42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 43 * 44 * ********** 45 * 46 * This file is part of mbed TLS (https://tls.mbed.org) 47 */ 48 49 #if !defined(MBEDTLS_CONFIG_FILE) 50 #include "mbedtls/config.h" 51 #else 52 #include MBEDTLS_CONFIG_FILE 53 #endif 54 55 #if defined(MBEDTLS_ECP_C) 56 57 #include "mbedtls/ecp.h" 58 59 #include <string.h> 60 61 #if !defined(MBEDTLS_ECP_ALT) 62 63 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ 64 !defined(inline) && !defined(__cplusplus) 65 #define inline __inline 66 #endif 67 68 /* 69 * Conversion macros for embedded constants: 70 * build lists of mbedtls_mpi_uint's from lists of unsigned char's grouped by 8, 4 or 2 71 */ 72 #if defined(MBEDTLS_HAVE_INT32) 73 74 #define BYTES_TO_T_UINT_4( a, b, c, d ) \ 75 ( (mbedtls_mpi_uint) a << 0 ) | \ 76 ( (mbedtls_mpi_uint) b << 8 ) | \ 77 ( (mbedtls_mpi_uint) c << 16 ) | \ 78 ( (mbedtls_mpi_uint) d << 24 ) 79 80 #define BYTES_TO_T_UINT_2( a, b ) \ 81 BYTES_TO_T_UINT_4( a, b, 0, 0 ) 82 83 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \ 84 BYTES_TO_T_UINT_4( a, b, c, d ), \ 85 BYTES_TO_T_UINT_4( e, f, g, h ) 86 87 #else /* 64-bits */ 88 89 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \ 90 ( (mbedtls_mpi_uint) a << 0 ) | \ 91 ( (mbedtls_mpi_uint) b << 8 ) | \ 92 ( (mbedtls_mpi_uint) c << 16 ) | \ 93 ( (mbedtls_mpi_uint) d << 24 ) | \ 94 ( (mbedtls_mpi_uint) e << 32 ) | \ 95 ( (mbedtls_mpi_uint) f << 40 ) | \ 96 ( (mbedtls_mpi_uint) g << 48 ) | \ 97 ( (mbedtls_mpi_uint) h << 56 ) 98 99 #define BYTES_TO_T_UINT_4( a, b, c, d ) \ 100 BYTES_TO_T_UINT_8( a, b, c, d, 0, 0, 0, 0 ) 101 102 #define BYTES_TO_T_UINT_2( a, b ) \ 103 BYTES_TO_T_UINT_8( a, b, 0, 0, 0, 0, 0, 0 ) 104 105 #endif /* bits in mbedtls_mpi_uint */ 106 107 /* 108 * Note: the constants are in little-endian order 109 * to be directly usable in MPIs 110 */ 111 112 /* 113 * Domain parameters for secp192r1 114 */ 115 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) 116 static const mbedtls_mpi_uint secp192r1_p[] = { 117 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 118 BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 119 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 120 }; 121 static const mbedtls_mpi_uint secp192r1_b[] = { 122 BYTES_TO_T_UINT_8( 0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE ), 123 BYTES_TO_T_UINT_8( 0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F ), 124 BYTES_TO_T_UINT_8( 0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64 ), 125 }; 126 static const mbedtls_mpi_uint secp192r1_gx[] = { 127 BYTES_TO_T_UINT_8( 0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4 ), 128 BYTES_TO_T_UINT_8( 0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C ), 129 BYTES_TO_T_UINT_8( 0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18 ), 130 }; 131 static const mbedtls_mpi_uint secp192r1_gy[] = { 132 BYTES_TO_T_UINT_8( 0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73 ), 133 BYTES_TO_T_UINT_8( 0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63 ), 134 BYTES_TO_T_UINT_8( 0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07 ), 135 }; 136 static const mbedtls_mpi_uint secp192r1_n[] = { 137 BYTES_TO_T_UINT_8( 0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14 ), 138 BYTES_TO_T_UINT_8( 0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF ), 139 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 140 }; 141 #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */ 142 143 /* 144 * Domain parameters for secp224r1 145 */ 146 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) 147 static const mbedtls_mpi_uint secp224r1_p[] = { 148 BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), 149 BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ), 150 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 151 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ), 152 }; 153 static const mbedtls_mpi_uint secp224r1_b[] = { 154 BYTES_TO_T_UINT_8( 0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27 ), 155 BYTES_TO_T_UINT_8( 0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50 ), 156 BYTES_TO_T_UINT_8( 0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C ), 157 BYTES_TO_T_UINT_4( 0x85, 0x0A, 0x05, 0xB4 ), 158 }; 159 static const mbedtls_mpi_uint secp224r1_gx[] = { 160 BYTES_TO_T_UINT_8( 0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34 ), 161 BYTES_TO_T_UINT_8( 0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A ), 162 BYTES_TO_T_UINT_8( 0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B ), 163 BYTES_TO_T_UINT_4( 0xBD, 0x0C, 0x0E, 0xB7 ), 164 }; 165 static const mbedtls_mpi_uint secp224r1_gy[] = { 166 BYTES_TO_T_UINT_8( 0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44 ), 167 BYTES_TO_T_UINT_8( 0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD ), 168 BYTES_TO_T_UINT_8( 0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5 ), 169 BYTES_TO_T_UINT_4( 0x88, 0x63, 0x37, 0xBD ), 170 }; 171 static const mbedtls_mpi_uint secp224r1_n[] = { 172 BYTES_TO_T_UINT_8( 0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13 ), 173 BYTES_TO_T_UINT_8( 0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF ), 174 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 175 BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ), 176 }; 177 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */ 178 179 /* 180 * Domain parameters for secp256r1 181 */ 182 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) 183 static const mbedtls_mpi_uint secp256r1_p[] = { 184 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 185 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ), 186 BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), 187 BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ), 188 }; 189 static const mbedtls_mpi_uint secp256r1_b[] = { 190 BYTES_TO_T_UINT_8( 0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B ), 191 BYTES_TO_T_UINT_8( 0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65 ), 192 BYTES_TO_T_UINT_8( 0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3 ), 193 BYTES_TO_T_UINT_8( 0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A ), 194 }; 195 static const mbedtls_mpi_uint secp256r1_gx[] = { 196 BYTES_TO_T_UINT_8( 0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4 ), 197 BYTES_TO_T_UINT_8( 0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77 ), 198 BYTES_TO_T_UINT_8( 0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8 ), 199 BYTES_TO_T_UINT_8( 0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B ), 200 }; 201 static const mbedtls_mpi_uint secp256r1_gy[] = { 202 BYTES_TO_T_UINT_8( 0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB ), 203 BYTES_TO_T_UINT_8( 0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B ), 204 BYTES_TO_T_UINT_8( 0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E ), 205 BYTES_TO_T_UINT_8( 0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F ), 206 }; 207 static const mbedtls_mpi_uint secp256r1_n[] = { 208 BYTES_TO_T_UINT_8( 0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3 ), 209 BYTES_TO_T_UINT_8( 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC ), 210 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 211 BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ), 212 }; 213 #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */ 214 215 /* 216 * Domain parameters for secp384r1 217 */ 218 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) 219 static const mbedtls_mpi_uint secp384r1_p[] = { 220 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ), 221 BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ), 222 BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 223 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 224 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 225 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 226 }; 227 static const mbedtls_mpi_uint secp384r1_b[] = { 228 BYTES_TO_T_UINT_8( 0xEF, 0x2A, 0xEC, 0xD3, 0xED, 0xC8, 0x85, 0x2A ), 229 BYTES_TO_T_UINT_8( 0x9D, 0xD1, 0x2E, 0x8A, 0x8D, 0x39, 0x56, 0xC6 ), 230 BYTES_TO_T_UINT_8( 0x5A, 0x87, 0x13, 0x50, 0x8F, 0x08, 0x14, 0x03 ), 231 BYTES_TO_T_UINT_8( 0x12, 0x41, 0x81, 0xFE, 0x6E, 0x9C, 0x1D, 0x18 ), 232 BYTES_TO_T_UINT_8( 0x19, 0x2D, 0xF8, 0xE3, 0x6B, 0x05, 0x8E, 0x98 ), 233 BYTES_TO_T_UINT_8( 0xE4, 0xE7, 0x3E, 0xE2, 0xA7, 0x2F, 0x31, 0xB3 ), 234 }; 235 static const mbedtls_mpi_uint secp384r1_gx[] = { 236 BYTES_TO_T_UINT_8( 0xB7, 0x0A, 0x76, 0x72, 0x38, 0x5E, 0x54, 0x3A ), 237 BYTES_TO_T_UINT_8( 0x6C, 0x29, 0x55, 0xBF, 0x5D, 0xF2, 0x02, 0x55 ), 238 BYTES_TO_T_UINT_8( 0x38, 0x2A, 0x54, 0x82, 0xE0, 0x41, 0xF7, 0x59 ), 239 BYTES_TO_T_UINT_8( 0x98, 0x9B, 0xA7, 0x8B, 0x62, 0x3B, 0x1D, 0x6E ), 240 BYTES_TO_T_UINT_8( 0x74, 0xAD, 0x20, 0xF3, 0x1E, 0xC7, 0xB1, 0x8E ), 241 BYTES_TO_T_UINT_8( 0x37, 0x05, 0x8B, 0xBE, 0x22, 0xCA, 0x87, 0xAA ), 242 }; 243 static const mbedtls_mpi_uint secp384r1_gy[] = { 244 BYTES_TO_T_UINT_8( 0x5F, 0x0E, 0xEA, 0x90, 0x7C, 0x1D, 0x43, 0x7A ), 245 BYTES_TO_T_UINT_8( 0x9D, 0x81, 0x7E, 0x1D, 0xCE, 0xB1, 0x60, 0x0A ), 246 BYTES_TO_T_UINT_8( 0xC0, 0xB8, 0xF0, 0xB5, 0x13, 0x31, 0xDA, 0xE9 ), 247 BYTES_TO_T_UINT_8( 0x7C, 0x14, 0x9A, 0x28, 0xBD, 0x1D, 0xF4, 0xF8 ), 248 BYTES_TO_T_UINT_8( 0x29, 0xDC, 0x92, 0x92, 0xBF, 0x98, 0x9E, 0x5D ), 249 BYTES_TO_T_UINT_8( 0x6F, 0x2C, 0x26, 0x96, 0x4A, 0xDE, 0x17, 0x36 ), 250 }; 251 static const mbedtls_mpi_uint secp384r1_n[] = { 252 BYTES_TO_T_UINT_8( 0x73, 0x29, 0xC5, 0xCC, 0x6A, 0x19, 0xEC, 0xEC ), 253 BYTES_TO_T_UINT_8( 0x7A, 0xA7, 0xB0, 0x48, 0xB2, 0x0D, 0x1A, 0x58 ), 254 BYTES_TO_T_UINT_8( 0xDF, 0x2D, 0x37, 0xF4, 0x81, 0x4D, 0x63, 0xC7 ), 255 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 256 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 257 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 258 }; 259 #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */ 260 261 /* 262 * Domain parameters for secp521r1 263 */ 264 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) 265 static const mbedtls_mpi_uint secp521r1_p[] = { 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 ), 269 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 270 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 271 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 272 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 273 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 274 BYTES_TO_T_UINT_2( 0xFF, 0x01 ), 275 }; 276 static const mbedtls_mpi_uint secp521r1_b[] = { 277 BYTES_TO_T_UINT_8( 0x00, 0x3F, 0x50, 0x6B, 0xD4, 0x1F, 0x45, 0xEF ), 278 BYTES_TO_T_UINT_8( 0xF1, 0x34, 0x2C, 0x3D, 0x88, 0xDF, 0x73, 0x35 ), 279 BYTES_TO_T_UINT_8( 0x07, 0xBF, 0xB1, 0x3B, 0xBD, 0xC0, 0x52, 0x16 ), 280 BYTES_TO_T_UINT_8( 0x7B, 0x93, 0x7E, 0xEC, 0x51, 0x39, 0x19, 0x56 ), 281 BYTES_TO_T_UINT_8( 0xE1, 0x09, 0xF1, 0x8E, 0x91, 0x89, 0xB4, 0xB8 ), 282 BYTES_TO_T_UINT_8( 0xF3, 0x15, 0xB3, 0x99, 0x5B, 0x72, 0xDA, 0xA2 ), 283 BYTES_TO_T_UINT_8( 0xEE, 0x40, 0x85, 0xB6, 0xA0, 0x21, 0x9A, 0x92 ), 284 BYTES_TO_T_UINT_8( 0x1F, 0x9A, 0x1C, 0x8E, 0x61, 0xB9, 0x3E, 0x95 ), 285 BYTES_TO_T_UINT_2( 0x51, 0x00 ), 286 }; 287 static const mbedtls_mpi_uint secp521r1_gx[] = { 288 BYTES_TO_T_UINT_8( 0x66, 0xBD, 0xE5, 0xC2, 0x31, 0x7E, 0x7E, 0xF9 ), 289 BYTES_TO_T_UINT_8( 0x9B, 0x42, 0x6A, 0x85, 0xC1, 0xB3, 0x48, 0x33 ), 290 BYTES_TO_T_UINT_8( 0xDE, 0xA8, 0xFF, 0xA2, 0x27, 0xC1, 0x1D, 0xFE ), 291 BYTES_TO_T_UINT_8( 0x28, 0x59, 0xE7, 0xEF, 0x77, 0x5E, 0x4B, 0xA1 ), 292 BYTES_TO_T_UINT_8( 0xBA, 0x3D, 0x4D, 0x6B, 0x60, 0xAF, 0x28, 0xF8 ), 293 BYTES_TO_T_UINT_8( 0x21, 0xB5, 0x3F, 0x05, 0x39, 0x81, 0x64, 0x9C ), 294 BYTES_TO_T_UINT_8( 0x42, 0xB4, 0x95, 0x23, 0x66, 0xCB, 0x3E, 0x9E ), 295 BYTES_TO_T_UINT_8( 0xCD, 0xE9, 0x04, 0x04, 0xB7, 0x06, 0x8E, 0x85 ), 296 BYTES_TO_T_UINT_2( 0xC6, 0x00 ), 297 }; 298 static const mbedtls_mpi_uint secp521r1_gy[] = { 299 BYTES_TO_T_UINT_8( 0x50, 0x66, 0xD1, 0x9F, 0x76, 0x94, 0xBE, 0x88 ), 300 BYTES_TO_T_UINT_8( 0x40, 0xC2, 0x72, 0xA2, 0x86, 0x70, 0x3C, 0x35 ), 301 BYTES_TO_T_UINT_8( 0x61, 0x07, 0xAD, 0x3F, 0x01, 0xB9, 0x50, 0xC5 ), 302 BYTES_TO_T_UINT_8( 0x40, 0x26, 0xF4, 0x5E, 0x99, 0x72, 0xEE, 0x97 ), 303 BYTES_TO_T_UINT_8( 0x2C, 0x66, 0x3E, 0x27, 0x17, 0xBD, 0xAF, 0x17 ), 304 BYTES_TO_T_UINT_8( 0x68, 0x44, 0x9B, 0x57, 0x49, 0x44, 0xF5, 0x98 ), 305 BYTES_TO_T_UINT_8( 0xD9, 0x1B, 0x7D, 0x2C, 0xB4, 0x5F, 0x8A, 0x5C ), 306 BYTES_TO_T_UINT_8( 0x04, 0xC0, 0x3B, 0x9A, 0x78, 0x6A, 0x29, 0x39 ), 307 BYTES_TO_T_UINT_2( 0x18, 0x01 ), 308 }; 309 static const mbedtls_mpi_uint secp521r1_n[] = { 310 BYTES_TO_T_UINT_8( 0x09, 0x64, 0x38, 0x91, 0x1E, 0xB7, 0x6F, 0xBB ), 311 BYTES_TO_T_UINT_8( 0xAE, 0x47, 0x9C, 0x89, 0xB8, 0xC9, 0xB5, 0x3B ), 312 BYTES_TO_T_UINT_8( 0xD0, 0xA5, 0x09, 0xF7, 0x48, 0x01, 0xCC, 0x7F ), 313 BYTES_TO_T_UINT_8( 0x6B, 0x96, 0x2F, 0xBF, 0x83, 0x87, 0x86, 0x51 ), 314 BYTES_TO_T_UINT_8( 0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 315 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 316 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 317 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 318 BYTES_TO_T_UINT_2( 0xFF, 0x01 ), 319 }; 320 #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */ 321 322 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) 323 static const mbedtls_mpi_uint secp192k1_p[] = { 324 BYTES_TO_T_UINT_8( 0x37, 0xEE, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ), 325 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 326 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 327 }; 328 static const mbedtls_mpi_uint secp192k1_a[] = { 329 BYTES_TO_T_UINT_2( 0x00, 0x00 ), 330 }; 331 static const mbedtls_mpi_uint secp192k1_b[] = { 332 BYTES_TO_T_UINT_2( 0x03, 0x00 ), 333 }; 334 static const mbedtls_mpi_uint secp192k1_gx[] = { 335 BYTES_TO_T_UINT_8( 0x7D, 0x6C, 0xE0, 0xEA, 0xB1, 0xD1, 0xA5, 0x1D ), 336 BYTES_TO_T_UINT_8( 0x34, 0xF4, 0xB7, 0x80, 0x02, 0x7D, 0xB0, 0x26 ), 337 BYTES_TO_T_UINT_8( 0xAE, 0xE9, 0x57, 0xC0, 0x0E, 0xF1, 0x4F, 0xDB ), 338 }; 339 static const mbedtls_mpi_uint secp192k1_gy[] = { 340 BYTES_TO_T_UINT_8( 0x9D, 0x2F, 0x5E, 0xD9, 0x88, 0xAA, 0x82, 0x40 ), 341 BYTES_TO_T_UINT_8( 0x34, 0x86, 0xBE, 0x15, 0xD0, 0x63, 0x41, 0x84 ), 342 BYTES_TO_T_UINT_8( 0xA7, 0x28, 0x56, 0x9C, 0x6D, 0x2F, 0x2F, 0x9B ), 343 }; 344 static const mbedtls_mpi_uint secp192k1_n[] = { 345 BYTES_TO_T_UINT_8( 0x8D, 0xFD, 0xDE, 0x74, 0x6A, 0x46, 0x69, 0x0F ), 346 BYTES_TO_T_UINT_8( 0x17, 0xFC, 0xF2, 0x26, 0xFE, 0xFF, 0xFF, 0xFF ), 347 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 348 }; 349 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */ 350 351 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) 352 static const mbedtls_mpi_uint secp224k1_p[] = { 353 BYTES_TO_T_UINT_8( 0x6D, 0xE5, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ), 354 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 355 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 356 BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ), 357 }; 358 static const mbedtls_mpi_uint secp224k1_a[] = { 359 BYTES_TO_T_UINT_2( 0x00, 0x00 ), 360 }; 361 static const mbedtls_mpi_uint secp224k1_b[] = { 362 BYTES_TO_T_UINT_2( 0x05, 0x00 ), 363 }; 364 static const mbedtls_mpi_uint secp224k1_gx[] = { 365 BYTES_TO_T_UINT_8( 0x5C, 0xA4, 0xB7, 0xB6, 0x0E, 0x65, 0x7E, 0x0F ), 366 BYTES_TO_T_UINT_8( 0xA9, 0x75, 0x70, 0xE4, 0xE9, 0x67, 0xA4, 0x69 ), 367 BYTES_TO_T_UINT_8( 0xA1, 0x28, 0xFC, 0x30, 0xDF, 0x99, 0xF0, 0x4D ), 368 BYTES_TO_T_UINT_4( 0x33, 0x5B, 0x45, 0xA1 ), 369 }; 370 static const mbedtls_mpi_uint secp224k1_gy[] = { 371 BYTES_TO_T_UINT_8( 0xA5, 0x61, 0x6D, 0x55, 0xDB, 0x4B, 0xCA, 0xE2 ), 372 BYTES_TO_T_UINT_8( 0x59, 0xBD, 0xB0, 0xC0, 0xF7, 0x19, 0xE3, 0xF7 ), 373 BYTES_TO_T_UINT_8( 0xD6, 0xFB, 0xCA, 0x82, 0x42, 0x34, 0xBA, 0x7F ), 374 BYTES_TO_T_UINT_4( 0xED, 0x9F, 0x08, 0x7E ), 375 }; 376 static const mbedtls_mpi_uint secp224k1_n[] = { 377 BYTES_TO_T_UINT_8( 0xF7, 0xB1, 0x9F, 0x76, 0x71, 0xA9, 0xF0, 0xCA ), 378 BYTES_TO_T_UINT_8( 0x84, 0x61, 0xEC, 0xD2, 0xE8, 0xDC, 0x01, 0x00 ), 379 BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), 380 BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ), 381 }; 382 #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */ 383 384 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) 385 static const mbedtls_mpi_uint secp256k1_p[] = { 386 BYTES_TO_T_UINT_8( 0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ), 387 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 388 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 389 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 390 }; 391 static const mbedtls_mpi_uint secp256k1_a[] = { 392 BYTES_TO_T_UINT_2( 0x00, 0x00 ), 393 }; 394 static const mbedtls_mpi_uint secp256k1_b[] = { 395 BYTES_TO_T_UINT_2( 0x07, 0x00 ), 396 }; 397 static const mbedtls_mpi_uint secp256k1_gx[] = { 398 BYTES_TO_T_UINT_8( 0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59 ), 399 BYTES_TO_T_UINT_8( 0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02 ), 400 BYTES_TO_T_UINT_8( 0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55 ), 401 BYTES_TO_T_UINT_8( 0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79 ), 402 }; 403 static const mbedtls_mpi_uint secp256k1_gy[] = { 404 BYTES_TO_T_UINT_8( 0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C ), 405 BYTES_TO_T_UINT_8( 0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD ), 406 BYTES_TO_T_UINT_8( 0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D ), 407 BYTES_TO_T_UINT_8( 0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48 ), 408 }; 409 static const mbedtls_mpi_uint secp256k1_n[] = { 410 BYTES_TO_T_UINT_8( 0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF ), 411 BYTES_TO_T_UINT_8( 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA ), 412 BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 413 BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), 414 }; 415 #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */ 416 417 /* 418 * Domain parameters for brainpoolP256r1 (RFC 5639 3.4) 419 */ 420 #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) 421 static const mbedtls_mpi_uint brainpoolP256r1_p[] = { 422 BYTES_TO_T_UINT_8( 0x77, 0x53, 0x6E, 0x1F, 0x1D, 0x48, 0x13, 0x20 ), 423 BYTES_TO_T_UINT_8( 0x28, 0x20, 0x26, 0xD5, 0x23, 0xF6, 0x3B, 0x6E ), 424 BYTES_TO_T_UINT_8( 0x72, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ), 425 BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ), 426 }; 427 static const mbedtls_mpi_uint brainpoolP256r1_a[] = { 428 BYTES_TO_T_UINT_8( 0xD9, 0xB5, 0x30, 0xF3, 0x44, 0x4B, 0x4A, 0xE9 ), 429 BYTES_TO_T_UINT_8( 0x6C, 0x5C, 0xDC, 0x26, 0xC1, 0x55, 0x80, 0xFB ), 430 BYTES_TO_T_UINT_8( 0xE7, 0xFF, 0x7A, 0x41, 0x30, 0x75, 0xF6, 0xEE ), 431 BYTES_TO_T_UINT_8( 0x57, 0x30, 0x2C, 0xFC, 0x75, 0x09, 0x5A, 0x7D ), 432 }; 433 static const mbedtls_mpi_uint brainpoolP256r1_b[] = { 434 BYTES_TO_T_UINT_8( 0xB6, 0x07, 0x8C, 0xFF, 0x18, 0xDC, 0xCC, 0x6B ), 435 BYTES_TO_T_UINT_8( 0xCE, 0xE1, 0xF7, 0x5C, 0x29, 0x16, 0x84, 0x95 ), 436 BYTES_TO_T_UINT_8( 0xBF, 0x7C, 0xD7, 0xBB, 0xD9, 0xB5, 0x30, 0xF3 ), 437 BYTES_TO_T_UINT_8( 0x44, 0x4B, 0x4A, 0xE9, 0x6C, 0x5C, 0xDC, 0x26 ), 438 }; 439 static const mbedtls_mpi_uint brainpoolP256r1_gx[] = { 440 BYTES_TO_T_UINT_8( 0x62, 0x32, 0xCE, 0x9A, 0xBD, 0x53, 0x44, 0x3A ), 441 BYTES_TO_T_UINT_8( 0xC2, 0x23, 0xBD, 0xE3, 0xE1, 0x27, 0xDE, 0xB9 ), 442 BYTES_TO_T_UINT_8( 0xAF, 0xB7, 0x81, 0xFC, 0x2F, 0x48, 0x4B, 0x2C ), 443 BYTES_TO_T_UINT_8( 0xCB, 0x57, 0x7E, 0xCB, 0xB9, 0xAE, 0xD2, 0x8B ), 444 }; 445 static const mbedtls_mpi_uint brainpoolP256r1_gy[] = { 446 BYTES_TO_T_UINT_8( 0x97, 0x69, 0x04, 0x2F, 0xC7, 0x54, 0x1D, 0x5C ), 447 BYTES_TO_T_UINT_8( 0x54, 0x8E, 0xED, 0x2D, 0x13, 0x45, 0x77, 0xC2 ), 448 BYTES_TO_T_UINT_8( 0xC9, 0x1D, 0x61, 0x14, 0x1A, 0x46, 0xF8, 0x97 ), 449 BYTES_TO_T_UINT_8( 0xFD, 0xC4, 0xDA, 0xC3, 0x35, 0xF8, 0x7E, 0x54 ), 450 }; 451 static const mbedtls_mpi_uint brainpoolP256r1_n[] = { 452 BYTES_TO_T_UINT_8( 0xA7, 0x56, 0x48, 0x97, 0x82, 0x0E, 0x1E, 0x90 ), 453 BYTES_TO_T_UINT_8( 0xF7, 0xA6, 0x61, 0xB5, 0xA3, 0x7A, 0x39, 0x8C ), 454 BYTES_TO_T_UINT_8( 0x71, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ), 455 BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ), 456 }; 457 #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */ 458 459 /* 460 * Domain parameters for brainpoolP384r1 (RFC 5639 3.6) 461 */ 462 #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) 463 static const mbedtls_mpi_uint brainpoolP384r1_p[] = { 464 BYTES_TO_T_UINT_8( 0x53, 0xEC, 0x07, 0x31, 0x13, 0x00, 0x47, 0x87 ), 465 BYTES_TO_T_UINT_8( 0x71, 0x1A, 0x1D, 0x90, 0x29, 0xA7, 0xD3, 0xAC ), 466 BYTES_TO_T_UINT_8( 0x23, 0x11, 0xB7, 0x7F, 0x19, 0xDA, 0xB1, 0x12 ), 467 BYTES_TO_T_UINT_8( 0xB4, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15 ), 468 BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ), 469 BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ), 470 }; 471 static const mbedtls_mpi_uint brainpoolP384r1_a[] = { 472 BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ), 473 BYTES_TO_T_UINT_8( 0xEB, 0xD4, 0x3A, 0x50, 0x4A, 0x81, 0xA5, 0x8A ), 474 BYTES_TO_T_UINT_8( 0x0F, 0xF9, 0x91, 0xBA, 0xEF, 0x65, 0x91, 0x13 ), 475 BYTES_TO_T_UINT_8( 0x87, 0x27, 0xB2, 0x4F, 0x8E, 0xA2, 0xBE, 0xC2 ), 476 BYTES_TO_T_UINT_8( 0xA0, 0xAF, 0x05, 0xCE, 0x0A, 0x08, 0x72, 0x3C ), 477 BYTES_TO_T_UINT_8( 0x0C, 0x15, 0x8C, 0x3D, 0xC6, 0x82, 0xC3, 0x7B ), 478 }; 479 static const mbedtls_mpi_uint brainpoolP384r1_b[] = { 480 BYTES_TO_T_UINT_8( 0x11, 0x4C, 0x50, 0xFA, 0x96, 0x86, 0xB7, 0x3A ), 481 BYTES_TO_T_UINT_8( 0x94, 0xC9, 0xDB, 0x95, 0x02, 0x39, 0xB4, 0x7C ), 482 BYTES_TO_T_UINT_8( 0xD5, 0x62, 0xEB, 0x3E, 0xA5, 0x0E, 0x88, 0x2E ), 483 BYTES_TO_T_UINT_8( 0xA6, 0xD2, 0xDC, 0x07, 0xE1, 0x7D, 0xB7, 0x2F ), 484 BYTES_TO_T_UINT_8( 0x7C, 0x44, 0xF0, 0x16, 0x54, 0xB5, 0x39, 0x8B ), 485 BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ), 486 }; 487 static const mbedtls_mpi_uint brainpoolP384r1_gx[] = { 488 BYTES_TO_T_UINT_8( 0x1E, 0xAF, 0xD4, 0x47, 0xE2, 0xB2, 0x87, 0xEF ), 489 BYTES_TO_T_UINT_8( 0xAA, 0x46, 0xD6, 0x36, 0x34, 0xE0, 0x26, 0xE8 ), 490 BYTES_TO_T_UINT_8( 0xE8, 0x10, 0xBD, 0x0C, 0xFE, 0xCA, 0x7F, 0xDB ), 491 BYTES_TO_T_UINT_8( 0xE3, 0x4F, 0xF1, 0x7E, 0xE7, 0xA3, 0x47, 0x88 ), 492 BYTES_TO_T_UINT_8( 0x6B, 0x3F, 0xC1, 0xB7, 0x81, 0x3A, 0xA6, 0xA2 ), 493 BYTES_TO_T_UINT_8( 0xFF, 0x45, 0xCF, 0x68, 0xF0, 0x64, 0x1C, 0x1D ), 494 }; 495 static const mbedtls_mpi_uint brainpoolP384r1_gy[] = { 496 BYTES_TO_T_UINT_8( 0x15, 0x53, 0x3C, 0x26, 0x41, 0x03, 0x82, 0x42 ), 497 BYTES_TO_T_UINT_8( 0x11, 0x81, 0x91, 0x77, 0x21, 0x46, 0x46, 0x0E ), 498 BYTES_TO_T_UINT_8( 0x28, 0x29, 0x91, 0xF9, 0x4F, 0x05, 0x9C, 0xE1 ), 499 BYTES_TO_T_UINT_8( 0x64, 0x58, 0xEC, 0xFE, 0x29, 0x0B, 0xB7, 0x62 ), 500 BYTES_TO_T_UINT_8( 0x52, 0xD5, 0xCF, 0x95, 0x8E, 0xEB, 0xB1, 0x5C ), 501 BYTES_TO_T_UINT_8( 0xA4, 0xC2, 0xF9, 0x20, 0x75, 0x1D, 0xBE, 0x8A ), 502 }; 503 static const mbedtls_mpi_uint brainpoolP384r1_n[] = { 504 BYTES_TO_T_UINT_8( 0x65, 0x65, 0x04, 0xE9, 0x02, 0x32, 0x88, 0x3B ), 505 BYTES_TO_T_UINT_8( 0x10, 0xC3, 0x7F, 0x6B, 0xAF, 0xB6, 0x3A, 0xCF ), 506 BYTES_TO_T_UINT_8( 0xA7, 0x25, 0x04, 0xAC, 0x6C, 0x6E, 0x16, 0x1F ), 507 BYTES_TO_T_UINT_8( 0xB3, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15 ), 508 BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ), 509 BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ), 510 }; 511 #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */ 512 513 /* 514 * Domain parameters for brainpoolP512r1 (RFC 5639 3.7) 515 */ 516 #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) 517 static const mbedtls_mpi_uint brainpoolP512r1_p[] = { 518 BYTES_TO_T_UINT_8( 0xF3, 0x48, 0x3A, 0x58, 0x56, 0x60, 0xAA, 0x28 ), 519 BYTES_TO_T_UINT_8( 0x85, 0xC6, 0x82, 0x2D, 0x2F, 0xFF, 0x81, 0x28 ), 520 BYTES_TO_T_UINT_8( 0xE6, 0x80, 0xA3, 0xE6, 0x2A, 0xA1, 0xCD, 0xAE ), 521 BYTES_TO_T_UINT_8( 0x42, 0x68, 0xC6, 0x9B, 0x00, 0x9B, 0x4D, 0x7D ), 522 BYTES_TO_T_UINT_8( 0x71, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6 ), 523 BYTES_TO_T_UINT_8( 0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB ), 524 BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ), 525 BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ), 526 }; 527 static const mbedtls_mpi_uint brainpoolP512r1_a[] = { 528 BYTES_TO_T_UINT_8( 0xCA, 0x94, 0xFC, 0x77, 0x4D, 0xAC, 0xC1, 0xE7 ), 529 BYTES_TO_T_UINT_8( 0xB9, 0xC7, 0xF2, 0x2B, 0xA7, 0x17, 0x11, 0x7F ), 530 BYTES_TO_T_UINT_8( 0xB5, 0xC8, 0x9A, 0x8B, 0xC9, 0xF1, 0x2E, 0x0A ), 531 BYTES_TO_T_UINT_8( 0xA1, 0x3A, 0x25, 0xA8, 0x5A, 0x5D, 0xED, 0x2D ), 532 BYTES_TO_T_UINT_8( 0xBC, 0x63, 0x98, 0xEA, 0xCA, 0x41, 0x34, 0xA8 ), 533 BYTES_TO_T_UINT_8( 0x10, 0x16, 0xF9, 0x3D, 0x8D, 0xDD, 0xCB, 0x94 ), 534 BYTES_TO_T_UINT_8( 0xC5, 0x4C, 0x23, 0xAC, 0x45, 0x71, 0x32, 0xE2 ), 535 BYTES_TO_T_UINT_8( 0x89, 0x3B, 0x60, 0x8B, 0x31, 0xA3, 0x30, 0x78 ), 536 }; 537 static const mbedtls_mpi_uint brainpoolP512r1_b[] = { 538 BYTES_TO_T_UINT_8( 0x23, 0xF7, 0x16, 0x80, 0x63, 0xBD, 0x09, 0x28 ), 539 BYTES_TO_T_UINT_8( 0xDD, 0xE5, 0xBA, 0x5E, 0xB7, 0x50, 0x40, 0x98 ), 540 BYTES_TO_T_UINT_8( 0x67, 0x3E, 0x08, 0xDC, 0xCA, 0x94, 0xFC, 0x77 ), 541 BYTES_TO_T_UINT_8( 0x4D, 0xAC, 0xC1, 0xE7, 0xB9, 0xC7, 0xF2, 0x2B ), 542 BYTES_TO_T_UINT_8( 0xA7, 0x17, 0x11, 0x7F, 0xB5, 0xC8, 0x9A, 0x8B ), 543 BYTES_TO_T_UINT_8( 0xC9, 0xF1, 0x2E, 0x0A, 0xA1, 0x3A, 0x25, 0xA8 ), 544 BYTES_TO_T_UINT_8( 0x5A, 0x5D, 0xED, 0x2D, 0xBC, 0x63, 0x98, 0xEA ), 545 BYTES_TO_T_UINT_8( 0xCA, 0x41, 0x34, 0xA8, 0x10, 0x16, 0xF9, 0x3D ), 546 }; 547 static const mbedtls_mpi_uint brainpoolP512r1_gx[] = { 548 BYTES_TO_T_UINT_8( 0x22, 0xF8, 0xB9, 0xBC, 0x09, 0x22, 0x35, 0x8B ), 549 BYTES_TO_T_UINT_8( 0x68, 0x5E, 0x6A, 0x40, 0x47, 0x50, 0x6D, 0x7C ), 550 BYTES_TO_T_UINT_8( 0x5F, 0x7D, 0xB9, 0x93, 0x7B, 0x68, 0xD1, 0x50 ), 551 BYTES_TO_T_UINT_8( 0x8D, 0xD4, 0xD0, 0xE2, 0x78, 0x1F, 0x3B, 0xFF ), 552 BYTES_TO_T_UINT_8( 0x8E, 0x09, 0xD0, 0xF4, 0xEE, 0x62, 0x3B, 0xB4 ), 553 BYTES_TO_T_UINT_8( 0xC1, 0x16, 0xD9, 0xB5, 0x70, 0x9F, 0xED, 0x85 ), 554 BYTES_TO_T_UINT_8( 0x93, 0x6A, 0x4C, 0x9C, 0x2E, 0x32, 0x21, 0x5A ), 555 BYTES_TO_T_UINT_8( 0x64, 0xD9, 0x2E, 0xD8, 0xBD, 0xE4, 0xAE, 0x81 ), 556 }; 557 static const mbedtls_mpi_uint brainpoolP512r1_gy[] = { 558 BYTES_TO_T_UINT_8( 0x92, 0x08, 0xD8, 0x3A, 0x0F, 0x1E, 0xCD, 0x78 ), 559 BYTES_TO_T_UINT_8( 0x06, 0x54, 0xF0, 0xA8, 0x2F, 0x2B, 0xCA, 0xD1 ), 560 BYTES_TO_T_UINT_8( 0xAE, 0x63, 0x27, 0x8A, 0xD8, 0x4B, 0xCA, 0x5B ), 561 BYTES_TO_T_UINT_8( 0x5E, 0x48, 0x5F, 0x4A, 0x49, 0xDE, 0xDC, 0xB2 ), 562 BYTES_TO_T_UINT_8( 0x11, 0x81, 0x1F, 0x88, 0x5B, 0xC5, 0x00, 0xA0 ), 563 BYTES_TO_T_UINT_8( 0x1A, 0x7B, 0xA5, 0x24, 0x00, 0xF7, 0x09, 0xF2 ), 564 BYTES_TO_T_UINT_8( 0xFD, 0x22, 0x78, 0xCF, 0xA9, 0xBF, 0xEA, 0xC0 ), 565 BYTES_TO_T_UINT_8( 0xEC, 0x32, 0x63, 0x56, 0x5D, 0x38, 0xDE, 0x7D ), 566 }; 567 static const mbedtls_mpi_uint brainpoolP512r1_n[] = { 568 BYTES_TO_T_UINT_8( 0x69, 0x00, 0xA9, 0x9C, 0x82, 0x96, 0x87, 0xB5 ), 569 BYTES_TO_T_UINT_8( 0xDD, 0xDA, 0x5D, 0x08, 0x81, 0xD3, 0xB1, 0x1D ), 570 BYTES_TO_T_UINT_8( 0x47, 0x10, 0xAC, 0x7F, 0x19, 0x61, 0x86, 0x41 ), 571 BYTES_TO_T_UINT_8( 0x19, 0x26, 0xA9, 0x4C, 0x41, 0x5C, 0x3E, 0x55 ), 572 BYTES_TO_T_UINT_8( 0x70, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6 ), 573 BYTES_TO_T_UINT_8( 0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB ), 574 BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ), 575 BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ), 576 }; 577 #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */ 578 579 /* 580 * Create an MPI from embedded constants 581 * (assumes len is an exact multiple of sizeof mbedtls_mpi_uint) 582 */ 583 static inline void ecp_mpi_load( mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_t len ) 584 { 585 X->s = 1; 586 X->n = len / sizeof( mbedtls_mpi_uint ); 587 X->p = (mbedtls_mpi_uint *) p; 588 } 589 590 /* 591 * Set an MPI to static value 1 592 */ 593 static inline void ecp_mpi_set1( mbedtls_mpi *X ) 594 { 595 static mbedtls_mpi_uint one[] = { 1 }; 596 X->s = 1; 597 X->n = 1; 598 X->p = one; 599 } 600 601 /* 602 * Make group available from embedded constants 603 */ 604 static int ecp_group_load( mbedtls_ecp_group *grp, 605 const mbedtls_mpi_uint *p, size_t plen, 606 const mbedtls_mpi_uint *a, size_t alen, 607 const mbedtls_mpi_uint *b, size_t blen, 608 const mbedtls_mpi_uint *gx, size_t gxlen, 609 const mbedtls_mpi_uint *gy, size_t gylen, 610 const mbedtls_mpi_uint *n, size_t nlen) 611 { 612 ecp_mpi_load( &grp->P, p, plen ); 613 if( a != NULL ) 614 ecp_mpi_load( &grp->A, a, alen ); 615 ecp_mpi_load( &grp->B, b, blen ); 616 ecp_mpi_load( &grp->N, n, nlen ); 617 618 ecp_mpi_load( &grp->G.X, gx, gxlen ); 619 ecp_mpi_load( &grp->G.Y, gy, gylen ); 620 ecp_mpi_set1( &grp->G.Z ); 621 622 grp->pbits = mbedtls_mpi_bitlen( &grp->P ); 623 grp->nbits = mbedtls_mpi_bitlen( &grp->N ); 624 625 grp->h = 1; 626 627 return( 0 ); 628 } 629 630 #if defined(MBEDTLS_ECP_NIST_OPTIM) 631 /* Forward declarations */ 632 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) 633 static int ecp_mod_p192( mbedtls_mpi * ); 634 #endif 635 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) 636 static int ecp_mod_p224( mbedtls_mpi * ); 637 #endif 638 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) 639 static int ecp_mod_p256( mbedtls_mpi * ); 640 #endif 641 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) 642 static int ecp_mod_p384( mbedtls_mpi * ); 643 #endif 644 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) 645 static int ecp_mod_p521( mbedtls_mpi * ); 646 #endif 647 648 #define NIST_MODP( P ) grp->modp = ecp_mod_ ## P; 649 #else 650 #define NIST_MODP( P ) 651 #endif /* MBEDTLS_ECP_NIST_OPTIM */ 652 653 /* Additional forward declarations */ 654 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) 655 static int ecp_mod_p255( mbedtls_mpi * ); 656 #endif 657 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) 658 static int ecp_mod_p192k1( mbedtls_mpi * ); 659 #endif 660 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) 661 static int ecp_mod_p224k1( mbedtls_mpi * ); 662 #endif 663 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) 664 static int ecp_mod_p256k1( mbedtls_mpi * ); 665 #endif 666 667 #define LOAD_GROUP_A( G ) ecp_group_load( grp, \ 668 G ## _p, sizeof( G ## _p ), \ 669 G ## _a, sizeof( G ## _a ), \ 670 G ## _b, sizeof( G ## _b ), \ 671 G ## _gx, sizeof( G ## _gx ), \ 672 G ## _gy, sizeof( G ## _gy ), \ 673 G ## _n, sizeof( G ## _n ) ) 674 675 #define LOAD_GROUP( G ) ecp_group_load( grp, \ 676 G ## _p, sizeof( G ## _p ), \ 677 NULL, 0, \ 678 G ## _b, sizeof( G ## _b ), \ 679 G ## _gx, sizeof( G ## _gx ), \ 680 G ## _gy, sizeof( G ## _gy ), \ 681 G ## _n, sizeof( G ## _n ) ) 682 683 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) 684 /* 685 * Specialized function for creating the Curve25519 group 686 */ 687 static int ecp_use_curve25519( mbedtls_ecp_group *grp ) 688 { 689 int ret; 690 691 /* Actually ( A + 2 ) / 4 */ 692 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &grp->A, 16, "01DB42" ) ); 693 694 /* P = 2^255 - 19 */ 695 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->P, 1 ) ); 696 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &grp->P, 255 ) ); 697 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &grp->P, &grp->P, 19 ) ); 698 grp->pbits = mbedtls_mpi_bitlen( &grp->P ); 699 700 /* Y intentionaly not set, since we use x/z coordinates. 701 * This is used as a marker to identify Montgomery curves! */ 702 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.X, 9 ) ); 703 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.Z, 1 ) ); 704 mbedtls_mpi_free( &grp->G.Y ); 705 706 /* Actually, the required msb for private keys */ 707 grp->nbits = 254; 708 709 cleanup: 710 if( ret != 0 ) 711 mbedtls_ecp_group_free( grp ); 712 713 return( ret ); 714 } 715 #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */ 716 717 /* 718 * Set a group using well-known domain parameters 719 */ 720 int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id ) 721 { 722 mbedtls_ecp_group_free( grp ); 723 724 grp->id = id; 725 726 switch( id ) 727 { 728 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) 729 case MBEDTLS_ECP_DP_SECP192R1: 730 NIST_MODP( p192 ); 731 return( LOAD_GROUP( secp192r1 ) ); 732 #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */ 733 734 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) 735 case MBEDTLS_ECP_DP_SECP224R1: 736 NIST_MODP( p224 ); 737 return( LOAD_GROUP( secp224r1 ) ); 738 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */ 739 740 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) 741 case MBEDTLS_ECP_DP_SECP256R1: 742 NIST_MODP( p256 ); 743 return( LOAD_GROUP( secp256r1 ) ); 744 #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */ 745 746 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) 747 case MBEDTLS_ECP_DP_SECP384R1: 748 NIST_MODP( p384 ); 749 return( LOAD_GROUP( secp384r1 ) ); 750 #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */ 751 752 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) 753 case MBEDTLS_ECP_DP_SECP521R1: 754 NIST_MODP( p521 ); 755 return( LOAD_GROUP( secp521r1 ) ); 756 #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */ 757 758 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) 759 case MBEDTLS_ECP_DP_SECP192K1: 760 grp->modp = ecp_mod_p192k1; 761 return( LOAD_GROUP_A( secp192k1 ) ); 762 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */ 763 764 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) 765 case MBEDTLS_ECP_DP_SECP224K1: 766 grp->modp = ecp_mod_p224k1; 767 return( LOAD_GROUP_A( secp224k1 ) ); 768 #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */ 769 770 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) 771 case MBEDTLS_ECP_DP_SECP256K1: 772 grp->modp = ecp_mod_p256k1; 773 return( LOAD_GROUP_A( secp256k1 ) ); 774 #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */ 775 776 #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) 777 case MBEDTLS_ECP_DP_BP256R1: 778 return( LOAD_GROUP_A( brainpoolP256r1 ) ); 779 #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */ 780 781 #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) 782 case MBEDTLS_ECP_DP_BP384R1: 783 return( LOAD_GROUP_A( brainpoolP384r1 ) ); 784 #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */ 785 786 #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) 787 case MBEDTLS_ECP_DP_BP512R1: 788 return( LOAD_GROUP_A( brainpoolP512r1 ) ); 789 #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */ 790 791 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) 792 case MBEDTLS_ECP_DP_CURVE25519: 793 grp->modp = ecp_mod_p255; 794 return( ecp_use_curve25519( grp ) ); 795 #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */ 796 797 default: 798 mbedtls_ecp_group_free( grp ); 799 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); 800 } 801 } 802 803 #if defined(MBEDTLS_ECP_NIST_OPTIM) 804 /* 805 * Fast reduction modulo the primes used by the NIST curves. 806 * 807 * These functions are critical for speed, but not needed for correct 808 * operations. So, we make the choice to heavily rely on the internals of our 809 * bignum library, which creates a tight coupling between these functions and 810 * our MPI implementation. However, the coupling between the ECP module and 811 * MPI remains loose, since these functions can be deactivated at will. 812 */ 813 814 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) 815 /* 816 * Compared to the way things are presented in FIPS 186-3 D.2, 817 * we proceed in columns, from right (least significant chunk) to left, 818 * adding chunks to N in place, and keeping a carry for the next chunk. 819 * This avoids moving things around in memory, and uselessly adding zeros, 820 * compared to the more straightforward, line-oriented approach. 821 * 822 * For this prime we need to handle data in chunks of 64 bits. 823 * Since this is always a multiple of our basic mbedtls_mpi_uint, we can 824 * use a mbedtls_mpi_uint * to designate such a chunk, and small loops to handle it. 825 */ 826 827 /* Add 64-bit chunks (dst += src) and update carry */ 828 static inline void add64( mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_mpi_uint *carry ) 829 { 830 unsigned char i; 831 mbedtls_mpi_uint c = 0; 832 for( i = 0; i < 8 / sizeof( mbedtls_mpi_uint ); i++, dst++, src++ ) 833 { 834 *dst += c; c = ( *dst < c ); 835 *dst += *src; c += ( *dst < *src ); 836 } 837 *carry += c; 838 } 839 840 /* Add carry to a 64-bit chunk and update carry */ 841 static inline void carry64( mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry ) 842 { 843 unsigned char i; 844 for( i = 0; i < 8 / sizeof( mbedtls_mpi_uint ); i++, dst++ ) 845 { 846 *dst += *carry; 847 *carry = ( *dst < *carry ); 848 } 849 } 850 851 #define WIDTH 8 / sizeof( mbedtls_mpi_uint ) 852 #define A( i ) N->p + i * WIDTH 853 #define ADD( i ) add64( p, A( i ), &c ) 854 #define NEXT p += WIDTH; carry64( p, &c ) 855 #define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0 856 857 /* 858 * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1) 859 */ 860 static int ecp_mod_p192( mbedtls_mpi *N ) 861 { 862 int ret; 863 mbedtls_mpi_uint c = 0; 864 mbedtls_mpi_uint *p, *end; 865 866 /* Make sure we have enough blocks so that A(5) is legal */ 867 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( N, 6 * WIDTH ) ); 868 869 p = N->p; 870 end = p + N->n; 871 872 ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5 873 ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5 874 ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5 875 876 cleanup: 877 return( ret ); 878 } 879 880 #undef WIDTH 881 #undef A 882 #undef ADD 883 #undef NEXT 884 #undef LAST 885 #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */ 886 887 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \ 888 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ 889 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) 890 /* 891 * The reader is advised to first understand ecp_mod_p192() since the same 892 * general structure is used here, but with additional complications: 893 * (1) chunks of 32 bits, and (2) subtractions. 894 */ 895 896 /* 897 * For these primes, we need to handle data in chunks of 32 bits. 898 * This makes it more complicated if we use 64 bits limbs in MPI, 899 * which prevents us from using a uniform access method as for p192. 900 * 901 * So, we define a mini abstraction layer to access 32 bit chunks, 902 * load them in 'cur' for work, and store them back from 'cur' when done. 903 * 904 * While at it, also define the size of N in terms of 32-bit chunks. 905 */ 906 #define LOAD32 cur = A( i ); 907 908 #if defined(MBEDTLS_HAVE_INT32) /* 32 bit */ 909 910 #define MAX32 N->n 911 #define A( j ) N->p[j] 912 #define STORE32 N->p[i] = cur; 913 914 #else /* 64-bit */ 915 916 #define MAX32 N->n * 2 917 #define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] ) 918 #define STORE32 \ 919 if( i % 2 ) { \ 920 N->p[i/2] &= 0x00000000FFFFFFFF; \ 921 N->p[i/2] |= ((mbedtls_mpi_uint) cur) << 32; \ 922 } else { \ 923 N->p[i/2] &= 0xFFFFFFFF00000000; \ 924 N->p[i/2] |= (mbedtls_mpi_uint) cur; \ 925 } 926 927 #endif /* sizeof( mbedtls_mpi_uint ) */ 928 929 /* 930 * Helpers for addition and subtraction of chunks, with signed carry. 931 */ 932 static inline void add32( uint32_t *dst, uint32_t src, signed char *carry ) 933 { 934 *dst += src; 935 *carry += ( *dst < src ); 936 } 937 938 static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry ) 939 { 940 *carry -= ( *dst < src ); 941 *dst -= src; 942 } 943 944 #define ADD( j ) add32( &cur, A( j ), &c ); 945 #define SUB( j ) sub32( &cur, A( j ), &c ); 946 947 /* 948 * Helpers for the main 'loop' 949 * (see fix_negative for the motivation of C) 950 */ 951 #define INIT( b ) \ 952 int ret; \ 953 signed char c = 0, cc; \ 954 uint32_t cur; \ 955 size_t i = 0, bits = b; \ 956 mbedtls_mpi C; \ 957 mbedtls_mpi_uint Cp[ b / 8 / sizeof( mbedtls_mpi_uint) + 1 ]; \ 958 \ 959 C.s = 1; \ 960 C.n = b / 8 / sizeof( mbedtls_mpi_uint) + 1; \ 961 C.p = Cp; \ 962 memset( Cp, 0, C.n * sizeof( mbedtls_mpi_uint ) ); \ 963 \ 964 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( N, b * 2 / 8 / sizeof( mbedtls_mpi_uint ) ) ); \ 965 LOAD32; 966 967 #define NEXT \ 968 STORE32; i++; LOAD32; \ 969 cc = c; c = 0; \ 970 if( cc < 0 ) \ 971 sub32( &cur, -cc, &c ); \ 972 else \ 973 add32( &cur, cc, &c ); \ 974 975 #define LAST \ 976 STORE32; i++; \ 977 cur = c > 0 ? c : 0; STORE32; \ 978 cur = 0; while( ++i < MAX32 ) { STORE32; } \ 979 if( c < 0 ) fix_negative( N, c, &C, bits ); 980 981 /* 982 * If the result is negative, we get it in the form 983 * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits' 984 */ 985 static inline int fix_negative( mbedtls_mpi *N, signed char c, mbedtls_mpi *C, size_t bits ) 986 { 987 int ret; 988 989 /* C = - c * 2^(bits + 32) */ 990 #if !defined(MBEDTLS_HAVE_INT64) 991 ((void) bits); 992 #else 993 if( bits == 224 ) 994 C->p[ C->n - 1 ] = ((mbedtls_mpi_uint) -c) << 32; 995 else 996 #endif 997 C->p[ C->n - 1 ] = (mbedtls_mpi_uint) -c; 998 999 /* N = - ( C - N ) */ 1000 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, C, N ) ); 1001 N->s = -1; 1002 1003 cleanup: 1004 1005 return( ret ); 1006 } 1007 1008 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) 1009 /* 1010 * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2) 1011 */ 1012 static int ecp_mod_p224( mbedtls_mpi *N ) 1013 { 1014 INIT( 224 ); 1015 1016 SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11 1017 SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12 1018 SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13 1019 SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11 1020 SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12 1021 SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13 1022 SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10 1023 1024 cleanup: 1025 return( ret ); 1026 } 1027 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */ 1028 1029 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) 1030 /* 1031 * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3) 1032 */ 1033 static int ecp_mod_p256( mbedtls_mpi *N ) 1034 { 1035 INIT( 256 ); 1036 1037 ADD( 8 ); ADD( 9 ); 1038 SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0 1039 1040 ADD( 9 ); ADD( 10 ); 1041 SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1 1042 1043 ADD( 10 ); ADD( 11 ); 1044 SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2 1045 1046 ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 ); 1047 SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3 1048 1049 ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 ); 1050 SUB( 9 ); SUB( 10 ); NEXT; // A4 1051 1052 ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 ); 1053 SUB( 10 ); SUB( 11 ); NEXT; // A5 1054 1055 ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 ); 1056 SUB( 8 ); SUB( 9 ); NEXT; // A6 1057 1058 ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 ); 1059 SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7 1060 1061 cleanup: 1062 return( ret ); 1063 } 1064 #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */ 1065 1066 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) 1067 /* 1068 * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4) 1069 */ 1070 static int ecp_mod_p384( mbedtls_mpi *N ) 1071 { 1072 INIT( 384 ); 1073 1074 ADD( 12 ); ADD( 21 ); ADD( 20 ); 1075 SUB( 23 ); NEXT; // A0 1076 1077 ADD( 13 ); ADD( 22 ); ADD( 23 ); 1078 SUB( 12 ); SUB( 20 ); NEXT; // A2 1079 1080 ADD( 14 ); ADD( 23 ); 1081 SUB( 13 ); SUB( 21 ); NEXT; // A2 1082 1083 ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 ); 1084 SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3 1085 1086 ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 ); 1087 SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4 1088 1089 ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 ); 1090 SUB( 16 ); NEXT; // A5 1091 1092 ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 ); 1093 SUB( 17 ); NEXT; // A6 1094 1095 ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 ); 1096 SUB( 18 ); NEXT; // A7 1097 1098 ADD( 20 ); ADD( 17 ); ADD( 16 ); 1099 SUB( 19 ); NEXT; // A8 1100 1101 ADD( 21 ); ADD( 18 ); ADD( 17 ); 1102 SUB( 20 ); NEXT; // A9 1103 1104 ADD( 22 ); ADD( 19 ); ADD( 18 ); 1105 SUB( 21 ); NEXT; // A10 1106 1107 ADD( 23 ); ADD( 20 ); ADD( 19 ); 1108 SUB( 22 ); LAST; // A11 1109 1110 cleanup: 1111 return( ret ); 1112 } 1113 #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */ 1114 1115 #undef A 1116 #undef LOAD32 1117 #undef STORE32 1118 #undef MAX32 1119 #undef INIT 1120 #undef NEXT 1121 #undef LAST 1122 1123 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED || 1124 MBEDTLS_ECP_DP_SECP256R1_ENABLED || 1125 MBEDTLS_ECP_DP_SECP384R1_ENABLED */ 1126 1127 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) 1128 /* 1129 * Here we have an actual Mersenne prime, so things are more straightforward. 1130 * However, chunks are aligned on a 'weird' boundary (521 bits). 1131 */ 1132 1133 /* Size of p521 in terms of mbedtls_mpi_uint */ 1134 #define P521_WIDTH ( 521 / 8 / sizeof( mbedtls_mpi_uint ) + 1 ) 1135 1136 /* Bits to keep in the most significant mbedtls_mpi_uint */ 1137 #define P521_MASK 0x01FF 1138 1139 /* 1140 * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5) 1141 * Write N as A1 + 2^521 A0, return A0 + A1 1142 */ 1143 static int ecp_mod_p521( mbedtls_mpi *N ) 1144 { 1145 int ret; 1146 size_t i; 1147 mbedtls_mpi M; 1148 mbedtls_mpi_uint Mp[P521_WIDTH + 1]; 1149 /* Worst case for the size of M is when mbedtls_mpi_uint is 16 bits: 1150 * we need to hold bits 513 to 1056, which is 34 limbs, that is 1151 * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */ 1152 1153 if( N->n < P521_WIDTH ) 1154 return( 0 ); 1155 1156 /* M = A1 */ 1157 M.s = 1; 1158 M.n = N->n - ( P521_WIDTH - 1 ); 1159 if( M.n > P521_WIDTH + 1 ) 1160 M.n = P521_WIDTH + 1; 1161 M.p = Mp; 1162 memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( mbedtls_mpi_uint ) ); 1163 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, 521 % ( 8 * sizeof( mbedtls_mpi_uint ) ) ) ); 1164 1165 /* N = A0 */ 1166 N->p[P521_WIDTH - 1] &= P521_MASK; 1167 for( i = P521_WIDTH; i < N->n; i++ ) 1168 N->p[i] = 0; 1169 1170 /* N = A0 + A1 */ 1171 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) ); 1172 1173 cleanup: 1174 return( ret ); 1175 } 1176 1177 #undef P521_WIDTH 1178 #undef P521_MASK 1179 #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */ 1180 1181 #endif /* MBEDTLS_ECP_NIST_OPTIM */ 1182 1183 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) 1184 1185 /* Size of p255 in terms of mbedtls_mpi_uint */ 1186 #define P255_WIDTH ( 255 / 8 / sizeof( mbedtls_mpi_uint ) + 1 ) 1187 1188 /* 1189 * Fast quasi-reduction modulo p255 = 2^255 - 19 1190 * Write N as A0 + 2^255 A1, return A0 + 19 * A1 1191 */ 1192 static int ecp_mod_p255( mbedtls_mpi *N ) 1193 { 1194 int ret; 1195 size_t i; 1196 mbedtls_mpi M; 1197 mbedtls_mpi_uint Mp[P255_WIDTH + 2]; 1198 1199 if( N->n < P255_WIDTH ) 1200 return( 0 ); 1201 1202 /* M = A1 */ 1203 M.s = 1; 1204 M.n = N->n - ( P255_WIDTH - 1 ); 1205 if( M.n > P255_WIDTH + 1 ) 1206 M.n = P255_WIDTH + 1; 1207 M.p = Mp; 1208 memset( Mp, 0, sizeof Mp ); 1209 memcpy( Mp, N->p + P255_WIDTH - 1, M.n * sizeof( mbedtls_mpi_uint ) ); 1210 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, 255 % ( 8 * sizeof( mbedtls_mpi_uint ) ) ) ); 1211 M.n++; /* Make room for multiplication by 19 */ 1212 1213 /* N = A0 */ 1214 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( N, 255, 0 ) ); 1215 for( i = P255_WIDTH; i < N->n; i++ ) 1216 N->p[i] = 0; 1217 1218 /* N = A0 + 19 * A1 */ 1219 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &M, 19 ) ); 1220 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) ); 1221 1222 cleanup: 1223 return( ret ); 1224 } 1225 #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */ 1226 1227 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \ 1228 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \ 1229 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) 1230 /* 1231 * Fast quasi-reduction modulo P = 2^s - R, 1232 * with R about 33 bits, used by the Koblitz curves. 1233 * 1234 * Write N as A0 + 2^224 A1, return A0 + R * A1. 1235 * Actually do two passes, since R is big. 1236 */ 1237 #define P_KOBLITZ_MAX ( 256 / 8 / sizeof( mbedtls_mpi_uint ) ) // Max limbs in P 1238 #define P_KOBLITZ_R ( 8 / sizeof( mbedtls_mpi_uint ) ) // Limbs in R 1239 static inline int ecp_mod_koblitz( mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t p_limbs, 1240 size_t adjust, size_t shift, mbedtls_mpi_uint mask ) 1241 { 1242 int ret; 1243 size_t i; 1244 mbedtls_mpi M, R; 1245 mbedtls_mpi_uint Mp[P_KOBLITZ_MAX + P_KOBLITZ_R + 1]; 1246 1247 if( N->n < p_limbs ) 1248 return( 0 ); 1249 1250 /* Init R */ 1251 R.s = 1; 1252 R.p = Rp; 1253 R.n = P_KOBLITZ_R; 1254 1255 /* Common setup for M */ 1256 M.s = 1; 1257 M.p = Mp; 1258 1259 /* M = A1 */ 1260 M.n = N->n - ( p_limbs - adjust ); 1261 if( M.n > p_limbs + adjust ) 1262 M.n = p_limbs + adjust; 1263 memset( Mp, 0, sizeof Mp ); 1264 memcpy( Mp, N->p + p_limbs - adjust, M.n * sizeof( mbedtls_mpi_uint ) ); 1265 if( shift != 0 ) 1266 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, shift ) ); 1267 M.n += R.n; /* Make room for multiplication by R */ 1268 1269 /* N = A0 */ 1270 if( mask != 0 ) 1271 N->p[p_limbs - 1] &= mask; 1272 for( i = p_limbs; i < N->n; i++ ) 1273 N->p[i] = 0; 1274 1275 /* N = A0 + R * A1 */ 1276 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &M, &M, &R ) ); 1277 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) ); 1278 1279 /* Second pass */ 1280 1281 /* M = A1 */ 1282 M.n = N->n - ( p_limbs - adjust ); 1283 if( M.n > p_limbs + adjust ) 1284 M.n = p_limbs + adjust; 1285 memset( Mp, 0, sizeof Mp ); 1286 memcpy( Mp, N->p + p_limbs - adjust, M.n * sizeof( mbedtls_mpi_uint ) ); 1287 if( shift != 0 ) 1288 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, shift ) ); 1289 M.n += R.n; /* Make room for multiplication by R */ 1290 1291 /* N = A0 */ 1292 if( mask != 0 ) 1293 N->p[p_limbs - 1] &= mask; 1294 for( i = p_limbs; i < N->n; i++ ) 1295 N->p[i] = 0; 1296 1297 /* N = A0 + R * A1 */ 1298 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &M, &M, &R ) ); 1299 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) ); 1300 1301 cleanup: 1302 return( ret ); 1303 } 1304 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED) || 1305 MBEDTLS_ECP_DP_SECP224K1_ENABLED) || 1306 MBEDTLS_ECP_DP_SECP256K1_ENABLED) */ 1307 1308 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) 1309 /* 1310 * Fast quasi-reduction modulo p192k1 = 2^192 - R, 1311 * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119 1312 */ 1313 static int ecp_mod_p192k1( mbedtls_mpi *N ) 1314 { 1315 static mbedtls_mpi_uint Rp[] = { 1316 BYTES_TO_T_UINT_8( 0xC9, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) }; 1317 1318 return( ecp_mod_koblitz( N, Rp, 192 / 8 / sizeof( mbedtls_mpi_uint ), 0, 0, 0 ) ); 1319 } 1320 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */ 1321 1322 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) 1323 /* 1324 * Fast quasi-reduction modulo p224k1 = 2^224 - R, 1325 * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93 1326 */ 1327 static int ecp_mod_p224k1( mbedtls_mpi *N ) 1328 { 1329 static mbedtls_mpi_uint Rp[] = { 1330 BYTES_TO_T_UINT_8( 0x93, 0x1A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) }; 1331 1332 #if defined(MBEDTLS_HAVE_INT64) 1333 return( ecp_mod_koblitz( N, Rp, 4, 1, 32, 0xFFFFFFFF ) ); 1334 #else 1335 return( ecp_mod_koblitz( N, Rp, 224 / 8 / sizeof( mbedtls_mpi_uint ), 0, 0, 0 ) ); 1336 #endif 1337 } 1338 1339 #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */ 1340 1341 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) 1342 /* 1343 * Fast quasi-reduction modulo p256k1 = 2^256 - R, 1344 * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1 1345 */ 1346 static int ecp_mod_p256k1( mbedtls_mpi *N ) 1347 { 1348 static mbedtls_mpi_uint Rp[] = { 1349 BYTES_TO_T_UINT_8( 0xD1, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) }; 1350 return( ecp_mod_koblitz( N, Rp, 256 / 8 / sizeof( mbedtls_mpi_uint ), 0, 0, 0 ) ); 1351 } 1352 #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */ 1353 1354 #endif /* !MBEDTLS_ECP_ALT */ 1355 1356 #endif /* MBEDTLS_ECP_C */ 1357