annotate demos/test/modes_test.c @ 89:cecb105ff479 libtomcrypt

Fix for big-endian load/store macros
author Matt Johnston <matt@ucc.asn.au>
date Fri, 03 Sep 2004 08:31:12 +0000
parents 6362d3854bb4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* test CFB/OFB/CBC modes */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #include "test.h"
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 int modes_test(void)
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 int x, cipher_idx;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 symmetric_CBC cbc;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 symmetric_CFB cfb;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 symmetric_OFB ofb;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 symmetric_CTR ctr;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 unsigned long l;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 /* make a random pt, key and iv */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 yarrow_read(pt, 64, &test_yarrow);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 yarrow_read(key, 16, &test_yarrow);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 yarrow_read(iv, 16, &test_yarrow);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 /* get idx of AES handy */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 cipher_idx = find_cipher("aes");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 if (cipher_idx == -1) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 printf("test requires AES");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 return 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 /* test CBC mode */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 /* encode the block */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 l = sizeof(iv2);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 DO(cbc_getiv(iv2, &l, &cbc));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 if (l != 16 || memcmp(iv2, iv, 16)) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 printf("cbc_getiv failed");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 return 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 for (x = 0; x < 4; x++) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 DO(cbc_encrypt(pt+x*16, ct+x*16, &cbc));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 /* decode the block */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 DO(cbc_setiv(iv2, l, &cbc));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 zeromem(tmp, sizeof(tmp));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 for (x = 0; x < 4; x++) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 DO(cbc_decrypt(ct+x*16, tmp+x*16, &cbc));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 if (memcmp(tmp, pt, 64) != 0) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 printf("CBC failed");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 return 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 /* test CFB mode */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 /* encode the block */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 l = sizeof(iv2);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 DO(cfb_getiv(iv2, &l, &cfb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if (l != 16) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 printf("cfb_getiv failed");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 return 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 DO(cfb_encrypt(pt, ct, 64, &cfb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 /* decode the block */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 DO(cfb_setiv(iv, l, &cfb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 zeromem(tmp, sizeof(tmp));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 DO(cfb_decrypt(ct, tmp, 64, &cfb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 if (memcmp(tmp, pt, 64) != 0) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 printf("CFB failed");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 return 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 /* test OFB mode */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 /* encode the block */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 l = sizeof(iv2);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 DO(ofb_getiv(iv2, &l, &ofb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 if (l != 16 || memcmp(iv2, iv, 16)) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 printf("ofb_getiv failed");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 return 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 DO(ofb_encrypt(pt, ct, 64, &ofb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 /* decode the block */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 DO(ofb_setiv(iv2, l, &ofb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 zeromem(tmp, sizeof(tmp));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 DO(ofb_decrypt(ct, tmp, 64, &ofb));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 if (memcmp(tmp, pt, 64) != 0) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 printf("OFB failed");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 return 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 /* test CTR mode */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 /* encode the block */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 DO(ctr_start(cipher_idx, iv, key, 16, 0, &ctr));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 l = sizeof(iv2);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 DO(ctr_getiv(iv2, &l, &ctr));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 if (l != 16 || memcmp(iv2, iv, 16)) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 printf("ctr_getiv failed");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 return 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 DO(ctr_encrypt(pt, ct, 64, &ctr));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 /* decode the block */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 DO(ctr_setiv(iv2, l, &ctr));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 zeromem(tmp, sizeof(tmp));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 DO(ctr_decrypt(ct, tmp, 64, &ctr));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 if (memcmp(tmp, pt, 64) != 0) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 printf("CTR failed");
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 return 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 return 0;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 }