forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/arch/x86/crypto/des3_ede_glue.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Glue Code for assembler optimized version of 3DES
34 *
....@@ -7,17 +8,6 @@
78 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
89 * CTR part based on code (crypto/ctr.c) by:
910 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
10
- *
11
- * This program is free software; you can redistribute it and/or modify
12
- * it under the terms of the GNU General Public License as published by
13
- * the Free Software Foundation; either version 2 of the License, or
14
- * (at your option) any later version.
15
- *
16
- * This program is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU General Public License for more details.
20
- *
2111 */
2212
2313 #include <crypto/algapi.h>
....@@ -29,8 +19,8 @@
2919 #include <linux/types.h>
3020
3121 struct des3_ede_x86_ctx {
32
- u32 enc_expkey[DES3_EDE_EXPKEY_WORDS];
33
- u32 dec_expkey[DES3_EDE_EXPKEY_WORDS];
22
+ struct des3_ede_ctx enc;
23
+ struct des3_ede_ctx dec;
3424 };
3525
3626 /* regular block cipher functions */
....@@ -44,7 +34,7 @@
4434 static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
4535 const u8 *src)
4636 {
47
- u32 *enc_ctx = ctx->enc_expkey;
37
+ u32 *enc_ctx = ctx->enc.expkey;
4838
4939 des3_ede_x86_64_crypt_blk(enc_ctx, dst, src);
5040 }
....@@ -52,7 +42,7 @@
5242 static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
5343 const u8 *src)
5444 {
55
- u32 *dec_ctx = ctx->dec_expkey;
45
+ u32 *dec_ctx = ctx->dec.expkey;
5646
5747 des3_ede_x86_64_crypt_blk(dec_ctx, dst, src);
5848 }
....@@ -60,7 +50,7 @@
6050 static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
6151 const u8 *src)
6252 {
63
- u32 *enc_ctx = ctx->enc_expkey;
53
+ u32 *enc_ctx = ctx->enc.expkey;
6454
6555 des3_ede_x86_64_crypt_blk_3way(enc_ctx, dst, src);
6656 }
....@@ -68,7 +58,7 @@
6858 static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
6959 const u8 *src)
7060 {
71
- u32 *dec_ctx = ctx->dec_expkey;
61
+ u32 *dec_ctx = ctx->dec.expkey;
7262
7363 des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src);
7464 }
....@@ -132,7 +122,7 @@
132122 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
133123 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
134124
135
- return ecb_crypt(req, ctx->enc_expkey);
125
+ return ecb_crypt(req, ctx->enc.expkey);
136126 }
137127
138128 static int ecb_decrypt(struct skcipher_request *req)
....@@ -140,7 +130,7 @@
140130 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
141131 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
142132
143
- return ecb_crypt(req, ctx->dec_expkey);
133
+ return ecb_crypt(req, ctx->dec.expkey);
144134 }
145135
146136 static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx,
....@@ -358,20 +348,28 @@
358348 u32 i, j, tmp;
359349 int err;
360350
361
- /* Generate encryption context using generic implementation. */
362
- err = __des3_ede_setkey(ctx->enc_expkey, &tfm->crt_flags, key, keylen);
363
- if (err < 0)
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
+ }
358
+
359
+ if (err) {
360
+ memset(ctx, 0, sizeof(*ctx));
364361 return err;
362
+ }
365363
366364 /* Fix encryption context for this implementation and form decryption
367365 * context. */
368366 j = DES3_EDE_EXPKEY_WORDS - 2;
369367 for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) {
370
- tmp = ror32(ctx->enc_expkey[i + 1], 4);
371
- ctx->enc_expkey[i + 1] = tmp;
368
+ tmp = ror32(ctx->enc.expkey[i + 1], 4);
369
+ ctx->enc.expkey[i + 1] = tmp;
372370
373
- ctx->dec_expkey[j + 0] = ctx->enc_expkey[i + 0];
374
- ctx->dec_expkey[j + 1] = tmp;
371
+ ctx->dec.expkey[j + 0] = ctx->enc.expkey[i + 0];
372
+ ctx->dec.expkey[j + 1] = tmp;
375373 }
376374
377375 return 0;