2
0

rsaz_exp.c 11 KB

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