1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * aesce-ccm-core.S - AES-CCM transform for ARMv8 with Crypto Extensions
5 * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
8 #include <linux/linkage.h>
9 #include <asm/assembler.h>
15 * void ce_aes_ccm_auth_data(u8 mac[], u8 const in[], u32 abytes,
16 * u32 *macp, u8 const rk[], u32 rounds);
18 ENTRY(ce_aes_ccm_auth_data)
19 ldr w8, [x3] /* leftover from prev round? */
20 ld1 {v0.16b}, [x0] /* load mac */
23 eor v1.16b, v1.16b, v1.16b
24 0: ldrb w7, [x1], #1 /* get 1 byte of input */
28 ext v1.16b, v1.16b, v1.16b, #1 /* rotate in the input bytes */
29 beq 8f /* out of input? */
31 eor v0.16b, v0.16b, v1.16b
32 1: ld1 {v3.4s}, [x4] /* load first round key */
34 cmp w5, #12 /* which key size? */
36 sub w7, w5, #2 /* modified # of rounds */
42 ld1 {v5.4s}, [x6], #16 /* load 2nd round key */
43 3: aese v0.16b, v4.16b
45 4: ld1 {v3.4s}, [x6], #16 /* load next round key */
48 5: ld1 {v4.4s}, [x6], #16 /* load next round key */
52 ld1 {v5.4s}, [x6], #16 /* load next round key */
55 subs w2, w2, #16 /* last data? */
56 eor v0.16b, v0.16b, v5.16b /* final round */
58 ld1 {v1.16b}, [x1], #16 /* load next input block */
59 eor v0.16b, v0.16b, v1.16b /* xor with mac */
61 6: st1 {v0.16b}, [x0] /* store mac */
72 ext v0.16b, v0.16b, v0.16b, #1 /* rotate out the mac bytes */
77 9: ext v1.16b, v1.16b, v1.16b, #1
80 91: eor v0.16b, v0.16b, v1.16b
84 ENDPROC(ce_aes_ccm_auth_data)
87 * void ce_aes_ccm_final(u8 mac[], u8 const ctr[], u8 const rk[],
90 ENTRY(ce_aes_ccm_final)
91 ld1 {v3.4s}, [x2], #16 /* load first round key */
92 ld1 {v0.16b}, [x0] /* load mac */
93 cmp w3, #12 /* which key size? */
94 sub w3, w3, #2 /* modified # of rounds */
95 ld1 {v1.16b}, [x1] /* load 1st ctriv */
100 0: mov v4.16b, v3.16b
101 1: ld1 {v5.4s}, [x2], #16 /* load next round key */
106 2: ld1 {v3.4s}, [x2], #16 /* load next round key */
111 3: ld1 {v4.4s}, [x2], #16 /* load next round key */
120 /* final round key cancels out */
121 eor v0.16b, v0.16b, v1.16b /* en-/decrypt the mac */
122 st1 {v0.16b}, [x0] /* store result */
124 ENDPROC(ce_aes_ccm_final)
126 .macro aes_ccm_do_crypt,enc
127 ldr x8, [x6, #8] /* load lower ctr */
128 ld1 {v0.16b}, [x5] /* load mac */
129 CPU_LE( rev x8, x8 ) /* keep swabbed ctr in reg */
131 ld1 {v1.8b}, [x6] /* load upper ctr */
135 cmp w4, #12 /* which key size? */
136 sub w7, w4, #2 /* get modified # of rounds */
137 ins v1.d[1], x9 /* no carry in lower ctr */
138 ld1 {v3.4s}, [x3] /* load first round key */
144 1: mov v4.16b, v3.16b
145 ld1 {v5.4s}, [x10], #16 /* load 2nd round key */
146 2: /* inner loop: 3 rounds, 2x interleaved */
151 3: ld1 {v3.4s}, [x10], #16 /* load next round key */
156 4: ld1 {v4.4s}, [x10], #16 /* load next round key */
162 ld1 {v5.4s}, [x10], #16 /* load next round key */
167 bmi 6f /* partial block? */
168 ld1 {v2.16b}, [x1], #16 /* load next input block */
170 eor v2.16b, v2.16b, v5.16b /* final round enc+mac */
171 eor v1.16b, v1.16b, v2.16b /* xor with crypted ctr */
173 eor v2.16b, v2.16b, v1.16b /* xor with crypted ctr */
174 eor v1.16b, v2.16b, v5.16b /* final round enc */
176 eor v0.16b, v0.16b, v2.16b /* xor mac with pt ^ rk[last] */
177 st1 {v1.16b}, [x0], #16 /* write output block */
180 st1 {v0.16b}, [x5] /* store mac */
181 str x8, [x6, #8] /* store lsb end of ctr (BE) */
184 6: eor v0.16b, v0.16b, v5.16b /* final round mac */
185 eor v1.16b, v1.16b, v5.16b /* final round enc */
186 st1 {v0.16b}, [x5] /* store mac */
187 add w2, w2, #16 /* process partial tail block */
188 7: ldrb w9, [x1], #1 /* get 1 byte of input */
189 umov w6, v1.b[0] /* get top crypted ctr byte */
190 umov w7, v0.b[0] /* get top mac byte */
198 strb w9, [x0], #1 /* store out byte */
199 strb w7, [x5], #1 /* store mac byte */
202 ext v0.16b, v0.16b, v0.16b, #1 /* shift out mac byte */
203 ext v1.16b, v1.16b, v1.16b, #1 /* shift out ctr byte */
208 * void ce_aes_ccm_encrypt(u8 out[], u8 const in[], u32 cbytes,
209 * u8 const rk[], u32 rounds, u8 mac[],
211 * void ce_aes_ccm_decrypt(u8 out[], u8 const in[], u32 cbytes,
212 * u8 const rk[], u32 rounds, u8 mac[],
215 ENTRY(ce_aes_ccm_encrypt)
217 ENDPROC(ce_aes_ccm_encrypt)
219 ENTRY(ce_aes_ccm_decrypt)
221 ENDPROC(ce_aes_ccm_decrypt)