Mercurial > dropbear
comparison testprof/modes_test.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05
Import of libtomcrypt 1.05
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 06 Jul 2005 03:53:40 +0000 |
parents | 1c15b283127b |
children |
comparison
equal
deleted
inserted
replaced
191:1c15b283127b | 209:39d5d58461d6 |
---|---|
17 yarrow_read(iv, 16, &yarrow_prng); | 17 yarrow_read(iv, 16, &yarrow_prng); |
18 | 18 |
19 /* get idx of AES handy */ | 19 /* get idx of AES handy */ |
20 cipher_idx = find_cipher("aes"); | 20 cipher_idx = find_cipher("aes"); |
21 if (cipher_idx == -1) { | 21 if (cipher_idx == -1) { |
22 printf("test requires AES"); | 22 fprintf(stderr, "test requires AES"); |
23 return 1; | 23 return 1; |
24 } | 24 } |
25 | 25 |
26 #ifdef CBC | 26 #ifdef CBC |
27 /* test CBC mode */ | 27 /* test CBC mode */ |
28 /* encode the block */ | 28 /* encode the block */ |
29 DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc)); | 29 DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc)); |
30 l = sizeof(iv2); | 30 l = sizeof(iv2); |
31 DO(cbc_getiv(iv2, &l, &cbc)); | 31 DO(cbc_getiv(iv2, &l, &cbc)); |
32 if (l != 16 || memcmp(iv2, iv, 16)) { | 32 if (l != 16 || memcmp(iv2, iv, 16)) { |
33 printf("cbc_getiv failed"); | 33 fprintf(stderr, "cbc_getiv failed"); |
34 return 1; | 34 return 1; |
35 } | 35 } |
36 DO(cbc_encrypt(pt, ct, 64, &cbc)); | 36 DO(cbc_encrypt(pt, ct, 64, &cbc)); |
37 | 37 |
38 /* decode the block */ | 38 /* decode the block */ |
39 DO(cbc_setiv(iv2, l, &cbc)); | 39 DO(cbc_setiv(iv2, l, &cbc)); |
40 zeromem(tmp, sizeof(tmp)); | 40 zeromem(tmp, sizeof(tmp)); |
41 DO(cbc_decrypt(ct, tmp, 64, &cbc)); | 41 DO(cbc_decrypt(ct, tmp, 64, &cbc)); |
42 if (memcmp(tmp, pt, 64) != 0) { | 42 if (memcmp(tmp, pt, 64) != 0) { |
43 printf("CBC failed"); | 43 fprintf(stderr, "CBC failed"); |
44 return 1; | 44 return 1; |
45 } | 45 } |
46 #endif | 46 #endif |
47 | 47 |
48 #ifdef CFB | 48 #ifdef CFB |
51 DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb)); | 51 DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb)); |
52 l = sizeof(iv2); | 52 l = sizeof(iv2); |
53 DO(cfb_getiv(iv2, &l, &cfb)); | 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 */ | 54 /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */ |
55 if (l != 16) { | 55 if (l != 16) { |
56 printf("cfb_getiv failed"); | 56 fprintf(stderr, "cfb_getiv failed"); |
57 return 1; | 57 return 1; |
58 } | 58 } |
59 DO(cfb_encrypt(pt, ct, 64, &cfb)); | 59 DO(cfb_encrypt(pt, ct, 64, &cfb)); |
60 | 60 |
61 /* decode the block */ | 61 /* decode the block */ |
62 DO(cfb_setiv(iv, l, &cfb)); | 62 DO(cfb_setiv(iv, l, &cfb)); |
63 zeromem(tmp, sizeof(tmp)); | 63 zeromem(tmp, sizeof(tmp)); |
64 DO(cfb_decrypt(ct, tmp, 64, &cfb)); | 64 DO(cfb_decrypt(ct, tmp, 64, &cfb)); |
65 if (memcmp(tmp, pt, 64) != 0) { | 65 if (memcmp(tmp, pt, 64) != 0) { |
66 printf("CFB failed"); | 66 fprintf(stderr, "CFB failed"); |
67 return 1; | 67 return 1; |
68 } | 68 } |
69 #endif | 69 #endif |
70 | 70 |
71 #ifdef OFB | 71 #ifdef OFB |
73 /* encode the block */ | 73 /* encode the block */ |
74 DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb)); | 74 DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb)); |
75 l = sizeof(iv2); | 75 l = sizeof(iv2); |
76 DO(ofb_getiv(iv2, &l, &ofb)); | 76 DO(ofb_getiv(iv2, &l, &ofb)); |
77 if (l != 16 || memcmp(iv2, iv, 16)) { | 77 if (l != 16 || memcmp(iv2, iv, 16)) { |
78 printf("ofb_getiv failed"); | 78 fprintf(stderr, "ofb_getiv failed"); |
79 return 1; | 79 return 1; |
80 } | 80 } |
81 DO(ofb_encrypt(pt, ct, 64, &ofb)); | 81 DO(ofb_encrypt(pt, ct, 64, &ofb)); |
82 | 82 |
83 /* decode the block */ | 83 /* decode the block */ |
84 DO(ofb_setiv(iv2, l, &ofb)); | 84 DO(ofb_setiv(iv2, l, &ofb)); |
85 zeromem(tmp, sizeof(tmp)); | 85 zeromem(tmp, sizeof(tmp)); |
86 DO(ofb_decrypt(ct, tmp, 64, &ofb)); | 86 DO(ofb_decrypt(ct, tmp, 64, &ofb)); |
87 if (memcmp(tmp, pt, 64) != 0) { | 87 if (memcmp(tmp, pt, 64) != 0) { |
88 printf("OFB failed"); | 88 fprintf(stderr, "OFB failed"); |
89 return 1; | 89 return 1; |
90 } | 90 } |
91 #endif | 91 #endif |
92 | 92 |
93 #ifdef CTR | 93 #ifdef CTR |
94 /* test CTR mode */ | 94 /* test CTR mode */ |
95 /* encode the block */ | 95 /* encode the block */ |
96 DO(ctr_start(cipher_idx, iv, key, 16, 0, &ctr)); | 96 DO(ctr_start(cipher_idx, iv, key, 16, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr)); |
97 l = sizeof(iv2); | 97 l = sizeof(iv2); |
98 DO(ctr_getiv(iv2, &l, &ctr)); | 98 DO(ctr_getiv(iv2, &l, &ctr)); |
99 if (l != 16 || memcmp(iv2, iv, 16)) { | 99 if (l != 16 || memcmp(iv2, iv, 16)) { |
100 printf("ctr_getiv failed"); | 100 fprintf(stderr, "ctr_getiv failed"); |
101 return 1; | 101 return 1; |
102 } | 102 } |
103 DO(ctr_encrypt(pt, ct, 57, &ctr)); | 103 DO(ctr_encrypt(pt, ct, 57, &ctr)); |
104 | 104 |
105 /* decode the block */ | 105 /* decode the block */ |
106 DO(ctr_setiv(iv2, l, &ctr)); | 106 DO(ctr_setiv(iv2, l, &ctr)); |
107 zeromem(tmp, sizeof(tmp)); | 107 zeromem(tmp, sizeof(tmp)); |
108 DO(ctr_decrypt(ct, tmp, 57, &ctr)); | 108 DO(ctr_decrypt(ct, tmp, 57, &ctr)); |
109 if (memcmp(tmp, pt, 57) != 0) { | 109 if (memcmp(tmp, pt, 57) != 0) { |
110 printf("CTR failed"); | 110 fprintf(stderr, "CTR failed"); |
111 return 1; | 111 return 1; |
112 } | 112 } |
113 #endif | 113 #endif |
114 | 114 |
115 return 0; | 115 return 0; |
116 } | 116 } |
117 | |
118 /* $Source: /cvs/libtom/libtomcrypt/testprof/modes_test.c,v $ */ | |
119 /* $Revision: 1.6 $ */ | |
120 /* $Date: 2005/05/21 12:51:25 $ */ |