2 * aesce-ccm-core.S - AES-CCM transform for ARMv8 with Crypto Extensions
4 * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/linkage.h>
12 #include <asm/assembler.h>
18 * void ce_aes_ccm_auth_data(u8 mac[], u8 const in[], u32 abytes,
19 * u32 *macp, u8 const rk[], u32 rounds);
21 ENTRY(ce_aes_ccm_auth_data)
22 ldr w8, [x3] /* leftover from prev round? */
23 ld1 {v0.16b}, [x0] /* load mac */
26 eor v1.16b, v1.16b, v1.16b
27 0: ldrb w7, [x1], #1 /* get 1 byte of input */
31 ext v1.16b, v1.16b, v1.16b, #1 /* rotate in the input bytes */
32 beq 8f /* out of input? */
34 eor v0.16b, v0.16b, v1.16b
35 1: ld1 {v3.4s}, [x4] /* load first round key */
37 cmp w5, #12 /* which key size? */
39 sub w7, w5, #2 /* modified # of rounds */
45 ld1 {v5.4s}, [x6], #16 /* load 2nd round key */
46 3: aese v0.16b, v4.16b
48 4: ld1 {v3.4s}, [x6], #16 /* load next round key */
51 5: ld1 {v4.4s}, [x6], #16 /* load next round key */
55 ld1 {v5.4s}, [x6], #16 /* load next round key */
58 subs w2, w2, #16 /* last data? */
59 eor v0.16b, v0.16b, v5.16b /* final round */
61 ld1 {v1.16b}, [x1], #16 /* load next input block */
62 eor v0.16b, v0.16b, v1.16b /* xor with mac */
64 6: st1 {v0.16b}, [x0] /* store mac */
75 ext v0.16b, v0.16b, v0.16b, #1 /* rotate out the mac bytes */
80 9: ext v1.16b, v1.16b, v1.16b, #1
83 91: eor v0.16b, v0.16b, v1.16b
87 ENDPROC(ce_aes_ccm_auth_data)
90 * void ce_aes_ccm_final(u8 mac[], u8 const ctr[], u8 const rk[],
93 ENTRY(ce_aes_ccm_final)
94 ld1 {v3.4s}, [x2], #16 /* load first round key */
95 ld1 {v0.16b}, [x0] /* load mac */
96 cmp w3, #12 /* which key size? */
97 sub w3, w3, #2 /* modified # of rounds */
98 ld1 {v1.16b}, [x1] /* load 1st ctriv */
103 0: mov v4.16b, v3.16b
104 1: ld1 {v5.4s}, [x2], #16 /* load next round key */
109 2: ld1 {v3.4s}, [x2], #16 /* load next round key */
114 3: ld1 {v4.4s}, [x2], #16 /* load next round key */
123 /* final round key cancels out */
124 eor v0.16b, v0.16b, v1.16b /* en-/decrypt the mac */
125 st1 {v0.16b}, [x0] /* store result */
127 ENDPROC(ce_aes_ccm_final)
129 .macro aes_ccm_do_crypt,enc
130 ldr x8, [x6, #8] /* load lower ctr */
131 ld1 {v0.16b}, [x5] /* load mac */
132 CPU_LE( rev x8, x8 ) /* keep swabbed ctr in reg */
134 ld1 {v1.8b}, [x6] /* load upper ctr */
138 cmp w4, #12 /* which key size? */
139 sub w7, w4, #2 /* get modified # of rounds */
140 ins v1.d[1], x9 /* no carry in lower ctr */
141 ld1 {v3.4s}, [x3] /* load first round key */
147 1: mov v4.16b, v3.16b
148 ld1 {v5.4s}, [x10], #16 /* load 2nd round key */
149 2: /* inner loop: 3 rounds, 2x interleaved */
154 3: ld1 {v3.4s}, [x10], #16 /* load next round key */
159 4: ld1 {v4.4s}, [x10], #16 /* load next round key */
165 ld1 {v5.4s}, [x10], #16 /* load next round key */
170 bmi 6f /* partial block? */
171 ld1 {v2.16b}, [x1], #16 /* load next input block */
173 eor v2.16b, v2.16b, v5.16b /* final round enc+mac */
174 eor v1.16b, v1.16b, v2.16b /* xor with crypted ctr */
176 eor v2.16b, v2.16b, v1.16b /* xor with crypted ctr */
177 eor v1.16b, v2.16b, v5.16b /* final round enc */
179 eor v0.16b, v0.16b, v2.16b /* xor mac with pt ^ rk[last] */
180 st1 {v1.16b}, [x0], #16 /* write output block */
183 st1 {v0.16b}, [x5] /* store mac */
184 str x8, [x6, #8] /* store lsb end of ctr (BE) */
187 6: eor v0.16b, v0.16b, v5.16b /* final round mac */
188 eor v1.16b, v1.16b, v5.16b /* final round enc */
189 st1 {v0.16b}, [x5] /* store mac */
190 add w2, w2, #16 /* process partial tail block */
191 7: ldrb w9, [x1], #1 /* get 1 byte of input */
192 umov w6, v1.b[0] /* get top crypted ctr byte */
193 umov w7, v0.b[0] /* get top mac byte */
201 strb w9, [x0], #1 /* store out byte */
202 strb w7, [x5], #1 /* store mac byte */
205 ext v0.16b, v0.16b, v0.16b, #1 /* shift out mac byte */
206 ext v1.16b, v1.16b, v1.16b, #1 /* shift out ctr byte */
211 * void ce_aes_ccm_encrypt(u8 out[], u8 const in[], u32 cbytes,
212 * u8 const rk[], u32 rounds, u8 mac[],
214 * void ce_aes_ccm_decrypt(u8 out[], u8 const in[], u32 cbytes,
215 * u8 const rk[], u32 rounds, u8 mac[],
218 ENTRY(ce_aes_ccm_encrypt)
220 ENDPROC(ce_aes_ccm_encrypt)
222 ENTRY(ce_aes_ccm_decrypt)
224 ENDPROC(ce_aes_ccm_decrypt)