Skip to content

Commit 560c06a

Browse files
committed
[CRYPTO] api: Get rid of flags argument to setkey
Now that the tfm is passed directly to setkey instead of the ctx, we no longer need to pass the &tfm->crt_flags pointer. This patch also gets rid of a few unnecessary checks on the key length for ciphers as the cipher layer guarantees that the key length is within the bounds specified by the algorithm. Rather than testing dia_setkey every time, this patch does it only once during crypto_alloc_tfm. The redundant check from crypto_digest_setkey is also removed. Signed-off-by: Herbert Xu <[email protected]>
1 parent 25cdbcd commit 560c06a

24 files changed

+63
-92
lines changed

arch/i386/crypto/aes.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,13 @@ static void gen_tabs(void)
379379
}
380380

381381
static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
382-
unsigned int key_len, u32 *flags)
382+
unsigned int key_len)
383383
{
384384
int i;
385385
u32 ss[8];
386386
struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
387387
const __le32 *key = (const __le32 *)in_key;
388+
u32 *flags = &tfm->crt_flags;
388389

389390
/* encryption schedule */
390391

arch/s390/crypto/aes_s390.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ struct s390_aes_ctx {
3838
};
3939

4040
static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
41-
unsigned int key_len, u32 *flags)
41+
unsigned int key_len)
4242
{
4343
struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
44+
u32 *flags = &tfm->crt_flags;
4445

4546
switch (key_len) {
4647
case 16:

arch/s390/crypto/des_s390.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ struct crypt_s390_des3_192_ctx {
4545
};
4646

4747
static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
48-
unsigned int keylen, u32 *flags)
48+
unsigned int keylen)
4949
{
5050
struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
51+
u32 *flags = &tfm->crt_flags;
5152
int ret;
5253

5354
/* test if key is valid (not a weak key) */
@@ -167,11 +168,12 @@ static struct crypto_alg des_alg = {
167168
*
168169
*/
169170
static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key,
170-
unsigned int keylen, u32 *flags)
171+
unsigned int keylen)
171172
{
172173
int i, ret;
173174
struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
174-
const u8* temp_key = key;
175+
const u8 *temp_key = key;
176+
u32 *flags = &tfm->crt_flags;
175177

176178
if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
177179
*flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
@@ -303,11 +305,12 @@ static struct crypto_alg des3_128_alg = {
303305
*
304306
*/
305307
static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
306-
unsigned int keylen, u32 *flags)
308+
unsigned int keylen)
307309
{
308310
int i, ret;
309311
struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
310-
const u8* temp_key = key;
312+
const u8 *temp_key = key;
313+
u32 *flags = &tfm->crt_flags;
311314

312315
if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
313316
memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],

arch/x86_64/crypto/aes.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,14 @@ static void __init gen_tabs(void)
228228
}
229229

