| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Glue Code for assembler optimized version of 3DES |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 7 | 8 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
|---|
| 8 | 9 | * CTR part based on code (crypto/ctr.c) by: |
|---|
| 9 | 10 | * (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 | | - * |
|---|
| 21 | 11 | */ |
|---|
| 22 | 12 | |
|---|
| 23 | 13 | #include <crypto/algapi.h> |
|---|
| .. | .. |
|---|
| 29 | 19 | #include <linux/types.h> |
|---|
| 30 | 20 | |
|---|
| 31 | 21 | 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; |
|---|
| 34 | 24 | }; |
|---|
| 35 | 25 | |
|---|
| 36 | 26 | /* regular block cipher functions */ |
|---|
| .. | .. |
|---|
| 44 | 34 | static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst, |
|---|
| 45 | 35 | const u8 *src) |
|---|
| 46 | 36 | { |
|---|
| 47 | | - u32 *enc_ctx = ctx->enc_expkey; |
|---|
| 37 | + u32 *enc_ctx = ctx->enc.expkey; |
|---|
| 48 | 38 | |
|---|
| 49 | 39 | des3_ede_x86_64_crypt_blk(enc_ctx, dst, src); |
|---|
| 50 | 40 | } |
|---|
| .. | .. |
|---|
| 52 | 42 | static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst, |
|---|
| 53 | 43 | const u8 *src) |
|---|
| 54 | 44 | { |
|---|
| 55 | | - u32 *dec_ctx = ctx->dec_expkey; |
|---|
| 45 | + u32 *dec_ctx = ctx->dec.expkey; |
|---|
| 56 | 46 | |
|---|
| 57 | 47 | des3_ede_x86_64_crypt_blk(dec_ctx, dst, src); |
|---|
| 58 | 48 | } |
|---|
| .. | .. |
|---|
| 60 | 50 | static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst, |
|---|
| 61 | 51 | const u8 *src) |
|---|
| 62 | 52 | { |
|---|
| 63 | | - u32 *enc_ctx = ctx->enc_expkey; |
|---|
| 53 | + u32 *enc_ctx = ctx->enc.expkey; |
|---|
| 64 | 54 | |
|---|
| 65 | 55 | des3_ede_x86_64_crypt_blk_3way(enc_ctx, dst, src); |
|---|
| 66 | 56 | } |
|---|
| .. | .. |
|---|
| 68 | 58 | static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst, |
|---|
| 69 | 59 | const u8 *src) |
|---|
| 70 | 60 | { |
|---|
| 71 | | - u32 *dec_ctx = ctx->dec_expkey; |
|---|
| 61 | + u32 *dec_ctx = ctx->dec.expkey; |
|---|
| 72 | 62 | |
|---|
| 73 | 63 | des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src); |
|---|
| 74 | 64 | } |
|---|
| .. | .. |
|---|
| 132 | 122 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| 133 | 123 | struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 134 | 124 | |
|---|
| 135 | | - return ecb_crypt(req, ctx->enc_expkey); |
|---|
| 125 | + return ecb_crypt(req, ctx->enc.expkey); |
|---|
| 136 | 126 | } |
|---|
| 137 | 127 | |
|---|
| 138 | 128 | static int ecb_decrypt(struct skcipher_request *req) |
|---|
| .. | .. |
|---|
| 140 | 130 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| 141 | 131 | struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 142 | 132 | |
|---|
| 143 | | - return ecb_crypt(req, ctx->dec_expkey); |
|---|
| 133 | + return ecb_crypt(req, ctx->dec.expkey); |
|---|
| 144 | 134 | } |
|---|
| 145 | 135 | |
|---|
| 146 | 136 | static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx, |
|---|
| .. | .. |
|---|
| 358 | 348 | u32 i, j, tmp; |
|---|
| 359 | 349 | int err; |
|---|
| 360 | 350 | |
|---|
| 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)); |
|---|
| 364 | 361 | return err; |
|---|
| 362 | + } |
|---|
| 365 | 363 | |
|---|
| 366 | 364 | /* Fix encryption context for this implementation and form decryption |
|---|
| 367 | 365 | * context. */ |
|---|
| 368 | 366 | j = DES3_EDE_EXPKEY_WORDS - 2; |
|---|
| 369 | 367 | 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; |
|---|
| 372 | 370 | |
|---|
| 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; |
|---|
| 375 | 373 | } |
|---|
| 376 | 374 | |
|---|
| 377 | 375 | return 0; |
|---|