Commit e4ca0bea authored by Davide Galassi's avatar Davide Galassi

DES tests started using OpenSSL vectors

parent 13da9bbc
Pipeline #36670211 passed with stages
in 1 minute and 21 seconds
......@@ -3,8 +3,8 @@
* @brief DES block cipher.
*/
#ifndef _CRY_DES_H_
#define _CRY_DES_H_
#ifndef CRY_DES_H_
#define CRY_DES_H_
#include <string.h>
......@@ -21,10 +21,8 @@ struct cry_des_ctx {
typedef struct cry_des_ctx cry_des_ctx;
#define cry_des_key_set(ctx, _key, _keylen) do { \
(ctx)->keylen = _keylen; \
memcpy((ctx)->key, _key, _keylen); \
} while(0)
void cry_des_key_set(cry_des_ctx *ctx, unsigned char *key,
unsigned int size);
void cry_des_encrypt(cry_des_ctx *ctx, unsigned char *dst,
const unsigned char *src, unsigned int size);
......@@ -32,4 +30,4 @@ void cry_des_encrypt(cry_des_ctx *ctx, unsigned char *dst,
void cry_des_decrypt(cry_des_ctx *ctx, unsigned char *dst,
const unsigned char *src, unsigned int size);
#endif /* _CRY_DES_H_ */
#endif /* CRY_DES_H_ */
......@@ -41,22 +41,19 @@ static const int pc1_tab[] = {
57, 49, 41, 33, 25, 17, 9, 1,
58, 50, 42, 34, 26, 18, 10, 2,
59, 51, 43, 35, 27, 19, 11, 3,
60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15, 7,
62, 54, 46, 38, 30, 22, 14, 6,
61, 53, 45, 37, 29, 21, 13, 5,
28, 20, 12, 4
60, 52, 44, 36, 63, 55, 47, 39,
31, 23, 15, 7, 62, 54, 46, 38,
30, 22, 14, 6, 61, 53, 45, 37,
29, 21, 13, 5, 28, 20, 12, 4
};
static const int pc2_tab[] = {
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
14, 17, 11, 24, 1, 5, 3, 28,
15, 6, 21, 10, 23, 19, 12, 4,
26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40,
51, 45, 33, 48, 44, 49, 39, 56,
34, 53, 46, 42, 50, 36, 29, 32
};
/* Block expansion permutation table */
......@@ -72,28 +69,24 @@ static const int exp_tab[] = {
};
static const int p_tab[] = {
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
16, 7, 20, 21, 29, 12, 28, 17,
1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25
};
static const int sbox[8][64] = {
{
14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
},
{
15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
},
{
10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
......@@ -328,3 +321,13 @@ void cry_des_decrypt(cry_des_ctx *ctx, unsigned char *dst,
size -= CRY_DES_BLOCK_SIZE;
}
}
void cry_des_key_set(cry_des_ctx *ctx, unsigned char *key,
unsigned int size)
{
memset(ctx, 0, sizeof(*ctx));
if (size > CRY_DES_BLOCK_SIZE*3)
size = CRY_DES_BLOCK_SIZE*3;
ctx->keylen = size;
memcpy(ctx->key, key, size);
}
......@@ -14,12 +14,11 @@ objs := \
base64_test.o \
mpi_test.o \
hmac_test.o \
des_test.o \
aes_test.o \
hill_test.o \
affine_test.o
# gcm_test.o \
des_test.o \
crc_test.o \
# crc_test.o \
md5_test.o \
sha256_test.o \
cmac_test.o \
......
#include "test.h"
#include <cry/des.h>
#include <cry/cbc.h>
#include <cry/ctr.h>
#include <cry/gcm.h>
#define KEY "12345678abcdefghILMNOPQR"
#define MSG "CRY is free soft"
#define LEN 16
#define CDES "\x04\xcf\xf8\x8a\xd8\x95\x79\x5c\x2e\x8d\xf1\xd9\xf5\xbd\x94\x54"
#define CTDES "\x93\x7c\x8d\xf0\xa1\xa4\x77\x61\x11\x4d\xb3\x17\x71\xdb\x10\x19"
static const cry_ciph_itf des_itf = {
.key_set = (cry_ciph_key_set_f) cry_des_key_set,
.encrypt = (cry_ciph_encrypt_f) cry_des_encrypt,
.decrypt = (cry_ciph_decrypt_f) cry_des_decrypt,
};
static cry_des_ctx des;
struct des_param {
unsigned int keylen;
unsigned int ivlen;
unsigned int aadlen;
unsigned int srclen;
unsigned int dstlen;
unsigned int maclen;
unsigned char key[32];
unsigned char iv[16];
unsigned char mac[16];
unsigned char aad[128];
unsigned char src[128];
unsigned char dst[128];
};
static void single(void)
static void param_init(struct des_param *par, int argc, char *argv[])
{
int i = 0;
memset(par, 0, sizeof(*par));
par->keylen = raw_init(par->key, sizeof(par->key), argv[i++]);
if (argc >= 4) {
/* CBC, CTR, GCM */
par->ivlen = raw_init(par->iv, sizeof(par->iv), argv[i++]);
if (argc == 6) {
/* GCM */
par->aadlen = raw_init(par->aad, sizeof(par->aad), argv[i++]);
par->maclen = raw_init(par->mac, sizeof(par->mac), argv[i++]);
}
}
par->srclen = raw_init(par->src, sizeof(par->src), argv[i++]);
par->dstlen = raw_init(par->dst, sizeof(par->dst), argv[i++]);
if (par->dstlen != -1)
ASSERT_EQ(par->srclen, par->dstlen);
}
static void des_ecb_encrypt(int argc, char *argv[])
{
cry_des_ctx ctx;
struct des_param par;
unsigned char dst[32];
ASSERT(argc == 3);
param_init(&par, argc, argv);
cry_des_key_set(&ctx, par.key, par.keylen);
cry_des_encrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
}
static void des_ecb_decrypt(int argc, char *argv[])
{
memcpy(buf, MSG, LEN); /* test in place */
cry_des_key_set(&des, KEY, 8);
cry_des_encrypt(&des, buf, buf, LEN);
PRINT_HEX("ciphertext", buf, LEN);
ASSERT_EQ_BUF(buf, CDES, LEN);
cry_des_decrypt(&des, buf, buf, LEN);
PRINT_ASC("cleartext ", buf, LEN);
ASSERT_EQ_BUF(buf, MSG, LEN);
cry_des_ctx ctx;
struct des_param par;
unsigned char dst[32];
ASSERT(argc == 3);
param_init(&par, argc, argv);
cry_des_key_set(&ctx, par.key, par.keylen);
cry_des_decrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
}
static void triple(void)
static void des_cbc_encrypt(int argc, char *argv[])
{
memcpy(buf, MSG, LEN); /* test in place */
cry_des_key_set(&des, KEY, 24);
cry_des_encrypt(&des, buf, buf, LEN);
PRINT_HEX("ciphertext", buf, LEN);
ASSERT_EQ_BUF(buf, CTDES, LEN);
cry_des_decrypt(&des, buf, buf, LEN);
PRINT_ASC("cleartext ", buf, LEN);
ASSERT_EQ_BUF(buf, MSG, LEN);
cry_cbc_ctx ctx;
cry_des_ctx des_ctx;
struct des_param par;
unsigned char dst[32];
ASSERT(argc == 4);
param_init(&par, argc, argv);
cry_cbc_init(&ctx, &des_ctx, &des_itf);
cry_cbc_key_set(&ctx, par.key, par.keylen);
cry_cbc_iv_set(&ctx, par.iv, 16);
cry_cbc_encrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
}
static void des_cbc_decrypt(int argc, char *argv[])
{
cry_cbc_ctx ctx;
cry_des_ctx des_ctx;
struct des_param par;
unsigned char dst[32];
ASSERT(argc == 4);
param_init(&par, argc, argv);
cry_cbc_init(&ctx, &des_ctx, &des_itf);
cry_cbc_key_set(&ctx, par.key, par.keylen);
cry_cbc_iv_set(&ctx, par.iv, 16);
cry_cbc_decrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
}
static void des_ctr_encrypt(int argc, char *argv[])
{
cry_ctr_ctx ctx;
cry_des_ctx des_ctx;
struct des_param par;
unsigned char dst[32];
ASSERT(argc == 4);
param_init(&par, argc, argv);
cry_ctr_init(&ctx, &des_ctx, &des_itf);
cry_ctr_key_set(&ctx, par.key, par.keylen);
cry_ctr_iv_set(&ctx, par.iv, 16);
cry_ctr_encrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
}
static void des_ctr_decrypt(int argc, char *argv[])
{
cry_ctr_ctx ctx;
cry_des_ctx des_ctx;
struct des_param par;
unsigned char dst[32];
ASSERT(argc == 4);
param_init(&par, argc, argv);
cry_ctr_init(&ctx, &des_ctx, &des_itf);
cry_ctr_key_set(&ctx, par.key, par.keylen);
cry_ctr_iv_set(&ctx, par.iv, 16);
cry_ctr_decrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
}
static void des_gcm_encrypt(int argc, char *argv[])
{
cry_gcm_ctx ctx;
cry_des_ctx des_ctx;
struct des_param par;
unsigned char dst[128];
unsigned char mac[16];
ASSERT(argc == 6);
param_init(&par, argc, argv);
cry_gcm_init(&ctx, &des_ctx, &des_itf);
cry_gcm_key_set(&ctx, par.key, par.keylen);
cry_gcm_iv_set(&ctx, par.iv, 16);
cry_gcm_update(&ctx, par.aad, par.aadlen);
cry_gcm_encrypt(&ctx, dst, par.src, par.srclen);
cry_gcm_digest(&ctx, mac, par.maclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
ASSERT_EQ_BUF(mac, par.mac, par.maclen);
}
static void des_gcm_decrypt(int argc, char *argv[])
{
cry_gcm_ctx ctx;
cry_des_ctx des_ctx;
struct des_param par;
unsigned char dst[128];
unsigned char mac[16];
ASSERT(argc == 6);
param_init(&par, argc, argv);
cry_gcm_init(&ctx, &des_ctx, &des_itf);
cry_gcm_key_set(&ctx, par.key, par.keylen);
cry_gcm_iv_set(&ctx, par.iv, 16);
cry_gcm_update(&ctx, par.aad, par.aadlen);
cry_gcm_decrypt(&ctx, dst, par.src, par.srclen);
cry_gcm_digest(&ctx, mac, par.maclen);
if (par.dstlen == par.srclen) {
ASSERT_EQ_BUF(mac, par.mac, par.maclen);
ASSERT_EQ_BUF(dst, par.dst, par.dstlen);
} else {
ASSERT_NE_BUF(mac, par.mac, par.maclen);
}
}
static void dispatch(int argc, char *argv[])
{
char *test = *argv;
argv++;
argc--;
if (strcmp(test, "des_ecb_encrypt") == 0)
des_ecb_encrypt(argc, argv);
else if (strcmp(test, "des_ecb_decrypt") == 0)
des_ecb_decrypt(argc, argv);
else if (strcmp(test, "des_cbc_encrypt") == 0)
des_cbc_encrypt(argc, argv);
else if (strcmp(test, "des_cbc_decrypt") == 0)
des_cbc_decrypt(argc, argv);
else if (strcmp(test, "des_ctr_encrypt") == 0)
des_ctr_encrypt(argc, argv);
else if (strcmp(test, "des_ctr_decrypt") == 0)
des_ctr_decrypt(argc, argv);
else if (strcmp(test, "des_gcm_encrypt") == 0)
des_gcm_encrypt(argc, argv);
else if (strcmp(test, "des_gcm_decrypt") == 0)
des_gcm_decrypt(argc, argv);
else
printf("Test '%s' not defined\n", test);
}
void des_test(void)
{
RUN(single);
RUN(triple);
printf("* DES OpenSSL Vectors\n");
func_test("des_test.data", dispatch);
printf("\n");
}
################################################################################
#
# OpenSSL test vectors
#
################################################################################
################################################################################
# DES-ECB encrypt
# p0 : key
# p1 : input
# p2 : output
################################################################################
DES-ECB Encrypt OpenSSL Test Vector #1
des_ecb_encrypt
0000000000000000
0000000000000000
8CA64DE9C1B123A7
DES-ECB Encrypt OpenSSL Test Vector #2
des_ecb_encrypt
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
7359B2163E4EDC58
DES-ECB Encrypt OpenSSL Test Vector #3
des_ecb_encrypt
3000000000000000
1000000000000001
958E6E627A05557B
DES-ECB Encrypt OpenSSL Test Vector #4
des_ecb_encrypt
1111111111111111
1111111111111111
F40379AB9E0EC533
DES-ECB Encrypt OpenSSL Test Vector #5
des_ecb_encrypt
0123456789ABCDEF
1111111111111111
17668DFC7292532D
DES-ECB Encrypt OpenSSL Test Vector #6
des_ecb_encrypt
1111111111111111
0123456789ABCDEF
8A5AE1F81AB8F2DD
DES-ECB Encrypt OpenSSL Test Vector #7
des_ecb_encrypt
0000000000000000
0000000000000000
8CA64DE9C1B123A7
DES-ECB Encrypt OpenSSL Test Vector #8
des_ecb_encrypt
FEDCBA9876543210
0123456789ABCDEF
ED39D950FA74BCC4
DES-ECB Encrypt OpenSSL Test Vector #9
des_ecb_encrypt
7CA110454A1A6E57
01A1D6D039776742
690F5B0D9A26939B
DES-ECB Encrypt OpenSSL Test Vector #10
des_ecb_encrypt
0131D9619DC1376E
5CD54CA83DEF57DA
7A389D10354BD271
DES-ECB Encrypt OpenSSL Test Vector #11
des_ecb_encrypt
07A1133E4A0B2686
0248D43806F67172
868EBB51CAB4599A
DES-ECB Encrypt OpenSSL Test Vector #12
des_ecb_encrypt
3849674C2602319E
51454B582DDF440A
7178876E01F19B2A
DES-ECB Encrypt OpenSSL Test Vector #13
des_ecb_encrypt
04B915BA43FEB5B6
42FD443059577FA2
AF37FB421F8C4095
DES-ECB Encrypt OpenSSL Test Vector #14
des_ecb_encrypt
0113B970FD34F2CE
059B5E0851CF143A
86A560F10EC6D85B
DES-ECB Encrypt OpenSSL Test Vector #15
des_ecb_encrypt
0170F175468FB5E6
0756D8E0774761D2
0CD3DA020021DC09
DES-ECB Encrypt OpenSSL Test Vector #16
des_ecb_encrypt
43297FAD38E373FE
762514B829BF486A
EA676B2CB7DB2B7A
DES-ECB Encrypt OpenSSL Test Vector #17
des_ecb_encrypt
07A7137045DA2A16
3BDD119049372802
DFD64A815CAF1A0F
DES-ECB Encrypt OpenSSL Test Vector #18
des_ecb_encrypt
04689104C2FD3B2F
26955F6835AF609A
5C513C9C4886C088
DES-ECB Encrypt OpenSSL Test Vector #19
des_ecb_encrypt
37D06BB516CB7546
164D5E404F275232
0A2AEEAE3FF4AB77
DES-ECB Encrypt OpenSSL Test Vector #20
des_ecb_encrypt
1F08260D1AC2465E
6B056E18759F5CCA
EF1BF03E5DFA575A
DES-ECB Encrypt OpenSSL Test Vector #21
des_ecb_encrypt
584023641ABA6176
004BD6EF09176062
88BF0DB6D70DEE56
DES-ECB Encrypt OpenSSL Test Vector #22
des_ecb_encrypt
025816164629B007
480D39006EE762F2
A1F9915541020B56
DES-ECB Encrypt OpenSSL Test Vector #23
des_ecb_encrypt
49793EBC79B3258F
437540C8698F3CFA
6FBF1CAFCFFD0556
DES-ECB Encrypt OpenSSL Test Vector #24
des_ecb_encrypt
4FB05E1515AB73A7
072D43A077075292
2F22E49BAB7CA1AC
DES-ECB Encrypt OpenSSL Test Vector #25
des_ecb_encrypt
49E95D6D4CA229BF
02FE55778117F12A
5A6B612CC26CCE4A
DES-ECB Encrypt OpenSSL Test Vector #26
des_ecb_encrypt
018310DC409B26D6
1D9D5C5018F728C2
5F4C038ED12B2E41
DES-ECB Encrypt OpenSSL Test Vector #27
des_ecb_encrypt
1C587F1C13924FEF
305532286D6F295A
63FAC0D034D9F793
DES-ECB Encrypt OpenSSL Test Vector #28
des_ecb_encrypt
0101010101010101
0123456789ABCDEF
617B3A0CE8F07100
DES-ECB Encrypt OpenSSL Test Vector #29
des_ecb_encrypt
1F1F1F1F0E0E0E0E
0123456789ABCDEF
DB958605F8C8C606
DES-ECB Encrypt OpenSSL Test Vector #30
des_ecb_encrypt
E0FEE0FEF1FEF1FE
0123456789ABCDEF
EDBFD1C66C29CCC7
DES-ECB Encrypt OpenSSL Test Vector #31
des_ecb_encrypt
0000000000000000
FFFFFFFFFFFFFFFF
355550B2150E2451
DES-ECB Encrypt OpenSSL Test Vector #32
des_ecb_encrypt
FFFFFFFFFFFFFFFF
0000000000000000
CAAAAF4DEAF1DBAE
DES-ECB Encrypt OpenSSL Test Vector #33
des_ecb_encrypt
0123456789ABCDEF
0000000000000000
D5D44FF720683D0D
DES-ECB Encrypt OpenSSL Test Vector #34
des_ecb_encrypt
FEDCBA9876543210
FFFFFFFFFFFFFFFF
2A2BB008DF97C2F2
DES-ECB Decrypt OpenSSL Test Vector #1
des_ecb_decrypt
0000000000000000
8CA64DE9C1B123A7
0000000000000000
DES-ECB Decrypt OpenSSL Test Vector #2
des_ecb_decrypt
FFFFFFFFFFFFFFFF
7359B2163E4EDC58
FFFFFFFFFFFFFFFF
DES-ECB Decrypt OpenSSL Test Vector #3
des_ecb_decrypt
3000000000000000
958E6E627A05557B
1000000000000001
DES-ECB Decrypt OpenSSL Test Vector #4
des_ecb_decrypt
1111111111111111
F40379AB9E0EC533
1111111111111111
DES-ECB Decrypt OpenSSL Test Vector #5
des_ecb_decrypt
0123456789ABCDEF
17668DFC7292532D
1111111111111111
DES-ECB Decrypt OpenSSL Test Vector #6
des_ecb_decrypt
1111111111111111
8A5AE1F81AB8F2DD
0123456789ABCDEF
DES-ECB Decrypt OpenSSL Test Vector #7
des_ecb_decrypt
0000000000000000
8CA64DE9C1B123A7
0000000000000000
DES-ECB Decrypt OpenSSL Test Vector #8
des_ecb_decrypt
FEDCBA9876543210
ED39D950FA74BCC4
0123456789ABCDEF
DES-ECB Decrypt OpenSSL Test Vector #9
des_ecb_decrypt
7CA110454A1A6E57
690F5B0D9A26939B
01A1D6D039776742
DES-ECB Decrypt OpenSSL Test Vector #10
des_ecb_decrypt
0131D9619DC1376E
7A389D10354BD271
5CD54CA83DEF57DA
DES-ECB Decrypt OpenSSL Test Vector #11
des_ecb_decrypt
07A1133E4A0B2686
868EBB51CAB4599A
0248D43806F67172
DES-ECB Decrypt OpenSSL Test Vector #12
des_ecb_decrypt
3849674C2602319E
7178876E01F19B2A
51454B582DDF440A
DES-ECB Decrypt OpenSSL Test Vector #13
des_ecb_decrypt
04B915BA43FEB5B6
AF37FB421F8C4095
42FD443059577FA2
DES-ECB Decrypt OpenSSL Test Vector #14
des_ecb_decrypt
0113B970FD34F2CE
86A560F10EC6D85B
059B5E0851CF143A
DES-ECB Decrypt OpenSSL Test Vector #15
des_ecb_decrypt
0170F175468FB5E6
0CD3DA020021DC09
0756D8E0774761D2
DES-ECB Decrypt OpenSSL Test Vector #16
des_ecb_decrypt
43297FAD38E373FE
EA676B2CB7DB2B7A
762514B829BF486A
DES-ECB Decrypt OpenSSL Test Vector #17
des_ecb_decrypt
07A7137045DA2A16
DFD64A815CAF1A0F
3BDD119049372802
DES-ECB Decrypt OpenSSL Test Vector #18
des_ecb_decrypt
04689104C2FD3B2F
5C513C9C4886C088
26955F6835AF609A
DES-ECB Decrypt OpenSSL Test Vector #19
des_ecb_decrypt
37D06BB516CB7546
0A2AEEAE3FF4AB77
164D5E404F275232
DES-ECB Decrypt OpenSSL Test Vector #20
des_ecb_decrypt
1F08260D1AC2465E
EF1BF03E5DFA575A
6B056E18759F5CCA
DES-ECB Decrypt OpenSSL Test Vector #21