230230
static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
231-
unsigned int key_len, u32 *flags)
231+
unsigned int key_len)
232232
{
233233
struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
234234
const __le32 *key = (const __le32 *)in_key;
235+
u32 *flags = &tfm->crt_flags;
235236
u32 i, j, t, u, v, w;
236237

237-
if (key_len != 16 && key_len != 24 && key_len != 32) {
238+
if (key_len % 8) {
238239
*flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
239240
return -EINVAL;
240241
}

crypto/aes.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,14 @@ gen_tabs (void)
249249
}
250250

251251
static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
252-
unsigned int key_len, u32 *flags)
252+
unsigned int key_len)
253253
{
254254
struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
255255
const __le32 *key = (const __le32 *)in_key;
256+
u32 *flags = &tfm->crt_flags;
256257
u32 i, t, u, v, w;
257258

258-
if (key_len != 16 && key_len != 24 && key_len != 32) {
259+
if (key_len % 8) {
259260
*flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
260261
return -EINVAL;
261262
}

crypto/anubis.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,11 @@ static const u32 rc[] = {
461461
};
462462

463463
static int anubis_setkey(struct crypto_tfm *tfm, const u8 *in_key,
464-
unsigned int key_len, u32 *flags)
464+
unsigned int key_len)
465465
{
466466
struct anubis_ctx *ctx = crypto_tfm_ctx(tfm);
467467
const __be32 *key = (const __be32 *)in_key;
468+
u32 *flags = &tfm->crt_flags;
468469
int N, R, i, r;
469470
u32 kappa[ANUBIS_MAX_N];
470471
u32 inter[ANUBIS_MAX_N];

crypto/arc4.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct arc4_ctx {
2525
};
2626

2727
static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
28-
unsigned int key_len, u32 *flags)
28+
unsigned int key_len)
2929
{
3030
struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
3131
int i, j = 0, k = 0;

crypto/blowfish.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,7 @@ static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
399399
/*
400400
* Calculates the blowfish S and P boxes for encryption and decryption.
401401
*/
402-
static int bf_setkey(struct crypto_tfm *tfm, const u8 *key,
403-
unsigned int keylen, u32 *flags)
402+
static int bf_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
404403
{
405404
struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
406405
u32 *P = ctx->p;

crypto/cast5.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -769,20 +769,14 @@ static void key_schedule(u32 * x, u32 * z, u32 * k)
769769
}
770770

771771

772-
static int cast5_setkey(struct crypto_tfm *tfm, const u8 *key,
773-
unsigned key_len, u32 *flags)
772+
static int cast5_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned key_len)
774773
{
775774
struct cast5_ctx *c = crypto_tfm_ctx(tfm);
776775
int i;
777776
u32 x[4];
778777
u32 z[4];
779778
u32 k[16];
780779
__be32 p_key[4];
781-
782-
if (key_len < 5 || key_len > 16) {
783-
*flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
784-
return -EINVAL;
785-
}
786780

787781
c->rr = key_len <= 10 ? 1 : 0;
788782

crypto/cast6.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,15 @@ static inline void W(u32 *key, unsigned int i) {
382382
}
383383

384384
static int cast6_setkey(struct crypto_tfm *tfm, const u8 *in_key,
385-
unsigned key_len, u32 *flags)
385+
unsigned key_len)
386386
{
387387
int i;
388388
u32 key[8];
389389
__be32 p_key[8]; /* padded key */
390390
struct cast6_ctx *c = crypto_tfm_ctx(tfm);
391+
u32 *flags = &tfm->crt_flags;
391392

392-
if (key_len < 16 || key_len > 32 || key_len % 4 != 0) {
393+
if (key_len % 4 != 0) {
393394
*flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
394395
return -EINVAL;
395396
}

crypto/cipher.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,12 @@ static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
264264
{
265265
struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
266266

267+
tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
267268
if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
268269
tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
269270
return -EINVAL;
270271
} else
271-
return cia->cia_setkey(tfm, key, keylen,
272-
&tfm->crt_flags);
272+
return cia->cia_setkey(tfm, key, keylen);
273273
}
274274

275275
static int ecb_encrypt(struct crypto_tfm *tfm,

crypto/crc32c.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ static void chksum_init(struct crypto_tfm *tfm)
4444
* the seed.
4545
*/
4646
static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
47-
unsigned int keylen, u32 *flags)
47+
unsigned int keylen)
4848
{
4949
struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
5050

5151
if (keylen != sizeof(mctx->crc)) {
52-
if (flags)
53-
*flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
52+
tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
5453
return -EINVAL;
5554
}
5655
mctx->key = le32_to_cpu(*(__le32 *)key);

crypto/crypto_null.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void null_final(struct crypto_tfm *tfm, u8 *out)
4848
{ }
4949

5050
static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
51-
unsigned int keylen, u32 *flags)
51+
unsigned int keylen)
5252
{ return 0; }
5353

5454
static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)

crypto/des.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,10 @@ static void dkey(u32 *pe, const u8 *k)
784784
}
785785

786786
static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
787-
unsigned int keylen, u32 *flags)
787+
unsigned int keylen)
788788
{
789789
struct des_ctx *dctx = crypto_tfm_ctx(tfm);
790+
u32 *flags = &tfm->crt_flags;
790791
u32 tmp[DES_EXPKEY_WORDS];
791792
int ret;
792793

@@ -864,11 +865,12 @@ static void des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
864865
*
865866
*/
866867
static int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key,
867-
unsigned int keylen, u32 *flags)
868+
unsigned int keylen)
868869
{
869870
const u32 *K = (const u32 *)key;
870871
struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
871872
u32 *expkey = dctx->expkey;
873+
u32 *flags = &tfm->crt_flags;
872874

873875
if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
874876
!((K[2] ^ K[4]) | (K[3] ^ K[5]))))

crypto/digest.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ static void final(struct crypto_tfm *tfm, u8 *out)
7676
tfm->__crt_alg->cra_digest.dia_final(tfm, out);
7777
}
7878

79+
static int nosetkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
80+
{
81+
tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
82+
return -ENOSYS;
83+
}
84+
7985
static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
8086
{
81-
u32 flags;
82-
if (tfm->__crt_alg->cra_digest.dia_setkey == NULL)
83-
return -ENOSYS;
84-
return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen, &flags);
87+
tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
88+
return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen);
8589
}
8690

