Skip to content

Commit b350bee

Browse files
keesherbertx
authored andcommitted
crypto: skcipher - Introduce crypto_sync_skcipher
In preparation for removal of VLAs due to skcipher requests on the stack via SKCIPHER_REQUEST_ON_STACK() usage, this introduces the infrastructure for the "sync skcipher" tfm, which is for handling the on-stack cases of skcipher, which are always non-ASYNC and have a known limited request size. The crypto API additions: struct crypto_sync_skcipher (wrapper for struct crypto_skcipher) crypto_alloc_sync_skcipher() crypto_free_sync_skcipher() crypto_sync_skcipher_setkey() crypto_sync_skcipher_get_flags() crypto_sync_skcipher_set_flags() crypto_sync_skcipher_clear_flags() crypto_sync_skcipher_blocksize() crypto_sync_skcipher_ivsize() crypto_sync_skcipher_reqtfm() skcipher_request_set_sync_tfm() SYNC_SKCIPHER_REQUEST_ON_STACK() (with tfm type check) Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3944f13 commit b350bee

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎crypto/skcipher.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,30 @@ struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
949949
}
950950
EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
951951

952+
struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(
953+
const char *alg_name, u32 type, u32 mask)
954+
{
955+
struct crypto_skcipher *tfm;
956+
957+
/* Only sync algorithms allowed. */
958+
mask |= CRYPTO_ALG_ASYNC;
959+
960+
tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
961+
962+
/*
963+
* Make sure we do not allocate something that might get used with
964+
* an on-stack request: check the request size.
965+
*/
966+
if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
967+
MAX_SYNC_SKCIPHER_REQSIZE)) {
968+
crypto_free_skcipher(tfm);
969+
return ERR_PTR(-EINVAL);
970+
}
971+
972+
return (struct crypto_sync_skcipher *)tfm;
973+
}
974+
EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
975+
952976
int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
953977
{
954978
return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,

‎include/crypto/skcipher.h‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ struct crypto_skcipher {
6565
struct crypto_tfm base;
6666
};
6767

68+
struct crypto_sync_skcipher {
69+
struct crypto_skcipher base;
70+
};
71+
6872
/**
6973
* struct skcipher_alg - symmetric key cipher definition
7074
* @min_keysize: Minimum key size supported by the transformation. This is the
@@ -139,6 +143,19 @@ struct skcipher_alg {
139143
struct crypto_alg base;
140144
};
141145

146+
#define MAX_SYNC_SKCIPHER_REQSIZE 384
147+
/*
148+
* This performs a type-check against the "tfm" argument to make sure
149+
* all users have the correct skcipher tfm for doing on-stack requests.
150+
*/
151+
#define SYNC_SKCIPHER_REQUEST_ON_STACK(name, tfm) \
152+
char __##name##_desc[sizeof(struct skcipher_request) + \
153+
MAX_SYNC_SKCIPHER_REQSIZE + \
154+
(!(sizeof((struct crypto_sync_skcipher *)1 == \
155+
(typeof(tfm))1))) \
156+
] CRYPTO_MINALIGN_ATTR; \
157+
struct skcipher_request *name = (void *)__##name##_desc
158+
142159
#define SKCIPHER_REQUEST_ON_STACK(name, tfm) \
143160
char __##name##_desc[sizeof(struct skcipher_request) + \
144161
crypto_skcipher_reqsize(tfm)] CRYPTO_MINALIGN_ATTR; \
@@ -197,6 +214,9 @@ static inline struct crypto_skcipher *__crypto_skcipher_cast(
197214
struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
198215
u32 type, u32 mask);
199216

217+
struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(const char *alg_name,
218+
u32 type, u32 mask);
219+
200220
static inline struct crypto_tfm *crypto_skcipher_tfm(
201221
struct crypto_skcipher *tfm)
202222
{
@@ -212,6 +232,11 @@ static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
212232
crypto_destroy_tfm(tfm, crypto_skcipher_tfm(tfm));
213233
}
214234

