crypto/poly1305: add floating-point reference implementation.
[openssl.git] / crypto / poly1305 / poly1305_ieee754.c
1 /* ====================================================================
2  * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
3  *
4  * Rights for redistribution and usage in source and binary
5  * forms are granted according to the OpenSSL license.
6  */
7
8 /*
9  * This module is meant to be used as template for non-x87 floating-
10  * point assembly modules. The template itself is x86_64-specific
11  * though, as it was debugged on x86_64. So that implementor would
12  * have to recognize platform-specific parts, UxTOy and inline asm,
13  * and act accordingly.
14  *
15  * Huh? x86_64-specific code as template for non-x87? Note seven, which
16  * is not a typo, but reference to 80-bit precision. This module on the
17  * other hand relies on 64-bit precision operations, which are default
18  * for x86_64 code. And since we are at it, just for sense of it,
19  * large-block performance in cycles per processed byte for *this* code
20  * is:
21  *                      gcc-4.8         icc-15.0        clang-3.4(*)
22  *
23  * Westmere             4.96            5.09            4.37
24  * Sandy Bridge         4.95            4.90            4.17
25  * Haswell              4.92            4.87            3.78
26  * Bulldozer            4.67            4.49            4.68
27  * VIA Nano             7.07            7.05            5.98
28  * Silvermont           10.6            9.61            12.6
29  *
30  * (*)  clang managed to discover parallelism and deployed SIMD;
31  *
32  * And for range of other platforms with unspecified gcc versions:
33  *
34  * Freescale e300       12.5
35  * PPC74x0              10.8
36  * POWER6               4.92
37  * POWER7               4.50
38  * POWER8               4.10
39  *
40  * z10                  11.2
41  * z196+                7.30
42  *
43  * UltraSPARC III       16.0
44  * SPARC T4             16.1
45  */
46
47 #if !(defined(__GNUC__) && __GNUC__>=2)
48 # error "this is gcc-specific template"
49 #endif
50
51 #include <stdlib.h>
52
53 typedef unsigned char u8;
54 typedef unsigned int u32;
55 typedef unsigned long long u64;
56 typedef union { double d; u64 u; } elem64;
57
58 #define TWO(p)          ((double)(1ULL<<(p)))
59 #define TWO0            TWO(0)
60 #define TWO32           TWO(32)
61 #define TWO64           (TWO32*TWO(32))
62 #define TWO96           (TWO64*TWO(32))
63 #define TWO130          (TWO96*TWO(34))
64
65 #define EXP(p)          ((1023ULL+(p))<<52)
66
67 #if defined(__x86_64__) || (defined(__PPC__) && defined(__LITTLE_ENDIAN__))
68 # define U8TOU32(p)     (*(const u32 *)(p))
69 # define U32TO8(p,v)    (*(u32 *)(p) = (v))
70 #elif defined(__PPC__)
71 # define U8TOU32(p)     ({u32 ret; asm ("lwbrx  %0,0,%1":"=r"(ret):"b"(p)); ret; })
72 # define U32TO8(p,v)    asm ("stwbrx %0,0,%1"::"r"(v),"b"(p):"memory")
73 #elif defined(__s390x__)
74 # define U8TOU32(p)     ({u32 ret; asm ("lrv    %0,%1":"=d"(ret):"m"(*(u32 *)(p))); ret; })
75 # define U32TO8(p,v)    asm ("strv      %1,%0":"=m"(*(u32 *)(p)):"d"(v))
76 #endif
77
78 #ifndef U8TOU32
79 # define U8TOU32(p)     ((u32)(p)[0]     | (u32)(p)[1]<<8 | \
80                          (u32)(p)[2]<<16 | (u32)(p)[3]<<24  )
81 #endif
82 #ifndef U32TO8
83 # define U32TO8(p,v)    ((p)[0] = (u8)(v),       (p)[1] = (u8)((v)>>8), \
84                          (p)[2] = (u8)((v)>>16), (p)[3] = (u8)((v)>>24) )
85 #endif
86
87 typedef struct {
88     elem64 h[4];
89     double r[8];
90     double s[6];
91 } poly1305_internal;
92
93 /* "round toward zero (truncate), mask all exceptions" */
94 #if defined(__x86_64__)
95 static const u32 mxcsr = 0x7f80;
96 #elif defined(__PPC__)
97 static const u64 one = 1;
98 #elif defined(__s390x__)
99 static const u32 fpc = 1;
100 #elif defined(__sparc__)
101 static const u64 fsr = 1ULL<<30;
102 #else
103 #error "unrecognized platform"
104 #endif
105
106 int poly1305_init(void *ctx, const unsigned char key[16])
107 {
108     poly1305_internal *st = (poly1305_internal *) ctx;
109     elem64 r0, r1, r2, r3;
110
111     /* h = 0, biased */
112 #if 0
113     st->h[0].d = TWO(52)*TWO0;
114     st->h[1].d = TWO(52)*TWO32;
115     st->h[2].d = TWO(52)*TWO64;
116     st->h[3].d = TWO(52)*TWO96;
117 #else
118     st->h[0].u = EXP(52+0);
119     st->h[1].u = EXP(52+32);
120     st->h[2].u = EXP(52+64);
121     st->h[3].u = EXP(52+96);
122 #endif
123
124     if (key) {
125         /*
126          * set "truncate" rounding mode
127          */
128 #if defined(__x86_64__)
129         u32 mxcsr_orig;
130
131         asm volatile ("stmxcsr  %0":"=m"(mxcsr_orig));
132         asm volatile ("ldmxcsr  %0"::"m"(mxcsr));
133 #elif defined(__PPC__)
134         double fpscr_orig, fpscr = *(double *)&one;
135
136         asm volatile ("mffs     %0":"=f"(fpscr_orig));
137         asm volatile ("mtfsf    255,%0"::"f"(fpscr));
138 #elif defined(__s390x__)
139         u32 fpc_orig;
140
141         asm volatile ("stfpc    %0":"=m"(fpc_orig));
142         asm volatile ("lfpc     %0"::"m"(fpc));
143 #elif defined(__sparc__)
144         u64 fsr_orig;
145
146         asm volatile ("stx      %%fsr,%0":"=m"(fsr_orig));
147         asm volatile ("ldx      %0,%%fsr"::"m"(fsr));
148 #endif
149
150         /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
151         r0.u = EXP(52+0)  | (U8TOU32(&key[0])  & 0x0fffffff);
152         r1.u = EXP(52+32) | (U8TOU32(&key[4])  & 0x0ffffffc);
153         r2.u = EXP(52+64) | (U8TOU32(&key[8])  & 0x0ffffffc);
154         r3.u = EXP(52+96) | (U8TOU32(&key[12]) & 0x0ffffffc);
155
156         st->r[0] = r0.d - TWO(52)*TWO0;
157         st->r[2] = r1.d - TWO(52)*TWO32;
158         st->r[4] = r2.d - TWO(52)*TWO64;
159         st->r[6] = r3.d - TWO(52)*TWO96;
160
161         st->s[0] = st->r[2] * (5.0/TWO130);
162         st->s[2] = st->r[4] * (5.0/TWO130);
163         st->s[4] = st->r[6] * (5.0/TWO130);
164
165         /*
166          * base 2^32 -> base 2^16
167          */
168         st->r[1] = (st->r[0] + TWO(52)*TWO(16)*TWO0) -
169                                TWO(52)*TWO(16)*TWO0;
170         st->r[0] -= st->r[1];
171
172         st->r[3] = (st->r[2] + TWO(52)*TWO(16)*TWO32) -
173                                TWO(52)*TWO(16)*TWO32;
174         st->r[2] -= st->r[3];
175
176         st->r[5] = (st->r[4] + TWO(52)*TWO(16)*TWO64) -
177                                TWO(52)*TWO(16)*TWO64;
178         st->r[4] -= st->r[5];
179
180         st->r[7] = (st->r[6] + TWO(52)*TWO(16)*TWO96) -
181                                TWO(52)*TWO(16)*TWO96;
182         st->r[6] -= st->r[7];
183
184         st->s[1] = (st->s[0] + TWO(52)*TWO(16)*TWO0/TWO96) -
185                                TWO(52)*TWO(16)*TWO0/TWO96;
186         st->s[0] -= st->s[1];
187
188         st->s[3] = (st->s[2] + TWO(52)*TWO(16)*TWO32/TWO96) -
189                                TWO(52)*TWO(16)*TWO32/TWO96;
190         st->s[2] -= st->s[3];
191
192         st->s[5] = (st->s[4] + TWO(52)*TWO(16)*TWO64/TWO96) -
193                                TWO(52)*TWO(16)*TWO64/TWO96;
194         st->s[4] -= st->s[5];
195
196         /*
197          * restore original FPU control register
198          */
199 #if defined(__x86_64__)
200         asm volatile ("ldmxcsr  %0"::"m"(mxcsr_orig));
201 #elif defined(__PPC__)
202         asm volatile ("mtfsf    255,%0"::"f"(fpscr_orig));
203 #elif defined(__s390x__)
204         asm volatile ("lfpc     %0"::"m"(fpc_orig));
205 #elif defined(__sparc__)
206         asm volatile ("ldx      %0,%%fsr"::"m"(fsr_orig));
207 #endif
208     }
209
210     return 0;
211 }
212
213 void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
214                      int padbit)
215 {
216     poly1305_internal *st = (poly1305_internal *)ctx;
217     elem64 in0, in1, in2, in3;
218     u64 pad = (u64)padbit<<32;
219
220     double x0, x1, x2, x3;
221     double h0lo, h0hi, h1lo, h1hi, h2lo, h2hi, h3lo, h3hi;
222     double c0lo, c0hi, c1lo, c1hi, c2lo, c2hi, c3lo, c3hi;
223
224     const double r0lo = st->r[0];
225     const double r0hi = st->r[1];
226     const double r1lo = st->r[2];
227     const double r1hi = st->r[3];
228     const double r2lo = st->r[4];
229     const double r2hi = st->r[5];
230     const double r3lo = st->r[6];
231     const double r3hi = st->r[7];
232
233     const double s1lo = st->s[0];
234     const double s1hi = st->s[1];
235     const double s2lo = st->s[2];
236     const double s2hi = st->s[3];
237     const double s3lo = st->s[4];
238     const double s3hi = st->s[5];
239
240     /*
241      * set "truncate" rounding mode
242      */
243 #if defined(__x86_64__)
244     u32 mxcsr_orig;
245
246     asm volatile ("stmxcsr      %0":"=m"(mxcsr_orig));
247     asm volatile ("ldmxcsr      %0"::"m"(mxcsr));
248 #elif defined(__PPC__)
249     double fpscr_orig, fpscr = *(double *)&one;
250
251     asm volatile ("mffs         %0":"=f"(fpscr_orig));
252     asm volatile ("mtfsf        255,%0"::"f"(fpscr));
253 #elif defined(__s390x__)
254     u32 fpc_orig;
255
256     asm volatile ("stfpc        %0":"=m"(fpc_orig));
257     asm volatile ("lfpc         %0"::"m"(fpc));
258 #elif defined(__sparc__)
259     u64 fsr_orig;
260
261     asm volatile ("stx          %%fsr,%0":"=m"(fsr_orig));
262     asm volatile ("ldx          %0,%%fsr"::"m"(fsr));
263 #endif
264
265     /*
266      * load base 2^32 and de-bias
267      */
268     h0lo = st->h[0].d - TWO(52)*TWO0;
269     h1lo = st->h[1].d - TWO(52)*TWO32;
270     h2lo = st->h[2].d - TWO(52)*TWO64;
271     h3lo = st->h[3].d - TWO(52)*TWO96;
272
273 #ifdef __clang__
274     h0hi = 0;
275     h1hi = 0;
276     h2hi = 0;
277     h3hi = 0;
278 #else
279     in0.u = EXP(52+0)  | U8TOU32(&inp[0]);
280     in1.u = EXP(52+32) | U8TOU32(&inp[4]);
281     in2.u = EXP(52+64) | U8TOU32(&inp[8]);
282     in3.u = EXP(52+96) | U8TOU32(&inp[12]) | pad;
283
284     x0 = in0.d - TWO(52)*TWO0;
285     x1 = in1.d - TWO(52)*TWO32;
286     x2 = in2.d - TWO(52)*TWO64;
287     x3 = in3.d - TWO(52)*TWO96;
288
289     x0 += h0lo;
290     x1 += h1lo;
291     x2 += h2lo;
292     x3 += h3lo;
293
294     goto fast_entry;
295 #endif
296
297     do {
298         in0.u = EXP(52+0)  | U8TOU32(&inp[0]);
299         in1.u = EXP(52+32) | U8TOU32(&inp[4]);
300         in2.u = EXP(52+64) | U8TOU32(&inp[8]);
301         in3.u = EXP(52+96) | U8TOU32(&inp[12]) | pad;
302
303         x0 = in0.d - TWO(52)*TWO0;
304         x1 = in1.d - TWO(52)*TWO32;
305         x2 = in2.d - TWO(52)*TWO64;
306         x3 = in3.d - TWO(52)*TWO96;
307
308         /*
309          * note that there are multiple ways to accumulate input, e.g.
310          * one can as well accumulate to h0lo-h1lo-h1hi-h2hi...
311          */
312         h0lo += x0;
313         h0hi += x1;
314         h2lo += x2;
315         h2hi += x3;
316
317         /*
318          * carries that cross 32n-bit (and 130-bit) boundaries
319          */
320         c0lo = (h0lo + TWO(52)*TWO32)  - TWO(52)*TWO32;
321         c1lo = (h1lo + TWO(52)*TWO64)  - TWO(52)*TWO64;
322         c2lo = (h2lo + TWO(52)*TWO96)  - TWO(52)*TWO96;
323         c3lo = (h3lo + TWO(52)*TWO130) - TWO(52)*TWO130;
324
325         c0hi = (h0hi + TWO(52)*TWO32)  - TWO(52)*TWO32;
326         c1hi = (h1hi + TWO(52)*TWO64)  - TWO(52)*TWO64;
327         c2hi = (h2hi + TWO(52)*TWO96)  - TWO(52)*TWO96;
328         c3hi = (h3hi + TWO(52)*TWO130) - TWO(52)*TWO130;
329
330         /*
331          * base 2^48 -> base 2^32 with last reduction step
332          */
333         x1 =  (h1lo - c1lo) + c0lo;
334         x2 =  (h2lo - c2lo) + c1lo;
335         x3 =  (h3lo - c3lo) + c2lo;
336         x0 =  (h0lo - c0lo) + c3lo * (5.0/TWO130);
337
338         x1 += (h1hi - c1hi) + c0hi;
339         x2 += (h2hi - c2hi) + c1hi;
340         x3 += (h3hi - c3hi) + c2hi;
341         x0 += (h0hi - c0hi) + c3hi * (5.0/TWO130);
342
343 #ifndef __clang__
344     fast_entry:
345 #endif
346         /*
347          * base 2^32 * base 2^16 = base 2^48
348          */
349         h0lo = s3lo * x1 + s2lo * x2 + s1lo * x3 + r0lo * x0;
350         h1lo = r0lo * x1 + s3lo * x2 + s2lo * x3 + r1lo * x0;
351         h2lo = r1lo * x1 + r0lo * x2 + s3lo * x3 + r2lo * x0;
352         h3lo = r2lo * x1 + r1lo * x2 + r0lo * x3 + r3lo * x0;
353
354         h0hi = s3hi * x1 + s2hi * x2 + s1hi * x3 + r0hi * x0;
355         h1hi = r0hi * x1 + s3hi * x2 + s2hi * x3 + r1hi * x0;
356         h2hi = r1hi * x1 + r0hi * x2 + s3hi * x3 + r2hi * x0;
357         h3hi = r2hi * x1 + r1hi * x2 + r0hi * x3 + r3hi * x0;
358
359         inp += 16;
360         len -= 16;
361
362     } while (len >= 16);
363
364     /*
365      * carries that cross 32n-bit (and 130-bit) boundaries
366      */
367     c0lo = (h0lo + TWO(52)*TWO32)  - TWO(52)*TWO32;
368     c1lo = (h1lo + TWO(52)*TWO64)  - TWO(52)*TWO64;
369     c2lo = (h2lo + TWO(52)*TWO96)  - TWO(52)*TWO96;
370     c3lo = (h3lo + TWO(52)*TWO130) - TWO(52)*TWO130;
371
372     c0hi = (h0hi + TWO(52)*TWO32)  - TWO(52)*TWO32;
373     c1hi = (h1hi + TWO(52)*TWO64)  - TWO(52)*TWO64;
374     c2hi = (h2hi + TWO(52)*TWO96)  - TWO(52)*TWO96;
375     c3hi = (h3hi + TWO(52)*TWO130) - TWO(52)*TWO130;
376
377     /*
378      * base 2^48 -> base 2^32 with last reduction step
379      */
380     x1 =  (h1lo - c1lo) + c0lo;
381     x2 =  (h2lo - c2lo) + c1lo;
382     x3 =  (h3lo - c3lo) + c2lo;
383     x0 =  (h0lo - c0lo) + c3lo * (5.0/TWO130);
384
385     x1 += (h1hi - c1hi) + c0hi;
386     x2 += (h2hi - c2hi) + c1hi;
387     x3 += (h3hi - c3hi) + c2hi;
388     x0 += (h0hi - c0hi) + c3hi * (5.0/TWO130);
389
390     /*
391      * store base 2^32, with bias
392      */
393     st->h[1].d = x1 + TWO(52)*TWO32;
394     st->h[2].d = x2 + TWO(52)*TWO64;
395     st->h[3].d = x3 + TWO(52)*TWO96;
396     st->h[0].d = x0 + TWO(52)*TWO0;
397
398     /*
399      * restore original FPU control register
400      */
401 #if defined(__x86_64__)
402     asm volatile ("ldmxcsr      %0"::"m"(mxcsr_orig));
403 #elif defined(__PPC__)
404     asm volatile ("mtfsf        255,%0"::"f"(fpscr_orig));
405 #elif defined(__s390x__)
406     asm volatile ("lfpc         %0"::"m"(fpc_orig));
407 #elif defined(__sparc__)
408     asm volatile ("ldx          %0,%%fsr"::"m"(fsr_orig));
409 #endif
410 }
411
412 void poly1305_emit(void *ctx, unsigned char mac[16], const u32 nonce[4])
413 {
414     poly1305_internal *st = (poly1305_internal *) ctx;
415     u64 h0, h1, h2, h3, h4;
416     u32 g0, g1, g2, g3, g4;
417     u64 t;
418     u32 mask;
419
420     /*
421      * thanks to bias masking exponent gives integer result
422      */
423     h0 = st->h[0].u & 0x000fffffffffffffULL;
424     h1 = st->h[1].u & 0x000fffffffffffffULL;
425     h2 = st->h[2].u & 0x000fffffffffffffULL;
426     h3 = st->h[3].u & 0x000fffffffffffffULL;
427
428     /*
429      * can be partially reduced, so reduce...
430      */
431     h4 = h3>>32; h3 &= 0xffffffffU;
432     g4 = h4&-4;
433     h4 &= 3;
434     g4 += g4>>2;
435
436     h0 += g4;
437     h1 += h0>>32; h0 &= 0xffffffffU;
438     h2 += h1>>32; h1 &= 0xffffffffU;
439     h3 += h2>>32; h2 &= 0xffffffffU;
440
441     /* compute h + -p */
442     g0 = (u32)(t = h0 + 5);
443     g1 = (u32)(t = h1 + (t >> 32));
444     g2 = (u32)(t = h2 + (t >> 32));
445     g3 = (u32)(t = h3 + (t >> 32));
446     g4 = h4 + (u32)(t >> 32);
447
448     /* if there was carry, select g0-g3 */
449     mask = 0 - (g4 >> 2);
450     g0 &= mask;
451     g1 &= mask;
452     g2 &= mask;
453     g3 &= mask;
454     mask = ~mask;
455     g0 |= (h0 & mask);
456     g1 |= (h1 & mask);
457     g2 |= (h2 & mask);
458     g3 |= (h3 & mask);
459
460     /* mac = (h + nonce) % (2^128) */
461     g0 = (u32)(t = (u64)g0 + nonce[0]);
462     g1 = (u32)(t = (u64)g1 + (t >> 32) + nonce[1]);
463     g2 = (u32)(t = (u64)g2 + (t >> 32) + nonce[2]);
464     g3 = (u32)(t = (u64)g3 + (t >> 32) + nonce[3]);
465
466     U32TO8(mac + 0, g0);
467     U32TO8(mac + 4, g1);
468     U32TO8(mac + 8, g2);
469     U32TO8(mac + 12, g3);
470 }