rsaz_exp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. /*****************************************************************************
  10. * *
  11. * Copyright (c) 2012, Intel Corporation *
  12. * *
  13. * All rights reserved. *
  14. * *
  15. * Redistribution and use in source and binary forms, with or without *
  16. * modification, are permitted provided that the following conditions are *
  17. * met: *
  18. * *
  19. * * Redistributions of source code must retain the above copyright *
  20. * notice, this list of conditions and the following disclaimer. *
  21. * *
  22. * * Redistributions in binary form must reproduce the above copyright *
  23. * notice, this list of conditions and the following disclaimer in the *
  24. * documentation and/or other materials provided with the *
  25. * distribution. *
  26. * *
  27. * * Neither the name of the Intel Corporation nor the names of its *
  28. * contributors may be used to endorse or promote products derived from *
  29. * this software without specific prior written permission. *
  30. * *
  31. * *
  32. * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION ""AS IS"" AND ANY *
  33. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR *
  35. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR *
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
  37. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
  38. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
  39. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  40. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
  41. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
  42. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
  43. * *
  44. ******************************************************************************
  45. * Developers and authors: *
  46. * Shay Gueron (1, 2), and Vlad Krasnov (1) *
  47. * (1) Intel Corporation, Israel Development Center, Haifa, Israel *
  48. * (2) University of Haifa, Israel *
  49. *****************************************************************************/
  50. #include <openssl/opensslconf.h>
  51. #include "rsaz_exp.h"
  52. #ifndef RSAZ_ENABLED
  53. NON_EMPTY_TRANSLATION_UNIT
  54. #else
  55. /*
  56. * See crypto/bn/asm/rsaz-avx2.pl for further details.
  57. */
  58. void rsaz_1024_norm2red_avx2(void *red, const void *norm);
  59. void rsaz_1024_mul_avx2(void *ret, const void *a, const void *b,
  60. const void *n, BN_ULONG k);
  61. void rsaz_1024_sqr_avx2(void *ret, const void *a, const void *n, BN_ULONG k,
  62. int cnt);
  63. void rsaz_1024_scatter5_avx2(void *tbl, const void *val, int i);
  64. void rsaz_1024_gather5_avx2(void *val, const void *tbl, int i);
  65. void rsaz_1024_red2norm_avx2(void *norm, const void *red);
  66. #if defined(__GNUC__)
  67. # define ALIGN64 __attribute__((aligned(64)))
  68. #elif defined(_MSC_VER)
  69. # define ALIGN64 __declspec(align(64))
  70. #elif defined(__SUNPRO_C)
  71. # define ALIGN64
  72. # pragma align 64(one,two80)
  73. #else
  74. /* not fatal, might hurt performance a little */
  75. # define ALIGN64
  76. #endif
  77. ALIGN64 static const BN_ULONG one[40] = {
  78. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  80. };
  81. ALIGN64 static const BN_ULONG two80[40] = {
  82. 0, 0, 1 << 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  84. };
  85. void RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16],
  86. const BN_ULONG base_norm[16],
  87. const BN_ULONG exponent[16],
  88. const BN_ULONG m_norm[16], const BN_ULONG RR[16],
  89. BN_ULONG k0)
  90. {
  91. unsigned char storage[320 * 3 + 32 * 9 * 16 + 64]; /* 5.5KB */
  92. unsigned char *p_str = storage + (64 - ((size_t)storage % 64));
  93. unsigned char *a_inv, *m, *result;
  94. unsigned char *table_s = p_str + 320 * 3;
  95. unsigned char *R2 = table_s; /* borrow */
  96. int index;
  97. int wvalue;
  98. if ((((size_t)p_str & 4095) + 320) >> 12) {
  99. result = p_str;
  100. a_inv = p_str + 320;
  101. m = p_str + 320 * 2; /* should not cross page */
  102. } else {
  103. m = p_str; /* should not cross page */
  104. result = p_str + 320;
  105. a_inv = p_str + 320 * 2;
  106. }
  107. rsaz_1024_norm2red_avx2(m, m_norm);
  108. rsaz_1024_norm2red_avx2(a_inv, base_norm);
  109. rsaz_1024_norm2red_avx2(R2, RR);
  110. rsaz_1024_mul_avx2(R2, R2, R2, m, k0);
  111. rsaz_1024_mul_avx2(R2, R2, two80, m, k0);
  112. /* table[0] = 1 */
  113. rsaz_1024_mul_avx2(result, R2, one, m, k0);
  114. /* table[1] = a_inv^1 */
  115. rsaz_1024_mul_avx2(a_inv, a_inv, R2, m, k0);
  116. rsaz_1024_scatter5_avx2(table_s, result, 0);
  117. rsaz_1024_scatter5_avx2(table_s, a_inv, 1);
  118. /* table[2] = a_inv^2 */
  119. rsaz_1024_sqr_avx2(result, a_inv, m, k0, 1);
  120. rsaz_1024_scatter5_avx2(table_s, result, 2);
  121. #if 0
  122. /* this is almost 2x smaller and less than 1% slower */
  123. for (index = 3; index < 32; index++) {
  124. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  125. rsaz_1024_scatter5_avx2(table_s, result, index);
  126. }
  127. #else
  128. /* table[4] = a_inv^4 */
  129. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  130. rsaz_1024_scatter5_avx2(table_s, result, 4);
  131. /* table[8] = a_inv^8 */
  132. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  133. rsaz_1024_scatter5_avx2(table_s, result, 8);
  134. /* table[16] = a_inv^16 */
  135. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  136. rsaz_1024_scatter5_avx2(table_s, result, 16);
  137. /* table[17] = a_inv^17 */
  138. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  139. rsaz_1024_scatter5_avx2(table_s, result, 17);
  140. /* table[3] */
  141. rsaz_1024_gather5_avx2(result, table_s, 2);
  142. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  143. rsaz_1024_scatter5_avx2(table_s, result, 3);
  144. /* table[6] */
  145. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  146. rsaz_1024_scatter5_avx2(table_s, result, 6);
  147. /* table[12] */
  148. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  149. rsaz_1024_scatter5_avx2(table_s, result, 12);
  150. /* table[24] */
  151. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  152. rsaz_1024_scatter5_avx2(table_s, result, 24);
  153. /* table[25] */
  154. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  155. rsaz_1024_scatter5_avx2(table_s, result, 25);
  156. /* table[5] */
  157. rsaz_1024_gather5_avx2(result, table_s, 4);
  158. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  159. rsaz_1024_scatter5_avx2(table_s, result, 5);
  160. /* table[10] */
  161. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  162. rsaz_1024_scatter5_avx2(table_s, result, 10);
  163. /* table[20] */
  164. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  165. rsaz_1024_scatter5_avx2(table_s, result, 20);
  166. /* table[21] */
  167. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  168. rsaz_1024_scatter5_avx2(table_s, result, 21);
  169. /* table[7] */
  170. rsaz_1024_gather5_avx2(result, table_s, 6);
  171. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  172. rsaz_1024_scatter5_avx2(table_s, result, 7);
  173. /* table[14] */
  174. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  175. rsaz_1024_scatter5_avx2(table_s, result, 14);
  176. /* table[28] */
  177. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  178. rsaz_1024_scatter5_avx2(table_s, result, 28);
  179. /* table[29] */
  180. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  181. rsaz_1024_scatter5_avx2(table_s, result, 29);
  182. /* table[9] */
  183. rsaz_1024_gather5_avx2(result, table_s, 8);
  184. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  185. rsaz_1024_scatter5_avx2(table_s, result, 9);
  186. /* table[18] */
  187. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  188. rsaz_1024_scatter5_avx2(table_s, result, 18);
  189. /* table[19] */
  190. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  191. rsaz_1024_scatter5_avx2(table_s, result, 19);
  192. /* table[11] */
  193. rsaz_1024_gather5_avx2(result, table_s, 10);
  194. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  195. rsaz_1024_scatter5_avx2(table_s, result, 11);
  196. /* table[22] */
  197. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  198. rsaz_1024_scatter5_avx2(table_s, result, 22);
  199. /* table[23] */
  200. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  201. rsaz_1024_scatter5_avx2(table_s, result, 23);
  202. /* table[13] */
  203. rsaz_1024_gather5_avx2(result, table_s, 12);
  204. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  205. rsaz_1024_scatter5_avx2(table_s, result, 13);
  206. /* table[26] */
  207. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  208. rsaz_1024_scatter5_avx2(table_s, result, 26);
  209. /* table[27] */
  210. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  211. rsaz_1024_scatter5_avx2(table_s, result, 27);
  212. /* table[15] */
  213. rsaz_1024_gather5_avx2(result, table_s, 14);
  214. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  215. rsaz_1024_scatter5_avx2(table_s, result, 15);
  216. /* table[30] */
  217. rsaz_1024_sqr_avx2(result, result, m, k0, 1);
  218. rsaz_1024_scatter5_avx2(table_s, result, 30);
  219. /* table[31] */
  220. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  221. rsaz_1024_scatter5_avx2(table_s, result, 31);
  222. #endif
  223. /* load first window */
  224. p_str = (unsigned char *)exponent;
  225. wvalue = p_str[127] >> 3;
  226. rsaz_1024_gather5_avx2(result, table_s, wvalue);
  227. index = 1014;
  228. while (index > -1) { /* loop for the remaining 127 windows */
  229. rsaz_1024_sqr_avx2(result, result, m, k0, 5);
  230. wvalue = (p_str[(index / 8) + 1] << 8) | p_str[index / 8];
  231. wvalue = (wvalue >> (index % 8)) & 31;
  232. index -= 5;
  233. rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); /* borrow a_inv */
  234. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  235. }
  236. /* square four times */
  237. rsaz_1024_sqr_avx2(result, result, m, k0, 4);
  238. wvalue = p_str[0] & 15;
  239. rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); /* borrow a_inv */
  240. rsaz_1024_mul_avx2(result, result, a_inv, m, k0);
  241. /* from Montgomery */
  242. rsaz_1024_mul_avx2(result, result, one, m, k0);
  243. rsaz_1024_red2norm_avx2(result_norm, result);
  244. OPENSSL_cleanse(storage, sizeof(storage));
  245. }
  246. /*
  247. * See crypto/bn/rsaz-x86_64.pl for further details.
  248. */
  249. void rsaz_512_mul(void *ret, const void *a, const void *b, const void *n,
  250. BN_ULONG k);
  251. void rsaz_512_mul_scatter4(void *ret, const void *a, const void *n,
  252. BN_ULONG k, const void *tbl, unsigned int power);
  253. void rsaz_512_mul_gather4(void *ret, const void *a, const void *tbl,
  254. const void *n, BN_ULONG k, unsigned int power);
  255. void rsaz_512_mul_by_one(void *ret, const void *a, const void *n, BN_ULONG k);
  256. void rsaz_512_sqr(void *ret, const void *a, const void *n, BN_ULONG k,
  257. int cnt);
  258. void rsaz_512_scatter4(void *tbl, const BN_ULONG *val, int power);
  259. void rsaz_512_gather4(BN_ULONG *val, const void *tbl, int power);
  260. void RSAZ_512_mod_exp(BN_ULONG result[8],
  261. const BN_ULONG base[8], const BN_ULONG exponent[8],
  262. const BN_ULONG m[8], BN_ULONG k0, const BN_ULONG RR[8])
  263. {
  264. unsigned char storage[16 * 8 * 8 + 64 * 2 + 64]; /* 1.2KB */
  265. unsigned char *table = storage + (64 - ((size_t)storage % 64));
  266. BN_ULONG *a_inv = (BN_ULONG *)(table + 16 * 8 * 8);
  267. BN_ULONG *temp = (BN_ULONG *)(table + 16 * 8 * 8 + 8 * 8);
  268. unsigned char *p_str = (unsigned char *)exponent;
  269. int index;
  270. unsigned int wvalue;
  271. /* table[0] = 1_inv */
  272. temp[0] = 0 - m[0];
  273. temp[1] = ~m[1];
  274. temp[2] = ~m[2];
  275. temp[3] = ~m[3];
  276. temp[4] = ~m[4];
  277. temp[5] = ~m[5];
  278. temp[6] = ~m[6];
  279. temp[7] = ~m[7];
  280. rsaz_512_scatter4(table, temp, 0);
  281. /* table [1] = a_inv^1 */
  282. rsaz_512_mul(a_inv, base, RR, m, k0);
  283. rsaz_512_scatter4(table, a_inv, 1);
  284. /* table [2] = a_inv^2 */
  285. rsaz_512_sqr(temp, a_inv, m, k0, 1);
  286. rsaz_512_scatter4(table, temp, 2);
  287. for (index = 3; index < 16; index++)
  288. rsaz_512_mul_scatter4(temp, a_inv, m, k0, table, index);
  289. /* load first window */
  290. wvalue = p_str[63];
  291. rsaz_512_gather4(temp, table, wvalue >> 4);
  292. rsaz_512_sqr(temp, temp, m, k0, 4);
  293. rsaz_512_mul_gather4(temp, temp, table, m, k0, wvalue & 0xf);
  294. for (index = 62; index >= 0; index--) {
  295. wvalue = p_str[index];
  296. rsaz_512_sqr(temp, temp, m, k0, 4);
  297. rsaz_512_mul_gather4(temp, temp, table, m, k0, wvalue >> 4);
  298. rsaz_512_sqr(temp, temp, m, k0, 4);
  299. rsaz_512_mul_gather4(temp, temp, table, m, k0, wvalue & 0x0f);
  300. }
  301. /* from Montgomery */
  302. rsaz_512_mul_by_one(result, temp, m, k0);
  303. OPENSSL_cleanse(storage, sizeof(storage));
  304. }
  305. #endif