235+
static inline void crypto_free_sync_skcipher(struct crypto_sync_skcipher *tfm)
236+
{
237+
crypto_free_skcipher(&tfm->base);
238+
}
239+
215240
/**
216241
* crypto_has_skcipher() - Search for the availability of an skcipher.
217242
* @alg_name: is the cra_name / name or cra_driver_name / driver name of the
@@ -280,6 +305,12 @@ static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm)
280305
return tfm->ivsize;
281306
}
282307

308+
static inline unsigned int crypto_sync_skcipher_ivsize(
309+
struct crypto_sync_skcipher *tfm)
310+
{
311+
return crypto_skcipher_ivsize(&tfm->base);
312+
}
313+
283314
static inline unsigned int crypto_skcipher_alg_chunksize(
284315
struct skcipher_alg *alg)
285316
{
@@ -356,6 +387,12 @@ static inline unsigned int crypto_skcipher_blocksize(
356387
return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm));
357388
}
358389

390+
static inline unsigned int crypto_sync_skcipher_blocksize(
391+
struct crypto_sync_skcipher *tfm)
392+
{
393+
return crypto_skcipher_blocksize(&tfm->base);
394+
}
395+
359396
static inline unsigned int crypto_skcipher_alignmask(
360397
struct crypto_skcipher *tfm)
361398
{
@@ -379,6 +416,24 @@ static inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm,
379416
crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags);
380417
}
381418

419+
static inline u32 crypto_sync_skcipher_get_flags(
420+
struct crypto_sync_skcipher *tfm)
421+
{
422+
return crypto_skcipher_get_flags(&tfm->base);
423+
}
424+
425+
static inline void crypto_sync_skcipher_set_flags(
426+
struct crypto_sync_skcipher *tfm, u32 flags)
427+
{
428+
crypto_skcipher_set_flags(&tfm->base, flags);
429+
}
430+
431+
static inline void crypto_sync_skcipher_clear_flags(
432+
struct crypto_sync_skcipher *tfm, u32 flags)
433+
{
434+
crypto_skcipher_clear_flags(&tfm->base, flags);
435+
}
436+
382437
/**
383438
* crypto_skcipher_setkey() - set key for cipher
384439
* @tfm: cipher handle
@@ -401,6 +456,12 @@ static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
401456
return tfm->setkey(tfm, key, keylen);
402457
}
403458

459+
static inline int crypto_sync_skcipher_setkey(struct crypto_sync_skcipher *tfm,
460+
const u8 *key, unsigned int keylen)
461+
{
462+
return crypto_skcipher_setkey(&tfm->base, key, keylen);
463+
}
464+
404465
static inline unsigned int crypto_skcipher_default_keysize(
405466
struct crypto_skcipher *tfm)
406467
{
@@ -422,6 +483,14 @@ static inline struct crypto_skcipher *crypto_skcipher_reqtfm(
422483
return __crypto_skcipher_cast(req->base.tfm);
423484
}
424485

486+
static inline struct crypto_sync_skcipher *crypto_sync_skcipher_reqtfm(
487+
struct skcipher_request *req)
488+
{
489+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
490+
491+
return container_of(tfm, struct crypto_sync_skcipher, base);
492+
}
493+
425494
/**
426495
* crypto_skcipher_encrypt() - encrypt plaintext
427496
* @req: reference to the skcipher_request handle that holds all information
@@ -500,6 +569,12 @@ static inline void skcipher_request_set_tfm(struct skcipher_request *req,
500569
req->base.tfm = crypto_skcipher_tfm(tfm);
501570
}
502571

572+
static inline void skcipher_request_set_sync_tfm(struct skcipher_request *req,
573+
struct crypto_sync_skcipher *tfm)
574+
{
575+
skcipher_request_set_tfm(req, &tfm->base);
576+
}
577+
503578
static inline struct skcipher_request *skcipher_request_cast(
504579
struct crypto_async_request *req)
505580
{

0 commit comments

Comments
 (0)