mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

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