bn_nist.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. #define nist_set_192(to, from, a1, a2, a3) \
  348. { \
  349. bn_cp_64(to, 0, from, (a3) - 3) \
  350. bn_cp_64(to, 1, from, (a2) - 3) \
  351. bn_cp_64(to, 2, from, (a1) - 3) \
  352. }
  353. int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  354. BN_CTX *ctx)
  355. {
  356. int top = a->top, i;
  357. int carry;
  358. register BN_ULONG *r_d, *a_d = a->d;
  359. BN_ULONG buf[BN_NIST_192_TOP],
  360. c_d[BN_NIST_192_TOP],
  361. *res;
  362. PTR_SIZE_INT mask;
  363. static const BIGNUM _bignum_nist_p_192_sqr = {
  364. (BN_ULONG *)_nist_p_192_sqr,
  365. sizeof(_nist_p_192_sqr)/sizeof(_nist_p_192_sqr[0]),
  366. sizeof(_nist_p_192_sqr)/sizeof(_nist_p_192_sqr[0]),
  367. 0,BN_FLG_STATIC_DATA };
  368. field = &_bignum_nist_p_192; /* just to make sure */
  369. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_192_sqr)>=0)
  370. return BN_nnmod(r, a, field, ctx);
  371. i = BN_ucmp(field, a);
  372. if (i == 0)
  373. {
  374. BN_zero(r);
  375. return 1;
  376. }
  377. else if (i > 0)
  378. return (r == a) ? 1 : (BN_copy(r ,a) != NULL);
  379. if (r != a)
  380. {
  381. if (!bn_wexpand(r, BN_NIST_192_TOP))
  382. return 0;
  383. r_d = r->d;
  384. nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);
  385. }
  386. else
  387. r_d = a_d;
  388. nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP);
  389. #if defined(NIST_INT64)
  390. {
  391. NIST_INT64 acc; /* accumulator */
  392. unsigned int *rp=(unsigned int *)r_d;
  393. const unsigned int *bp=(const unsigned int *)buf;
  394. acc = rp[0]; acc += bp[3*2-6];
  395. acc += bp[5*2-6]; rp[0] = (unsigned int)acc; acc >>= 32;
  396. acc += rp[1]; acc += bp[3*2-5];
  397. acc += bp[5*2-5]; rp[1] = (unsigned int)acc; acc >>= 32;
  398. acc += rp[2]; acc += bp[3*2-6];
  399. acc += bp[4*2-6];
  400. acc += bp[5*2-6]; rp[2] = (unsigned int)acc; acc >>= 32;
  401. acc += rp[3]; acc += bp[3*2-5];
  402. acc += bp[4*2-5];
  403. acc += bp[5*2-5]; rp[3] = (unsigned int)acc; acc >>= 32;
  404. acc += rp[4]; acc += bp[4*2-6];
  405. acc += bp[5*2-6]; rp[4] = (unsigned int)acc; acc >>= 32;
  406. acc += rp[5]; acc += bp[4*2-5];
  407. acc += bp[5*2-5]; rp[5] = (unsigned int)acc;
  408. carry = (int)(acc>>32);
  409. }
  410. #else
  411. {
  412. BN_ULONG t_d[BN_NIST_192_TOP];
  413. nist_set_192(t_d, buf, 0, 3, 3);
  414. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  415. nist_set_192(t_d, buf, 4, 4, 0);
  416. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  417. nist_set_192(t_d, buf, 5, 5, 5)
  418. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  419. }
  420. #endif
  421. if (carry > 0)
  422. carry = (int)bn_sub_words(r_d,r_d,_nist_p_192[carry-1],BN_NIST_192_TOP);
  423. else
  424. carry = 1;
  425. /*
  426. * we need 'if (carry==0 || result>=modulus) result-=modulus;'
  427. * as comparison implies subtraction, we can write
  428. * 'tmp=result-modulus; if (!carry || !borrow) result=tmp;'
  429. * this is what happens below, but without explicit if:-) a.
  430. */
  431. mask = 0-(PTR_SIZE_INT)bn_sub_words(c_d,r_d,_nist_p_192[0],BN_NIST_192_TOP);
  432. mask &= 0-(PTR_SIZE_INT)carry;
  433. res = (BN_ULONG *)
  434. (((PTR_SIZE_INT)c_d&~mask) | ((PTR_SIZE_INT)r_d&mask));
  435. nist_cp_bn(r_d, res, BN_NIST_192_TOP);
  436. r->top = BN_NIST_192_TOP;
  437. bn_correct_top(r);
  438. return 1;
  439. }
  440. typedef BN_ULONG (*bn_addsub_f)(BN_ULONG *,const BN_ULONG *,const BN_ULONG *,int);
  441. #define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \
  442. { \
  443. bn_cp_32(to, 0, from, (a7) - 7) \
  444. bn_cp_32(to, 1, from, (a6) - 7) \
  445. bn_cp_32(to, 2, from, (a5) - 7) \
  446. bn_cp_32(to, 3, from, (a4) - 7) \
  447. bn_cp_32(to, 4, from, (a3) - 7) \
  448. bn_cp_32(to, 5, from, (a2) - 7) \
  449. bn_cp_32(to, 6, from, (a1) - 7) \
  450. }
  451. int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  452. BN_CTX *ctx)
  453. {
  454. int top = a->top, i;
  455. int carry;
  456. BN_ULONG *r_d, *a_d = a->d;
  457. BN_ULONG buf[BN_NIST_224_TOP],
  458. c_d[BN_NIST_224_TOP],
  459. *res;
  460. PTR_SIZE_INT mask;
  461. union { bn_addsub_f f; PTR_SIZE_INT p; } u;
  462. static const BIGNUM _bignum_nist_p_224_sqr = {
  463. (BN_ULONG *)_nist_p_224_sqr,
  464. sizeof(_nist_p_224_sqr)/sizeof(_nist_p_224_sqr[0]),
  465. sizeof(_nist_p_224_sqr)/sizeof(_nist_p_224_sqr[0]),
  466. 0,BN_FLG_STATIC_DATA };
  467. field = &_bignum_nist_p_224; /* just to make sure */
  468. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_224_sqr)>=0)
  469. return BN_nnmod(r, a, field, ctx);
  470. i = BN_ucmp(field, a);
  471. if (i == 0)
  472. {
  473. BN_zero(r);
  474. return 1;
  475. }
  476. else if (i > 0)
  477. return (r == a)? 1 : (BN_copy(r ,a) != NULL);
  478. if (r != a)
  479. {
  480. if (!bn_wexpand(r, BN_NIST_224_TOP))
  481. return 0;
  482. r_d = r->d;
  483. nist_cp_bn(r_d, a_d, BN_NIST_224_TOP);
  484. }
  485. else
  486. r_d = a_d;
  487. #if BN_BITS2==64
  488. /* copy upper 256 bits of 448 bit number ... */
  489. nist_cp_bn_0(c_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP);
  490. /* ... and right shift by 32 to obtain upper 224 bits */
  491. nist_set_224(buf, c_d, 14, 13, 12, 11, 10, 9, 8);
  492. /* truncate lower part to 224 bits too */
  493. r_d[BN_NIST_224_TOP-1] &= BN_MASK2l;
  494. #else
  495. nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);
  496. #endif
  497. #if defined(NIST_INT64) && BN_BITS2!=64
  498. {
  499. NIST_INT64 acc; /* accumulator */
  500. unsigned int *rp=(unsigned int *)r_d;
  501. const unsigned int *bp=(const unsigned int *)buf;
  502. acc = rp[0]; acc -= bp[7-7];
  503. acc -= bp[11-7]; rp[0] = (unsigned int)acc; acc >>= 32;
  504. acc += rp[1]; acc -= bp[8-7];
  505. acc -= bp[12-7]; rp[1] = (unsigned int)acc; acc >>= 32;
  506. acc += rp[2]; acc -= bp[9-7];
  507. acc -= bp[13-7]; rp[2] = (unsigned int)acc; acc >>= 32;
  508. acc += rp[3]; acc += bp[7-7];
  509. acc += bp[11-7];
  510. acc -= bp[10-7]; rp[3] = (unsigned int)acc; acc>>= 32;
  511. acc += rp[4]; acc += bp[8-7];
  512. acc += bp[12-7];
  513. acc -= bp[11-7]; rp[4] = (unsigned int)acc; acc >>= 32;
  514. acc += rp[5]; acc += bp[9-7];
  515. acc += bp[13-7];
  516. acc -= bp[12-7]; rp[5] = (unsigned int)acc; acc >>= 32;
  517. acc += rp[6]; acc += bp[10-7];
  518. acc -= bp[13-7]; rp[6] = (unsigned int)acc;
  519. carry = (int)(acc>>32);
  520. # if BN_BITS2==64
  521. rp[7] = carry;
  522. # endif
  523. }
  524. #else
  525. {
  526. BN_ULONG t_d[BN_NIST_224_TOP];
  527. nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0);
  528. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  529. nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0);
  530. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  531. nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7);
  532. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  533. nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11);
  534. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  535. #if BN_BITS2==64
  536. carry = (int)(r_d[BN_NIST_224_TOP-1]>>32);
  537. #endif
  538. }
  539. #endif
  540. u.f = bn_sub_words;
  541. if (carry > 0)
  542. {
  543. carry = (int)bn_sub_words(r_d,r_d,_nist_p_224[carry-1],BN_NIST_224_TOP);
  544. #if BN_BITS2==64
  545. carry=(int)(~(r_d[BN_NIST_224_TOP-1]>>32))&1;
  546. #endif
  547. }
  548. else if (carry < 0)
  549. {
  550. /* it's a bit more comlicated logic in this case.
  551. * if bn_add_words yields no carry, then result
  552. * has to be adjusted by unconditionally *adding*
  553. * the modulus. but if it does, then result has
  554. * to be compared to the modulus and conditionally
  555. * adjusted by *subtracting* the latter. */
  556. carry = (int)bn_add_words(r_d,r_d,_nist_p_224[-carry-1],BN_NIST_224_TOP);
  557. mask = 0-(PTR_SIZE_INT)carry;
  558. u.p = ((PTR_SIZE_INT)bn_sub_words&mask) |
  559. ((PTR_SIZE_INT)bn_add_words&~mask);
  560. }
  561. else
  562. carry = 1;
  563. /* otherwise it's effectively same as in BN_nist_mod_192... */
  564. mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_224[0],BN_NIST_224_TOP);
  565. mask &= 0-(PTR_SIZE_INT)carry;
  566. res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) |
  567. ((PTR_SIZE_INT)r_d&mask));
  568. nist_cp_bn(r_d, res, BN_NIST_224_TOP);
  569. r->top = BN_NIST_224_TOP;
  570. bn_correct_top(r);
  571. return 1;
  572. }
  573. #define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \
  574. { \
  575. bn_cp_32(to, 0, from, (a8) - 8) \
  576. bn_cp_32(to, 1, from, (a7) - 8) \
  577. bn_cp_32(to, 2, from, (a6) - 8) \
  578. bn_cp_32(to, 3, from, (a5) - 8) \
  579. bn_cp_32(to, 4, from, (a4) - 8) \
  580. bn_cp_32(to, 5, from, (a3) - 8) \
  581. bn_cp_32(to, 6, from, (a2) - 8) \
  582. bn_cp_32(to, 7, from, (a1) - 8) \
  583. }
  584. int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  585. BN_CTX *ctx)
  586. {
  587. int i, top = a->top;
  588. int carry = 0;
  589. register BN_ULONG *a_d = a->d, *r_d;
  590. BN_ULONG buf[BN_NIST_256_TOP],
  591. c_d[BN_NIST_256_TOP],
  592. *res;
  593. PTR_SIZE_INT mask;
  594. union { bn_addsub_f f; PTR_SIZE_INT p; } u;
  595. static const BIGNUM _bignum_nist_p_256_sqr = {
  596. (BN_ULONG *)_nist_p_256_sqr,
  597. sizeof(_nist_p_256_sqr)/sizeof(_nist_p_256_sqr[0]),
  598. sizeof(_nist_p_256_sqr)/sizeof(_nist_p_256_sqr[0]),
  599. 0,BN_FLG_STATIC_DATA };
  600. field = &_bignum_nist_p_256; /* just to make sure */
  601. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_256_sqr)>=0)
  602. return BN_nnmod(r, a, field, ctx);
  603. i = BN_ucmp(field, a);
  604. if (i == 0)
  605. {
  606. BN_zero(r);
  607. return 1;
  608. }
  609. else if (i > 0)
  610. return (r == a)? 1 : (BN_copy(r ,a) != NULL);
  611. if (r != a)
  612. {
  613. if (!bn_wexpand(r, BN_NIST_256_TOP))
  614. return 0;
  615. r_d = r->d;
  616. nist_cp_bn(r_d, a_d, BN_NIST_256_TOP);
  617. }
  618. else
  619. r_d = a_d;
  620. nist_cp_bn_0(buf, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP, BN_NIST_256_TOP);
  621. #if defined(NIST_INT64)
  622. {
  623. NIST_INT64 acc; /* accumulator */
  624. unsigned int *rp=(unsigned int *)r_d;
  625. const unsigned int *bp=(const unsigned int *)buf;
  626. acc = rp[0]; acc += bp[8-8];
  627. acc += bp[9-8];
  628. acc -= bp[11-8];
  629. acc -= bp[12-8];
  630. acc -= bp[13-8];
  631. acc -= bp[14-8]; rp[0] = (unsigned int)acc; acc >>= 32;
  632. acc += rp[1]; acc += bp[9-8];
  633. acc += bp[10-8];
  634. acc -= bp[12-8];
  635. acc -= bp[13-8];
  636. acc -= bp[14-8];
  637. acc -= bp[15-8]; rp[1] = (unsigned int)acc; acc >>= 32;
  638. acc += rp[2]; acc += bp[10-8];
  639. acc += bp[11-8];
  640. acc -= bp[13-8];
  641. acc -= bp[14-8];
  642. acc -= bp[15-8]; rp[2] = (unsigned int)acc; acc >>= 32;
  643. acc += rp[3]; acc += bp[11-8];
  644. acc += bp[11-8];
  645. acc += bp[12-8];
  646. acc += bp[12-8];
  647. acc += bp[13-8];
  648. acc -= bp[15-8];
  649. acc -= bp[8-8];
  650. acc -= bp[9-8]; rp[3] = (unsigned int)acc; acc >>= 32;
  651. acc += rp[4]; acc += bp[12-8];
  652. acc += bp[12-8];
  653. acc += bp[13-8];
  654. acc += bp[13-8];
  655. acc += bp[14-8];
  656. acc -= bp[9-8];
  657. acc -= bp[10-8]; rp[4] = (unsigned int)acc; acc >>= 32;
  658. acc += rp[5]; acc += bp[13-8];
  659. acc += bp[13-8];
  660. acc += bp[14-8];
  661. acc += bp[14-8];
  662. acc += bp[15-8];
  663. acc -= bp[10-8];
  664. acc -= bp[11-8]; rp[5] = (unsigned int)acc; acc >>= 32;
  665. acc += rp[6]; acc += bp[14-8];
  666. acc += bp[14-8];
  667. acc += bp[15-8];
  668. acc += bp[15-8];
  669. acc += bp[14-8];
  670. acc += bp[13-8];
  671. acc -= bp[8-8];
  672. acc -= bp[9-8]; rp[6] = (unsigned int)acc; acc >>= 32;
  673. acc += rp[7]; acc += bp[15-8];
  674. acc += bp[15-8];
  675. acc += bp[15-8];
  676. acc += bp[8 -8];
  677. acc -= bp[10-8];
  678. acc -= bp[11-8];
  679. acc -= bp[12-8];
  680. acc -= bp[13-8]; rp[7] = (unsigned int)acc;
  681. carry = (int)(acc>>32);
  682. }
  683. #else
  684. {
  685. BN_ULONG t_d[BN_NIST_256_TOP];
  686. /*S1*/
  687. nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0);
  688. /*S2*/
  689. nist_set_256(c_d, buf, 0, 15, 14, 13, 12, 0, 0, 0);
  690. carry = (int)bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
  691. /* left shift */
  692. {
  693. register BN_ULONG *ap,t,c;
  694. ap = t_d;
  695. c=0;
  696. for (i = BN_NIST_256_TOP; i != 0; --i)
  697. {
  698. t= *ap;
  699. *(ap++)=((t<<1)|c)&BN_MASK2;
  700. c=(t & BN_TBIT)?1:0;
  701. }
  702. carry <<= 1;
  703. carry |= c;
  704. }
  705. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  706. /*S3*/
  707. nist_set_256(t_d, buf, 15, 14, 0, 0, 0, 10, 9, 8);
  708. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  709. /*S4*/
  710. nist_set_256(t_d, buf, 8, 13, 15, 14, 13, 11, 10, 9);
  711. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  712. /*D1*/
  713. nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11);
  714. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  715. /*D2*/
  716. nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12);
  717. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  718. /*D3*/
  719. nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13);
  720. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  721. /*D4*/
  722. nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14);
  723. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  724. }
  725. #endif
  726. /* see BN_nist_mod_224 for explanation */
  727. u.f = bn_sub_words;
  728. if (carry > 0)
  729. carry = (int)bn_sub_words(r_d,r_d,_nist_p_256[carry-1],BN_NIST_256_TOP);
  730. else if (carry < 0)
  731. {
  732. carry = (int)bn_add_words(r_d,r_d,_nist_p_256[-carry-1],BN_NIST_256_TOP);
  733. mask = 0-(PTR_SIZE_INT)carry;
  734. u.p = ((PTR_SIZE_INT)bn_sub_words&mask) |
  735. ((PTR_SIZE_INT)bn_add_words&~mask);
  736. }
  737. else
  738. carry = 1;
  739. mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_256[0],BN_NIST_256_TOP);
  740. mask &= 0-(PTR_SIZE_INT)carry;
  741. res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) |
  742. ((PTR_SIZE_INT)r_d&mask));
  743. nist_cp_bn(r_d, res, BN_NIST_256_TOP);
  744. r->top = BN_NIST_256_TOP;
  745. bn_correct_top(r);
  746. return 1;
  747. }
  748. #define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
  749. { \
  750. bn_cp_32(to, 0, from, (a12) - 12) \
  751. bn_cp_32(to, 1, from, (a11) - 12) \
  752. bn_cp_32(to, 2, from, (a10) - 12) \
  753. bn_cp_32(to, 3, from, (a9) - 12) \
  754. bn_cp_32(to, 4, from, (a8) - 12) \
  755. bn_cp_32(to, 5, from, (a7) - 12) \
  756. bn_cp_32(to, 6, from, (a6) - 12) \
  757. bn_cp_32(to, 7, from, (a5) - 12) \
  758. bn_cp_32(to, 8, from, (a4) - 12) \
  759. bn_cp_32(to, 9, from, (a3) - 12) \
  760. bn_cp_32(to, 10, from, (a2) - 12) \
  761. bn_cp_32(to, 11, from, (a1) - 12) \
  762. }
  763. int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  764. BN_CTX *ctx)
  765. {
  766. int i, top = a->top;
  767. int carry = 0;
  768. register BN_ULONG *r_d, *a_d = a->d;
  769. BN_ULONG buf[BN_NIST_384_TOP],
  770. c_d[BN_NIST_384_TOP],
  771. *res;
  772. PTR_SIZE_INT mask;
  773. union { bn_addsub_f f; PTR_SIZE_INT p; } u;
  774. static const BIGNUM _bignum_nist_p_384_sqr = {
  775. (BN_ULONG *)_nist_p_384_sqr,
  776. sizeof(_nist_p_384_sqr)/sizeof(_nist_p_384_sqr[0]),
  777. sizeof(_nist_p_384_sqr)/sizeof(_nist_p_384_sqr[0]),
  778. 0,BN_FLG_STATIC_DATA };
  779. field = &_bignum_nist_p_384; /* just to make sure */
  780. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_384_sqr)>=0)
  781. return BN_nnmod(r, a, field, ctx);
  782. i = BN_ucmp(field, a);
  783. if (i == 0)
  784. {
  785. BN_zero(r);
  786. return 1;
  787. }
  788. else if (i > 0)
  789. return (r == a)? 1 : (BN_copy(r ,a) != NULL);
  790. if (r != a)
  791. {
  792. if (!bn_wexpand(r, BN_NIST_384_TOP))
  793. return 0;
  794. r_d = r->d;
  795. nist_cp_bn(r_d, a_d, BN_NIST_384_TOP);
  796. }
  797. else
  798. r_d = a_d;
  799. nist_cp_bn_0(buf, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP, BN_NIST_384_TOP);
  800. #if defined(NIST_INT64)
  801. {
  802. NIST_INT64 acc; /* accumulator */
  803. unsigned int *rp=(unsigned int *)r_d;
  804. const unsigned int *bp=(const unsigned int *)buf;
  805. acc = rp[0]; acc += bp[12-12];
  806. acc += bp[21-12];
  807. acc += bp[20-12];
  808. acc -= bp[23-12]; rp[0] = (unsigned int)acc; acc >>= 32;
  809. acc += rp[1]; acc += bp[13-12];
  810. acc += bp[22-12];
  811. acc += bp[23-12];
  812. acc -= bp[12-12];
  813. acc -= bp[20-12]; rp[1] = (unsigned int)acc; acc >>= 32;
  814. acc += rp[2]; acc += bp[14-12];
  815. acc += bp[23-12];
  816. acc -= bp[13-12];
  817. acc -= bp[21-12]; rp[2] = (unsigned int)acc; acc >>= 32;
  818. acc += rp[3]; acc += bp[15-12];
  819. acc += bp[12-12];
  820. acc += bp[20-12];
  821. acc += bp[21-12];
  822. acc -= bp[14-12];
  823. acc -= bp[22-12];
  824. acc -= bp[23-12]; rp[3] = (unsigned int)acc; acc >>= 32;
  825. acc += rp[4]; acc += bp[21-12];
  826. acc += bp[21-12];
  827. acc += bp[16-12];
  828. acc += bp[13-12];
  829. acc += bp[12-12];
  830. acc += bp[20-12];
  831. acc += bp[22-12];
  832. acc -= bp[15-12];
  833. acc -= bp[23-12];
  834. acc -= bp[23-12]; rp[4] = (unsigned int)acc; acc >>= 32;
  835. acc += rp[5]; acc += bp[22-12];
  836. acc += bp[22-12];
  837. acc += bp[17-12];
  838. acc += bp[14-12];
  839. acc += bp[13-12];
  840. acc += bp[21-12];
  841. acc += bp[23-12];
  842. acc -= bp[16-12]; rp[5] = (unsigned int)acc; acc >>= 32;
  843. acc += rp[6]; acc += bp[23-12];
  844. acc += bp[23-12];
  845. acc += bp[18-12];
  846. acc += bp[15-12];
  847. acc += bp[14-12];
  848. acc += bp[22-12];
  849. acc -= bp[17-12]; rp[6] = (unsigned int)acc; acc >>= 32;
  850. acc += rp[7]; acc += bp[19-12];
  851. acc += bp[16-12];
  852. acc += bp[15-12];
  853. acc += bp[23-12];
  854. acc -= bp[18-12]; rp[7] = (unsigned int)acc; acc >>= 32;
  855. acc += rp[8]; acc += bp[20-12];
  856. acc += bp[17-12];
  857. acc += bp[16-12];
  858. acc -= bp[19-12]; rp[8] = (unsigned int)acc; acc >>= 32;
  859. acc += rp[9]; acc += bp[21-12];
  860. acc += bp[18-12];
  861. acc += bp[17-12];
  862. acc -= bp[20-12]; rp[9] = (unsigned int)acc; acc >>= 32;
  863. acc += rp[10]; acc += bp[22-12];
  864. acc += bp[19-12];
  865. acc += bp[18-12];
  866. acc -= bp[21-12]; rp[10] = (unsigned int)acc; acc >>= 32;
  867. acc += rp[11]; acc += bp[23-12];
  868. acc += bp[20-12];
  869. acc += bp[19-12];
  870. acc -= bp[22-12]; rp[11] = (unsigned int)acc;
  871. carry = (int)(acc>>32);
  872. }
  873. #else
  874. {
  875. BN_ULONG t_d[BN_NIST_384_TOP];
  876. /*S1*/
  877. nist_set_256(t_d, buf, 0, 0, 0, 0, 0, 23-4, 22-4, 21-4);
  878. /* left shift */
  879. {
  880. register BN_ULONG *ap,t,c;
  881. ap = t_d;
  882. c=0;
  883. for (i = 3; i != 0; --i)
  884. {
  885. t= *ap;
  886. *(ap++)=((t<<1)|c)&BN_MASK2;
  887. c=(t & BN_TBIT)?1:0;
  888. }
  889. *ap=c;
  890. }
  891. carry = (int)bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2),
  892. t_d, BN_NIST_256_TOP);
  893. /*S2 */
  894. carry += (int)bn_add_words(r_d, r_d, buf, BN_NIST_384_TOP);
  895. /*S3*/
  896. nist_set_384(t_d,buf,20,19,18,17,16,15,14,13,12,23,22,21);
  897. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  898. /*S4*/
  899. nist_set_384(t_d,buf,19,18,17,16,15,14,13,12,20,0,23,0);
  900. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  901. /*S5*/
  902. nist_set_384(t_d, buf,0,0,0,0,23,22,21,20,0,0,0,0);
  903. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  904. /*S6*/
  905. nist_set_384(t_d,buf,0,0,0,0,0,0,23,22,21,0,0,20);
  906. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  907. /*D1*/
  908. nist_set_384(t_d,buf,22,21,20,19,18,17,16,15,14,13,12,23);
  909. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  910. /*D2*/
  911. nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,22,21,20,0);
  912. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  913. /*D3*/
  914. nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,23,0,0,0);
  915. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  916. }
  917. #endif
  918. /* see BN_nist_mod_224 for explanation */
  919. u.f = bn_sub_words;
  920. if (carry > 0)
  921. carry = (int)bn_sub_words(r_d,r_d,_nist_p_384[carry-1],BN_NIST_384_TOP);
  922. else if (carry < 0)
  923. {
  924. carry = (int)bn_add_words(r_d,r_d,_nist_p_384[-carry-1],BN_NIST_384_TOP);
  925. mask = 0-(PTR_SIZE_INT)carry;
  926. u.p = ((PTR_SIZE_INT)bn_sub_words&mask) |
  927. ((PTR_SIZE_INT)bn_add_words&~mask);
  928. }
  929. else
  930. carry = 1;
  931. mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_384[0],BN_NIST_384_TOP);
  932. mask &= 0-(PTR_SIZE_INT)carry;
  933. res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) |
  934. ((PTR_SIZE_INT)r_d&mask));
  935. nist_cp_bn(r_d, res, BN_NIST_384_TOP);
  936. r->top = BN_NIST_384_TOP;
  937. bn_correct_top(r);
  938. return 1;
  939. }
  940. #define BN_NIST_521_RSHIFT (521%BN_BITS2)
  941. #define BN_NIST_521_LSHIFT (BN_BITS2-BN_NIST_521_RSHIFT)
  942. #define BN_NIST_521_TOP_MASK ((BN_ULONG)BN_MASK2>>BN_NIST_521_LSHIFT)
  943. int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  944. BN_CTX *ctx)
  945. {
  946. int top = a->top, i;
  947. BN_ULONG *r_d, *a_d = a->d,
  948. t_d[BN_NIST_521_TOP],
  949. val,tmp,*res;
  950. PTR_SIZE_INT mask;
  951. static const BIGNUM _bignum_nist_p_521_sqr = {
  952. (BN_ULONG *)_nist_p_521_sqr,
  953. sizeof(_nist_p_521_sqr)/sizeof(_nist_p_521_sqr[0]),
  954. sizeof(_nist_p_521_sqr)/sizeof(_nist_p_521_sqr[0]),
  955. 0,BN_FLG_STATIC_DATA };
  956. field = &_bignum_nist_p_521; /* just to make sure */
  957. if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_521_sqr)>=0)
  958. return BN_nnmod(r, a, field, ctx);
  959. i = BN_ucmp(field, a);
  960. if (i == 0)
  961. {
  962. BN_zero(r);
  963. return 1;
  964. }
  965. else if (i > 0)
  966. return (r == a)? 1 : (BN_copy(r ,a) != NULL);
  967. if (r != a)
  968. {
  969. if (!bn_wexpand(r,BN_NIST_521_TOP))
  970. return 0;
  971. r_d = r->d;
  972. nist_cp_bn(r_d,a_d, BN_NIST_521_TOP);
  973. }
  974. else
  975. r_d = a_d;
  976. /* upper 521 bits, copy ... */
  977. nist_cp_bn_0(t_d,a_d + (BN_NIST_521_TOP-1), top - (BN_NIST_521_TOP-1),BN_NIST_521_TOP);
  978. /* ... and right shift */
  979. for (val=t_d[0],i=0; i<BN_NIST_521_TOP-1; i++)
  980. {
  981. tmp = val>>BN_NIST_521_RSHIFT;
  982. val = t_d[i+1];
  983. t_d[i] = (tmp | val<<BN_NIST_521_LSHIFT) & BN_MASK2;
  984. }
  985. t_d[i] = val>>BN_NIST_521_RSHIFT;
  986. /* lower 521 bits */
  987. r_d[i] &= BN_NIST_521_TOP_MASK;
  988. bn_add_words(r_d,r_d,t_d,BN_NIST_521_TOP);
  989. mask = 0-(PTR_SIZE_INT)bn_sub_words(t_d,r_d,_nist_p_521,BN_NIST_521_TOP);
  990. res = (BN_ULONG *)(((PTR_SIZE_INT)t_d&~mask) |
  991. ((PTR_SIZE_INT)r_d&mask));
  992. nist_cp_bn(r_d,res,BN_NIST_521_TOP);
  993. r->top = BN_NIST_521_TOP;
  994. bn_correct_top(r);
  995. return 1;
  996. }
  997. int (*BN_nist_mod_func(const BIGNUM *p))(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, BN_CTX *ctx)
  998. {
  999. if (BN_ucmp(&_bignum_nist_p_192, p) == 0)
  1000. return BN_nist_mod_192;
  1001. if (BN_ucmp(&_bignum_nist_p_224, p) == 0)
  1002. return BN_nist_mod_224;
  1003. if (BN_ucmp(&_bignum_nist_p_256, p) == 0)
  1004. return BN_nist_mod_256;
  1005. if (BN_ucmp(&_bignum_nist_p_384, p) == 0)
  1006. return BN_nist_mod_384;
  1007. if (BN_ucmp(&_bignum_nist_p_521, p) == 0)
  1008. return BN_nist_mod_521;
  1009. return 0;
  1010. }