2
0

rsaz_exp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2013-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2012, 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. * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)
  11. * (1) Intel Corporation, Israel Development Center, Haifa, Israel
  12. * (2) University of Haifa, Israel
  13. */
  14. #include <openssl/opensslconf.h>
  15. #include "internal/common.h"
  16. #include "rsaz_exp.h"
  17. #ifndef RSAZ_ENABLED
  18. NON_EMPTY_TRANSLATION_UNIT
  19. #else
  20. /*
  21. * See crypto/bn/asm/rsaz-avx2.pl for further details.
  22. */
  23. void rsaz_1024_norm2red_avx2(void *red, const void *norm);
  24. void rsaz_1024_mul_avx2(void *ret, const void *a, const void *b,
  25. const void *n, BN_ULONG k);
  26. void rsaz_1024_sqr_avx2(void *ret, const void *a, const void *n, BN_ULONG k,
  27. int cnt);
  28. void rsaz_1024_scatter5_avx2(void *tbl, const void *val, int i);
  29. void rsaz_1024_gather5_avx2(void *val, const void *tbl, int i);
  30. void rsaz_1024_red2norm_avx2(void *norm, const void *red);
  31. #if defined(__SUNPRO_C)
  32. # pragma align 64(one,two80)
  33. #endif
  34. ALIGN64 static const BN_ULONG one[40] = {
  35. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  37. };
  38. ALIGN64 static const BN_ULONG two80[40] = {
  39. 0, 0, 1 << 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  41. };
  42. void RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16],
  43. const BN_ULONG base_norm[16],
  44. const BN_ULONG exponent[16],
  45. const BN_ULONG m_norm[16], const BN_ULONG RR[16],
  46. BN_ULONG k0)
  47. {
  48. unsigned char storage[320 * 3 + 32 * 9 * 16 + 64]; /* 5.5KB */
  49. unsigned char *p_str = storage + (64 - ((size_t)storage % 64));
  50. unsigned char *a_inv, *m, *result;
  51. unsigned char *table_s = p_str + 320 * 3;
  52. unsigned char *R2 = table_s; /* borrow */
  53. int index;
  54. int wvalue;
  55. BN_ULONG tmp[16];
  56. if ((((size_t)p_str & 4095) + 320) >> 12) {
  57. result = p_str;
  58. a_inv = p_str + 320;
  59. m = p_str + 320 * 2; /* should not cross page */
  60. } else {
  61. m = p_str; /* should not cross page */
  62. result = p_str + 320;
  63. a_inv = p_str + 320 * 2;
  64. }
  65. rsaz_1024_norm2red_avx2(m, m_norm);
  66. rsaz_1024_norm2red_avx2(a_inv, base_norm);
  67. rsaz_1024_norm2red_avx2(R2, RR);
  68. rsaz_1024_mul_avx2(R2, R2, R2, m, k0);
  69. rsaz_1024_mul_avx2(R2, R2, two80, m, k0);
  70. /* table[0] = 1 */
  71. rsaz_1024_mul_avx2(result, R2, one, m, k0);
  72. /* table[1] = a_inv^1 */
  73. rsaz_1024_mul_avx2(a_inv, a_inv, R2, m, k0);
  74. rsaz_1024_scatter5_avx2(table_s, result, 0);
  75. rsaz_1024_scatter5_avx2(table_s, a_inv, 1);
  76. /* table[2] = a_inv^2 */
  77. rsaz_1024_sqr_avx2(result, a_inv, m, k0, 1);
  78. rsaz_1024_scatter5_avx2(table_s, result, 2);
  79. #if 0
  80. /* this is almost 2x smaller and less than 1% slower */
  81. for (index = 3; index < 32; index++) {
  82. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  83. rsaz_1024_scatter5_avx2(table_s, result, index);
  84. }
  85. #else
  86. /* table[4] = a_inv^4 */
  87. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  88. rsaz_1024_scatter5_avx2(table_s, result, 4);
  89. /* table[8] = a_inv^8 */
  90. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  91. rsaz_1024_scatter5_avx2(table_s, result, 8);
  92. /* table[16] = a_inv^16 */
  93. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  94. rsaz_1024_scatter5_avx2(table_s, result, 16);
  95. /* table[17] = a_inv^17 */
  96. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  97. rsaz_1024_scatter5_avx2(table_s, result, 17);
  98. /* table[3] */
  99. rsaz_1024_gather5_avx2(result, table_s, 2);
  100. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  101. rsaz_1024_scatter5_avx2(table_s, result, 3);
  102. /* table[6] */
  103. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  104. rsaz_1024_scatter5_avx2(table_s, result, 6);
  105. /* table[12] */
  106. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  107. rsaz_1024_scatter5_avx2(table_s, result, 12);
  108. /* table[24] */
  109. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  110. rsaz_1024_scatter5_avx2(table_s, result, 24);
  111. /* table[25] */
  112. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  113. rsaz_1024_scatter5_avx2(table_s, result, 25);
  114. /* table[5] */
  115. rsaz_1024_gather5_avx2(result, table_s, 4);
  116. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  117. rsaz_1024_scatter5_avx2(table_s, result, 5);
  118. /* table[10] */
  119. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  120. rsaz_1024_scatter5_avx2(table_s, result, 10);
  121. /* table[20] */
  122. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  123. rsaz_1024_scatter5_avx2(table_s, result, 20);
  124. /* table[21] */
  125. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  126. rsaz_1024_scatter5_avx2(table_s, result, 21);
  127. /* table[7] */
  128. rsaz_1024_gather5_avx2(result, table_s, 6);
  129. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  130. rsaz_1024_scatter5_avx2(table_s, result, 7);
  131. /* table[14] */
  132. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  133. rsaz_1024_scatter5_avx2(table_s, result, 14);
  134. /* table[28] */
  135. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  136. rsaz_1024_scatter5_avx2(table_s, result, 28);
  137. /* table[29] */
  138. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  139. rsaz_1024_scatter5_avx2(table_s, result, 29);
  140. /* table[9] */
  141. rsaz_1024_gather5_avx2(result, table_s, 8);
  142. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  143. rsaz_1024_scatter5_avx2(table_s, result, 9);
  144. /* table[18] */
  145. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  146. rsaz_1024_scatter5_avx2(table_s, result, 18);
  147. /* table[19] */
  148. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  149. rsaz_1024_scatter5_avx2(table_s, result, 19);
  150. /* table[11] */
  151. rsaz_1024_gather5_avx2(result, table_s, 10);
  152. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  153. rsaz_1024_scatter5_avx2(table_s, result, 11);
  154. /* table[22] */
  155. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  156. rsaz_1024_scatter5_avx2(table_s, result, 22);
  157. /* table[23] */
  158. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  159. rsaz_1024_scatter5_avx2(table_s, result, 23);
  160. /* table[13] */
  161. rsaz_1024_gather5_avx2(result, table_s, 12);
  162. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  163. rsaz_1024_scatter5_avx2(table_s, result, 13);
  164. /* table[26] */
  165. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  166. rsaz_1024_scatter5_avx2(table_s, result, 26);
  167. /* table[27] */
  168. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  169. rsaz_1024_scatter5_avx2(table_s, result, 27);
  170. /* table[15] */
  171. rsaz_1024_gather5_avx2(result, table_s, 14);
  172. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  173. rsaz_1024_scatter5_avx2(table_s, result, 15);
  174. /* table[30] */
  175. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  176. rsaz_1024_scatter5_avx2(table_s, result, 30);
  177. /* table[31] */
  178. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  179. rsaz_1024_scatter5_avx2(table_s, result, 31);
  180. #endif
  181. /* load first window */
  182. p_str = (unsigned char *)exponent;
  183. wvalue = p_str[127] >> 3;
  184. rsaz_1024_gather5_avx2(result, table_s, wvalue);
  185. index = 1014;
  186. while (index > -1) { /* loop for the remaining 127 windows */
  187. rsaz_1024_sqr_avx2(result, result, m, k0, 5);
  188. wvalue = (p_str[(index / 8) + 1] << 8) | p_str[index / 8];
  189. wvalue = (wvalue >> (index % 8)) & 31;
  190. index -= 5;
  191. rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); /* borrow a_inv */
  192. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  193. }
  194. /* square four times */
  195. rsaz_1024_sqr_avx2(result, result, m, k0, 4);
  196. wvalue = p_str[0] & 15;
  197. rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); /* borrow a_inv */
  198. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  199. /* from Montgomery */
  200. rsaz_1024_mul_avx2(result, result, one, m, k0);
  201. rsaz_1024_red2norm_avx2(result_norm, result);
  202. bn_reduce_once_in_place(result_norm, /*carry=*/0, m_norm, tmp, 16);
  203. OPENSSL_cleanse(storage, sizeof(storage));
  204. OPENSSL_cleanse(tmp, sizeof(tmp));
  205. }
  206. /*
  207. * See crypto/bn/rsaz-x86_64.pl for further details.
  208. */
  209. void rsaz_512_mul(void *ret, const void *a, const void *b, const void *n,
  210. BN_ULONG k);
  211. void rsaz_512_mul_scatter4(void *ret, const void *a, const void *n,
  212. BN_ULONG k, const void *tbl, unsigned int power);
  213. void rsaz_512_mul_gather4(void *ret, const void *a, const void *tbl,
  214. const void *n, BN_ULONG k, unsigned int power);
  215. void rsaz_512_mul_by_one(void *ret, const void *a, const void *n, BN_ULONG k);
  216. void rsaz_512_sqr(void *ret, const void *a, const void *n, BN_ULONG k,
  217. int cnt);
  218. void rsaz_512_scatter4(void *tbl, const BN_ULONG *val, int power);
  219. void rsaz_512_gather4(BN_ULONG *val, const void *tbl, int power);
  220. void RSAZ_512_mod_exp(BN_ULONG result[8],
  221. const BN_ULONG base[8], const BN_ULONG exponent[8],
  222. const BN_ULONG m[8], BN_ULONG k0, const BN_ULONG RR[8])
  223. {
  224. unsigned char storage[16 * 8 * 8 + 64 * 2 + 64]; /* 1.2KB */
  225. unsigned char *table = storage + (64 - ((size_t)storage % 64));
  226. BN_ULONG *a_inv = (BN_ULONG *)(table + 16 * 8 * 8);
  227. BN_ULONG *temp = (BN_ULONG *)(table + 16 * 8 * 8 + 8 * 8);
  228. unsigned char *p_str = (unsigned char *)exponent;
  229. int index;
  230. unsigned int wvalue;
  231. BN_ULONG tmp[8];
  232. /* table[0] = 1_inv */
  233. temp[0] = 0 - m[0];
  234. temp[1] = ~m[1];
  235. temp[2] = ~m[2];
  236. temp[3] = ~m[3];
  237. temp[4] = ~m[4];
  238. temp[5] = ~m[5];
  239. temp[6] = ~m[6];
  240. temp[7] = ~m[7];
  241. rsaz_512_scatter4(table, temp, 0);
  242. /* table [1] = a_inv^1 */
  243. rsaz_512_mul(a_inv, base, RR, m, k0);
  244. rsaz_512_scatter4(table, a_inv, 1);
  245. /* table [2] = a_inv^2 */
  246. rsaz_512_sqr(temp, a_inv, m, k0, 1);
  247. rsaz_512_scatter4(table, temp, 2);
  248. for (index = 3; index < 16; index++)
  249. rsaz_512_mul_scatter4(temp, a_inv, m, k0, table, index);
  250. /* load first window */
  251. wvalue = p_str[63];
  252. rsaz_512_gather4(temp, table, wvalue >> 4);
  253. rsaz_512_sqr(temp, temp, m, k0, 4);
  254. rsaz_512_mul_gather4(temp, temp, table, m, k0, wvalue & 0xf);
  255. for (index = 62; index >= 0; index--) {
  256. wvalue = p_str[index];
  257. rsaz_512_sqr(temp, temp, m, k0, 4);
  258. rsaz_512_mul_gather4(temp, temp, table, m, k0, wvalue >> 4);
  259. rsaz_512_sqr(temp, temp, m, k0, 4);
  260. rsaz_512_mul_gather4(temp, temp, table, m, k0, wvalue & 0x0f);
  261. }
  262. /* from Montgomery */
  263. rsaz_512_mul_by_one(result, temp, m, k0);
  264. bn_reduce_once_in_place(result, /*carry=*/0, m, tmp, 8);
  265. OPENSSL_cleanse(storage, sizeof(storage));
  266. OPENSSL_cleanse(tmp, sizeof(tmp));
  267. }
  268. #endif