Commit 04007b0e authored by Ard Biesheuvel's avatar Ard Biesheuvel Committed by Herbert Xu

crypto: des - split off DES library from generic DES cipher driver

Another one for the cipher museum: split off DES core processing into
a separate module so other drivers (mostly for crypto accelerators)
can reuse the code without pulling in the generic DES cipher itself.
This will also permit the cipher interface to be made private to the
crypto API itself once we move the only user in the kernel (CIFS) to
this library interface.
Signed-off-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 6b5c4818
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
*/ */
#include <crypto/algapi.h> #include <crypto/algapi.h>
#include <crypto/des.h> #include <crypto/internal/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>
......
...@@ -1306,9 +1306,13 @@ config CRYPTO_CAST6_AVX_X86_64 ...@@ -1306,9 +1306,13 @@ config CRYPTO_CAST6_AVX_X86_64
This module provides the Cast6 cipher algorithm that processes This module provides the Cast6 cipher algorithm that processes
eight blocks parallel using the AVX instruction set. eight blocks parallel using the AVX instruction set.
config CRYPTO_LIB_DES
tristate
config CRYPTO_DES config CRYPTO_DES
tristate "DES and Triple DES EDE cipher algorithms" tristate "DES and Triple DES EDE cipher algorithms"
select CRYPTO_ALGAPI select CRYPTO_ALGAPI
select CRYPTO_LIB_DES
help help
DES cipher algorithm (FIPS 46-2), and Triple DES EDE (FIPS 46-3). DES cipher algorithm (FIPS 46-2), and Triple DES EDE (FIPS 46-3).
...@@ -1316,7 +1320,7 @@ config CRYPTO_DES_SPARC64 ...@@ -1316,7 +1320,7 @@ config CRYPTO_DES_SPARC64
tristate "DES and Triple DES EDE cipher algorithms (SPARC64)" tristate "DES and Triple DES EDE cipher algorithms (SPARC64)"
depends on SPARC64 depends on SPARC64
select CRYPTO_ALGAPI select CRYPTO_ALGAPI
select CRYPTO_DES select CRYPTO_LIB_DES
help help
DES cipher algorithm (FIPS 46-2), and Triple DES EDE (FIPS 46-3), DES cipher algorithm (FIPS 46-2), and Triple DES EDE (FIPS 46-3),
optimized using SPARC64 crypto opcodes. optimized using SPARC64 crypto opcodes.
...@@ -1325,7 +1329,7 @@ config CRYPTO_DES3_EDE_X86_64 ...@@ -1325,7 +1329,7 @@ config CRYPTO_DES3_EDE_X86_64
tristate "Triple DES EDE cipher algorithm (x86-64)" tristate "Triple DES EDE cipher algorithm (x86-64)"
depends on X86 && 64BIT depends on X86 && 64BIT
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_DES select CRYPTO_LIB_DES
help help
Triple DES EDE (FIPS 46-3) algorithm. Triple DES EDE (FIPS 46-3) algorithm.
......
This diff is collapsed.
...@@ -150,7 +150,7 @@ config CRYPTO_DES_S390 ...@@ -150,7 +150,7 @@ config CRYPTO_DES_S390
depends on S390 depends on S390
select CRYPTO_ALGAPI select CRYPTO_ALGAPI
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_DES select CRYPTO_LIB_DES
help help
This is the s390 hardware accelerated implementation of the This is the s390 hardware accelerated implementation of the
DES cipher algorithm (FIPS 46-2), and Triple DES EDE (FIPS 46-3). DES cipher algorithm (FIPS 46-2), and Triple DES EDE (FIPS 46-3).
...@@ -215,7 +215,7 @@ config CRYPTO_DEV_MARVELL_CESA ...@@ -215,7 +215,7 @@ config CRYPTO_DEV_MARVELL_CESA
tristate "Marvell's Cryptographic Engine driver" tristate "Marvell's Cryptographic Engine driver"
depends on PLAT_ORION || ARCH_MVEBU depends on PLAT_ORION || ARCH_MVEBU
select CRYPTO_LIB_AES select CRYPTO_LIB_AES
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_HASH select CRYPTO_HASH
select SRAM select SRAM
...@@ -227,7 +227,7 @@ config CRYPTO_DEV_MARVELL_CESA ...@@ -227,7 +227,7 @@ config CRYPTO_DEV_MARVELL_CESA
config CRYPTO_DEV_NIAGARA2 config CRYPTO_DEV_NIAGARA2
tristate "Niagara2 Stream Processing Unit driver" tristate "Niagara2 Stream Processing Unit driver"
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_HASH select CRYPTO_HASH
select CRYPTO_MD5 select CRYPTO_MD5
...@@ -244,7 +244,7 @@ config CRYPTO_DEV_NIAGARA2 ...@@ -244,7 +244,7 @@ config CRYPTO_DEV_NIAGARA2
config CRYPTO_DEV_HIFN_795X config CRYPTO_DEV_HIFN_795X
tristate "Driver HIFN 795x crypto accelerator chips" tristate "Driver HIFN 795x crypto accelerator chips"
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select HW_RANDOM if CRYPTO_DEV_HIFN_795X_RNG select HW_RANDOM if CRYPTO_DEV_HIFN_795X_RNG
depends on PCI depends on PCI
...@@ -300,7 +300,7 @@ config CRYPTO_DEV_TALITOS2 ...@@ -300,7 +300,7 @@ config CRYPTO_DEV_TALITOS2
config CRYPTO_DEV_IXP4XX config CRYPTO_DEV_IXP4XX
tristate "Driver for IXP4xx crypto hardware acceleration" tristate "Driver for IXP4xx crypto hardware acceleration"
depends on ARCH_IXP4XX && IXP4XX_QMGR && IXP4XX_NPE depends on ARCH_IXP4XX && IXP4XX_QMGR && IXP4XX_NPE
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_AEAD select CRYPTO_AEAD
select CRYPTO_AUTHENC select CRYPTO_AUTHENC
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
...@@ -366,7 +366,7 @@ config CRYPTO_DEV_OMAP_AES ...@@ -366,7 +366,7 @@ config CRYPTO_DEV_OMAP_AES
config CRYPTO_DEV_OMAP_DES config CRYPTO_DEV_OMAP_DES
tristate "Support for OMAP DES/3DES hw engine" tristate "Support for OMAP DES/3DES hw engine"
depends on ARCH_OMAP2PLUS depends on ARCH_OMAP2PLUS
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_ENGINE select CRYPTO_ENGINE
help help
...@@ -384,7 +384,7 @@ config CRYPTO_DEV_PICOXCELL ...@@ -384,7 +384,7 @@ config CRYPTO_DEV_PICOXCELL
select CRYPTO_AES select CRYPTO_AES
select CRYPTO_AUTHENC select CRYPTO_AUTHENC
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_CBC select CRYPTO_CBC
select CRYPTO_ECB select CRYPTO_ECB
select CRYPTO_SEQIV select CRYPTO_SEQIV
...@@ -497,7 +497,7 @@ config CRYPTO_DEV_ATMEL_AES ...@@ -497,7 +497,7 @@ config CRYPTO_DEV_ATMEL_AES
config CRYPTO_DEV_ATMEL_TDES config CRYPTO_DEV_ATMEL_TDES
tristate "Support for Atmel DES/TDES hw accelerator" tristate "Support for Atmel DES/TDES hw accelerator"
depends on ARCH_AT91 || COMPILE_TEST depends on ARCH_AT91 || COMPILE_TEST
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
help help
Some Atmel processors have DES/TDES hw accelerator. Some Atmel processors have DES/TDES hw accelerator.
...@@ -595,7 +595,7 @@ config CRYPTO_DEV_QCE ...@@ -595,7 +595,7 @@ config CRYPTO_DEV_QCE
depends on ARCH_QCOM || COMPILE_TEST depends on ARCH_QCOM || COMPILE_TEST
depends on HAS_IOMEM depends on HAS_IOMEM
select CRYPTO_AES select CRYPTO_AES
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_ECB select CRYPTO_ECB
select CRYPTO_CBC select CRYPTO_CBC
select CRYPTO_XTS select CRYPTO_XTS
...@@ -643,7 +643,7 @@ config CRYPTO_DEV_SUN4I_SS ...@@ -643,7 +643,7 @@ config CRYPTO_DEV_SUN4I_SS
select CRYPTO_MD5 select CRYPTO_MD5
select CRYPTO_SHA1 select CRYPTO_SHA1
select CRYPTO_AES select CRYPTO_AES
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
help help
Some Allwinner SoC have a crypto accelerator named Some Allwinner SoC have a crypto accelerator named
...@@ -666,7 +666,7 @@ config CRYPTO_DEV_ROCKCHIP ...@@ -666,7 +666,7 @@ config CRYPTO_DEV_ROCKCHIP
tristate "Rockchip's Cryptographic Engine driver" tristate "Rockchip's Cryptographic Engine driver"
depends on OF && ARCH_ROCKCHIP depends on OF && ARCH_ROCKCHIP
select CRYPTO_AES select CRYPTO_AES
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_MD5 select CRYPTO_MD5
select CRYPTO_SHA1 select CRYPTO_SHA1
select CRYPTO_SHA256 select CRYPTO_SHA256
...@@ -703,7 +703,7 @@ config CRYPTO_DEV_BCM_SPU ...@@ -703,7 +703,7 @@ config CRYPTO_DEV_BCM_SPU
depends on MAILBOX depends on MAILBOX
default m default m
select CRYPTO_AUTHENC select CRYPTO_AUTHENC
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_MD5 select CRYPTO_MD5
select CRYPTO_SHA1 select CRYPTO_SHA1
select CRYPTO_SHA256 select CRYPTO_SHA256
...@@ -722,7 +722,7 @@ config CRYPTO_DEV_SAFEXCEL ...@@ -722,7 +722,7 @@ config CRYPTO_DEV_SAFEXCEL
select CRYPTO_LIB_AES select CRYPTO_LIB_AES
select CRYPTO_AUTHENC select CRYPTO_AUTHENC
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_HASH select CRYPTO_HASH
select CRYPTO_HMAC select CRYPTO_HMAC
select CRYPTO_MD5 select CRYPTO_MD5
...@@ -760,7 +760,7 @@ config CRYPTO_DEV_CCREE ...@@ -760,7 +760,7 @@ config CRYPTO_DEV_CCREE
default n default n
select CRYPTO_HASH select CRYPTO_HASH
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_DES select CRYPTO_LIB_DES
select CRYPTO_AEAD select CRYPTO_AEAD
select CRYPTO_AUTHENC select CRYPTO_AUTHENC
select CRYPTO_SHA1 select CRYPTO_SHA1
......
...@@ -98,7 +98,7 @@ config CRYPTO_DEV_FSL_CAAM_CRYPTO_API ...@@ -98,7 +98,7 @@ config CRYPTO_DEV_FSL_CAAM_CRYPTO_API
select CRYPTO_AEAD select CRYPTO_AEAD
select CRYPTO_AUTHENC select CRYPTO_AUTHENC
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_DES select CRYPTO_LIB_DES
help help
Selecting this will offload crypto for users of the Selecting this will offload crypto for users of the
scatterlist crypto API (such as the linux native IPSec scatterlist crypto API (such as the linux native IPSec
......
...@@ -6,7 +6,7 @@ config CRYPTO_DEV_NITROX ...@@ -6,7 +6,7 @@ config CRYPTO_DEV_NITROX
tristate tristate
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_AES select CRYPTO_AES
select CRYPTO_DES select CRYPTO_LIB_DES
select FW_LOADER select FW_LOADER
config CRYPTO_DEV_NITROX_CNN55XX config CRYPTO_DEV_NITROX_CNN55XX
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#include <crypto/aes.h> #include <crypto/aes.h>
#include <crypto/authenc.h> #include <crypto/authenc.h>
#include <crypto/ctr.h> #include <crypto/ctr.h>
#include <crypto/des.h> #include <crypto/internal/des.h>
#include <crypto/sha.h> #include <crypto/sha.h>
#include <crypto/skcipher.h> #include <crypto/skcipher.h>
#include <crypto/internal/aead.h> #include <crypto/internal/aead.h>
......
...@@ -25,7 +25,7 @@ config CRYPTO_DEV_STM32_CRYP ...@@ -25,7 +25,7 @@ config CRYPTO_DEV_STM32_CRYP
depends on ARCH_STM32 depends on ARCH_STM32
select CRYPTO_HASH select CRYPTO_HASH
select CRYPTO_ENGINE select CRYPTO_ENGINE
select CRYPTO_DES select CRYPTO_LIB_DES
help help
This enables support for the CRYP (AES/DES/TDES) hw accelerator which This enables support for the CRYP (AES/DES/TDES) hw accelerator which
can be found on STMicroelectronics STM32 SOC. can be found on STMicroelectronics STM32 SOC.
...@@ -9,7 +9,7 @@ config CRYPTO_DEV_UX500_CRYP ...@@ -9,7 +9,7 @@ config CRYPTO_DEV_UX500_CRYP
depends on CRYPTO_DEV_UX500 depends on CRYPTO_DEV_UX500
select CRYPTO_ALGAPI select CRYPTO_ALGAPI
select CRYPTO_BLKCIPHER select CRYPTO_BLKCIPHER
select CRYPTO_DES select CRYPTO_LIB_DES
help help
This selects the crypto driver for the UX500_CRYP hardware. It supports This selects the crypto driver for the UX500_CRYP hardware. It supports
AES-ECB, CBC and CTR with keys sizes of 128, 192 and 256 bit sizes. AES-ECB, CBC and CTR with keys sizes of 128, 192 and 256 bit sizes.
......
...@@ -6,10 +6,7 @@ ...@@ -6,10 +6,7 @@
#ifndef __CRYPTO_DES_H #ifndef __CRYPTO_DES_H
#define __CRYPTO_DES_H #define __CRYPTO_DES_H
#include <crypto/skcipher.h> #include <linux/types.h>
#include <linux/compiler.h>
#include <linux/fips.h>
#include <linux/string.h>
#define DES_KEY_SIZE 8 #define DES_KEY_SIZE 8
#define DES_EXPKEY_WORDS 32 #define DES_EXPKEY_WORDS 32
...@@ -19,6 +16,44 @@ ...@@ -19,6 +16,44 @@
#define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS) #define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS)
#define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE #define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE
struct des_ctx {
u32 expkey[DES_EXPKEY_WORDS];
};
struct des3_ede_ctx {
u32 expkey[DES3_EDE_EXPKEY_WORDS];
};
void des_encrypt(const struct des_ctx *ctx, u8 *dst, const u8 *src);
void des_decrypt(const struct des_ctx *ctx, u8 *dst, const u8 *src);
void des3_ede_encrypt(const struct des3_ede_ctx *dctx, u8 *dst, const u8 *src);
void des3_ede_decrypt(const struct des3_ede_ctx *dctx, u8 *dst, const u8 *src);
/**
* des_expand_key - Expand a DES input key into a key schedule
* @ctx: the key schedule
* @key: buffer containing the input key
* @len: size of the buffer contents
*
* Returns 0 on success, -EINVAL if the input key is rejected and -ENOKEY if
* the key is accepted but has been found to be weak.
*/
int des_expand_key(struct des_ctx *ctx, const u8 *key, unsigned int keylen);
/**
* des3_ede_expand_key - Expand a triple DES input key into a key schedule
* @ctx: the key schedule
* @key: buffer containing the input key
* @len: size of the buffer contents
*
* Returns 0 on success, -EINVAL if the input key is rejected and -ENOKEY if
* the key is accepted but has been found to be weak. Note that weak keys will
* be rejected (and -EINVAL will be returned) when running in FIPS mode.
*/
int des3_ede_expand_key(struct des3_ede_ctx *ctx, const u8 *key,
unsigned int keylen);
extern int __des3_ede_setkey(u32 *expkey, u32 *flags, const u8 *key, extern int __des3_ede_setkey(u32 *expkey, u32 *flags, const u8 *key,
unsigned int keylen); unsigned int keylen);
......
...@@ -25,18 +25,21 @@ ...@@ -25,18 +25,21 @@
*/ */
static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key) static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key)
{ {
u32 tmp[DES_EXPKEY_WORDS]; struct des_ctx tmp;
int err = 0; int err;
if (!(crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) err = des_expand_key(&tmp, key, DES_KEY_SIZE);
return 0; if (err == -ENOKEY) {
if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
if (!des_ekey(tmp, key)) {
crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
err = -EINVAL; err = -EINVAL;
else
err = 0;
} }
memzero_explicit(tmp, sizeof(tmp)); if (err)
crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
memzero_explicit(&tmp, sizeof(tmp));
return err; return err;
} }
...@@ -53,6 +56,28 @@ static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key) ...@@ -53,6 +56,28 @@ static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key)
* property. * property.
* *
*/ */
static inline int des3_ede_verify_key(const u8 *key, unsigned int key_len,
bool check_weak)
{
int ret = fips_enabled ? -EINVAL : -ENOKEY;
u32 K[6];
memcpy(K, key, DES3_EDE_KEY_SIZE);
if ((!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
!((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
(fips_enabled || check_weak))
goto bad;
if ((!((K[0] ^ K[4]) | (K[1] ^ K[5]))) && fips_enabled)
goto bad;
ret = 0;
bad:
memzero_explicit(K, DES3_EDE_KEY_SIZE);
return ret;
}
/** /**
* crypto_des3_ede_verify_key - Check whether a DES3-EDE key is weak * crypto_des3_ede_verify_key - Check whether a DES3-EDE key is weak
...@@ -70,28 +95,14 @@ static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key) ...@@ -70,28 +95,14 @@ static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key)
static inline int crypto_des3_ede_verify_key(struct crypto_tfm *tfm, static inline int crypto_des3_ede_verify_key(struct crypto_tfm *tfm,
const u8 *key) const u8 *key)
{ {
int err = -EINVAL; int err;
u32 K[6];
memcpy(K, key, DES3_EDE_KEY_SIZE);
if ((!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
!((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
(fips_enabled || (crypto_tfm_get_flags(tfm) &
CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)))
goto bad;
if ((!((K[0] ^ K[4]) | (K[1] ^ K[5]))) && fips_enabled) err = des3_ede_verify_key(key, DES3_EDE_KEY_SIZE,
goto bad; crypto_tfm_get_flags(tfm) &
CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
err = 0; if (err)
out:
memzero_explicit(K, DES3_EDE_KEY_SIZE);
return err;
bad:
crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY); crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
goto out; return err;
} }
static inline int verify_skcipher_des_key(struct crypto_skcipher *tfm, static inline int verify_skcipher_des_key(struct crypto_skcipher *tfm,
......
...@@ -5,3 +5,6 @@ libaes-y := aes.o ...@@ -5,3 +5,6 @@ libaes-y := aes.o
obj-$(CONFIG_CRYPTO_LIB_ARC4) += libarc4.o obj-$(CONFIG_CRYPTO_LIB_ARC4) += libarc4.o
libarc4-y := arc4.o libarc4-y := arc4.o
obj-$(CONFIG_CRYPTO_LIB_DES) += libdes.o
libdes-y := des.o
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment