/* 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 LONG_TEST_STREAM_SIZEB 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[LONG_TEST_STREAM_SIZEB];
u8 ciphertext[LONG_TEST_STREAM_SIZEB];
u8 checktext[LONG_TEST_STREAM_SIZEB];
#ifdef ECRYPT_AE
int macsize;
int aadlen;
u8 aad[TEST_CHUNK];
u8 mac[MAXMACSIZEB];
u8 checkmac[MAXMACSIZEB];
#endif
u8 xored[TEST_CHUNK];
FILE *fd;
int vector;
} test_struct;
int errors = 0;
void encrypt_and_check(test_struct* t, void (*print)(test_struct*, int))
{
u8* plaintext;
u8* ciphertext;
u8* checktext;
int msglen;
unsigned int i;
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
plaintext = t->plaintext;
checktext = t->checktext;
msglen = t->msglen;
for (i = (t->vector + 1) * 1381; msglen >= ECRYPT_BLOCKLENGTH; i *= 1487)
{
const int blocks = i % (msglen / ECRYPT_BLOCKLENGTH + 1);
const int bytes = blocks * ECRYPT_BLOCKLENGTH;
ENCRYPT_BLOCKS(&t->ctx, plaintext, checktext, blocks);
plaintext += bytes;
checktext += bytes;
msglen -= bytes;
if (blocks == 0)
break;
}
ENCRYPT_BYTES(&t->ctx, plaintext, checktext, 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_blocks/bytes:\n"
"*** encrypt_blocks/bytes generates differen
trivium官方代码
5星 · 超过95%的资源 需积分: 47 78 浏览量
2012-05-24
11:21:33
上传
评论 3
收藏 13KB ZIP 举报
magicyandy
- 粉丝: 0
- 资源: 2
最新资源
- 基于图像的三维模型重建C++源代码+文档说明(高分课程设计)
- 基于聚焦法的工件立体测量方案,根据数据进行三维重建 使用HALCON处理图像,MATLAB拟合数据+源代码+数据集+效果图
- 锄战三国村 修改:货币使用不减 v1.10(2) 原创 (中文).apk
- 基于python实现的单目双目视觉三维重建+源代码+图像图片(高分课程设计)
- 基于C+++OPENCV的全景图像拼接源码(课程设计)
- 基于Python+OpenCV对多张图片进行全景图像拼接,消除鬼影,消除裂缝+源代码+文档说明+界面截图(高分课程设计)
- 基于C++实现的全景图像拼接源码(课程设计)
- 基于SIFT特征点提取和RASIC算法实现全景图像拼接python源码+文档说明+界面截图+详细注释(95分以上课程大作业)
- 基于matlab实现眼部判别的疲劳检测系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的异常姿势识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