15
|
1 /* test CFB/OFB/CBC modes */ |
|
2 #include "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 x, 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, &test_yarrow); |
|
16 yarrow_read(key, 16, &test_yarrow); |
|
17 yarrow_read(iv, 16, &test_yarrow); |
|
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 /* test CBC mode */ |
|
27 /* encode the block */ |
|
28 DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc)); |
|
29 l = sizeof(iv2); |
|
30 DO(cbc_getiv(iv2, &l, &cbc)); |
|
31 if (l != 16 || memcmp(iv2, iv, 16)) { |
|
32 printf("cbc_getiv failed"); |
|
33 return 1; |
|
34 } |
|
35 for (x = 0; x < 4; x++) { |
|
36 DO(cbc_encrypt(pt+x*16, ct+x*16, &cbc)); |
|
37 } |
|
38 |
|
39 /* decode the block */ |
|
40 DO(cbc_setiv(iv2, l, &cbc)); |
|
41 zeromem(tmp, sizeof(tmp)); |
|
42 for (x = 0; x < 4; x++) { |
|
43 DO(cbc_decrypt(ct+x*16, tmp+x*16, &cbc)); |
|
44 } |
|
45 if (memcmp(tmp, pt, 64) != 0) { |
|
46 printf("CBC failed"); |
|
47 return 1; |
|
48 } |
|
49 |
|
50 /* test CFB mode */ |
|
51 /* encode the block */ |
|
52 DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb)); |
|
53 l = sizeof(iv2); |
|
54 DO(cfb_getiv(iv2, &l, &cfb)); |
|
55 /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */ |
|
56 if (l != 16) { |
|
57 printf("cfb_getiv failed"); |
|
58 return 1; |
|
59 } |
|
60 DO(cfb_encrypt(pt, ct, 64, &cfb)); |
|
61 |
|
62 /* decode the block */ |
|
63 DO(cfb_setiv(iv, l, &cfb)); |
|
64 zeromem(tmp, sizeof(tmp)); |
|
65 DO(cfb_decrypt(ct, tmp, 64, &cfb)); |
|
66 if (memcmp(tmp, pt, 64) != 0) { |
|
67 printf("CFB failed"); |
|
68 return 1; |
|
69 } |
|
70 |
|
71 /* test OFB mode */ |
|
72 /* encode the block */ |
|
73 DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb)); |
|
74 l = sizeof(iv2); |
|
75 DO(ofb_getiv(iv2, &l, &ofb)); |
|
76 if (l != 16 || memcmp(iv2, iv, 16)) { |
|
77 printf("ofb_getiv failed"); |
|
78 return 1; |
|
79 } |
|
80 DO(ofb_encrypt(pt, ct, 64, &ofb)); |
|
81 |
|
82 /* decode the block */ |
|
83 DO(ofb_setiv(iv2, l, &ofb)); |
|
84 zeromem(tmp, sizeof(tmp)); |
|
85 DO(ofb_decrypt(ct, tmp, 64, &ofb)); |
|
86 if (memcmp(tmp, pt, 64) != 0) { |
|
87 printf("OFB failed"); |
|
88 return 1; |
|
89 } |
|
90 |
|
91 /* test CTR mode */ |
|
92 /* encode the block */ |
|
93 DO(ctr_start(cipher_idx, iv, key, 16, 0, &ctr)); |
|
94 l = sizeof(iv2); |
|
95 DO(ctr_getiv(iv2, &l, &ctr)); |
|
96 if (l != 16 || memcmp(iv2, iv, 16)) { |
|
97 printf("ctr_getiv failed"); |
|
98 return 1; |
|
99 } |
|
100 DO(ctr_encrypt(pt, ct, 64, &ctr)); |
|
101 |
|
102 /* decode the block */ |
|
103 DO(ctr_setiv(iv2, l, &ctr)); |
|
104 zeromem(tmp, sizeof(tmp)); |
|
105 DO(ctr_decrypt(ct, tmp, 64, &ctr)); |
|
106 if (memcmp(tmp, pt, 64) != 0) { |
|
107 printf("CTR failed"); |
|
108 return 1; |
|
109 } |
|
110 |
|
111 return 0; |
|
112 } |