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