tomcrypt/testprof/modes_test.c

137 lines
3.0 KiB
C
Raw Normal View History

2004-05-31 02:36:47 +00:00
/* test CFB/OFB/CBC modes */
2005-04-17 11:37:13 +00:00
#include <tomcrypt_test.h>
2004-05-31 02:36:47 +00:00
int modes_test(void)
{
unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
2005-04-17 11:37:13 +00:00
int cipher_idx;
2006-01-26 18:11:56 +00:00
#ifdef CBC
2004-05-31 02:36:47 +00:00
symmetric_CBC cbc;
2006-01-26 18:11:56 +00:00
#endif
#ifdef CFB
2004-05-31 02:36:47 +00:00
symmetric_CFB cfb;
2006-01-26 18:11:56 +00:00
#endif
#ifdef OFB
2004-05-31 02:36:47 +00:00
symmetric_OFB ofb;
2006-01-26 18:11:56 +00:00
#endif
#ifdef CTR
2004-05-31 02:36:47 +00:00
symmetric_CTR ctr;
2006-01-26 18:11:56 +00:00
#endif
2004-05-31 02:36:47 +00:00
unsigned long l;
/* make a random pt, key and iv */
2005-04-17 11:37:13 +00:00
yarrow_read(pt, 64, &yarrow_prng);
yarrow_read(key, 16, &yarrow_prng);
yarrow_read(iv, 16, &yarrow_prng);
2004-05-31 02:36:47 +00:00
/* get idx of AES handy */
cipher_idx = find_cipher("aes");
if (cipher_idx == -1) {
2005-06-09 00:08:13 +00:00
fprintf(stderr, "test requires AES");
2004-05-31 02:36:47 +00:00
return 1;
}
2006-06-18 01:37:50 +00:00
#ifdef LTC_F8_MODE
DO(f8_test_mode());
#endif
2006-01-26 18:11:56 +00:00
#ifdef LRW_MODE
DO(lrw_test());
#endif
2005-04-17 11:37:13 +00:00
#ifdef CBC
2004-05-31 02:36:47 +00:00
/* test CBC mode */
/* encode the block */
DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
l = sizeof(iv2);
DO(cbc_getiv(iv2, &l, &cbc));
if (l != 16 || memcmp(iv2, iv, 16)) {
2005-06-09 00:08:13 +00:00
fprintf(stderr, "cbc_getiv failed");
2004-05-31 02:36:47 +00:00
return 1;
}
2005-04-17 11:37:13 +00:00
DO(cbc_encrypt(pt, ct, 64, &cbc));
2004-05-31 02:36:47 +00:00
/* decode the block */
DO(cbc_setiv(iv2, l, &cbc));
zeromem(tmp, sizeof(tmp));
2005-04-17 11:37:13 +00:00
DO(cbc_decrypt(ct, tmp, 64, &cbc));
2004-05-31 02:36:47 +00:00
if (memcmp(tmp, pt, 64) != 0) {
2005-06-09 00:08:13 +00:00
fprintf(stderr, "CBC failed");
2004-05-31 02:36:47 +00:00
return 1;
}
2005-04-17 11:37:13 +00:00
#endif
#ifdef CFB
2004-05-31 02:36:47 +00:00
/* test CFB mode */
/* encode the block */
DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
l = sizeof(iv2);
DO(cfb_getiv(iv2, &l, &cfb));
/* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
if (l != 16) {
2005-06-09 00:08:13 +00:00
fprintf(stderr, "cfb_getiv failed");
2004-05-31 02:36:47 +00:00
return 1;
}
DO(cfb_encrypt(pt, ct, 64, &cfb));
/* decode the block */
DO(cfb_setiv(iv, l, &cfb));
zeromem(tmp, sizeof(tmp));
DO(cfb_decrypt(ct, tmp, 64, &cfb));
if (memcmp(tmp, pt, 64) != 0) {
2005-06-09 00:08:13 +00:00
fprintf(stderr, "CFB failed");
2004-05-31 02:36:47 +00:00
return 1;
}
2005-04-17 11:37:13 +00:00
#endif
2004-05-31 02:36:47 +00:00
2005-04-17 11:37:13 +00:00
#ifdef OFB
2004-05-31 02:36:47 +00:00
/* test OFB mode */
/* encode the block */
DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
l = sizeof(iv2);
DO(ofb_getiv(iv2, &l, &ofb));
if (l != 16 || memcmp(iv2, iv, 16)) {
2005-06-09 00:08:13 +00:00
fprintf(stderr, "ofb_getiv failed");
2004-05-31 02:36:47 +00:00
return 1;
}
DO(ofb_encrypt(pt, ct, 64, &ofb));
/* decode the block */
DO(ofb_setiv(iv2, l, &ofb));
zeromem(tmp, sizeof(tmp));
DO(ofb_decrypt(ct, tmp, 64, &ofb));
if (memcmp(tmp, pt, 64) != 0) {
2005-06-09 00:08:13 +00:00
fprintf(stderr, "OFB failed");
2004-05-31 02:36:47 +00:00
return 1;
}
2005-04-17 11:37:13 +00:00
#endif
#ifdef CTR
2004-05-31 02:36:47 +00:00
/* test CTR mode */
/* encode the block */
2005-06-09 00:08:13 +00:00
DO(ctr_start(cipher_idx, iv, key, 16, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr));
2004-05-31 02:36:47 +00:00
l = sizeof(iv2);
DO(ctr_getiv(iv2, &l, &ctr));
if (l != 16 || memcmp(iv2, iv, 16)) {
2005-06-09 00:08:13 +00:00
fprintf(stderr, "ctr_getiv failed");
2004-05-31 02:36:47 +00:00
return 1;
}
2005-04-17 11:37:13 +00:00
DO(ctr_encrypt(pt, ct, 57, &ctr));
2004-05-31 02:36:47 +00:00
/* decode the block */
DO(ctr_setiv(iv2, l, &ctr));
zeromem(tmp, sizeof(tmp));
2005-04-17 11:37:13 +00:00
DO(ctr_decrypt(ct, tmp, 57, &ctr));
if (memcmp(tmp, pt, 57) != 0) {
2005-06-09 00:08:13 +00:00
fprintf(stderr, "CTR failed");
2004-05-31 02:36:47 +00:00
return 1;
}
2005-04-17 11:37:13 +00:00
#endif
2004-05-31 02:36:47 +00:00
return 0;
}
2005-06-09 00:08:13 +00:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */