Commit 560c06ae1ab7c677002ea3b6ac83521bf12ee07d

Authored by Herbert Xu
1 parent 25cdbcd9e5

[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 <herbert@gondor.apana.org.au>

Showing 24 changed files with 63 additions and 92 deletions Side-by-side Diff

arch/i386/crypto/aes.c
... ... @@ -379,12 +379,13 @@
379 379 }
380 380  
381 381 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)
383 383 {
384 384 int i;
385 385 u32 ss[8];
386 386 struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
387 387 const __le32 *key = (const __le32 *)in_key;
  388 + u32 *flags = &tfm->crt_flags;
388 389  
389 390 /* encryption schedule */
390 391  
arch/s390/crypto/aes_s390.c
... ... @@ -38,9 +38,10 @@
38 38 };
39 39  
40 40 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)
42 42 {
43 43 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  44 + u32 *flags = &tfm->crt_flags;
44 45  
45 46 switch (key_len) {
46 47 case 16:
arch/s390/crypto/des_s390.c
... ... @@ -45,9 +45,10 @@
45 45 };
46 46  
47 47 static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
48   - unsigned int keylen, u32 *flags)
  48 + unsigned int keylen)
49 49 {
50 50 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  51 + u32 *flags = &tfm->crt_flags;
51 52 int ret;
52 53  
53 54 /* test if key is valid (not a weak key) */
54 55  
... ... @@ -167,11 +168,12 @@
167 168 *
168 169 */
169 170 static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key,
170   - unsigned int keylen, u32 *flags)
  171 + unsigned int keylen)
171 172 {
172 173 int i, ret;
173 174 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;
175 177  
176 178 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
177 179 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
178 180  
... ... @@ -303,11 +305,12 @@
303 305 *
304 306 */
305 307 static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
306   - unsigned int keylen, u32 *flags)
  308 + unsigned int keylen)
307 309 {
308 310 int i, ret;
309 311 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;
311 314  
312 315 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
313 316 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
arch/x86_64/crypto/aes.c
... ... @@ -228,13 +228,14 @@
228 228 }
229 229  
230 230 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)
232 232 {
233 233 struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
234 234 const __le32 *key = (const __le32 *)in_key;
  235 + u32 *flags = &tfm->crt_flags;
235 236 u32 i, j, t, u, v, w;
236 237  
237   - if (key_len != 16 && key_len != 24 && key_len != 32) {
  238 + if (key_len % 8) {
238 239 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
239 240 return -EINVAL;
240 241 }
... ... @@ -249,13 +249,14 @@
249 249 }
250 250  
251 251 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)
253 253 {
254 254 struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
255 255 const __le32 *key = (const __le32 *)in_key;
  256 + u32 *flags = &tfm->crt_flags;
256 257 u32 i, t, u, v, w;
257 258  
258   - if (key_len != 16 && key_len != 24 && key_len != 32) {
  259 + if (key_len % 8) {
259 260 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
260 261 return -EINVAL;
261 262 }
... ... @@ -461,10 +461,11 @@
461 461 };
462 462  
463 463 static int anubis_setkey(struct crypto_tfm *tfm, const u8 *in_key,
464   - unsigned int key_len, u32 *flags)
  464 + unsigned int key_len)
465 465 {
466 466 struct anubis_ctx *ctx = crypto_tfm_ctx(tfm);
467 467 const __be32 *key = (const __be32 *)in_key;
  468 + u32 *flags = &tfm->crt_flags;
468 469 int N, R, i, r;
469 470 u32 kappa[ANUBIS_MAX_N];
470 471 u32 inter[ANUBIS_MAX_N];
... ... @@ -25,7 +25,7 @@
25 25 };
26 26  
27 27 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)
29 29 {
30 30 struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
31 31 int i, j = 0, k = 0;
... ... @@ -399,8 +399,7 @@
399 399 /*
400 400 * Calculates the blowfish S and P boxes for encryption and decryption.
401 401 */
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)
404 403 {
405 404 struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
406 405 u32 *P = ctx->p;
... ... @@ -769,8 +769,7 @@
769 769 }
770 770  
771 771  
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)
774 773 {
775 774 struct cast5_ctx *c = crypto_tfm_ctx(tfm);
776 775 int i;
... ... @@ -778,11 +777,6 @@
778 777 u32 z[4];
779 778 u32 k[16];
780 779 __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   - }
786 780  
787 781 c->rr = key_len <= 10 ? 1 : 0;
788 782  
... ... @@ -382,14 +382,15 @@
382 382 }
383 383  
384 384 static int cast6_setkey(struct crypto_tfm *tfm, const u8 *in_key,
385   - unsigned key_len, u32 *flags)
  385 + unsigned key_len)
