Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[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>

+15 -3
+1 -1
crypto/api.c
··· 165 165 break; 166 166 } 167 167 168 - return len + alg->cra_alignmask; 168 + return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1)); 169 169 } 170 170 171 171 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
+5 -1
drivers/crypto/padlock-aes.c
··· 284 284 285 285 static inline struct aes_ctx *aes_ctx(void *ctx) 286 286 { 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); 288 292 } 289 293 290 294 static int
+9 -1
include/linux/crypto.h
··· 229 229 } crt_u; 230 230 231 231 struct crypto_alg *__crt_alg; 232 + 233 + char __crt_ctx[] __attribute__ ((__aligned__)); 232 234 }; 233 235 234 236 /* ··· 303 301 304 302 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) 305 303 { 306 - return (void *)&tfm[1]; 304 + return tfm->__crt_ctx; 305 + } 306 + 307 + static inline unsigned int crypto_tfm_ctx_alignment(void) 308 + { 309 + struct crypto_tfm *tfm; 310 + return __alignof__(tfm->__crt_ctx); 307 311 } 308 312 309 313 /*