2
0

rsaz_exp_x2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2020-2021, Intel Corporation. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. *
  11. * Originally written by Sergey Kirillov and Andrey Matyukov.
  12. * Special thanks to Ilya Albrekht for his valuable hints.
  13. * Intel Corporation
  14. *
  15. */
  16. #include <openssl/opensslconf.h>
  17. #include <openssl/crypto.h>
  18. #include "rsaz_exp.h"
  19. #ifndef RSAZ_ENABLED
  20. NON_EMPTY_TRANSLATION_UNIT
  21. #else
  22. # include <assert.h>
  23. # include <string.h>
  24. # if defined(__GNUC__)
  25. # define ALIGN1 __attribute__((aligned(1)))
  26. # elif defined(_MSC_VER)
  27. # define ALIGN1 __declspec(align(1))
  28. # else
  29. # define ALIGN1
  30. # endif
  31. # define ALIGN_OF(ptr, boundary) \
  32. ((unsigned char *)(ptr) + (boundary - (((size_t)(ptr)) & (boundary - 1))))
  33. /* Internal radix */
  34. # define DIGIT_SIZE (52)
  35. /* 52-bit mask */
  36. # define DIGIT_MASK ((uint64_t)0xFFFFFFFFFFFFF)
  37. # define BITS2WORD8_SIZE(x) (((x) + 7) >> 3)
  38. # define BITS2WORD64_SIZE(x) (((x) + 63) >> 6)
  39. /* Number of registers required to hold |digits_num| amount of qword digits */
  40. # define NUMBER_OF_REGISTERS(digits_num, register_size) \
  41. (((digits_num) * 64 + (register_size) - 1) / (register_size))
  42. typedef uint64_t ALIGN1 uint64_t_align1;
  43. static ossl_inline uint64_t get_digit(const uint8_t *in, int in_len);
  44. static ossl_inline void put_digit(uint8_t *out, int out_len, uint64_t digit);
  45. static void to_words52(BN_ULONG *out, int out_len, const BN_ULONG *in,
  46. int in_bitsize);
  47. static void from_words52(BN_ULONG *bn_out, int out_bitsize, const BN_ULONG *in);
  48. static ossl_inline void set_bit(BN_ULONG *a, int idx);
  49. /* Number of |digit_size|-bit digits in |bitsize|-bit value */
  50. static ossl_inline int number_of_digits(int bitsize, int digit_size)
  51. {
  52. return (bitsize + digit_size - 1) / digit_size;
  53. }
  54. /*
  55. * For details of the methods declared below please refer to
  56. * crypto/bn/asm/rsaz-avx512.pl
  57. *
  58. * Naming conventions:
  59. * amm = Almost Montgomery Multiplication
  60. * ams = Almost Montgomery Squaring
  61. * 52xZZ - data represented as array of ZZ digits in 52-bit radix
  62. * _x1_/_x2_ - 1 or 2 independent inputs/outputs
  63. * _ifma256 - uses 256-bit wide IFMA ISA (AVX512_IFMA256)
  64. */
  65. void ossl_rsaz_amm52x20_x1_ifma256(BN_ULONG *res, const BN_ULONG *a,
  66. const BN_ULONG *b, const BN_ULONG *m,
  67. BN_ULONG k0);
  68. void ossl_rsaz_amm52x20_x2_ifma256(BN_ULONG *out, const BN_ULONG *a,
  69. const BN_ULONG *b, const BN_ULONG *m,
  70. const BN_ULONG k0[2]);
  71. void ossl_extract_multiplier_2x20_win5(BN_ULONG *red_Y,
  72. const BN_ULONG *red_table,
  73. int red_table_idx1, int red_table_idx2);
  74. void ossl_rsaz_amm52x30_x1_ifma256(BN_ULONG *res, const BN_ULONG *a,
  75. const BN_ULONG *b, const BN_ULONG *m,
  76. BN_ULONG k0);
  77. void ossl_rsaz_amm52x30_x2_ifma256(BN_ULONG *out, const BN_ULONG *a,
  78. const BN_ULONG *b, const BN_ULONG *m,
  79. const BN_ULONG k0[2]);
  80. void ossl_extract_multiplier_2x30_win5(BN_ULONG *red_Y,
  81. const BN_ULONG *red_table,
  82. int red_table_idx1, int red_table_idx2);
  83. void ossl_rsaz_amm52x40_x1_ifma256(BN_ULONG *res, const BN_ULONG *a,
  84. const BN_ULONG *b, const BN_ULONG *m,
  85. BN_ULONG k0);
  86. void ossl_rsaz_amm52x40_x2_ifma256(BN_ULONG *out, const BN_ULONG *a,
  87. const BN_ULONG *b, const BN_ULONG *m,
  88. const BN_ULONG k0[2]);
  89. void ossl_extract_multiplier_2x40_win5(BN_ULONG *red_Y,
  90. const BN_ULONG *red_table,
  91. int red_table_idx1, int red_table_idx2);
  92. static int RSAZ_mod_exp_x2_ifma256(BN_ULONG *res, const BN_ULONG *base,
  93. const BN_ULONG *exp[2], const BN_ULONG *m,
  94. const BN_ULONG *rr, const BN_ULONG k0[2],
  95. int modulus_bitsize);
  96. /*
  97. * Dual Montgomery modular exponentiation using prime moduli of the
  98. * same bit size, optimized with AVX512 ISA.
  99. *
  100. * Input and output parameters for each exponentiation are independent and
  101. * denoted here by index |i|, i = 1..2.
  102. *
  103. * Input and output are all in regular 2^64 radix.
  104. *
  105. * Each moduli shall be |factor_size| bit size.
  106. *
  107. * Supported cases:
  108. * - 2x1024
  109. * - 2x1536
  110. * - 2x2048
  111. *
  112. * [out] res|i| - result of modular exponentiation: array of qword values
  113. * in regular (2^64) radix. Size of array shall be enough
  114. * to hold |factor_size| bits.
  115. * [in] base|i| - base
  116. * [in] exp|i| - exponent
  117. * [in] m|i| - moduli
  118. * [in] rr|i| - Montgomery parameter RR = R^2 mod m|i|
  119. * [in] k0_|i| - Montgomery parameter k0 = -1/m|i| mod 2^64
  120. * [in] factor_size - moduli bit size
  121. *
  122. * \return 0 in case of failure,
  123. * 1 in case of success.
  124. */
  125. int ossl_rsaz_mod_exp_avx512_x2(BN_ULONG *res1,
  126. const BN_ULONG *base1,
  127. const BN_ULONG *exp1,
  128. const BN_ULONG *m1,
  129. const BN_ULONG *rr1,
  130. BN_ULONG k0_1,
  131. BN_ULONG *res2,
  132. const BN_ULONG *base2,
  133. const BN_ULONG *exp2,
  134. const BN_ULONG *m2,
  135. const BN_ULONG *rr2,
  136. BN_ULONG k0_2,
  137. int factor_size)
  138. {
  139. typedef void (*AMM)(BN_ULONG *res, const BN_ULONG *a,
  140. const BN_ULONG *b, const BN_ULONG *m, BN_ULONG k0);
  141. int ret = 0;
  142. /*
  143. * Number of word-size (BN_ULONG) digits to store exponent in redundant
  144. * representation.
  145. */
  146. int exp_digits = number_of_digits(factor_size + 2, DIGIT_SIZE);
  147. int coeff_pow = 4 * (DIGIT_SIZE * exp_digits - factor_size);
  148. /* Number of YMM registers required to store exponent's digits */
  149. int ymm_regs_num = NUMBER_OF_REGISTERS(exp_digits, 256 /* ymm bit size */);
  150. /* Capacity of the register set (in qwords) to store exponent */
  151. int regs_capacity = ymm_regs_num * 4;
  152. BN_ULONG *base1_red, *m1_red, *rr1_red;
  153. BN_ULONG *base2_red, *m2_red, *rr2_red;
  154. BN_ULONG *coeff_red;
  155. BN_ULONG *storage = NULL;
  156. BN_ULONG *storage_aligned = NULL;
  157. int storage_len_bytes = 7 * regs_capacity * sizeof(BN_ULONG)
  158. + 64 /* alignment */;
  159. const BN_ULONG *exp[2] = {0};
  160. BN_ULONG k0[2] = {0};
  161. /* AMM = Almost Montgomery Multiplication */
  162. AMM amm = NULL;
  163. switch (factor_size) {
  164. case 1024:
  165. amm = ossl_rsaz_amm52x20_x1_ifma256;
  166. break;
  167. case 1536:
  168. amm = ossl_rsaz_amm52x30_x1_ifma256;
  169. break;
  170. case 2048:
  171. amm = ossl_rsaz_amm52x40_x1_ifma256;
  172. break;
  173. default:
  174. goto err;
  175. }
  176. storage = (BN_ULONG *)OPENSSL_malloc(storage_len_bytes);
  177. if (storage == NULL)
  178. goto err;
  179. storage_aligned = (BN_ULONG *)ALIGN_OF(storage, 64);
  180. /* Memory layout for red(undant) representations */
  181. base1_red = storage_aligned;
  182. base2_red = storage_aligned + 1 * regs_capacity;
  183. m1_red = storage_aligned + 2 * regs_capacity;
  184. m2_red = storage_aligned + 3 * regs_capacity;
  185. rr1_red = storage_aligned + 4 * regs_capacity;
  186. rr2_red = storage_aligned + 5 * regs_capacity;
  187. coeff_red = storage_aligned + 6 * regs_capacity;
  188. /* Convert base_i, m_i, rr_i, from regular to 52-bit radix */
  189. to_words52(base1_red, regs_capacity, base1, factor_size);
  190. to_words52(base2_red, regs_capacity, base2, factor_size);
  191. to_words52(m1_red, regs_capacity, m1, factor_size);
  192. to_words52(m2_red, regs_capacity, m2, factor_size);
  193. to_words52(rr1_red, regs_capacity, rr1, factor_size);
  194. to_words52(rr2_red, regs_capacity, rr2, factor_size);
  195. /*
  196. * Compute target domain Montgomery converters RR' for each modulus
  197. * based on precomputed original domain's RR.
  198. *
  199. * RR -> RR' transformation steps:
  200. * (1) coeff = 2^k
  201. * (2) t = AMM(RR,RR) = RR^2 / R' mod m
  202. * (3) RR' = AMM(t, coeff) = RR^2 * 2^k / R'^2 mod m
  203. * where
  204. * k = 4 * (52 * digits52 - modlen)
  205. * R = 2^(64 * ceil(modlen/64)) mod m
  206. * RR = R^2 mod m
  207. * R' = 2^(52 * ceil(modlen/52)) mod m
  208. *
  209. * EX/ modlen = 1024: k = 64, RR = 2^2048 mod m, RR' = 2^2080 mod m
  210. */
  211. memset(coeff_red, 0, exp_digits * sizeof(BN_ULONG));
  212. /* (1) in reduced domain representation */
  213. set_bit(coeff_red, 64 * (int)(coeff_pow / 52) + coeff_pow % 52);
  214. amm(rr1_red, rr1_red, rr1_red, m1_red, k0_1); /* (2) for m1 */
  215. amm(rr1_red, rr1_red, coeff_red, m1_red, k0_1); /* (3) for m1 */
  216. amm(rr2_red, rr2_red, rr2_red, m2_red, k0_2); /* (2) for m2 */
  217. amm(rr2_red, rr2_red, coeff_red, m2_red, k0_2); /* (3) for m2 */
  218. exp[0] = exp1;
  219. exp[1] = exp2;
  220. k0[0] = k0_1;
  221. k0[1] = k0_2;
  222. /* Dual (2-exps in parallel) exponentiation */
  223. ret = RSAZ_mod_exp_x2_ifma256(rr1_red, base1_red, exp, m1_red, rr1_red,
  224. k0, factor_size);
  225. if (!ret)
  226. goto err;
  227. /* Convert rr_i back to regular radix */
  228. from_words52(res1, factor_size, rr1_red);
  229. from_words52(res2, factor_size, rr2_red);
  230. /* bn_reduce_once_in_place expects number of BN_ULONG, not bit size */
  231. factor_size /= sizeof(BN_ULONG) * 8;
  232. bn_reduce_once_in_place(res1, /*carry=*/0, m1, storage, factor_size);
  233. bn_reduce_once_in_place(res2, /*carry=*/0, m2, storage, factor_size);
  234. err:
  235. if (storage != NULL) {
  236. OPENSSL_cleanse(storage, storage_len_bytes);
  237. OPENSSL_free(storage);
  238. }
  239. return ret;
  240. }
  241. /*
  242. * Dual {1024,1536,2048}-bit w-ary modular exponentiation using prime moduli of
  243. * the same bit size using Almost Montgomery Multiplication, optimized with
  244. * AVX512_IFMA256 ISA.
  245. *
  246. * The parameter w (window size) = 5.
  247. *
  248. * [out] res - result of modular exponentiation: 2x{20,30,40} qword
  249. * values in 2^52 radix.
  250. * [in] base - base (2x{20,30,40} qword values in 2^52 radix)
  251. * [in] exp - array of 2 pointers to {16,24,32} qword values in 2^64 radix.
  252. * Exponent is not converted to redundant representation.
  253. * [in] m - moduli (2x{20,30,40} qword values in 2^52 radix)
  254. * [in] rr - Montgomery parameter for 2 moduli:
  255. * RR(1024) = 2^2080 mod m.
  256. * RR(1536) = 2^3120 mod m.
  257. * RR(2048) = 2^4160 mod m.
  258. * (2x{20,30,40} qword values in 2^52 radix)
  259. * [in] k0 - Montgomery parameter for 2 moduli: k0 = -1/m mod 2^64
  260. *
  261. * \return (void).
  262. */
  263. int RSAZ_mod_exp_x2_ifma256(BN_ULONG *out,
  264. const BN_ULONG *base,
  265. const BN_ULONG *exp[2],
  266. const BN_ULONG *m,
  267. const BN_ULONG *rr,
  268. const BN_ULONG k0[2],
  269. int modulus_bitsize)
  270. {
  271. typedef void (*DAMM)(BN_ULONG *res, const BN_ULONG *a,
  272. const BN_ULONG *b, const BN_ULONG *m,
  273. const BN_ULONG k0[2]);
  274. typedef void (*DEXTRACT)(BN_ULONG *res, const BN_ULONG *red_table,
  275. int red_table_idx, int tbl_idx);
  276. int ret = 0;
  277. int idx;
  278. /* Exponent window size */
  279. int exp_win_size = 5;
  280. int exp_win_mask = (1U << exp_win_size) - 1;
  281. /*
  282. * Number of digits (64-bit words) in redundant representation to handle
  283. * modulus bits
  284. */
  285. int red_digits = 0;
  286. int exp_digits = 0;
  287. BN_ULONG *storage = NULL;
  288. BN_ULONG *storage_aligned = NULL;
  289. int storage_len_bytes = 0;
  290. /* Red(undant) result Y and multiplier X */
  291. BN_ULONG *red_Y = NULL; /* [2][red_digits] */
  292. BN_ULONG *red_X = NULL; /* [2][red_digits] */
  293. /* Pre-computed table of base powers */
  294. BN_ULONG *red_table = NULL; /* [1U << exp_win_size][2][red_digits] */
  295. /* Expanded exponent */
  296. BN_ULONG *expz = NULL; /* [2][exp_digits + 1] */
  297. /* Dual AMM */
  298. DAMM damm = NULL;
  299. /* Extractor from red_table */
  300. DEXTRACT extract = NULL;
  301. /*
  302. * Squaring is done using multiplication now. That can be a subject of
  303. * optimization in future.
  304. */
  305. # define DAMS(r,a,m,k0) damm((r),(a),(a),(m),(k0))
  306. switch (modulus_bitsize) {
  307. case 1024:
  308. red_digits = 20;
  309. exp_digits = 16;
  310. damm = ossl_rsaz_amm52x20_x2_ifma256;
  311. extract = ossl_extract_multiplier_2x20_win5;
  312. break;
  313. case 1536:
  314. /* Extended with 2 digits padding to avoid mask ops in high YMM register */
  315. red_digits = 30 + 2;
  316. exp_digits = 24;
  317. damm = ossl_rsaz_amm52x30_x2_ifma256;
  318. extract = ossl_extract_multiplier_2x30_win5;
  319. break;
  320. case 2048:
  321. red_digits = 40;
  322. exp_digits = 32;
  323. damm = ossl_rsaz_amm52x40_x2_ifma256;
  324. extract = ossl_extract_multiplier_2x40_win5;
  325. break;
  326. default:
  327. goto err;
  328. }
  329. storage_len_bytes = (2 * red_digits /* red_Y */
  330. + 2 * red_digits /* red_X */
  331. + 2 * red_digits * (1U << exp_win_size) /* red_table */
  332. + 2 * (exp_digits + 1)) /* expz */
  333. * sizeof(BN_ULONG)
  334. + 64; /* alignment */
  335. storage = (BN_ULONG *)OPENSSL_zalloc(storage_len_bytes);
  336. if (storage == NULL)
  337. goto err;
  338. storage_aligned = (BN_ULONG *)ALIGN_OF(storage, 64);
  339. red_Y = storage_aligned;
  340. red_X = red_Y + 2 * red_digits;
  341. red_table = red_X + 2 * red_digits;
  342. expz = red_table + 2 * red_digits * (1U << exp_win_size);
  343. /*
  344. * Compute table of powers base^i, i = 0, ..., (2^EXP_WIN_SIZE) - 1
  345. * table[0] = mont(x^0) = mont(1)
  346. * table[1] = mont(x^1) = mont(x)
  347. */
  348. red_X[0 * red_digits] = 1;
  349. red_X[1 * red_digits] = 1;
  350. damm(&red_table[0 * 2 * red_digits], (const BN_ULONG*)red_X, rr, m, k0);
  351. damm(&red_table[1 * 2 * red_digits], base, rr, m, k0);
  352. for (idx = 1; idx < (int)((1U << exp_win_size) / 2); idx++) {
  353. DAMS(&red_table[(2 * idx + 0) * 2 * red_digits],
  354. &red_table[(1 * idx) * 2 * red_digits], m, k0);
  355. damm(&red_table[(2 * idx + 1) * 2 * red_digits],
  356. &red_table[(2 * idx) * 2 * red_digits],
  357. &red_table[1 * 2 * red_digits], m, k0);
  358. }
  359. /* Copy and expand exponents */
  360. memcpy(&expz[0 * (exp_digits + 1)], exp[0], exp_digits * sizeof(BN_ULONG));
  361. expz[1 * (exp_digits + 1) - 1] = 0;
  362. memcpy(&expz[1 * (exp_digits + 1)], exp[1], exp_digits * sizeof(BN_ULONG));
  363. expz[2 * (exp_digits + 1) - 1] = 0;
  364. /* Exponentiation */
  365. {
  366. const int rem = modulus_bitsize % exp_win_size;
  367. const BN_ULONG table_idx_mask = exp_win_mask;
  368. int exp_bit_no = modulus_bitsize - rem;
  369. int exp_chunk_no = exp_bit_no / 64;
  370. int exp_chunk_shift = exp_bit_no % 64;
  371. BN_ULONG red_table_idx_0, red_table_idx_1;
  372. /*
  373. * If rem == 0, then
  374. * exp_bit_no = modulus_bitsize - exp_win_size
  375. * However, this isn't possible because rem is { 1024, 1536, 2048 } % 5
  376. * which is { 4, 1, 3 } respectively.
  377. *
  378. * If this assertion ever fails the fix above is easy.
  379. */
  380. OPENSSL_assert(rem != 0);
  381. /* Process 1-st exp window - just init result */
  382. red_table_idx_0 = expz[exp_chunk_no + 0 * (exp_digits + 1)];
  383. red_table_idx_1 = expz[exp_chunk_no + 1 * (exp_digits + 1)];
  384. /*
  385. * The function operates with fixed moduli sizes divisible by 64,
  386. * thus table index here is always in supported range [0, EXP_WIN_SIZE).
  387. */
  388. red_table_idx_0 >>= exp_chunk_shift;
  389. red_table_idx_1 >>= exp_chunk_shift;
  390. extract(&red_Y[0 * red_digits], (const BN_ULONG*)red_table, (int)red_table_idx_0, (int)red_table_idx_1);
  391. /* Process other exp windows */
  392. for (exp_bit_no -= exp_win_size; exp_bit_no >= 0; exp_bit_no -= exp_win_size) {
  393. /* Extract pre-computed multiplier from the table */
  394. {
  395. BN_ULONG T;
  396. exp_chunk_no = exp_bit_no / 64;
  397. exp_chunk_shift = exp_bit_no % 64;
  398. {
  399. red_table_idx_0 = expz[exp_chunk_no + 0 * (exp_digits + 1)];
  400. T = expz[exp_chunk_no + 1 + 0 * (exp_digits + 1)];
  401. red_table_idx_0 >>= exp_chunk_shift;
  402. /*
  403. * Get additional bits from then next quadword
  404. * when 64-bit boundaries are crossed.
  405. */
  406. if (exp_chunk_shift > 64 - exp_win_size) {
  407. T <<= (64 - exp_chunk_shift);
  408. red_table_idx_0 ^= T;
  409. }
  410. red_table_idx_0 &= table_idx_mask;
  411. }
  412. {
  413. red_table_idx_1 = expz[exp_chunk_no + 1 * (exp_digits + 1)];
  414. T = expz[exp_chunk_no + 1 + 1 * (exp_digits + 1)];
  415. red_table_idx_1 >>= exp_chunk_shift;
  416. /*
  417. * Get additional bits from then next quadword
  418. * when 64-bit boundaries are crossed.
  419. */
  420. if (exp_chunk_shift > 64 - exp_win_size) {
  421. T <<= (64 - exp_chunk_shift);
  422. red_table_idx_1 ^= T;
  423. }
  424. red_table_idx_1 &= table_idx_mask;
  425. }
  426. extract(&red_X[0 * red_digits], (const BN_ULONG*)red_table, (int)red_table_idx_0, (int)red_table_idx_1);
  427. }
  428. /* Series of squaring */
  429. DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0);
  430. DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0);
  431. DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0);
  432. DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0);
  433. DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0);
  434. damm((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, (const BN_ULONG*)red_X, m, k0);
  435. }
  436. }
  437. /*
  438. *
  439. * NB: After the last AMM of exponentiation in Montgomery domain, the result
  440. * may be (modulus_bitsize + 1), but the conversion out of Montgomery domain
  441. * performs an AMM(x,1) which guarantees that the final result is less than
  442. * |m|, so no conditional subtraction is needed here. See [1] for details.
  443. *
  444. * [1] Gueron, S. Efficient software implementations of modular exponentiation.
  445. * DOI: 10.1007/s13389-012-0031-5
  446. */
  447. /* Convert result back in regular 2^52 domain */
  448. memset(red_X, 0, 2 * red_digits * sizeof(BN_ULONG));
  449. red_X[0 * red_digits] = 1;
  450. red_X[1 * red_digits] = 1;
  451. damm(out, (const BN_ULONG*)red_Y, (const BN_ULONG*)red_X, m, k0);
  452. ret = 1;
  453. err:
  454. if (storage != NULL) {
  455. /* Clear whole storage */
  456. OPENSSL_cleanse(storage, storage_len_bytes);
  457. OPENSSL_free(storage);
  458. }
  459. #undef DAMS
  460. return ret;
  461. }
  462. static ossl_inline uint64_t get_digit(const uint8_t *in, int in_len)
  463. {
  464. uint64_t digit = 0;
  465. assert(in != NULL);
  466. assert(in_len <= 8);
  467. for (; in_len > 0; in_len--) {
  468. digit <<= 8;
  469. digit += (uint64_t)(in[in_len - 1]);
  470. }
  471. return digit;
  472. }
  473. /*
  474. * Convert array of words in regular (base=2^64) representation to array of
  475. * words in redundant (base=2^52) one.
  476. */
  477. static void to_words52(BN_ULONG *out, int out_len,
  478. const BN_ULONG *in, int in_bitsize)
  479. {
  480. uint8_t *in_str = NULL;
  481. assert(out != NULL);
  482. assert(in != NULL);
  483. /* Check destination buffer capacity */
  484. assert(out_len >= number_of_digits(in_bitsize, DIGIT_SIZE));
  485. in_str = (uint8_t *)in;
  486. for (; in_bitsize >= (2 * DIGIT_SIZE); in_bitsize -= (2 * DIGIT_SIZE), out += 2) {
  487. out[0] = (*(uint64_t_align1 *)in_str) & DIGIT_MASK;
  488. in_str += 6;
  489. out[1] = ((*(uint64_t_align1 *)in_str) >> 4) & DIGIT_MASK;
  490. in_str += 7;
  491. out_len -= 2;
  492. }
  493. if (in_bitsize > DIGIT_SIZE) {
  494. uint64_t digit = get_digit(in_str, 7);
  495. out[0] = digit & DIGIT_MASK;
  496. in_str += 6;
  497. in_bitsize -= DIGIT_SIZE;
  498. digit = get_digit(in_str, BITS2WORD8_SIZE(in_bitsize));
  499. out[1] = digit >> 4;
  500. out += 2;
  501. out_len -= 2;
  502. } else if (in_bitsize > 0) {
  503. out[0] = get_digit(in_str, BITS2WORD8_SIZE(in_bitsize));
  504. out++;
  505. out_len--;
  506. }
  507. while (out_len > 0) {
  508. *out = 0;
  509. out_len--;
  510. out++;
  511. }
  512. }
  513. static ossl_inline void put_digit(uint8_t *out, int out_len, uint64_t digit)
  514. {
  515. assert(out != NULL);
  516. assert(out_len <= 8);
  517. for (; out_len > 0; out_len--) {
  518. *out++ = (uint8_t)(digit & 0xFF);
  519. digit >>= 8;
  520. }
  521. }
  522. /*
  523. * Convert array of words in redundant (base=2^52) representation to array of
  524. * words in regular (base=2^64) one.
  525. */
  526. static void from_words52(BN_ULONG *out, int out_bitsize, const BN_ULONG *in)
  527. {
  528. int i;
  529. int out_len = BITS2WORD64_SIZE(out_bitsize);
  530. assert(out != NULL);
  531. assert(in != NULL);
  532. for (i = 0; i < out_len; i++)
  533. out[i] = 0;
  534. {
  535. uint8_t *out_str = (uint8_t *)out;
  536. for (; out_bitsize >= (2 * DIGIT_SIZE);
  537. out_bitsize -= (2 * DIGIT_SIZE), in += 2) {
  538. (*(uint64_t_align1 *)out_str) = in[0];
  539. out_str += 6;
  540. (*(uint64_t_align1 *)out_str) ^= in[1] << 4;
  541. out_str += 7;
  542. }
  543. if (out_bitsize > DIGIT_SIZE) {
  544. put_digit(out_str, 7, in[0]);
  545. out_str += 6;
  546. out_bitsize -= DIGIT_SIZE;
  547. put_digit(out_str, BITS2WORD8_SIZE(out_bitsize),
  548. (in[1] << 4 | in[0] >> 48));
  549. } else if (out_bitsize) {
  550. put_digit(out_str, BITS2WORD8_SIZE(out_bitsize), in[0]);
  551. }
  552. }
  553. }
  554. /*
  555. * Set bit at index |idx| in the words array |a|.
  556. * It does not do any boundaries checks, make sure the index is valid before
  557. * calling the function.
  558. */
  559. static ossl_inline void set_bit(BN_ULONG *a, int idx)
  560. {
  561. assert(a != NULL);
  562. {
  563. int i, j;
  564. i = idx / BN_BITS2;
  565. j = idx % BN_BITS2;
  566. a[i] |= (((BN_ULONG)1) << j);
  567. }
  568. }
  569. #endif