Commit 697592eb authored by Davide Galassi's avatar Davide Galassi

DES and TDES tests in ECB, CBC and CTR counter modes

The tests leads to a fix in the cbc and ctr code.
Same fix is probably required in GCM code.
parent d38226a4
Pipeline #36969549 passed with stages
in 1 minute and 23 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;
......
......@@ -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);
......
......@@ -89,7 +89,7 @@ static void des_cbc_encrypt(int argc, char *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_iv_set(&ctx, par.iv, par.ivlen);
cry_cbc_encrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
......@@ -107,7 +107,7 @@ static void des_cbc_decrypt(int argc, char *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_iv_set(&ctx, par.iv, par.ivlen);
cry_cbc_decrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
......@@ -125,7 +125,7 @@ static void des_ctr_encrypt(int argc, char *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_iv_set(&ctx, par.iv, par.ivlen);
cry_ctr_encrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
......@@ -143,7 +143,7 @@ static void des_ctr_decrypt(int argc, char *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_iv_set(&ctx, par.iv, par.ivlen);
cry_ctr_decrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.srclen);
......
......@@ -215,6 +215,13 @@ FEDCBA9876543210
FFFFFFFFFFFFFFFF
2A2BB008DF97C2F2
################################################################################
# DES-ECB decrypt
# p0 : key
# p1 : input
# p2 : output
################################################################################
DES-ECB Decrypt OpenSSL Test Vector #1
des_ecb_decrypt
0000000000000000
......@@ -419,3 +426,245 @@ FEDCBA9876543210
2A2BB008DF97C2F2
FFFFFFFFFFFFFFFF
################################################################################
# TDES-EBC encrypt
# p0 : key
# p2 : input
# p3 : output
################################################################################
TDES-ECB Encrypt OpenSSL Test Vector #1
des_ecb_encrypt
000000000000000000000000000000000000000000000000
0000000000000000
8CA64DE9C1B123A7
TDES-ECB Encrypt OpenSSL Test Vector #2
des_ecb_encrypt
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
7359B2163E4EDC58
TDES-ECB Encrypt OpenSSL Test Vector #3
des_ecb_encrypt
300000000000000030000000000000003000000000000000
1000000000000001
958E6E627A05557B
TDES-ECB Encrypt OpenSSL Test Vector #4
des_ecb_encrypt
111111111111111111111111111111111111111111111111
1111111111111111
F40379AB9E0EC533
TDES-ECB Encrypt OpenSSL Test Vector #5
des_ecb_encrypt
0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
1111111111111111
17668DFC7292532D
TDES-ECB Encrypt OpenSSL Test Vector #6
des_ecb_encrypt
111111111111111111111111111111111111111111111111
0123456789ABCDEF
8A5AE1F81AB8F2DD
TDES-ECB Encrypt OpenSSL Test Vector #7
des_ecb_encrypt
000000000000000000000000000000000000000000000000
0000000000000000
8CA64DE9C1B123A7
TDES-ECB Encrypt OpenSSL Test Vector #8
des_ecb_encrypt
FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210
0123456789ABCDEF
ED39D950FA74BCC4
TDES-ECB Encrypt OpenSSL Test Vector #9
des_ecb_encrypt
7CA110454A1A6E577CA110454A1A6E577CA110454A1A6E57
01A1D6D039776742
690F5B0D9A26939B
################################################################################
# TDES-ECB decrypt
# p0 : key
# p1 : input
# p2 : output
################################################################################
TDES-ECB Decrypt OpenSSL Test Vector #1
des_ecb_decrypt
000000000000000000000000000000000000000000000000
8CA64DE9C1B123A7
0000000000000000
TDES-ECB Decrypt OpenSSL Test Vector #2
des_ecb_decrypt
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
7359B2163E4EDC58
FFFFFFFFFFFFFFFF
TDES-ECB Decrypt OpenSSL Test Vector #3
des_ecb_decrypt
300000000000000030000000000000003000000000000000
958E6E627A05557B
1000000000000001
TDES-ECB Decrypt OpenSSL Test Vector #4
des_ecb_decrypt
111111111111111111111111111111111111111111111111
F40379AB9E0EC533
1111111111111111
TDES-ECB Decrypt OpenSSL Test Vector #5
des_ecb_decrypt
0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
17668DFC7292532D
1111111111111111
TDES-ECB Decrypt OpenSSL Test Vector #6
des_ecb_decrypt
111111111111111111111111111111111111111111111111
8A5AE1F81AB8F2DD
0123456789ABCDEF
TDES-ECB Decrypt OpenSSL Test Vector #7
des_ecb_decrypt
000000000000000000000000000000000000000000000000
8CA64DE9C1B123A7
0000000000000000
TDES-ECB Decrypt OpenSSL Test Vector #8
des_ecb_decrypt
FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210
ED39D950FA74BCC4
0123456789ABCDEF
TDES-ECB Decrypt OpenSSL Test Vector #9
des_ecb_decrypt
7CA110454A1A6E577CA110454A1A6E577CA110454A1A6E57
690F5B0D9A26939B
01A1D6D039776742
################################################################################
# DES-CBC encrypt
# p0 : key
# p1 : iv
# p2 : input
# p3 : output
################################################################################
DES-CBC Encrypt OpenSSL Test Vector #1
des_cbc_encrypt
0123456789abcdef
fedcba9876543210
37363534333231204E6F77206973207468652074696D6520
ccd173ffab2039f4acd8aefddfd8a1eb468e91157888ba68
################################################################################
# DES-CBC decrypt
# p0 : key
# p1 : iv
# p2 : input
# p3 : output
################################################################################
DES-CBC Decrypt OpenSSL Test Vector #1
des_cbc_decrypt
0123456789abcdef
fedcba9876543210
ccd173ffab2039f4acd8aefddfd8a1eb468e91157888ba68
37363534333231204E6F77206973207468652074696D6520
################################################################################
# TDES-CBC encrypt
# p0 : key
# p1 : iv
# p2 : input
# p3 : output
################################################################################
TDES-CBC Encrypt OpenSSL Test Vector #1
des_cbc_encrypt
0123456789abcdef0123456789abcdef0123456789abcdef
fedcba9876543210
37363534333231204E6F77206973207468652074696D6520
ccd173ffab2039f4acd8aefddfd8a1eb468e91157888ba68
################################################################################
# TDES-CBC decrypt
# p0 : key
# p1 : iv
# p2 : input
# p3 : output
################################################################################
TDES-CBC Decrypt OpenSSL Test Vector #1
des_cbc_decrypt
0123456789abcdef0123456789abcdef0123456789abcdef
fedcba9876543210
ccd173ffab2039f4acd8aefddfd8a1eb468e91157888ba68
37363534333231204E6F77206973207468652074696D6520
################################################################################
# DES-CTR encrypt
# p0 : key
# p1 : iv
# p2 : input
# p3 : output
################################################################################
DES-CTR Encrypt OpenSSL Test Vector #1
des_ctr_encrypt
0123456789abcdef
fedcba9876543210
37363534333231204E6F77206973207468652074696D6520
25f0139b36b9721b357dee68a3fe09a2b93fc605c3552ed5
################################################################################
# DES-CTR decrypt
# p0 : key
# p1 : iv
# p2 : input
# p3 : output
################################################################################
DES-CTR Decrypt OpenSSL Test Vector #1
des_ctr_decrypt
0123456789abcdef
fedcba9876543210
25f0139b36b9721b357dee68a3fe09a2b93fc605c3552ed5
37363534333231204E6F77206973207468652074696D6520
################################################################################
# TDES-CTR encrypt
# p0 : key
# p1 : iv
# p2 : input
# p3 : output
################################################################################
TDES-CTR Encrypt OpenSSL Test Vector #1
des_ctr_encrypt
0123456789abcdef0123456789abcdef0123456789abcdef
fedcba9876543210
37363534333231204E6F77206973207468652074696D6520
25f0139b36b9721b357dee68a3fe09a2b93fc605c3552ed5
################################################################################
# TDES-CTR decrypt
# p0 : key
# p1 : iv
# p2 : input
# p3 : output
################################################################################
TDES-CTR Decrypt OpenSSL Test Vector #1
des_ctr_decrypt
0123456789abcdef0123456789abcdef0123456789abcdef
fedcba9876543210
25f0139b36b9721b357dee68a3fe09a2b93fc605c3552ed5
37363534333231204E6F77206973207468652074696D6520
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