386 386 {
387 387 int i;
388 388 u32 key[8];
389 389 __be32 p_key[8]; /* padded key */
390 390 struct cast6_ctx *c = crypto_tfm_ctx(tfm);
  391 + u32 *flags = &tfm->crt_flags;
391 392  
392   - if (key_len < 16 || key_len > 32 || key_len % 4 != 0) {
  393 + if (key_len % 4 != 0) {
393 394 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
394 395 return -EINVAL;
395 396 }
... ... @@ -264,12 +264,12 @@
264 264 {
265 265 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
266 266  
  267 + tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
267 268 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
268 269 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
269 270 return -EINVAL;
270 271 } else
271   - return cia->cia_setkey(tfm, key, keylen,
272   - &tfm->crt_flags);
  272 + return cia->cia_setkey(tfm, key, keylen);
273 273 }
274 274  
275 275 static int ecb_encrypt(struct crypto_tfm *tfm,
... ... @@ -44,13 +44,12 @@
44 44 * the seed.
45 45 */
46 46 static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
47   - unsigned int keylen, u32 *flags)
  47 + unsigned int keylen)
48 48 {
49 49 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
50 50  
51 51 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;
54 53 return -EINVAL;
55 54 }
56 55 mctx->key = le32_to_cpu(*(__le32 *)key);
crypto/crypto_null.c
... ... @@ -48,7 +48,7 @@
48 48 { }
49 49  
50 50 static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
51   - unsigned int keylen, u32 *flags)
  51 + unsigned int keylen)
52 52 { return 0; }
53 53  
54 54 static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
... ... @@ -784,9 +784,10 @@
784 784 }
785 785  
786 786 static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
787   - unsigned int keylen, u32 *flags)
  787 + unsigned int keylen)
788 788 {
789 789 struct des_ctx *dctx = crypto_tfm_ctx(tfm);
  790 + u32 *flags = &tfm->crt_flags;
790 791 u32 tmp[DES_EXPKEY_WORDS];
791 792 int ret;
792 793  
793 794  
... ... @@ -864,11 +865,12 @@
864 865 *
865 866 */
866 867 static int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key,
867   - unsigned int keylen, u32 *flags)
  868 + unsigned int keylen)
868 869 {
869 870 const u32 *K = (const u32 *)key;
870 871 struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
871 872 u32 *expkey = dctx->expkey;
  873 + u32 *flags = &tfm->crt_flags;
872 874  
873 875 if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
874 876 !((K[2] ^ K[4]) | (K[3] ^ K[5]))))
... ... @@ -76,12 +76,16 @@
76 76 tfm->__crt_alg->cra_digest.dia_final(tfm, out);
77 77 }
78 78  
  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 +
79 85 static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
80 86 {
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);
85 89 }
86 90  
87 91 static void digest(struct crypto_tfm *tfm,
88 92  
... ... @@ -100,12 +104,13 @@
100 104 int crypto_init_digest_ops(struct crypto_tfm *tfm)
101 105 {
102 106 struct digest_tfm *ops = &tfm->crt_digest;
  107 + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
103 108  
104 109 ops->dit_init = init;
105 110 ops->dit_update = update;
106 111 ops->dit_final = final;
107 112 ops->dit_digest = digest;
108   - ops->dit_setkey = setkey;
  113 + ops->dit_setkey = dalg->dia_setkey ? setkey : nosetkey;
109 114  
110 115 return crypto_alloc_hmac_block(tfm);
111 116 }
... ... @@ -755,19 +755,13 @@
755 755 };
756 756  
757 757 static int khazad_setkey(struct crypto_tfm *tfm, const u8 *in_key,
758   - unsigned int key_len, u32 *flags)
  758 + unsigned int key_len)
759 759 {
760 760 struct khazad_ctx *ctx = crypto_tfm_ctx(tfm);
761 761 const __be32 *key = (const __be32 *)in_key;
762 762 int r;
763 763 const u64 *S = T7;
764 764 u64 K2, K1;
765   -
766   - if (key_len != 16)
767   - {
768   - *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
769   - return -EINVAL;
770   - }
771 765  
772 766 /* key is supposed to be 32-bit aligned */
773 767 K2 = ((u64)be32_to_cpu(key[0]) << 32) | be32_to_cpu(key[1]);
crypto/michael_mic.c
... ... @@ -123,14 +123,13 @@
123 123  
124 124  
125 125 static int michael_setkey(struct crypto_tfm *tfm, const u8 *key,
126   - unsigned int keylen, u32 *flags)
  126 + unsigned int keylen)
127 127 {
128 128 struct michael_mic_ctx *mctx = crypto_tfm_ctx(tfm);
129 129 const __le32 *data = (const __le32 *)key;
130 130  
131 131 if (keylen != 8) {
132   - if (flags)
133   - *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
  132 + tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
134 133 return -EINVAL;
135 134 }
136 135  
... ... @@ -216,7 +216,7 @@
216 216  
217 217  
218 218 static int serpent_setkey(struct crypto_tfm *tfm, const u8 *key,
219   - unsigned int keylen, u32 *flags)
  219 + unsigned int keylen)
