Modified mbed TLS headers for AES functionality only to reduce build size

Dependents:   BLE_Gateway_Linker_fix BLE_Gateway

Fork of mbedtls by sandbox

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

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