1111 */
1212
1313#include <crypto/algapi.h>
14- #include <crypto/internal/ des.h>
14+ #include <crypto/des.h>
1515#include <crypto/internal/skcipher.h>
1616#include <linux/crypto.h>
1717#include <linux/init.h>
1818#include <linux/module.h>
1919#include <linux/types.h>
2020
2121struct des3_ede_x86_ctx {
22- u32 enc_expkey [ DES3_EDE_EXPKEY_WORDS ] ;
23- u32 dec_expkey [ DES3_EDE_EXPKEY_WORDS ] ;
22+ struct des3_ede_ctx enc ;
23+ struct des3_ede_ctx dec ;
2424};
2525
2626/* regular block cipher functions */
@@ -34,31 +34,31 @@ asmlinkage void des3_ede_x86_64_crypt_blk_3way(const u32 *expkey, u8 *dst,
3434static inline void des3_ede_enc_blk (struct des3_ede_x86_ctx * ctx , u8 * dst ,
3535 const u8 * src )
3636{
37- u32 * enc_ctx = ctx -> enc_expkey ;
37+ u32 * enc_ctx = ctx -> enc . expkey ;
3838
3939 des3_ede_x86_64_crypt_blk (enc_ctx , dst , src );
4040}
4141
4242static inline void des3_ede_dec_blk (struct des3_ede_x86_ctx * ctx , u8 * dst ,
4343 const u8 * src )
4444{
45- u32 * dec_ctx = ctx -> dec_expkey ;
45+ u32 * dec_ctx = ctx -> dec . expkey ;
4646
4747 des3_ede_x86_64_crypt_blk (dec_ctx , dst , src );
4848}
4949
5050static inline void des3_ede_enc_blk_3way (struct des3_ede_x86_ctx * ctx , u8 * dst ,
5151 const u8 * src )
5252{
53- u32 * enc_ctx = ctx -> enc_expkey ;
53+ u32 * enc_ctx = ctx -> enc . expkey ;
5454
5555 des3_ede_x86_64_crypt_blk_3way (enc_ctx , dst , src );
5656}
5757
5858static inline void des3_ede_dec_blk_3way (struct des3_ede_x86_ctx * ctx , u8 * dst ,
5959 const u8 * src )
6060{
61- u32 * dec_ctx = ctx -> dec_expkey ;
61+ u32 * dec_ctx = ctx -> dec . expkey ;
6262
6363 des3_ede_x86_64_crypt_blk_3way (dec_ctx , dst , src );
6464}
@@ -122,15 +122,15 @@ static int ecb_encrypt(struct skcipher_request *req)
122122 struct crypto_skcipher * tfm = crypto_skcipher_reqtfm (req );
123123 struct des3_ede_x86_ctx * ctx = crypto_skcipher_ctx (tfm );
124124
125- return ecb_crypt (req , ctx -> enc_expkey );
125+ return ecb_crypt (req , ctx -> enc . expkey );
126126}
127127
128128static int ecb_decrypt (struct skcipher_request * req )
129129{
130130 struct crypto_skcipher * tfm = crypto_skcipher_reqtfm (req );
131131 struct des3_ede_x86_ctx * ctx = crypto_skcipher_ctx (tfm );
132132
133- return ecb_crypt (req , ctx -> dec_expkey );
133+ return ecb_crypt (req , ctx -> dec . expkey );
134134}
135135
136136static unsigned int __cbc_encrypt (struct des3_ede_x86_ctx * ctx ,
@@ -348,24 +348,28 @@ static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key,
348348 u32 i , j , tmp ;
349349 int err ;
350350
351- err = crypto_des3_ede_verify_key (tfm , key );
352- if (err )
353- return err ;
351+ err = des3_ede_expand_key (& ctx -> enc , key , keylen );
352+ if (err == - ENOKEY ) {
353+ if (crypto_tfm_get_flags (tfm ) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS )
354+ err = - EINVAL ;
355+ else
356+ err = 0 ;
357+ }
354358
355- /* Generate encryption context using generic implementation. */
356- err = __des3_ede_setkey (ctx -> enc_expkey , & tfm -> crt_flags , key , keylen );
357- if (err < 0 )
359+ if (err ) {
360+ memset (ctx , 0 , sizeof (* ctx ));
358361 return err ;
362+ }
359363
360364 /* Fix encryption context for this implementation and form decryption
361365 * context. */
362366 j = DES3_EDE_EXPKEY_WORDS - 2 ;
363367 for (i = 0 ; i < DES3_EDE_EXPKEY_WORDS ; i += 2 , j -= 2 ) {
364- tmp = ror32 (ctx -> enc_expkey [i + 1 ], 4 );
365- ctx -> enc_expkey [i + 1 ] = tmp ;
368+ tmp = ror32 (ctx -> enc . expkey [i + 1 ], 4 );
369+ ctx -> enc . expkey [i + 1 ] = tmp ;
366370
367- ctx -> dec_expkey [j + 0 ] = ctx -> enc_expkey [i + 0 ];
368- ctx -> dec_expkey [j + 1 ] = tmp ;
371+ ctx -> dec . expkey [j + 0 ] = ctx -> enc . expkey [i + 0 ];
372+ ctx -> dec . expkey [j + 1 ] = tmp ;
369373 }
370374
371375 return 0 ;
0 commit comments