Skip to content

Commit fbdae9f

Browse files
herbertxdavem330
authored andcommitted
[CRYPTO] Ensure cit_iv is aligned correctly
This patch ensures that cit_iv is aligned according to cra_alignmask by allocating it as part of the tfm structure. As a side effect the crypto layer will also guarantee that the tfm ctx area has enough space to be aligned by cra_alignmask. This allows us to remove the extra space reservation from the Padlock driver. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 176c365 commit fbdae9f

5 files changed

Lines changed: 72 additions & 11 deletions

File tree

‎crypto/api.c‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,46 @@ static void crypto_exit_ops(struct crypto_tfm *tfm)
125125
}
126126
}
127127

128+
static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
129+
{
130+
unsigned int len;
131+
132+
switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
133+
default:
134+
BUG();
135+
136+
case CRYPTO_ALG_TYPE_CIPHER:
137+
len = crypto_cipher_ctxsize(alg, flags);
138+
break;
139+
140+
case CRYPTO_ALG_TYPE_DIGEST:
141+
len = crypto_digest_ctxsize(alg, flags);
142+
break;
143+
144+
case CRYPTO_ALG_TYPE_COMPRESS:
145+
len = crypto_compress_ctxsize(alg, flags);
146+
break;
147+
}
148+
149+
return len + alg->cra_alignmask;
150+
}
151+
128152
struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
129153
{
130154
struct crypto_tfm *tfm = NULL;
131155
struct crypto_alg *alg;
156+
unsigned int tfm_size;
132157

133158
alg = crypto_alg_mod_lookup(name);
134159
if (alg == NULL)
135160
goto out;
136-
137-
tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
161+
162+
tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
163+
tfm = kmalloc(tfm_size, GFP_KERNEL);
138164
if (tfm == NULL)
139165
goto out_put;
140166

141-
memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
167+
memset(tfm, 0, tfm_size);
142168

143169
tfm->__crt_alg = alg;
144170

‎crypto/cipher.c‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static unsigned int crypt_slow(const struct cipher_desc *desc,
4141
struct scatter_walk *in,
4242
struct scatter_walk *out, unsigned int bsize)
4343
{
44-
unsigned int alignmask = desc->tfm->__crt_alg->cra_alignmask;
44+
unsigned int alignmask = crypto_tfm_alg_alignmask(desc->tfm);
4545
u8 buffer[bsize * 2 + alignmask];
4646
u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
4747
u8 *dst = src + bsize;
@@ -98,7 +98,7 @@ static int crypt(const struct cipher_desc *desc,
9898
struct scatter_walk walk_in, walk_out;
9999
struct crypto_tfm *tfm = desc->tfm;
100100
const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
101-
unsigned int alignmask = tfm->__crt_alg->cra_alignmask;
101+
unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
102102
unsigned long buffer = 0;
103103

104104
if (!nbytes)
@@ -399,6 +399,8 @@ int crypto_init_cipher_ops(struct crypto_tfm *tfm)
399399
}
400400

401401
if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
402+
unsigned int align;
403+
unsigned long addr;
402404

403405
switch (crypto_tfm_alg_blocksize(tfm)) {
404406
case 8:
@@ -418,9 +420,11 @@ int crypto_init_cipher_ops(struct crypto_tfm *tfm)
418420
}
419421

420422
ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
421-
ops->cit_iv = kmalloc(ops->cit_ivsize, GFP_KERNEL);
422-
if (ops->cit_iv == NULL)
423-
ret = -ENOMEM;
423+
align = crypto_tfm_alg_alignmask(tfm) + 1;
424+
addr = (unsigned long)crypto_tfm_ctx(tfm);
425+
addr = ALIGN(addr, align);
426+
addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
427+
ops->cit_iv = (void *)addr;
424428
}
425429

426430
out:
@@ -429,5 +433,4 @@ int crypto_init_cipher_ops(struct crypto_tfm *tfm)
429433

430434
void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
431435
{
432-
kfree(tfm->crt_cipher.cit_iv);
433436
}

‎crypto/internal.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/highmem.h>
1717
#include <linux/interrupt.h>
1818
#include <linux/init.h>
19+
#include <linux/kernel.h>
1920
#include <asm/kmap_types.h>
2021

2122
extern enum km_type crypto_km_types[];
@@ -61,6 +62,33 @@ static inline void crypto_init_proc(void)
6162
{ }
6263
#endif
6364

65+
static inline unsigned int crypto_digest_ctxsize(struct crypto_alg *alg,
66+
int flags)
67+
{
68+
return alg->cra_ctxsize;
69+
}
70+
71+
static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg,
72+
int flags)
73+
{
74+
unsigned int len = alg->cra_ctxsize;
75+
76+
switch (flags & CRYPTO_TFM_MODE_MASK) {
77+
case CRYPTO_TFM_MODE_CBC:
78+
len = ALIGN(len, alg->cra_alignmask + 1);
79+
len += alg->cra_blocksize;
80+
break;
81+
}
82+
83+
return len;
84+
}
85+
86+
static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg,
87+
int flags)
88+
{
89+
return alg->cra_ctxsize;
90+
}
91+
6492
int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags);
6593
int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags);
6694
int crypto_init_compress_flags(struct crypto_tfm *tfm, u32 flags);

‎drivers/crypto/padlock-aes.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ static struct crypto_alg aes_alg = {
465465
.cra_name = "aes",
466466
.cra_flags = CRYPTO_ALG_TYPE_CIPHER,
467467
.cra_blocksize = AES_BLOCK_SIZE,
468-
.cra_ctxsize = sizeof(struct aes_ctx) +
469-
PADLOCK_ALIGNMENT,
468+
.cra_ctxsize = sizeof(struct aes_ctx),
470469
.cra_alignmask = PADLOCK_ALIGNMENT - 1,
471470
.cra_module = THIS_MODULE,
472471
.cra_list = LIST_HEAD_INIT(aes_alg.cra_list),

‎include/linux/crypto.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
288288
return tfm->__crt_alg->cra_digest.dia_digestsize;
289289
}
290290

291+
static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
292+
{
293+
return tfm->__crt_alg->cra_alignmask;
294+
}
295+
291296
static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
292297
{
293298
return (void *)&tfm[1];

0 commit comments

Comments
 (0)