Skip to content

Commit f10b789

Browse files
committed
[CRYPTO] api: Align tfm context as wide as possible
Since tfm contexts can contain arbitrary types we should provide at least natural alignment (__attribute__ ((__aligned__))) for them. In particular, this is needed on the Xscale which is a 32-bit architecture with a u64 type that requires 64-bit alignment. This problem was reported by Ronen Shitrit. The crypto_tfm structure's size was 44 bytes on 32-bit architectures and 80 bytes on 64-bit architectures. So adding this requirement only means that we have to add an extra 4 bytes on 32-bit architectures. On i386 the natural alignment is 16 bytes which also benefits the VIA Padlock as it no longer has to manually align its context structure to 128 bits. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent a5f8c47 commit f10b789

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

‎crypto/api.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
165165
break;
166166
}
167167

168-
return len + alg->cra_alignmask;
168+
return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
169169
}
170170

171171
struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)

‎drivers/crypto/padlock-aes.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,11 @@ aes_hw_extkey_available(uint8_t key_len)
284284

285285
static inline struct aes_ctx *aes_ctx(void *ctx)
286286
{
287-
return (struct aes_ctx *)ALIGN((unsigned long)ctx, PADLOCK_ALIGNMENT);
287+
unsigned long align = PADLOCK_ALIGNMENT;
288+
289+
if (align <= crypto_tfm_ctx_alignment())
290+
align = 1;
291+
return (struct aes_ctx *)ALIGN((unsigned long)ctx, align);
288292
}
289293

290294
static int

‎include/linux/crypto.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ struct crypto_tfm {
229229
} crt_u;
230230

231231
struct crypto_alg *__crt_alg;
232+
233+
char __crt_ctx[] __attribute__ ((__aligned__));
232234
};
233235

234236
/*
@@ -301,7 +303,13 @@ static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
301303

302304
static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
303305
{
304-
return (void *)&tfm[1];
306+
return tfm->__crt_ctx;
307+
}
308+
309+
static inline unsigned int crypto_tfm_ctx_alignment(void)
310+
{
311+
struct crypto_tfm *tfm;
312+
return __alignof__(tfm->__crt_ctx);
305313
}
306314

307315
/*

0 commit comments

Comments
 (0)