|
1 | 1 | /*
|
2 | 2 | * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
|
3 | 3 | *
|
4 |
| - * Copyright (C) 2013 - 2014 Linaro Ltd <[email protected]> |
| 4 | + * Copyright (C) 2013 - 2017 Linaro Ltd <[email protected]> |
5 | 5 | *
|
6 | 6 | * This program is free software; you can redistribute it and/or modify
|
7 | 7 | * it under the terms of the GNU General Public License version 2 as
|
8 | 8 | * published by the Free Software Foundation.
|
9 | 9 | */
|
10 | 10 |
|
11 | 11 | #include <asm/neon.h>
|
| 12 | +#include <asm/unaligned.h> |
12 | 13 | #include <crypto/aes.h>
|
13 | 14 | #include <linux/cpufeature.h>
|
14 | 15 | #include <linux/crypto.h>
|
@@ -47,24 +48,24 @@ static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
|
47 | 48 | kernel_neon_begin_partial(4);
|
48 | 49 |
|
49 | 50 | __asm__(" ld1 {v0.16b}, %[in] ;"
|
50 |
| - " ld1 {v1.16b}, [%[key]], #16 ;" |
| 51 | + " ld1 {v1.4s}, [%[key]], #16 ;" |
51 | 52 | " cmp %w[rounds], #10 ;"
|
52 | 53 | " bmi 0f ;"
|
53 | 54 | " bne 3f ;"
|
54 | 55 | " mov v3.16b, v1.16b ;"
|
55 | 56 | " b 2f ;"
|
56 | 57 | "0: mov v2.16b, v1.16b ;"
|
57 |
| - " ld1 {v3.16b}, [%[key]], #16 ;" |
| 58 | + " ld1 {v3.4s}, [%[key]], #16 ;" |
58 | 59 | "1: aese v0.16b, v2.16b ;"
|
59 | 60 | " aesmc v0.16b, v0.16b ;"
|
60 |
| - "2: ld1 {v1.16b}, [%[key]], #16 ;" |
| 61 | + "2: ld1 {v1.4s}, [%[key]], #16 ;" |
61 | 62 | " aese v0.16b, v3.16b ;"
|
62 | 63 | " aesmc v0.16b, v0.16b ;"
|
63 |
| - "3: ld1 {v2.16b}, [%[key]], #16 ;" |
| 64 | + "3: ld1 {v2.4s}, [%[key]], #16 ;" |
64 | 65 | " subs %w[rounds], %w[rounds], #3 ;"
|
65 | 66 | " aese v0.16b, v1.16b ;"
|
66 | 67 | " aesmc v0.16b, v0.16b ;"
|
67 |
| - " ld1 {v3.16b}, [%[key]], #16 ;" |
| 68 | + " ld1 {v3.4s}, [%[key]], #16 ;" |
68 | 69 | " bpl 1b ;"
|
69 | 70 | " aese v0.16b, v2.16b ;"
|
70 | 71 | " eor v0.16b, v0.16b, v3.16b ;"
|
@@ -92,24 +93,24 @@ static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
|
92 | 93 | kernel_neon_begin_partial(4);
|
93 | 94 |
|
94 | 95 | __asm__(" ld1 {v0.16b}, %[in] ;"
|
95 |
| - " ld1 {v1.16b}, [%[key]], #16 ;" |
| 96 | + " ld1 {v1.4s}, [%[key]], #16 ;" |
96 | 97 | " cmp %w[rounds], #10 ;"
|
97 | 98 | " bmi 0f ;"
|
98 | 99 | " bne 3f ;"
|
99 | 100 | " mov v3.16b, v1.16b ;"
|
100 | 101 | " b 2f ;"
|
101 | 102 | "0: mov v2.16b, v1.16b ;"
|
102 |
| - " ld1 {v3.16b}, [%[key]], #16 ;" |
| 103 | + " ld1 {v3.4s}, [%[key]], #16 ;" |
103 | 104 | "1: aesd v0.16b, v2.16b ;"
|
104 | 105 | " aesimc v0.16b, v0.16b ;"
|
105 |
| - "2: ld1 {v1.16b}, [%[key]], #16 ;" |
| 106 | + "2: ld1 {v1.4s}, [%[key]], #16 ;" |
106 | 107 | " aesd v0.16b, v3.16b ;"
|
107 | 108 | " aesimc v0.16b, v0.16b ;"
|
108 |
| - "3: ld1 {v2.16b}, [%[key]], #16 ;" |
| 109 | + "3: ld1 {v2.4s}, [%[key]], #16 ;" |
109 | 110 | " subs %w[rounds], %w[rounds], #3 ;"
|
110 | 111 | " aesd v0.16b, v1.16b ;"
|
111 | 112 | " aesimc v0.16b, v0.16b ;"
|
112 |
| - " ld1 {v3.16b}, [%[key]], #16 ;" |
| 113 | + " ld1 {v3.4s}, [%[key]], #16 ;" |
113 | 114 | " bpl 1b ;"
|
114 | 115 | " aesd v0.16b, v2.16b ;"
|
115 | 116 | " eor v0.16b, v0.16b, v3.16b ;"
|
@@ -165,20 +166,16 @@ int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
|
165 | 166 | key_len != AES_KEYSIZE_256)
|
166 | 167 | return -EINVAL;
|
167 | 168 |
|
168 |
| - memcpy(ctx->key_enc, in_key, key_len); |
169 | 169 | ctx->key_length = key_len;
|
| 170 | + for (i = 0; i < kwords; i++) |
| 171 | + ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32)); |
170 | 172 |
|
171 | 173 | kernel_neon_begin_partial(2);
|
172 | 174 | for (i = 0; i < sizeof(rcon); i++) {
|
173 | 175 | u32 *rki = ctx->key_enc + (i * kwords);
|
174 | 176 | u32 *rko = rki + kwords;
|
175 | 177 |
|
176 |
| -#ifndef CONFIG_CPU_BIG_ENDIAN |
177 | 178 | rko[0] = ror32(aes_sub(rki[kwords - 1]), 8) ^ rcon[i] ^ rki[0];
|
178 |
| -#else |
179 |
| - rko[0] = rol32(aes_sub(rki[kwords - 1]), 8) ^ (rcon[i] << 24) ^ |
180 |
| - rki[0]; |
181 |
| -#endif |
182 | 179 | rko[1] = rko[0] ^ rki[1];
|
183 | 180 | rko[2] = rko[1] ^ rki[2];
|
184 | 181 | rko[3] = rko[2] ^ rki[3];
|
@@ -210,9 +207,9 @@ int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
|
210 | 207 |
|
211 | 208 | key_dec[0] = key_enc[j];
|
212 | 209 | for (i = 1, j--; j > 0; i++, j--)
|
213 |
| - __asm__("ld1 {v0.16b}, %[in] ;" |
| 210 | + __asm__("ld1 {v0.4s}, %[in] ;" |
214 | 211 | "aesimc v1.16b, v0.16b ;"
|
215 |
| - "st1 {v1.16b}, %[out] ;" |
| 212 | + "st1 {v1.4s}, %[out] ;" |
216 | 213 |
|
217 | 214 | : [out] "=Q"(key_dec[i])
|
218 | 215 | : [in] "Q"(key_enc[j])
|
|
0 commit comments