bn_nist.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
  68. {0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFEULL, 0xFFFFFFFFFFFFFFFFULL},
  69. {0xFFFFFFFFFFFFFFFEULL, 0xFFFFFFFFFFFFFFFDULL, 0xFFFFFFFFFFFFFFFFULL},
  70. {0xFFFFFFFFFFFFFFFDULL, 0xFFFFFFFFFFFFFFFCULL, 0xFFFFFFFFFFFFFFFFULL}
  71. };
  72. static const BN_ULONG _nist_p_192_sqr[] = {
  73. 0x0000000000000001ULL, 0x0000000000000002ULL, 0x0000000000000001ULL,
  74. 0xFFFFFFFFFFFFFFFEULL, 0xFFFFFFFFFFFFFFFDULL, 0xFFFFFFFFFFFFFFFFULL
  75. };
  76. static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
  77. {0x0000000000000001ULL, 0xFFFFFFFF00000000ULL,
  78. 0xFFFFFFFFFFFFFFFFULL, 0x00000000FFFFFFFFULL},
  79. {0x0000000000000002ULL, 0xFFFFFFFE00000000ULL,
  80. 0xFFFFFFFFFFFFFFFFULL, 0x00000001FFFFFFFFULL} /* this one is
  81. * "carry-full" */
  82. };
  83. static const BN_ULONG _nist_p_224_sqr[] = {
  84. 0x0000000000000001ULL, 0xFFFFFFFE00000000ULL,
  85. 0xFFFFFFFFFFFFFFFFULL, 0x0000000200000000ULL,
  86. 0x0000000000000000ULL, 0xFFFFFFFFFFFFFFFEULL,
  87. 0xFFFFFFFFFFFFFFFFULL
  88. };
  89. static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
  90. {0xFFFFFFFFFFFFFFFFULL, 0x00000000FFFFFFFFULL,
  91. 0x0000000000000000ULL, 0xFFFFFFFF00000001ULL},
  92. {0xFFFFFFFFFFFFFFFEULL, 0x00000001FFFFFFFFULL,
  93. 0x0000000000000000ULL, 0xFFFFFFFE00000002ULL},
  94. {0xFFFFFFFFFFFFFFFDULL, 0x00000002FFFFFFFFULL,
  95. 0x0000000000000000ULL, 0xFFFFFFFD00000003ULL},
  96. {0xFFFFFFFFFFFFFFFCULL, 0x00000003FFFFFFFFULL,
  97. 0x0000000000000000ULL, 0xFFFFFFFC00000004ULL},
  98. {0xFFFFFFFFFFFFFFFBULL, 0x00000004FFFFFFFFULL,
  99. 0x0000000000000000ULL, 0xFFFFFFFB00000005ULL},
  100. };
  101. static const BN_ULONG _nist_p_256_sqr[] = {
  102. 0x0000000000000001ULL, 0xFFFFFFFE00000000ULL,
  103. 0xFFFFFFFFFFFFFFFFULL, 0x00000001FFFFFFFEULL,
  104. 0x00000001FFFFFFFEULL, 0x00000001FFFFFFFEULL,
  105. 0xFFFFFFFE00000001ULL, 0xFFFFFFFE00000002ULL
  106. };
  107. static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
  108. {0x00000000FFFFFFFFULL, 0xFFFFFFFF00000000ULL, 0xFFFFFFFFFFFFFFFEULL,
  109. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  110. {0x00000001FFFFFFFEULL, 0xFFFFFFFE00000000ULL, 0xFFFFFFFFFFFFFFFDULL,
  111. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  112. {0x00000002FFFFFFFDULL, 0xFFFFFFFD00000000ULL, 0xFFFFFFFFFFFFFFFCULL,
  113. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  114. {0x00000003FFFFFFFCULL, 0xFFFFFFFC00000000ULL, 0xFFFFFFFFFFFFFFFBULL,
  115. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  116. {0x00000004FFFFFFFBULL, 0xFFFFFFFB00000000ULL, 0xFFFFFFFFFFFFFFFAULL,
  117. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL},
  118. };
  119. static const BN_ULONG _nist_p_384_sqr[] = {
  120. 0xFFFFFFFE00000001ULL, 0x0000000200000000ULL, 0xFFFFFFFE00000000ULL,
  121. 0x0000000200000000ULL, 0x0000000000000001ULL, 0x0000000000000000ULL,
  122. 0x00000001FFFFFFFEULL, 0xFFFFFFFE00000000ULL, 0xFFFFFFFFFFFFFFFDULL,
  123. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL
  124. };
  125. static const BN_ULONG _nist_p_521[] =
  126. { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  127. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  128. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  129. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  130. 0x00000000000001FFULL
  131. };
  132. static const BN_ULONG _nist_p_521_sqr[] = {
  133. 0x0000000000000001ULL, 0x0000000000000000ULL, 0x0000000000000000ULL,
  134. 0x0000000000000000ULL, 0x0000000000000000ULL, 0x0000000000000000ULL,
  135. 0x0000000000000000ULL, 0x0000000000000000ULL, 0xFFFFFFFFFFFFFC00ULL,
  136. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  137. 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
  138. 0xFFFFFFFFFFFFFFFFULL, 0x000000000003FFFFULL
  139. };
  140. #elif BN_BITS2 == 32
  141. static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
  142. {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  143. {0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  144. {0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFC, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  145. };
  146. static const BN_ULONG _nist_p_192_sqr[] = {
  147. 0x00000001, 0x00000000, 0x00000002, 0x00000000, 0x00000001, 0x00000000,
  148. 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  149. };
  150. static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
  151. {0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFF,
  152. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  153. {0x00000002, 0x00000000, 0x00000000, 0xFFFFFFFE,
  154. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  155. };
  156. static const BN_ULONG _nist_p_224_sqr[] = {
  157. 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE,
  158. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000002,
  159. 0x00000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFF,
  160. 0xFFFFFFFF, 0xFFFFFFFF
  161. };
  162. static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
  163. {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000,
  164. 0x00000000, 0x00000000, 0x00000001, 0xFFFFFFFF},
  165. {0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000001,
  166. 0x00000000, 0x00000000, 0x00000002, 0xFFFFFFFE},
  167. {0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000002,
  168. 0x00000000, 0x00000000, 0x00000003, 0xFFFFFFFD},
  169. {0xFFFFFFFC, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000003,
  170. 0x00000000, 0x00000000, 0x00000004, 0xFFFFFFFC},
  171. {0xFFFFFFFB, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000004,
  172. 0x00000000, 0x00000000, 0x00000005, 0xFFFFFFFB},
  173. };
  174. static const BN_ULONG _nist_p_256_sqr[] = {
  175. 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE,
  176. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0x00000001,
  177. 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001,
  178. 0x00000001, 0xFFFFFFFE, 0x00000002, 0xFFFFFFFE
  179. };
  180. static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
  181. {0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF,
  182. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  183. {0xFFFFFFFE, 0x00000001, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFF,
  184. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  185. {0xFFFFFFFD, 0x00000002, 0x00000000, 0xFFFFFFFD, 0xFFFFFFFC, 0xFFFFFFFF,
  186. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  187. {0xFFFFFFFC, 0x00000003, 0x00000000, 0xFFFFFFFC, 0xFFFFFFFB, 0xFFFFFFFF,
  188. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  189. {0xFFFFFFFB, 0x00000004, 0x00000000, 0xFFFFFFFB, 0xFFFFFFFA, 0xFFFFFFFF,
  190. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
  191. };
  192. static const BN_ULONG _nist_p_384_sqr[] = {
  193. 0x00000001, 0xFFFFFFFE, 0x00000000, 0x00000002, 0x00000000, 0xFFFFFFFE,
  194. 0x00000000, 0x00000002, 0x00000001, 0x00000000, 0x00000000, 0x00000000,
  195. 0xFFFFFFFE, 0x00000001, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFF,
  196. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  197. };
  198. static const BN_ULONG _nist_p_521[] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  199. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  200. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  201. 0xFFFFFFFF, 0x000001FF
  202. };
  203. static const BN_ULONG _nist_p_521_sqr[] = {
  204. 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  205. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  206. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xFFFFFC00, 0xFFFFFFFF,
  207. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  208. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  209. 0xFFFFFFFF, 0xFFFFFFFF, 0x0003FFFF
  210. };
  211. #else
  212. # error "unsupported BN_BITS2"
  213. #endif
  214. static const BIGNUM _bignum_nist_p_192 = {
  215. (BN_ULONG *)_nist_p_192[0],
  216. BN_NIST_192_TOP,
  217. BN_NIST_192_TOP,
  218. 0,
  219. BN_FLG_STATIC_DATA
  220. };
  221. static const BIGNUM _bignum_nist_p_224 = {
  222. (BN_ULONG *)_nist_p_224[0],
  223. BN_NIST_224_TOP,
  224. BN_NIST_224_TOP,
  225. 0,
  226. BN_FLG_STATIC_DATA
  227. };
  228. static const BIGNUM _bignum_nist_p_256 = {
  229. (BN_ULONG *)_nist_p_256[0],
  230. BN_NIST_256_TOP,
  231. BN_NIST_256_TOP,
  232. 0,
  233. BN_FLG_STATIC_DATA
  234. };
  235. static const BIGNUM _bignum_nist_p_384 = {
  236. (BN_ULONG *)_nist_p_384[0],
  237. BN_NIST_384_TOP,
  238. BN_NIST_384_TOP,
  239. 0,
  240. BN_FLG_STATIC_DATA
  241. };
  242. static const BIGNUM _bignum_nist_p_521 = {
  243. (BN_ULONG *)_nist_p_521,
  244. BN_NIST_521_TOP,
  245. BN_NIST_521_TOP,
  246. 0,
  247. BN_FLG_STATIC_DATA
  248. };
  249. const BIGNUM *BN_get0_nist_prime_192(void)
  250. {
  251. return &_bignum_nist_p_192;
  252. }
  253. const BIGNUM *BN_get0_nist_prime_224(void)
  254. {
  255. return &_bignum_nist_p_224;
  256. }
  257. const BIGNUM *BN_get0_nist_prime_256(void)
  258. {
  259. return &_bignum_nist_p_256;
  260. }
  261. const BIGNUM *BN_get0_nist_prime_384(void)
  262. {
  263. return &_bignum_nist_p_384;
  264. }
  265. const BIGNUM *BN_get0_nist_prime_521(void)
  266. {
  267. return &_bignum_nist_p_521;
  268. }
  269. static void nist_cp_bn_0(BN_ULONG *buf, BN_ULONG *a, int top, int max)
  270. {
  271. int i;
  272. BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
  273. #ifdef BN_DEBUG
  274. OPENSSL_assert(top <= max);
  275. #endif
  276. for (i = (top); i != 0; i--)
  277. *_tmp1++ = *_tmp2++;
  278. for (i = (max) - (top); i != 0; i--)
  279. *_tmp1++ = (BN_ULONG)0;
  280. }
  281. static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top)
  282. {
  283. int i;
  284. BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
  285. for (i = (top); i != 0; i--)
  286. *_tmp1++ = *_tmp2++;
  287. }
  288. #if BN_BITS2 == 64
  289. # define bn_cp_64(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
  290. # define bn_64_set_0(to, n) (to)[n] = (BN_ULONG)0;
  291. /*
  292. * two following macros are implemented under assumption that they
  293. * are called in a sequence with *ascending* n, i.e. as they are...
  294. */
  295. # 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))\
  296. :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l)))
  297. # define bn_32_set_0(to, n) (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0));
  298. # define bn_cp_32(to,n,from,m) ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n)
  299. #else
  300. # define bn_cp_64(to, n, from, m) \
  301. { \
  302. bn_cp_32(to, (n)*2, from, (m)*2); \
  303. bn_cp_32(to, (n)*2+1, from, (m)*2+1); \
  304. }
  305. # define bn_64_set_0(to, n) \
  306. { \
  307. bn_32_set_0(to, (n)*2); \
  308. bn_32_set_0(to, (n)*2+1); \
  309. }
  310. # if BN_BITS2 == 32
  311. # define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
  312. # define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0;
  313. # endif
  314. #endif /* BN_BITS2 != 64 */
  315. #define nist_set_192(to, from, a1, a2, a3) \
  316. { \
  317. bn_cp_64(to, 0, from, (a3) - 3) \
  318. bn_cp_64(to, 1, from, (a2) - 3) \
  319. bn_cp_64(to, 2, from, (a1) - 3) \
  320. }
  321. int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  322. BN_CTX *ctx)
  323. {
  324. int top = a->top, i;
  325. int carry;
  326. register BN_ULONG *r_d, *a_d = a->d;
  327. BN_ULONG t_d[BN_NIST_192_TOP],
  328. buf[BN_NIST_192_TOP], c_d[BN_NIST_192_TOP], *res;
  329. size_t mask;
  330. static const BIGNUM _bignum_nist_p_192_sqr = {
  331. (BN_ULONG *)_nist_p_192_sqr,
  332. sizeof(_nist_p_192_sqr) / sizeof(_nist_p_192_sqr[0]),
  333. sizeof(_nist_p_192_sqr) / sizeof(_nist_p_192_sqr[0]),
  334. 0, BN_FLG_STATIC_DATA
  335. };
  336. field = &_bignum_nist_p_192; /* just to make sure */
  337. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_192_sqr) >= 0)
  338. return BN_nnmod(r, a, field, ctx);
  339. i = BN_ucmp(field, a);
  340. if (i == 0) {
  341. BN_zero(r);
  342. return 1;
  343. } else if (i > 0)
  344. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  345. if (r != a) {
  346. if (!bn_wexpand(r, BN_NIST_192_TOP))
  347. return 0;
  348. r_d = r->d;
  349. nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);
  350. } else
  351. r_d = a_d;
  352. nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP,
  353. BN_NIST_192_TOP);
  354. nist_set_192(t_d, buf, 0, 3, 3);
  355. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  356. nist_set_192(t_d, buf, 4, 4, 0);
  357. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  358. nist_set_192(t_d, buf, 5, 5, 5)
  359. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  360. if (carry > 0)
  361. carry =
  362. (int)bn_sub_words(r_d, r_d, _nist_p_192[carry - 1],
  363. BN_NIST_192_TOP);
  364. else
  365. carry = 1;
  366. /*
  367. * we need 'if (carry==0 || result>=modulus) result-=modulus;'
  368. * as comparison implies subtraction, we can write
  369. * 'tmp=result-modulus; if (!carry || !borrow) result=tmp;'
  370. * this is what happens below, but without explicit if:-) a.
  371. */
  372. mask =
  373. 0 - (size_t)bn_sub_words(c_d, r_d, _nist_p_192[0], BN_NIST_192_TOP);
  374. mask &= 0 - (size_t)carry;
  375. res = (BN_ULONG *)(((size_t)c_d & ~mask) | ((size_t)r_d & mask));
  376. nist_cp_bn(r_d, res, BN_NIST_192_TOP);
  377. r->top = BN_NIST_192_TOP;
  378. bn_correct_top(r);
  379. return 1;
  380. }
  381. typedef BN_ULONG (*bn_addsub_f) (BN_ULONG *, const BN_ULONG *,
  382. const BN_ULONG *, int);
  383. #define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \
  384. { \
  385. bn_cp_32(to, 0, from, (a7) - 7) \
  386. bn_cp_32(to, 1, from, (a6) - 7) \
  387. bn_cp_32(to, 2, from, (a5) - 7) \
  388. bn_cp_32(to, 3, from, (a4) - 7) \
  389. bn_cp_32(to, 4, from, (a3) - 7) \
  390. bn_cp_32(to, 5, from, (a2) - 7) \
  391. bn_cp_32(to, 6, from, (a1) - 7) \
  392. }
  393. int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  394. BN_CTX *ctx)
  395. {
  396. int top = a->top, i;
  397. int carry;
  398. BN_ULONG *r_d, *a_d = a->d;
  399. BN_ULONG t_d[BN_NIST_224_TOP],
  400. buf[BN_NIST_224_TOP], c_d[BN_NIST_224_TOP], *res;
  401. size_t mask;
  402. union {
  403. bn_addsub_f f;
  404. size_t p;
  405. } u;
  406. static const BIGNUM _bignum_nist_p_224_sqr = {
  407. (BN_ULONG *)_nist_p_224_sqr,
  408. sizeof(_nist_p_224_sqr) / sizeof(_nist_p_224_sqr[0]),
  409. sizeof(_nist_p_224_sqr) / sizeof(_nist_p_224_sqr[0]),
  410. 0, BN_FLG_STATIC_DATA
  411. };
  412. field = &_bignum_nist_p_224; /* just to make sure */
  413. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_224_sqr) >= 0)
  414. return BN_nnmod(r, a, field, ctx);
  415. i = BN_ucmp(field, a);
  416. if (i == 0) {
  417. BN_zero(r);
  418. return 1;
  419. } else if (i > 0)
  420. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  421. if (r != a) {
  422. if (!bn_wexpand(r, BN_NIST_224_TOP))
  423. return 0;
  424. r_d = r->d;
  425. nist_cp_bn(r_d, a_d, BN_NIST_224_TOP);
  426. } else
  427. r_d = a_d;
  428. #if BN_BITS2==64
  429. /* copy upper 256 bits of 448 bit number ... */
  430. nist_cp_bn_0(t_d, a_d + (BN_NIST_224_TOP - 1),
  431. top - (BN_NIST_224_TOP - 1), BN_NIST_224_TOP);
  432. /* ... and right shift by 32 to obtain upper 224 bits */
  433. nist_set_224(buf, t_d, 14, 13, 12, 11, 10, 9, 8);
  434. /* truncate lower part to 224 bits too */
  435. r_d[BN_NIST_224_TOP - 1] &= BN_MASK2l;
  436. #else
  437. nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP,
  438. BN_NIST_224_TOP);
  439. #endif
  440. nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0);
  441. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  442. nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0);
  443. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  444. nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7);
  445. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  446. nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11);
  447. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  448. #if BN_BITS2==64
  449. carry = (int)(r_d[BN_NIST_224_TOP - 1] >> 32);
  450. #endif
  451. u.f = bn_sub_words;
  452. if (carry > 0) {
  453. carry =
  454. (int)bn_sub_words(r_d, r_d, _nist_p_224[carry - 1],
  455. BN_NIST_224_TOP);
  456. #if BN_BITS2==64
  457. carry = (int)(~(r_d[BN_NIST_224_TOP - 1] >> 32)) & 1;
  458. #endif
  459. } else if (carry < 0) {
  460. /*
  461. * it's a bit more comlicated logic in this case. if bn_add_words
  462. * yields no carry, then result has to be adjusted by unconditionally
  463. * *adding* the modulus. but if it does, then result has to be
  464. * compared to the modulus and conditionally adjusted by
  465. * *subtracting* the latter.
  466. */
  467. carry =
  468. (int)bn_add_words(r_d, r_d, _nist_p_224[-carry - 1],
  469. BN_NIST_224_TOP);
  470. mask = 0 - (size_t)carry;
  471. u.p = ((size_t)bn_sub_words & mask) | ((size_t)bn_add_words & ~mask);
  472. } else
  473. carry = 1;
  474. /* otherwise it's effectively same as in BN_nist_mod_192... */
  475. mask = 0 - (size_t)(*u.f) (c_d, r_d, _nist_p_224[0], BN_NIST_224_TOP);
  476. mask &= 0 - (size_t)carry;
  477. res = (BN_ULONG *)(((size_t)c_d & ~mask) | ((size_t)r_d & mask));
  478. nist_cp_bn(r_d, res, BN_NIST_224_TOP);
  479. r->top = BN_NIST_224_TOP;
  480. bn_correct_top(r);
  481. return 1;
  482. }
  483. #define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \
  484. { \
  485. bn_cp_32(to, 0, from, (a8) - 8) \
  486. bn_cp_32(to, 1, from, (a7) - 8) \
  487. bn_cp_32(to, 2, from, (a6) - 8) \
  488. bn_cp_32(to, 3, from, (a5) - 8) \
  489. bn_cp_32(to, 4, from, (a4) - 8) \
  490. bn_cp_32(to, 5, from, (a3) - 8) \
  491. bn_cp_32(to, 6, from, (a2) - 8) \
  492. bn_cp_32(to, 7, from, (a1) - 8) \
  493. }
  494. int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  495. BN_CTX *ctx)
  496. {
  497. int i, top = a->top;
  498. int carry = 0;
  499. register BN_ULONG *a_d = a->d, *r_d;
  500. BN_ULONG t_d[BN_NIST_256_TOP],
  501. buf[BN_NIST_256_TOP], c_d[BN_NIST_256_TOP], *res;
  502. size_t mask;
  503. union {
  504. bn_addsub_f f;
  505. size_t p;
  506. } u;
  507. static const BIGNUM _bignum_nist_p_256_sqr = {
  508. (BN_ULONG *)_nist_p_256_sqr,
  509. sizeof(_nist_p_256_sqr) / sizeof(_nist_p_256_sqr[0]),
  510. sizeof(_nist_p_256_sqr) / sizeof(_nist_p_256_sqr[0]),
  511. 0, BN_FLG_STATIC_DATA
  512. };
  513. field = &_bignum_nist_p_256; /* just to make sure */
  514. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_256_sqr) >= 0)
  515. return BN_nnmod(r, a, field, ctx);
  516. i = BN_ucmp(field, a);
  517. if (i == 0) {
  518. BN_zero(r);
  519. return 1;
  520. } else if (i > 0)
  521. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  522. if (r != a) {
  523. if (!bn_wexpand(r, BN_NIST_256_TOP))
  524. return 0;
  525. r_d = r->d;
  526. nist_cp_bn(r_d, a_d, BN_NIST_256_TOP);
  527. } else
  528. r_d = a_d;
  529. nist_cp_bn_0(buf, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP,
  530. BN_NIST_256_TOP);
  531. /*
  532. * S1
  533. */
  534. nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0);
  535. /*
  536. * S2
  537. */
  538. nist_set_256(c_d, buf, 0, 15, 14, 13, 12, 0, 0, 0);
  539. carry = (int)bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
  540. /* left shift */
  541. {
  542. register BN_ULONG *ap, t, c;
  543. ap = t_d;
  544. c = 0;
  545. for (i = BN_NIST_256_TOP; i != 0; --i) {
  546. t = *ap;
  547. *(ap++) = ((t << 1) | c) & BN_MASK2;
  548. c = (t & BN_TBIT) ? 1 : 0;
  549. }
  550. carry <<= 1;
  551. carry |= c;
  552. }
  553. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  554. /*
  555. * S3
  556. */
  557. nist_set_256(t_d, buf, 15, 14, 0, 0, 0, 10, 9, 8);
  558. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  559. /*
  560. * S4
  561. */
  562. nist_set_256(t_d, buf, 8, 13, 15, 14, 13, 11, 10, 9);
  563. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  564. /*
  565. * D1
  566. */
  567. nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11);
  568. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  569. /*
  570. * D2
  571. */
  572. nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12);
  573. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  574. /*
  575. * D3
  576. */
  577. nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13);
  578. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  579. /*
  580. * D4
  581. */
  582. nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14);
  583. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  584. /* see BN_nist_mod_224 for explanation */
  585. u.f = bn_sub_words;
  586. if (carry > 0)
  587. carry =
  588. (int)bn_sub_words(r_d, r_d, _nist_p_256[carry - 1],
  589. BN_NIST_256_TOP);
  590. else if (carry < 0) {
  591. carry =
  592. (int)bn_add_words(r_d, r_d, _nist_p_256[-carry - 1],
  593. BN_NIST_256_TOP);
  594. mask = 0 - (size_t)carry;
  595. u.p = ((size_t)bn_sub_words & mask) | ((size_t)bn_add_words & ~mask);
  596. } else
  597. carry = 1;
  598. mask = 0 - (size_t)(*u.f) (c_d, r_d, _nist_p_256[0], BN_NIST_256_TOP);
  599. mask &= 0 - (size_t)carry;
  600. res = (BN_ULONG *)(((size_t)c_d & ~mask) | ((size_t)r_d & mask));
  601. nist_cp_bn(r_d, res, BN_NIST_256_TOP);
  602. r->top = BN_NIST_256_TOP;
  603. bn_correct_top(r);
  604. return 1;
  605. }
  606. #define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
  607. { \
  608. bn_cp_32(to, 0, from, (a12) - 12) \
  609. bn_cp_32(to, 1, from, (a11) - 12) \
  610. bn_cp_32(to, 2, from, (a10) - 12) \
  611. bn_cp_32(to, 3, from, (a9) - 12) \
  612. bn_cp_32(to, 4, from, (a8) - 12) \
  613. bn_cp_32(to, 5, from, (a7) - 12) \
  614. bn_cp_32(to, 6, from, (a6) - 12) \
  615. bn_cp_32(to, 7, from, (a5) - 12) \
  616. bn_cp_32(to, 8, from, (a4) - 12) \
  617. bn_cp_32(to, 9, from, (a3) - 12) \
  618. bn_cp_32(to, 10, from, (a2) - 12) \
  619. bn_cp_32(to, 11, from, (a1) - 12) \
  620. }
  621. int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  622. BN_CTX *ctx)
  623. {
  624. int i, top = a->top;
  625. int carry = 0;
  626. register BN_ULONG *r_d, *a_d = a->d;
  627. BN_ULONG t_d[BN_NIST_384_TOP],
  628. buf[BN_NIST_384_TOP], c_d[BN_NIST_384_TOP], *res;
  629. size_t mask;
  630. union {
  631. bn_addsub_f f;
  632. size_t p;
  633. } u;
  634. static const BIGNUM _bignum_nist_p_384_sqr = {
  635. (BN_ULONG *)_nist_p_384_sqr,
  636. sizeof(_nist_p_384_sqr) / sizeof(_nist_p_384_sqr[0]),
  637. sizeof(_nist_p_384_sqr) / sizeof(_nist_p_384_sqr[0]),
  638. 0, BN_FLG_STATIC_DATA
  639. };
  640. field = &_bignum_nist_p_384; /* just to make sure */
  641. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_384_sqr) >= 0)
  642. return BN_nnmod(r, a, field, ctx);
  643. i = BN_ucmp(field, a);
  644. if (i == 0) {
  645. BN_zero(r);
  646. return 1;
  647. } else if (i > 0)
  648. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  649. if (r != a) {
  650. if (!bn_wexpand(r, BN_NIST_384_TOP))
  651. return 0;
  652. r_d = r->d;
  653. nist_cp_bn(r_d, a_d, BN_NIST_384_TOP);
  654. } else
  655. r_d = a_d;
  656. nist_cp_bn_0(buf, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP,
  657. BN_NIST_384_TOP);
  658. /*
  659. * S1
  660. */
  661. nist_set_256(t_d, buf, 0, 0, 0, 0, 0, 23 - 4, 22 - 4, 21 - 4);
  662. /* left shift */
  663. {
  664. register BN_ULONG *ap, t, c;
  665. ap = t_d;
  666. c = 0;
  667. for (i = 3; i != 0; --i) {
  668. t = *ap;
  669. *(ap++) = ((t << 1) | c) & BN_MASK2;
  670. c = (t & BN_TBIT) ? 1 : 0;
  671. }
  672. *ap = c;
  673. }
  674. carry = (int)bn_add_words(r_d + (128 / BN_BITS2), r_d + (128 / BN_BITS2),
  675. t_d, BN_NIST_256_TOP);
  676. /*
  677. * S2
  678. */
  679. carry += (int)bn_add_words(r_d, r_d, buf, BN_NIST_384_TOP);
  680. /*
  681. * S3
  682. */
  683. nist_set_384(t_d, buf, 20, 19, 18, 17, 16, 15, 14, 13, 12, 23, 22, 21);
  684. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  685. /*
  686. * S4
  687. */
  688. nist_set_384(t_d, buf, 19, 18, 17, 16, 15, 14, 13, 12, 20, 0, 23, 0);
  689. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  690. /*
  691. * S5
  692. */
  693. nist_set_384(t_d, buf, 0, 0, 0, 0, 23, 22, 21, 20, 0, 0, 0, 0);
  694. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  695. /*
  696. * S6
  697. */
  698. nist_set_384(t_d, buf, 0, 0, 0, 0, 0, 0, 23, 22, 21, 0, 0, 20);
  699. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  700. /*
  701. * D1
  702. */
  703. nist_set_384(t_d, buf, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 23);
  704. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  705. /*
  706. * D2
  707. */
  708. nist_set_384(t_d, buf, 0, 0, 0, 0, 0, 0, 0, 23, 22, 21, 20, 0);
  709. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  710. /*
  711. * D3
  712. */
  713. nist_set_384(t_d, buf, 0, 0, 0, 0, 0, 0, 0, 23, 23, 0, 0, 0);
  714. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  715. /* see BN_nist_mod_224 for explanation */
  716. u.f = bn_sub_words;
  717. if (carry > 0)
  718. carry =
  719. (int)bn_sub_words(r_d, r_d, _nist_p_384[carry - 1],
  720. BN_NIST_384_TOP);
  721. else if (carry < 0) {
  722. carry =
  723. (int)bn_add_words(r_d, r_d, _nist_p_384[-carry - 1],
  724. BN_NIST_384_TOP);
  725. mask = 0 - (size_t)carry;
  726. u.p = ((size_t)bn_sub_words & mask) | ((size_t)bn_add_words & ~mask);
  727. } else
  728. carry = 1;
  729. mask = 0 - (size_t)(*u.f) (c_d, r_d, _nist_p_384[0], BN_NIST_384_TOP);
  730. mask &= 0 - (size_t)carry;
  731. res = (BN_ULONG *)(((size_t)c_d & ~mask) | ((size_t)r_d & mask));
  732. nist_cp_bn(r_d, res, BN_NIST_384_TOP);
  733. r->top = BN_NIST_384_TOP;
  734. bn_correct_top(r);
  735. return 1;
  736. }
  737. #define BN_NIST_521_RSHIFT (521%BN_BITS2)
  738. #define BN_NIST_521_LSHIFT (BN_BITS2-BN_NIST_521_RSHIFT)
  739. #define BN_NIST_521_TOP_MASK ((BN_ULONG)BN_MASK2>>BN_NIST_521_LSHIFT)
  740. int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  741. BN_CTX *ctx)
  742. {
  743. int top = a->top, i;
  744. BN_ULONG *r_d, *a_d = a->d, t_d[BN_NIST_521_TOP], val, tmp, *res;
  745. size_t mask;
  746. static const BIGNUM _bignum_nist_p_521_sqr = {
  747. (BN_ULONG *)_nist_p_521_sqr,
  748. sizeof(_nist_p_521_sqr) / sizeof(_nist_p_521_sqr[0]),
  749. sizeof(_nist_p_521_sqr) / sizeof(_nist_p_521_sqr[0]),
  750. 0, BN_FLG_STATIC_DATA
  751. };
  752. field = &_bignum_nist_p_521; /* just to make sure */
  753. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_521_sqr) >= 0)
  754. return BN_nnmod(r, a, field, ctx);
  755. i = BN_ucmp(field, a);
  756. if (i == 0) {
  757. BN_zero(r);
  758. return 1;
  759. } else if (i > 0)
  760. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  761. if (r != a) {
  762. if (!bn_wexpand(r, BN_NIST_521_TOP))
  763. return 0;
  764. r_d = r->d;
  765. nist_cp_bn(r_d, a_d, BN_NIST_521_TOP);
  766. } else
  767. r_d = a_d;
  768. /* upper 521 bits, copy ... */
  769. nist_cp_bn_0(t_d, a_d + (BN_NIST_521_TOP - 1),
  770. top - (BN_NIST_521_TOP - 1), BN_NIST_521_TOP);
  771. /* ... and right shift */
  772. for (val = t_d[0], i = 0; i < BN_NIST_521_TOP - 1; i++) {
  773. tmp = val >> BN_NIST_521_RSHIFT;
  774. val = t_d[i + 1];
  775. t_d[i] = (tmp | val << BN_NIST_521_LSHIFT) & BN_MASK2;
  776. }
  777. t_d[i] = val >> BN_NIST_521_RSHIFT;
  778. /* lower 521 bits */
  779. r_d[i] &= BN_NIST_521_TOP_MASK;
  780. bn_add_words(r_d, r_d, t_d, BN_NIST_521_TOP);
  781. mask = 0 - (size_t)bn_sub_words(t_d, r_d, _nist_p_521, BN_NIST_521_TOP);
  782. res = (BN_ULONG *)(((size_t)t_d & ~mask) | ((size_t)r_d & mask));
  783. nist_cp_bn(r_d, res, BN_NIST_521_TOP);
  784. r->top = BN_NIST_521_TOP;
  785. bn_correct_top(r);
  786. return 1;
  787. }