1 From 4865b122d4aff5151c88d2f7442d5a87f7e795ae Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@gmail.com>
3 Date: Wed, 4 Oct 2017 01:00:10 +0200
4 Subject: [PATCH 18/25] crypto: crypto4xx - use the correct LE32 format for IV
7 The hardware expects that the keys, IVs (and inner/outer hashes)
8 are in the le32 format.
10 This patch changes all hardware interface declarations to use
11 the correct LE32 data format for each field.
13 In order to pass __CHECK_ENDIAN__ checks, crypto4xx_memcpy_le
14 has to be honest about the endianness of its parameters.
15 The function was split and moved to the common crypto4xx_core.h
16 header. This allows the compiler to generate better code if the
17 sizes/len is a constant (various *_IV_LEN).
19 Please note that the hardware isn't consistent with the endiannes
20 of the save_digest field in the state record struct though.
21 The hashes produced by GHASH and CBC (for CCM) will be in LE32.
22 Whereas md5 and sha{1/,256,...} do not need any conversion.
24 Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
25 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
27 drivers/crypto/amcc/crypto4xx_alg.c | 4 +--
28 drivers/crypto/amcc/crypto4xx_core.c | 40 ++----------------------------
29 drivers/crypto/amcc/crypto4xx_core.h | 47 +++++++++++++++++++++++++++++++++---
30 drivers/crypto/amcc/crypto4xx_sa.h | 29 ++++++++++++----------
31 4 files changed, 64 insertions(+), 56 deletions(-)
33 --- a/drivers/crypto/amcc/crypto4xx_alg.c
34 +++ b/drivers/crypto/amcc/crypto4xx_alg.c
35 @@ -150,8 +150,8 @@ static int crypto4xx_setkey_aes(struct c
36 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
37 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
39 - crypto4xx_memcpy_le(get_dynamic_sa_key_field(sa),
41 + crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
43 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
44 sa->sa_command_1.bf.key_len = keylen >> 3;
46 --- a/drivers/crypto/amcc/crypto4xx_core.c
47 +++ b/drivers/crypto/amcc/crypto4xx_core.c
48 @@ -623,42 +623,6 @@ static u32 crypto4xx_pd_done(struct cryp
49 return crypto4xx_ahash_done(dev, pd_uinfo);
53 - * Note: Only use this function to copy items that is word aligned.
55 -void crypto4xx_memcpy_le(unsigned int *dst,
56 - const unsigned char *buf,
60 - for (; len >= 4; buf += 4, len -= 4)
61 - *dst++ = cpu_to_le32(*(unsigned int *) buf);
88 static void crypto4xx_stop_all(struct crypto4xx_core_device *core_dev)
90 crypto4xx_destroy_pdr(core_dev->dev);
91 @@ -818,8 +782,8 @@ u32 crypto4xx_build_pd(struct crypto_asy
95 - crypto4xx_memcpy_le(pd_uinfo->sr_va->save_iv,
97 + crypto4xx_memcpy_to_le32(pd_uinfo->sr_va->save_iv,
100 if (ctx->direction == DIR_INBOUND) {
101 pd->sa = ctx->sa_in_dma_addr;
102 --- a/drivers/crypto/amcc/crypto4xx_core.h
103 +++ b/drivers/crypto/amcc/crypto4xx_core.h
104 @@ -166,9 +166,7 @@ int crypto4xx_alloc_sa(struct crypto4xx_
105 void crypto4xx_free_sa(struct crypto4xx_ctx *ctx);
106 void crypto4xx_free_ctx(struct crypto4xx_ctx *ctx);
107 u32 crypto4xx_alloc_state_record(struct crypto4xx_ctx *ctx);
108 -void crypto4xx_memcpy_le(unsigned int *dst,
109 - const unsigned char *buf, int len);
110 -u32 crypto4xx_build_pd(struct crypto_async_request *req,
111 +int crypto4xx_build_pd(struct crypto_async_request *req,
112 struct crypto4xx_ctx *ctx,
113 struct scatterlist *src,
114 struct scatterlist *dst,
115 @@ -193,4 +191,47 @@ int crypto4xx_hash_digest(struct ahash_r
116 int crypto4xx_hash_final(struct ahash_request *req);
117 int crypto4xx_hash_update(struct ahash_request *req);
118 int crypto4xx_hash_init(struct ahash_request *req);
121 + * Note: Only use this function to copy items that is word aligned.
123 +static inline void crypto4xx_memcpy_swab32(u32 *dst, const void *buf,
126 + for (; len >= 4; buf += 4, len -= 4)
127 + *dst++ = __swab32p((u32 *) buf);
130 + const u8 *tmp = (u8 *)buf;
134 + *dst = (tmp[2] << 16) |
139 + *dst = (tmp[1] << 8) |
151 +static inline void crypto4xx_memcpy_from_le32(u32 *dst, const void *buf,
154 + crypto4xx_memcpy_swab32(dst, buf, len);
157 +static inline void crypto4xx_memcpy_to_le32(__le32 *dst, const void *buf,
160 + crypto4xx_memcpy_swab32((u32 *)dst, buf, len);
163 --- a/drivers/crypto/amcc/crypto4xx_sa.h
164 +++ b/drivers/crypto/amcc/crypto4xx_sa.h
165 @@ -181,9 +181,12 @@ struct dynamic_sa_ctl {
166 * State Record for Security Association (SA)
168 struct sa_state_record {
170 - u32 save_hash_byte_cnt[2];
171 - u32 save_digest[16];
173 + __le32 save_hash_byte_cnt[2];
175 + u32 save_digest[16]; /* for MD5/SHA */
176 + __le32 save_digest_le32[16]; /* GHASH / CBC */
178 } __attribute__((packed));
181 @@ -192,8 +195,8 @@ struct sa_state_record {
183 struct dynamic_sa_aes128 {
184 struct dynamic_sa_ctl ctrl;
186 - u32 iv[4]; /* for CBC, OFC, and CFB mode */
188 + __le32 iv[4]; /* for CBC, OFC, and CFB mode */
191 } __attribute__((packed));
192 @@ -206,8 +209,8 @@ struct dynamic_sa_aes128 {
194 struct dynamic_sa_aes192 {
195 struct dynamic_sa_ctl ctrl;
197 - u32 iv[4]; /* for CBC, OFC, and CFB mode */
199 + __le32 iv[4]; /* for CBC, OFC, and CFB mode */
202 } __attribute__((packed));
203 @@ -220,8 +223,8 @@ struct dynamic_sa_aes192 {
205 struct dynamic_sa_aes256 {
206 struct dynamic_sa_ctl ctrl;
208 - u32 iv[4]; /* for CBC, OFC, and CFB mode */
210 + __le32 iv[4]; /* for CBC, OFC, and CFB mode */
213 } __attribute__((packed));
214 @@ -235,8 +238,8 @@ struct dynamic_sa_aes256 {
216 struct dynamic_sa_hash160 {
217 struct dynamic_sa_ctl ctrl;
218 - u32 inner_digest[5];
219 - u32 outer_digest[5];
220 + __le32 inner_digest[5];
221 + __le32 outer_digest[5];
224 } __attribute__((packed));
225 @@ -266,9 +269,9 @@ get_dynamic_sa_offset_state_ptr_field(st
226 return sizeof(struct dynamic_sa_ctl) + offset * 4;
229 -static inline u32 *get_dynamic_sa_key_field(struct dynamic_sa_ctl *cts)
230 +static inline __le32 *get_dynamic_sa_key_field(struct dynamic_sa_ctl *cts)
232 - return (u32 *) ((unsigned long)cts + sizeof(struct dynamic_sa_ctl));
233 + return (__le32 *) ((unsigned long)cts + sizeof(struct dynamic_sa_ctl));