bn_nist.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /*
  2. * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "bn_local.h"
  10. #include "internal/cryptlib.h"
  11. #define BN_NIST_192_TOP (192+BN_BITS2-1)/BN_BITS2
  12. #define BN_NIST_224_TOP (224+BN_BITS2-1)/BN_BITS2
  13. #define BN_NIST_256_TOP (256+BN_BITS2-1)/BN_BITS2
  14. #define BN_NIST_384_TOP (384+BN_BITS2-1)/BN_BITS2
  15. #define BN_NIST_521_TOP (521+BN_BITS2-1)/BN_BITS2
  16. /* pre-computed tables are "carry-less" values of modulus*(i+1) */
  17. #if BN_BITS2 == 64
  18. static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
  19. {0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFEULL, 0xFFFFFFFFFFFFFFFFULL},
  20. {0xFFFFFFFFFFFFFFFEULL, 0xFFFFFFFFFFFFFFFDULL, 0xFFFFFFFFFFFFFFFFULL},
  21. {0xFFFFFFFFFFFFFFFDULL, 0xFFFFFFFFFFFFFFFCULL, 0xFFFFFFFFFFFFFFFFULL}
  22. };
  23. static const BN_ULONG _nist_p_192_sqr[] = {
  24. 0x0000000000000001ULL, 0x0000000000000002ULL, 0x0000000000000001ULL,
  25. 0xFFFFFFFFFFFFFFFEULL, 0xFFFFFFFFFFFFFFFDULL, 0xFFFFFFFFFFFFFFFFULL
  26. };
  27. static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
  28. {0x0000000000000001ULL, 0xFFFFFFFF00000000ULL,
  29. 0xFFFFFFFFFFFFFFFFULL, 0x00000000FFFFFFFFULL},
  30. {0x0000000000000002ULL, 0xFFFFFFFE00000000ULL,
  31. 0xFFFFFFFFFFFFFFFFULL, 0x00000001FFFFFFFFULL} /* this one is
  32. * "carry-full" */
  33. };
  34. static const BN_ULONG _nist_p_224_sqr[] = {
  35. 0x0000000000000001ULL, 0xFFFFFFFE00000000ULL,
  36. 0xFFFFFFFFFFFFFFFFULL, 0x0000000200000000ULL,
  37. 0x0000000000000000ULL, 0xFFFFFFFFFFFFFFFEULL,
  38. 0xFFFFFFFFFFFFFFFFULL
  39. };
  40. static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
  41. {0xFFFFFFFFFFFFFFFFULL, 0x00000000FFFFFFFFULL,
  42. 0x0000000000000000ULL, 0xFFFFFFFF00000001ULL},
  43. {0xFFFFFFFFFFFFFFFEULL, 0x00000001FFFFFFFFULL,
  44. 0x0000000000000000ULL, 0xFFFFFFFE00000002ULL},
  45. {0xFFFFFFFFFFFFFFFDULL, 0x00000002FFFFFFFFULL,
  46. 0x0000000000000000ULL, 0xFFFFFFFD00000003ULL},
  47. {0xFFFFFFFFFFFFFFFCULL, 0x00000003FFFFFFFFULL,
  48. 0x0000000000000000ULL, 0xFFFFFFFC00000004ULL},
  49. {0xFFFFFFFFFFFFFFFBULL, 0x00000004FFFFFFFFULL,
  50. 0x0000000000000000ULL, 0xFFFFFFFB00000005ULL},
  51. };
  52. static const BN_ULONG _nist_p_256_sqr[] = {
  53. 0x0000000000000001ULL, 0xFFFFFFFE00000000ULL,
  54. 0xFFFFFFFFFFFFFFFFULL, 0x00000001FFFFFFFEULL,
  55. 0x00000001FFFFFFFEULL, 0x00000001FFFFFFFEULL,
  56. 0xFFFFFFFE00000001ULL, 0xFFFFFFFE00000002ULL
  57. };
  58. static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
  59. {0x00000000FFFFFFFFULL, 0xFFFFFFFF00000000ULL, 0xFFFFFFFFFFFFFFFEULL,
  60. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  61. {0x00000001FFFFFFFEULL, 0xFFFFFFFE00000000ULL, 0xFFFFFFFFFFFFFFFDULL,
  62. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  63. {0x00000002FFFFFFFDULL, 0xFFFFFFFD00000000ULL, 0xFFFFFFFFFFFFFFFCULL,
  64. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  65. {0x00000003FFFFFFFCULL, 0xFFFFFFFC00000000ULL, 0xFFFFFFFFFFFFFFFBULL,
  66. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  67. {0x00000004FFFFFFFBULL, 0xFFFFFFFB00000000ULL, 0xFFFFFFFFFFFFFFFAULL,
  68. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  69. };
  70. static const BN_ULONG _nist_p_384_sqr[] = {
  71. 0xFFFFFFFE00000001ULL, 0x0000000200000000ULL, 0xFFFFFFFE00000000ULL,
  72. 0x0000000200000000ULL, 0x0000000000000001ULL, 0x0000000000000000ULL,
  73. 0x00000001FFFFFFFEULL, 0xFFFFFFFE00000000ULL, 0xFFFFFFFFFFFFFFFDULL,
  74. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL
  75. };
  76. static const BN_ULONG _nist_p_521[] =
  77. { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  78. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  79. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  80. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  81. 0x00000000000001FFULL
  82. };
  83. static const BN_ULONG _nist_p_521_sqr[] = {
  84. 0x0000000000000001ULL, 0x0000000000000000ULL, 0x0000000000000000ULL,
  85. 0x0000000000000000ULL, 0x0000000000000000ULL, 0x0000000000000000ULL,
  86. 0x0000000000000000ULL, 0x0000000000000000ULL, 0xFFFFFFFFFFFFFC00ULL,
  87. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  88. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  89. 0xFFFFFFFFFFFFFFFFULL, 0x000000000003FFFFULL
  90. };
  91. #elif BN_BITS2 == 32
  92. static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
  93. {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  94. {0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  95. {0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFC, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  96. };
  97. static const BN_ULONG _nist_p_192_sqr[] = {
  98. 0x00000001, 0x00000000, 0x00000002, 0x00000000, 0x00000001, 0x00000000,
  99. 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  100. };
  101. static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
  102. {0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFF,
  103. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  104. {0x00000002, 0x00000000, 0x00000000, 0xFFFFFFFE,
  105. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  106. };
  107. static const BN_ULONG _nist_p_224_sqr[] = {
  108. 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE,
  109. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000002,
  110. 0x00000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFF,
  111. 0xFFFFFFFF, 0xFFFFFFFF
  112. };
  113. static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
  114. {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000,
  115. 0x00000000, 0x00000000, 0x00000001, 0xFFFFFFFF},
  116. {0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000001,
  117. 0x00000000, 0x00000000, 0x00000002, 0xFFFFFFFE},
  118. {0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000002,
  119. 0x00000000, 0x00000000, 0x00000003, 0xFFFFFFFD},
  120. {0xFFFFFFFC, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000003,
  121. 0x00000000, 0x00000000, 0x00000004, 0xFFFFFFFC},
  122. {0xFFFFFFFB, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000004,
  123. 0x00000000, 0x00000000, 0x00000005, 0xFFFFFFFB},
  124. };
  125. static const BN_ULONG _nist_p_256_sqr[] = {
  126. 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE,
  127. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0x00000001,
  128. 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001,
  129. 0x00000001, 0xFFFFFFFE, 0x00000002, 0xFFFFFFFE
  130. };
  131. static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
  132. {0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF,
  133. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  134. {0xFFFFFFFE, 0x00000001, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFF,
  135. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  136. {0xFFFFFFFD, 0x00000002, 0x00000000, 0xFFFFFFFD, 0xFFFFFFFC, 0xFFFFFFFF,
  137. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  138. {0xFFFFFFFC, 0x00000003, 0x00000000, 0xFFFFFFFC, 0xFFFFFFFB, 0xFFFFFFFF,
  139. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  140. {0xFFFFFFFB, 0x00000004, 0x00000000, 0xFFFFFFFB, 0xFFFFFFFA, 0xFFFFFFFF,
  141. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  142. };
  143. static const BN_ULONG _nist_p_384_sqr[] = {
  144. 0x00000001, 0xFFFFFFFE, 0x00000000, 0x00000002, 0x00000000, 0xFFFFFFFE,
  145. 0x00000000, 0x00000002, 0x00000001, 0x00000000, 0x00000000, 0x00000000,
  146. 0xFFFFFFFE, 0x00000001, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFF,
  147. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  148. };
  149. static const BN_ULONG _nist_p_521[] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  150. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  151. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  152. 0xFFFFFFFF, 0x000001FF
  153. };
  154. static const BN_ULONG _nist_p_521_sqr[] = {
  155. 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  156. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  157. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xFFFFFC00, 0xFFFFFFFF,
  158. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  159. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  160. 0xFFFFFFFF, 0xFFFFFFFF, 0x0003FFFF
  161. };
  162. #else
  163. # error "unsupported BN_BITS2"
  164. #endif
  165. static const BIGNUM ossl_bignum_nist_p_192 = {
  166. (BN_ULONG *)_nist_p_192[0],
  167. BN_NIST_192_TOP,
  168. BN_NIST_192_TOP,
  169. 0,
  170. BN_FLG_STATIC_DATA
  171. };
  172. static const BIGNUM ossl_bignum_nist_p_224 = {
  173. (BN_ULONG *)_nist_p_224[0],
  174. BN_NIST_224_TOP,
  175. BN_NIST_224_TOP,
  176. 0,
  177. BN_FLG_STATIC_DATA
  178. };
  179. static const BIGNUM ossl_bignum_nist_p_256 = {
  180. (BN_ULONG *)_nist_p_256[0],
  181. BN_NIST_256_TOP,
  182. BN_NIST_256_TOP,
  183. 0,
  184. BN_FLG_STATIC_DATA
  185. };
  186. static const BIGNUM ossl_bignum_nist_p_384 = {
  187. (BN_ULONG *)_nist_p_384[0],
  188. BN_NIST_384_TOP,
  189. BN_NIST_384_TOP,
  190. 0,
  191. BN_FLG_STATIC_DATA
  192. };
  193. static const BIGNUM ossl_bignum_nist_p_521 = {
  194. (BN_ULONG *)_nist_p_521,
  195. BN_NIST_521_TOP,
  196. BN_NIST_521_TOP,
  197. 0,
  198. BN_FLG_STATIC_DATA
  199. };
  200. const BIGNUM *BN_get0_nist_prime_192(void)
  201. {
  202. return &ossl_bignum_nist_p_192;
  203. }
  204. const BIGNUM *BN_get0_nist_prime_224(void)
  205. {
  206. return &ossl_bignum_nist_p_224;
  207. }
  208. const BIGNUM *BN_get0_nist_prime_256(void)
  209. {
  210. return &ossl_bignum_nist_p_256;
  211. }
  212. const BIGNUM *BN_get0_nist_prime_384(void)
  213. {
  214. return &ossl_bignum_nist_p_384;
  215. }
  216. const BIGNUM *BN_get0_nist_prime_521(void)
  217. {
  218. return &ossl_bignum_nist_p_521;
  219. }
  220. /*
  221. * To avoid more recent compilers (specifically clang-14) from treating this
  222. * code as a violation of the strict aliasing conditions and omiting it, this
  223. * cannot be declared as a function. Moreover, the dst parameter cannot be
  224. * cached in a local since this no longer references the union and again falls
  225. * foul of the strict aliasing criteria. Refer to #18225 for the initial
  226. * diagnostics and llvm/llvm-project#55255 for the later discussions with the
  227. * LLVM developers. The problem boils down to if an array in the union is
  228. * converted to a pointer or if it is used directly.
  229. *
  230. * This function was inlined regardless, so there is no space cost to be
  231. * paid for making it a macro.
  232. */
  233. #define nist_cp_bn_0(dst, src_in, top, max) \
  234. { \
  235. int ii; \
  236. const BN_ULONG *src = src_in; \
  237. \
  238. for (ii = 0; ii < top; ii++) \
  239. (dst)[ii] = src[ii]; \
  240. for (; ii < max; ii++) \
  241. (dst)[ii] = 0; \
  242. }
  243. static void nist_cp_bn(BN_ULONG *dst, const BN_ULONG *src, int top)
  244. {
  245. int i;
  246. for (i = 0; i < top; i++)
  247. dst[i] = src[i];
  248. }
  249. #if BN_BITS2 == 64
  250. # define bn_cp_64(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
  251. # define bn_64_set_0(to, n) (to)[n] = (BN_ULONG)0;
  252. /*
  253. * two following macros are implemented under assumption that they
  254. * are called in a sequence with *ascending* n, i.e. as they are...
  255. */
  256. # define bn_cp_32_naked(to, n, from, m) (((n)&1)?(to[(n)/2]|=((m)&1)?(from[(m)/2]&BN_MASK2h):(from[(m)/2]<<32))\
  257. :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l)))
  258. # define bn_32_set_0(to, n) (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0));
  259. # define bn_cp_32(to,n,from,m) ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n)
  260. # if defined(L_ENDIAN)
  261. # if defined(__arch64__)
  262. # define NIST_INT64 long
  263. # else
  264. # define NIST_INT64 long long
  265. # endif
  266. # endif
  267. #else
  268. # define bn_cp_64(to, n, from, m) \
  269. { \
  270. bn_cp_32(to, (n)*2, from, (m)*2); \
  271. bn_cp_32(to, (n)*2+1, from, (m)*2+1); \
  272. }
  273. # define bn_64_set_0(to, n) \
  274. { \
  275. bn_32_set_0(to, (n)*2); \
  276. bn_32_set_0(to, (n)*2+1); \
  277. }
  278. # define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
  279. # define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0;
  280. # if defined(_WIN32) && !defined(__GNUC__)
  281. # define NIST_INT64 __int64
  282. # elif defined(BN_LLONG)
  283. # define NIST_INT64 long long
  284. # endif
  285. #endif /* BN_BITS2 != 64 */
  286. #define nist_set_192(to, from, a1, a2, a3) \
  287. { \
  288. bn_cp_64(to, 0, from, (a3) - 3) \
  289. bn_cp_64(to, 1, from, (a2) - 3) \
  290. bn_cp_64(to, 2, from, (a1) - 3) \
  291. }
  292. int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  293. BN_CTX *ctx)
  294. {
  295. int top = a->top, i;
  296. int carry;
  297. register BN_ULONG *r_d, *a_d = a->d;
  298. union {
  299. BN_ULONG bn[BN_NIST_192_TOP];
  300. unsigned int ui[BN_NIST_192_TOP * sizeof(BN_ULONG) /
  301. sizeof(unsigned int)];
  302. } buf;
  303. BN_ULONG c_d[BN_NIST_192_TOP], *res;
  304. PTR_SIZE_INT mask;
  305. static const BIGNUM ossl_bignum_nist_p_192_sqr = {
  306. (BN_ULONG *)_nist_p_192_sqr,
  307. OSSL_NELEM(_nist_p_192_sqr),
  308. OSSL_NELEM(_nist_p_192_sqr),
  309. 0, BN_FLG_STATIC_DATA
  310. };
  311. field = &ossl_bignum_nist_p_192; /* just to make sure */
  312. if (BN_is_negative(a) || BN_ucmp(a, &ossl_bignum_nist_p_192_sqr) >= 0)
  313. return BN_nnmod(r, a, field, ctx);
  314. i = BN_ucmp(field, a);
  315. if (i == 0) {
  316. BN_zero(r);
  317. return 1;
  318. } else if (i > 0)
  319. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  320. if (r != a) {
  321. if (!bn_wexpand(r, BN_NIST_192_TOP))
  322. return 0;
  323. r_d = r->d;
  324. nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);
  325. } else
  326. r_d = a_d;
  327. nist_cp_bn_0(buf.bn, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP,
  328. BN_NIST_192_TOP);
  329. #if defined(NIST_INT64)
  330. {
  331. NIST_INT64 acc; /* accumulator */
  332. unsigned int *rp = (unsigned int *)r_d;
  333. const unsigned int *bp = (const unsigned int *)buf.ui;
  334. acc = rp[0];
  335. acc += bp[3 * 2 - 6];
  336. acc += bp[5 * 2 - 6];
  337. rp[0] = (unsigned int)acc;
  338. acc >>= 32;
  339. acc += rp[1];
  340. acc += bp[3 * 2 - 5];
  341. acc += bp[5 * 2 - 5];
  342. rp[1] = (unsigned int)acc;
  343. acc >>= 32;
  344. acc += rp[2];
  345. acc += bp[3 * 2 - 6];
  346. acc += bp[4 * 2 - 6];
  347. acc += bp[5 * 2 - 6];
  348. rp[2] = (unsigned int)acc;
  349. acc >>= 32;
  350. acc += rp[3];
  351. acc += bp[3 * 2 - 5];
  352. acc += bp[4 * 2 - 5];
  353. acc += bp[5 * 2 - 5];
  354. rp[3] = (unsigned int)acc;
  355. acc >>= 32;
  356. acc += rp[4];
  357. acc += bp[4 * 2 - 6];
  358. acc += bp[5 * 2 - 6];
  359. rp[4] = (unsigned int)acc;
  360. acc >>= 32;
  361. acc += rp[5];
  362. acc += bp[4 * 2 - 5];
  363. acc += bp[5 * 2 - 5];
  364. rp[5] = (unsigned int)acc;
  365. carry = (int)(acc >> 32);
  366. }
  367. #else
  368. {
  369. BN_ULONG t_d[BN_NIST_192_TOP];
  370. nist_set_192(t_d, buf.bn, 0, 3, 3);
  371. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  372. nist_set_192(t_d, buf.bn, 4, 4, 0);
  373. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  374. nist_set_192(t_d, buf.bn, 5, 5, 5)
  375. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  376. }
  377. #endif
  378. if (carry > 0)
  379. carry =
  380. (int)bn_sub_words(r_d, r_d, _nist_p_192[carry - 1],
  381. BN_NIST_192_TOP);
  382. else
  383. carry = 1;
  384. /*
  385. * we need 'if (carry==0 || result>=modulus) result-=modulus;'
  386. * as comparison implies subtraction, we can write
  387. * 'tmp=result-modulus; if (!carry || !borrow) result=tmp;'
  388. * this is what happens below, but without explicit if:-) a.
  389. */
  390. mask =
  391. 0 - (PTR_SIZE_INT) bn_sub_words(c_d, r_d, _nist_p_192[0],
  392. BN_NIST_192_TOP);
  393. mask &= 0 - (PTR_SIZE_INT) carry;
  394. res = c_d;
  395. res = (BN_ULONG *)
  396. (((PTR_SIZE_INT) res & ~mask) | ((PTR_SIZE_INT) r_d & mask));
  397. nist_cp_bn(r_d, res, BN_NIST_192_TOP);
  398. r->top = BN_NIST_192_TOP;
  399. bn_correct_top(r);
  400. return 1;
  401. }
  402. typedef BN_ULONG (*bn_addsub_f) (BN_ULONG *, const BN_ULONG *,
  403. const BN_ULONG *, int);
  404. #define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \
  405. { \
  406. bn_cp_32(to, 0, from, (a7) - 7) \
  407. bn_cp_32(to, 1, from, (a6) - 7) \
  408. bn_cp_32(to, 2, from, (a5) - 7) \
  409. bn_cp_32(to, 3, from, (a4) - 7) \
  410. bn_cp_32(to, 4, from, (a3) - 7) \
  411. bn_cp_32(to, 5, from, (a2) - 7) \
  412. bn_cp_32(to, 6, from, (a1) - 7) \
  413. }
  414. int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  415. BN_CTX *ctx)
  416. {
  417. int top = a->top, i;
  418. int carry;
  419. BN_ULONG *r_d, *a_d = a->d;
  420. union {
  421. BN_ULONG bn[BN_NIST_224_TOP];
  422. unsigned int ui[BN_NIST_224_TOP * sizeof(BN_ULONG) /
  423. sizeof(unsigned int)];
  424. } buf;
  425. BN_ULONG c_d[BN_NIST_224_TOP], *res;
  426. PTR_SIZE_INT mask;
  427. union {
  428. bn_addsub_f f;
  429. PTR_SIZE_INT p;
  430. } u;
  431. static const BIGNUM ossl_bignum_nist_p_224_sqr = {
  432. (BN_ULONG *)_nist_p_224_sqr,
  433. OSSL_NELEM(_nist_p_224_sqr),
  434. OSSL_NELEM(_nist_p_224_sqr),
  435. 0, BN_FLG_STATIC_DATA
  436. };
  437. field = &ossl_bignum_nist_p_224; /* just to make sure */
  438. if (BN_is_negative(a) || BN_ucmp(a, &ossl_bignum_nist_p_224_sqr) >= 0)
  439. return BN_nnmod(r, a, field, ctx);
  440. i = BN_ucmp(field, a);
  441. if (i == 0) {
  442. BN_zero(r);
  443. return 1;
  444. } else if (i > 0)
  445. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  446. if (r != a) {
  447. if (!bn_wexpand(r, BN_NIST_224_TOP))
  448. return 0;
  449. r_d = r->d;
  450. nist_cp_bn(r_d, a_d, BN_NIST_224_TOP);
  451. } else
  452. r_d = a_d;
  453. #if BN_BITS2==64
  454. /* copy upper 256 bits of 448 bit number ... */
  455. nist_cp_bn_0(c_d, a_d + (BN_NIST_224_TOP - 1),
  456. top - (BN_NIST_224_TOP - 1), BN_NIST_224_TOP);
  457. /* ... and right shift by 32 to obtain upper 224 bits */
  458. nist_set_224(buf.bn, c_d, 14, 13, 12, 11, 10, 9, 8);
  459. /* truncate lower part to 224 bits too */
  460. r_d[BN_NIST_224_TOP - 1] &= BN_MASK2l;
  461. #else
  462. nist_cp_bn_0(buf.bn, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP,
  463. BN_NIST_224_TOP);
  464. #endif
  465. #if defined(NIST_INT64) && BN_BITS2!=64
  466. {
  467. NIST_INT64 acc; /* accumulator */
  468. unsigned int *rp = (unsigned int *)r_d;
  469. const unsigned int *bp = (const unsigned int *)buf.ui;
  470. acc = rp[0];
  471. acc -= bp[7 - 7];
  472. acc -= bp[11 - 7];
  473. rp[0] = (unsigned int)acc;
  474. acc >>= 32;
  475. acc += rp[1];
  476. acc -= bp[8 - 7];
  477. acc -= bp[12 - 7];
  478. rp[1] = (unsigned int)acc;
  479. acc >>= 32;
  480. acc += rp[2];
  481. acc -= bp[9 - 7];
  482. acc -= bp[13 - 7];
  483. rp[2] = (unsigned int)acc;
  484. acc >>= 32;
  485. acc += rp[3];
  486. acc += bp[7 - 7];
  487. acc += bp[11 - 7];
  488. acc -= bp[10 - 7];
  489. rp[3] = (unsigned int)acc;
  490. acc >>= 32;
  491. acc += rp[4];
  492. acc += bp[8 - 7];
  493. acc += bp[12 - 7];
  494. acc -= bp[11 - 7];
  495. rp[4] = (unsigned int)acc;
  496. acc >>= 32;
  497. acc += rp[5];
  498. acc += bp[9 - 7];
  499. acc += bp[13 - 7];
  500. acc -= bp[12 - 7];
  501. rp[5] = (unsigned int)acc;
  502. acc >>= 32;
  503. acc += rp[6];
  504. acc += bp[10 - 7];
  505. acc -= bp[13 - 7];
  506. rp[6] = (unsigned int)acc;
  507. carry = (int)(acc >> 32);
  508. # if BN_BITS2==64
  509. rp[7] = carry;
  510. # endif
  511. }
  512. #else
  513. {
  514. BN_ULONG t_d[BN_NIST_224_TOP];
  515. nist_set_224(t_d, buf.bn, 10, 9, 8, 7, 0, 0, 0);
  516. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  517. nist_set_224(t_d, buf.bn, 0, 13, 12, 11, 0, 0, 0);
  518. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  519. nist_set_224(t_d, buf.bn, 13, 12, 11, 10, 9, 8, 7);
  520. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  521. nist_set_224(t_d, buf.bn, 0, 0, 0, 0, 13, 12, 11);
  522. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  523. # if BN_BITS2==64
  524. carry = (int)(r_d[BN_NIST_224_TOP - 1] >> 32);
  525. # endif
  526. }
  527. #endif
  528. u.f = bn_sub_words;
  529. if (carry > 0) {
  530. carry =
  531. (int)bn_sub_words(r_d, r_d, _nist_p_224[carry - 1],
  532. BN_NIST_224_TOP);
  533. #if BN_BITS2==64
  534. carry = (int)(~(r_d[BN_NIST_224_TOP - 1] >> 32)) & 1;
  535. #endif
  536. } else if (carry < 0) {
  537. /*
  538. * it's a bit more complicated logic in this case. if bn_add_words
  539. * yields no carry, then result has to be adjusted by unconditionally
  540. * *adding* the modulus. but if it does, then result has to be
  541. * compared to the modulus and conditionally adjusted by
  542. * *subtracting* the latter.
  543. */
  544. carry =
  545. (int)bn_add_words(r_d, r_d, _nist_p_224[-carry - 1],
  546. BN_NIST_224_TOP);
  547. mask = 0 - (PTR_SIZE_INT) carry;
  548. u.p = ((PTR_SIZE_INT) bn_sub_words & mask) |
  549. ((PTR_SIZE_INT) bn_add_words & ~mask);
  550. } else
  551. carry = 1;
  552. /* otherwise it's effectively same as in BN_nist_mod_192... */
  553. mask =
  554. 0 - (PTR_SIZE_INT) (*u.f) (c_d, r_d, _nist_p_224[0], BN_NIST_224_TOP);
  555. mask &= 0 - (PTR_SIZE_INT) carry;
  556. res = c_d;
  557. res = (BN_ULONG *)(((PTR_SIZE_INT) res & ~mask) |
  558. ((PTR_SIZE_INT) r_d & mask));
  559. nist_cp_bn(r_d, res, BN_NIST_224_TOP);
  560. r->top = BN_NIST_224_TOP;
  561. bn_correct_top(r);
  562. return 1;
  563. }
  564. #define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \
  565. { \
  566. bn_cp_32(to, 0, from, (a8) - 8) \
  567. bn_cp_32(to, 1, from, (a7) - 8) \
  568. bn_cp_32(to, 2, from, (a6) - 8) \
  569. bn_cp_32(to, 3, from, (a5) - 8) \
  570. bn_cp_32(to, 4, from, (a4) - 8) \
  571. bn_cp_32(to, 5, from, (a3) - 8) \
  572. bn_cp_32(to, 6, from, (a2) - 8) \
  573. bn_cp_32(to, 7, from, (a1) - 8) \
  574. }
  575. int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  576. BN_CTX *ctx)
  577. {
  578. int i, top = a->top;
  579. int carry = 0;
  580. register BN_ULONG *a_d = a->d, *r_d;
  581. union {
  582. BN_ULONG bn[BN_NIST_256_TOP];
  583. unsigned int ui[BN_NIST_256_TOP * sizeof(BN_ULONG) /
  584. sizeof(unsigned int)];
  585. } buf;
  586. BN_ULONG c_d[BN_NIST_256_TOP], *res;
  587. PTR_SIZE_INT mask;
  588. union {
  589. bn_addsub_f f;
  590. PTR_SIZE_INT p;
  591. } u;
  592. static const BIGNUM ossl_bignum_nist_p_256_sqr = {
  593. (BN_ULONG *)_nist_p_256_sqr,
  594. OSSL_NELEM(_nist_p_256_sqr),
  595. OSSL_NELEM(_nist_p_256_sqr),
  596. 0, BN_FLG_STATIC_DATA
  597. };
  598. field = &ossl_bignum_nist_p_256; /* just to make sure */
  599. if (BN_is_negative(a) || BN_ucmp(a, &ossl_bignum_nist_p_256_sqr) >= 0)
  600. return BN_nnmod(r, a, field, ctx);
  601. i = BN_ucmp(field, a);
  602. if (i == 0) {
  603. BN_zero(r);
  604. return 1;
  605. } else if (i > 0)
  606. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  607. if (r != a) {
  608. if (!bn_wexpand(r, BN_NIST_256_TOP))
  609. return 0;
  610. r_d = r->d;
  611. nist_cp_bn(r_d, a_d, BN_NIST_256_TOP);
  612. } else
  613. r_d = a_d;
  614. nist_cp_bn_0(buf.bn, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP,
  615. BN_NIST_256_TOP);
  616. #if defined(NIST_INT64)
  617. {
  618. NIST_INT64 acc; /* accumulator */
  619. unsigned int *rp = (unsigned int *)r_d;
  620. const unsigned int *bp = (const unsigned int *)buf.ui;
  621. acc = rp[0];
  622. acc += bp[8 - 8];
  623. acc += bp[9 - 8];
  624. acc -= bp[11 - 8];
  625. acc -= bp[12 - 8];
  626. acc -= bp[13 - 8];
  627. acc -= bp[14 - 8];
  628. rp[0] = (unsigned int)acc;
  629. acc >>= 32;
  630. acc += rp[1];
  631. acc += bp[9 - 8];
  632. acc += bp[10 - 8];
  633. acc -= bp[12 - 8];
  634. acc -= bp[13 - 8];
  635. acc -= bp[14 - 8];
  636. acc -= bp[15 - 8];
  637. rp[1] = (unsigned int)acc;
  638. acc >>= 32;
  639. acc += rp[2];
  640. acc += bp[10 - 8];
  641. acc += bp[11 - 8];
  642. acc -= bp[13 - 8];
  643. acc -= bp[14 - 8];
  644. acc -= bp[15 - 8];
  645. rp[2] = (unsigned int)acc;
  646. acc >>= 32;
  647. acc += rp[3];
  648. acc += bp[11 - 8];
  649. acc += bp[11 - 8];
  650. acc += bp[12 - 8];
  651. acc += bp[12 - 8];
  652. acc += bp[13 - 8];
  653. acc -= bp[15 - 8];
  654. acc -= bp[8 - 8];
  655. acc -= bp[9 - 8];
  656. rp[3] = (unsigned int)acc;
  657. acc >>= 32;
  658. acc += rp[4];
  659. acc += bp[12 - 8];
  660. acc += bp[12 - 8];
  661. acc += bp[13 - 8];
  662. acc += bp[13 - 8];
  663. acc += bp[14 - 8];
  664. acc -= bp[9 - 8];
  665. acc -= bp[10 - 8];
  666. rp[4] = (unsigned int)acc;
  667. acc >>= 32;
  668. acc += rp[5];
  669. acc += bp[13 - 8];
  670. acc += bp[13 - 8];
  671. acc += bp[14 - 8];
  672. acc += bp[14 - 8];
  673. acc += bp[15 - 8];
  674. acc -= bp[10 - 8];
  675. acc -= bp[11 - 8];
  676. rp[5] = (unsigned int)acc;
  677. acc >>= 32;
  678. acc += rp[6];
  679. acc += bp[14 - 8];
  680. acc += bp[14 - 8];
  681. acc += bp[15 - 8];
  682. acc += bp[15 - 8];
  683. acc += bp[14 - 8];
  684. acc += bp[13 - 8];
  685. acc -= bp[8 - 8];
  686. acc -= bp[9 - 8];
  687. rp[6] = (unsigned int)acc;
  688. acc >>= 32;
  689. acc += rp[7];
  690. acc += bp[15 - 8];
  691. acc += bp[15 - 8];
  692. acc += bp[15 - 8];
  693. acc += bp[8 - 8];
  694. acc -= bp[10 - 8];
  695. acc -= bp[11 - 8];
  696. acc -= bp[12 - 8];
  697. acc -= bp[13 - 8];
  698. rp[7] = (unsigned int)acc;
  699. carry = (int)(acc >> 32);
  700. }
  701. #else
  702. {
  703. BN_ULONG t_d[BN_NIST_256_TOP];
  704. /*
  705. * S1
  706. */
  707. nist_set_256(t_d, buf.bn, 15, 14, 13, 12, 11, 0, 0, 0);
  708. /*
  709. * S2
  710. */
  711. nist_set_256(c_d, buf.bn, 0, 15, 14, 13, 12, 0, 0, 0);
  712. carry = (int)bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
  713. /* left shift */
  714. {
  715. register BN_ULONG *ap, t, c;
  716. ap = t_d;
  717. c = 0;
  718. for (i = BN_NIST_256_TOP; i != 0; --i) {
  719. t = *ap;
  720. *(ap++) = ((t << 1) | c) & BN_MASK2;
  721. c = (t & BN_TBIT) ? 1 : 0;
  722. }
  723. carry <<= 1;
  724. carry |= c;
  725. }
  726. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  727. /*
  728. * S3
  729. */
  730. nist_set_256(t_d, buf.bn, 15, 14, 0, 0, 0, 10, 9, 8);
  731. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  732. /*
  733. * S4
  734. */
  735. nist_set_256(t_d, buf.bn, 8, 13, 15, 14, 13, 11, 10, 9);
  736. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  737. /*
  738. * D1
  739. */
  740. nist_set_256(t_d, buf.bn, 10, 8, 0, 0, 0, 13, 12, 11);
  741. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  742. /*
  743. * D2
  744. */
  745. nist_set_256(t_d, buf.bn, 11, 9, 0, 0, 15, 14, 13, 12);
  746. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  747. /*
  748. * D3
  749. */
  750. nist_set_256(t_d, buf.bn, 12, 0, 10, 9, 8, 15, 14, 13);
  751. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  752. /*
  753. * D4
  754. */
  755. nist_set_256(t_d, buf.bn, 13, 0, 11, 10, 9, 0, 15, 14);
  756. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  757. }
  758. #endif
  759. /* see BN_nist_mod_224 for explanation */
  760. u.f = bn_sub_words;
  761. if (carry > 0)
  762. carry =
  763. (int)bn_sub_words(r_d, r_d, _nist_p_256[carry - 1],
  764. BN_NIST_256_TOP);
  765. else if (carry < 0) {
  766. carry =
  767. (int)bn_add_words(r_d, r_d, _nist_p_256[-carry - 1],
  768. BN_NIST_256_TOP);
  769. mask = 0 - (PTR_SIZE_INT) carry;
  770. u.p = ((PTR_SIZE_INT) bn_sub_words & mask) |
  771. ((PTR_SIZE_INT) bn_add_words & ~mask);
  772. } else
  773. carry = 1;
  774. mask =
  775. 0 - (PTR_SIZE_INT) (*u.f) (c_d, r_d, _nist_p_256[0], BN_NIST_256_TOP);
  776. mask &= 0 - (PTR_SIZE_INT) carry;
  777. res = c_d;
  778. res = (BN_ULONG *)(((PTR_SIZE_INT) res & ~mask) |
  779. ((PTR_SIZE_INT) r_d & mask));
  780. nist_cp_bn(r_d, res, BN_NIST_256_TOP);
  781. r->top = BN_NIST_256_TOP;
  782. bn_correct_top(r);
  783. return 1;
  784. }
  785. #define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
  786. { \
  787. bn_cp_32(to, 0, from, (a12) - 12) \
  788. bn_cp_32(to, 1, from, (a11) - 12) \
  789. bn_cp_32(to, 2, from, (a10) - 12) \
  790. bn_cp_32(to, 3, from, (a9) - 12) \
  791. bn_cp_32(to, 4, from, (a8) - 12) \
  792. bn_cp_32(to, 5, from, (a7) - 12) \
  793. bn_cp_32(to, 6, from, (a6) - 12) \
  794. bn_cp_32(to, 7, from, (a5) - 12) \
  795. bn_cp_32(to, 8, from, (a4) - 12) \
  796. bn_cp_32(to, 9, from, (a3) - 12) \
  797. bn_cp_32(to, 10, from, (a2) - 12) \
  798. bn_cp_32(to, 11, from, (a1) - 12) \
  799. }
  800. int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  801. BN_CTX *ctx)
  802. {
  803. int i, top = a->top;
  804. int carry = 0;
  805. register BN_ULONG *r_d, *a_d = a->d;
  806. union {
  807. BN_ULONG bn[BN_NIST_384_TOP];
  808. unsigned int ui[BN_NIST_384_TOP * sizeof(BN_ULONG) /
  809. sizeof(unsigned int)];
  810. } buf;
  811. BN_ULONG c_d[BN_NIST_384_TOP], *res;
  812. PTR_SIZE_INT mask;
  813. union {
  814. bn_addsub_f f;
  815. PTR_SIZE_INT p;
  816. } u;
  817. static const BIGNUM ossl_bignum_nist_p_384_sqr = {
  818. (BN_ULONG *)_nist_p_384_sqr,
  819. OSSL_NELEM(_nist_p_384_sqr),
  820. OSSL_NELEM(_nist_p_384_sqr),
  821. 0, BN_FLG_STATIC_DATA
  822. };
  823. field = &ossl_bignum_nist_p_384; /* just to make sure */
  824. if (BN_is_negative(a) || BN_ucmp(a, &ossl_bignum_nist_p_384_sqr) >= 0)
  825. return BN_nnmod(r, a, field, ctx);
  826. i = BN_ucmp(field, a);
  827. if (i == 0) {
  828. BN_zero(r);
  829. return 1;
  830. } else if (i > 0)
  831. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  832. if (r != a) {
  833. if (!bn_wexpand(r, BN_NIST_384_TOP))
  834. return 0;
  835. r_d = r->d;
  836. nist_cp_bn(r_d, a_d, BN_NIST_384_TOP);
  837. } else
  838. r_d = a_d;
  839. nist_cp_bn_0(buf.bn, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP,
  840. BN_NIST_384_TOP);
  841. #if defined(NIST_INT64)
  842. {
  843. NIST_INT64 acc; /* accumulator */
  844. unsigned int *rp = (unsigned int *)r_d;
  845. const unsigned int *bp = (const unsigned int *)buf.ui;
  846. acc = rp[0];
  847. acc += bp[12 - 12];
  848. acc += bp[21 - 12];
  849. acc += bp[20 - 12];
  850. acc -= bp[23 - 12];
  851. rp[0] = (unsigned int)acc;
  852. acc >>= 32;
  853. acc += rp[1];
  854. acc += bp[13 - 12];
  855. acc += bp[22 - 12];
  856. acc += bp[23 - 12];
  857. acc -= bp[12 - 12];
  858. acc -= bp[20 - 12];
  859. rp[1] = (unsigned int)acc;
  860. acc >>= 32;
  861. acc += rp[2];
  862. acc += bp[14 - 12];
  863. acc += bp[23 - 12];
  864. acc -= bp[13 - 12];
  865. acc -= bp[21 - 12];
  866. rp[2] = (unsigned int)acc;
  867. acc >>= 32;
  868. acc += rp[3];
  869. acc += bp[15 - 12];
  870. acc += bp[12 - 12];
  871. acc += bp[20 - 12];
  872. acc += bp[21 - 12];
  873. acc -= bp[14 - 12];
  874. acc -= bp[22 - 12];
  875. acc -= bp[23 - 12];
  876. rp[3] = (unsigned int)acc;
  877. acc >>= 32;
  878. acc += rp[4];
  879. acc += bp[21 - 12];
  880. acc += bp[21 - 12];
  881. acc += bp[16 - 12];
  882. acc += bp[13 - 12];
  883. acc += bp[12 - 12];
  884. acc += bp[20 - 12];
  885. acc += bp[22 - 12];
  886. acc -= bp[15 - 12];
  887. acc -= bp[23 - 12];
  888. acc -= bp[23 - 12];
  889. rp[4] = (unsigned int)acc;
  890. acc >>= 32;
  891. acc += rp[5];
  892. acc += bp[22 - 12];
  893. acc += bp[22 - 12];
  894. acc += bp[17 - 12];
  895. acc += bp[14 - 12];
  896. acc += bp[13 - 12];
  897. acc += bp[21 - 12];
  898. acc += bp[23 - 12];
  899. acc -= bp[16 - 12];
  900. rp[5] = (unsigned int)acc;
  901. acc >>= 32;
  902. acc += rp[6];
  903. acc += bp[23 - 12];
  904. acc += bp[23 - 12];
  905. acc += bp[18 - 12];
  906. acc += bp[15 - 12];
  907. acc += bp[14 - 12];
  908. acc += bp[22 - 12];
  909. acc -= bp[17 - 12];
  910. rp[6] = (unsigned int)acc;
  911. acc >>= 32;
  912. acc += rp[7];
  913. acc += bp[19 - 12];
  914. acc += bp[16 - 12];
  915. acc += bp[15 - 12];
  916. acc += bp[23 - 12];
  917. acc -= bp[18 - 12];
  918. rp[7] = (unsigned int)acc;
  919. acc >>= 32;
  920. acc += rp[8];
  921. acc += bp[20 - 12];
  922. acc += bp[17 - 12];
  923. acc += bp[16 - 12];
  924. acc -= bp[19 - 12];
  925. rp[8] = (unsigned int)acc;
  926. acc >>= 32;
  927. acc += rp[9];
  928. acc += bp[21 - 12];
  929. acc += bp[18 - 12];
  930. acc += bp[17 - 12];
  931. acc -= bp[20 - 12];
  932. rp[9] = (unsigned int)acc;
  933. acc >>= 32;
  934. acc += rp[10];
  935. acc += bp[22 - 12];
  936. acc += bp[19 - 12];
  937. acc += bp[18 - 12];
  938. acc -= bp[21 - 12];
  939. rp[10] = (unsigned int)acc;
  940. acc >>= 32;
  941. acc += rp[11];
  942. acc += bp[23 - 12];
  943. acc += bp[20 - 12];
  944. acc += bp[19 - 12];
  945. acc -= bp[22 - 12];
  946. rp[11] = (unsigned int)acc;
  947. carry = (int)(acc >> 32);
  948. }
  949. #else
  950. {
  951. BN_ULONG t_d[BN_NIST_384_TOP];
  952. /*
  953. * S1
  954. */
  955. nist_set_256(t_d, buf.bn, 0, 0, 0, 0, 0, 23 - 4, 22 - 4, 21 - 4);
  956. /* left shift */
  957. {
  958. register BN_ULONG *ap, t, c;
  959. ap = t_d;
  960. c = 0;
  961. for (i = 3; i != 0; --i) {
  962. t = *ap;
  963. *(ap++) = ((t << 1) | c) & BN_MASK2;
  964. c = (t & BN_TBIT) ? 1 : 0;
  965. }
  966. *ap = c;
  967. }
  968. carry =
  969. (int)bn_add_words(r_d + (128 / BN_BITS2), r_d + (128 / BN_BITS2),
  970. t_d, BN_NIST_256_TOP);
  971. /*
  972. * S2
  973. */
  974. carry += (int)bn_add_words(r_d, r_d, buf.bn, BN_NIST_384_TOP);
  975. /*
  976. * S3
  977. */
  978. nist_set_384(t_d, buf.bn, 20, 19, 18, 17, 16, 15, 14, 13, 12, 23, 22,
  979. 21);
  980. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  981. /*
  982. * S4
  983. */
  984. nist_set_384(t_d, buf.bn, 19, 18, 17, 16, 15, 14, 13, 12, 20, 0, 23,
  985. 0);
  986. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  987. /*
  988. * S5
  989. */
  990. nist_set_384(t_d, buf.bn, 0, 0, 0, 0, 23, 22, 21, 20, 0, 0, 0, 0);
  991. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  992. /*
  993. * S6
  994. */
  995. nist_set_384(t_d, buf.bn, 0, 0, 0, 0, 0, 0, 23, 22, 21, 0, 0, 20);
  996. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  997. /*
  998. * D1
  999. */
  1000. nist_set_384(t_d, buf.bn, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12,
  1001. 23);
  1002. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  1003. /*
  1004. * D2
  1005. */
  1006. nist_set_384(t_d, buf.bn, 0, 0, 0, 0, 0, 0, 0, 23, 22, 21, 20, 0);
  1007. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  1008. /*
  1009. * D3
  1010. */
  1011. nist_set_384(t_d, buf.bn, 0, 0, 0, 0, 0, 0, 0, 23, 23, 0, 0, 0);
  1012. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  1013. }
  1014. #endif
  1015. /* see BN_nist_mod_224 for explanation */
  1016. u.f = bn_sub_words;
  1017. if (carry > 0)
  1018. carry =
  1019. (int)bn_sub_words(r_d, r_d, _nist_p_384[carry - 1],
  1020. BN_NIST_384_TOP);
  1021. else if (carry < 0) {
  1022. carry =
  1023. (int)bn_add_words(r_d, r_d, _nist_p_384[-carry - 1],
  1024. BN_NIST_384_TOP);
  1025. mask = 0 - (PTR_SIZE_INT) carry;
  1026. u.p = ((PTR_SIZE_INT) bn_sub_words & mask) |
  1027. ((PTR_SIZE_INT) bn_add_words & ~mask);
  1028. } else
  1029. carry = 1;
  1030. mask =
  1031. 0 - (PTR_SIZE_INT) (*u.f) (c_d, r_d, _nist_p_384[0], BN_NIST_384_TOP);
  1032. mask &= 0 - (PTR_SIZE_INT) carry;
  1033. res = c_d;
  1034. res = (BN_ULONG *)(((PTR_SIZE_INT) res & ~mask) |
  1035. ((PTR_SIZE_INT) r_d & mask));
  1036. nist_cp_bn(r_d, res, BN_NIST_384_TOP);
  1037. r->top = BN_NIST_384_TOP;
  1038. bn_correct_top(r);
  1039. return 1;
  1040. }
  1041. #define BN_NIST_521_RSHIFT (521%BN_BITS2)
  1042. #define BN_NIST_521_LSHIFT (BN_BITS2-BN_NIST_521_RSHIFT)
  1043. #define BN_NIST_521_TOP_MASK ((BN_ULONG)BN_MASK2>>BN_NIST_521_LSHIFT)
  1044. int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  1045. BN_CTX *ctx)
  1046. {
  1047. int top = a->top, i;
  1048. BN_ULONG *r_d, *a_d = a->d, t_d[BN_NIST_521_TOP], val, tmp, *res;
  1049. PTR_SIZE_INT mask;
  1050. static const BIGNUM ossl_bignum_nist_p_521_sqr = {
  1051. (BN_ULONG *)_nist_p_521_sqr,
  1052. OSSL_NELEM(_nist_p_521_sqr),
  1053. OSSL_NELEM(_nist_p_521_sqr),
  1054. 0, BN_FLG_STATIC_DATA
  1055. };
  1056. field = &ossl_bignum_nist_p_521; /* just to make sure */
  1057. if (BN_is_negative(a) || BN_ucmp(a, &ossl_bignum_nist_p_521_sqr) >= 0)
  1058. return BN_nnmod(r, a, field, ctx);
  1059. i = BN_ucmp(field, a);
  1060. if (i == 0) {
  1061. BN_zero(r);
  1062. return 1;
  1063. } else if (i > 0)
  1064. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  1065. if (r != a) {
  1066. if (!bn_wexpand(r, BN_NIST_521_TOP))
  1067. return 0;
  1068. r_d = r->d;
  1069. nist_cp_bn(r_d, a_d, BN_NIST_521_TOP);
  1070. } else
  1071. r_d = a_d;
  1072. /* upper 521 bits, copy ... */
  1073. nist_cp_bn_0(t_d, a_d + (BN_NIST_521_TOP - 1),
  1074. top - (BN_NIST_521_TOP - 1), BN_NIST_521_TOP);
  1075. /* ... and right shift */
  1076. for (val = t_d[0], i = 0; i < BN_NIST_521_TOP - 1; i++) {
  1077. #if 0
  1078. /*
  1079. * MSC ARM compiler [version 2013, presumably even earlier,
  1080. * much earlier] miscompiles this code, but not one in
  1081. * #else section. See RT#3541.
  1082. */
  1083. tmp = val >> BN_NIST_521_RSHIFT;
  1084. val = t_d[i + 1];
  1085. t_d[i] = (tmp | val << BN_NIST_521_LSHIFT) & BN_MASK2;
  1086. #else
  1087. t_d[i] = (val >> BN_NIST_521_RSHIFT |
  1088. (tmp = t_d[i + 1]) << BN_NIST_521_LSHIFT) & BN_MASK2;
  1089. val = tmp;
  1090. #endif
  1091. }
  1092. t_d[i] = val >> BN_NIST_521_RSHIFT;
  1093. /* lower 521 bits */
  1094. r_d[i] &= BN_NIST_521_TOP_MASK;
  1095. bn_add_words(r_d, r_d, t_d, BN_NIST_521_TOP);
  1096. mask =
  1097. 0 - (PTR_SIZE_INT) bn_sub_words(t_d, r_d, _nist_p_521,
  1098. BN_NIST_521_TOP);
  1099. res = t_d;
  1100. res = (BN_ULONG *)(((PTR_SIZE_INT) res & ~mask) |
  1101. ((PTR_SIZE_INT) r_d & mask));
  1102. nist_cp_bn(r_d, res, BN_NIST_521_TOP);
  1103. r->top = BN_NIST_521_TOP;
  1104. bn_correct_top(r);
  1105. return 1;
  1106. }
  1107. int (*BN_nist_mod_func(const BIGNUM *p)) (BIGNUM *r, const BIGNUM *a,
  1108. const BIGNUM *field, BN_CTX *ctx) {
  1109. if (BN_ucmp(&ossl_bignum_nist_p_192, p) == 0)
  1110. return BN_nist_mod_192;
  1111. if (BN_ucmp(&ossl_bignum_nist_p_224, p) == 0)
  1112. return BN_nist_mod_224;
  1113. if (BN_ucmp(&ossl_bignum_nist_p_256, p) == 0)
  1114. return BN_nist_mod_256;
  1115. if (BN_ucmp(&ossl_bignum_nist_p_384, p) == 0)
  1116. return BN_nist_mod_384;
  1117. if (BN_ucmp(&ossl_bignum_nist_p_521, p) == 0)
  1118. return BN_nist_mod_521;
  1119. return 0;
  1120. }