2 * aesce-ccm-core.S - AES-CCM transform for ARMv8 with Crypto Extensions
4 * Copyright (C) 2013 - 2014 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>
17 * void ce_aes_ccm_auth_data(u8 mac[], u8 const in[], u32 abytes,
18 * u32 *macp, u8 const rk[], u32 rounds);
20 ENTRY(ce_aes_ccm_auth_data)
21 ldr w8, [x3] /* leftover from prev round? */
22 ld1 {v0.2d}, [x0] /* load mac */
25 eor v1.16b, v1.16b, v1.16b
26 0: ldrb w7, [x1], #1 /* get 1 byte of input */
30 ext v1.16b, v1.16b, v1.16b, #1 /* rotate in the input bytes */
31 beq 8f /* out of input? */
33 eor v0.16b, v0.16b, v1.16b
34 1: ld1 {v3.2d}, [x4] /* load first round key */
36 cmp w5, #12 /* which key size? */
38 sub w7, w5, #2 /* modified # of rounds */
44 ld1 {v5.2d}, [x6], #16 /* load 2nd round key */
45 3: aese v0.16b, v4.16b
47 4: ld1 {v3.2d}, [x6], #16 /* load next round key */
50 5: ld1 {v4.2d}, [x6], #16 /* load next round key */
54 ld1 {v5.2d}, [x6], #16 /* load next round key */
57 subs w2, w2, #16 /* last data? */
58 eor v0.16b, v0.16b, v5.16b /* final round */
60 ld1 {v1.16b}, [x1], #16 /* load next input block */
61 eor v0.16b, v0.16b, v1.16b /* xor with mac */
63 6: st1 {v0.2d}, [x0] /* store mac */
74 ext v0.16b, v0.16b, v0.16b, #1 /* rotate out the mac bytes */
78 9: ext v1.16b, v1.16b, v1.16b, #1
81 eor v0.16b, v0.16b, v1.16b
85 ENDPROC(ce_aes_ccm_auth_data)
88 * void ce_aes_ccm_final(u8 mac[], u8 const ctr[], u8 const rk[],
91 ENTRY(ce_aes_ccm_final)
92 ld1 {v3.2d}, [x2], #16 /* load first round key */
93 ld1 {v0.2d}, [x0] /* load mac */
94 cmp w3, #12 /* which key size? */
95 sub w3, w3, #2 /* modified # of rounds */
96 ld1 {v1.2d}, [x1] /* load 1st ctriv */
101 0: mov v4.16b, v3.16b
102 1: ld1 {v5.2d}, [x2], #16 /* load next round key */
107 2: ld1 {v3.2d}, [x2], #16 /* load next round key */
112 3: ld1 {v4.2d}, [x2], #16 /* load next round key */
121 /* final round key cancels out */
122 eor v0.16b, v0.16b, v1.16b /* en-/decrypt the mac */
123 st1 {v0.2d}, [x0] /* store result */
125 ENDPROC(ce_aes_ccm_final)
127 .macro aes_ccm_do_crypt,enc
128 ldr x8, [x6, #8] /* load lower ctr */
129 ld1 {v0.2d}, [x5] /* load mac */
130 rev x8, x8 /* keep swabbed ctr in reg */
132 ld1 {v1.1d}, [x6] /* load upper ctr */
136 cmp w4, #12 /* which key size? */
137 sub w7, w4, #2 /* get modified # of rounds */
138 ins v1.d[1], x9 /* no carry in lower ctr */
139 ld1 {v3.2d}, [x3] /* load first round key */
145 1: mov v4.16b, v3.16b
146 ld1 {v5.2d}, [x10], #16 /* load 2nd round key */
147 2: /* inner loop: 3 rounds, 2x interleaved */
152 3: ld1 {v3.2d}, [x10], #16 /* load next round key */
157 4: ld1 {v4.2d}, [x10], #16 /* load next round key */
163 ld1 {v5.2d}, [x10], #16 /* load next round key */
168 bmi 6f /* partial block? */
169 ld1 {v2.16b}, [x1], #16 /* load next input block */
171 eor v2.16b, v2.16b, v5.16b /* final round enc+mac */
172 eor v1.16b, v1.16b, v2.16b /* xor with crypted ctr */
174 eor v2.16b, v2.16b, v1.16b /* xor with crypted ctr */
175 eor v1.16b, v2.16b, v5.16b /* final round enc */
177 eor v0.16b, v0.16b, v2.16b /* xor mac with pt ^ rk[last] */
178 st1 {v1.16b}, [x0], #16 /* write output block */
181 st1 {v0.2d}, [x5] /* store mac */
182 str x8, [x6, #8] /* store lsb end of ctr (BE) */
185 6: eor v0.16b, v0.16b, v5.16b /* final round mac */
186 eor v1.16b, v1.16b, v5.16b /* final round enc */
187 st1 {v0.2d}, [x5] /* store mac */
188 add w2, w2, #16 /* process partial tail block */
189 7: ldrb w9, [x1], #1 /* get 1 byte of input */
190 umov w6, v1.b[0] /* get top crypted ctr byte */
191 umov w7, v0.b[0] /* get top mac byte */
199 strb w9, [x0], #1 /* store out byte */
200 strb w7, [x5], #1 /* store mac byte */
203 ext v0.16b, v0.16b, v0.16b, #1 /* shift out mac byte */
204 ext v1.16b, v1.16b, v1.16b, #1 /* shift out ctr byte */
209 * void ce_aes_ccm_encrypt(u8 out[], u8 const in[], u32 cbytes,
210 * u8 const rk[], u32 rounds, u8 mac[],
212 * void ce_aes_ccm_decrypt(u8 out[], u8 const in[], u32 cbytes,
213 * u8 const rk[], u32 rounds, u8 mac[],
216 ENTRY(ce_aes_ccm_encrypt)
218 ENDPROC(ce_aes_ccm_encrypt)
220 ENTRY(ce_aes_ccm_decrypt)
222 ENDPROC(ce_aes_ccm_decrypt)