Skip to content

Commit

Permalink
[CRYPTO] api: Align tfm context as wide as possible
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Herbert Xu committed Mar 21, 2006
1 parent a5f8c47 commit f10b789
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crypto/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
break;
}

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

struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
Expand Down
6 changes: 5 additions & 1 deletion drivers/crypto/padlock-aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,11 @@ aes_hw_extkey_available(uint8_t key_len)

static inline struct aes_ctx *aes_ctx(void *ctx)
{
return (struct aes_ctx *)ALIGN((unsigned long)ctx, PADLOCK_ALIGNMENT);
unsigned long align = PADLOCK_ALIGNMENT;

if (align <= crypto_tfm_ctx_alignment())
align = 1;
return (struct aes_ctx *)ALIGN((unsigned long)ctx, align);
}

static int
Expand Down
10 changes: 9 additions & 1 deletion include/linux/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ struct crypto_tfm {
} crt_u;

struct crypto_alg *__crt_alg;

char __crt_ctx[] __attribute__ ((__aligned__));
};

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

static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
{
return (void *)&tfm[1];
return tfm->__crt_ctx;
}

static inline unsigned int crypto_tfm_ctx_alignment(void)
{
struct crypto_tfm *tfm;
return __alignof__(tfm->__crt_ctx);
}

/*
Expand Down

0 comments on commit f10b789

Please sign in to comment.