2
0

bn_nist.c 37 KB

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