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