comparison libtomcrypt/testprof/modes_test.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
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 symmetric_CBC cbc;
9 symmetric_CFB cfb;
10 symmetric_OFB ofb;
11 symmetric_CTR ctr;
12 unsigned long l;
13
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);
18
19 /* get idx of AES handy */
20 cipher_idx = find_cipher("aes");
21 if (cipher_idx == -1) {
22 fprintf(stderr, "test requires AES");
23 return 1;
24 }
25
26 #ifdef CBC
27 /* test CBC mode */
28 /* encode the block */
29 DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
30 l = sizeof(iv2);
31 DO(cbc_getiv(iv2, &l, &cbc));
32 if (l != 16 || memcmp(iv2, iv, 16)) {
33 fprintf(stderr, "cbc_getiv failed");
34 return 1;
35 }
36 DO(cbc_encrypt(pt, ct, 64, &cbc));
37
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");
44 return 1;
45 }
46 #endif
47
48 #ifdef CFB
49 /* test CFB mode */
50 /* encode the block */
51 DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
52 l = sizeof(iv2);
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 */
55 if (l != 16) {
56 fprintf(stderr, "cfb_getiv failed");
57 return 1;
58 }
59 DO(cfb_encrypt(pt, ct, 64, &cfb));
60
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");
67 return 1;
68 }
69 #endif
70
71 #ifdef OFB
72 /* test OFB mode */
73 /* encode the block */
74 DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
75 l = sizeof(iv2);
76 DO(ofb_getiv(iv2, &l, &ofb));
77 if (l != 16 || memcmp(iv2, iv, 16)) {
78 fprintf(stderr, "ofb_getiv failed");
79 return 1;
80 }
81 DO(ofb_encrypt(pt, ct, 64, &ofb));
82
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");
89 return 1;
90 }
91 #endif
92
93 #ifdef CTR
94 /* test CTR mode */
95 /* encode the block */
96 DO(ctr_start(cipher_idx, iv, key, 16, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr));
97 l = sizeof(iv2);
98 DO(ctr_getiv(iv2, &l, &ctr));
99 if (l != 16 || memcmp(iv2, iv, 16)) {
100 fprintf(stderr, "ctr_getiv failed");
101 return 1;
102 }
103 DO(ctr_encrypt(pt, ct, 57, &ctr));
104
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");
111 return 1;
112 }
113 #endif
114
115 return 0;
116 }
117
118 /* $Source: /cvs/libtom/libtomcrypt/testprof/modes_test.c,v $ */
119 /* $Revision: 1.6 $ */
120 /* $Date: 2005/05/21 12:51:25 $ */