Commit 67929eab authored by Davide Galassi's avatar Davide Galassi

Merge branch 'test/des_cipher'

parents 5361876f 697592eb
Pipeline #41385733 passed with stages
in 2 minutes and 10 seconds
......@@ -9,7 +9,7 @@
#include <cry/ciph.h>
/** CBC block size. */
#define CRY_CBC_BLOCK_SIZE 16
#define CRY_CBC_BLOCK_MAX 16
/** CBC context structure. */
......@@ -19,7 +19,9 @@ struct cry_cbc_ctx {
/** Cipher interface. */
const struct cry_ciph_itf *ciph_itf;
/** Counter */
unsigned char ctr[CRY_CBC_BLOCK_SIZE];
unsigned char ctr[CRY_CBC_BLOCK_MAX];
/** Counter length */
unsigned int ctrlen;
};
typedef struct cry_cbc_ctx cry_cbc_ctx;
......
......@@ -16,19 +16,20 @@
#include <cry/ciph.h>
/** Block size. */
#define CRY_CTR_BLOCK_SIZE 16
#define CRY_CTR_BLOCK_MAX 16
/**
* CTR context.
*/
struct cry_ctr_ctx
{
struct cry_ctr_ctx {
/** Cipher context. */
void *ciph_ctx;
/** Cipher interface. */
const struct cry_ciph_itf *ciph_itf;
/** Counter. */
unsigned char ctr[CRY_CTR_BLOCK_SIZE];
unsigned char ctr[CRY_CTR_BLOCK_MAX];
/** Counter length */
unsigned int ctrlen;
};
typedef struct cry_ctr_ctx cry_ctr_ctx;
......
......@@ -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);
......
......@@ -26,9 +26,9 @@ void cry_cbc_key_set(struct cry_cbc_ctx *ctx, const unsigned char *key,
void cry_cbc_iv_set(struct cry_cbc_ctx *ctx, const unsigned char *iv,
unsigned int size)
{
size = CRY_MIN(CRY_CBC_BLOCK_SIZE, size);
memcpy(ctx->ctr, iv, size);
memset(ctx->ctr + size, 0, CRY_CBC_BLOCK_SIZE - size);
ctx->ctrlen = CRY_MIN(CRY_CBC_BLOCK_MAX, size);
memcpy(ctx->ctr, iv, ctx->ctrlen);
memset(ctx->ctr + ctx->ctrlen, 0, CRY_CBC_BLOCK_MAX - ctx->ctrlen);
}
void cry_cbc_encrypt(struct cry_cbc_ctx *ctx, unsigned char *dst,
......@@ -37,16 +37,16 @@ void cry_cbc_encrypt(struct cry_cbc_ctx *ctx, unsigned char *dst,
void *ciph = ctx->ciph_ctx;
cry_ciph_encrypt_f encrypt = ctx->ciph_itf->encrypt;
size &= ~(CRY_CBC_BLOCK_SIZE - 1);
size &= ~(ctx->ctrlen - 1);
while (size != 0) {
cry_memxor(ctx->ctr, src, CRY_CBC_BLOCK_SIZE);
encrypt(ciph, dst, ctx->ctr, CRY_CBC_BLOCK_SIZE);
memcpy(ctx->ctr, dst, CRY_CBC_BLOCK_SIZE);
cry_memxor(ctx->ctr, src, ctx->ctrlen);
encrypt(ciph, dst, ctx->ctr, ctx->ctrlen);
memcpy(ctx->ctr, dst, ctx->ctrlen);
src += CRY_CBC_BLOCK_SIZE;
dst += CRY_CBC_BLOCK_SIZE;
size -= CRY_CBC_BLOCK_SIZE;
src += ctx->ctrlen;
dst += ctx->ctrlen;
size -= ctx->ctrlen;
}
}
......@@ -58,52 +58,51 @@ void cry_cbc_decrypt(struct cry_cbc_ctx *ctx, unsigned char *dst,
void *ciph = ctx->ciph_ctx;
cry_ciph_encrypt_f decrypt = ctx->ciph_itf->decrypt;
size &= ~(CRY_CBC_BLOCK_SIZE - 1);
size &= ~(ctx->ctrlen - 1);
if (src != dst) {
decrypt(ciph, dst, src, size);
cry_memxor(dst, ctx->ctr, CRY_CBC_BLOCK_SIZE);
cry_memxor(dst + CRY_CBC_BLOCK_SIZE, src, size - CRY_CBC_BLOCK_SIZE);
memcpy(ctx->ctr, src + size - CRY_CBC_BLOCK_SIZE, CRY_CBC_BLOCK_SIZE);
cry_memxor(dst, ctx->ctr, ctx->ctrlen);
cry_memxor(dst + ctx->ctrlen, src, size - ctx->ctrlen);
memcpy(ctx->ctr, src + size - ctx->ctrlen, ctx->ctrlen);
} else {
/*
* For in-place CBC, we decrypt into a temporary buffer of size
* at most CBC_BUFFER_LIMIT, and process that amount of data at
* a time.
*/
unsigned char init_iv[CRY_CBC_BLOCK_SIZE];
unsigned char fallback[CRY_CBC_BLOCK_SIZE]; /* if malloc fails */
unsigned char init_iv[CRY_CBC_BLOCK_MAX];
unsigned char fallback[CRY_CBC_BLOCK_MAX]; /* if malloc fails */
unsigned char *buf;
size_t buf_siz = CRY_MIN(size, CBC_BUFFER_LIMIT);
buf = malloc(buf_siz);
if (buf == NULL) {
buf = fallback;
buf_siz = CRY_CBC_BLOCK_SIZE;
buf_siz = CRY_CBC_BLOCK_MAX;
}
while (size > buf_siz) {
decrypt(ciph, buf, src, buf_siz);
memcpy(init_iv, ctx->ctr, CRY_CBC_BLOCK_SIZE);
memcpy(ctx->ctr, src + buf_siz - CRY_CBC_BLOCK_SIZE,
CRY_CBC_BLOCK_SIZE);
cry_memxor2(dst + CRY_CBC_BLOCK_SIZE, buf + CRY_CBC_BLOCK_SIZE,
src, buf_siz - CRY_CBC_BLOCK_SIZE);
cry_memxor2(dst, buf, init_iv, CRY_CBC_BLOCK_SIZE);
memcpy(init_iv, ctx->ctr, ctx->ctrlen);
memcpy(ctx->ctr, src + buf_siz - ctx->ctrlen, ctx->ctrlen);
cry_memxor2(dst + ctx->ctrlen, buf + ctx->ctrlen,
src, buf_siz - ctx->ctrlen);
cry_memxor2(dst, buf, init_iv, ctx->ctrlen);
size -= buf_siz;
src += buf_siz;
dst += buf_siz;
}
decrypt(ciph, buf, src, size);
memcpy(init_iv, ctx->ctr, CRY_CBC_BLOCK_SIZE);
memcpy(init_iv, ctx->ctr, ctx->ctrlen);
/* Copies last block */
memcpy(ctx->ctr, src + size - CRY_CBC_BLOCK_SIZE, CRY_CBC_BLOCK_SIZE);
memcpy(ctx->ctr, src + size - ctx->ctrlen, ctx->ctrlen);
/* Writes all but first block, reads all but last block. */
cry_memxor2(dst + CRY_CBC_BLOCK_SIZE, buf + CRY_CBC_BLOCK_SIZE,
src, size - CRY_CBC_BLOCK_SIZE);
cry_memxor2(dst + ctx->ctrlen, buf + ctx->ctrlen,
src, size - ctx->ctrlen);
/* Writes first block. */
cry_memxor2(dst, buf, init_iv, CRY_CBC_BLOCK_SIZE);
cry_memxor2(dst, buf, init_iv, ctx->ctrlen);
if (buf != fallback)
free(buf);
......
......@@ -4,7 +4,7 @@
#include <string.h>
#define BUF_BLOCKS 4
#define BUF_LEN (CRY_CTR_BLOCK_SIZE * BUF_BLOCKS)
#define BUF_LEN (CRY_CTR_BLOCK_MAX * BUF_BLOCKS)
void cry_ctr_init(struct cry_ctr_ctx *ctx, void *ciph_ctx,
const struct cry_ciph_itf *ciph_itf)
......@@ -26,9 +26,9 @@ void cry_ctr_key_set(struct cry_ctr_ctx *ctx, const unsigned char *key,
void cry_ctr_iv_set(struct cry_ctr_ctx *ctx, const unsigned char *iv,
unsigned int size)
{
size = CRY_MIN(CRY_CTR_BLOCK_SIZE, size);
memcpy(ctx->ctr, iv, size);
memset(ctx->ctr + size, 0, CRY_CTR_BLOCK_SIZE - size);
ctx->ctrlen = CRY_MIN(CRY_CTR_BLOCK_MAX, size);
memcpy(ctx->ctr, iv, ctx->ctrlen);
memset(ctx->ctr + ctx->ctrlen, 0, CRY_CTR_BLOCK_MAX - ctx->ctrlen);
}
void cry_ctr_encrypt(struct cry_ctr_ctx *ctx, unsigned char *dst,
......@@ -42,27 +42,27 @@ void cry_ctr_encrypt(struct cry_ctr_ctx *ctx, unsigned char *dst,
if (src != dst) {
p = dst;
for (n = len; n >= CRY_CTR_BLOCK_SIZE; n -= CRY_CTR_BLOCK_SIZE) {
memcpy(p, ctx->ctr, CRY_CTR_BLOCK_SIZE);
CRY_INCREMENT_BE(ctx->ctr, CRY_CTR_BLOCK_SIZE);
p += CRY_CTR_BLOCK_SIZE;
for (n = len; n >= ctx->ctrlen; n -= ctx->ctrlen) {
memcpy(p, ctx->ctr, ctx->ctrlen);
CRY_INCREMENT_BE(ctx->ctr, ctx->ctrlen);
p += ctx->ctrlen;
}
encrypt(ciph, dst, dst, len - n);
cry_memxor(dst, src, len - n);
if (n != 0) {
encrypt(ciph, buf, ctx->ctr, CRY_CTR_BLOCK_SIZE);
CRY_INCREMENT_BE(ctx->ctr, CRY_CTR_BLOCK_SIZE);
encrypt(ciph, buf, ctx->ctr, ctx->ctrlen);
CRY_INCREMENT_BE(ctx->ctr, ctx->ctrlen);
cry_memxor2(dst + len - n, src + len - n, buf, n);
}
} else {
while (len >= BUF_LEN) {
p = buf;
for (n = 0; n < BUF_BLOCKS; n++) {
memcpy(p, ctx->ctr, CRY_CTR_BLOCK_SIZE);
CRY_INCREMENT_BE(ctx->ctr, CRY_CTR_BLOCK_SIZE);
p += CRY_CTR_BLOCK_SIZE;
memcpy(p, ctx->ctr, ctx->ctrlen);
CRY_INCREMENT_BE(ctx->ctr, ctx->ctrlen);
p += ctx->ctrlen;
}
encrypt(ciph, buf, buf, BUF_LEN);
cry_memxor2(dst, src, buf, BUF_LEN);
......@@ -73,10 +73,10 @@ void cry_ctr_encrypt(struct cry_ctr_ctx *ctx, unsigned char *dst,
if (len != 0) {
p = buf;
for (n = 0; n < len; n += CRY_CTR_BLOCK_SIZE) {
memcpy(p, ctx->ctr, CRY_CTR_BLOCK_SIZE);
CRY_INCREMENT_BE(ctx->ctr, CRY_CTR_BLOCK_SIZE);
p += CRY_CTR_BLOCK_SIZE;
for (n = 0; n < len; n += ctx->ctrlen) {
memcpy(p, ctx->ctr, ctx->ctrlen);
CRY_INCREMENT_BE(ctx->ctr, ctx->ctrlen);
p += ctx->ctrlen;
}
encrypt(ciph, buf, buf, n);
cry_memxor2(dst, src, buf, len);
......
......@@ -14,53 +14,50 @@
#define SUBKEY_SIZE 6
/* Initial permutation table */
static const int ip_tab[] = {
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
static const unsigned char ip_tab[] = {
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
/* Final permutation table */
static const int fp_tab[] = {
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
static const unsigned char fp_tab[] = {
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
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
static const unsigned char 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
};
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
static const unsigned char 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
};
/* Block expansion permutation table */
static const int exp_tab[] = {
static const unsigned char exp_tab[] = {
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
......@@ -71,65 +68,71 @@ static const int exp_tab[] = {
28, 29, 30, 31, 32, 1
};
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
static const unsigned char 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
};
static const int sbox[8][64] = {
/* S-Box table.
* In textbooks, the presented S-Box table is usually constructed by
* interpreting the six bits input as
* [ R1 | C3 | C2 | C1 | C0 | R0 ]
* Thus, for example, the output for the bit string 101100 is found
* in row 2 and column 6.
* The table below was obviously constructed as a more software
* friendly array. Thus the same element could be found at offset 44.
*
*/
static const unsigned char 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,
1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
},
{
7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
},
{
2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
},
{
12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
},
{
4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
},
{
13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
}
};
......@@ -141,24 +144,24 @@ static const int sbox[8][64] = {
* rather than 0-based arrays, since they're given that way in the
* specification.
*/
static void permute(unsigned char dst[], const unsigned char src[],
const int permute_tab[], int len)
static void permute(unsigned char *dst, const unsigned char *src,
const unsigned char *tab, unsigned int len)
{
int i;
unsigned int i;
for (i = 0; i < len * 8; i++) {
if (GET_BIT(src, permute_tab[i] - 1))
if (GET_BIT(src, tab[i] - 1))
SET_BIT(dst, i);
else
CLEAR_BIT(dst, i);
}
}
/**
* Perform the left rotation operation on the key. This is made fairly
* complex by the fact that the key is split into two 28-bit halves, each
* of which has to be rotated independently (so the second rotation operation
* starts in the middle of byte 3).
/*
* Perform the left rotation operation on the key.
* This is made fairly complex by the fact that the key is split into
* two 28-bit halves, each of which has to be rotated independentlyi
* (so the second rotation operation starts in the middle of byte 3).
*/
static void rol(unsigned char *buf)
{
......@@ -199,7 +202,8 @@ static void ror(unsigned char *buf)
buf[0] = (buf[0] >> 1) | carry_left;
}
static void memxor(unsigned char *dst, unsigned char *src, int len)
static void memxor(unsigned char *dst, unsigned char *src,
unsigned int len)
{
while (len--)
*dst++ ^= *src++;
......@@ -328,3 +332,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,13 +14,12 @@ objs := \
base64_test.o \
mpi_test.o \
hmac_test.o \
des_test.o \
aes_test.o \
trivium_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];
A