📄 ecrypt-test.c
字号:
/* ecrypt-test.c */
/*
* API conformance test and test vector generation (DRAFT)
*
* Based on the NESSIE test suite (http://www.cryptonessie.org/)
*/
/* ------------------------------------------------------------------------- */
#define QUOTE(str) QUOTE_HELPER(str)
#define QUOTE_HELPER(str) # str
#include "ecrypt-portable.h"
#include QUOTE(ECRYPT_API)
#if defined(ECRYPT_SSYN) || defined(ECRYPT_SSYN_AE)
#error self-synchronising stream ciphers are not supported yet
#endif
#if defined(ECRYPT_SYNC_AE) || defined(ECRYPT_SSYN_AE)
#define ECRYPT_AE
#endif
#include <stdio.h>
#include <string.h>
#define MAXKEYSIZEB ((ECRYPT_MAXKEYSIZE + 7) / 8)
#define MAXIVSIZEB ((ECRYPT_MAXIVSIZE + 7) / 8)
#define MAXMACSIZEB ((ECRYPT_MAXMACSIZE + 7) / 8)
/* ------------------------------------------------------------------------- */
int compare_blocks(const u8 *m1, const u8 *m2, int len_bits)
{
int i;
const int lenb = (len_bits + 7) >> 3;
const int mask0 = (1 << (((len_bits - 1) & 7) + 1)) - 1;
if ((m1[0] & mask0) != (m2[0] & mask0))
return 1;
for (i = 1; i < lenb; i++)
if (m1[i] != m2[i])
return 1;
return 0;
}
void print_data(FILE *fd, const char *str, const u8 *val, int len)
{
int i;
static const char hex[] = "0123456789ABCDEF";
fprintf(fd, "%28s = ", str);
for (i = 0; i < len; i++)
{
if (i > 0 && (i & 0xF) == 0 && (len > 24))
fprintf(fd, "\n%28s ", "");
putc(hex[(val[i] >> 4) & 0xF], fd);
putc(hex[(val[i] ) & 0xF], fd);
}
fprintf(fd, "\n");
}
void print_chunk(FILE *fd, const char *str, const u8 *val, int start, int len)
{
char indexed[80];
sprintf(indexed, "%s[%d..%d]", str, start, start + len - 1);
print_data(fd, indexed, val + start, len);
}
void xor_digest(const u8 *stream, int size, u8 *out, int outsize)
{
int i;
memset(out, 0, outsize);
for (i = 0; i < size; i++)
out[i % outsize] ^= stream[i];
}
/* ------------------------------------------------------------------------- */
#define TEST_STREAM_SIZEB 0x200
#define TEST_STREAM_SIZEB_SET4 0x20000
#define TEST_CHUNK 64
#ifdef ECRYPT_AE
#define CTX ECRYPT_AE_ctx
#define IVSETUP ECRYPT_AE_ivsetup
#define ENCRYPT_BYTES ECRYPT_AE_encrypt_bytes
#define DECRYPT_BYTES ECRYPT_AE_decrypt_bytes
#define AUTHENTICATE_BYTES ECRYPT_AE_authenticate_bytes
#define ENCRYPT_BLOCKS ECRYPT_AE_encrypt_blocks
#define DECRYPT_BLOCKS ECRYPT_AE_decrypt_blocks
#define KEYSETUP ECRYPT_AE_keysetup
#define ENCRYPT_PACKET ECRYPT_AE_encrypt_packet
#define DECRYPT_PACKET ECRYPT_AE_decrypt_packet
#define FINALIZE ECRYPT_AE_finalize
#else
#define CTX ECRYPT_ctx
#define IVSETUP ECRYPT_ivsetup
#define ENCRYPT_BYTES ECRYPT_encrypt_bytes
#define DECRYPT_BYTES ECRYPT_decrypt_bytes
#define ENCRYPT_BLOCKS ECRYPT_encrypt_blocks
#define DECRYPT_BLOCKS ECRYPT_decrypt_blocks
#define KEYSETUP(ctx, key, keysize, ivsize, macsize) \
ECRYPT_keysetup(ctx, key, keysize, ivsize)
#define ENCRYPT_PACKET( \
ctx, iv, aad, aadlen, plaintext, ciphertext, msglen, mac) \
ECRYPT_encrypt_packet(ctx, iv, plaintext, ciphertext, msglen)
#define DECRYPT_PACKET( \
ctx, iv, aad, aadlen, ciphertext, plaintext, msglen, mac) \
ECRYPT_decrypt_packet(ctx, iv, ciphertext, plaintext, msglen)
#define FINALIZE(ctx, checkmac)
#endif
typedef struct
{
int keysize;
int ivsize;
int msglen;
CTX ctx;
u8 key[MAXKEYSIZEB];
u8 iv[MAXIVSIZEB];
u8 plaintext[TEST_STREAM_SIZEB_SET4];
u8 ciphertext[TEST_STREAM_SIZEB_SET4];
u8 checktext[TEST_STREAM_SIZEB_SET4];
#ifdef ECRYPT_AE
int macsize;
int aadlen;
u8 aad[TEST_CHUNK];
u8 mac[MAXMACSIZEB];
u8 checkmac[MAXMACSIZEB];
#endif
u8 xored[TEST_CHUNK];
FILE *fd;
} test_struct;
int errors = 0;
void encrypt_and_check(test_struct* t, void (*print)(test_struct*, int))
{
const int blocks = t->msglen / ECRYPT_BLOCKLENGTH;
const int tail = blocks * ECRYPT_BLOCKLENGTH;
const int bytes = t->msglen % ECRYPT_BLOCKLENGTH;
memset(t->ciphertext, 0, sizeof(t->ciphertext));
#ifdef ECRYPT_AE
memset(t->mac, 0, sizeof(t->mac));
#endif
KEYSETUP(&t->ctx, t->key, t->keysize, t->ivsize, t->macsize);
ENCRYPT_PACKET(&t->ctx, t->iv,
t->aad, t->aadlen, t->plaintext, t->ciphertext, t->msglen, t->mac);
print(t, 0);
#ifdef ECRYPT_AE
memset(t->checkmac, 0, sizeof(t->checkmac));
#endif
memset(t->checktext, 0, sizeof(t->checktext));
KEYSETUP(&t->ctx, t->key, t->keysize, t->ivsize, t->macsize);
DECRYPT_PACKET(&t->ctx, t->iv,
t->aad, t->aadlen, t->ciphertext, t->checktext, t->msglen, t->checkmac);
if (compare_blocks(t->plaintext, t->checktext, t->msglen * 8) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> decrypt_packet:\n"
"*** decrypted text differs from plaintext:\n");
print(t, 1);
}
#ifdef ECRYPT_AE
else if (compare_blocks(t->mac, t->checkmac, t->macsize) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> decrypt_packet:\n"
"*** decryption MAC differs from encryption MAC:\n");
print_data(t->fd, "MAC", t->checkmac, (t->macsize + 7) / 8);
}
memset(t->checkmac, 0, sizeof(t->checkmac));
#endif
memset(t->checktext, 0, sizeof(t->checktext));
IVSETUP(&t->ctx, t->iv);
#ifdef ECRYPT_SUPPORTS_AAD
AUTHENTICATE_BYTES(&t->ctx, t->aad, t->aadlen);
#endif
ENCRYPT_BYTES(&t->ctx, t->plaintext, t->checktext, t->msglen);
FINALIZE(&t->ctx, t->checkmac);
if (compare_blocks(t->ciphertext, t->checktext, t->msglen * 8) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> encrypt_bytes:\n"
"*** encrypt_bytes generates different ciphertext:\n");
print(t, 2);
}
#ifdef ECRYPT_AE
else if (compare_blocks(t->mac, t->checkmac, t->macsize) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> encrypt_bytes:\n"
"*** encrypt_bytes generates different MAC:\n");
print_data(t->fd, "MAC", t->checkmac, (t->macsize + 7) / 8);
}
memset(t->checkmac, 0, sizeof(t->checkmac));
#endif
memset(t->checktext, 0, sizeof(t->checktext));
IVSETUP(&t->ctx, t->iv);
#ifdef ECRYPT_SUPPORTS_AAD
AUTHENTICATE_BYTES(&t->ctx, t->aad, t->aadlen);
#endif
DECRYPT_BYTES(&t->ctx, t->ciphertext, t->checktext, t->msglen);
FINALIZE(&t->ctx, t->checkmac);
if (compare_blocks(t->plaintext, t->checktext, t->msglen * 8) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> decrypt_bytes:\n"
"*** decrypt_bytes generates different plaintext:\n");
print(t, 2);
}
#ifdef ECRYPT_AE
else if (compare_blocks(t->mac, t->checkmac, t->macsize) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> decrypt_bytes:\n"
"*** decrypt_bytes generates different MAC:\n");
print_data(t->fd, "MAC", t->checkmac, (t->macsize + 7) / 8);
}
memset(t->checkmac, 0, sizeof(t->checkmac));
#endif
memset(t->checktext, 0, sizeof(t->checktext));
IVSETUP(&t->ctx, t->iv);
#ifdef ECRYPT_SUPPORTS_AAD
AUTHENTICATE_BYTES(&t->ctx, t->aad, t->aadlen);
#endif
ENCRYPT_BLOCKS(&t->ctx, t->plaintext, t->checktext, blocks);
ENCRYPT_BYTES(&t->ctx, t->plaintext + tail, t->checktext + tail, bytes);
FINALIZE(&t->ctx, t->checkmac);
if (compare_blocks(t->ciphertext, t->checktext, t->msglen * 8) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> encrypt_blocks/bytes:\n"
"*** encrypt_blocks/bytes generates different ciphertext:\n");
print(t, 2);
}
#ifdef ECRYPT_AE
else if (compare_blocks(t->mac, t->checkmac, t->macsize) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> encrypt_blocks/bytes:\n"
"*** encrypt_blocks/bytes generates different MAC:\n");
print_data(t->fd, "MAC", t->checkmac, (t->macsize + 7) / 8);
}
memset(t->checkmac, 0, sizeof(t->checkmac));
#endif
memset(t->checktext, 0, sizeof(t->checktext));
IVSETUP(&t->ctx, t->iv);
#ifdef ECRYPT_SUPPORTS_AAD
AUTHENTICATE_BYTES(&t->ctx, t->aad, t->aadlen);
#endif
DECRYPT_BLOCKS(&t->ctx, t->ciphertext, t->checktext, blocks);
DECRYPT_BYTES(&t->ctx, t->ciphertext + tail, t->checktext + tail, bytes);
FINALIZE(&t->ctx, t->checkmac);
if (compare_blocks(t->plaintext, t->checktext, t->msglen * 8) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> decrypt_blocks/bytes:\n"
"*** decrypt_blocks/bytes generates different plaintext:\n");
print(t, 2);
}
#ifdef ECRYPT_AE
else if (compare_blocks(t->mac, t->checkmac, t->macsize) != 0)
{
++errors;
fprintf(t->fd,
"*** ERROR: encrypt_packet <-> decrypt_blocks/bytes:\n"
"*** decrypt_blocks/bytes generates different MAC:\n");
print_data(t->fd, "MAC", t->checkmac, (t->macsize + 7) / 8);
}
#endif
fprintf(t->fd, "\n");
}
void print_stream(test_struct* t, int type)
{
const int chunk = TEST_CHUNK;
switch (type)
{
case 0:
print_data(t->fd, "key", t->key, (t->keysize + 7) / 8);
print_data(t->fd, "IV", t->iv, (t->ivsize + 7) / 8);
print_chunk(t->fd, "stream", t->ciphertext, 0, chunk);
print_chunk(t->fd, "stream", t->ciphertext, t->msglen/2-chunk, chunk);
print_chunk(t->fd, "stream", t->ciphertext, t->msglen/2, chunk);
print_chunk(t->fd, "stream", t->ciphertext, t->msglen-chunk, chunk);
xor_digest(t->ciphertext, t->msglen, t->xored, chunk);
print_data(t->fd, "xor-digest", t->xored, chunk);
#ifdef ECRYPT_AE
print_data(t->fd, "MAC", t->mac, (t->macsize + 7) / 8);
#endif
break;
case 1:
print_chunk(t->fd, "decryption", t->checktext, 0, chunk);
print_chunk(t->fd, "decryption", t->checktext, t->msglen/2-chunk, chunk);
print_chunk(t->fd, "decryption", t->checktext, t->msglen/2, chunk);
print_chunk(t->fd, "decryption", t->checktext, t->msglen-chunk, chunk);
xor_digest(t->checktext, t->msglen, t->xored, chunk);
print_data(t->fd, "xor-digest", t->xored, chunk);
#ifdef ECRYPT_AE
print_data(t->fd, "MAC", t->checkmac, (t->macsize + 7) / 8);
#endif
break;
case 2:
print_chunk(t->fd, "stream", t->checktext, 0, chunk);
print_chunk(t->fd, "stream", t->checktext, t->msglen/2-chunk, chunk);
print_chunk(t->fd, "stream", t->checktext, t->msglen/2, chunk);
print_chunk(t->fd, "stream", t->checktext, t->msglen-chunk, chunk);
xor_digest(t->checktext, t->msglen, t->xored, chunk);
print_data(t->fd, "xor-digest", t->xored, chunk);
#ifdef ECRYPT_AE
print_data(t->fd, "MAC", t->checkmac, (t->macsize + 7) / 8);
#endif
break;
}
}
void print_pair(test_struct* t, int type)
{
switch (type)
{
case 0:
print_data(t->fd, "key", t->key, (t->keysize + 7) / 8);
print_data(t->fd, "IV", t->iv, (t->ivsize + 7) / 8);
#ifdef ECRYPT_SUPPORTS_AAD
if (t->aadlen)
print_data(t->fd, "AAD", t->aad, t->aadlen);
#endif
print_data(t->fd, "plaintext", t->plaintext, t->msglen);
print_data(t->fd, "ciphertext", t->ciphertext, t->msglen);
#ifdef ECRYPT_AE
print_data(t->fd, "MAC", t->mac, (t->macsize + 7) / 8);
#endif
break;
case 1:
print_data(t->fd, "decryption", t->checktext, t->msglen);
#ifdef ECRYPT_AE
print_data(t->fd, "MAC", t->checkmac, (t->macsize + 7) / 8);
#endif
break;
case 2:
print_data(t->fd, "ciphertext", t->checktext, t->msglen);
#ifdef ECRYPT_AE
print_data(t->fd, "MAC", t->checkmac, (t->macsize + 7) / 8);
#endif
break;
}
}
void test_vectors(FILE *fd, int keysize, int ivsize, int macsize)
{
#define STREAM_VECTOR(set, vector) \
do { \
fprintf(fd, "Set %d, vector#%3d:\n", set, vector); \
encrypt_and_check(&t, print_stream); \
} while (0)
#define MAC_VECTOR(set, vector) \
do { \
fprintf(fd, "Set %d, vector#%3d:\n", set, vector); \
encrypt_and_check(&t, print_pair); \
} while (0)
#define AAD_VECTOR(set, vector) \
do { \
fprintf(fd, "Set %d, vector#%3d:\n", set, vector); \
encrypt_and_check(&t, print_pair); \
} while (0)
test_struct t;
int i, v;
fprintf(fd,
"****************************************"
"****************************************\n");
fprintf(fd,
"* ECRYPT Stream"
" Cipher Project *\n");
fprintf(fd,
"****************************************"
"****************************************\n");
fprintf(fd, "\n");
fprintf(fd, "Primitive Name: %s\n", ECRYPT_NAME);
fprintf(fd, "================%.*s\n", (int)strlen(ECRYPT_NAME),
"==========================================");
fprintf(fd, "Key size: %d bits\n", keysize);
fprintf(fd, "IV size: %d bits\n", ivsize);
#ifdef ECRYPT_AE
fprintf(fd, "MAC size: %d bits\n", macsize);
#endif
fprintf(fd, "\n");
fprintf(fd, "Preferred block length: %d bytes\n", ECRYPT_BLOCKLENGTH);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -