You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

320 lines
12 KiB

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