Mercurial > dropbear
comparison testprof/modes_test.c @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children | 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
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 printf("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 printf("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 printf("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 printf("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 printf("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 printf("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 printf("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)); | |
97 l = sizeof(iv2); | |
98 DO(ctr_getiv(iv2, &l, &ctr)); | |
99 if (l != 16 || memcmp(iv2, iv, 16)) { | |
100 printf("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 printf("CTR failed"); | |
111 return 1; | |
112 } | |
113 #endif | |
114 | |
115 return 0; | |
116 } |