14 #include "crypto_onetimeauth_poly1305_53.h"
18 # pragma STDC FENV_ACCESS ON
21 typedef uint8_t uchar
;
22 typedef int32_t int32
;
23 typedef uint32_t uint32
;
24 typedef int64_t int64
;
25 typedef uint64_t uint64
;
27 static const double poly1305_53_constants
[] = {
28 0.00000000558793544769287109375 /* alpham80 = 3 2^(-29) */
29 , 24.0 /* alpham48 = 3 2^3 */
30 , 103079215104.0 /* alpham16 = 3 2^35 */
31 , 6755399441055744.0 /* alpha0 = 3 2^51 */
32 , 1770887431076116955136.0 /* alpha18 = 3 2^69 */
33 , 29014219670751100192948224.0 /* alpha32 = 3 2^83 */
34 , 7605903601369376408980219232256.0 /* alpha50 = 3 2^101 */
35 , 124615124604835863084731911901282304.0 /* alpha64 = 3 2^115 */
36 , 32667107224410092492483962313449748299776.0 /* alpha82 = 3 2^133 */
37 , 535217884764734955396857238543560676143529984.0 /* alpha96 = 3 2^147 */
38 , 35076039295941670036888435985190792471742381031424.0 /* alpha112 = 3 2^163 */
39 , 9194973245195333150150082162901855101712434733101613056.0 /* alpha130 = 3 2^181 */
40 , 0.0000000000000000000000000000000000000036734198463196484624023016788195177431833298649127735047148490821200539357960224151611328125 /* scale = 5 2^(-130) */
41 , 6755408030990331.0 /* offset0 = alpha0 + 2^33 - 5 */
42 , 29014256564239239022116864.0 /* offset1 = alpha32 + 2^65 - 2^33 */
43 , 124615283061160854719918951570079744.0 /* offset2 = alpha64 + 2^97 - 2^65 */
44 , 535219245894202480694386063513315216128475136.0 /* offset3 = alpha96 + 2^130 - 2^97 */
47 int crypto_onetimeauth(unsigned char *out
,const unsigned char *m
,unsigned long long l
,const unsigned char *k
)
49 register const unsigned char *r
= k
;
50 register const unsigned char *s
= k
+ 16;
69 register double scale
;
70 register double alpha0
;
71 register double alpha32
;
72 register double alpha64
;
73 register double alpha96
;
74 register double alpha130
;
95 register double r3low
;
96 register double r0low
;
97 register double r3high
;
98 register double r0high
;
99 register double sr1low
;
101 register double r3lowx0
;
102 register double sr1high
;
104 register double r0lowx6
;
105 register double r1low
;
107 register double r3highx0
;
108 register double r1high
;
110 register double r0highx6
;
111 register double sr2low
;
112 register double r0lowx0
;
113 register double sr2high
;
114 register double sr1lowx6
;
115 register double r2low
;
116 register double r0highx0
;
117 register double r2high
;
118 register double sr1highx6
;
119 register double sr3low
;
120 register double r1lowx0
;
121 register double sr3high
;
122 register double sr2lowx6
;
123 register double r1highx0
;
124 register double sr2highx6
;
125 register double r2lowx0
;
126 register double sr3lowx6
;
127 register double r2highx0
;
128 register double sr3highx6
;
129 register double r1highx4
;
130 register double r1lowx4
;
131 register double r0highx4
;
132 register double r0lowx4
;
133 register double sr3highx4
;
134 register double sr3lowx4
;
135 register double sr2highx4
;
136 register double sr2lowx4
;
137 register double r0lowx2
;
138 register double r0highx2
;
139 register double r1lowx2
;
140 register double r1highx2
;
141 register double r2lowx2
;
142 register double r2highx2
;
143 register double sr3lowx2
;
144 register double sr3highx2
;
189 register char *constants
;
190 register int32 lbelow2
;
191 register int32 lbelow3
;
192 register int32 lbelow4
;
193 register int32 lbelow5
;
194 register int32 lbelow6
;
195 register int32 lbelow7
;
196 register int32 lbelow8
;
197 register int32 lbelow9
;
198 register int32 lbelow10
;
199 register int32 lbelow11
;
200 register int32 lbelow12
;
201 register int32 lbelow13
;
202 register int32 lbelow14
;
203 register int32 lbelow15
;
204 register double alpham80
;
205 register double alpham48
;
206 register double alpham16
;
207 register double alpha18
;
208 register double alpha50
;
209 register double alpha82
;
210 register double alpha112
;
211 register double offset0
;
212 register double offset1
;
213 register double offset2
;
214 register double offset3
;
231 register uint64 bits32
;
246 const int previous_rounding_mode
= fegetround();
247 if (previous_rounding_mode
!= FE_TONEAREST
) {
248 if (fesetround(FE_TONEAREST
) != 0) {
254 r00
= *(uchar
*) (r
+ 0);
255 constants
= (char *) &poly1305_53_constants
;
257 r01
= *(uchar
*) (r
+ 1);
259 r02
= *(uchar
*) (r
+ 2);
262 r03
= *(uchar
*) (r
+ 3); r03
&= 15;
265 r10
= *(uchar
*) (r
+ 4); r10
&= 252;
269 r11
= *(uchar
*) (r
+ 5);
273 r12
= *(uchar
*) (r
+ 6);
277 r13
= *(uchar
*) (r
+ 7); r13
&= 15;
285 r20
= *(uchar
*) (r
+ 8); r20
&= 252;
289 r21
= *(uchar
*) (r
+ 9);
293 r22
= *(uchar
*) (r
+ 10);
297 r23
= *(uchar
*) (r
+ 11); r23
&= 15;
305 r30
= *(uchar
*) (r
+ 12); r30
&= 252;
309 r31
= *(uchar
*) (r
+ 13);
313 r32
= *(uchar
*) (r
+ 14);
319 alpha32
= *(double *) (constants
+ 40);
321 r33
= *(uchar
*) (r
+ 15); r33
&= 15;
332 h0
= alpha32
- alpha32
;
335 h1
= alpha32
- alpha32
;
337 alpha0
= *(double *) (constants
+ 24);
338 h2
= alpha32
- alpha32
;
340 alpha64
= *(double *) (constants
+ 56);
341 h3
= alpha32
- alpha32
;
343 alpha18
= *(double *) (constants
+ 32);
344 h4
= alpha32
- alpha32
;
346 r0low
= *(double *) &d0
;
347 h5
= alpha32
- alpha32
;
349 r1low
= *(double *) &d1
;
350 h6
= alpha32
- alpha32
;
352 r2low
= *(double *) &d2
;
353 h7
= alpha32
- alpha32
;
355 alpha50
= *(double *) (constants
+ 48);
358 alpha82
= *(double *) (constants
+ 64);
361 scale
= *(double *) (constants
+ 96);
364 alpha96
= *(double *) (constants
+ 72);
365 r0high
= r0low
+ alpha18
;
367 r3low
= *(double *) &d3
;
369 alpham80
= *(double *) (constants
+ 0);
370 r1high
= r1low
+ alpha50
;
371 sr1low
= scale
* r1low
;
373 alpham48
= *(double *) (constants
+ 8);
374 r2high
= r2low
+ alpha82
;
375 sr2low
= scale
* r2low
;
378 r0high_stack
= r0high
;
383 r1high_stack
= r1high
;
385 sr1high
= sr1low
+ alpham80
;
387 alpha112
= *(double *) (constants
+ 80);
390 alpham16
= *(double *) (constants
+ 16);
392 sr3low
= scale
* r3low
;
394 alpha130
= *(double *) (constants
+ 88);
395 sr2high
= sr2low
+ alpham48
;
401 sr1high_stack
= sr1high
;
407 sr2high_stack
= sr2high
;
409 r3high
= r3low
+ alpha112
;
413 sr1low_stack
= sr1low
;
415 sr3high
= sr3low
+ alpham16
;
416 r2high_stack
= r2high
;
419 sr2low_stack
= sr2low
;
422 r3high_stack
= r3high
;
426 sr3high_stack
= sr3high
;
434 sr3low_stack
= sr3low
;
436 if (l
< 16) goto addatmost15bytes
;
438 m00
= *(uchar
*) (m
+ 0);
443 m01
= *(uchar
*) (m
+ 1);
447 m02
= *(uchar
*) (m
+ 2);
451 m03
= *(uchar
*) (m
+ 3);
453 m10
= *(uchar
*) (m
+ 4);
457 m11
= *(uchar
*) (m
+ 5);
461 m12
= *(uchar
*) (m
+ 6);
465 m13
= *(uchar
*) (m
+ 7);
469 m20
= *(uchar
*) (m
+ 8);
473 m21
= *(uchar
*) (m
+ 9);
477 m22
= *(uchar
*) (m
+ 10);
481 m23
= *(uchar
*) (m
+ 11);
484 m30
= *(uchar
*) (m
+ 12);
488 m31
= *(uchar
*) (m
+ 13);
492 m32
= *(uchar
*) (m
+ 14);
496 m33
= *(uchar
*) (m
+ 15);
519 z0
= *(double *) &d0
;
521 z1
= *(double *) &d1
;
523 z2
= *(double *) &d2
;
525 z3
= *(double *) &d3
;
543 if (l
< 16) goto multiplyaddatmost15bytes
;
545 multiplyaddatleast16bytes
:;
548 m20
= *(uchar
*) (m
+ 8);
553 m21
= *(uchar
*) (m
+ 9);
558 m22
= *(uchar
*) (m
+ 10);
563 m23
= *(uchar
*) (m
+ 11);
567 m30
= *(uchar
*) (m
+ 12);
572 m31
= *(uchar
*) (m
+ 13);
577 m32
= *(uchar
*) (m
+ 14);
582 m33
= *(uchar
*) (m
+ 15);
586 m00
= *(uchar
*) (m
+ 0);
591 m01
= *(uchar
*) (m
+ 1);
595 m02
= *(uchar
*) (m
+ 2);
600 m03
= *(uchar
*) (m
+ 3);
606 m10
= *(uchar
*) (m
+ 4);
611 m11
= *(uchar
*) (m
+ 5);
616 m12
= *(uchar
*) (m
+ 6);
621 m13
= *(uchar
*) (m
+ 7);
661 r3lowx0
= r3low
* x0
;
662 r3high
= r3high_stack
;
665 r0lowx6
= r0low
* x6
;
666 r0high
= r0high_stack
;
669 r3highx0
= r3high
* x0
;
670 sr1low
= sr1low_stack
;
673 r0highx6
= r0high
* x6
;
674 sr1high
= sr1high_stack
;
677 r0lowx0
= r0low
* x0
;
680 h6
= r3lowx0
+ r0lowx6
;
681 sr1lowx6
= sr1low
* x6
;
682 r1high
= r1high_stack
;
685 r0highx0
= r0high
* x0
;
686 sr2low
= sr2low_stack
;
688 h7
= r3highx0
+ r0highx6
;
689 sr1highx6
= sr1high
* x6
;
690 sr2high
= sr2high_stack
;
693 r1lowx0
= r1low
* x0
;
696 h0
= r0lowx0
+ sr1lowx6
;
697 sr2lowx6
= sr2low
* x6
;
698 r2high
= r2high_stack
;
701 r1highx0
= r1high
* x0
;
702 sr3low
= sr3low_stack
;
704 h1
= r0highx0
+ sr1highx6
;
705 sr2highx6
= sr2high
* x6
;
706 sr3high
= sr3high_stack
;
709 r2lowx0
= r2low
* x0
;
710 z2
= *(double *) &d2
;
712 h2
= r1lowx0
+ sr2lowx6
;
713 sr3lowx6
= sr3low
* x6
;
716 r2highx0
= r2high
* x0
;
717 z3
= *(double *) &d3
;
719 h3
= r1highx0
+ sr2highx6
;
720 sr3highx6
= sr3high
* x6
;
722 r1highx4
= r1high
* x4
;
725 h4
= r2lowx0
+ sr3lowx6
;
726 r1lowx4
= r1low
* x4
;
728 r0highx4
= r0high
* x4
;
731 h5
= r2highx0
+ sr3highx6
;
732 r0lowx4
= r0low
* x4
;
735 sr3highx4
= sr3high
* x4
;
738 sr3lowx4
= sr3low
* x4
;
741 sr2highx4
= sr2high
* x4
;
744 sr2lowx4
= sr2low
* x4
;
747 r0lowx2
= r0low
* x2
;
750 r0highx2
= r0high
* x2
;
753 r1lowx2
= r1low
* x2
;
756 r1highx2
= r1high
* x2
;
759 r2lowx2
= r2low
* x2
;
762 r2highx2
= r2high
* x2
;
765 sr3lowx2
= sr3low
* x2
;
768 sr3highx2
= sr3high
* x2
;
769 alpha0
= *(double *) (constants
+ 24);
777 z1
= *(double *) &d1
;
780 z0
= *(double *) &d0
;
795 if (l
>= 16) goto multiplyaddatleast16bytes
;
797 multiplyaddatmost15bytes
:;
856 r3lowx0
= r3low
* x0
;
857 r3high
= r3high_stack
;
860 r0lowx6
= r0low
* x6
;
861 r0high
= r0high_stack
;
864 r3highx0
= r3high
* x0
;
865 sr1low
= sr1low_stack
;
868 r0highx6
= r0high
* x6
;
869 sr1high
= sr1high_stack
;
872 r0lowx0
= r0low
* x0
;
875 h6
= r3lowx0
+ r0lowx6
;
876 sr1lowx6
= sr1low
* x6
;
877 r1high
= r1high_stack
;
880 r0highx0
= r0high
* x0
;
881 sr2low
= sr2low_stack
;
883 h7
= r3highx0
+ r0highx6
;
884 sr1highx6
= sr1high
* x6
;
885 sr2high
= sr2high_stack
;
888 r1lowx0
= r1low
* x0
;
891 h0
= r0lowx0
+ sr1lowx6
;
892 sr2lowx6
= sr2low
* x6
;
893 r2high
= r2high_stack
;
896 r1highx0
= r1high
* x0
;
897 sr3low
= sr3low_stack
;
899 h1
= r0highx0
+ sr1highx6
;
900 sr2highx6
= sr2high
* x6
;
901 sr3high
= sr3high_stack
;
904 r2lowx0
= r2low
* x0
;
906 h2
= r1lowx0
+ sr2lowx6
;
907 sr3lowx6
= sr3low
* x6
;
910 r2highx0
= r2high
* x0
;
912 h3
= r1highx0
+ sr2highx6
;
913 sr3highx6
= sr3high
* x6
;
915 r1highx4
= r1high
* x4
;
917 h4
= r2lowx0
+ sr3lowx6
;
918 r1lowx4
= r1low
* x4
;
920 r0highx4
= r0high
* x4
;
922 h5
= r2highx0
+ sr3highx6
;
923 r0lowx4
= r0low
* x4
;
926 sr3highx4
= sr3high
* x4
;
929 sr3lowx4
= sr3low
* x4
;
932 sr2highx4
= sr2high
* x4
;
935 sr2lowx4
= sr2low
* x4
;
938 r0lowx2
= r0low
* x2
;
941 r0highx2
= r0high
* x2
;
944 r1lowx2
= r1low
* x2
;
947 r1highx2
= r1high
* x2
;
950 r2lowx2
= r2low
* x2
;
953 r2highx2
= r2high
* x2
;
956 sr3lowx2
= sr3low
* x2
;
959 sr3highx2
= sr3high
* x2
;
971 if (l
== 0) goto nomorebytes
;
980 m00
= *(uchar
*) (m
+ 0);
984 m01
= *(uchar
*) (m
+ 1);
988 m02
= *(uchar
*) (m
+ 2);
992 m03
= *(uchar
*) (m
+ 3);
1029 m10
= *(uchar
*) (m
+ 4);
1033 m11
= *(uchar
*) (m
+ 5);
1037 m12
= *(uchar
*) (m
+ 6);
1041 m13
= *(uchar
*) (m
+ 7);
1084 m20
= *(uchar
*) (m
+ 8);
1088 m21
= *(uchar
*) (m
+ 9);
1092 m22
= *(uchar
*) (m
+ 10);
1096 m23
= *(uchar
*) (m
+ 11);
1101 lbelow9
-= lbelow10
;
1112 lbelow10
-= lbelow11
;
1116 lbelow11
-= lbelow12
;
1134 m30
= *(uchar
*) (m
+ 12);
1138 m31
= *(uchar
*) (m
+ 13);
1142 m32
= *(uchar
*) (m
+ 14);
1144 lbelow12
-= lbelow13
;
1147 lbelow13
-= lbelow14
;
1156 lbelow14
-= lbelow15
;
1173 alpha0
= *(double *) (constants
+ 24);
1175 z3
= *(double *) &d3
;
1177 z2
= *(double *) &d2
;
1179 z1
= *(double *) &d1
;
1181 z0
= *(double *) &d0
;
1250 r3low
= r3low_stack
;
1253 r0low
= r0low_stack
;
1256 r3lowx0
= r3low
* x0
;
1257 r3high
= r3high_stack
;
1260 r0lowx6
= r0low
* x6
;
1261 r0high
= r0high_stack
;
1264 r3highx0
= r3high
* x0
;
1265 sr1low
= sr1low_stack
;
1268 r0highx6
= r0high
* x6
;
1269 sr1high
= sr1high_stack
;
1272 r0lowx0
= r0low
* x0
;
1273 r1low
= r1low_stack
;
1275 h6
= r3lowx0
+ r0lowx6
;
1276 sr1lowx6
= sr1low
* x6
;
1277 r1high
= r1high_stack
;
1280 r0highx0
= r0high
* x0
;
1281 sr2low
= sr2low_stack
;
1283 h7
= r3highx0
+ r0highx6
;
1284 sr1highx6
= sr1high
* x6
;
1285 sr2high
= sr2high_stack
;
1288 r1lowx0
= r1low
* x0
;
1289 r2low
= r2low_stack
;
1291 h0
= r0lowx0
+ sr1lowx6
;
1292 sr2lowx6
= sr2low
* x6
;
1293 r2high
= r2high_stack
;
1296 r1highx0
= r1high
* x0
;
1297 sr3low
= sr3low_stack
;
1299 h1
= r0highx0
+ sr1highx6
;
1300 sr2highx6
= sr2high
* x6
;
1301 sr3high
= sr3high_stack
;
1304 r2lowx0
= r2low
* x0
;
1306 h2
= r1lowx0
+ sr2lowx6
;
1307 sr3lowx6
= sr3low
* x6
;
1310 r2highx0
= r2high
* x0
;
1312 h3
= r1highx0
+ sr2highx6
;
1313 sr3highx6
= sr3high
* x6
;
1315 r1highx4
= r1high
* x4
;
1317 h4
= r2lowx0
+ sr3lowx6
;
1318 r1lowx4
= r1low
* x4
;
1320 r0highx4
= r0high
* x4
;
1322 h5
= r2highx0
+ sr3highx6
;
1323 r0lowx4
= r0low
* x4
;
1326 sr3highx4
= sr3high
* x4
;
1329 sr3lowx4
= sr3low
* x4
;
1332 sr2highx4
= sr2high
* x4
;
1335 sr2lowx4
= sr2low
* x4
;
1338 r0lowx2
= r0low
* x2
;
1341 r0highx2
= r0high
* x2
;
1344 r1lowx2
= r1low
* x2
;
1347 r1highx2
= r1high
* x2
;
1350 r2lowx2
= r2low
* x2
;
1353 r2highx2
= r2high
* x2
;
1356 sr3lowx2
= sr3low
* x2
;
1359 sr3highx2
= sr3high
* x2
;
1372 offset0
= *(double *) (constants
+ 104);
1375 offset1
= *(double *) (constants
+ 112);
1378 offset2
= *(double *) (constants
+ 120);
1381 offset3
= *(double *) (constants
+ 128);
1452 *(double *) &d1
= x2
;
1455 *(double *) &d0
= x0
;
1458 *(double *) &d2
= x4
;
1461 *(double *) &d3
= x6
;
1519 s00
= *(uchar
*) (s
+ 0);
1521 f
= (int64
) g4
>> 63;
1522 s01
= *(uchar
*) (s
+ 1);
1526 s02
= *(uchar
*) (s
+ 2);
1530 s03
= *(uchar
*) (s
+ 3);
1534 s10
= *(uchar
*) (s
+ 4);
1538 s11
= *(uchar
*) (s
+ 5);
1542 s12
= *(uchar
*) (s
+ 6);
1546 s13
= *(uchar
*) (s
+ 7);
1550 s20
= *(uchar
*) (s
+ 8);
1554 s21
= *(uchar
*) (s
+ 9);
1558 s22
= *(uchar
*) (s
+ 10);
1562 s23
= *(uchar
*) (s
+ 11);
1566 s30
= *(uchar
*) (s
+ 12);
1570 s31
= *(uchar
*) (s
+ 13);
1574 s32
= *(uchar
*) (s
+ 14);
1578 s33
= *(uchar
*) (s
+ 15);
1598 *(uchar
*) (out
+ 0) = f0
;
1600 *(uchar
*) (out
+ 1) = f0
;
1602 *(uchar
*) (out
+ 2) = f0
;
1604 *(uchar
*) (out
+ 3) = f0
;
1608 *(uchar
*) (out
+ 4) = f1
;
1610 *(uchar
*) (out
+ 5) = f1
;
1612 *(uchar
*) (out
+ 6) = f1
;
1614 *(uchar
*) (out
+ 7) = f1
;
1618 *(uchar
*) (out
+ 8) = f2
;
1620 *(uchar
*) (out
+ 9) = f2
;
1622 *(uchar
*) (out
+ 10) = f2
;
1624 *(uchar
*) (out
+ 11) = f2
;
1628 *(uchar
*) (out
+ 12) = f3
;
1630 *(uchar
*) (out
+ 13) = f3
;
1632 *(uchar
*) (out
+ 14) = f3
;
1634 *(uchar
*) (out
+ 15) = f3
;
1637 if (previous_rounding_mode
!= FE_TONEAREST
&&
1638 fesetround(previous_rounding_mode
) != 0) {
1647 crypto_onetimeauth_poly1305_implementation_name(void)
1652 struct crypto_onetimeauth_poly1305_implementation
1653 crypto_onetimeauth_poly1305_53_implementation
= {
1654 _SODIUM_C99(.implementation_name
=) crypto_onetimeauth_poly1305_implementation_name
,
1655 _SODIUM_C99(.onetimeauth
=) crypto_onetimeauth
,
1656 _SODIUM_C99(.onetimeauth_verify
=) crypto_onetimeauth_verify