ecp_curves.c 51 KB

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