220 220 {
221 221 struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
222 222 u32 *k = ctx->expkey;
... ... @@ -224,13 +224,6 @@
224 224 u32 r0,r1,r2,r3,r4;
225 225 int i;
226 226  
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   -
234 227 /* Copy key, add padding */
235 228  
236 229 for (i = 0; i < keylen; ++i)
237 230  
238 231  
... ... @@ -497,21 +490,15 @@
497 490 };
498 491  
499 492 static int tnepres_setkey(struct crypto_tfm *tfm, const u8 *key,
500   - unsigned int keylen, u32 *flags)
  493 + unsigned int keylen)
501 494 {
502 495 u8 rev_key[SERPENT_MAX_KEY_SIZE];
503 496 int i;
504 497  
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   -
511 498 for (i = 0; i < keylen; ++i)
512 499 rev_key[keylen - i - 1] = key[i];
513 500  
514   - return serpent_setkey(tfm, rev_key, keylen, flags);
  501 + return serpent_setkey(tfm, rev_key, keylen);
515 502 }
516 503  
517 504 static void tnepres_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
... ... @@ -118,10 +118,7 @@
118 118 sg_set_buf(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
119 119  
120 120 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);
125 122 crypto_digest_update(tfm, sg, 1);
126 123 crypto_digest_final(tfm, result);
127 124  
... ... @@ -46,16 +46,10 @@
46 46 };
47 47  
48 48 static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
49   - unsigned int key_len, u32 *flags)
  49 + unsigned int key_len)
50 50 {
51 51 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
52 52 const __le32 *key = (const __le32 *)in_key;
53   -
54   - if (key_len != 16)
55   - {
56   - *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
57   - return -EINVAL;
58   - }
59 53  
60 54 ctx->KEY[0] = le32_to_cpu(key[0]);
61 55 ctx->KEY[1] = le32_to_cpu(key[1]);
62 56  
... ... @@ -125,16 +119,10 @@
125 119 }
126 120  
127 121 static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
128   - unsigned int key_len, u32 *flags)
  122 + unsigned int key_len)
129 123 {
130 124 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
131 125 const __le32 *key = (const __le32 *)in_key;
132   -
133   - if (key_len != 16)
134   - {
135   - *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
136   - return -EINVAL;
137   - }
138 126  
139 127 ctx->KEY[0] = le32_to_cpu(key[0]);
140 128 ctx->KEY[1] = le32_to_cpu(key[1]);
crypto/twofish_common.c
... ... @@ -580,11 +580,11 @@
580 580 ctx->a[(j) + 1] = rol32(y, 9)
581 581  
582 582 /* Perform the key setup. */
583   -int twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
584   - unsigned int key_len, u32 *flags)
  583 +int twofish_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len)
585 584 {
586 585  
587 586 struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
  587 + u32 *flags = &tfm->crt_flags;
588 588  
589 589 int i, j, k;
590 590  
... ... @@ -600,7 +600,7 @@
600 600 u8 tmp;
601 601  
602 602 /* Check key length. */
603   - if (key_len != 16 && key_len != 24 && key_len != 32)
  603 + if (key_len % 8)
604 604 {
605 605 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
606 606 return -EINVAL; /* unsupported key length */
drivers/crypto/padlock-aes.c
... ... @@ -308,15 +308,16 @@
308 308 }
309 309  
310 310 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
311   - unsigned int key_len, u32 *flags)
  311 + unsigned int key_len)
312 312 {
313 313 struct aes_ctx *ctx = aes_ctx(tfm);
314 314 const __le32 *key = (const __le32 *)in_key;
  315 + u32 *flags = &tfm->crt_flags;
315 316 uint32_t i, t, u, v, w;
316 317 uint32_t P[AES_EXTENDED_KEY_SIZE];
317 318 uint32_t rounds;
318 319  
319   - if (key_len != 16 && key_len != 24 && key_len != 32) {
  320 + if (key_len % 8) {
320 321 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
321 322 return -EINVAL;
322 323 }
include/crypto/twofish.h
... ... @@ -17,8 +17,7 @@
17 17 u32 s[4][256], w[8], k[32];
18 18 };
19 19  
20   -int twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
21   - unsigned int key_len, u32 *flags);
  20 +int twofish_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len);
22 21  
23 22 #endif
include/linux/crypto.h
... ... @@ -106,7 +106,7 @@
106 106 unsigned int cia_min_keysize;
107 107 unsigned int cia_max_keysize;
108 108 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
109   - unsigned int keylen, u32 *flags);
  109 + unsigned int keylen);
110 110 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
111 111 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
112 112  
... ... @@ -131,7 +131,7 @@
131 131 unsigned int len);
132 132 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
133 133 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
134   - unsigned int keylen, u32 *flags);
  134 + unsigned int keylen);
135 135 };
136 136  
137 137 struct compress_alg {
... ... @@ -397,8 +397,6 @@
397 397 const u8 *key, unsigned int keylen)
398 398 {
399 399 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
400   - if (tfm->crt_digest.dit_setkey == NULL)
401   - return -ENOSYS;
402 400 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
403 401 }
404 402