ecp_curves.c 57 KB

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