19d935509SArd Biesheuvel /*
29d935509SArd Biesheuvel * crypto_helper.c - emulate v8 Crypto Extensions instructions
39d935509SArd Biesheuvel *
490b827d1SArd Biesheuvel * Copyright (C) 2013 - 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
59d935509SArd Biesheuvel *
69d935509SArd Biesheuvel * This library is free software; you can redistribute it and/or
79d935509SArd Biesheuvel * modify it under the terms of the GNU Lesser General Public
89d935509SArd Biesheuvel * License as published by the Free Software Foundation; either
950f57e09SChetan Pant * version 2.1 of the License, or (at your option) any later version.
109d935509SArd Biesheuvel */
119d935509SArd Biesheuvel
1274c21bd0SPeter Maydell #include "qemu/osdep.h"
13*9f8d0024SPierrick Bouvier #include "qemu/bitops.h"
149d935509SArd Biesheuvel
15a04b68e1SRichard Henderson #include "tcg/tcg-gvec-desc.h"
16552d8924SRichard Henderson #include "crypto/aes-round.h"
17c29da5a7SWeiwei Li #include "crypto/sm4.h"
18a04b68e1SRichard Henderson #include "vec_internal.h"
199d935509SArd Biesheuvel
20*9f8d0024SPierrick Bouvier #define HELPER_H "tcg/helper.h"
21*9f8d0024SPierrick Bouvier #include "exec/helper-proto.h.inc"
22*9f8d0024SPierrick Bouvier
23f1ecb913SArd Biesheuvel union CRYPTO_STATE {
249d935509SArd Biesheuvel uint8_t bytes[16];
25f1ecb913SArd Biesheuvel uint32_t words[4];
269d935509SArd Biesheuvel uint64_t l[2];
279d935509SArd Biesheuvel };
289d935509SArd Biesheuvel
29e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
30afc8b7d3SRichard Henderson #define CR_ST_BYTE(state, i) ((state).bytes[(15 - (i)) ^ 8])
31afc8b7d3SRichard Henderson #define CR_ST_WORD(state, i) ((state).words[(3 - (i)) ^ 2])
32b449ca3cSArd Biesheuvel #else
33afc8b7d3SRichard Henderson #define CR_ST_BYTE(state, i) ((state).bytes[i])
34afc8b7d3SRichard Henderson #define CR_ST_WORD(state, i) ((state).words[i])
35b449ca3cSArd Biesheuvel #endif
36b449ca3cSArd Biesheuvel
37aaffebd6SRichard Henderson /*
38aaffebd6SRichard Henderson * The caller has not been converted to full gvec, and so only
39aaffebd6SRichard Henderson * modifies the low 16 bytes of the vector register.
40aaffebd6SRichard Henderson */
clear_tail_16(void * vd,uint32_t desc)41aaffebd6SRichard Henderson static void clear_tail_16(void *vd, uint32_t desc)
42aaffebd6SRichard Henderson {
43aaffebd6SRichard Henderson int opr_sz = simd_oprsz(desc);
44aaffebd6SRichard Henderson int max_sz = simd_maxsz(desc);
45aaffebd6SRichard Henderson
46aaffebd6SRichard Henderson assert(opr_sz == 16);
47aaffebd6SRichard Henderson clear_tail(vd, opr_sz, max_sz);
48aaffebd6SRichard Henderson }
49aaffebd6SRichard Henderson
50552d8924SRichard Henderson static const AESState aes_zero = { };
51552d8924SRichard Henderson
HELPER(crypto_aese)52a04b68e1SRichard Henderson void HELPER(crypto_aese)(void *vd, void *vn, void *vm, uint32_t desc)
53a04b68e1SRichard Henderson {
54a04b68e1SRichard Henderson intptr_t i, opr_sz = simd_oprsz(desc);
55a04b68e1SRichard Henderson
56a04b68e1SRichard Henderson for (i = 0; i < opr_sz; i += 16) {
57552d8924SRichard Henderson AESState *ad = (AESState *)(vd + i);
58552d8924SRichard Henderson AESState *st = (AESState *)(vn + i);
59552d8924SRichard Henderson AESState *rk = (AESState *)(vm + i);
60552d8924SRichard Henderson AESState t;
61552d8924SRichard Henderson
62552d8924SRichard Henderson /*
63552d8924SRichard Henderson * Our uint64_t are in the wrong order for big-endian.
64552d8924SRichard Henderson * The Arm AddRoundKey comes first, while the API AddRoundKey
65552d8924SRichard Henderson * comes last: perform the xor here, and provide zero to API.
66552d8924SRichard Henderson */
67552d8924SRichard Henderson if (HOST_BIG_ENDIAN) {
68552d8924SRichard Henderson t.d[0] = st->d[1] ^ rk->d[1];
69552d8924SRichard Henderson t.d[1] = st->d[0] ^ rk->d[0];
70552d8924SRichard Henderson aesenc_SB_SR_AK(&t, &t, &aes_zero, false);
71552d8924SRichard Henderson ad->d[0] = t.d[1];
72552d8924SRichard Henderson ad->d[1] = t.d[0];
73552d8924SRichard Henderson } else {
74552d8924SRichard Henderson t.v = st->v ^ rk->v;
75552d8924SRichard Henderson aesenc_SB_SR_AK(ad, &t, &aes_zero, false);
76552d8924SRichard Henderson }
77a04b68e1SRichard Henderson }
78a04b68e1SRichard Henderson clear_tail(vd, opr_sz, simd_maxsz(desc));
79a04b68e1SRichard Henderson }
80a04b68e1SRichard Henderson
HELPER(crypto_aesd)810f23908cSRichard Henderson void HELPER(crypto_aesd)(void *vd, void *vn, void *vm, uint32_t desc)
820f23908cSRichard Henderson {
830f23908cSRichard Henderson intptr_t i, opr_sz = simd_oprsz(desc);
840f23908cSRichard Henderson
850f23908cSRichard Henderson for (i = 0; i < opr_sz; i += 16) {
862a8b545fSRichard Henderson AESState *ad = (AESState *)(vd + i);
872a8b545fSRichard Henderson AESState *st = (AESState *)(vn + i);
882a8b545fSRichard Henderson AESState *rk = (AESState *)(vm + i);
892a8b545fSRichard Henderson AESState t;
902a8b545fSRichard Henderson
912a8b545fSRichard Henderson /* Our uint64_t are in the wrong order for big-endian. */
922a8b545fSRichard Henderson if (HOST_BIG_ENDIAN) {
932a8b545fSRichard Henderson t.d[0] = st->d[1] ^ rk->d[1];
942a8b545fSRichard Henderson t.d[1] = st->d[0] ^ rk->d[0];
952a8b545fSRichard Henderson aesdec_ISB_ISR_AK(&t, &t, &aes_zero, false);
962a8b545fSRichard Henderson ad->d[0] = t.d[1];
972a8b545fSRichard Henderson ad->d[1] = t.d[0];
982a8b545fSRichard Henderson } else {
992a8b545fSRichard Henderson t.v = st->v ^ rk->v;
1002a8b545fSRichard Henderson aesdec_ISB_ISR_AK(ad, &t, &aes_zero, false);
1012a8b545fSRichard Henderson }
1020f23908cSRichard Henderson }
1030f23908cSRichard Henderson clear_tail(vd, opr_sz, simd_maxsz(desc));
1040f23908cSRichard Henderson }
1050f23908cSRichard Henderson
HELPER(crypto_aesmc)106a04b68e1SRichard Henderson void HELPER(crypto_aesmc)(void *vd, void *vm, uint32_t desc)
107a04b68e1SRichard Henderson {
108a04b68e1SRichard Henderson intptr_t i, opr_sz = simd_oprsz(desc);
109a04b68e1SRichard Henderson
110a04b68e1SRichard Henderson for (i = 0; i < opr_sz; i += 16) {
1118b103ed7SRichard Henderson AESState *ad = (AESState *)(vd + i);
1128b103ed7SRichard Henderson AESState *st = (AESState *)(vm + i);
1138b103ed7SRichard Henderson AESState t;
1148b103ed7SRichard Henderson
1158b103ed7SRichard Henderson /* Our uint64_t are in the wrong order for big-endian. */
1168b103ed7SRichard Henderson if (HOST_BIG_ENDIAN) {
1178b103ed7SRichard Henderson t.d[0] = st->d[1];
1188b103ed7SRichard Henderson t.d[1] = st->d[0];
1198b103ed7SRichard Henderson aesenc_MC(&t, &t, false);
1208b103ed7SRichard Henderson ad->d[0] = t.d[1];
1218b103ed7SRichard Henderson ad->d[1] = t.d[0];
1228b103ed7SRichard Henderson } else {
1238b103ed7SRichard Henderson aesenc_MC(ad, st, false);
1248b103ed7SRichard Henderson }
1250f23908cSRichard Henderson }
1260f23908cSRichard Henderson clear_tail(vd, opr_sz, simd_maxsz(desc));
1270f23908cSRichard Henderson }
1280f23908cSRichard Henderson
HELPER(crypto_aesimc)1290f23908cSRichard Henderson void HELPER(crypto_aesimc)(void *vd, void *vm, uint32_t desc)
1300f23908cSRichard Henderson {
1310f23908cSRichard Henderson intptr_t i, opr_sz = simd_oprsz(desc);
1320f23908cSRichard Henderson
1330f23908cSRichard Henderson for (i = 0; i < opr_sz; i += 16) {
134bdb01515SRichard Henderson AESState *ad = (AESState *)(vd + i);
135bdb01515SRichard Henderson AESState *st = (AESState *)(vm + i);
136bdb01515SRichard Henderson AESState t;
137bdb01515SRichard Henderson
138bdb01515SRichard Henderson /* Our uint64_t are in the wrong order for big-endian. */
139bdb01515SRichard Henderson if (HOST_BIG_ENDIAN) {
140bdb01515SRichard Henderson t.d[0] = st->d[1];
141bdb01515SRichard Henderson t.d[1] = st->d[0];
142bdb01515SRichard Henderson aesdec_IMC(&t, &t, false);
143bdb01515SRichard Henderson ad->d[0] = t.d[1];
144bdb01515SRichard Henderson ad->d[1] = t.d[0];
145bdb01515SRichard Henderson } else {
146bdb01515SRichard Henderson aesdec_IMC(ad, st, false);
147bdb01515SRichard Henderson }
148a04b68e1SRichard Henderson }
149a04b68e1SRichard Henderson clear_tail(vd, opr_sz, simd_maxsz(desc));
150a04b68e1SRichard Henderson }
151a04b68e1SRichard Henderson
152f1ecb913SArd Biesheuvel /*
153f1ecb913SArd Biesheuvel * SHA-1 logical functions
154f1ecb913SArd Biesheuvel */
155f1ecb913SArd Biesheuvel
cho(uint32_t x,uint32_t y,uint32_t z)156f1ecb913SArd Biesheuvel static uint32_t cho(uint32_t x, uint32_t y, uint32_t z)
157f1ecb913SArd Biesheuvel {
158f1ecb913SArd Biesheuvel return (x & (y ^ z)) ^ z;
159f1ecb913SArd Biesheuvel }
160f1ecb913SArd Biesheuvel
par(uint32_t x,uint32_t y,uint32_t z)161f1ecb913SArd Biesheuvel static uint32_t par(uint32_t x, uint32_t y, uint32_t z)
162f1ecb913SArd Biesheuvel {
163f1ecb913SArd Biesheuvel return x ^ y ^ z;
164f1ecb913SArd Biesheuvel }
165f1ecb913SArd Biesheuvel
maj(uint32_t x,uint32_t y,uint32_t z)166f1ecb913SArd Biesheuvel static uint32_t maj(uint32_t x, uint32_t y, uint32_t z)
167f1ecb913SArd Biesheuvel {
168f1ecb913SArd Biesheuvel return (x & y) | ((x | y) & z);
169f1ecb913SArd Biesheuvel }
170f1ecb913SArd Biesheuvel
HELPER(crypto_sha1su0)171afc8b7d3SRichard Henderson void HELPER(crypto_sha1su0)(void *vd, void *vn, void *vm, uint32_t desc)
172f1ecb913SArd Biesheuvel {
173afc8b7d3SRichard Henderson uint64_t *d = vd, *n = vn, *m = vm;
174afc8b7d3SRichard Henderson uint64_t d0, d1;
175afc8b7d3SRichard Henderson
176afc8b7d3SRichard Henderson d0 = d[1] ^ d[0] ^ m[0];
177afc8b7d3SRichard Henderson d1 = n[0] ^ d[1] ^ m[1];
178afc8b7d3SRichard Henderson d[0] = d0;
179afc8b7d3SRichard Henderson d[1] = d1;
180afc8b7d3SRichard Henderson
181afc8b7d3SRichard Henderson clear_tail_16(vd, desc);
182afc8b7d3SRichard Henderson }
183afc8b7d3SRichard Henderson
crypto_sha1_3reg(uint64_t * rd,uint64_t * rn,uint64_t * rm,uint32_t desc,uint32_t (* fn)(union CRYPTO_STATE * d))184afc8b7d3SRichard Henderson static inline void crypto_sha1_3reg(uint64_t *rd, uint64_t *rn,
185afc8b7d3SRichard Henderson uint64_t *rm, uint32_t desc,
186afc8b7d3SRichard Henderson uint32_t (*fn)(union CRYPTO_STATE *d))
187afc8b7d3SRichard Henderson {
1881a66ac61SRichard Henderson union CRYPTO_STATE d = { .l = { rd[0], rd[1] } };
1891a66ac61SRichard Henderson union CRYPTO_STATE n = { .l = { rn[0], rn[1] } };
1901a66ac61SRichard Henderson union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
191f1ecb913SArd Biesheuvel int i;
192f1ecb913SArd Biesheuvel
193f1ecb913SArd Biesheuvel for (i = 0; i < 4; i++) {
194afc8b7d3SRichard Henderson uint32_t t = fn(&d);
195f1ecb913SArd Biesheuvel
196b449ca3cSArd Biesheuvel t += rol32(CR_ST_WORD(d, 0), 5) + CR_ST_WORD(n, 0)
197b449ca3cSArd Biesheuvel + CR_ST_WORD(m, i);
198f1ecb913SArd Biesheuvel
199b449ca3cSArd Biesheuvel CR_ST_WORD(n, 0) = CR_ST_WORD(d, 3);
200b449ca3cSArd Biesheuvel CR_ST_WORD(d, 3) = CR_ST_WORD(d, 2);
201b449ca3cSArd Biesheuvel CR_ST_WORD(d, 2) = ror32(CR_ST_WORD(d, 1), 2);
202b449ca3cSArd Biesheuvel CR_ST_WORD(d, 1) = CR_ST_WORD(d, 0);
203b449ca3cSArd Biesheuvel CR_ST_WORD(d, 0) = t;
204f1ecb913SArd Biesheuvel }
2051a66ac61SRichard Henderson rd[0] = d.l[0];
2061a66ac61SRichard Henderson rd[1] = d.l[1];
207afc8b7d3SRichard Henderson
208afc8b7d3SRichard Henderson clear_tail_16(rd, desc);
209afc8b7d3SRichard Henderson }
210afc8b7d3SRichard Henderson
do_sha1c(union CRYPTO_STATE * d)211afc8b7d3SRichard Henderson static uint32_t do_sha1c(union CRYPTO_STATE *d)
212afc8b7d3SRichard Henderson {
213afc8b7d3SRichard Henderson return cho(CR_ST_WORD(*d, 1), CR_ST_WORD(*d, 2), CR_ST_WORD(*d, 3));
214afc8b7d3SRichard Henderson }
215afc8b7d3SRichard Henderson
HELPER(crypto_sha1c)216afc8b7d3SRichard Henderson void HELPER(crypto_sha1c)(void *vd, void *vn, void *vm, uint32_t desc)
217afc8b7d3SRichard Henderson {
218afc8b7d3SRichard Henderson crypto_sha1_3reg(vd, vn, vm, desc, do_sha1c);
219afc8b7d3SRichard Henderson }
220afc8b7d3SRichard Henderson
do_sha1p(union CRYPTO_STATE * d)221afc8b7d3SRichard Henderson static uint32_t do_sha1p(union CRYPTO_STATE *d)
222afc8b7d3SRichard Henderson {
223afc8b7d3SRichard Henderson return par(CR_ST_WORD(*d, 1), CR_ST_WORD(*d, 2), CR_ST_WORD(*d, 3));
224afc8b7d3SRichard Henderson }
225afc8b7d3SRichard Henderson
HELPER(crypto_sha1p)226afc8b7d3SRichard Henderson void HELPER(crypto_sha1p)(void *vd, void *vn, void *vm, uint32_t desc)
227afc8b7d3SRichard Henderson {
228afc8b7d3SRichard Henderson crypto_sha1_3reg(vd, vn, vm, desc, do_sha1p);
229afc8b7d3SRichard Henderson }
230afc8b7d3SRichard Henderson
do_sha1m(union CRYPTO_STATE * d)231afc8b7d3SRichard Henderson static uint32_t do_sha1m(union CRYPTO_STATE *d)
232afc8b7d3SRichard Henderson {
233afc8b7d3SRichard Henderson return maj(CR_ST_WORD(*d, 1), CR_ST_WORD(*d, 2), CR_ST_WORD(*d, 3));
234afc8b7d3SRichard Henderson }
235afc8b7d3SRichard Henderson
HELPER(crypto_sha1m)236afc8b7d3SRichard Henderson void HELPER(crypto_sha1m)(void *vd, void *vn, void *vm, uint32_t desc)
237afc8b7d3SRichard Henderson {
238afc8b7d3SRichard Henderson crypto_sha1_3reg(vd, vn, vm, desc, do_sha1m);
239f1ecb913SArd Biesheuvel }
240f1ecb913SArd Biesheuvel
HELPER(crypto_sha1h)241effa992fSRichard Henderson void HELPER(crypto_sha1h)(void *vd, void *vm, uint32_t desc)
242f1ecb913SArd Biesheuvel {
2431a66ac61SRichard Henderson uint64_t *rd = vd;
2441a66ac61SRichard Henderson uint64_t *rm = vm;
2451a66ac61SRichard Henderson union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
246f1ecb913SArd Biesheuvel
247b449ca3cSArd Biesheuvel CR_ST_WORD(m, 0) = ror32(CR_ST_WORD(m, 0), 2);
248b449ca3cSArd Biesheuvel CR_ST_WORD(m, 1) = CR_ST_WORD(m, 2) = CR_ST_WORD(m, 3) = 0;
249f1ecb913SArd Biesheuvel
2501a66ac61SRichard Henderson rd[0] = m.l[0];
2511a66ac61SRichard Henderson rd[1] = m.l[1];
252effa992fSRichard Henderson
253effa992fSRichard Henderson clear_tail_16(vd, desc);
254f1ecb913SArd Biesheuvel }
255f1ecb913SArd Biesheuvel
HELPER(crypto_sha1su1)256effa992fSRichard Henderson void HELPER(crypto_sha1su1)(void *vd, void *vm, uint32_t desc)
257f1ecb913SArd Biesheuvel {
2581a66ac61SRichard Henderson uint64_t *rd = vd;
2591a66ac61SRichard Henderson uint64_t *rm = vm;
2601a66ac61SRichard Henderson union CRYPTO_STATE d = { .l = { rd[0], rd[1] } };
2611a66ac61SRichard Henderson union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
262f1ecb913SArd Biesheuvel
263b449ca3cSArd Biesheuvel CR_ST_WORD(d, 0) = rol32(CR_ST_WORD(d, 0) ^ CR_ST_WORD(m, 1), 1);
264b449ca3cSArd Biesheuvel CR_ST_WORD(d, 1) = rol32(CR_ST_WORD(d, 1) ^ CR_ST_WORD(m, 2), 1);
265b449ca3cSArd Biesheuvel CR_ST_WORD(d, 2) = rol32(CR_ST_WORD(d, 2) ^ CR_ST_WORD(m, 3), 1);
266b449ca3cSArd Biesheuvel CR_ST_WORD(d, 3) = rol32(CR_ST_WORD(d, 3) ^ CR_ST_WORD(d, 0), 1);
267f1ecb913SArd Biesheuvel
2681a66ac61SRichard Henderson rd[0] = d.l[0];
2691a66ac61SRichard Henderson rd[1] = d.l[1];
270effa992fSRichard Henderson
271effa992fSRichard Henderson clear_tail_16(vd, desc);
272f1ecb913SArd Biesheuvel }
273f1ecb913SArd Biesheuvel
274f1ecb913SArd Biesheuvel /*
275f1ecb913SArd Biesheuvel * The SHA-256 logical functions, according to
276f1ecb913SArd Biesheuvel * http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf
277f1ecb913SArd Biesheuvel */
278f1ecb913SArd Biesheuvel
S0(uint32_t x)279f1ecb913SArd Biesheuvel static uint32_t S0(uint32_t x)
280f1ecb913SArd Biesheuvel {
281f1ecb913SArd Biesheuvel return ror32(x, 2) ^ ror32(x, 13) ^ ror32(x, 22);
282f1ecb913SArd Biesheuvel }
283f1ecb913SArd Biesheuvel
S1(uint32_t x)284f1ecb913SArd Biesheuvel static uint32_t S1(uint32_t x)
285f1ecb913SArd Biesheuvel {
286f1ecb913SArd Biesheuvel return ror32(x, 6) ^ ror32(x, 11) ^ ror32(x, 25);
287f1ecb913SArd Biesheuvel }
288f1ecb913SArd Biesheuvel
s0(uint32_t x)289f1ecb913SArd Biesheuvel static uint32_t s0(uint32_t x)
290f1ecb913SArd Biesheuvel {
291f1ecb913SArd Biesheuvel return ror32(x, 7) ^ ror32(x, 18) ^ (x >> 3);
292f1ecb913SArd Biesheuvel }
293f1ecb913SArd Biesheuvel
s1(uint32_t x)294f1ecb913SArd Biesheuvel static uint32_t s1(uint32_t x)
295f1ecb913SArd Biesheuvel {
296f1ecb913SArd Biesheuvel return ror32(x, 17) ^ ror32(x, 19) ^ (x >> 10);
297f1ecb913SArd Biesheuvel }
298f1ecb913SArd Biesheuvel
HELPER(crypto_sha256h)299effa992fSRichard Henderson void HELPER(crypto_sha256h)(void *vd, void *vn, void *vm, uint32_t desc)
300f1ecb913SArd Biesheuvel {
3011a66ac61SRichard Henderson uint64_t *rd = vd;
3021a66ac61SRichard Henderson uint64_t *rn = vn;
3031a66ac61SRichard Henderson uint64_t *rm = vm;
3041a66ac61SRichard Henderson union CRYPTO_STATE d = { .l = { rd[0], rd[1] } };
3051a66ac61SRichard Henderson union CRYPTO_STATE n = { .l = { rn[0], rn[1] } };
3061a66ac61SRichard Henderson union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
307f1ecb913SArd Biesheuvel int i;
308f1ecb913SArd Biesheuvel
309f1ecb913SArd Biesheuvel for (i = 0; i < 4; i++) {
310b449ca3cSArd Biesheuvel uint32_t t = cho(CR_ST_WORD(n, 0), CR_ST_WORD(n, 1), CR_ST_WORD(n, 2))
311b449ca3cSArd Biesheuvel + CR_ST_WORD(n, 3) + S1(CR_ST_WORD(n, 0))
312b449ca3cSArd Biesheuvel + CR_ST_WORD(m, i);
313f1ecb913SArd Biesheuvel
314b449ca3cSArd Biesheuvel CR_ST_WORD(n, 3) = CR_ST_WORD(n, 2);
315b449ca3cSArd Biesheuvel CR_ST_WORD(n, 2) = CR_ST_WORD(n, 1);
316b449ca3cSArd Biesheuvel CR_ST_WORD(n, 1) = CR_ST_WORD(n, 0);
317b449ca3cSArd Biesheuvel CR_ST_WORD(n, 0) = CR_ST_WORD(d, 3) + t;
318f1ecb913SArd Biesheuvel
319b449ca3cSArd Biesheuvel t += maj(CR_ST_WORD(d, 0), CR_ST_WORD(d, 1), CR_ST_WORD(d, 2))
320b449ca3cSArd Biesheuvel + S0(CR_ST_WORD(d, 0));
321f1ecb913SArd Biesheuvel
322b449ca3cSArd Biesheuvel CR_ST_WORD(d, 3) = CR_ST_WORD(d, 2);
323b449ca3cSArd Biesheuvel CR_ST_WORD(d, 2) = CR_ST_WORD(d, 1);
324b449ca3cSArd Biesheuvel CR_ST_WORD(d, 1) = CR_ST_WORD(d, 0);
325b449ca3cSArd Biesheuvel CR_ST_WORD(d, 0) = t;
326f1ecb913SArd Biesheuvel }
327f1ecb913SArd Biesheuvel
3281a66ac61SRichard Henderson rd[0] = d.l[0];
3291a66ac61SRichard Henderson rd[1] = d.l[1];
330effa992fSRichard Henderson
331effa992fSRichard Henderson clear_tail_16(vd, desc);
332f1ecb913SArd Biesheuvel }
333f1ecb913SArd Biesheuvel
HELPER(crypto_sha256h2)334effa992fSRichard Henderson void HELPER(crypto_sha256h2)(void *vd, void *vn, void *vm, uint32_t desc)
335f1ecb913SArd Biesheuvel {
3361a66ac61SRichard Henderson uint64_t *rd = vd;
3371a66ac61SRichard Henderson uint64_t *rn = vn;
3381a66ac61SRichard Henderson uint64_t *rm = vm;
3391a66ac61SRichard Henderson union CRYPTO_STATE d = { .l = { rd[0], rd[1] } };
3401a66ac61SRichard Henderson union CRYPTO_STATE n = { .l = { rn[0], rn[1] } };
3411a66ac61SRichard Henderson union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
342f1ecb913SArd Biesheuvel int i;
343f1ecb913SArd Biesheuvel
344f1ecb913SArd Biesheuvel for (i = 0; i < 4; i++) {
345b449ca3cSArd Biesheuvel uint32_t t = cho(CR_ST_WORD(d, 0), CR_ST_WORD(d, 1), CR_ST_WORD(d, 2))
346b449ca3cSArd Biesheuvel + CR_ST_WORD(d, 3) + S1(CR_ST_WORD(d, 0))
347b449ca3cSArd Biesheuvel + CR_ST_WORD(m, i);
348f1ecb913SArd Biesheuvel
349b449ca3cSArd Biesheuvel CR_ST_WORD(d, 3) = CR_ST_WORD(d, 2);
350b449ca3cSArd Biesheuvel CR_ST_WORD(d, 2) = CR_ST_WORD(d, 1);
351b449ca3cSArd Biesheuvel CR_ST_WORD(d, 1) = CR_ST_WORD(d, 0);
352b449ca3cSArd Biesheuvel CR_ST_WORD(d, 0) = CR_ST_WORD(n, 3 - i) + t;
353f1ecb913SArd Biesheuvel }
354f1ecb913SArd Biesheuvel
3551a66ac61SRichard Henderson rd[0] = d.l[0];
3561a66ac61SRichard Henderson rd[1] = d.l[1];
357effa992fSRichard Henderson
358effa992fSRichard Henderson clear_tail_16(vd, desc);
359f1ecb913SArd Biesheuvel }
360f1ecb913SArd Biesheuvel
HELPER(crypto_sha256su0)361effa992fSRichard Henderson void HELPER(crypto_sha256su0)(void *vd, void *vm, uint32_t desc)
362f1ecb913SArd Biesheuvel {
3631a66ac61SRichard Henderson uint64_t *rd = vd;
3641a66ac61SRichard Henderson uint64_t *rm = vm;
3651a66ac61SRichard Henderson union CRYPTO_STATE d = { .l = { rd[0], rd[1] } };
3661a66ac61SRichard Henderson union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
367f1ecb913SArd Biesheuvel
368b449ca3cSArd Biesheuvel CR_ST_WORD(d, 0) += s0(CR_ST_WORD(d, 1));
369b449ca3cSArd Biesheuvel CR_ST_WORD(d, 1) += s0(CR_ST_WORD(d, 2));
370b449ca3cSArd Biesheuvel CR_ST_WORD(d, 2) += s0(CR_ST_WORD(d, 3));
371b449ca3cSArd Biesheuvel CR_ST_WORD(d, 3) += s0(CR_ST_WORD(m, 0));
372f1ecb913SArd Biesheuvel
3731a66ac61SRichard Henderson rd[0] = d.l[0];
3741a66ac61SRichard Henderson rd[1] = d.l[1];
375effa992fSRichard Henderson
376effa992fSRichard Henderson clear_tail_16(vd, desc);
377f1ecb913SArd Biesheuvel }
378f1ecb913SArd Biesheuvel
HELPER(crypto_sha256su1)379effa992fSRichard Henderson void HELPER(crypto_sha256su1)(void *vd, void *vn, void *vm, uint32_t desc)
380f1ecb913SArd Biesheuvel {
3811a66ac61SRichard Henderson uint64_t *rd = vd;
3821a66ac61SRichard Henderson uint64_t *rn = vn;
3831a66ac61SRichard Henderson uint64_t *rm = vm;
3841a66ac61SRichard Henderson union CRYPTO_STATE d = { .l = { rd[0], rd[1] } };
3851a66ac61SRichard Henderson union CRYPTO_STATE n = { .l = { rn[0], rn[1] } };
3861a66ac61SRichard Henderson union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
387f1ecb913SArd Biesheuvel
388b449ca3cSArd Biesheuvel CR_ST_WORD(d, 0) += s1(CR_ST_WORD(m, 2)) + CR_ST_WORD(n, 1);
389b449ca3cSArd Biesheuvel CR_ST_WORD(d, 1) += s1(CR_ST_WORD(m, 3)) + CR_ST_WORD(n, 2);
390b449ca3cSArd Biesheuvel CR_ST_WORD(d, 2) += s1(CR_ST_WORD(d, 0)) + CR_ST_WORD(n, 3);
391b449ca3cSArd Biesheuvel CR_ST_WORD(d, 3) += s1(CR_ST_WORD(d, 1)) + CR_ST_WORD(m, 0);
392f1ecb913SArd Biesheuvel
3931a66ac61SRichard Henderson rd[0] = d.l[0];
3941a66ac61SRichard Henderson rd[1] = d.l[1];
395effa992fSRichard Henderson
396effa992fSRichard Henderson clear_tail_16(vd, desc);
397f1ecb913SArd Biesheuvel }
39890b827d1SArd Biesheuvel
39990b827d1SArd Biesheuvel /*
40090b827d1SArd Biesheuvel * The SHA-512 logical functions (same as above but using 64-bit operands)
40190b827d1SArd Biesheuvel */
40290b827d1SArd Biesheuvel
cho512(uint64_t x,uint64_t y,uint64_t z)40390b827d1SArd Biesheuvel static uint64_t cho512(uint64_t x, uint64_t y, uint64_t z)
40490b827d1SArd Biesheuvel {
40590b827d1SArd Biesheuvel return (x & (y ^ z)) ^ z;
40690b827d1SArd Biesheuvel }
40790b827d1SArd Biesheuvel
maj512(uint64_t x,uint64_t y,uint64_t z)40890b827d1SArd Biesheuvel static uint64_t maj512(uint64_t x, uint64_t y, uint64_t z)
40990b827d1SArd Biesheuvel {
41090b827d1SArd Biesheuvel return (x & y) | ((x | y) & z);
41190b827d1SArd Biesheuvel }
41290b827d1SArd Biesheuvel
S0_512(uint64_t x)41390b827d1SArd Biesheuvel static uint64_t S0_512(uint64_t x)
41490b827d1SArd Biesheuvel {
41590b827d1SArd Biesheuvel return ror64(x, 28) ^ ror64(x, 34) ^ ror64(x, 39);
41690b827d1SArd Biesheuvel }
41790b827d1SArd Biesheuvel
S1_512(uint64_t x)41890b827d1SArd Biesheuvel static uint64_t S1_512(uint64_t x)
41990b827d1SArd Biesheuvel {
42090b827d1SArd Biesheuvel return ror64(x, 14) ^ ror64(x, 18) ^ ror64(x, 41);
42190b827d1SArd Biesheuvel }
42290b827d1SArd Biesheuvel
s0_512(uint64_t x)42390b827d1SArd Biesheuvel static uint64_t s0_512(uint64_t x)
42490b827d1SArd Biesheuvel {
42590b827d1SArd Biesheuvel return ror64(x, 1) ^ ror64(x, 8) ^ (x >> 7);
42690b827d1SArd Biesheuvel }
42790b827d1SArd Biesheuvel
s1_512(uint64_t x)42890b827d1SArd Biesheuvel static uint64_t s1_512(uint64_t x)
42990b827d1SArd Biesheuvel {
43090b827d1SArd Biesheuvel return ror64(x, 19) ^ ror64(x, 61) ^ (x >> 6);
43190b827d1SArd Biesheuvel }
43290b827d1SArd Biesheuvel
HELPER(crypto_sha512h)433aaffebd6SRichard Henderson void HELPER(crypto_sha512h)(void *vd, void *vn, void *vm, uint32_t desc)
43490b827d1SArd Biesheuvel {
43590b827d1SArd Biesheuvel uint64_t *rd = vd;
43690b827d1SArd Biesheuvel uint64_t *rn = vn;
43790b827d1SArd Biesheuvel uint64_t *rm = vm;
43890b827d1SArd Biesheuvel uint64_t d0 = rd[0];
43990b827d1SArd Biesheuvel uint64_t d1 = rd[1];
44090b827d1SArd Biesheuvel
44190b827d1SArd Biesheuvel d1 += S1_512(rm[1]) + cho512(rm[1], rn[0], rn[1]);
44290b827d1SArd Biesheuvel d0 += S1_512(d1 + rm[0]) + cho512(d1 + rm[0], rm[1], rn[0]);
44390b827d1SArd Biesheuvel
44490b827d1SArd Biesheuvel rd[0] = d0;
44590b827d1SArd Biesheuvel rd[1] = d1;
446aaffebd6SRichard Henderson
447aaffebd6SRichard Henderson clear_tail_16(vd, desc);
44890b827d1SArd Biesheuvel }
44990b827d1SArd Biesheuvel
HELPER(crypto_sha512h2)450aaffebd6SRichard Henderson void HELPER(crypto_sha512h2)(void *vd, void *vn, void *vm, uint32_t desc)
45190b827d1SArd Biesheuvel {
45290b827d1SArd Biesheuvel uint64_t *rd = vd;
45390b827d1SArd Biesheuvel uint64_t *rn = vn;
45490b827d1SArd Biesheuvel uint64_t *rm = vm;
45590b827d1SArd Biesheuvel uint64_t d0 = rd[0];
45690b827d1SArd Biesheuvel uint64_t d1 = rd[1];
45790b827d1SArd Biesheuvel
45890b827d1SArd Biesheuvel d1 += S0_512(rm[0]) + maj512(rn[0], rm[1], rm[0]);
45990b827d1SArd Biesheuvel d0 += S0_512(d1) + maj512(d1, rm[0], rm[1]);
46090b827d1SArd Biesheuvel
46190b827d1SArd Biesheuvel rd[0] = d0;
46290b827d1SArd Biesheuvel rd[1] = d1;
463aaffebd6SRichard Henderson
464aaffebd6SRichard Henderson clear_tail_16(vd, desc);
46590b827d1SArd Biesheuvel }
46690b827d1SArd Biesheuvel
HELPER(crypto_sha512su0)467aaffebd6SRichard Henderson void HELPER(crypto_sha512su0)(void *vd, void *vn, uint32_t desc)
46890b827d1SArd Biesheuvel {
46990b827d1SArd Biesheuvel uint64_t *rd = vd;
47090b827d1SArd Biesheuvel uint64_t *rn = vn;
47190b827d1SArd Biesheuvel uint64_t d0 = rd[0];
47290b827d1SArd Biesheuvel uint64_t d1 = rd[1];
47390b827d1SArd Biesheuvel
47490b827d1SArd Biesheuvel d0 += s0_512(rd[1]);
47590b827d1SArd Biesheuvel d1 += s0_512(rn[0]);
47690b827d1SArd Biesheuvel
47790b827d1SArd Biesheuvel rd[0] = d0;
47890b827d1SArd Biesheuvel rd[1] = d1;
479aaffebd6SRichard Henderson
480aaffebd6SRichard Henderson clear_tail_16(vd, desc);
48190b827d1SArd Biesheuvel }
48290b827d1SArd Biesheuvel
HELPER(crypto_sha512su1)483aaffebd6SRichard Henderson void HELPER(crypto_sha512su1)(void *vd, void *vn, void *vm, uint32_t desc)
48490b827d1SArd Biesheuvel {
48590b827d1SArd Biesheuvel uint64_t *rd = vd;
48690b827d1SArd Biesheuvel uint64_t *rn = vn;
48790b827d1SArd Biesheuvel uint64_t *rm = vm;
48890b827d1SArd Biesheuvel
48990b827d1SArd Biesheuvel rd[0] += s1_512(rn[0]) + rm[0];
49090b827d1SArd Biesheuvel rd[1] += s1_512(rn[1]) + rm[1];
491aaffebd6SRichard Henderson
492aaffebd6SRichard Henderson clear_tail_16(vd, desc);
49390b827d1SArd Biesheuvel }
49480d6f4c6SArd Biesheuvel
HELPER(crypto_sm3partw1)495aaffebd6SRichard Henderson void HELPER(crypto_sm3partw1)(void *vd, void *vn, void *vm, uint32_t desc)
49680d6f4c6SArd Biesheuvel {
49780d6f4c6SArd Biesheuvel uint64_t *rd = vd;
49880d6f4c6SArd Biesheuvel uint64_t *rn = vn;
49980d6f4c6SArd Biesheuvel uint64_t *rm = vm;
50080d6f4c6SArd Biesheuvel union CRYPTO_STATE d = { .l = { rd[0], rd[1] } };
50180d6f4c6SArd Biesheuvel union CRYPTO_STATE n = { .l = { rn[0], rn[1] } };
50280d6f4c6SArd Biesheuvel union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
50380d6f4c6SArd Biesheuvel uint32_t t;
50480d6f4c6SArd Biesheuvel
50580d6f4c6SArd Biesheuvel t = CR_ST_WORD(d, 0) ^ CR_ST_WORD(n, 0) ^ ror32(CR_ST_WORD(m, 1), 17);
50680d6f4c6SArd Biesheuvel CR_ST_WORD(d, 0) = t ^ ror32(t, 17) ^ ror32(t, 9);
50780d6f4c6SArd Biesheuvel
50880d6f4c6SArd Biesheuvel t = CR_ST_WORD(d, 1) ^ CR_ST_WORD(n, 1) ^ ror32(CR_ST_WORD(m, 2), 17);
50980d6f4c6SArd Biesheuvel CR_ST_WORD(d, 1) = t ^ ror32(t, 17) ^ ror32(t, 9);
51080d6f4c6SArd Biesheuvel
51180d6f4c6SArd Biesheuvel t = CR_ST_WORD(d, 2) ^ CR_ST_WORD(n, 2) ^ ror32(CR_ST_WORD(m, 3), 17);
51280d6f4c6SArd Biesheuvel CR_ST_WORD(d, 2) = t ^ ror32(t, 17) ^ ror32(t, 9);
51380d6f4c6SArd Biesheuvel
51480d6f4c6SArd Biesheuvel t = CR_ST_WORD(d, 3) ^ CR_ST_WORD(n, 3) ^ ror32(CR_ST_WORD(d, 0), 17);
51580d6f4c6SArd Biesheuvel CR_ST_WORD(d, 3) = t ^ ror32(t, 17) ^ ror32(t, 9);
51680d6f4c6SArd Biesheuvel
51780d6f4c6SArd Biesheuvel rd[0] = d.l[0];
51880d6f4c6SArd Biesheuvel rd[1] = d.l[1];
519aaffebd6SRichard Henderson
520aaffebd6SRichard Henderson clear_tail_16(vd, desc);
52180d6f4c6SArd Biesheuvel }
52280d6f4c6SArd Biesheuvel
HELPER(crypto_sm3partw2)523aaffebd6SRichard Henderson void HELPER(crypto_sm3partw2)(void *vd, void *vn, void *vm, uint32_t desc)
52480d6f4c6SArd Biesheuvel {
52580d6f4c6SArd Biesheuvel uint64_t *rd = vd;
52680d6f4c6SArd Biesheuvel uint64_t *rn = vn;
52780d6f4c6SArd Biesheuvel uint64_t *rm = vm;
52880d6f4c6SArd Biesheuvel union CRYPTO_STATE d = { .l = { rd[0], rd[1] } };
52980d6f4c6SArd Biesheuvel union CRYPTO_STATE n = { .l = { rn[0], rn[1] } };
53080d6f4c6SArd Biesheuvel union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
53180d6f4c6SArd Biesheuvel uint32_t t = CR_ST_WORD(n, 0) ^ ror32(CR_ST_WORD(m, 0), 25);
53280d6f4c6SArd Biesheuvel
53380d6f4c6SArd Biesheuvel CR_ST_WORD(d, 0) ^= t;
53480d6f4c6SArd Biesheuvel CR_ST_WORD(d, 1) ^= CR_ST_WORD(n, 1) ^ ror32(CR_ST_WORD(m, 1), 25);
53580d6f4c6SArd Biesheuvel CR_ST_WORD(d, 2) ^= CR_ST_WORD(n, 2) ^ ror32(CR_ST_WORD(m, 2), 25);
53680d6f4c6SArd Biesheuvel CR_ST_WORD(d, 3) ^= CR_ST_WORD(n, 3) ^ ror32(CR_ST_WORD(m, 3), 25) ^
53780d6f4c6SArd Biesheuvel ror32(t, 17) ^ ror32(t, 2) ^ ror32(t, 26);
53880d6f4c6SArd Biesheuvel
53980d6f4c6SArd Biesheuvel rd[0] = d.l[0];
54080d6f4c6SArd Biesheuvel rd[1] = d.l[1];
541aaffebd6SRichard Henderson
542aaffebd6SRichard Henderson clear_tail_16(vd, desc);
54380d6f4c6SArd Biesheuvel }
54480d6f4c6SArd Biesheuvel
54543fa36c9SRichard Henderson static inline void QEMU_ALWAYS_INLINE
crypto_sm3tt(uint64_t * rd,uint64_t * rn,uint64_t * rm,uint32_t desc,uint32_t opcode)54643fa36c9SRichard Henderson crypto_sm3tt(uint64_t *rd, uint64_t *rn, uint64_t *rm,
54743fa36c9SRichard Henderson uint32_t desc, uint32_t opcode)
54880d6f4c6SArd Biesheuvel {
54980d6f4c6SArd Biesheuvel union CRYPTO_STATE d = { .l = { rd[0], rd[1] } };
55080d6f4c6SArd Biesheuvel union CRYPTO_STATE n = { .l = { rn[0], rn[1] } };
55180d6f4c6SArd Biesheuvel union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
55243fa36c9SRichard Henderson uint32_t imm2 = simd_data(desc);
55380d6f4c6SArd Biesheuvel uint32_t t;
55480d6f4c6SArd Biesheuvel
55580d6f4c6SArd Biesheuvel assert(imm2 < 4);
55680d6f4c6SArd Biesheuvel
55780d6f4c6SArd Biesheuvel if (opcode == 0 || opcode == 2) {
55880d6f4c6SArd Biesheuvel /* SM3TT1A, SM3TT2A */
55980d6f4c6SArd Biesheuvel t = par(CR_ST_WORD(d, 3), CR_ST_WORD(d, 2), CR_ST_WORD(d, 1));
56080d6f4c6SArd Biesheuvel } else if (opcode == 1) {
56180d6f4c6SArd Biesheuvel /* SM3TT1B */
56280d6f4c6SArd Biesheuvel t = maj(CR_ST_WORD(d, 3), CR_ST_WORD(d, 2), CR_ST_WORD(d, 1));
56380d6f4c6SArd Biesheuvel } else if (opcode == 3) {
56480d6f4c6SArd Biesheuvel /* SM3TT2B */
56580d6f4c6SArd Biesheuvel t = cho(CR_ST_WORD(d, 3), CR_ST_WORD(d, 2), CR_ST_WORD(d, 1));
56680d6f4c6SArd Biesheuvel } else {
56743fa36c9SRichard Henderson qemu_build_not_reached();
56880d6f4c6SArd Biesheuvel }
56980d6f4c6SArd Biesheuvel
57080d6f4c6SArd Biesheuvel t += CR_ST_WORD(d, 0) + CR_ST_WORD(m, imm2);
57180d6f4c6SArd Biesheuvel
57280d6f4c6SArd Biesheuvel CR_ST_WORD(d, 0) = CR_ST_WORD(d, 1);
57380d6f4c6SArd Biesheuvel
57480d6f4c6SArd Biesheuvel if (opcode < 2) {
57580d6f4c6SArd Biesheuvel /* SM3TT1A, SM3TT1B */
57680d6f4c6SArd Biesheuvel t += CR_ST_WORD(n, 3) ^ ror32(CR_ST_WORD(d, 3), 20);
57780d6f4c6SArd Biesheuvel
57880d6f4c6SArd Biesheuvel CR_ST_WORD(d, 1) = ror32(CR_ST_WORD(d, 2), 23);
57980d6f4c6SArd Biesheuvel } else {
58080d6f4c6SArd Biesheuvel /* SM3TT2A, SM3TT2B */
58180d6f4c6SArd Biesheuvel t += CR_ST_WORD(n, 3);
58280d6f4c6SArd Biesheuvel t ^= rol32(t, 9) ^ rol32(t, 17);
58380d6f4c6SArd Biesheuvel
58480d6f4c6SArd Biesheuvel CR_ST_WORD(d, 1) = ror32(CR_ST_WORD(d, 2), 13);
58580d6f4c6SArd Biesheuvel }
58680d6f4c6SArd Biesheuvel
58780d6f4c6SArd Biesheuvel CR_ST_WORD(d, 2) = CR_ST_WORD(d, 3);
58880d6f4c6SArd Biesheuvel CR_ST_WORD(d, 3) = t;
58980d6f4c6SArd Biesheuvel
59080d6f4c6SArd Biesheuvel rd[0] = d.l[0];
59180d6f4c6SArd Biesheuvel rd[1] = d.l[1];
59243fa36c9SRichard Henderson
59343fa36c9SRichard Henderson clear_tail_16(rd, desc);
59480d6f4c6SArd Biesheuvel }
595b6577bcdSArd Biesheuvel
59643fa36c9SRichard Henderson #define DO_SM3TT(NAME, OPCODE) \
59743fa36c9SRichard Henderson void HELPER(NAME)(void *vd, void *vn, void *vm, uint32_t desc) \
59843fa36c9SRichard Henderson { crypto_sm3tt(vd, vn, vm, desc, OPCODE); }
59943fa36c9SRichard Henderson
60043fa36c9SRichard Henderson DO_SM3TT(crypto_sm3tt1a, 0)
60143fa36c9SRichard Henderson DO_SM3TT(crypto_sm3tt1b, 1)
60243fa36c9SRichard Henderson DO_SM3TT(crypto_sm3tt2a, 2)
60343fa36c9SRichard Henderson DO_SM3TT(crypto_sm3tt2b, 3)
60443fa36c9SRichard Henderson
60543fa36c9SRichard Henderson #undef DO_SM3TT
60643fa36c9SRichard Henderson
do_crypto_sm4e(uint64_t * rd,uint64_t * rn,uint64_t * rm)607a04b68e1SRichard Henderson static void do_crypto_sm4e(uint64_t *rd, uint64_t *rn, uint64_t *rm)
608b6577bcdSArd Biesheuvel {
609a04b68e1SRichard Henderson union CRYPTO_STATE d = { .l = { rn[0], rn[1] } };
610a04b68e1SRichard Henderson union CRYPTO_STATE n = { .l = { rm[0], rm[1] } };
611b6577bcdSArd Biesheuvel uint32_t t, i;
612b6577bcdSArd Biesheuvel
613b6577bcdSArd Biesheuvel for (i = 0; i < 4; i++) {
614b6577bcdSArd Biesheuvel t = CR_ST_WORD(d, (i + 1) % 4) ^
615b6577bcdSArd Biesheuvel CR_ST_WORD(d, (i + 2) % 4) ^
616b6577bcdSArd Biesheuvel CR_ST_WORD(d, (i + 3) % 4) ^
617b6577bcdSArd Biesheuvel CR_ST_WORD(n, i);
618b6577bcdSArd Biesheuvel
619f6ef550fSMax Chou t = sm4_subword(t);
620b6577bcdSArd Biesheuvel
621b6577bcdSArd Biesheuvel CR_ST_WORD(d, i) ^= t ^ rol32(t, 2) ^ rol32(t, 10) ^ rol32(t, 18) ^
622b6577bcdSArd Biesheuvel rol32(t, 24);
623b6577bcdSArd Biesheuvel }
624b6577bcdSArd Biesheuvel
625b6577bcdSArd Biesheuvel rd[0] = d.l[0];
626b6577bcdSArd Biesheuvel rd[1] = d.l[1];
627b6577bcdSArd Biesheuvel }
628b6577bcdSArd Biesheuvel
HELPER(crypto_sm4e)629a04b68e1SRichard Henderson void HELPER(crypto_sm4e)(void *vd, void *vn, void *vm, uint32_t desc)
630b6577bcdSArd Biesheuvel {
631a04b68e1SRichard Henderson intptr_t i, opr_sz = simd_oprsz(desc);
632a04b68e1SRichard Henderson
633a04b68e1SRichard Henderson for (i = 0; i < opr_sz; i += 16) {
634a04b68e1SRichard Henderson do_crypto_sm4e(vd + i, vn + i, vm + i);
635a04b68e1SRichard Henderson }
636a04b68e1SRichard Henderson clear_tail(vd, opr_sz, simd_maxsz(desc));
637a04b68e1SRichard Henderson }
638a04b68e1SRichard Henderson
do_crypto_sm4ekey(uint64_t * rd,uint64_t * rn,uint64_t * rm)639a04b68e1SRichard Henderson static void do_crypto_sm4ekey(uint64_t *rd, uint64_t *rn, uint64_t *rm)
640a04b68e1SRichard Henderson {
641b6577bcdSArd Biesheuvel union CRYPTO_STATE d;
642b6577bcdSArd Biesheuvel union CRYPTO_STATE n = { .l = { rn[0], rn[1] } };
643b6577bcdSArd Biesheuvel union CRYPTO_STATE m = { .l = { rm[0], rm[1] } };
644b6577bcdSArd Biesheuvel uint32_t t, i;
645b6577bcdSArd Biesheuvel
646b6577bcdSArd Biesheuvel d = n;
647b6577bcdSArd Biesheuvel for (i = 0; i < 4; i++) {
648b6577bcdSArd Biesheuvel t = CR_ST_WORD(d, (i + 1) % 4) ^
649b6577bcdSArd Biesheuvel CR_ST_WORD(d, (i + 2) % 4) ^
650b6577bcdSArd Biesheuvel CR_ST_WORD(d, (i + 3) % 4) ^
651b6577bcdSArd Biesheuvel CR_ST_WORD(m, i);
652b6577bcdSArd Biesheuvel
653f6ef550fSMax Chou t = sm4_subword(t);
654b6577bcdSArd Biesheuvel
655b6577bcdSArd Biesheuvel CR_ST_WORD(d, i) ^= t ^ rol32(t, 13) ^ rol32(t, 23);
656b6577bcdSArd Biesheuvel }
657b6577bcdSArd Biesheuvel
658b6577bcdSArd Biesheuvel rd[0] = d.l[0];
659b6577bcdSArd Biesheuvel rd[1] = d.l[1];
660b6577bcdSArd Biesheuvel }
661a04b68e1SRichard Henderson
HELPER(crypto_sm4ekey)662a04b68e1SRichard Henderson void HELPER(crypto_sm4ekey)(void *vd, void *vn, void* vm, uint32_t desc)
663a04b68e1SRichard Henderson {
664a04b68e1SRichard Henderson intptr_t i, opr_sz = simd_oprsz(desc);
665a04b68e1SRichard Henderson
666a04b68e1SRichard Henderson for (i = 0; i < opr_sz; i += 16) {
667a04b68e1SRichard Henderson do_crypto_sm4ekey(vd + i, vn + i, vm + i);
668a04b68e1SRichard Henderson }
669a04b68e1SRichard Henderson clear_tail(vd, opr_sz, simd_maxsz(desc));
670a04b68e1SRichard Henderson }
6711738860dSRichard Henderson
HELPER(crypto_rax1)6721738860dSRichard Henderson void HELPER(crypto_rax1)(void *vd, void *vn, void *vm, uint32_t desc)
6731738860dSRichard Henderson {
6741738860dSRichard Henderson intptr_t i, opr_sz = simd_oprsz(desc);
6751738860dSRichard Henderson uint64_t *d = vd, *n = vn, *m = vm;
6761738860dSRichard Henderson
6771738860dSRichard Henderson for (i = 0; i < opr_sz / 8; ++i) {
6781738860dSRichard Henderson d[i] = n[i] ^ rol64(m[i], 1);
6791738860dSRichard Henderson }
6801738860dSRichard Henderson clear_tail(vd, opr_sz, simd_maxsz(desc));
6811738860dSRichard Henderson }
682