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];
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
);
24 /* get idx of AES handy */
25 cipher_idx
= find_cipher("aes");
26 if (cipher_idx
== -1) {
27 fprintf(stderr
, "test requires AES");
41 /* encode the block */
42 DO(cbc_start(cipher_idx
, iv
, key
, 16, 0, &cbc
));
44 DO(cbc_getiv(iv2
, &l
, &cbc
));
45 if (l
!= 16 || memcmp(iv2
, iv
, 16)) {
46 fprintf(stderr
, "cbc_getiv failed");
49 DO(cbc_encrypt(pt
, ct
, 64, &cbc
));
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");
63 /* encode the block */
64 DO(cfb_start(cipher_idx
, iv
, key
, 16, 0, &cfb
));
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 */
69 fprintf(stderr
, "cfb_getiv failed");
72 DO(cfb_encrypt(pt
, ct
, 64, &cfb
));
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");
86 /* encode the block */
87 DO(ofb_start(cipher_idx
, iv
, key
, 16, 0, &ofb
));
89 DO(ofb_getiv(iv2
, &l
, &ofb
));
90 if (l
!= 16 || memcmp(iv2
, iv
, 16)) {
91 fprintf(stderr
, "ofb_getiv failed");
94 DO(ofb_encrypt(pt
, ct
, 64, &ofb
));
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");
113 /* $Source: /cvs/libtom/libtomcrypt/testprof/modes_test.c,v $ */
114 /* $Revision: 1.14 $ */
115 /* $Date: 2006/11/13 11:55:25 $ */