2017-06-19 09:49:18 -04:00
|
|
|
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
|
|
|
*
|
|
|
|
* LibTomCrypt is a library that provides various cryptographic
|
|
|
|
* algorithms in a highly modular and flexible manner.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*/
|
2004-05-30 22:36:47 -04:00
|
|
|
/* test CFB/OFB/CBC modes */
|
2005-04-17 07:37:13 -04:00
|
|
|
#include <tomcrypt_test.h>
|
2004-05-30 22:36:47 -04:00
|
|
|
|
|
|
|
int modes_test(void)
|
|
|
|
{
|
2015-09-01 04:09:41 -04:00
|
|
|
int ret = CRYPT_NOP;
|
2006-08-30 19:30:00 -04:00
|
|
|
#ifdef LTC_CBC_MODE
|
2004-05-30 22:36:47 -04:00
|
|
|
symmetric_CBC cbc;
|
2006-01-26 13:11:56 -05:00
|
|
|
#endif
|
2006-08-30 19:30:00 -04:00
|
|
|
#ifdef LTC_CFB_MODE
|
2004-05-30 22:36:47 -04:00
|
|
|
symmetric_CFB cfb;
|
2006-01-26 13:11:56 -05:00
|
|
|
#endif
|
2006-08-30 19:30:00 -04:00
|
|
|
#ifdef LTC_OFB_MODE
|
2004-05-30 22:36:47 -04:00
|
|
|
symmetric_OFB ofb;
|
2006-01-26 13:11:56 -05:00
|
|
|
#endif
|
2015-09-01 04:09:41 -04:00
|
|
|
#if defined(LTC_CBC_MODE) || defined(LTC_CFB_MODE) || defined(LTC_OFB_MODE)
|
|
|
|
unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
|
|
|
|
int cipher_idx;
|
2004-05-30 22:36:47 -04:00
|
|
|
unsigned long l;
|
2015-09-01 04:09:41 -04:00
|
|
|
|
2004-05-30 22:36:47 -04:00
|
|
|
/* make a random pt, key and iv */
|
2005-04-17 07:37:13 -04:00
|
|
|
yarrow_read(pt, 64, &yarrow_prng);
|
|
|
|
yarrow_read(key, 16, &yarrow_prng);
|
|
|
|
yarrow_read(iv, 16, &yarrow_prng);
|
2015-09-01 04:09:41 -04:00
|
|
|
|
2004-05-30 22:36:47 -04:00
|
|
|
/* get idx of AES handy */
|
|
|
|
cipher_idx = find_cipher("aes");
|
|
|
|
if (cipher_idx == -1) {
|
2005-06-08 20:08:13 -04:00
|
|
|
fprintf(stderr, "test requires AES");
|
2004-05-30 22:36:47 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2015-09-01 04:09:41 -04:00
|
|
|
#endif
|
|
|
|
|
2006-06-17 21:37:50 -04:00
|
|
|
#ifdef LTC_F8_MODE
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = f8_test_mode());
|
|
|
|
#endif
|
|
|
|
|
2006-08-30 19:30:00 -04:00
|
|
|
#ifdef LTC_LRW_MODE
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = lrw_test());
|
2006-01-26 13:11:56 -05:00
|
|
|
#endif
|
|
|
|
|
2006-08-30 19:30:00 -04:00
|
|
|
#ifdef LTC_CBC_MODE
|
2004-05-30 22:36:47 -04:00
|
|
|
/* test CBC mode */
|
|
|
|
/* encode the block */
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
|
2004-05-30 22:36:47 -04:00
|
|
|
l = sizeof(iv2);
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cbc_getiv(iv2, &l, &cbc));
|
2004-05-30 22:36:47 -04:00
|
|
|
if (l != 16 || memcmp(iv2, iv, 16)) {
|
2005-06-08 20:08:13 -04:00
|
|
|
fprintf(stderr, "cbc_getiv failed");
|
2004-05-30 22:36:47 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cbc_encrypt(pt, ct, 64, &cbc));
|
|
|
|
|
2004-05-30 22:36:47 -04:00
|
|
|
/* decode the block */
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cbc_setiv(iv2, l, &cbc));
|
2004-05-30 22:36:47 -04:00
|
|
|
zeromem(tmp, sizeof(tmp));
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cbc_decrypt(ct, tmp, 64, &cbc));
|
2004-05-30 22:36:47 -04:00
|
|
|
if (memcmp(tmp, pt, 64) != 0) {
|
2005-06-08 20:08:13 -04:00
|
|
|
fprintf(stderr, "CBC failed");
|
2004-05-30 22:36:47 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2005-04-17 07:37:13 -04:00
|
|
|
#endif
|
|
|
|
|
2006-08-30 19:30:00 -04:00
|
|
|
#ifdef LTC_CFB_MODE
|
2004-05-30 22:36:47 -04:00
|
|
|
/* test CFB mode */
|
|
|
|
/* encode the block */
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
|
2004-05-30 22:36:47 -04:00
|
|
|
l = sizeof(iv2);
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cfb_getiv(iv2, &l, &cfb));
|
2004-05-30 22:36:47 -04:00
|
|
|
/* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
|
|
|
|
if (l != 16) {
|
2005-06-08 20:08:13 -04:00
|
|
|
fprintf(stderr, "cfb_getiv failed");
|
2004-05-30 22:36:47 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cfb_encrypt(pt, ct, 64, &cfb));
|
|
|
|
|
2004-05-30 22:36:47 -04:00
|
|
|
/* decode the block */
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cfb_setiv(iv, l, &cfb));
|
2004-05-30 22:36:47 -04:00
|
|
|
zeromem(tmp, sizeof(tmp));
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = cfb_decrypt(ct, tmp, 64, &cfb));
|
2004-05-30 22:36:47 -04:00
|
|
|
if (memcmp(tmp, pt, 64) != 0) {
|
2005-06-08 20:08:13 -04:00
|
|
|
fprintf(stderr, "CFB failed");
|
2004-05-30 22:36:47 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2005-04-17 07:37:13 -04:00
|
|
|
#endif
|
2015-09-01 04:09:41 -04:00
|
|
|
|
2006-08-30 19:30:00 -04:00
|
|
|
#ifdef LTC_OFB_MODE
|
2004-05-30 22:36:47 -04:00
|
|
|
/* test OFB mode */
|
|
|
|
/* encode the block */
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
|
2004-05-30 22:36:47 -04:00
|
|
|
l = sizeof(iv2);
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = ofb_getiv(iv2, &l, &ofb));
|
2004-05-30 22:36:47 -04:00
|
|
|
if (l != 16 || memcmp(iv2, iv, 16)) {
|
2005-06-08 20:08:13 -04:00
|
|
|
fprintf(stderr, "ofb_getiv failed");
|
2004-05-30 22:36:47 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = ofb_encrypt(pt, ct, 64, &ofb));
|
|
|
|
|
2004-05-30 22:36:47 -04:00
|
|
|
/* decode the block */
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = ofb_setiv(iv2, l, &ofb));
|
2004-05-30 22:36:47 -04:00
|
|
|
zeromem(tmp, sizeof(tmp));
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = ofb_decrypt(ct, tmp, 64, &ofb));
|
2004-05-30 22:36:47 -04:00
|
|
|
if (memcmp(tmp, pt, 64) != 0) {
|
2005-06-08 20:08:13 -04:00
|
|
|
fprintf(stderr, "OFB failed");
|
2004-05-30 22:36:47 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2005-04-17 07:37:13 -04:00
|
|
|
#endif
|
|
|
|
|
2015-09-01 04:09:41 -04:00
|
|
|
#if defined(LTC_CTR_MODE) && defined(LTC_RIJNDAEL)
|
|
|
|
DO(ret = ctr_test());
|
2005-04-17 07:37:13 -04:00
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
|
|
|
|
#ifdef LTC_XTS_MODE
|
2015-09-01 04:09:41 -04:00
|
|
|
DO(ret = xts_test());
|
2007-07-20 13:48:02 -04:00
|
|
|
#endif
|
2015-09-01 04:09:41 -04:00
|
|
|
|
2004-05-30 22:36:47 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-08 20:08:13 -04:00
|
|
|
|
2017-06-19 07:43:49 -04:00
|
|
|
/* ref: $Format:%D$ */
|
|
|
|
/* git commit: $Format:%H$ */
|
|
|
|
/* commit time: $Format:%ai$ */
|