mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ecp_curves.c Source File

ecp_curves.c

00001 /*
00002  *  Elliptic curves over GF(p): curve-specific data and functions
00003  *
00004  *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
00005  *
00006  *  This file is part of mbed TLS (https://tls.mbed.org)
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License along
00019  *  with this program; if not, write to the Free Software Foundation, Inc.,
00020  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021  */
00022 
00023 #if !defined(POLARSSL_CONFIG_FILE)
00024 #include "polarssl/config.h"
00025 #else
00026 #include POLARSSL_CONFIG_FILE
00027 #endif
00028 
00029 #if defined(POLARSSL_ECP_C)
00030 
00031 #include "polarssl/ecp.h"
00032 
00033 #include <string.h>
00034 
00035 #if defined(_MSC_VER) && !defined(inline)
00036 #define inline _inline
00037 #else
00038 #if defined(__ARMCC_VERSION) && !defined(inline)
00039 #define inline __inline
00040 #endif /* __ARMCC_VERSION */
00041 #endif /*_MSC_VER */
00042 
00043 /*
00044  * Conversion macros for embedded constants:
00045  * build lists of t_uint's from lists of unsigned char's grouped by 8, 4 or 2
00046  */
00047 #if defined(POLARSSL_HAVE_INT8)
00048 
00049 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
00050     a, b, c, d, e, f, g, h
00051 
00052 #define BYTES_TO_T_UINT_4( a, b, c, d )             \
00053     a, b, c, d
00054 
00055 #define BYTES_TO_T_UINT_2( a, b )                   \
00056     a, b
00057 
00058 #elif defined(POLARSSL_HAVE_INT16)
00059 
00060 #define BYTES_TO_T_UINT_2( a, b )                   \
00061     ( (t_uint) a << 0 ) |                           \
00062     ( (t_uint) b << 8 )
00063 
00064 #define BYTES_TO_T_UINT_4( a, b, c, d )             \
00065     BYTES_TO_T_UINT_2( a, b ),                      \
00066     BYTES_TO_T_UINT_2( c, d )
00067 
00068 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
00069     BYTES_TO_T_UINT_2( a, b ),                      \
00070     BYTES_TO_T_UINT_2( c, d ),                      \
00071     BYTES_TO_T_UINT_2( e, f ),                      \
00072     BYTES_TO_T_UINT_2( g, h )
00073 
00074 #elif defined(POLARSSL_HAVE_INT32)
00075 
00076 #define BYTES_TO_T_UINT_4( a, b, c, d )             \
00077     ( (t_uint) a <<  0 ) |                          \
00078     ( (t_uint) b <<  8 ) |                          \
00079     ( (t_uint) c << 16 ) |                          \
00080     ( (t_uint) d << 24 )
00081 
00082 #define BYTES_TO_T_UINT_2( a, b )                   \
00083     BYTES_TO_T_UINT_4( a, b, 0, 0 )
00084 
00085 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
00086     BYTES_TO_T_UINT_4( a, b, c, d ),                \
00087     BYTES_TO_T_UINT_4( e, f, g, h )
00088 
00089 #else /* 64-bits */
00090 
00091 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
00092     ( (t_uint) a <<  0 ) |                          \
00093     ( (t_uint) b <<  8 ) |                          \
00094     ( (t_uint) c << 16 ) |                          \
00095     ( (t_uint) d << 24 ) |                          \
00096     ( (t_uint) e << 32 ) |                          \
00097     ( (t_uint) f << 40 ) |                          \
00098     ( (t_uint) g << 48 ) |                          \
00099     ( (t_uint) h << 56 )
00100 
00101 #define BYTES_TO_T_UINT_4( a, b, c, d )             \
00102     BYTES_TO_T_UINT_8( a, b, c, d, 0, 0, 0, 0 )
00103 
00104 #define BYTES_TO_T_UINT_2( a, b )                   \
00105     BYTES_TO_T_UINT_8( a, b, 0, 0, 0, 0, 0, 0 )
00106 
00107 #endif /* bits in t_uint */
00108 
00109 /*
00110  * Note: the constants are in little-endian order
00111  * to be directly usable in MPIs
00112  */
00113 
00114 /*
00115  * Domain parameters for secp192r1
00116  */
00117 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
00118 static const t_uint secp192r1_p[] = {
00119     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00120     BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00121     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00122 };
00123 static const t_uint secp192r1_b[] = {
00124     BYTES_TO_T_UINT_8( 0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE ),
00125     BYTES_TO_T_UINT_8( 0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F ),
00126     BYTES_TO_T_UINT_8( 0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64 ),
00127 };
00128 static const t_uint secp192r1_gx[] = {
00129     BYTES_TO_T_UINT_8( 0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4 ),
00130     BYTES_TO_T_UINT_8( 0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C ),
00131     BYTES_TO_T_UINT_8( 0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18 ),
00132 };
00133 static const t_uint secp192r1_gy[] = {
00134     BYTES_TO_T_UINT_8( 0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73 ),
00135     BYTES_TO_T_UINT_8( 0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63 ),
00136     BYTES_TO_T_UINT_8( 0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07 ),
00137 };
00138 static const t_uint secp192r1_n[] = {
00139     BYTES_TO_T_UINT_8( 0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14 ),
00140     BYTES_TO_T_UINT_8( 0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF ),
00141     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00142 };
00143 #endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
00144 
00145 /*
00146  * Domain parameters for secp224r1
00147  */
00148 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
00149 static const t_uint secp224r1_p[] = {
00150     BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
00151     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
00152     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00153     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ),
00154 };
00155 static const t_uint secp224r1_b[] = {
00156     BYTES_TO_T_UINT_8( 0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27 ),
00157     BYTES_TO_T_UINT_8( 0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50 ),
00158     BYTES_TO_T_UINT_8( 0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C ),
00159     BYTES_TO_T_UINT_4( 0x85, 0x0A, 0x05, 0xB4 ),
00160 };
00161 static const t_uint secp224r1_gx[] = {
00162     BYTES_TO_T_UINT_8( 0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34 ),
00163     BYTES_TO_T_UINT_8( 0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A ),
00164     BYTES_TO_T_UINT_8( 0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B ),
00165     BYTES_TO_T_UINT_4( 0xBD, 0x0C, 0x0E, 0xB7 ),
00166 };
00167 static const t_uint secp224r1_gy[] = {
00168     BYTES_TO_T_UINT_8( 0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44 ),
00169     BYTES_TO_T_UINT_8( 0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD ),
00170     BYTES_TO_T_UINT_8( 0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5 ),
00171     BYTES_TO_T_UINT_4( 0x88, 0x63, 0x37, 0xBD ),
00172 };
00173 static const t_uint secp224r1_n[] = {
00174     BYTES_TO_T_UINT_8( 0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13 ),
00175     BYTES_TO_T_UINT_8( 0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF ),
00176     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00177     BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ),
00178 };
00179 #endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
00180 
00181 /*
00182  * Domain parameters for secp256r1
00183  */
00184 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
00185 static const t_uint secp256r1_p[] = {
00186     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00187     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ),
00188     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
00189     BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
00190 };
00191 static const t_uint secp256r1_b[] = {
00192     BYTES_TO_T_UINT_8( 0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B ),
00193     BYTES_TO_T_UINT_8( 0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65 ),
00194     BYTES_TO_T_UINT_8( 0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3 ),
00195     BYTES_TO_T_UINT_8( 0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A ),
00196 };
00197 static const t_uint secp256r1_gx[] = {
00198     BYTES_TO_T_UINT_8( 0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4 ),
00199     BYTES_TO_T_UINT_8( 0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77 ),
00200     BYTES_TO_T_UINT_8( 0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8 ),
00201     BYTES_TO_T_UINT_8( 0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B ),
00202 };
00203 static const t_uint secp256r1_gy[] = {
00204     BYTES_TO_T_UINT_8( 0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB ),
00205     BYTES_TO_T_UINT_8( 0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B ),
00206     BYTES_TO_T_UINT_8( 0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E ),
00207     BYTES_TO_T_UINT_8( 0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F ),
00208 };
00209 static const t_uint secp256r1_n[] = {
00210     BYTES_TO_T_UINT_8( 0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3 ),
00211     BYTES_TO_T_UINT_8( 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC ),
00212     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00213     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
00214 };
00215 #endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
00216 
00217 /*
00218  * Domain parameters for secp384r1
00219  */
00220 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
00221 static const t_uint secp384r1_p[] = {
00222     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ),
00223     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
00224     BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00225     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00226     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00227     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00228 };
00229 static const t_uint secp384r1_b[] = {
00230     BYTES_TO_T_UINT_8( 0xEF, 0x2A, 0xEC, 0xD3, 0xED, 0xC8, 0x85, 0x2A ),
00231     BYTES_TO_T_UINT_8( 0x9D, 0xD1, 0x2E, 0x8A, 0x8D, 0x39, 0x56, 0xC6 ),
00232     BYTES_TO_T_UINT_8( 0x5A, 0x87, 0x13, 0x50, 0x8F, 0x08, 0x14, 0x03 ),
00233     BYTES_TO_T_UINT_8( 0x12, 0x41, 0x81, 0xFE, 0x6E, 0x9C, 0x1D, 0x18 ),
00234     BYTES_TO_T_UINT_8( 0x19, 0x2D, 0xF8, 0xE3, 0x6B, 0x05, 0x8E, 0x98 ),
00235     BYTES_TO_T_UINT_8( 0xE4, 0xE7, 0x3E, 0xE2, 0xA7, 0x2F, 0x31, 0xB3 ),
00236 };
00237 static const t_uint secp384r1_gx[] = {
00238     BYTES_TO_T_UINT_8( 0xB7, 0x0A, 0x76, 0x72, 0x38, 0x5E, 0x54, 0x3A ),
00239     BYTES_TO_T_UINT_8( 0x6C, 0x29, 0x55, 0xBF, 0x5D, 0xF2, 0x02, 0x55 ),
00240     BYTES_TO_T_UINT_8( 0x38, 0x2A, 0x54, 0x82, 0xE0, 0x41, 0xF7, 0x59 ),
00241     BYTES_TO_T_UINT_8( 0x98, 0x9B, 0xA7, 0x8B, 0x62, 0x3B, 0x1D, 0x6E ),
00242     BYTES_TO_T_UINT_8( 0x74, 0xAD, 0x20, 0xF3, 0x1E, 0xC7, 0xB1, 0x8E ),
00243     BYTES_TO_T_UINT_8( 0x37, 0x05, 0x8B, 0xBE, 0x22, 0xCA, 0x87, 0xAA ),
00244 };
00245 static const t_uint secp384r1_gy[] = {
00246     BYTES_TO_T_UINT_8( 0x5F, 0x0E, 0xEA, 0x90, 0x7C, 0x1D, 0x43, 0x7A ),
00247     BYTES_TO_T_UINT_8( 0x9D, 0x81, 0x7E, 0x1D, 0xCE, 0xB1, 0x60, 0x0A ),
00248     BYTES_TO_T_UINT_8( 0xC0, 0xB8, 0xF0, 0xB5, 0x13, 0x31, 0xDA, 0xE9 ),
00249     BYTES_TO_T_UINT_8( 0x7C, 0x14, 0x9A, 0x28, 0xBD, 0x1D, 0xF4, 0xF8 ),
00250     BYTES_TO_T_UINT_8( 0x29, 0xDC, 0x92, 0x92, 0xBF, 0x98, 0x9E, 0x5D ),
00251     BYTES_TO_T_UINT_8( 0x6F, 0x2C, 0x26, 0x96, 0x4A, 0xDE, 0x17, 0x36 ),
00252 };
00253 static const t_uint secp384r1_n[] = {
00254     BYTES_TO_T_UINT_8( 0x73, 0x29, 0xC5, 0xCC, 0x6A, 0x19, 0xEC, 0xEC ),
00255     BYTES_TO_T_UINT_8( 0x7A, 0xA7, 0xB0, 0x48, 0xB2, 0x0D, 0x1A, 0x58 ),
00256     BYTES_TO_T_UINT_8( 0xDF, 0x2D, 0x37, 0xF4, 0x81, 0x4D, 0x63, 0xC7 ),
00257     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00258     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00259     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00260 };
00261 #endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
00262 
00263 /*
00264  * Domain parameters for secp521r1
00265  */
00266 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
00267 static const t_uint secp521r1_p[] = {
00268     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00269     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00270     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00271     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00272     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00273     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00274     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00275     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00276     BYTES_TO_T_UINT_2( 0xFF, 0x01 ),
00277 };
00278 static const t_uint secp521r1_b[] = {
00279     BYTES_TO_T_UINT_8( 0x00, 0x3F, 0x50, 0x6B, 0xD4, 0x1F, 0x45, 0xEF ),
00280     BYTES_TO_T_UINT_8( 0xF1, 0x34, 0x2C, 0x3D, 0x88, 0xDF, 0x73, 0x35 ),
00281     BYTES_TO_T_UINT_8( 0x07, 0xBF, 0xB1, 0x3B, 0xBD, 0xC0, 0x52, 0x16 ),
00282     BYTES_TO_T_UINT_8( 0x7B, 0x93, 0x7E, 0xEC, 0x51, 0x39, 0x19, 0x56 ),
00283     BYTES_TO_T_UINT_8( 0xE1, 0x09, 0xF1, 0x8E, 0x91, 0x89, 0xB4, 0xB8 ),
00284     BYTES_TO_T_UINT_8( 0xF3, 0x15, 0xB3, 0x99, 0x5B, 0x72, 0xDA, 0xA2 ),
00285     BYTES_TO_T_UINT_8( 0xEE, 0x40, 0x85, 0xB6, 0xA0, 0x21, 0x9A, 0x92 ),
00286     BYTES_TO_T_UINT_8( 0x1F, 0x9A, 0x1C, 0x8E, 0x61, 0xB9, 0x3E, 0x95 ),
00287     BYTES_TO_T_UINT_2( 0x51, 0x00 ),
00288 };
00289 static const t_uint secp521r1_gx[] = {
00290     BYTES_TO_T_UINT_8( 0x66, 0xBD, 0xE5, 0xC2, 0x31, 0x7E, 0x7E, 0xF9 ),
00291     BYTES_TO_T_UINT_8( 0x9B, 0x42, 0x6A, 0x85, 0xC1, 0xB3, 0x48, 0x33 ),
00292     BYTES_TO_T_UINT_8( 0xDE, 0xA8, 0xFF, 0xA2, 0x27, 0xC1, 0x1D, 0xFE ),
00293     BYTES_TO_T_UINT_8( 0x28, 0x59, 0xE7, 0xEF, 0x77, 0x5E, 0x4B, 0xA1 ),
00294     BYTES_TO_T_UINT_8( 0xBA, 0x3D, 0x4D, 0x6B, 0x60, 0xAF, 0x28, 0xF8 ),
00295     BYTES_TO_T_UINT_8( 0x21, 0xB5, 0x3F, 0x05, 0x39, 0x81, 0x64, 0x9C ),
00296     BYTES_TO_T_UINT_8( 0x42, 0xB4, 0x95, 0x23, 0x66, 0xCB, 0x3E, 0x9E ),
00297     BYTES_TO_T_UINT_8( 0xCD, 0xE9, 0x04, 0x04, 0xB7, 0x06, 0x8E, 0x85 ),
00298     BYTES_TO_T_UINT_2( 0xC6, 0x00 ),
00299 };
00300 static const t_uint secp521r1_gy[] = {
00301     BYTES_TO_T_UINT_8( 0x50, 0x66, 0xD1, 0x9F, 0x76, 0x94, 0xBE, 0x88 ),
00302     BYTES_TO_T_UINT_8( 0x40, 0xC2, 0x72, 0xA2, 0x86, 0x70, 0x3C, 0x35 ),
00303     BYTES_TO_T_UINT_8( 0x61, 0x07, 0xAD, 0x3F, 0x01, 0xB9, 0x50, 0xC5 ),
00304     BYTES_TO_T_UINT_8( 0x40, 0x26, 0xF4, 0x5E, 0x99, 0x72, 0xEE, 0x97 ),
00305     BYTES_TO_T_UINT_8( 0x2C, 0x66, 0x3E, 0x27, 0x17, 0xBD, 0xAF, 0x17 ),
00306     BYTES_TO_T_UINT_8( 0x68, 0x44, 0x9B, 0x57, 0x49, 0x44, 0xF5, 0x98 ),
00307     BYTES_TO_T_UINT_8( 0xD9, 0x1B, 0x7D, 0x2C, 0xB4, 0x5F, 0x8A, 0x5C ),
00308     BYTES_TO_T_UINT_8( 0x04, 0xC0, 0x3B, 0x9A, 0x78, 0x6A, 0x29, 0x39 ),
00309     BYTES_TO_T_UINT_2( 0x18, 0x01 ),
00310 };
00311 static const t_uint secp521r1_n[] = {
00312     BYTES_TO_T_UINT_8( 0x09, 0x64, 0x38, 0x91, 0x1E, 0xB7, 0x6F, 0xBB ),
00313     BYTES_TO_T_UINT_8( 0xAE, 0x47, 0x9C, 0x89, 0xB8, 0xC9, 0xB5, 0x3B ),
00314     BYTES_TO_T_UINT_8( 0xD0, 0xA5, 0x09, 0xF7, 0x48, 0x01, 0xCC, 0x7F ),
00315     BYTES_TO_T_UINT_8( 0x6B, 0x96, 0x2F, 0xBF, 0x83, 0x87, 0x86, 0x51 ),
00316     BYTES_TO_T_UINT_8( 0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00317     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00318     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00319     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00320     BYTES_TO_T_UINT_2( 0xFF, 0x01 ),
00321 };
00322 #endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
00323 
00324 #if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
00325 static const t_uint secp192k1_p[] = {
00326     BYTES_TO_T_UINT_8( 0x37, 0xEE, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ),
00327     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00328     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00329 };
00330 static const t_uint secp192k1_a[] = {
00331     BYTES_TO_T_UINT_2( 0x00, 0x00 ),
00332 };
00333 static const t_uint secp192k1_b[] = {
00334     BYTES_TO_T_UINT_2( 0x03, 0x00 ),
00335 };
00336 static const t_uint secp192k1_gx[] = {
00337     BYTES_TO_T_UINT_8( 0x7D, 0x6C, 0xE0, 0xEA, 0xB1, 0xD1, 0xA5, 0x1D ),
00338     BYTES_TO_T_UINT_8( 0x34, 0xF4, 0xB7, 0x80, 0x02, 0x7D, 0xB0, 0x26 ),
00339     BYTES_TO_T_UINT_8( 0xAE, 0xE9, 0x57, 0xC0, 0x0E, 0xF1, 0x4F, 0xDB ),
00340 };
00341 static const t_uint secp192k1_gy[] = {
00342     BYTES_TO_T_UINT_8( 0x9D, 0x2F, 0x5E, 0xD9, 0x88, 0xAA, 0x82, 0x40 ),
00343     BYTES_TO_T_UINT_8( 0x34, 0x86, 0xBE, 0x15, 0xD0, 0x63, 0x41, 0x84 ),
00344     BYTES_TO_T_UINT_8( 0xA7, 0x28, 0x56, 0x9C, 0x6D, 0x2F, 0x2F, 0x9B ),
00345 };
00346 static const t_uint secp192k1_n[] = {
00347     BYTES_TO_T_UINT_8( 0x8D, 0xFD, 0xDE, 0x74, 0x6A, 0x46, 0x69, 0x0F ),
00348     BYTES_TO_T_UINT_8( 0x17, 0xFC, 0xF2, 0x26, 0xFE, 0xFF, 0xFF, 0xFF ),
00349     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00350 };
00351 #endif /* POLARSSL_ECP_DP_SECP192K1_ENABLED */
00352 
00353 #if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
00354 static const t_uint secp224k1_p[] = {
00355     BYTES_TO_T_UINT_8( 0x6D, 0xE5, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ),
00356     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00357     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00358     BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ),
00359 };
00360 static const t_uint secp224k1_a[] = {
00361     BYTES_TO_T_UINT_2( 0x00, 0x00 ),
00362 };
00363 static const t_uint secp224k1_b[] = {
00364     BYTES_TO_T_UINT_2( 0x05, 0x00 ),
00365 };
00366 static const t_uint secp224k1_gx[] = {
00367     BYTES_TO_T_UINT_8( 0x5C, 0xA4, 0xB7, 0xB6, 0x0E, 0x65, 0x7E, 0x0F ),
00368     BYTES_TO_T_UINT_8( 0xA9, 0x75, 0x70, 0xE4, 0xE9, 0x67, 0xA4, 0x69 ),
00369     BYTES_TO_T_UINT_8( 0xA1, 0x28, 0xFC, 0x30, 0xDF, 0x99, 0xF0, 0x4D ),
00370     BYTES_TO_T_UINT_4( 0x33, 0x5B, 0x45, 0xA1 ),
00371 };
00372 static const t_uint secp224k1_gy[] = {
00373     BYTES_TO_T_UINT_8( 0xA5, 0x61, 0x6D, 0x55, 0xDB, 0x4B, 0xCA, 0xE2 ),
00374     BYTES_TO_T_UINT_8( 0x59, 0xBD, 0xB0, 0xC0, 0xF7, 0x19, 0xE3, 0xF7 ),
00375     BYTES_TO_T_UINT_8( 0xD6, 0xFB, 0xCA, 0x82, 0x42, 0x34, 0xBA, 0x7F ),
00376     BYTES_TO_T_UINT_4( 0xED, 0x9F, 0x08, 0x7E ),
00377 };
00378 static const t_uint secp224k1_n[] = {
00379     BYTES_TO_T_UINT_8( 0xF7, 0xB1, 0x9F, 0x76, 0x71, 0xA9, 0xF0, 0xCA ),
00380     BYTES_TO_T_UINT_8( 0x84, 0x61, 0xEC, 0xD2, 0xE8, 0xDC, 0x01, 0x00 ),
00381     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
00382     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ),
00383 };
00384 #endif /* POLARSSL_ECP_DP_SECP224K1_ENABLED */
00385 
00386 #if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
00387 static const t_uint secp256k1_p[] = {
00388     BYTES_TO_T_UINT_8( 0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ),
00389     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00390     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00391     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00392 };
00393 static const t_uint secp256k1_a[] = {
00394     BYTES_TO_T_UINT_2( 0x00, 0x00 ),
00395 };
00396 static const t_uint secp256k1_b[] = {
00397     BYTES_TO_T_UINT_2( 0x07, 0x00 ),
00398 };
00399 static const t_uint secp256k1_gx[] = {
00400     BYTES_TO_T_UINT_8( 0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59 ),
00401     BYTES_TO_T_UINT_8( 0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02 ),
00402     BYTES_TO_T_UINT_8( 0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55 ),
00403     BYTES_TO_T_UINT_8( 0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79 ),
00404 };
00405 static const t_uint secp256k1_gy[] = {
00406     BYTES_TO_T_UINT_8( 0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C ),
00407     BYTES_TO_T_UINT_8( 0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD ),
00408     BYTES_TO_T_UINT_8( 0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D ),
00409     BYTES_TO_T_UINT_8( 0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48 ),
00410 };
00411 static const t_uint secp256k1_n[] = {
00412     BYTES_TO_T_UINT_8( 0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF ),
00413     BYTES_TO_T_UINT_8( 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA ),
00414     BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00415     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
00416 };
00417 #endif /* POLARSSL_ECP_DP_SECP256K1_ENABLED */
00418 
00419 /*
00420  * Domain parameters for brainpoolP256r1 (RFC 5639 3.4)
00421  */
00422 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
00423 static const t_uint brainpoolP256r1_p[] = {
00424     BYTES_TO_T_UINT_8( 0x77, 0x53, 0x6E, 0x1F, 0x1D, 0x48, 0x13, 0x20 ),
00425     BYTES_TO_T_UINT_8( 0x28, 0x20, 0x26, 0xD5, 0x23, 0xF6, 0x3B, 0x6E ),
00426     BYTES_TO_T_UINT_8( 0x72, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ),
00427     BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ),
00428 };
00429 static const t_uint brainpoolP256r1_a[] = {
00430     BYTES_TO_T_UINT_8( 0xD9, 0xB5, 0x30, 0xF3, 0x44, 0x4B, 0x4A, 0xE9 ),
00431     BYTES_TO_T_UINT_8( 0x6C, 0x5C, 0xDC, 0x26, 0xC1, 0x55, 0x80, 0xFB ),
00432     BYTES_TO_T_UINT_8( 0xE7, 0xFF, 0x7A, 0x41, 0x30, 0x75, 0xF6, 0xEE ),
00433     BYTES_TO_T_UINT_8( 0x57, 0x30, 0x2C, 0xFC, 0x75, 0x09, 0x5A, 0x7D ),
00434 };
00435 static const t_uint brainpoolP256r1_b[] = {
00436     BYTES_TO_T_UINT_8( 0xB6, 0x07, 0x8C, 0xFF, 0x18, 0xDC, 0xCC, 0x6B ),
00437     BYTES_TO_T_UINT_8( 0xCE, 0xE1, 0xF7, 0x5C, 0x29, 0x16, 0x84, 0x95 ),
00438     BYTES_TO_T_UINT_8( 0xBF, 0x7C, 0xD7, 0xBB, 0xD9, 0xB5, 0x30, 0xF3 ),
00439     BYTES_TO_T_UINT_8( 0x44, 0x4B, 0x4A, 0xE9, 0x6C, 0x5C, 0xDC, 0x26 ),
00440 };
00441 static const t_uint brainpoolP256r1_gx[] = {
00442     BYTES_TO_T_UINT_8( 0x62, 0x32, 0xCE, 0x9A, 0xBD, 0x53, 0x44, 0x3A ),
00443     BYTES_TO_T_UINT_8( 0xC2, 0x23, 0xBD, 0xE3, 0xE1, 0x27, 0xDE, 0xB9 ),
00444     BYTES_TO_T_UINT_8( 0xAF, 0xB7, 0x81, 0xFC, 0x2F, 0x48, 0x4B, 0x2C ),
00445     BYTES_TO_T_UINT_8( 0xCB, 0x57, 0x7E, 0xCB, 0xB9, 0xAE, 0xD2, 0x8B ),
00446 };
00447 static const t_uint brainpoolP256r1_gy[] = {
00448     BYTES_TO_T_UINT_8( 0x97, 0x69, 0x04, 0x2F, 0xC7, 0x54, 0x1D, 0x5C ),
00449     BYTES_TO_T_UINT_8( 0x54, 0x8E, 0xED, 0x2D, 0x13, 0x45, 0x77, 0xC2 ),
00450     BYTES_TO_T_UINT_8( 0xC9, 0x1D, 0x61, 0x14, 0x1A, 0x46, 0xF8, 0x97 ),
00451     BYTES_TO_T_UINT_8( 0xFD, 0xC4, 0xDA, 0xC3, 0x35, 0xF8, 0x7E, 0x54 ),
00452 };
00453 static const t_uint brainpoolP256r1_n[] = {
00454     BYTES_TO_T_UINT_8( 0xA7, 0x56, 0x48, 0x97, 0x82, 0x0E, 0x1E, 0x90 ),
00455     BYTES_TO_T_UINT_8( 0xF7, 0xA6, 0x61, 0xB5, 0xA3, 0x7A, 0x39, 0x8C ),
00456     BYTES_TO_T_UINT_8( 0x71, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ),
00457     BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ),
00458 };
00459 #endif /* POLARSSL_ECP_DP_BP256R1_ENABLED */
00460 
00461 /*
00462  * Domain parameters for brainpoolP384r1 (RFC 5639 3.6)
00463  */
00464 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
00465 static const t_uint brainpoolP384r1_p[] = {
00466     BYTES_TO_T_UINT_8( 0x53, 0xEC, 0x07, 0x31, 0x13, 0x00, 0x47, 0x87 ),
00467     BYTES_TO_T_UINT_8( 0x71, 0x1A, 0x1D, 0x90, 0x29, 0xA7, 0xD3, 0xAC ),
00468     BYTES_TO_T_UINT_8( 0x23, 0x11, 0xB7, 0x7F, 0x19, 0xDA, 0xB1, 0x12 ),
00469     BYTES_TO_T_UINT_8( 0xB4, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15 ),
00470     BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ),
00471     BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ),
00472 };
00473 static const t_uint brainpoolP384r1_a[] = {
00474     BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ),
00475     BYTES_TO_T_UINT_8( 0xEB, 0xD4, 0x3A, 0x50, 0x4A, 0x81, 0xA5, 0x8A ),
00476     BYTES_TO_T_UINT_8( 0x0F, 0xF9, 0x91, 0xBA, 0xEF, 0x65, 0x91, 0x13 ),
00477     BYTES_TO_T_UINT_8( 0x87, 0x27, 0xB2, 0x4F, 0x8E, 0xA2, 0xBE, 0xC2 ),
00478     BYTES_TO_T_UINT_8( 0xA0, 0xAF, 0x05, 0xCE, 0x0A, 0x08, 0x72, 0x3C ),
00479     BYTES_TO_T_UINT_8( 0x0C, 0x15, 0x8C, 0x3D, 0xC6, 0x82, 0xC3, 0x7B ),
00480 };
00481 static const t_uint brainpoolP384r1_b[] = {
00482     BYTES_TO_T_UINT_8( 0x11, 0x4C, 0x50, 0xFA, 0x96, 0x86, 0xB7, 0x3A ),
00483     BYTES_TO_T_UINT_8( 0x94, 0xC9, 0xDB, 0x95, 0x02, 0x39, 0xB4, 0x7C ),
00484     BYTES_TO_T_UINT_8( 0xD5, 0x62, 0xEB, 0x3E, 0xA5, 0x0E, 0x88, 0x2E ),
00485     BYTES_TO_T_UINT_8( 0xA6, 0xD2, 0xDC, 0x07, 0xE1, 0x7D, 0xB7, 0x2F ),
00486     BYTES_TO_T_UINT_8( 0x7C, 0x44, 0xF0, 0x16, 0x54, 0xB5, 0x39, 0x8B ),
00487     BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ),
00488 };
00489 static const t_uint brainpoolP384r1_gx[] = {
00490     BYTES_TO_T_UINT_8( 0x1E, 0xAF, 0xD4, 0x47, 0xE2, 0xB2, 0x87, 0xEF ),
00491     BYTES_TO_T_UINT_8( 0xAA, 0x46, 0xD6, 0x36, 0x34, 0xE0, 0x26, 0xE8 ),
00492     BYTES_TO_T_UINT_8( 0xE8, 0x10, 0xBD, 0x0C, 0xFE, 0xCA, 0x7F, 0xDB ),
00493     BYTES_TO_T_UINT_8( 0xE3, 0x4F, 0xF1, 0x7E, 0xE7, 0xA3, 0x47, 0x88 ),
00494     BYTES_TO_T_UINT_8( 0x6B, 0x3F, 0xC1, 0xB7, 0x81, 0x3A, 0xA6, 0xA2 ),
00495     BYTES_TO_T_UINT_8( 0xFF, 0x45, 0xCF, 0x68, 0xF0, 0x64, 0x1C, 0x1D ),
00496 };
00497 static const t_uint brainpoolP384r1_gy[] = {
00498     BYTES_TO_T_UINT_8( 0x15, 0x53, 0x3C, 0x26, 0x41, 0x03, 0x82, 0x42 ),
00499     BYTES_TO_T_UINT_8( 0x11, 0x81, 0x91, 0x77, 0x21, 0x46, 0x46, 0x0E ),
00500     BYTES_TO_T_UINT_8( 0x28, 0x29, 0x91, 0xF9, 0x4F, 0x05, 0x9C, 0xE1 ),
00501     BYTES_TO_T_UINT_8( 0x64, 0x58, 0xEC, 0xFE, 0x29, 0x0B, 0xB7, 0x62 ),
00502     BYTES_TO_T_UINT_8( 0x52, 0xD5, 0xCF, 0x95, 0x8E, 0xEB, 0xB1, 0x5C ),
00503     BYTES_TO_T_UINT_8( 0xA4, 0xC2, 0xF9, 0x20, 0x75, 0x1D, 0xBE, 0x8A ),
00504 };
00505 static const t_uint brainpoolP384r1_n[] = {
00506     BYTES_TO_T_UINT_8( 0x65, 0x65, 0x04, 0xE9, 0x02, 0x32, 0x88, 0x3B ),
00507     BYTES_TO_T_UINT_8( 0x10, 0xC3, 0x7F, 0x6B, 0xAF, 0xB6, 0x3A, 0xCF ),
00508     BYTES_TO_T_UINT_8( 0xA7, 0x25, 0x04, 0xAC, 0x6C, 0x6E, 0x16, 0x1F ),
00509     BYTES_TO_T_UINT_8( 0xB3, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15 ),
00510     BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ),
00511     BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ),
00512 };
00513 #endif /* POLARSSL_ECP_DP_BP384R1_ENABLED */
00514 
00515 /*
00516  * Domain parameters for brainpoolP512r1 (RFC 5639 3.7)
00517  */
00518 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
00519 static const t_uint brainpoolP512r1_p[] = {
00520     BYTES_TO_T_UINT_8( 0xF3, 0x48, 0x3A, 0x58, 0x56, 0x60, 0xAA, 0x28 ),
00521     BYTES_TO_T_UINT_8( 0x85, 0xC6, 0x82, 0x2D, 0x2F, 0xFF, 0x81, 0x28 ),
00522     BYTES_TO_T_UINT_8( 0xE6, 0x80, 0xA3, 0xE6, 0x2A, 0xA1, 0xCD, 0xAE ),
00523     BYTES_TO_T_UINT_8( 0x42, 0x68, 0xC6, 0x9B, 0x00, 0x9B, 0x4D, 0x7D ),
00524     BYTES_TO_T_UINT_8( 0x71, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6 ),
00525     BYTES_TO_T_UINT_8( 0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB ),
00526     BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ),
00527     BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ),
00528 };
00529 static const t_uint brainpoolP512r1_a[] = {
00530     BYTES_TO_T_UINT_8( 0xCA, 0x94, 0xFC, 0x77, 0x4D, 0xAC, 0xC1, 0xE7 ),
00531     BYTES_TO_T_UINT_8( 0xB9, 0xC7, 0xF2, 0x2B, 0xA7, 0x17, 0x11, 0x7F ),
00532     BYTES_TO_T_UINT_8( 0xB5, 0xC8, 0x9A, 0x8B, 0xC9, 0xF1, 0x2E, 0x0A ),
00533     BYTES_TO_T_UINT_8( 0xA1, 0x3A, 0x25, 0xA8, 0x5A, 0x5D, 0xED, 0x2D ),
00534     BYTES_TO_T_UINT_8( 0xBC, 0x63, 0x98, 0xEA, 0xCA, 0x41, 0x34, 0xA8 ),
00535     BYTES_TO_T_UINT_8( 0x10, 0x16, 0xF9, 0x3D, 0x8D, 0xDD, 0xCB, 0x94 ),
00536     BYTES_TO_T_UINT_8( 0xC5, 0x4C, 0x23, 0xAC, 0x45, 0x71, 0x32, 0xE2 ),
00537     BYTES_TO_T_UINT_8( 0x89, 0x3B, 0x60, 0x8B, 0x31, 0xA3, 0x30, 0x78 ),
00538 };
00539 static const t_uint brainpoolP512r1_b[] = {
00540     BYTES_TO_T_UINT_8( 0x23, 0xF7, 0x16, 0x80, 0x63, 0xBD, 0x09, 0x28 ),
00541     BYTES_TO_T_UINT_8( 0xDD, 0xE5, 0xBA, 0x5E, 0xB7, 0x50, 0x40, 0x98 ),
00542     BYTES_TO_T_UINT_8( 0x67, 0x3E, 0x08, 0xDC, 0xCA, 0x94, 0xFC, 0x77 ),
00543     BYTES_TO_T_UINT_8( 0x4D, 0xAC, 0xC1, 0xE7, 0xB9, 0xC7, 0xF2, 0x2B ),
00544     BYTES_TO_T_UINT_8( 0xA7, 0x17, 0x11, 0x7F, 0xB5, 0xC8, 0x9A, 0x8B ),
00545     BYTES_TO_T_UINT_8( 0xC9, 0xF1, 0x2E, 0x0A, 0xA1, 0x3A, 0x25, 0xA8 ),
00546     BYTES_TO_T_UINT_8( 0x5A, 0x5D, 0xED, 0x2D, 0xBC, 0x63, 0x98, 0xEA ),
00547     BYTES_TO_T_UINT_8( 0xCA, 0x41, 0x34, 0xA8, 0x10, 0x16, 0xF9, 0x3D ),
00548 };
00549 static const t_uint brainpoolP512r1_gx[] = {
00550     BYTES_TO_T_UINT_8( 0x22, 0xF8, 0xB9, 0xBC, 0x09, 0x22, 0x35, 0x8B ),
00551     BYTES_TO_T_UINT_8( 0x68, 0x5E, 0x6A, 0x40, 0x47, 0x50, 0x6D, 0x7C ),
00552     BYTES_TO_T_UINT_8( 0x5F, 0x7D, 0xB9, 0x93, 0x7B, 0x68, 0xD1, 0x50 ),
00553     BYTES_TO_T_UINT_8( 0x8D, 0xD4, 0xD0, 0xE2, 0x78, 0x1F, 0x3B, 0xFF ),
00554     BYTES_TO_T_UINT_8( 0x8E, 0x09, 0xD0, 0xF4, 0xEE, 0x62, 0x3B, 0xB4 ),
00555     BYTES_TO_T_UINT_8( 0xC1, 0x16, 0xD9, 0xB5, 0x70, 0x9F, 0xED, 0x85 ),
00556     BYTES_TO_T_UINT_8( 0x93, 0x6A, 0x4C, 0x9C, 0x2E, 0x32, 0x21, 0x5A ),
00557     BYTES_TO_T_UINT_8( 0x64, 0xD9, 0x2E, 0xD8, 0xBD, 0xE4, 0xAE, 0x81 ),
00558 };
00559 static const t_uint brainpoolP512r1_gy[] = {
00560     BYTES_TO_T_UINT_8( 0x92, 0x08, 0xD8, 0x3A, 0x0F, 0x1E, 0xCD, 0x78 ),
00561     BYTES_TO_T_UINT_8( 0x06, 0x54, 0xF0, 0xA8, 0x2F, 0x2B, 0xCA, 0xD1 ),
00562     BYTES_TO_T_UINT_8( 0xAE, 0x63, 0x27, 0x8A, 0xD8, 0x4B, 0xCA, 0x5B ),
00563     BYTES_TO_T_UINT_8( 0x5E, 0x48, 0x5F, 0x4A, 0x49, 0xDE, 0xDC, 0xB2 ),
00564     BYTES_TO_T_UINT_8( 0x11, 0x81, 0x1F, 0x88, 0x5B, 0xC5, 0x00, 0xA0 ),
00565     BYTES_TO_T_UINT_8( 0x1A, 0x7B, 0xA5, 0x24, 0x00, 0xF7, 0x09, 0xF2 ),
00566     BYTES_TO_T_UINT_8( 0xFD, 0x22, 0x78, 0xCF, 0xA9, 0xBF, 0xEA, 0xC0 ),
00567     BYTES_TO_T_UINT_8( 0xEC, 0x32, 0x63, 0x56, 0x5D, 0x38, 0xDE, 0x7D ),
00568 };
00569 static const t_uint brainpoolP512r1_n[] = {
00570     BYTES_TO_T_UINT_8( 0x69, 0x00, 0xA9, 0x9C, 0x82, 0x96, 0x87, 0xB5 ),
00571     BYTES_TO_T_UINT_8( 0xDD, 0xDA, 0x5D, 0x08, 0x81, 0xD3, 0xB1, 0x1D ),
00572     BYTES_TO_T_UINT_8( 0x47, 0x10, 0xAC, 0x7F, 0x19, 0x61, 0x86, 0x41 ),
00573     BYTES_TO_T_UINT_8( 0x19, 0x26, 0xA9, 0x4C, 0x41, 0x5C, 0x3E, 0x55 ),
00574     BYTES_TO_T_UINT_8( 0x70, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6 ),
00575     BYTES_TO_T_UINT_8( 0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB ),
00576     BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ),
00577     BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ),
00578 };
00579 #endif /* POLARSSL_ECP_DP_BP512R1_ENABLED */
00580 
00581 /*
00582  * Create an MPI from embedded constants
00583  * (assumes len is an exact multiple of sizeof t_uint)
00584  */
00585 static inline void ecp_mpi_load( mpi *X, const t_uint *p, size_t len )
00586 {
00587     X->s  = 1;
00588     X->n  = len / sizeof( t_uint );
00589     X->p  = (t_uint *) p;
00590 }
00591 
00592 /*
00593  * Set an MPI to static value 1
00594  */
00595 static inline void ecp_mpi_set1( mpi *X )
00596 {
00597     static t_uint one[] = { 1 };
00598     X->s  = 1;
00599     X->n  = 1;
00600     X->p  = one;
00601 }
00602 
00603 /*
00604  * Make group available from embedded constants
00605  */
00606 static int ecp_group_load( ecp_group *grp,
00607                            const t_uint *p,  size_t plen,
00608                            const t_uint *a,  size_t alen,
00609                            const t_uint *b,  size_t blen,
00610                            const t_uint *gx, size_t gxlen,
00611                            const t_uint *gy, size_t gylen,
00612                            const t_uint *n,  size_t nlen)
00613 {
00614     ecp_mpi_load( &grp->P , p, plen );
00615     if( a != NULL )
00616         ecp_mpi_load( &grp->A , a, alen );
00617     ecp_mpi_load( &grp->B , b, blen );
00618     ecp_mpi_load( &grp->N , n, nlen );
00619 
00620     ecp_mpi_load( &grp->G .X , gx, gxlen );
00621     ecp_mpi_load( &grp->G .Y , gy, gylen );
00622     ecp_mpi_set1( &grp->G .Z  );
00623 
00624     grp->pbits  = mpi_msb( &grp->P  );
00625     grp->nbits  = mpi_msb( &grp->N  );
00626 
00627     grp->h  = 1;
00628 
00629     return( 0 );
00630 }
00631 
00632 #if defined(POLARSSL_ECP_NIST_OPTIM)
00633 /* Forward declarations */
00634 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
00635 static int ecp_mod_p192( mpi * );
00636 #endif
00637 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
00638 static int ecp_mod_p224( mpi * );
00639 #endif
00640 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
00641 static int ecp_mod_p256( mpi * );
00642 #endif
00643 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
00644 static int ecp_mod_p384( mpi * );
00645 #endif
00646 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
00647 static int ecp_mod_p521( mpi * );
00648 #endif
00649 
00650 #define NIST_MODP( P )      grp->modp = ecp_mod_ ## P;
00651 #else
00652 #define NIST_MODP( P )
00653 #endif /* POLARSSL_ECP_NIST_OPTIM */
00654 
00655 /* Additional forward declarations */
00656 #if defined(POLARSSL_ECP_DP_M255_ENABLED)
00657 static int ecp_mod_p255( mpi * );
00658 #endif
00659 #if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
00660 static int ecp_mod_p192k1( mpi * );
00661 #endif
00662 #if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
00663 static int ecp_mod_p224k1( mpi * );
00664 #endif
00665 #if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
00666 static int ecp_mod_p256k1( mpi * );
00667 #endif
00668 
00669 #define LOAD_GROUP_A( G )   ecp_group_load( grp,            \
00670                             G ## _p,  sizeof( G ## _p  ),   \
00671                             G ## _a,  sizeof( G ## _a  ),   \
00672                             G ## _b,  sizeof( G ## _b  ),   \
00673                             G ## _gx, sizeof( G ## _gx ),   \
00674                             G ## _gy, sizeof( G ## _gy ),   \
00675                             G ## _n,  sizeof( G ## _n  ) )
00676 
00677 #define LOAD_GROUP( G )     ecp_group_load( grp,            \
00678                             G ## _p,  sizeof( G ## _p  ),   \
00679                             NULL,     0,                    \
00680                             G ## _b,  sizeof( G ## _b  ),   \
00681                             G ## _gx, sizeof( G ## _gx ),   \
00682                             G ## _gy, sizeof( G ## _gy ),   \
00683                             G ## _n,  sizeof( G ## _n  ) )
00684 
00685 #if defined(POLARSSL_ECP_DP_M255_ENABLED)
00686 /*
00687  * Specialized function for creating the Curve25519 group
00688  */
00689 static int ecp_use_curve25519( ecp_group *grp )
00690 {
00691     int ret;
00692 
00693     /* Actually ( A + 2 ) / 4 */
00694     MPI_CHK( mpi_read_string( &grp->A , 16, "01DB42" ) );
00695 
00696     /* P = 2^255 - 19 */
00697     MPI_CHK( mpi_lset( &grp->P , 1 ) );
00698     MPI_CHK( mpi_shift_l( &grp->P , 255 ) );
00699     MPI_CHK( mpi_sub_int( &grp->P , &grp->P , 19 ) );
00700     grp->pbits  = mpi_msb( &grp->P  );
00701 
00702     /* Y intentionaly not set, since we use x/z coordinates.
00703      * This is used as a marker to identify Montgomery curves! */
00704     MPI_CHK( mpi_lset( &grp->G .X , 9 ) );
00705     MPI_CHK( mpi_lset( &grp->G .Z , 1 ) );
00706     mpi_free( &grp->G .Y  );
00707 
00708     /* Actually, the required msb for private keys */
00709     grp->nbits  = 254;
00710 
00711 cleanup:
00712     if( ret != 0 )
00713         ecp_group_free( grp );
00714 
00715     return( ret );
00716 }
00717 #endif /* POLARSSL_ECP_DP_M255_ENABLED */
00718 
00719 /*
00720  * Set a group using well-known domain parameters
00721  */
00722 int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
00723 {
00724     ecp_group_free( grp );
00725 
00726     grp->id  = id;
00727 
00728     switch( id )
00729     {
00730 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
00731         case POLARSSL_ECP_DP_SECP192R1 :
00732             NIST_MODP( p192 );
00733             return( LOAD_GROUP( secp192r1 ) );
00734 #endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
00735 
00736 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
00737         case POLARSSL_ECP_DP_SECP224R1 :
00738             NIST_MODP( p224 );
00739             return( LOAD_GROUP( secp224r1 ) );
00740 #endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
00741 
00742 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
00743         case POLARSSL_ECP_DP_SECP256R1 :
00744             NIST_MODP( p256 );
00745             return( LOAD_GROUP( secp256r1 ) );
00746 #endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
00747 
00748 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
00749         case POLARSSL_ECP_DP_SECP384R1 :
00750             NIST_MODP( p384 );
00751             return( LOAD_GROUP( secp384r1 ) );
00752 #endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
00753 
00754 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
00755         case POLARSSL_ECP_DP_SECP521R1 :
00756             NIST_MODP( p521 );
00757             return( LOAD_GROUP( secp521r1 ) );
00758 #endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
00759 
00760 #if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
00761         case POLARSSL_ECP_DP_SECP192K1 :
00762             grp->modp  = ecp_mod_p192k1;
00763             return( LOAD_GROUP_A( secp192k1 ) );
00764 #endif /* POLARSSL_ECP_DP_SECP192K1_ENABLED */
00765 
00766 #if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
00767         case POLARSSL_ECP_DP_SECP224K1 :
00768             grp->modp  = ecp_mod_p224k1;
00769             return( LOAD_GROUP_A( secp224k1 ) );
00770 #endif /* POLARSSL_ECP_DP_SECP224K1_ENABLED */
00771 
00772 #if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
00773         case POLARSSL_ECP_DP_SECP256K1 :
00774             grp->modp  = ecp_mod_p256k1;
00775             return( LOAD_GROUP_A( secp256k1 ) );
00776 #endif /* POLARSSL_ECP_DP_SECP256K1_ENABLED */
00777 
00778 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
00779         case POLARSSL_ECP_DP_BP256R1 :
00780             return( LOAD_GROUP_A( brainpoolP256r1 ) );
00781 #endif /* POLARSSL_ECP_DP_BP256R1_ENABLED */
00782 
00783 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
00784         case POLARSSL_ECP_DP_BP384R1 :
00785             return( LOAD_GROUP_A( brainpoolP384r1 ) );
00786 #endif /* POLARSSL_ECP_DP_BP384R1_ENABLED */
00787 
00788 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
00789         case POLARSSL_ECP_DP_BP512R1 :
00790             return( LOAD_GROUP_A( brainpoolP512r1 ) );
00791 #endif /* POLARSSL_ECP_DP_BP512R1_ENABLED */
00792 
00793 #if defined(POLARSSL_ECP_DP_M255_ENABLED)
00794         case POLARSSL_ECP_DP_M255 :
00795             grp->modp  = ecp_mod_p255;
00796             return( ecp_use_curve25519( grp ) );
00797 #endif /* POLARSSL_ECP_DP_M255_ENABLED */
00798 
00799         default:
00800             ecp_group_free( grp );
00801             return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
00802     }
00803 }
00804 
00805 #if defined(POLARSSL_ECP_NIST_OPTIM)
00806 /*
00807  * Fast reduction modulo the primes used by the NIST curves.
00808  *
00809  * These functions are critical for speed, but not needed for correct
00810  * operations. So, we make the choice to heavily rely on the internals of our
00811  * bignum library, which creates a tight coupling between these functions and
00812  * our MPI implementation.  However, the coupling between the ECP module and
00813  * MPI remains loose, since these functions can be deactivated at will.
00814  */
00815 
00816 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
00817 /*
00818  * Compared to the way things are presented in FIPS 186-3 D.2,
00819  * we proceed in columns, from right (least significant chunk) to left,
00820  * adding chunks to N in place, and keeping a carry for the next chunk.
00821  * This avoids moving things around in memory, and uselessly adding zeros,
00822  * compared to the more straightforward, line-oriented approach.
00823  *
00824  * For this prime we need to handle data in chunks of 64 bits.
00825  * Since this is always a multiple of our basic t_uint, we can
00826  * use a t_uint * to designate such a chunk, and small loops to handle it.
00827  */
00828 
00829 /* Add 64-bit chunks (dst += src) and update carry */
00830 static inline void add64( t_uint *dst, t_uint *src, t_uint *carry )
00831 {
00832     unsigned char i;
00833     t_uint c = 0;
00834     for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ )
00835     {
00836         *dst += c;      c  = ( *dst < c );
00837         *dst += *src;   c += ( *dst < *src );
00838     }
00839     *carry += c;
00840 }
00841 
00842 /* Add carry to a 64-bit chunk and update carry */
00843 static inline void carry64( t_uint *dst, t_uint *carry )
00844 {
00845     unsigned char i;
00846     for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ )
00847     {
00848         *dst += *carry;
00849         *carry  = ( *dst < *carry );
00850     }
00851 }
00852 
00853 #define WIDTH       8 / sizeof( t_uint )
00854 #define A( i )      N->p + i * WIDTH
00855 #define ADD( i )    add64( p, A( i ), &c )
00856 #define NEXT        p += WIDTH; carry64( p, &c )
00857 #define LAST        p += WIDTH; *p = c; while( ++p < end ) *p = 0
00858 
00859 /*
00860  * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
00861  */
00862 static int ecp_mod_p192( mpi *N )
00863 {
00864     int ret;
00865     t_uint c = 0;
00866     t_uint *p, *end;
00867 
00868     /* Make sure we have enough blocks so that A(5) is legal */
00869     MPI_CHK( mpi_grow( N, 6 * WIDTH ) );
00870 
00871     p = N->p ;
00872     end = p + N->n ;
00873 
00874     ADD( 3 ); ADD( 5 );             NEXT; // A0 += A3 + A5
00875     ADD( 3 ); ADD( 4 ); ADD( 5 );   NEXT; // A1 += A3 + A4 + A5
00876     ADD( 4 ); ADD( 5 );             LAST; // A2 += A4 + A5
00877 
00878 cleanup:
00879     return( ret );
00880 }
00881 
00882 #undef WIDTH
00883 #undef A
00884 #undef ADD
00885 #undef NEXT
00886 #undef LAST
00887 #endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
00888 
00889 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) ||   \
00890     defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) ||   \
00891     defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
00892 /*
00893  * The reader is advised to first understand ecp_mod_p192() since the same
00894  * general structure is used here, but with additional complications:
00895  * (1) chunks of 32 bits, and (2) subtractions.
00896  */
00897 
00898 /*
00899  * For these primes, we need to handle data in chunks of 32 bits.
00900  * This makes it more complicated if we use 64 bits limbs in MPI,
00901  * which prevents us from using a uniform access method as for p192.
00902  *
00903  * So, we define a mini abstraction layer to access 32 bit chunks,
00904  * load them in 'cur' for work, and store them back from 'cur' when done.
00905  *
00906  * While at it, also define the size of N in terms of 32-bit chunks.
00907  */
00908 #define LOAD32      cur = A( i );
00909 
00910 #if defined(POLARSSL_HAVE_INT8)     /* 8 bit */
00911 
00912 #define MAX32       N->n / 4
00913 #define A( j )      (uint32_t)( N->p[4*j+0]       ) |  \
00914                               ( N->p[4*j+1] << 8  ) |  \
00915                               ( N->p[4*j+2] << 16 ) |  \
00916                               ( N->p[4*j+3] << 24 )
00917 #define STORE32     N->p[4*i+0] = (t_uint)( cur       );   \
00918                     N->p[4*i+1] = (t_uint)( cur >> 8  );   \
00919                     N->p[4*i+2] = (t_uint)( cur >> 16 );   \
00920                     N->p[4*i+3] = (t_uint)( cur >> 24 );
00921 
00922 #elif defined(POLARSSL_HAVE_INT16)  /* 16 bit */
00923 
00924 #define MAX32       N->n / 2
00925 #define A( j )      (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 )
00926 #define STORE32     N->p[2*i+0] = (t_uint)( cur       );  \
00927                     N->p[2*i+1] = (t_uint)( cur >> 16 );
00928 
00929 #elif defined(POLARSSL_HAVE_INT32)  /* 32 bit */
00930 
00931 #define MAX32       N->n
00932 #define A( j )      N->p[j]
00933 #define STORE32     N->p[i] = cur;
00934 
00935 #else                               /* 64-bit */
00936 
00937 #define MAX32       N->n * 2
00938 #define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] )
00939 #define STORE32                                   \
00940     if( i % 2 ) {                                 \
00941         N->p[i/2] &= 0x00000000FFFFFFFF;          \
00942         N->p[i/2] |= ((t_uint) cur) << 32;        \
00943     } else {                                      \
00944         N->p[i/2] &= 0xFFFFFFFF00000000;          \
00945         N->p[i/2] |= (t_uint) cur;                \
00946     }
00947 
00948 #endif /* sizeof( t_uint ) */
00949 
00950 /*
00951  * Helpers for addition and subtraction of chunks, with signed carry.
00952  */
00953 static inline void add32( uint32_t *dst, uint32_t src, signed char *carry )
00954 {
00955     *dst += src;
00956     *carry += ( *dst < src );
00957 }
00958 
00959 static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
00960 {
00961     *carry -= ( *dst < src );
00962     *dst -= src;
00963 }
00964 
00965 #define ADD( j )    add32( &cur, A( j ), &c );
00966 #define SUB( j )    sub32( &cur, A( j ), &c );
00967 
00968 /*
00969  * Helpers for the main 'loop'
00970  * (see fix_negative for the motivation of C)
00971  */
00972 #define INIT( b )                                           \
00973     int ret;                                                \
00974     signed char c = 0, cc;                                  \
00975     uint32_t cur;                                           \
00976     size_t i = 0, bits = b;                                 \
00977     mpi C;                                                  \
00978     t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ];               \
00979                                                             \
00980     C.s = 1;                                                \
00981     C.n = b / 8 / sizeof( t_uint) + 1;                      \
00982     C.p = Cp;                                               \
00983     memset( Cp, 0, C.n * sizeof( t_uint ) );                \
00984                                                             \
00985     MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
00986     LOAD32;
00987 
00988 #define NEXT                    \
00989     STORE32; i++; LOAD32;       \
00990     cc = c; c = 0;              \
00991     if( cc < 0 )                \
00992         sub32( &cur, -cc, &c ); \
00993     else                        \
00994         add32( &cur, cc, &c );  \
00995 
00996 #define LAST                                    \
00997     STORE32; i++;                               \
00998     cur = c > 0 ? c : 0; STORE32;               \
00999     cur = 0; while( ++i < MAX32 ) { STORE32; }  \
01000     if( c < 0 ) fix_negative( N, c, &C, bits );
01001 
01002 /*
01003  * If the result is negative, we get it in the form
01004  * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
01005  */
01006 static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits )
01007 {
01008     int ret;
01009 
01010     /* C = - c * 2^(bits + 32) */
01011 #if !defined(POLARSSL_HAVE_INT64)
01012     ((void) bits);
01013 #else
01014     if( bits == 224 )
01015         C->p [ C->n  - 1 ] = ((t_uint) -c) << 32;
01016     else
01017 #endif
01018         C->p [ C->n  - 1 ] = (t_uint) -c;
01019 
01020     /* N = - ( C - N ) */
01021     MPI_CHK( mpi_sub_abs( N, C, N ) );
01022     N->s  = -1;
01023 
01024 cleanup:
01025 
01026     return( ret );
01027 }
01028 
01029 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
01030 /*
01031  * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
01032  */
01033 static int ecp_mod_p224( mpi *N )
01034 {
01035     INIT( 224 );
01036 
01037     SUB(  7 ); SUB( 11 );               NEXT; // A0 += -A7 - A11
01038     SUB(  8 ); SUB( 12 );               NEXT; // A1 += -A8 - A12
01039     SUB(  9 ); SUB( 13 );               NEXT; // A2 += -A9 - A13
01040     SUB( 10 ); ADD(  7 ); ADD( 11 );    NEXT; // A3 += -A10 + A7 + A11
01041     SUB( 11 ); ADD(  8 ); ADD( 12 );    NEXT; // A4 += -A11 + A8 + A12
01042     SUB( 12 ); ADD(  9 ); ADD( 13 );    NEXT; // A5 += -A12 + A9 + A13
01043     SUB( 13 ); ADD( 10 );               LAST; // A6 += -A13 + A10
01044 
01045 cleanup:
01046     return( ret );
01047 }
01048 #endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
01049 
01050 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
01051 /*
01052  * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
01053  */
01054 static int ecp_mod_p256( mpi *N )
01055 {
01056     INIT( 256 );
01057 
01058     ADD(  8 ); ADD(  9 );
01059     SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 );             NEXT; // A0
01060 
01061     ADD(  9 ); ADD( 10 );
01062     SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 );             NEXT; // A1
01063 
01064     ADD( 10 ); ADD( 11 );
01065     SUB( 13 ); SUB( 14 ); SUB( 15 );                        NEXT; // A2
01066 
01067     ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 );
01068     SUB( 15 ); SUB(  8 ); SUB(  9 );                        NEXT; // A3
01069 
01070     ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 );
01071     SUB(  9 ); SUB( 10 );                                   NEXT; // A4
01072 
01073     ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 );
01074     SUB( 10 ); SUB( 11 );                                   NEXT; // A5
01075 
01076     ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 );
01077     SUB(  8 ); SUB(  9 );                                   NEXT; // A6
01078 
01079     ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 );
01080     SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 );             LAST; // A7
01081 
01082 cleanup:
01083     return( ret );
01084 }
01085 #endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
01086 
01087 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
01088 /*
01089  * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
01090  */
01091 static int ecp_mod_p384( mpi *N )
01092 {
01093     INIT( 384 );
01094 
01095     ADD( 12 ); ADD( 21 ); ADD( 20 );
01096     SUB( 23 );                                              NEXT; // A0
01097 
01098     ADD( 13 ); ADD( 22 ); ADD( 23 );
01099     SUB( 12 ); SUB( 20 );                                   NEXT; // A2
01100 
01101     ADD( 14 ); ADD( 23 );
01102     SUB( 13 ); SUB( 21 );                                   NEXT; // A2
01103 
01104     ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 );
01105     SUB( 14 ); SUB( 22 ); SUB( 23 );                        NEXT; // A3
01106 
01107     ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 );
01108     SUB( 15 ); SUB( 23 ); SUB( 23 );                        NEXT; // A4
01109 
01110     ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 );
01111     SUB( 16 );                                              NEXT; // A5
01112 
01113     ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 );
01114     SUB( 17 );                                              NEXT; // A6
01115 
01116     ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 );
01117     SUB( 18 );                                              NEXT; // A7
01118 
01119     ADD( 20 ); ADD( 17 ); ADD( 16 );
01120     SUB( 19 );                                              NEXT; // A8
01121 
01122     ADD( 21 ); ADD( 18 ); ADD( 17 );
01123     SUB( 20 );                                              NEXT; // A9
01124 
01125     ADD( 22 ); ADD( 19 ); ADD( 18 );
01126     SUB( 21 );                                              NEXT; // A10
01127 
01128     ADD( 23 ); ADD( 20 ); ADD( 19 );
01129     SUB( 22 );                                              LAST; // A11
01130 
01131 cleanup:
01132     return( ret );
01133 }
01134 #endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
01135 
01136 #undef A
01137 #undef LOAD32
01138 #undef STORE32
01139 #undef MAX32
01140 #undef INIT
01141 #undef NEXT
01142 #undef LAST
01143 
01144 #endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED ||
01145           POLARSSL_ECP_DP_SECP256R1_ENABLED ||
01146           POLARSSL_ECP_DP_SECP384R1_ENABLED */
01147 
01148 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
01149 /*
01150  * Here we have an actual Mersenne prime, so things are more straightforward.
01151  * However, chunks are aligned on a 'weird' boundary (521 bits).
01152  */
01153 
01154 /* Size of p521 in terms of t_uint */
01155 #define P521_WIDTH      ( 521 / 8 / sizeof( t_uint ) + 1 )
01156 
01157 /* Bits to keep in the most significant t_uint */
01158 #if defined(POLARSSL_HAVE_INT8)
01159 #define P521_MASK       0x01
01160 #else
01161 #define P521_MASK       0x01FF
01162 #endif
01163 
01164 /*
01165  * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
01166  * Write N as A1 + 2^521 A0, return A0 + A1
01167  */
01168 static int ecp_mod_p521( mpi *N )
01169 {
01170     int ret;
01171     size_t i;
01172     mpi M;
01173     t_uint Mp[P521_WIDTH + 1];
01174     /* Worst case for the size of M is when t_uint is 16 bits:
01175      * we need to hold bits 513 to 1056, which is 34 limbs, that is
01176      * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
01177 
01178     if( N->n  < P521_WIDTH )
01179         return( 0 );
01180 
01181     /* M = A1 */
01182     M.s  = 1;
01183     M.n  = N->n  - ( P521_WIDTH - 1 );
01184     if( M.n  > P521_WIDTH + 1 )
01185         M.n  = P521_WIDTH + 1;
01186     M.p  = Mp;
01187     memcpy( Mp, N->p  + P521_WIDTH - 1, M.n  * sizeof( t_uint ) );
01188     MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) );
01189 
01190     /* N = A0 */
01191     N->p [P521_WIDTH - 1] &= P521_MASK;
01192     for( i = P521_WIDTH; i < N->n ; i++ )
01193         N->p [i] = 0;
01194 
01195     /* N = A0 + A1 */
01196     MPI_CHK( mpi_add_abs( N, N, &M ) );
01197 
01198 cleanup:
01199     return( ret );
01200 }
01201 
01202 #undef P521_WIDTH
01203 #undef P521_MASK
01204 #endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
01205 
01206 #endif /* POLARSSL_ECP_NIST_OPTIM */
01207 
01208 #if defined(POLARSSL_ECP_DP_M255_ENABLED)
01209 
01210 /* Size of p255 in terms of t_uint */
01211 #define P255_WIDTH      ( 255 / 8 / sizeof( t_uint ) + 1 )
01212 
01213 /*
01214  * Fast quasi-reduction modulo p255 = 2^255 - 19
01215  * Write N as A0 + 2^255 A1, return A0 + 19 * A1
01216  */
01217 static int ecp_mod_p255( mpi *N )
01218 {
01219     int ret;
01220     size_t i;
01221     mpi M;
01222     t_uint Mp[P255_WIDTH + 2];
01223 
01224     if( N->n  < P255_WIDTH )
01225         return( 0 );
01226 
01227     /* M = A1 */
01228     M.s  = 1;
01229     M.n  = N->n  - ( P255_WIDTH - 1 );
01230     if( M.n  > P255_WIDTH + 1 )
01231         M.n  = P255_WIDTH + 1;
01232     M.p  = Mp;
01233     memset( Mp, 0, sizeof Mp );
01234     memcpy( Mp, N->p  + P255_WIDTH - 1, M.n  * sizeof( t_uint ) );
01235     MPI_CHK( mpi_shift_r( &M, 255 % ( 8 * sizeof( t_uint ) ) ) );
01236     M.n ++; /* Make room for multiplication by 19 */
01237 
01238     /* N = A0 */
01239     MPI_CHK( mpi_set_bit( N, 255, 0 ) );
01240     for( i = P255_WIDTH; i < N->n ; i++ )
01241         N->p [i] = 0;
01242 
01243     /* N = A0 + 19 * A1 */
01244     MPI_CHK( mpi_mul_int( &M, &M, 19 ) );
01245     MPI_CHK( mpi_add_abs( N, N, &M ) );
01246 
01247 cleanup:
01248     return( ret );
01249 }
01250 #endif /* POLARSSL_ECP_DP_M255_ENABLED */
01251 
01252 #if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) ||   \
01253     defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) ||   \
01254     defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
01255 /*
01256  * Fast quasi-reduction modulo P = 2^s - R,
01257  * with R about 33 bits, used by the Koblitz curves.
01258  *
01259  * Write N as A0 + 2^224 A1, return A0 + R * A1.
01260  * Actually do two passes, since R is big.
01261  */
01262 #define P_KOBLITZ_MAX   ( 256 / 8 / sizeof( t_uint ) )  // Max limbs in P
01263 #define P_KOBLITZ_R     ( 8 / sizeof( t_uint ) )        // Limbs in R
01264 static inline int ecp_mod_koblitz( mpi *N, t_uint *Rp, size_t p_limbs,
01265                                    size_t adjust, size_t shift, t_uint mask )
01266 {
01267     int ret;
01268     size_t i;
01269     mpi M, R;
01270     t_uint Mp[P_KOBLITZ_MAX + P_KOBLITZ_R];
01271 
01272     if( N->n  < p_limbs )
01273         return( 0 );
01274 
01275     /* Init R */
01276     R.s  = 1;
01277     R.p  = Rp;
01278     R.n  = P_KOBLITZ_R;
01279 
01280     /* Common setup for M */
01281     M.s  = 1;
01282     M.p  = Mp;
01283 
01284     /* M = A1 */
01285     M.n  = N->n  - ( p_limbs - adjust );
01286     if( M.n  > p_limbs + adjust )
01287         M.n  = p_limbs + adjust;
01288     memset( Mp, 0, sizeof Mp );
01289     memcpy( Mp, N->p  + p_limbs - adjust, M.n  * sizeof( t_uint ) );
01290     if( shift != 0 )
01291         MPI_CHK( mpi_shift_r( &M, shift ) );
01292     M.n  += R.n  - adjust; /* Make room for multiplication by R */
01293 
01294     /* N = A0 */
01295     if( mask != 0 )
01296         N->p [p_limbs - 1] &= mask;
01297     for( i = p_limbs; i < N->n ; i++ )
01298         N->p [i] = 0;
01299 
01300     /* N = A0 + R * A1 */
01301     MPI_CHK( mpi_mul_mpi( &M, &M, &R ) );
01302     MPI_CHK( mpi_add_abs( N, N, &M ) );
01303 
01304     /* Second pass */
01305 
01306     /* M = A1 */
01307     M.n  = N->n  - ( p_limbs - adjust );
01308     if( M.n  > p_limbs + adjust )
01309         M.n  = p_limbs + adjust;
01310     memset( Mp, 0, sizeof Mp );
01311     memcpy( Mp, N->p  + p_limbs - adjust, M.n  * sizeof( t_uint ) );
01312     if( shift != 0 )
01313         MPI_CHK( mpi_shift_r( &M, shift ) );
01314     M.n  += R.n  - adjust; /* Make room for multiplication by R */
01315 
01316     /* N = A0 */
01317     if( mask != 0 )
01318         N->p [p_limbs - 1] &= mask;
01319     for( i = p_limbs; i < N->n ; i++ )
01320         N->p [i] = 0;
01321 
01322     /* N = A0 + R * A1 */
01323     MPI_CHK( mpi_mul_mpi( &M, &M, &R ) );
01324     MPI_CHK( mpi_add_abs( N, N, &M ) );
01325 
01326 cleanup:
01327     return( ret );
01328 }
01329 #endif /* POLARSSL_ECP_DP_SECP192K1_ENABLED) ||
01330           POLARSSL_ECP_DP_SECP224K1_ENABLED) ||
01331           POLARSSL_ECP_DP_SECP256K1_ENABLED) */
01332 
01333 #if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
01334 /*
01335  * Fast quasi-reduction modulo p192k1 = 2^192 - R,
01336  * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119
01337  */
01338 static int ecp_mod_p192k1( mpi *N )
01339 {
01340     static t_uint Rp[] = {
01341         BYTES_TO_T_UINT_8( 0xC9, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) };
01342 
01343     return( ecp_mod_koblitz( N, Rp, 192 / 8 / sizeof( t_uint ), 0, 0, 0 ) );
01344 }
01345 #endif /* POLARSSL_ECP_DP_SECP192K1_ENABLED */
01346 
01347 #if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
01348 /*
01349  * Fast quasi-reduction modulo p224k1 = 2^224 - R,
01350  * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
01351  */
01352 static int ecp_mod_p224k1( mpi *N )
01353 {
01354     static t_uint Rp[] = {
01355         BYTES_TO_T_UINT_8( 0x93, 0x1A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) };
01356 
01357 #if defined(POLARSSL_HAVE_INT64)
01358     return( ecp_mod_koblitz( N, Rp, 4, 1, 32, 0xFFFFFFFF ) );
01359 #else
01360     return( ecp_mod_koblitz( N, Rp, 224 / 8 / sizeof( t_uint ), 0, 0, 0 ) );
01361 #endif
01362 }
01363 
01364 #endif /* POLARSSL_ECP_DP_SECP224K1_ENABLED */
01365 
01366 #if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
01367 /*
01368  * Fast quasi-reduction modulo p256k1 = 2^256 - R,
01369  * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
01370  */
01371 static int ecp_mod_p256k1( mpi *N )
01372 {
01373     static t_uint Rp[] = {
01374         BYTES_TO_T_UINT_8( 0xD1, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) };
01375     return( ecp_mod_koblitz( N, Rp, 256 / 8 / sizeof( t_uint ), 0, 0, 0 ) );
01376 }
01377 #endif /* POLARSSL_ECP_DP_SECP256K1_ENABLED */
01378 
01379 #endif /* POLARSSL_ECP_C */
01380