bn_nist.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /* crypto/bn/bn_nist.c */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include "bn_lcl.h"
  59. #include "cryptlib.h"
  60. #define BN_NIST_192_TOP (192+BN_BITS2-1)/BN_BITS2
  61. #define BN_NIST_224_TOP (224+BN_BITS2-1)/BN_BITS2
  62. #define BN_NIST_256_TOP (256+BN_BITS2-1)/BN_BITS2
  63. #define BN_NIST_384_TOP (384+BN_BITS2-1)/BN_BITS2
  64. #define BN_NIST_521_TOP (521+BN_BITS2-1)/BN_BITS2
  65. /* pre-computed tables are "carry-less" values of modulus*(i+1) */
  66. #if BN_BITS2 == 64
  67. __fips_constseg
  68. static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
  69. {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFFULL},
  70. {0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFDULL,0xFFFFFFFFFFFFFFFFULL},
  71. {0xFFFFFFFFFFFFFFFDULL,0xFFFFFFFFFFFFFFFCULL,0xFFFFFFFFFFFFFFFFULL}
  72. };
  73. __fips_constseg
  74. static const BN_ULONG _nist_p_192_sqr[] = {
  75. 0x0000000000000001ULL,0x0000000000000002ULL,0x0000000000000001ULL,
  76. 0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFDULL,0xFFFFFFFFFFFFFFFFULL
  77. };
  78. __fips_constseg
  79. static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
  80. {0x0000000000000001ULL,0xFFFFFFFF00000000ULL,
  81. 0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL},
  82. {0x0000000000000002ULL,0xFFFFFFFE00000000ULL,
  83. 0xFFFFFFFFFFFFFFFFULL,0x00000001FFFFFFFFULL} /* this one is "carry-full" */
  84. };
  85. __fips_constseg
  86. static const BN_ULONG _nist_p_224_sqr[] = {
  87. 0x0000000000000001ULL,0xFFFFFFFE00000000ULL,
  88. 0xFFFFFFFFFFFFFFFFULL,0x0000000200000000ULL,
  89. 0x0000000000000000ULL,0xFFFFFFFFFFFFFFFEULL,
  90. 0xFFFFFFFFFFFFFFFFULL
  91. };
  92. __fips_constseg
  93. static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
  94. {0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL,
  95. 0x0000000000000000ULL,0xFFFFFFFF00000001ULL},
  96. {0xFFFFFFFFFFFFFFFEULL,0x00000001FFFFFFFFULL,
  97. 0x0000000000000000ULL,0xFFFFFFFE00000002ULL},
  98. {0xFFFFFFFFFFFFFFFDULL,0x00000002FFFFFFFFULL,
  99. 0x0000000000000000ULL,0xFFFFFFFD00000003ULL},
  100. {0xFFFFFFFFFFFFFFFCULL,0x00000003FFFFFFFFULL,
  101. 0x0000000000000000ULL,0xFFFFFFFC00000004ULL},
  102. {0xFFFFFFFFFFFFFFFBULL,0x00000004FFFFFFFFULL,
  103. 0x0000000000000000ULL,0xFFFFFFFB00000005ULL},
  104. };
  105. __fips_constseg
  106. static const BN_ULONG _nist_p_256_sqr[] = {
  107. 0x0000000000000001ULL,0xFFFFFFFE00000000ULL,
  108. 0xFFFFFFFFFFFFFFFFULL,0x00000001FFFFFFFEULL,
  109. 0x00000001FFFFFFFEULL,0x00000001FFFFFFFEULL,
  110. 0xFFFFFFFE00000001ULL,0xFFFFFFFE00000002ULL
  111. };
  112. __fips_constseg
  113. static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
  114. {0x00000000FFFFFFFFULL,0xFFFFFFFF00000000ULL,0xFFFFFFFFFFFFFFFEULL,
  115. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
  116. {0x00000001FFFFFFFEULL,0xFFFFFFFE00000000ULL,0xFFFFFFFFFFFFFFFDULL,
  117. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
  118. {0x00000002FFFFFFFDULL,0xFFFFFFFD00000000ULL,0xFFFFFFFFFFFFFFFCULL,
  119. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
  120. {0x00000003FFFFFFFCULL,0xFFFFFFFC00000000ULL,0xFFFFFFFFFFFFFFFBULL,
  121. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
  122. {0x00000004FFFFFFFBULL,0xFFFFFFFB00000000ULL,0xFFFFFFFFFFFFFFFAULL,
  123. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
  124. };
  125. __fips_constseg
  126. static const BN_ULONG _nist_p_384_sqr[] = {
  127. 0xFFFFFFFE00000001ULL,0x0000000200000000ULL,0xFFFFFFFE00000000ULL,
  128. 0x0000000200000000ULL,0x0000000000000001ULL,0x0000000000000000ULL,
  129. 0x00000001FFFFFFFEULL,0xFFFFFFFE00000000ULL,0xFFFFFFFFFFFFFFFDULL,
  130. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL
  131. };
  132. __fips_constseg
  133. static const BN_ULONG _nist_p_521[] =
  134. {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
  135. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
  136. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
  137. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
  138. 0x00000000000001FFULL};
  139. __fips_constseg
  140. static const BN_ULONG _nist_p_521_sqr[] = {
  141. 0x0000000000000001ULL,0x0000000000000000ULL,0x0000000000000000ULL,
  142. 0x0000000000000000ULL,0x0000000000000000ULL,0x0000000000000000ULL,
  143. 0x0000000000000000ULL,0x0000000000000000ULL,0xFFFFFFFFFFFFFC00ULL,
  144. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
  145. 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
  146. 0xFFFFFFFFFFFFFFFFULL,0x000000000003FFFFULL
  147. };
  148. #elif BN_BITS2 == 32
  149. __fips_constseg
  150. static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
  151. {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  152. {0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  153. {0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFC,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}
  154. };
  155. __fips_constseg
  156. static const BN_ULONG _nist_p_192_sqr[] = {
  157. 0x00000001,0x00000000,0x00000002,0x00000000,0x00000001,0x00000000,
  158. 0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF
  159. };
  160. __fips_constseg
  161. static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
  162. {0x00000001,0x00000000,0x00000000,0xFFFFFFFF,
  163. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  164. {0x00000002,0x00000000,0x00000000,0xFFFFFFFE,
  165. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}
  166. };
  167. __fips_constseg
  168. static const BN_ULONG _nist_p_224_sqr[] = {
  169. 0x00000001,0x00000000,0x00000000,0xFFFFFFFE,
  170. 0xFFFFFFFF,0xFFFFFFFF,0x00000000,0x00000002,
  171. 0x00000000,0x00000000,0xFFFFFFFE,0xFFFFFFFF,
  172. 0xFFFFFFFF,0xFFFFFFFF
  173. };
  174. __fips_constseg
  175. static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
  176. {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0x00000000,
  177. 0x00000000,0x00000000,0x00000001,0xFFFFFFFF},
  178. {0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0x00000001,
  179. 0x00000000,0x00000000,0x00000002,0xFFFFFFFE},
  180. {0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFF,0x00000002,
  181. 0x00000000,0x00000000,0x00000003,0xFFFFFFFD},
  182. {0xFFFFFFFC,0xFFFFFFFF,0xFFFFFFFF,0x00000003,
  183. 0x00000000,0x00000000,0x00000004,0xFFFFFFFC},
  184. {0xFFFFFFFB,0xFFFFFFFF,0xFFFFFFFF,0x00000004,
  185. 0x00000000,0x00000000,0x00000005,0xFFFFFFFB},
  186. };
  187. __fips_constseg
  188. static const BN_ULONG _nist_p_256_sqr[] = {
  189. 0x00000001,0x00000000,0x00000000,0xFFFFFFFE,
  190. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFE,0x00000001,
  191. 0xFFFFFFFE,0x00000001,0xFFFFFFFE,0x00000001,
  192. 0x00000001,0xFFFFFFFE,0x00000002,0xFFFFFFFE
  193. };
  194. __fips_constseg
  195. static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
  196. {0xFFFFFFFF,0x00000000,0x00000000,0xFFFFFFFF,0xFFFFFFFE,0xFFFFFFFF,
  197. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  198. {0xFFFFFFFE,0x00000001,0x00000000,0xFFFFFFFE,0xFFFFFFFD,0xFFFFFFFF,
  199. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  200. {0xFFFFFFFD,0x00000002,0x00000000,0xFFFFFFFD,0xFFFFFFFC,0xFFFFFFFF,
  201. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  202. {0xFFFFFFFC,0x00000003,0x00000000,0xFFFFFFFC,0xFFFFFFFB,0xFFFFFFFF,
  203. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  204. {0xFFFFFFFB,0x00000004,0x00000000,0xFFFFFFFB,0xFFFFFFFA,0xFFFFFFFF,
  205. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  206. };
  207. __fips_constseg
  208. static const BN_ULONG _nist_p_384_sqr[] = {
  209. 0x00000001,0xFFFFFFFE,0x00000000,0x00000002,0x00000000,0xFFFFFFFE,
  210. 0x00000000,0x00000002,0x00000001,0x00000000,0x00000000,0x00000000,
  211. 0xFFFFFFFE,0x00000001,0x00000000,0xFFFFFFFE,0xFFFFFFFD,0xFFFFFFFF,
  212. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF
  213. };
  214. __fips_constseg
  215. static const BN_ULONG _nist_p_521[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  216. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  217. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  218. 0xFFFFFFFF,0x000001FF};
  219. __fips_constseg
  220. static const BN_ULONG _nist_p_521_sqr[] = {
  221. 0x00000001,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
  222. 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
  223. 0x00000000,0x00000000,0x00000000,0x00000000,0xFFFFFC00,0xFFFFFFFF,
  224. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  225. 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  226. 0xFFFFFFFF,0xFFFFFFFF,0x0003FFFF
  227. };
  228. #else
  229. #error "unsupported BN_BITS2"
  230. #endif
  231. static const BIGNUM _bignum_nist_p_192 =
  232. {
  233. (BN_ULONG *)_nist_p_192[0],
  234. BN_NIST_192_TOP,
  235. BN_NIST_192_TOP,
  236. 0,
  237. BN_FLG_STATIC_DATA
  238. };
  239. static const BIGNUM _bignum_nist_p_224 =
  240. {
  241. (BN_ULONG *)_nist_p_224[0],
  242. BN_NIST_224_TOP,
  243. BN_NIST_224_TOP,
  244. 0,
  245. BN_FLG_STATIC_DATA
  246. };
  247. static const BIGNUM _bignum_nist_p_256 =
  248. {
  249. (BN_ULONG *)_nist_p_256[0],
  250. BN_NIST_256_TOP,
  251. BN_NIST_256_TOP,
  252. 0,
  253. BN_FLG_STATIC_DATA
  254. };
  255. static const BIGNUM _bignum_nist_p_384 =
  256. {
  257. (BN_ULONG *)_nist_p_384[0],
  258. BN_NIST_384_TOP,
  259. BN_NIST_384_TOP,
  260. 0,
  261. BN_FLG_STATIC_DATA
  262. };
  263. static const BIGNUM _bignum_nist_p_521 =
  264. {
  265. (BN_ULONG *)_nist_p_521,
  266. BN_NIST_521_TOP,
  267. BN_NIST_521_TOP,
  268. 0,
  269. BN_FLG_STATIC_DATA
  270. };
  271. const BIGNUM *BN_get0_nist_prime_192(void)
  272. {
  273. return &_bignum_nist_p_192;
  274. }
  275. const BIGNUM *BN_get0_nist_prime_224(void)
  276. {
  277. return &_bignum_nist_p_224;
  278. }
  279. const BIGNUM *BN_get0_nist_prime_256(void)
  280. {
  281. return &_bignum_nist_p_256;
  282. }
  283. const BIGNUM *BN_get0_nist_prime_384(void)
  284. {
  285. return &_bignum_nist_p_384;
  286. }
  287. const BIGNUM *BN_get0_nist_prime_521(void)
  288. {
  289. return &_bignum_nist_p_521;
  290. }
  291. static void nist_cp_bn_0(BN_ULONG *buf, BN_ULONG *a, int top, int max)
  292. {
  293. int i;
  294. BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
  295. #ifdef BN_DEBUG
  296. OPENSSL_assert(top <= max);
  297. #endif
  298. for (i = (top); i != 0; i--)
  299. *_tmp1++ = *_tmp2++;
  300. for (i = (max) - (top); i != 0; i--)
  301. *_tmp1++ = (BN_ULONG) 0;
  302. }
  303. static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top)
  304. {
  305. int i;
  306. BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
  307. for (i = (top); i != 0; i--)
  308. *_tmp1++ = *_tmp2++;
  309. }
  310. #if BN_BITS2 == 64
  311. #define bn_cp_64(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
  312. #define bn_64_set_0(to, n) (to)[n] = (BN_ULONG)0;
  313. /*
  314. * two following macros are implemented under assumption that they
  315. * are called in a sequence with *ascending* n, i.e. as they are...
  316. */
  317. #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))\
  318. :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l)))
  319. #define bn_32_set_0(to, n) (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0));
  320. #define bn_cp_32(to,n,from,m) ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n)
  321. # if defined(L_ENDIAN)
  322. # if defined(__arch64__)
  323. # define NIST_INT64 long
  324. # else
  325. # define NIST_INT64 long long
  326. # endif
  327. # endif
  328. #else
  329. #define bn_cp_64(to, n, from, m) \
  330. { \
  331. bn_cp_32(to, (n)*2, from, (m)*2); \
  332. bn_cp_32(to, (n)*2+1, from, (m)*2+1); \
  333. }
  334. #define bn_64_set_0(to, n) \
  335. { \
  336. bn_32_set_0(to, (n)*2); \
  337. bn_32_set_0(to, (n)*2+1); \
  338. }
  339. #define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
  340. #define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0;
  341. # if defined(_WIN32) && !defined(__GNUC__)
  342. # define NIST_INT64 __int64
  343. # else
  344. # define NIST_INT64 long long
  345. # endif
  346. #endif /* BN_BITS2 != 64 */
  347. #if defined(_TMS320C6X) && defined(NIST_INT64)
  348. # undef NIST_INT64 /* compiler bug */
  349. # pragma diag_suppress 177
  350. #endif
  351. #define nist_set_192(to, from, a1, a2, a3) \
  352. { \
  353. bn_cp_64(to, 0, from, (a3) - 3) \
  354. bn_cp_64(to, 1, from, (a2) - 3) \
  355. bn_cp_64(to, 2, from, (a1) - 3) \
  356. }
  357. int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  358. BN_CTX *ctx)
  359. {
  360. int top = a->top, i;
  361. int carry;
  362. register BN_ULONG *r_d, *a_d = a->d;
  363. BN_ULONG buf[BN_NIST_192_TOP],
  364. c_d[BN_NIST_192_TOP],
  365. *res;
  366. PTR_SIZE_INT mask;
  367. static const BIGNUM _bignum_nist_p_192_sqr = {
  368. (BN_ULONG *)_nist_p_192_sqr,
  369. sizeof(_nist_p_192_sqr)/sizeof(_nist_p_192_sqr[0]),
  370. sizeof(_nist_p_192_sqr)/sizeof(_nist_p_192_sqr[0]),
  371. 0,BN_FLG_STATIC_DATA };
  372. field = &_bignum_nist_p_192; /* just to make sure */
  373. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_192_sqr)>=0)
  374. return BN_nnmod(r, a, field, ctx);
  375. i = BN_ucmp(field, a);
  376. if (i == 0)
  377. {
  378. BN_zero(r);
  379. return 1;
  380. }
  381. else if (i > 0)
  382. return (r == a) ? 1 : (BN_copy(r ,a) != NULL);
  383. if (r != a)
  384. {
  385. if (!bn_wexpand(r, BN_NIST_192_TOP))
  386. return 0;
  387. r_d = r->d;
  388. nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);
  389. }
  390. else
  391. r_d = a_d;
  392. nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP);
  393. #if defined(NIST_INT64)
  394. {
  395. NIST_INT64 acc; /* accumulator */
  396. unsigned int *rp=(unsigned int *)r_d;
  397. const unsigned int *bp=(const unsigned int *)buf;
  398. acc = rp[0]; acc += bp[3*2-6];
  399. acc += bp[5*2-6]; rp[0] = (unsigned int)acc; acc >>= 32;
  400. acc += rp[1]; acc += bp[3*2-5];
  401. acc += bp[5*2-5]; rp[1] = (unsigned int)acc; acc >>= 32;
  402. acc += rp[2]; acc += bp[3*2-6];
  403. acc += bp[4*2-6];
  404. acc += bp[5*2-6]; rp[2] = (unsigned int)acc; acc >>= 32;
  405. acc += rp[3]; acc += bp[3*2-5];
  406. acc += bp[4*2-5];
  407. acc += bp[5*2-5]; rp[3] = (unsigned int)acc; acc >>= 32;
  408. acc += rp[4]; acc += bp[4*2-6];
  409. acc += bp[5*2-6]; rp[4] = (unsigned int)acc; acc >>= 32;
  410. acc += rp[5]; acc += bp[4*2-5];
  411. acc += bp[5*2-5]; rp[5] = (unsigned int)acc;
  412. carry = (int)(acc>>32);
  413. }
  414. #else
  415. {
  416. BN_ULONG t_d[BN_NIST_192_TOP];
  417. nist_set_192(t_d, buf, 0, 3, 3);
  418. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  419. nist_set_192(t_d, buf, 4, 4, 0);
  420. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  421. nist_set_192(t_d, buf, 5, 5, 5)
  422. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  423. }
  424. #endif
  425. if (carry > 0)
  426. carry = (int)bn_sub_words(r_d,r_d,_nist_p_192[carry-1],BN_NIST_192_TOP);
  427. else
  428. carry = 1;
  429. /*
  430. * we need 'if (carry==0 || result>=modulus) result-=modulus;'
  431. * as comparison implies subtraction, we can write
  432. * 'tmp=result-modulus; if (!carry || !borrow) result=tmp;'
  433. * this is what happens below, but without explicit if:-) a.
  434. */
  435. mask = 0-(PTR_SIZE_INT)bn_sub_words(c_d,r_d,_nist_p_192[0],BN_NIST_192_TOP);
  436. mask &= 0-(PTR_SIZE_INT)carry;
  437. res = (BN_ULONG *)
  438. (((PTR_SIZE_INT)c_d&~mask) | ((PTR_SIZE_INT)r_d&mask));
  439. nist_cp_bn(r_d, res, BN_NIST_192_TOP);
  440. r->top = BN_NIST_192_TOP;
  441. bn_correct_top(r);
  442. return 1;
  443. }
  444. typedef BN_ULONG (*bn_addsub_f)(BN_ULONG *,const BN_ULONG *,const BN_ULONG *,int);
  445. #define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \
  446. { \
  447. bn_cp_32(to, 0, from, (a7) - 7) \
  448. bn_cp_32(to, 1, from, (a6) - 7) \
  449. bn_cp_32(to, 2, from, (a5) - 7) \
  450. bn_cp_32(to, 3, from, (a4) - 7) \
  451. bn_cp_32(to, 4, from, (a3) - 7) \
  452. bn_cp_32(to, 5, from, (a2) - 7) \
  453. bn_cp_32(to, 6, from, (a1) - 7) \
  454. }
  455. int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  456. BN_CTX *ctx)
  457. {
  458. int top = a->top, i;
  459. int carry;
  460. BN_ULONG *r_d, *a_d = a->d;
  461. BN_ULONG buf[BN_NIST_224_TOP],
  462. c_d[BN_NIST_224_TOP],
  463. *res;
  464. PTR_SIZE_INT mask;
  465. union { bn_addsub_f f; PTR_SIZE_INT p; } u;
  466. static const BIGNUM _bignum_nist_p_224_sqr = {
  467. (BN_ULONG *)_nist_p_224_sqr,
  468. sizeof(_nist_p_224_sqr)/sizeof(_nist_p_224_sqr[0]),
  469. sizeof(_nist_p_224_sqr)/sizeof(_nist_p_224_sqr[0]),
  470. 0,BN_FLG_STATIC_DATA };
  471. field = &_bignum_nist_p_224; /* just to make sure */
  472. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_224_sqr)>=0)
  473. return BN_nnmod(r, a, field, ctx);
  474. i = BN_ucmp(field, a);
  475. if (i == 0)
  476. {
  477. BN_zero(r);
  478. return 1;
  479. }
  480. else if (i > 0)
  481. return (r == a)? 1 : (BN_copy(r ,a) != NULL);
  482. if (r != a)
  483. {
  484. if (!bn_wexpand(r, BN_NIST_224_TOP))
  485. return 0;
  486. r_d = r->d;
  487. nist_cp_bn(r_d, a_d, BN_NIST_224_TOP);
  488. }
  489. else
  490. r_d = a_d;
  491. #if BN_BITS2==64
  492. /* copy upper 256 bits of 448 bit number ... */
  493. nist_cp_bn_0(c_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP);
  494. /* ... and right shift by 32 to obtain upper 224 bits */
  495. nist_set_224(buf, c_d, 14, 13, 12, 11, 10, 9, 8);
  496. /* truncate lower part to 224 bits too */
  497. r_d[BN_NIST_224_TOP-1] &= BN_MASK2l;
  498. #else
  499. nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);
  500. #endif
  501. #if defined(NIST_INT64) && BN_BITS2!=64
  502. {
  503. NIST_INT64 acc; /* accumulator */
  504. unsigned int *rp=(unsigned int *)r_d;
  505. const unsigned int *bp=(const unsigned int *)buf;
  506. acc = rp[0]; acc -= bp[7-7];
  507. acc -= bp[11-7]; rp[0] = (unsigned int)acc; acc >>= 32;
  508. acc += rp[1]; acc -= bp[8-7];
  509. acc -= bp[12-7]; rp[1] = (unsigned int)acc; acc >>= 32;
  510. acc += rp[2]; acc -= bp[9-7];
  511. acc -= bp[13-7]; rp[2] = (unsigned int)acc; acc >>= 32;
  512. acc += rp[3]; acc += bp[7-7];
  513. acc += bp[11-7];
  514. acc -= bp[10-7]; rp[3] = (unsigned int)acc; acc>>= 32;
  515. acc += rp[4]; acc += bp[8-7];
  516. acc += bp[12-7];
  517. acc -= bp[11-7]; rp[4] = (unsigned int)acc; acc >>= 32;
  518. acc += rp[5]; acc += bp[9-7];
  519. acc += bp[13-7];
  520. acc -= bp[12-7]; rp[5] = (unsigned int)acc; acc >>= 32;
  521. acc += rp[6]; acc += bp[10-7];
  522. acc -= bp[13-7]; rp[6] = (unsigned int)acc;
  523. carry = (int)(acc>>32);
  524. # if BN_BITS2==64
  525. rp[7] = carry;
  526. # endif
  527. }
  528. #else
  529. {
  530. BN_ULONG t_d[BN_NIST_224_TOP];
  531. nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0);
  532. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  533. nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0);
  534. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  535. nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7);
  536. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  537. nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11);
  538. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  539. #if BN_BITS2==64
  540. carry = (int)(r_d[BN_NIST_224_TOP-1]>>32);
  541. #endif
  542. }
  543. #endif
  544. u.f = bn_sub_words;
  545. if (carry > 0)
  546. {
  547. carry = (int)bn_sub_words(r_d,r_d,_nist_p_224[carry-1],BN_NIST_224_TOP);
  548. #if BN_BITS2==64
  549. carry=(int)(~(r_d[BN_NIST_224_TOP-1]>>32))&1;
  550. #endif
  551. }
  552. else if (carry < 0)
  553. {
  554. /* it's a bit more comlicated logic in this case.
  555. * if bn_add_words yields no carry, then result
  556. * has to be adjusted by unconditionally *adding*
  557. * the modulus. but if it does, then result has
  558. * to be compared to the modulus and conditionally
  559. * adjusted by *subtracting* the latter. */
  560. carry = (int)bn_add_words(r_d,r_d,_nist_p_224[-carry-1],BN_NIST_224_TOP);
  561. mask = 0-(PTR_SIZE_INT)carry;
  562. u.p = ((PTR_SIZE_INT)bn_sub_words&mask) |
  563. ((PTR_SIZE_INT)bn_add_words&~mask);
  564. }
  565. else
  566. carry = 1;
  567. /* otherwise it's effectively same as in BN_nist_mod_192... */
  568. mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_224[0],BN_NIST_224_TOP);
  569. mask &= 0-(PTR_SIZE_INT)carry;
  570. res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) |
  571. ((PTR_SIZE_INT)r_d&mask));
  572. nist_cp_bn(r_d, res, BN_NIST_224_TOP);
  573. r->top = BN_NIST_224_TOP;
  574. bn_correct_top(r);
  575. return 1;
  576. }
  577. #define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \
  578. { \
  579. bn_cp_32(to, 0, from, (a8) - 8) \
  580. bn_cp_32(to, 1, from, (a7) - 8) \
  581. bn_cp_32(to, 2, from, (a6) - 8) \
  582. bn_cp_32(to, 3, from, (a5) - 8) \
  583. bn_cp_32(to, 4, from, (a4) - 8) \
  584. bn_cp_32(to, 5, from, (a3) - 8) \
  585. bn_cp_32(to, 6, from, (a2) - 8) \
  586. bn_cp_32(to, 7, from, (a1) - 8) \
  587. }
  588. int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  589. BN_CTX *ctx)
  590. {
  591. int i, top = a->top;
  592. int carry = 0;
  593. register BN_ULONG *a_d = a->d, *r_d;
  594. BN_ULONG buf[BN_NIST_256_TOP],
  595. c_d[BN_NIST_256_TOP],
  596. *res;
  597. PTR_SIZE_INT mask;
  598. union { bn_addsub_f f; PTR_SIZE_INT p; } u;
  599. static const BIGNUM _bignum_nist_p_256_sqr = {
  600. (BN_ULONG *)_nist_p_256_sqr,
  601. sizeof(_nist_p_256_sqr)/sizeof(_nist_p_256_sqr[0]),
  602. sizeof(_nist_p_256_sqr)/sizeof(_nist_p_256_sqr[0]),
  603. 0,BN_FLG_STATIC_DATA };
  604. field = &_bignum_nist_p_256; /* just to make sure */
  605. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_256_sqr)>=0)
  606. return BN_nnmod(r, a, field, ctx);
  607. i = BN_ucmp(field, a);
  608. if (i == 0)
  609. {
  610. BN_zero(r);
  611. return 1;
  612. }
  613. else if (i > 0)
  614. return (r == a)? 1 : (BN_copy(r ,a) != NULL);
  615. if (r != a)
  616. {
  617. if (!bn_wexpand(r, BN_NIST_256_TOP))
  618. return 0;
  619. r_d = r->d;
  620. nist_cp_bn(r_d, a_d, BN_NIST_256_TOP);
  621. }
  622. else
  623. r_d = a_d;
  624. nist_cp_bn_0(buf, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP, BN_NIST_256_TOP);
  625. #if defined(NIST_INT64)
  626. {
  627. NIST_INT64 acc; /* accumulator */
  628. unsigned int *rp=(unsigned int *)r_d;
  629. const unsigned int *bp=(const unsigned int *)buf;
  630. acc = rp[0]; acc += bp[8-8];
  631. acc += bp[9-8];
  632. acc -= bp[11-8];
  633. acc -= bp[12-8];
  634. acc -= bp[13-8];
  635. acc -= bp[14-8]; rp[0] = (unsigned int)acc; acc >>= 32;
  636. acc += rp[1]; acc += bp[9-8];
  637. acc += bp[10-8];
  638. acc -= bp[12-8];
  639. acc -= bp[13-8];
  640. acc -= bp[14-8];
  641. acc -= bp[15-8]; rp[1] = (unsigned int)acc; acc >>= 32;
  642. acc += rp[2]; acc += bp[10-8];
  643. acc += bp[11-8];
  644. acc -= bp[13-8];
  645. acc -= bp[14-8];
  646. acc -= bp[15-8]; rp[2] = (unsigned int)acc; acc >>= 32;
  647. acc += rp[3]; acc += bp[11-8];
  648. acc += bp[11-8];
  649. acc += bp[12-8];
  650. acc += bp[12-8];
  651. acc += bp[13-8];
  652. acc -= bp[15-8];
  653. acc -= bp[8-8];
  654. acc -= bp[9-8]; rp[3] = (unsigned int)acc; acc >>= 32;
  655. acc += rp[4]; acc += bp[12-8];
  656. acc += bp[12-8];
  657. acc += bp[13-8];
  658. acc += bp[13-8];
  659. acc += bp[14-8];
  660. acc -= bp[9-8];
  661. acc -= bp[10-8]; rp[4] = (unsigned int)acc; acc >>= 32;
  662. acc += rp[5]; acc += bp[13-8];
  663. acc += bp[13-8];
  664. acc += bp[14-8];
  665. acc += bp[14-8];
  666. acc += bp[15-8];
  667. acc -= bp[10-8];
  668. acc -= bp[11-8]; rp[5] = (unsigned int)acc; acc >>= 32;
  669. acc += rp[6]; acc += bp[14-8];
  670. acc += bp[14-8];
  671. acc += bp[15-8];
  672. acc += bp[15-8];
  673. acc += bp[14-8];
  674. acc += bp[13-8];
  675. acc -= bp[8-8];
  676. acc -= bp[9-8]; rp[6] = (unsigned int)acc; acc >>= 32;
  677. acc += rp[7]; acc += bp[15-8];
  678. acc += bp[15-8];
  679. acc += bp[15-8];
  680. acc += bp[8 -8];
  681. acc -= bp[10-8];
  682. acc -= bp[11-8];
  683. acc -= bp[12-8];
  684. acc -= bp[13-8]; rp[7] = (unsigned int)acc;
  685. carry = (int)(acc>>32);
  686. }
  687. #else
  688. {
  689. BN_ULONG t_d[BN_NIST_256_TOP];
  690. /*S1*/
  691. nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0);
  692. /*S2*/
  693. nist_set_256(c_d, buf, 0, 15, 14, 13, 12, 0, 0, 0);
  694. carry = (int)bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
  695. /* left shift */
  696. {
  697. register BN_ULONG *ap,t,c;
  698. ap = t_d;
  699. c=0;
  700. for (i = BN_NIST_256_TOP; i != 0; --i)
  701. {
  702. t= *ap;
  703. *(ap++)=((t<<1)|c)&BN_MASK2;
  704. c=(t & BN_TBIT)?1:0;
  705. }
  706. carry <<= 1;
  707. carry |= c;
  708. }
  709. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  710. /*S3*/
  711. nist_set_256(t_d, buf, 15, 14, 0, 0, 0, 10, 9, 8);
  712. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  713. /*S4*/
  714. nist_set_256(t_d, buf, 8, 13, 15, 14, 13, 11, 10, 9);
  715. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  716. /*D1*/
  717. nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11);
  718. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  719. /*D2*/
  720. nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12);
  721. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  722. /*D3*/
  723. nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13);
  724. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  725. /*D4*/
  726. nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14);
  727. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  728. }
  729. #endif
  730. /* see BN_nist_mod_224 for explanation */
  731. u.f = bn_sub_words;
  732. if (carry > 0)
  733. carry = (int)bn_sub_words(r_d,r_d,_nist_p_256[carry-1],BN_NIST_256_TOP);
  734. else if (carry < 0)
  735. {
  736. carry = (int)bn_add_words(r_d,r_d,_nist_p_256[-carry-1],BN_NIST_256_TOP);
  737. mask = 0-(PTR_SIZE_INT)carry;
  738. u.p = ((PTR_SIZE_INT)bn_sub_words&mask) |
  739. ((PTR_SIZE_INT)bn_add_words&~mask);
  740. }
  741. else
  742. carry = 1;
  743. mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_256[0],BN_NIST_256_TOP);
  744. mask &= 0-(PTR_SIZE_INT)carry;
  745. res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) |
  746. ((PTR_SIZE_INT)r_d&mask));
  747. nist_cp_bn(r_d, res, BN_NIST_256_TOP);
  748. r->top = BN_NIST_256_TOP;
  749. bn_correct_top(r);
  750. return 1;
  751. }
  752. #define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
  753. { \
  754. bn_cp_32(to, 0, from, (a12) - 12) \
  755. bn_cp_32(to, 1, from, (a11) - 12) \
  756. bn_cp_32(to, 2, from, (a10) - 12) \
  757. bn_cp_32(to, 3, from, (a9) - 12) \
  758. bn_cp_32(to, 4, from, (a8) - 12) \
  759. bn_cp_32(to, 5, from, (a7) - 12) \
  760. bn_cp_32(to, 6, from, (a6) - 12) \
  761. bn_cp_32(to, 7, from, (a5) - 12) \
  762. bn_cp_32(to, 8, from, (a4) - 12) \
  763. bn_cp_32(to, 9, from, (a3) - 12) \
  764. bn_cp_32(to, 10, from, (a2) - 12) \
  765. bn_cp_32(to, 11, from, (a1) - 12) \
  766. }
  767. int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  768. BN_CTX *ctx)
  769. {
  770. int i, top = a->top;
  771. int carry = 0;
  772. register BN_ULONG *r_d, *a_d = a->d;
  773. BN_ULONG buf[BN_NIST_384_TOP],
  774. c_d[BN_NIST_384_TOP],
  775. *res;
  776. PTR_SIZE_INT mask;
  777. union { bn_addsub_f f; PTR_SIZE_INT p; } u;
  778. static const BIGNUM _bignum_nist_p_384_sqr = {
  779. (BN_ULONG *)_nist_p_384_sqr,
  780. sizeof(_nist_p_384_sqr)/sizeof(_nist_p_384_sqr[0]),
  781. sizeof(_nist_p_384_sqr)/sizeof(_nist_p_384_sqr[0]),
  782. 0,BN_FLG_STATIC_DATA };
  783. field = &_bignum_nist_p_384; /* just to make sure */
  784. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_384_sqr)>=0)
  785. return BN_nnmod(r, a, field, ctx);
  786. i = BN_ucmp(field, a);
  787. if (i == 0)
  788. {
  789. BN_zero(r);
  790. return 1;
  791. }
  792. else if (i > 0)
  793. return (r == a)? 1 : (BN_copy(r ,a) != NULL);
  794. if (r != a)
  795. {
  796. if (!bn_wexpand(r, BN_NIST_384_TOP))
  797. return 0;
  798. r_d = r->d;
  799. nist_cp_bn(r_d, a_d, BN_NIST_384_TOP);
  800. }
  801. else
  802. r_d = a_d;
  803. nist_cp_bn_0(buf, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP, BN_NIST_384_TOP);
  804. #if defined(NIST_INT64)
  805. {
  806. NIST_INT64 acc; /* accumulator */
  807. unsigned int *rp=(unsigned int *)r_d;
  808. const unsigned int *bp=(const unsigned int *)buf;
  809. acc = rp[0]; acc += bp[12-12];
  810. acc += bp[21-12];
  811. acc += bp[20-12];
  812. acc -= bp[23-12]; rp[0] = (unsigned int)acc; acc >>= 32;
  813. acc += rp[1]; acc += bp[13-12];
  814. acc += bp[22-12];
  815. acc += bp[23-12];
  816. acc -= bp[12-12];
  817. acc -= bp[20-12]; rp[1] = (unsigned int)acc; acc >>= 32;
  818. acc += rp[2]; acc += bp[14-12];
  819. acc += bp[23-12];
  820. acc -= bp[13-12];
  821. acc -= bp[21-12]; rp[2] = (unsigned int)acc; acc >>= 32;
  822. acc += rp[3]; acc += bp[15-12];
  823. acc += bp[12-12];
  824. acc += bp[20-12];
  825. acc += bp[21-12];
  826. acc -= bp[14-12];
  827. acc -= bp[22-12];
  828. acc -= bp[23-12]; rp[3] = (unsigned int)acc; acc >>= 32;
  829. acc += rp[4]; acc += bp[21-12];
  830. acc += bp[21-12];
  831. acc += bp[16-12];
  832. acc += bp[13-12];
  833. acc += bp[12-12];
  834. acc += bp[20-12];
  835. acc += bp[22-12];
  836. acc -= bp[15-12];
  837. acc -= bp[23-12];
  838. acc -= bp[23-12]; rp[4] = (unsigned int)acc; acc >>= 32;
  839. acc += rp[5]; acc += bp[22-12];
  840. acc += bp[22-12];
  841. acc += bp[17-12];
  842. acc += bp[14-12];
  843. acc += bp[13-12];
  844. acc += bp[21-12];
  845. acc += bp[23-12];
  846. acc -= bp[16-12]; rp[5] = (unsigned int)acc; acc >>= 32;
  847. acc += rp[6]; acc += bp[23-12];
  848. acc += bp[23-12];
  849. acc += bp[18-12];
  850. acc += bp[15-12];
  851. acc += bp[14-12];
  852. acc += bp[22-12];
  853. acc -= bp[17-12]; rp[6] = (unsigned int)acc; acc >>= 32;
  854. acc += rp[7]; acc += bp[19-12];
  855. acc += bp[16-12];
  856. acc += bp[15-12];
  857. acc += bp[23-12];
  858. acc -= bp[18-12]; rp[7] = (unsigned int)acc; acc >>= 32;
  859. acc += rp[8]; acc += bp[20-12];
  860. acc += bp[17-12];
  861. acc += bp[16-12];
  862. acc -= bp[19-12]; rp[8] = (unsigned int)acc; acc >>= 32;
  863. acc += rp[9]; acc += bp[21-12];
  864. acc += bp[18-12];
  865. acc += bp[17-12];
  866. acc -= bp[20-12]; rp[9] = (unsigned int)acc; acc >>= 32;
  867. acc += rp[10]; acc += bp[22-12];
  868. acc += bp[19-12];
  869. acc += bp[18-12];
  870. acc -= bp[21-12]; rp[10] = (unsigned int)acc; acc >>= 32;
  871. acc += rp[11]; acc += bp[23-12];
  872. acc += bp[20-12];
  873. acc += bp[19-12];
  874. acc -= bp[22-12]; rp[11] = (unsigned int)acc;
  875. carry = (int)(acc>>32);
  876. }
  877. #else
  878. {
  879. BN_ULONG t_d[BN_NIST_384_TOP];
  880. /*S1*/
  881. nist_set_256(t_d, buf, 0, 0, 0, 0, 0, 23-4, 22-4, 21-4);
  882. /* left shift */
  883. {
  884. register BN_ULONG *ap,t,c;
  885. ap = t_d;
  886. c=0;
  887. for (i = 3; i != 0; --i)
  888. {
  889. t= *ap;
  890. *(ap++)=((t<<1)|c)&BN_MASK2;
  891. c=(t & BN_TBIT)?1:0;
  892. }
  893. *ap=c;
  894. }
  895. carry = (int)bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2),
  896. t_d, BN_NIST_256_TOP);
  897. /*S2 */
  898. carry += (int)bn_add_words(r_d, r_d, buf, BN_NIST_384_TOP);
  899. /*S3*/
  900. nist_set_384(t_d,buf,20,19,18,17,16,15,14,13,12,23,22,21);
  901. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  902. /*S4*/
  903. nist_set_384(t_d,buf,19,18,17,16,15,14,13,12,20,0,23,0);
  904. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  905. /*S5*/
  906. nist_set_384(t_d, buf,0,0,0,0,23,22,21,20,0,0,0,0);
  907. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  908. /*S6*/
  909. nist_set_384(t_d,buf,0,0,0,0,0,0,23,22,21,0,0,20);
  910. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  911. /*D1*/
  912. nist_set_384(t_d,buf,22,21,20,19,18,17,16,15,14,13,12,23);
  913. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  914. /*D2*/
  915. nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,22,21,20,0);
  916. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  917. /*D3*/
  918. nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,23,0,0,0);
  919. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  920. }
  921. #endif
  922. /* see BN_nist_mod_224 for explanation */
  923. u.f = bn_sub_words;
  924. if (carry > 0)
  925. carry = (int)bn_sub_words(r_d,r_d,_nist_p_384[carry-1],BN_NIST_384_TOP);
  926. else if (carry < 0)
  927. {
  928. carry = (int)bn_add_words(r_d,r_d,_nist_p_384[-carry-1],BN_NIST_384_TOP);
  929. mask = 0-(PTR_SIZE_INT)carry;
  930. u.p = ((PTR_SIZE_INT)bn_sub_words&mask) |
  931. ((PTR_SIZE_INT)bn_add_words&~mask);
  932. }
  933. else
  934. carry = 1;
  935. mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_384[0],BN_NIST_384_TOP);
  936. mask &= 0-(PTR_SIZE_INT)carry;
  937. res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) |
  938. ((PTR_SIZE_INT)r_d&mask));
  939. nist_cp_bn(r_d, res, BN_NIST_384_TOP);
  940. r->top = BN_NIST_384_TOP;
  941. bn_correct_top(r);
  942. return 1;
  943. }
  944. #ifdef _WIN32_WCE
  945. /* Workaround for compiler bug under CE */
  946. #pragma optimize( "", off )
  947. #endif
  948. #define BN_NIST_521_RSHIFT (521%BN_BITS2)
  949. #define BN_NIST_521_LSHIFT (BN_BITS2-BN_NIST_521_RSHIFT)
  950. #define BN_NIST_521_TOP_MASK ((BN_ULONG)BN_MASK2>>BN_NIST_521_LSHIFT)
  951. int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  952. BN_CTX *ctx)
  953. {
  954. int top = a->top, i;
  955. BN_ULONG *r_d, *a_d = a->d,
  956. t_d[BN_NIST_521_TOP],
  957. val,tmp,*res;
  958. PTR_SIZE_INT mask;
  959. static const BIGNUM _bignum_nist_p_521_sqr = {
  960. (BN_ULONG *)_nist_p_521_sqr,
  961. sizeof(_nist_p_521_sqr)/sizeof(_nist_p_521_sqr[0]),
  962. sizeof(_nist_p_521_sqr)/sizeof(_nist_p_521_sqr[0]),
  963. 0,BN_FLG_STATIC_DATA };
  964. field = &_bignum_nist_p_521; /* just to make sure */
  965. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_521_sqr)>=0)
  966. return BN_nnmod(r, a, field, ctx);
  967. i = BN_ucmp(field, a);
  968. if (i == 0)
  969. {
  970. BN_zero(r);
  971. return 1;
  972. }
  973. else if (i > 0)
  974. return (r == a)? 1 : (BN_copy(r ,a) != NULL);
  975. if (r != a)
  976. {
  977. if (!bn_wexpand(r,BN_NIST_521_TOP))
  978. return 0;
  979. r_d = r->d;
  980. nist_cp_bn(r_d,a_d, BN_NIST_521_TOP);
  981. }
  982. else
  983. r_d = a_d;
  984. /* upper 521 bits, copy ... */
  985. nist_cp_bn_0(t_d,a_d + (BN_NIST_521_TOP-1), top - (BN_NIST_521_TOP-1),BN_NIST_521_TOP);
  986. /* ... and right shift */
  987. for (val=t_d[0],i=0; i<BN_NIST_521_TOP-1; i++)
  988. {
  989. tmp = val>>BN_NIST_521_RSHIFT;
  990. val = t_d[i+1];
  991. t_d[i] = (tmp | val<<BN_NIST_521_LSHIFT) & BN_MASK2;
  992. }
  993. t_d[i] = val>>BN_NIST_521_RSHIFT;
  994. /* lower 521 bits */
  995. r_d[i] &= BN_NIST_521_TOP_MASK;
  996. bn_add_words(r_d,r_d,t_d,BN_NIST_521_TOP);
  997. mask = 0-(PTR_SIZE_INT)bn_sub_words(t_d,r_d,_nist_p_521,BN_NIST_521_TOP);
  998. res = (BN_ULONG *)(((PTR_SIZE_INT)t_d&~mask) |
  999. ((PTR_SIZE_INT)r_d&mask));
  1000. nist_cp_bn(r_d,res,BN_NIST_521_TOP);
  1001. r->top = BN_NIST_521_TOP;
  1002. bn_correct_top(r);
  1003. return 1;
  1004. }
  1005. #ifdef _WIN32_WCE
  1006. #pragma optimize( "", on )
  1007. #endif
  1008. int (*BN_nist_mod_func(const BIGNUM *p))(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, BN_CTX *ctx)
  1009. {
  1010. if (BN_ucmp(&_bignum_nist_p_192, p) == 0)
  1011. return BN_nist_mod_192;
  1012. if (BN_ucmp(&_bignum_nist_p_224, p) == 0)
  1013. return BN_nist_mod_224;
  1014. if (BN_ucmp(&_bignum_nist_p_256, p) == 0)
  1015. return BN_nist_mod_256;
  1016. if (BN_ucmp(&_bignum_nist_p_384, p) == 0)
  1017. return BN_nist_mod_384;
  1018. if (BN_ucmp(&_bignum_nist_p_521, p) == 0)
  1019. return BN_nist_mod_521;
  1020. return 0;
  1021. }