Home | History | Annotate | Download | only in testprof
      1 /* test CFB/OFB/CBC modes */
      2 #include <tomcrypt_test.h>
      3 
      4 int modes_test(void)
      5 {
      6    unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
      7    int cipher_idx;
      8 #ifdef LTC_CBC_MODE
      9    symmetric_CBC cbc;
     10 #endif
     11 #ifdef LTC_CFB_MODE
     12    symmetric_CFB cfb;
     13 #endif
     14 #ifdef LTC_OFB_MODE
     15    symmetric_OFB ofb;
     16 #endif
     17    unsigned long l;
     18 
     19    /* make a random pt, key and iv */
     20    yarrow_read(pt,  64, &yarrow_prng);
     21    yarrow_read(key, 16, &yarrow_prng);
     22    yarrow_read(iv,  16, &yarrow_prng);
     23 
     24    /* get idx of AES handy */
     25    cipher_idx = find_cipher("aes");
     26    if (cipher_idx == -1) {
     27       fprintf(stderr, "test requires AES");
     28       return 1;
     29    }
     30 
     31 #ifdef LTC_F8_MODE
     32    DO(f8_test_mode());
     33 #endif
     34 
     35 #ifdef LTC_LRW_MODE
     36    DO(lrw_test());
     37 #endif
     38 
     39 #ifdef LTC_CBC_MODE
     40    /* test CBC mode */
     41    /* encode the block */
     42    DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
     43    l = sizeof(iv2);
     44    DO(cbc_getiv(iv2, &l, &cbc));
     45    if (l != 16 || memcmp(iv2, iv, 16)) {
     46       fprintf(stderr, "cbc_getiv failed");
     47       return 1;
     48    }
     49    DO(cbc_encrypt(pt, ct, 64, &cbc));
     50 
     51    /* decode the block */
     52    DO(cbc_setiv(iv2, l, &cbc));
     53    zeromem(tmp, sizeof(tmp));
     54    DO(cbc_decrypt(ct, tmp, 64, &cbc));
     55    if (memcmp(tmp, pt, 64) != 0) {
     56       fprintf(stderr, "CBC failed");
     57       return 1;
     58    }
     59 #endif
     60 
     61 #ifdef LTC_CFB_MODE
     62    /* test CFB mode */
     63    /* encode the block */
     64    DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
     65    l = sizeof(iv2);
     66    DO(cfb_getiv(iv2, &l, &cfb));
     67    /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
     68    if (l != 16) {
     69       fprintf(stderr, "cfb_getiv failed");
     70       return 1;
     71    }
     72    DO(cfb_encrypt(pt, ct, 64, &cfb));
     73 
     74    /* decode the block */
     75    DO(cfb_setiv(iv, l, &cfb));
     76    zeromem(tmp, sizeof(tmp));
     77    DO(cfb_decrypt(ct, tmp, 64, &cfb));
     78    if (memcmp(tmp, pt, 64) != 0) {
     79       fprintf(stderr, "CFB failed");
     80       return 1;
     81    }
     82 #endif
     83 
     84 #ifdef LTC_OFB_MODE
     85    /* test OFB mode */
     86    /* encode the block */
     87    DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
     88    l = sizeof(iv2);
     89    DO(ofb_getiv(iv2, &l, &ofb));
     90    if (l != 16 || memcmp(iv2, iv, 16)) {
     91       fprintf(stderr, "ofb_getiv failed");
     92       return 1;
     93    }
     94    DO(ofb_encrypt(pt, ct, 64, &ofb));
     95 
     96    /* decode the block */
     97    DO(ofb_setiv(iv2, l, &ofb));
     98    zeromem(tmp, sizeof(tmp));
     99    DO(ofb_decrypt(ct, tmp, 64, &ofb));
    100    if (memcmp(tmp, pt, 64) != 0) {
    101       fprintf(stderr, "OFB failed");
    102       return 1;
    103    }
    104 #endif
    105 
    106 #ifdef LTC_CTR_MODE
    107    DO(ctr_test());
    108 #endif
    109 
    110    return 0;
    111 }
    112 
    113 /* $Source: /cvs/libtom/libtomcrypt/testprof/modes_test.c,v $ */
    114 /* $Revision: 1.14 $ */
    115 /* $Date: 2006/11/13 11:55:25 $ */
    116