1
/* test CFB/OFB/CBC modes */
2
#include <tomcrypt_test.h>
6
unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
14
/* make a random pt, key and iv */
15
yarrow_read(pt, 64, &yarrow_prng);
16
yarrow_read(key, 16, &yarrow_prng);
17
yarrow_read(iv, 16, &yarrow_prng);
19
/* get idx of AES handy */
20
cipher_idx = find_cipher("aes");
21
if (cipher_idx == -1) {
22
fprintf(stderr, "test requires AES");
28
/* encode the block */
29
DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
31
DO(cbc_getiv(iv2, &l, &cbc));
32
if (l != 16 || memcmp(iv2, iv, 16)) {
33
fprintf(stderr, "cbc_getiv failed");
36
DO(cbc_encrypt(pt, ct, 64, &cbc));
38
/* decode the block */
39
DO(cbc_setiv(iv2, l, &cbc));
40
zeromem(tmp, sizeof(tmp));
41
DO(cbc_decrypt(ct, tmp, 64, &cbc));
42
if (memcmp(tmp, pt, 64) != 0) {
43
fprintf(stderr, "CBC failed");
50
/* encode the block */
51
DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
53
DO(cfb_getiv(iv2, &l, &cfb));
54
/* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
56
fprintf(stderr, "cfb_getiv failed");
59
DO(cfb_encrypt(pt, ct, 64, &cfb));
61
/* decode the block */
62
DO(cfb_setiv(iv, l, &cfb));
63
zeromem(tmp, sizeof(tmp));
64
DO(cfb_decrypt(ct, tmp, 64, &cfb));
65
if (memcmp(tmp, pt, 64) != 0) {
66
fprintf(stderr, "CFB failed");
73
/* encode the block */
74
DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
76
DO(ofb_getiv(iv2, &l, &ofb));
77
if (l != 16 || memcmp(iv2, iv, 16)) {
78
fprintf(stderr, "ofb_getiv failed");
81
DO(ofb_encrypt(pt, ct, 64, &ofb));
83
/* decode the block */
84
DO(ofb_setiv(iv2, l, &ofb));
85
zeromem(tmp, sizeof(tmp));
86
DO(ofb_decrypt(ct, tmp, 64, &ofb));
87
if (memcmp(tmp, pt, 64) != 0) {
88
fprintf(stderr, "OFB failed");
95
/* encode the block */
96
DO(ctr_start(cipher_idx, iv, key, 16, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr));
98
DO(ctr_getiv(iv2, &l, &ctr));
99
if (l != 16 || memcmp(iv2, iv, 16)) {
100
fprintf(stderr, "ctr_getiv failed");
103
DO(ctr_encrypt(pt, ct, 57, &ctr));
105
/* decode the block */
106
DO(ctr_setiv(iv2, l, &ctr));
107
zeromem(tmp, sizeof(tmp));
108
DO(ctr_decrypt(ct, tmp, 57, &ctr));
109
if (memcmp(tmp, pt, 57) != 0) {
110
fprintf(stderr, "CTR failed");
118
/* $Source: /cvs/libtom/libtomcrypt/testprof/modes_test.c,v $ */
119
/* $Revision: 1.6 $ */
120
/* $Date: 2005/05/21 12:51:25 $ */