Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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