rsaz_exp.c 11 KB

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