mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 19:50:30 2017 +0100
Revision:
2:bbdeda018a3c
Parent:
1:9ebc941037d5
Update to mbedtls 2.6.0, many changes.

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

remove the following directorys:

programs
yotta
visualc

Who changed what in which revision?

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