Commit a5c660e6 authored by James Morris's avatar James Morris

[CRYPTO]: Add in 3des implementation.

parent c7f36a27
......@@ -11,7 +11,7 @@ CONFIG_CRYPTO_SHA1
SHA-1 secure hash standard (FIPS 180-1), including HMAC (RFC2104, RFC2404).
CONFIG_CRYPTO_DES
DES cipher algorithm (FIPS 46-2), and 3DES_EDE.
DES cipher algorithm (FIPS 46-2), and Triple DES EDE (FIPS 46-3).
CONFIG_CRYPTO_TEST
Quick & dirty crypto test module.
......
......@@ -8,7 +8,7 @@ bool 'Cryptographic API' CONFIG_CRYPTO
if [ "$CONFIG_CRYPTO" = "y" ]; then
tristate ' MD5 digest algorithm' CONFIG_CRYPTO_MD5
tristate ' SHA-1 digest algorithm' CONFIG_CRYPTO_SHA1
tristate ' DES/3DES cipher algorithms' CONFIG_CRYPTO_DES
tristate ' DES and Triple DES EDE cipher algorithms' CONFIG_CRYPTO_DES
tristate ' Testing module' CONFIG_CRYPTO_TEST
fi
......
This diff is collapsed.
......@@ -42,7 +42,6 @@
#define IDX7 27333
#define IDX8 3000
static int mode = 0;
static char *xbuf;
static char *tvmem;
......@@ -1094,6 +1093,115 @@ void test_des(void)
return;
}
void test_des3_ede(void)
{
int ret, i, len;
size_t tsize;
char *p, *q;
struct crypto_tfm *tfm;
char *key;
/*char res[8];*/
struct des_tv *des_tv;
struct scatterlist sg[8];
printk("\ntesting des3 ede encryption\n");
tsize = sizeof(des3_ede_enc_tv_template);
if (tsize > TVMEMSIZE) {
printk("template (%Zd) too big for tvmem (%d)\n", tsize, TVMEMSIZE);
return;
}
memcpy(tvmem, des3_ede_enc_tv_template, tsize);
des_tv = (void *)tvmem;
tfm = crypto_alloc_tfm(CRYPTO_ALG_DES3_EDE_ECB);
if (tfm == NULL) {
printk("failed to load transform for CRYPTO_ALG_DES3_EDE_ECB\n");
return;
}
for (i = 0; i < DES3_EDE_ENC_TEST_VECTORS; i++) {
printk("test %d:\n", i + 1);
key = des_tv[i].key;
tfm->crt_flags = CRYPTO_WEAK_KEY_CHECK;
ret = crypto_cipher_setkey(tfm, key, 24);
if (ret) {
printk("setkey() failed flags=%x\n", tfm->crt_flags);
if (!des_tv[i].fail)
goto out;
}
len = des_tv[i].len;
p = des_tv[i].plaintext;
sg[0].page = virt_to_page(p);
sg[0].offset = ((long)p & ~PAGE_MASK);
sg[0].length = len;
ret = crypto_cipher_encrypt(tfm, sg, 1);
if (ret) {
printk("encrypt() failed flags=%x\n", tfm->crt_flags);
goto out;
}
q = kmap(sg[0].page) + sg[0].offset;
hexdump(q, len);
printk("%s\n", memcmp(q, des_tv[i].result, len) ? "fail" : "pass");
}
printk("\ntesting des3 ede decryption\n");
tsize = sizeof(des3_ede_dec_tv_template);
if (tsize > TVMEMSIZE) {
printk("template (%Zd) too big for tvmem (%d)\n", tsize, TVMEMSIZE);
return;
}
memcpy(tvmem, des3_ede_dec_tv_template, tsize);
des_tv = (void *)tvmem;
for (i = 0; i < DES3_EDE_DEC_TEST_VECTORS; i++) {
printk("test %d:\n", i + 1);
key = des_tv[i].key;
tfm->crt_flags = CRYPTO_WEAK_KEY_CHECK;
ret = crypto_cipher_setkey(tfm, key, 24);
if (ret) {
printk("setkey() failed flags=%x\n", tfm->crt_flags);
if (!des_tv[i].fail)
goto out;
}
len = des_tv[i].len;
p = des_tv[i].plaintext;
sg[0].page = virt_to_page(p);
sg[0].offset = ((long)p & ~PAGE_MASK);
sg[0].length = len;
ret = crypto_cipher_decrypt(tfm, sg, 1);
if (ret) {
printk("decrypt() failed flags=%x\n", tfm->crt_flags);
goto out;
}
q = kmap(sg[0].page) + sg[0].offset;
hexdump(q, len);
printk("%s\n", memcmp(q, des_tv[i].result, len) ? "fail" : "pass");
}
out:
crypto_free_tfm(tfm);
return;
}
static void do_test(void)
{
switch (mode) {
......@@ -1102,6 +1210,7 @@ static void do_test(void)
test_md5();
test_sha1();
test_des();
test_des3_ede();
break;
case 1:
......@@ -1116,6 +1225,10 @@ static void do_test(void)
test_des();
break;
case 4:
test_des3_ede();
break;
default:
/* useful for debugging */
printk("not testing anything\n");
......
......@@ -306,11 +306,13 @@ struct sha1_testvec {
#define DES_DEC_TEST_VECTORS 2
#define DES_CBC_ENC_TEST_VECTORS 4
#define DES_CBC_DEC_TEST_VECTORS 3
#define DES3_EDE_ENC_TEST_VECTORS 3
#define DES3_EDE_DEC_TEST_VECTORS 3
struct des_tv {
int len;
int fail;
char key[8];
char key[24];
char iv[8];
char plaintext[128];
char result[128];
......@@ -581,7 +583,104 @@ struct des_tv des_cbc_dec_tv_template[] = {
},
};
/*
* We really need some more test vectors, especially for DES3 CBC.
*/
struct des_tv des3_ede_enc_tv_template[] = {
/* These are from openssl */
{
8, 0,
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10},
{ 0 },
{ 0x73, 0x6F, 0x6D, 0x65, 0x64, 0x61, 0x74, 0x61 },
{ 0x18, 0xd7, 0x48, 0xe5, 0x63, 0x62, 0x05, 0x72 },
},
{
8, 0,
{ 0x03,0x52,0x02,0x07,0x67,0x20,0x82,0x17,
0x86,0x02,0x87,0x66,0x59,0x08,0x21,0x98,
0x64,0x05,0x6A,0xBD,0xFE,0xA9,0x34,0x57 },
{ 0 },
{ 0x73,0x71,0x75,0x69,0x67,0x67,0x6C,0x65 },
{ 0xc0,0x7d,0x2a,0x0f,0xa5,0x66,0xfa,0x30 }
},
{
8, 0,
{ 0x10,0x46,0x10,0x34,0x89,0x98,0x80,0x20,
0x91,0x07,0xD0,0x15,0x89,0x19,0x01,0x01,
0x19,0x07,0x92,0x10,0x98,0x1A,0x01,0x01 },
{ 0 },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
{ 0xe1,0xef,0x62,0xc3,0x32,0xfe,0x82,0x5b }
},
};
struct des_tv des3_ede_dec_tv_template[] = {
/* These are from openssl */
{
8, 0,
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10},
{ 0 },
{ 0x18, 0xd7, 0x48, 0xe5, 0x63, 0x62, 0x05, 0x72 },
{ 0x73, 0x6F, 0x6D, 0x65, 0x64, 0x61, 0x74, 0x61 },
},
{
8, 0,
{ 0x03,0x52,0x02,0x07,0x67,0x20,0x82,0x17,
0x86,0x02,0x87,0x66,0x59,0x08,0x21,0x98,
0x64,0x05,0x6A,0xBD,0xFE,0xA9,0x34,0x57 },
{ 0 },
{ 0xc0,0x7d,0x2a,0x0f,0xa5,0x66,0xfa,0x30 },
{ 0x73,0x71,0x75,0x69,0x67,0x67,0x6C,0x65 },
},
{
8, 0,
{ 0x10,0x46,0x10,0x34,0x89,0x98,0x80,0x20,
0x91,0x07,0xD0,0x15,0x89,0x19,0x01,0x01,
0x19,0x07,0x92,0x10,0x98,0x1A,0x01,0x01 },
{ 0 },
{ 0xe1,0xef,0x62,0xc3,0x32,0xfe,0x82,0x5b },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
},
};
#endif /* _CRYPTO_TCRYPT_H */
......@@ -22,7 +22,8 @@
#define CRYPTO_WEAK_KEY_CHECK 0x0001
#define CRYPTO_WEAK_KEY 0x0008
#define CRYPTO_BAD_KEY_LEN 0x0010
#define CRYPTO_BAD_BLOCK_LEN 0x0020
#define CRYPTO_BAD_KEY_SCHED 0x0020
#define CRYPTO_BAD_BLOCK_LEN 0x0040
#define CRYPTO_ATOMIC 0x1000
/*
......@@ -51,9 +52,9 @@
#define CRYPTO_ALG_DES_ECB (CRYPTO_ALG_DES|CRYPTO_MODE_ECB)
#define CRYPTO_ALG_DES_CBC (CRYPTO_ALG_DES|CRYPTO_MODE_CBC)
#define CRYPTO_ALG_3DES_EDE (0x00000002|CRYPTO_TYPE_CIPHER)
#define CRYPTO_ALG_3DES_EDE_ECB (CRYPTO_ALG_3DES_EDE|CRYPTO_MODE_ECB)
#define CRYPTO_ALG_3DES_EDE_CBC (CRYPTO_ALG_3DES_EDE|CRYPTO_MODE_CBC)
#define CRYPTO_ALG_DES3_EDE (0x00000002|CRYPTO_TYPE_CIPHER)
#define CRYPTO_ALG_DES3_EDE_ECB (CRYPTO_ALG_DES3_EDE|CRYPTO_MODE_ECB)
#define CRYPTO_ALG_DES3_EDE_CBC (CRYPTO_ALG_DES3_EDE|CRYPTO_MODE_CBC)
#define CRYPTO_ALG_MD5 (0x00000f00|CRYPTO_TYPE_DIGEST)
#define CRYPTO_ALG_SHA1 (0x00000f01|CRYPTO_TYPE_DIGEST)
......@@ -61,10 +62,6 @@
#define CRYPTO_MAX_ALG_NAME 64
#define CRYPTO_MAX_BLOCK_SIZE 16
#define cra_cipher cra_u.cipher
#define cra_digest cra_u.digest
#define cra_compress cra_u.compress
struct scatterlist;
struct cipher_alg {
......
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