Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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