Apply %restore_fcommon macro for Address Sanitizer
[platform/upstream/nettle.git] / x86_64 / chacha-core-internal.asm
1 C x86_64/chacha-core-internal.asm
2
3 ifelse(<
4    Copyright (C) 2012, 2014 Niels Möller
5
6    This file is part of GNU Nettle.
7
8    GNU Nettle is free software: you can redistribute it and/or
9    modify it under the terms of either:
10
11      * the GNU Lesser General Public License as published by the Free
12        Software Foundation; either version 3 of the License, or (at your
13        option) any later version.
14
15    or
16
17      * the GNU General Public License as published by the Free
18        Software Foundation; either version 2 of the License, or (at your
19        option) any later version.
20
21    or both in parallel, as here.
22
23    GNU Nettle is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26    General Public License for more details.
27
28    You should have received copies of the GNU General Public License and
29    the GNU Lesser General Public License along with this program.  If
30    not, see http://www.gnu.org/licenses/.
31 >)
32
33 define(<DST>, <%rdi>)
34 define(<SRC>, <%rsi>)
35 define(<COUNT>, <%rdx>)
36 define(<X0>, <%xmm0>)
37 define(<X1>, <%xmm1>)
38 define(<X2>, <%xmm2>)
39 define(<X3>, <%xmm3>)
40 define(<T0>, <%xmm4>)
41 define(<T1>, <%xmm5>)
42
43 define(<USE_PSHUFW>, <yes>)
44
45 C ROTL_BY_16(REG, TMP)
46 ifelse(USE_PSHUFW, <yes>, <
47 define(<ROTL_BY_16>, <
48         pshufhw <$>0xb1, $1, $1
49         pshuflw <$>0xb1, $1, $1
50 >)>, <
51 define(<ROTL_BY_16>, <
52         pslld   <$>16, $1
53         psrld   <$>16, $2
54         por     $2, $1
55 >)
56 >)
57 C QROUND
58 define(<QROUND>, <
59         paddd   X1, X0
60         pxor    X0, X3
61         movaps  X3, T0
62         ROTL_BY_16(X3, T0)
63
64         paddd   X3, X2
65         pxor    X2, X1
66         movaps  X1, T0
67         pslld   <$>12, X1
68         psrld   <$>20, T0
69         por     T0, X1
70
71         paddd   X1, X0
72         pxor    X0, X3
73         movaps  X3, T0
74         pslld   <$>8, X3
75         psrld   <$>24, T0
76         por     T0, X3
77                 
78         paddd   X3, X2
79         pxor    X2, X1
80         movaps  X1, T0
81         pslld   <$>7, X1
82         psrld   <$>25, T0
83         por     T0, X1
84 >)
85         
86         C _chacha_core(uint32_t *dst, const uint32_t *src, unsigned rounds)
87         .text
88         ALIGN(16)
89 PROLOGUE(_nettle_chacha_core)
90         W64_ENTRY(3, 6)
91
92         movups  (SRC), X0
93         movups  16(SRC), X1
94         movups  32(SRC), X2
95         movups  48(SRC), X3
96
97         shrl    $1, XREG(COUNT)
98
99         ALIGN(16)
100 .Loop:
101         QROUND(X0, X1, X2, X3)
102         pshufd  $0x39, X1, X1
103         pshufd  $0x4e, X2, X2
104         pshufd  $0x93, X3, X3
105
106         QROUND(X0, X1, X2, X3)
107         pshufd  $0x93, X1, X1
108         pshufd  $0x4e, X2, X2
109         pshufd  $0x39, X3, X3
110
111         decl    XREG(COUNT)
112         jnz     .Loop
113
114         movups  (SRC), T0
115         movups  16(SRC), T1
116         paddd   T0, X0
117         paddd   T1, X1
118         movups  X0,(DST)
119         movups  X1,16(DST)
120         movups  32(SRC), T0
121         movups  48(SRC), T1
122         paddd   T0, X2
123         paddd   T1, X3
124         movups  X2,32(DST)
125         movups  X3,48(DST)
126         W64_EXIT(3, 6)
127         ret
128 EPILOGUE(_nettle_chacha_core)