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