8791
static void digest(struct crypto_tfm *tfm,
@@ -100,12 +104,13 @@ int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)
100104
int crypto_init_digest_ops(struct crypto_tfm *tfm)
101105
{
102106
struct digest_tfm *ops = &tfm->crt_digest;
107+
struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
103108

104109
ops->dit_init = init;
105110
ops->dit_update = update;
106111
ops->dit_final = final;
107112
ops->dit_digest = digest;
108-
ops->dit_setkey = setkey;
113+
ops->dit_setkey = dalg->dia_setkey ? setkey : nosetkey;
109114

110115
return crypto_alloc_hmac_block(tfm);
111116
}

crypto/khazad.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -755,19 +755,13 @@ static const u64 c[KHAZAD_ROUNDS + 1] = {
755755
};
756756

757757
static int khazad_setkey(struct crypto_tfm *tfm, const u8 *in_key,
758-
unsigned int key_len, u32 *flags)
758+
unsigned int key_len)
759759
{
760760
struct khazad_ctx *ctx = crypto_tfm_ctx(tfm);
761761
const __be32 *key = (const __be32 *)in_key;
762762
int r;
763763
const u64 *S = T7;
764764
u64 K2, K1;
765-
766-
if (key_len != 16)
767-
{
768-
*flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
769-
return -EINVAL;
770-
}
771765

772766
/* key is supposed to be 32-bit aligned */
773767
K2 = ((u64)be32_to_cpu(key[0]) << 32) | be32_to_cpu(key[1]);

crypto/michael_mic.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,13 @@ static void michael_final(struct crypto_tfm *tfm, u8 *out)
123123

124124

125125
static int michael_setkey(struct crypto_tfm *tfm, const u8 *key,
126-
unsigned int keylen, u32 *flags)
126+
unsigned int keylen)
127127
{
128128
struct michael_mic_ctx *mctx = crypto_tfm_ctx(tfm);
129129
const __le32 *data = (const __le32 *)key;
130130

131131
if (keylen != 8) {
132-
if (flags)
133-
*flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
132+
tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
134133
return -EINVAL;
135134
}
136135

crypto/serpent.c

+3-16
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,14 @@ struct serpent_ctx {
216216

217217

218218
static int serpent_setkey(struct crypto_tfm *tfm, const u8 *key,
219-
unsigned int keylen, u32 *flags)
219+
unsigned int keylen)
220220
{
221221
struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
222222
u32 *k = ctx->expkey;
223223
u8 *k8 = (u8 *)k;
224224
u32 r0,r1,r2,r3,r4;
225225
int i;
226226

227-
if ((keylen < SERPENT_MIN_KEY_SIZE)
228-
|| (keylen > SERPENT_MAX_KEY_SIZE))
229-
{
230-
*flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
231-
return -EINVAL;
232-
}
233-
234227
/* Copy key, add padding */
235228

236229
for (i = 0; i < keylen; ++i)
@@ -497,21 +490,15 @@ static struct crypto_alg serpent_alg = {
497490
};
498491

499492
static int tnepres_setkey(struct crypto_tfm *tfm, const u8 *key,
500-
unsigned int keylen, u32 *flags)
493+
unsigned int keylen)
501494
{
502495
u8 rev_key[SERPENT_MAX_KEY_SIZE];
503496
int i;
504497

505-
if ((keylen < SERPENT_MIN_KEY_SIZE)
506-
|| (keylen > SERPENT_MAX_KEY_SIZE)) {
507-
*flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
508-
return -EINVAL;
509-
}
510-
511498
for (i = 0; i < keylen; ++i)
512499
rev_key[keylen - i - 1] = key[i];
513500

514-
return serpent_setkey(tfm, rev_key, keylen, flags);
501+
return serpent_setkey(tfm, rev_key, keylen);
515502
}
516503

517504
static void tnepres_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)

crypto/tcrypt.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ static void test_hash(char *algo, struct hash_testvec *template,
118118
sg_set_buf(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
119119

120120
crypto_digest_init(tfm);
121-
if (tfm->crt_u.digest.dit_setkey) {
122-
crypto_digest_setkey(tfm, hash_tv[i].key,
123-
hash_tv[i].ksize);
124-
}
121+
crypto_digest_setkey(tfm, hash_tv[i].key, hash_tv[i].ksize);
125122
crypto_digest_update(tfm, sg, 1);
126123
crypto_digest_final(tfm, result);
127124

0 commit comments

Comments
 (0)