Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cc1d24b9 authored by Ard Biesheuvel's avatar Ard Biesheuvel Committed by Herbert Xu
Browse files

crypto: x86/des - switch to library interface

parent 04007b0e
Loading
Loading
Loading
Loading
+23 −19
Original line number Original line Diff line number Diff line
@@ -11,7 +11,7 @@
 */
 */


#include <crypto/algapi.h>
#include <crypto/algapi.h>
#include <crypto/internal/des.h>
#include <crypto/des.h>
#include <crypto/internal/skcipher.h>
#include <crypto/internal/skcipher.h>
#include <linux/crypto.h>
#include <linux/crypto.h>
#include <linux/init.h>
#include <linux/init.h>
@@ -19,8 +19,8 @@
#include <linux/types.h>
#include <linux/types.h>


struct des3_ede_x86_ctx {
struct des3_ede_x86_ctx {
	u32 enc_expkey[DES3_EDE_EXPKEY_WORDS];
	struct des3_ede_ctx enc;
	u32 dec_expkey[DES3_EDE_EXPKEY_WORDS];
	struct des3_ede_ctx dec;
};
};


/* regular block cipher functions */
/* regular block cipher functions */
@@ -34,7 +34,7 @@ asmlinkage void des3_ede_x86_64_crypt_blk_3way(const u32 *expkey, u8 *dst,
static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
				    const u8 *src)
				    const u8 *src)
{
{
	u32 *enc_ctx = ctx->enc_expkey;
	u32 *enc_ctx = ctx->enc.expkey;


	des3_ede_x86_64_crypt_blk(enc_ctx, dst, src);
	des3_ede_x86_64_crypt_blk(enc_ctx, dst, src);
}
}
@@ -42,7 +42,7 @@ static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
				    const u8 *src)
				    const u8 *src)
{
{
	u32 *dec_ctx = ctx->dec_expkey;
	u32 *dec_ctx = ctx->dec.expkey;


	des3_ede_x86_64_crypt_blk(dec_ctx, dst, src);
	des3_ede_x86_64_crypt_blk(dec_ctx, dst, src);
}
}
@@ -50,7 +50,7 @@ static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
					 const u8 *src)
					 const u8 *src)
{
{
	u32 *enc_ctx = ctx->enc_expkey;
	u32 *enc_ctx = ctx->enc.expkey;


	des3_ede_x86_64_crypt_blk_3way(enc_ctx, dst, src);
	des3_ede_x86_64_crypt_blk_3way(enc_ctx, dst, src);
}
}
@@ -58,7 +58,7 @@ static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
					 const u8 *src)
					 const u8 *src)
{
{
	u32 *dec_ctx = ctx->dec_expkey;
	u32 *dec_ctx = ctx->dec.expkey;


	des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src);
	des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src);
}
}
@@ -122,7 +122,7 @@ static int ecb_encrypt(struct skcipher_request *req)
	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
	struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
	struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);


	return ecb_crypt(req, ctx->enc_expkey);
	return ecb_crypt(req, ctx->enc.expkey);
}
}


static int ecb_decrypt(struct skcipher_request *req)
static int ecb_decrypt(struct skcipher_request *req)
@@ -130,7 +130,7 @@ static int ecb_decrypt(struct skcipher_request *req)
	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
	struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
	struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);


	return ecb_crypt(req, ctx->dec_expkey);
	return ecb_crypt(req, ctx->dec.expkey);
}
}


static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx,
static 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,
	u32 i, j, tmp;
	u32 i, j, tmp;
	int err;
	int err;


	err = crypto_des3_ede_verify_key(tfm, key);
	err = des3_ede_expand_key(&ctx->enc, key, keylen);
	if (err)
	if (err == -ENOKEY) {
		return err;
		if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
			err = -EINVAL;
		else
			err = 0;
	}


	/* Generate encryption context using generic implementation. */
	if (err) {
	err = __des3_ede_setkey(ctx->enc_expkey, &tfm->crt_flags, key, keylen);
		memset(ctx, 0, sizeof(*ctx));
	if (err < 0)
		return err;
		return err;
	}


	/* Fix encryption context for this implementation and form decryption
	/* Fix encryption context for this implementation and form decryption
	 * context. */
	 * context. */
	j = DES3_EDE_EXPKEY_WORDS - 2;
	j = DES3_EDE_EXPKEY_WORDS - 2;
	for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) {
	for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) {
		tmp = ror32(ctx->enc_expkey[i + 1], 4);
		tmp = ror32(ctx->enc.expkey[i + 1], 4);
		ctx->enc_expkey[i + 1] = tmp;
		ctx->enc.expkey[i + 1] = tmp;


		ctx->dec_expkey[j + 0] = ctx->enc_expkey[i + 0];
		ctx->dec.expkey[j + 0] = ctx->enc.expkey[i + 0];
		ctx->dec_expkey[j + 1] = tmp;
		ctx->dec.expkey[j + 1] = tmp;
	}
	}


	return 0;
	return 0;