bn_nist.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * Copyright 2002-2018 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 _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 _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 _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 _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 _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 &_bignum_nist_p_192;
  203. }
  204. const BIGNUM *BN_get0_nist_prime_224(void)
  205. {
  206. return &_bignum_nist_p_224;
  207. }
  208. const BIGNUM *BN_get0_nist_prime_256(void)
  209. {
  210. return &_bignum_nist_p_256;
  211. }
  212. const BIGNUM *BN_get0_nist_prime_384(void)
  213. {
  214. return &_bignum_nist_p_384;
  215. }
  216. const BIGNUM *BN_get0_nist_prime_521(void)
  217. {
  218. return &_bignum_nist_p_521;
  219. }
  220. static void nist_cp_bn_0(BN_ULONG *dst, const BN_ULONG *src, int top, int max)
  221. {
  222. int i;
  223. #ifdef BN_DEBUG
  224. (void)ossl_assert(top <= max);
  225. #endif
  226. for (i = 0; i < top; i++)
  227. dst[i] = src[i];
  228. for (; i < max; i++)
  229. dst[i] = 0;
  230. }
  231. static void nist_cp_bn(BN_ULONG *dst, const BN_ULONG *src, int top)
  232. {
  233. int i;
  234. for (i = 0; i < top; i++)
  235. dst[i] = src[i];
  236. }
  237. #if BN_BITS2 == 64
  238. # define bn_cp_64(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
  239. # define bn_64_set_0(to, n) (to)[n] = (BN_ULONG)0;
  240. /*
  241. * two following macros are implemented under assumption that they
  242. * are called in a sequence with *ascending* n, i.e. as they are...
  243. */
  244. # 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))\
  245. :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l)))
  246. # define bn_32_set_0(to, n) (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0));
  247. # define bn_cp_32(to,n,from,m) ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n)
  248. # if defined(L_ENDIAN)
  249. # if defined(__arch64__)
  250. # define NIST_INT64 long
  251. # else
  252. # define NIST_INT64 long long
  253. # endif
  254. # endif
  255. #else
  256. # define bn_cp_64(to, n, from, m) \
  257. { \
  258. bn_cp_32(to, (n)*2, from, (m)*2); \
  259. bn_cp_32(to, (n)*2+1, from, (m)*2+1); \
  260. }
  261. # define bn_64_set_0(to, n) \
  262. { \
  263. bn_32_set_0(to, (n)*2); \
  264. bn_32_set_0(to, (n)*2+1); \
  265. }
  266. # define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
  267. # define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0;
  268. # if defined(_WIN32) && !defined(__GNUC__)
  269. # define NIST_INT64 __int64
  270. # elif defined(BN_LLONG)
  271. # define NIST_INT64 long long
  272. # endif
  273. #endif /* BN_BITS2 != 64 */
  274. #define nist_set_192(to, from, a1, a2, a3) \
  275. { \
  276. bn_cp_64(to, 0, from, (a3) - 3) \
  277. bn_cp_64(to, 1, from, (a2) - 3) \
  278. bn_cp_64(to, 2, from, (a1) - 3) \
  279. }
  280. int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  281. BN_CTX *ctx)
  282. {
  283. int top = a->top, i;
  284. int carry;
  285. register BN_ULONG *r_d, *a_d = a->d;
  286. union {
  287. BN_ULONG bn[BN_NIST_192_TOP];
  288. unsigned int ui[BN_NIST_192_TOP * sizeof(BN_ULONG) /
  289. sizeof(unsigned int)];
  290. } buf;
  291. BN_ULONG c_d[BN_NIST_192_TOP], *res;
  292. PTR_SIZE_INT mask;
  293. static const BIGNUM _bignum_nist_p_192_sqr = {
  294. (BN_ULONG *)_nist_p_192_sqr,
  295. OSSL_NELEM(_nist_p_192_sqr),
  296. OSSL_NELEM(_nist_p_192_sqr),
  297. 0, BN_FLG_STATIC_DATA
  298. };
  299. field = &_bignum_nist_p_192; /* just to make sure */
  300. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_192_sqr) >= 0)
  301. return BN_nnmod(r, a, field, ctx);
  302. i = BN_ucmp(field, a);
  303. if (i == 0) {
  304. BN_zero(r);
  305. return 1;
  306. } else if (i > 0)
  307. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  308. if (r != a) {
  309. if (!bn_wexpand(r, BN_NIST_192_TOP))
  310. return 0;
  311. r_d = r->d;
  312. nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);
  313. } else
  314. r_d = a_d;
  315. nist_cp_bn_0(buf.bn, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP,
  316. BN_NIST_192_TOP);
  317. #if defined(NIST_INT64)
  318. {
  319. NIST_INT64 acc; /* accumulator */
  320. unsigned int *rp = (unsigned int *)r_d;
  321. const unsigned int *bp = (const unsigned int *)buf.ui;
  322. acc = rp[0];
  323. acc += bp[3 * 2 - 6];
  324. acc += bp[5 * 2 - 6];
  325. rp[0] = (unsigned int)acc;
  326. acc >>= 32;
  327. acc += rp[1];
  328. acc += bp[3 * 2 - 5];
  329. acc += bp[5 * 2 - 5];
  330. rp[1] = (unsigned int)acc;
  331. acc >>= 32;
  332. acc += rp[2];
  333. acc += bp[3 * 2 - 6];
  334. acc += bp[4 * 2 - 6];
  335. acc += bp[5 * 2 - 6];
  336. rp[2] = (unsigned int)acc;
  337. acc >>= 32;
  338. acc += rp[3];
  339. acc += bp[3 * 2 - 5];
  340. acc += bp[4 * 2 - 5];
  341. acc += bp[5 * 2 - 5];
  342. rp[3] = (unsigned int)acc;
  343. acc >>= 32;
  344. acc += rp[4];
  345. acc += bp[4 * 2 - 6];
  346. acc += bp[5 * 2 - 6];
  347. rp[4] = (unsigned int)acc;
  348. acc >>= 32;
  349. acc += rp[5];
  350. acc += bp[4 * 2 - 5];
  351. acc += bp[5 * 2 - 5];
  352. rp[5] = (unsigned int)acc;
  353. carry = (int)(acc >> 32);
  354. }
  355. #else
  356. {
  357. BN_ULONG t_d[BN_NIST_192_TOP];
  358. nist_set_192(t_d, buf.bn, 0, 3, 3);
  359. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  360. nist_set_192(t_d, buf.bn, 4, 4, 0);
  361. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  362. nist_set_192(t_d, buf.bn, 5, 5, 5)
  363. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
  364. }
  365. #endif
  366. if (carry > 0)
  367. carry =
  368. (int)bn_sub_words(r_d, r_d, _nist_p_192[carry - 1],
  369. BN_NIST_192_TOP);
  370. else
  371. carry = 1;
  372. /*
  373. * we need 'if (carry==0 || result>=modulus) result-=modulus;'
  374. * as comparison implies subtraction, we can write
  375. * 'tmp=result-modulus; if (!carry || !borrow) result=tmp;'
  376. * this is what happens below, but without explicit if:-) a.
  377. */
  378. mask =
  379. 0 - (PTR_SIZE_INT) bn_sub_words(c_d, r_d, _nist_p_192[0],
  380. BN_NIST_192_TOP);
  381. mask &= 0 - (PTR_SIZE_INT) carry;
  382. res = c_d;
  383. res = (BN_ULONG *)
  384. (((PTR_SIZE_INT) res & ~mask) | ((PTR_SIZE_INT) r_d & mask));
  385. nist_cp_bn(r_d, res, BN_NIST_192_TOP);
  386. r->top = BN_NIST_192_TOP;
  387. bn_correct_top(r);
  388. return 1;
  389. }
  390. typedef BN_ULONG (*bn_addsub_f) (BN_ULONG *, const BN_ULONG *,
  391. const BN_ULONG *, int);
  392. #define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \
  393. { \
  394. bn_cp_32(to, 0, from, (a7) - 7) \
  395. bn_cp_32(to, 1, from, (a6) - 7) \
  396. bn_cp_32(to, 2, from, (a5) - 7) \
  397. bn_cp_32(to, 3, from, (a4) - 7) \
  398. bn_cp_32(to, 4, from, (a3) - 7) \
  399. bn_cp_32(to, 5, from, (a2) - 7) \
  400. bn_cp_32(to, 6, from, (a1) - 7) \
  401. }
  402. int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  403. BN_CTX *ctx)
  404. {
  405. int top = a->top, i;
  406. int carry;
  407. BN_ULONG *r_d, *a_d = a->d;
  408. union {
  409. BN_ULONG bn[BN_NIST_224_TOP];
  410. unsigned int ui[BN_NIST_224_TOP * sizeof(BN_ULONG) /
  411. sizeof(unsigned int)];
  412. } buf;
  413. BN_ULONG c_d[BN_NIST_224_TOP], *res;
  414. PTR_SIZE_INT mask;
  415. union {
  416. bn_addsub_f f;
  417. PTR_SIZE_INT p;
  418. } u;
  419. static const BIGNUM _bignum_nist_p_224_sqr = {
  420. (BN_ULONG *)_nist_p_224_sqr,
  421. OSSL_NELEM(_nist_p_224_sqr),
  422. OSSL_NELEM(_nist_p_224_sqr),
  423. 0, BN_FLG_STATIC_DATA
  424. };
  425. field = &_bignum_nist_p_224; /* just to make sure */
  426. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_224_sqr) >= 0)
  427. return BN_nnmod(r, a, field, ctx);
  428. i = BN_ucmp(field, a);
  429. if (i == 0) {
  430. BN_zero(r);
  431. return 1;
  432. } else if (i > 0)
  433. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  434. if (r != a) {
  435. if (!bn_wexpand(r, BN_NIST_224_TOP))
  436. return 0;
  437. r_d = r->d;
  438. nist_cp_bn(r_d, a_d, BN_NIST_224_TOP);
  439. } else
  440. r_d = a_d;
  441. #if BN_BITS2==64
  442. /* copy upper 256 bits of 448 bit number ... */
  443. nist_cp_bn_0(c_d, a_d + (BN_NIST_224_TOP - 1),
  444. top - (BN_NIST_224_TOP - 1), BN_NIST_224_TOP);
  445. /* ... and right shift by 32 to obtain upper 224 bits */
  446. nist_set_224(buf.bn, c_d, 14, 13, 12, 11, 10, 9, 8);
  447. /* truncate lower part to 224 bits too */
  448. r_d[BN_NIST_224_TOP - 1] &= BN_MASK2l;
  449. #else
  450. nist_cp_bn_0(buf.bn, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP,
  451. BN_NIST_224_TOP);
  452. #endif
  453. #if defined(NIST_INT64) && BN_BITS2!=64
  454. {
  455. NIST_INT64 acc; /* accumulator */
  456. unsigned int *rp = (unsigned int *)r_d;
  457. const unsigned int *bp = (const unsigned int *)buf.ui;
  458. acc = rp[0];
  459. acc -= bp[7 - 7];
  460. acc -= bp[11 - 7];
  461. rp[0] = (unsigned int)acc;
  462. acc >>= 32;
  463. acc += rp[1];
  464. acc -= bp[8 - 7];
  465. acc -= bp[12 - 7];
  466. rp[1] = (unsigned int)acc;
  467. acc >>= 32;
  468. acc += rp[2];
  469. acc -= bp[9 - 7];
  470. acc -= bp[13 - 7];
  471. rp[2] = (unsigned int)acc;
  472. acc >>= 32;
  473. acc += rp[3];
  474. acc += bp[7 - 7];
  475. acc += bp[11 - 7];
  476. acc -= bp[10 - 7];
  477. rp[3] = (unsigned int)acc;
  478. acc >>= 32;
  479. acc += rp[4];
  480. acc += bp[8 - 7];
  481. acc += bp[12 - 7];
  482. acc -= bp[11 - 7];
  483. rp[4] = (unsigned int)acc;
  484. acc >>= 32;
  485. acc += rp[5];
  486. acc += bp[9 - 7];
  487. acc += bp[13 - 7];
  488. acc -= bp[12 - 7];
  489. rp[5] = (unsigned int)acc;
  490. acc >>= 32;
  491. acc += rp[6];
  492. acc += bp[10 - 7];
  493. acc -= bp[13 - 7];
  494. rp[6] = (unsigned int)acc;
  495. carry = (int)(acc >> 32);
  496. # if BN_BITS2==64
  497. rp[7] = carry;
  498. # endif
  499. }
  500. #else
  501. {
  502. BN_ULONG t_d[BN_NIST_224_TOP];
  503. nist_set_224(t_d, buf.bn, 10, 9, 8, 7, 0, 0, 0);
  504. carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  505. nist_set_224(t_d, buf.bn, 0, 13, 12, 11, 0, 0, 0);
  506. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  507. nist_set_224(t_d, buf.bn, 13, 12, 11, 10, 9, 8, 7);
  508. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  509. nist_set_224(t_d, buf.bn, 0, 0, 0, 0, 13, 12, 11);
  510. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
  511. # if BN_BITS2==64
  512. carry = (int)(r_d[BN_NIST_224_TOP - 1] >> 32);
  513. # endif
  514. }
  515. #endif
  516. u.f = bn_sub_words;
  517. if (carry > 0) {
  518. carry =
  519. (int)bn_sub_words(r_d, r_d, _nist_p_224[carry - 1],
  520. BN_NIST_224_TOP);
  521. #if BN_BITS2==64
  522. carry = (int)(~(r_d[BN_NIST_224_TOP - 1] >> 32)) & 1;
  523. #endif
  524. } else if (carry < 0) {
  525. /*
  526. * it's a bit more complicated logic in this case. if bn_add_words
  527. * yields no carry, then result has to be adjusted by unconditionally
  528. * *adding* the modulus. but if it does, then result has to be
  529. * compared to the modulus and conditionally adjusted by
  530. * *subtracting* the latter.
  531. */
  532. carry =
  533. (int)bn_add_words(r_d, r_d, _nist_p_224[-carry - 1],
  534. BN_NIST_224_TOP);
  535. mask = 0 - (PTR_SIZE_INT) carry;
  536. u.p = ((PTR_SIZE_INT) bn_sub_words & mask) |
  537. ((PTR_SIZE_INT) bn_add_words & ~mask);
  538. } else
  539. carry = 1;
  540. /* otherwise it's effectively same as in BN_nist_mod_192... */
  541. mask =
  542. 0 - (PTR_SIZE_INT) (*u.f) (c_d, r_d, _nist_p_224[0], BN_NIST_224_TOP);
  543. mask &= 0 - (PTR_SIZE_INT) carry;
  544. res = c_d;
  545. res = (BN_ULONG *)(((PTR_SIZE_INT) res & ~mask) |
  546. ((PTR_SIZE_INT) r_d & mask));
  547. nist_cp_bn(r_d, res, BN_NIST_224_TOP);
  548. r->top = BN_NIST_224_TOP;
  549. bn_correct_top(r);
  550. return 1;
  551. }
  552. #define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \
  553. { \
  554. bn_cp_32(to, 0, from, (a8) - 8) \
  555. bn_cp_32(to, 1, from, (a7) - 8) \
  556. bn_cp_32(to, 2, from, (a6) - 8) \
  557. bn_cp_32(to, 3, from, (a5) - 8) \
  558. bn_cp_32(to, 4, from, (a4) - 8) \
  559. bn_cp_32(to, 5, from, (a3) - 8) \
  560. bn_cp_32(to, 6, from, (a2) - 8) \
  561. bn_cp_32(to, 7, from, (a1) - 8) \
  562. }
  563. int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  564. BN_CTX *ctx)
  565. {
  566. int i, top = a->top;
  567. int carry = 0;
  568. register BN_ULONG *a_d = a->d, *r_d;
  569. union {
  570. BN_ULONG bn[BN_NIST_256_TOP];
  571. unsigned int ui[BN_NIST_256_TOP * sizeof(BN_ULONG) /
  572. sizeof(unsigned int)];
  573. } buf;
  574. BN_ULONG c_d[BN_NIST_256_TOP], *res;
  575. PTR_SIZE_INT mask;
  576. union {
  577. bn_addsub_f f;
  578. PTR_SIZE_INT p;
  579. } u;
  580. static const BIGNUM _bignum_nist_p_256_sqr = {
  581. (BN_ULONG *)_nist_p_256_sqr,
  582. OSSL_NELEM(_nist_p_256_sqr),
  583. OSSL_NELEM(_nist_p_256_sqr),
  584. 0, BN_FLG_STATIC_DATA
  585. };
  586. field = &_bignum_nist_p_256; /* just to make sure */
  587. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_256_sqr) >= 0)
  588. return BN_nnmod(r, a, field, ctx);
  589. i = BN_ucmp(field, a);
  590. if (i == 0) {
  591. BN_zero(r);
  592. return 1;
  593. } else if (i > 0)
  594. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  595. if (r != a) {
  596. if (!bn_wexpand(r, BN_NIST_256_TOP))
  597. return 0;
  598. r_d = r->d;
  599. nist_cp_bn(r_d, a_d, BN_NIST_256_TOP);
  600. } else
  601. r_d = a_d;
  602. nist_cp_bn_0(buf.bn, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP,
  603. BN_NIST_256_TOP);
  604. #if defined(NIST_INT64)
  605. {
  606. NIST_INT64 acc; /* accumulator */
  607. unsigned int *rp = (unsigned int *)r_d;
  608. const unsigned int *bp = (const unsigned int *)buf.ui;
  609. acc = rp[0];
  610. acc += bp[8 - 8];
  611. acc += bp[9 - 8];
  612. acc -= bp[11 - 8];
  613. acc -= bp[12 - 8];
  614. acc -= bp[13 - 8];
  615. acc -= bp[14 - 8];
  616. rp[0] = (unsigned int)acc;
  617. acc >>= 32;
  618. acc += rp[1];
  619. acc += bp[9 - 8];
  620. acc += bp[10 - 8];
  621. acc -= bp[12 - 8];
  622. acc -= bp[13 - 8];
  623. acc -= bp[14 - 8];
  624. acc -= bp[15 - 8];
  625. rp[1] = (unsigned int)acc;
  626. acc >>= 32;
  627. acc += rp[2];
  628. acc += bp[10 - 8];
  629. acc += bp[11 - 8];
  630. acc -= bp[13 - 8];
  631. acc -= bp[14 - 8];
  632. acc -= bp[15 - 8];
  633. rp[2] = (unsigned int)acc;
  634. acc >>= 32;
  635. acc += rp[3];
  636. acc += bp[11 - 8];
  637. acc += bp[11 - 8];
  638. acc += bp[12 - 8];
  639. acc += bp[12 - 8];
  640. acc += bp[13 - 8];
  641. acc -= bp[15 - 8];
  642. acc -= bp[8 - 8];
  643. acc -= bp[9 - 8];
  644. rp[3] = (unsigned int)acc;
  645. acc >>= 32;
  646. acc += rp[4];
  647. acc += bp[12 - 8];
  648. acc += bp[12 - 8];
  649. acc += bp[13 - 8];
  650. acc += bp[13 - 8];
  651. acc += bp[14 - 8];
  652. acc -= bp[9 - 8];
  653. acc -= bp[10 - 8];
  654. rp[4] = (unsigned int)acc;
  655. acc >>= 32;
  656. acc += rp[5];
  657. acc += bp[13 - 8];
  658. acc += bp[13 - 8];
  659. acc += bp[14 - 8];
  660. acc += bp[14 - 8];
  661. acc += bp[15 - 8];
  662. acc -= bp[10 - 8];
  663. acc -= bp[11 - 8];
  664. rp[5] = (unsigned int)acc;
  665. acc >>= 32;
  666. acc += rp[6];
  667. acc += bp[14 - 8];
  668. acc += bp[14 - 8];
  669. acc += bp[15 - 8];
  670. acc += bp[15 - 8];
  671. acc += bp[14 - 8];
  672. acc += bp[13 - 8];
  673. acc -= bp[8 - 8];
  674. acc -= bp[9 - 8];
  675. rp[6] = (unsigned int)acc;
  676. acc >>= 32;
  677. acc += rp[7];
  678. acc += bp[15 - 8];
  679. acc += bp[15 - 8];
  680. acc += bp[15 - 8];
  681. acc += bp[8 - 8];
  682. acc -= bp[10 - 8];
  683. acc -= bp[11 - 8];
  684. acc -= bp[12 - 8];
  685. acc -= bp[13 - 8];
  686. rp[7] = (unsigned int)acc;
  687. carry = (int)(acc >> 32);
  688. }
  689. #else
  690. {
  691. BN_ULONG t_d[BN_NIST_256_TOP];
  692. /*
  693. * S1
  694. */
  695. nist_set_256(t_d, buf.bn, 15, 14, 13, 12, 11, 0, 0, 0);
  696. /*
  697. * S2
  698. */
  699. nist_set_256(c_d, buf.bn, 0, 15, 14, 13, 12, 0, 0, 0);
  700. carry = (int)bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
  701. /* left shift */
  702. {
  703. register BN_ULONG *ap, t, c;
  704. ap = t_d;
  705. c = 0;
  706. for (i = BN_NIST_256_TOP; i != 0; --i) {
  707. t = *ap;
  708. *(ap++) = ((t << 1) | c) & BN_MASK2;
  709. c = (t & BN_TBIT) ? 1 : 0;
  710. }
  711. carry <<= 1;
  712. carry |= c;
  713. }
  714. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  715. /*
  716. * S3
  717. */
  718. nist_set_256(t_d, buf.bn, 15, 14, 0, 0, 0, 10, 9, 8);
  719. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  720. /*
  721. * S4
  722. */
  723. nist_set_256(t_d, buf.bn, 8, 13, 15, 14, 13, 11, 10, 9);
  724. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  725. /*
  726. * D1
  727. */
  728. nist_set_256(t_d, buf.bn, 10, 8, 0, 0, 0, 13, 12, 11);
  729. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  730. /*
  731. * D2
  732. */
  733. nist_set_256(t_d, buf.bn, 11, 9, 0, 0, 15, 14, 13, 12);
  734. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  735. /*
  736. * D3
  737. */
  738. nist_set_256(t_d, buf.bn, 12, 0, 10, 9, 8, 15, 14, 13);
  739. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  740. /*
  741. * D4
  742. */
  743. nist_set_256(t_d, buf.bn, 13, 0, 11, 10, 9, 0, 15, 14);
  744. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
  745. }
  746. #endif
  747. /* see BN_nist_mod_224 for explanation */
  748. u.f = bn_sub_words;
  749. if (carry > 0)
  750. carry =
  751. (int)bn_sub_words(r_d, r_d, _nist_p_256[carry - 1],
  752. BN_NIST_256_TOP);
  753. else if (carry < 0) {
  754. carry =
  755. (int)bn_add_words(r_d, r_d, _nist_p_256[-carry - 1],
  756. BN_NIST_256_TOP);
  757. mask = 0 - (PTR_SIZE_INT) carry;
  758. u.p = ((PTR_SIZE_INT) bn_sub_words & mask) |
  759. ((PTR_SIZE_INT) bn_add_words & ~mask);
  760. } else
  761. carry = 1;
  762. mask =
  763. 0 - (PTR_SIZE_INT) (*u.f) (c_d, r_d, _nist_p_256[0], BN_NIST_256_TOP);
  764. mask &= 0 - (PTR_SIZE_INT) carry;
  765. res = c_d;
  766. res = (BN_ULONG *)(((PTR_SIZE_INT) res & ~mask) |
  767. ((PTR_SIZE_INT) r_d & mask));
  768. nist_cp_bn(r_d, res, BN_NIST_256_TOP);
  769. r->top = BN_NIST_256_TOP;
  770. bn_correct_top(r);
  771. return 1;
  772. }
  773. #define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
  774. { \
  775. bn_cp_32(to, 0, from, (a12) - 12) \
  776. bn_cp_32(to, 1, from, (a11) - 12) \
  777. bn_cp_32(to, 2, from, (a10) - 12) \
  778. bn_cp_32(to, 3, from, (a9) - 12) \
  779. bn_cp_32(to, 4, from, (a8) - 12) \
  780. bn_cp_32(to, 5, from, (a7) - 12) \
  781. bn_cp_32(to, 6, from, (a6) - 12) \
  782. bn_cp_32(to, 7, from, (a5) - 12) \
  783. bn_cp_32(to, 8, from, (a4) - 12) \
  784. bn_cp_32(to, 9, from, (a3) - 12) \
  785. bn_cp_32(to, 10, from, (a2) - 12) \
  786. bn_cp_32(to, 11, from, (a1) - 12) \
  787. }
  788. int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  789. BN_CTX *ctx)
  790. {
  791. int i, top = a->top;
  792. int carry = 0;
  793. register BN_ULONG *r_d, *a_d = a->d;
  794. union {
  795. BN_ULONG bn[BN_NIST_384_TOP];
  796. unsigned int ui[BN_NIST_384_TOP * sizeof(BN_ULONG) /
  797. sizeof(unsigned int)];
  798. } buf;
  799. BN_ULONG c_d[BN_NIST_384_TOP], *res;
  800. PTR_SIZE_INT mask;
  801. union {
  802. bn_addsub_f f;
  803. PTR_SIZE_INT p;
  804. } u;
  805. static const BIGNUM _bignum_nist_p_384_sqr = {
  806. (BN_ULONG *)_nist_p_384_sqr,
  807. OSSL_NELEM(_nist_p_384_sqr),
  808. OSSL_NELEM(_nist_p_384_sqr),
  809. 0, BN_FLG_STATIC_DATA
  810. };
  811. field = &_bignum_nist_p_384; /* just to make sure */
  812. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_384_sqr) >= 0)
  813. return BN_nnmod(r, a, field, ctx);
  814. i = BN_ucmp(field, a);
  815. if (i == 0) {
  816. BN_zero(r);
  817. return 1;
  818. } else if (i > 0)
  819. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  820. if (r != a) {
  821. if (!bn_wexpand(r, BN_NIST_384_TOP))
  822. return 0;
  823. r_d = r->d;
  824. nist_cp_bn(r_d, a_d, BN_NIST_384_TOP);
  825. } else
  826. r_d = a_d;
  827. nist_cp_bn_0(buf.bn, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP,
  828. BN_NIST_384_TOP);
  829. #if defined(NIST_INT64)
  830. {
  831. NIST_INT64 acc; /* accumulator */
  832. unsigned int *rp = (unsigned int *)r_d;
  833. const unsigned int *bp = (const unsigned int *)buf.ui;
  834. acc = rp[0];
  835. acc += bp[12 - 12];
  836. acc += bp[21 - 12];
  837. acc += bp[20 - 12];
  838. acc -= bp[23 - 12];
  839. rp[0] = (unsigned int)acc;
  840. acc >>= 32;
  841. acc += rp[1];
  842. acc += bp[13 - 12];
  843. acc += bp[22 - 12];
  844. acc += bp[23 - 12];
  845. acc -= bp[12 - 12];
  846. acc -= bp[20 - 12];
  847. rp[1] = (unsigned int)acc;
  848. acc >>= 32;
  849. acc += rp[2];
  850. acc += bp[14 - 12];
  851. acc += bp[23 - 12];
  852. acc -= bp[13 - 12];
  853. acc -= bp[21 - 12];
  854. rp[2] = (unsigned int)acc;
  855. acc >>= 32;
  856. acc += rp[3];
  857. acc += bp[15 - 12];
  858. acc += bp[12 - 12];
  859. acc += bp[20 - 12];
  860. acc += bp[21 - 12];
  861. acc -= bp[14 - 12];
  862. acc -= bp[22 - 12];
  863. acc -= bp[23 - 12];
  864. rp[3] = (unsigned int)acc;
  865. acc >>= 32;
  866. acc += rp[4];
  867. acc += bp[21 - 12];
  868. acc += bp[21 - 12];
  869. acc += bp[16 - 12];
  870. acc += bp[13 - 12];
  871. acc += bp[12 - 12];
  872. acc += bp[20 - 12];
  873. acc += bp[22 - 12];
  874. acc -= bp[15 - 12];
  875. acc -= bp[23 - 12];
  876. acc -= bp[23 - 12];
  877. rp[4] = (unsigned int)acc;
  878. acc >>= 32;
  879. acc += rp[5];
  880. acc += bp[22 - 12];
  881. acc += bp[22 - 12];
  882. acc += bp[17 - 12];
  883. acc += bp[14 - 12];
  884. acc += bp[13 - 12];
  885. acc += bp[21 - 12];
  886. acc += bp[23 - 12];
  887. acc -= bp[16 - 12];
  888. rp[5] = (unsigned int)acc;
  889. acc >>= 32;
  890. acc += rp[6];
  891. acc += bp[23 - 12];
  892. acc += bp[23 - 12];
  893. acc += bp[18 - 12];
  894. acc += bp[15 - 12];
  895. acc += bp[14 - 12];
  896. acc += bp[22 - 12];
  897. acc -= bp[17 - 12];
  898. rp[6] = (unsigned int)acc;
  899. acc >>= 32;
  900. acc += rp[7];
  901. acc += bp[19 - 12];
  902. acc += bp[16 - 12];
  903. acc += bp[15 - 12];
  904. acc += bp[23 - 12];
  905. acc -= bp[18 - 12];
  906. rp[7] = (unsigned int)acc;
  907. acc >>= 32;
  908. acc += rp[8];
  909. acc += bp[20 - 12];
  910. acc += bp[17 - 12];
  911. acc += bp[16 - 12];
  912. acc -= bp[19 - 12];
  913. rp[8] = (unsigned int)acc;
  914. acc >>= 32;
  915. acc += rp[9];
  916. acc += bp[21 - 12];
  917. acc += bp[18 - 12];
  918. acc += bp[17 - 12];
  919. acc -= bp[20 - 12];
  920. rp[9] = (unsigned int)acc;
  921. acc >>= 32;
  922. acc += rp[10];
  923. acc += bp[22 - 12];
  924. acc += bp[19 - 12];
  925. acc += bp[18 - 12];
  926. acc -= bp[21 - 12];
  927. rp[10] = (unsigned int)acc;
  928. acc >>= 32;
  929. acc += rp[11];
  930. acc += bp[23 - 12];
  931. acc += bp[20 - 12];
  932. acc += bp[19 - 12];
  933. acc -= bp[22 - 12];
  934. rp[11] = (unsigned int)acc;
  935. carry = (int)(acc >> 32);
  936. }
  937. #else
  938. {
  939. BN_ULONG t_d[BN_NIST_384_TOP];
  940. /*
  941. * S1
  942. */
  943. nist_set_256(t_d, buf.bn, 0, 0, 0, 0, 0, 23 - 4, 22 - 4, 21 - 4);
  944. /* left shift */
  945. {
  946. register BN_ULONG *ap, t, c;
  947. ap = t_d;
  948. c = 0;
  949. for (i = 3; i != 0; --i) {
  950. t = *ap;
  951. *(ap++) = ((t << 1) | c) & BN_MASK2;
  952. c = (t & BN_TBIT) ? 1 : 0;
  953. }
  954. *ap = c;
  955. }
  956. carry =
  957. (int)bn_add_words(r_d + (128 / BN_BITS2), r_d + (128 / BN_BITS2),
  958. t_d, BN_NIST_256_TOP);
  959. /*
  960. * S2
  961. */
  962. carry += (int)bn_add_words(r_d, r_d, buf.bn, BN_NIST_384_TOP);
  963. /*
  964. * S3
  965. */
  966. nist_set_384(t_d, buf.bn, 20, 19, 18, 17, 16, 15, 14, 13, 12, 23, 22,
  967. 21);
  968. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  969. /*
  970. * S4
  971. */
  972. nist_set_384(t_d, buf.bn, 19, 18, 17, 16, 15, 14, 13, 12, 20, 0, 23,
  973. 0);
  974. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  975. /*
  976. * S5
  977. */
  978. nist_set_384(t_d, buf.bn, 0, 0, 0, 0, 23, 22, 21, 20, 0, 0, 0, 0);
  979. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  980. /*
  981. * S6
  982. */
  983. nist_set_384(t_d, buf.bn, 0, 0, 0, 0, 0, 0, 23, 22, 21, 0, 0, 20);
  984. carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  985. /*
  986. * D1
  987. */
  988. nist_set_384(t_d, buf.bn, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12,
  989. 23);
  990. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  991. /*
  992. * D2
  993. */
  994. nist_set_384(t_d, buf.bn, 0, 0, 0, 0, 0, 0, 0, 23, 22, 21, 20, 0);
  995. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  996. /*
  997. * D3
  998. */
  999. nist_set_384(t_d, buf.bn, 0, 0, 0, 0, 0, 0, 0, 23, 23, 0, 0, 0);
  1000. carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
  1001. }
  1002. #endif
  1003. /* see BN_nist_mod_224 for explanation */
  1004. u.f = bn_sub_words;
  1005. if (carry > 0)
  1006. carry =
  1007. (int)bn_sub_words(r_d, r_d, _nist_p_384[carry - 1],
  1008. BN_NIST_384_TOP);
  1009. else if (carry < 0) {
  1010. carry =
  1011. (int)bn_add_words(r_d, r_d, _nist_p_384[-carry - 1],
  1012. BN_NIST_384_TOP);
  1013. mask = 0 - (PTR_SIZE_INT) carry;
  1014. u.p = ((PTR_SIZE_INT) bn_sub_words & mask) |
  1015. ((PTR_SIZE_INT) bn_add_words & ~mask);
  1016. } else
  1017. carry = 1;
  1018. mask =
  1019. 0 - (PTR_SIZE_INT) (*u.f) (c_d, r_d, _nist_p_384[0], BN_NIST_384_TOP);
  1020. mask &= 0 - (PTR_SIZE_INT) carry;
  1021. res = c_d;
  1022. res = (BN_ULONG *)(((PTR_SIZE_INT) res & ~mask) |
  1023. ((PTR_SIZE_INT) r_d & mask));
  1024. nist_cp_bn(r_d, res, BN_NIST_384_TOP);
  1025. r->top = BN_NIST_384_TOP;
  1026. bn_correct_top(r);
  1027. return 1;
  1028. }
  1029. #define BN_NIST_521_RSHIFT (521%BN_BITS2)
  1030. #define BN_NIST_521_LSHIFT (BN_BITS2-BN_NIST_521_RSHIFT)
  1031. #define BN_NIST_521_TOP_MASK ((BN_ULONG)BN_MASK2>>BN_NIST_521_LSHIFT)
  1032. int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
  1033. BN_CTX *ctx)
  1034. {
  1035. int top = a->top, i;
  1036. BN_ULONG *r_d, *a_d = a->d, t_d[BN_NIST_521_TOP], val, tmp, *res;
  1037. PTR_SIZE_INT mask;
  1038. static const BIGNUM _bignum_nist_p_521_sqr = {
  1039. (BN_ULONG *)_nist_p_521_sqr,
  1040. OSSL_NELEM(_nist_p_521_sqr),
  1041. OSSL_NELEM(_nist_p_521_sqr),
  1042. 0, BN_FLG_STATIC_DATA
  1043. };
  1044. field = &_bignum_nist_p_521; /* just to make sure */
  1045. if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_521_sqr) >= 0)
  1046. return BN_nnmod(r, a, field, ctx);
  1047. i = BN_ucmp(field, a);
  1048. if (i == 0) {
  1049. BN_zero(r);
  1050. return 1;
  1051. } else if (i > 0)
  1052. return (r == a) ? 1 : (BN_copy(r, a) != NULL);
  1053. if (r != a) {
  1054. if (!bn_wexpand(r, BN_NIST_521_TOP))
  1055. return 0;
  1056. r_d = r->d;
  1057. nist_cp_bn(r_d, a_d, BN_NIST_521_TOP);
  1058. } else
  1059. r_d = a_d;
  1060. /* upper 521 bits, copy ... */
  1061. nist_cp_bn_0(t_d, a_d + (BN_NIST_521_TOP - 1),
  1062. top - (BN_NIST_521_TOP - 1), BN_NIST_521_TOP);
  1063. /* ... and right shift */
  1064. for (val = t_d[0], i = 0; i < BN_NIST_521_TOP - 1; i++) {
  1065. #if 0
  1066. /*
  1067. * MSC ARM compiler [version 2013, presumably even earlier,
  1068. * much earlier] miscompiles this code, but not one in
  1069. * #else section. See RT#3541.
  1070. */
  1071. tmp = val >> BN_NIST_521_RSHIFT;
  1072. val = t_d[i + 1];
  1073. t_d[i] = (tmp | val << BN_NIST_521_LSHIFT) & BN_MASK2;
  1074. #else
  1075. t_d[i] = (val >> BN_NIST_521_RSHIFT |
  1076. (tmp = t_d[i + 1]) << BN_NIST_521_LSHIFT) & BN_MASK2;
  1077. val = tmp;
  1078. #endif
  1079. }
  1080. t_d[i] = val >> BN_NIST_521_RSHIFT;
  1081. /* lower 521 bits */
  1082. r_d[i] &= BN_NIST_521_TOP_MASK;
  1083. bn_add_words(r_d, r_d, t_d, BN_NIST_521_TOP);
  1084. mask =
  1085. 0 - (PTR_SIZE_INT) bn_sub_words(t_d, r_d, _nist_p_521,
  1086. BN_NIST_521_TOP);
  1087. res = t_d;
  1088. res = (BN_ULONG *)(((PTR_SIZE_INT) res & ~mask) |
  1089. ((PTR_SIZE_INT) r_d & mask));
  1090. nist_cp_bn(r_d, res, BN_NIST_521_TOP);
  1091. r->top = BN_NIST_521_TOP;
  1092. bn_correct_top(r);
  1093. return 1;
  1094. }
  1095. int (*BN_nist_mod_func(const BIGNUM *p)) (BIGNUM *r, const BIGNUM *a,
  1096. const BIGNUM *field, BN_CTX *ctx) {
  1097. if (BN_ucmp(&_bignum_nist_p_192, p) == 0)
  1098. return BN_nist_mod_192;
  1099. if (BN_ucmp(&_bignum_nist_p_224, p) == 0)
  1100. return BN_nist_mod_224;
  1101. if (BN_ucmp(&_bignum_nist_p_256, p) == 0)
  1102. return BN_nist_mod_256;
  1103. if (BN_ucmp(&_bignum_nist_p_384, p) == 0)
  1104. return BN_nist_mod_384;
  1105. if (BN_ucmp(&_bignum_nist_p_521, p) == 0)
  1106. return BN_nist_mod_521;
  1107. return 0;
  1108. }