bn_nist.c 38 KB

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