comparison demos/tv_gen.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 7faae8f46238
children 1c15b283127b
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
1 #include <mycrypt.h> 1 #include <mycrypt.h>
2 2
3 void reg_algs(void) 3 void reg_algs(void)
4 { 4 {
5 int err;
6
5 #ifdef RIJNDAEL 7 #ifdef RIJNDAEL
6 register_cipher (&aes_desc); 8 register_cipher (&aes_desc);
7 #endif 9 #endif
8 #ifdef BLOWFISH 10 #ifdef BLOWFISH
9 register_cipher (&blowfish_desc); 11 register_cipher (&blowfish_desc);
80 register_hash (&rmd160_desc); 82 register_hash (&rmd160_desc);
81 #endif 83 #endif
82 #ifdef WHIRLPOOL 84 #ifdef WHIRLPOOL
83 register_hash (&whirlpool_desc); 85 register_hash (&whirlpool_desc);
84 #endif 86 #endif
87 #ifdef CHC_HASH
88 register_hash(&chc_desc);
89 if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) {
90 printf("chc_register error: %s\n", error_to_string(err));
91 exit(EXIT_FAILURE);
92 }
93 #endif
94
85 } 95 }
86 96
87 void hash_gen(void) 97 void hash_gen(void)
88 { 98 {
89 unsigned char md[MAXBLOCKSIZE], buf[MAXBLOCKSIZE*2+2]; 99 unsigned char md[MAXBLOCKSIZE], *buf;
90 unsigned long outlen, x, y, z; 100 unsigned long outlen, x, y, z;
91 FILE *out; 101 FILE *out;
102 int err;
92 103
93 out = fopen("hash_tv.txt", "w"); 104 out = fopen("hash_tv.txt", "w");
105 if (out == NULL) {
106 perror("can't open hash_tv");
107 }
94 108
95 fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n"); 109 fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");
96 for (x = 0; hash_descriptor[x].name != NULL; x++) { 110 for (x = 0; hash_descriptor[x].name != NULL; x++) {
111 buf = XMALLOC(2 * hash_descriptor[x].blocksize + 1);
112 if (buf == NULL) {
113 perror("can't alloc mem");
114 exit(EXIT_FAILURE);
115 }
97 fprintf(out, "Hash: %s\n", hash_descriptor[x].name); 116 fprintf(out, "Hash: %s\n", hash_descriptor[x].name);
98
99 for (y = 0; y <= (hash_descriptor[x].blocksize * 2); y++) { 117 for (y = 0; y <= (hash_descriptor[x].blocksize * 2); y++) {
100 for (z = 0; z < y; z++) { 118 for (z = 0; z < y; z++) {
101 buf[z] = (unsigned char)(z & 255); 119 buf[z] = (unsigned char)(z & 255);
102 } 120 }
103 outlen = sizeof(md); 121 outlen = sizeof(md);
104 hash_memory(x, buf, y, md, &outlen); 122 if ((err = hash_memory(x, buf, y, md, &outlen)) != CRYPT_OK) {
123 printf("hash_memory error: %s\n", error_to_string(err));
124 exit(EXIT_FAILURE);
125 }
105 fprintf(out, "%3lu: ", y); 126 fprintf(out, "%3lu: ", y);
106 for (z = 0; z < outlen; z++) { 127 for (z = 0; z < outlen; z++) {
107 fprintf(out, "%02X", md[z]); 128 fprintf(out, "%02X", md[z]);
108 } 129 }
109 fprintf(out, "\n"); 130 fprintf(out, "\n");
110 } 131 }
111 fprintf(out, "\n"); 132 fprintf(out, "\n");
133 XFREE(buf);
112 } 134 }
113 fclose(out); 135 fclose(out);
114 } 136 }
115 137
116 void cipher_gen(void) 138 void cipher_gen(void)
117 { 139 {
118 unsigned char key[MAXBLOCKSIZE], pt[MAXBLOCKSIZE]; 140 unsigned char *key, pt[MAXBLOCKSIZE];
119 unsigned long x, y, z, w; 141 unsigned long x, y, z, w;
120 int kl, lastkl; 142 int err, kl, lastkl;
121 FILE *out; 143 FILE *out;
122 symmetric_key skey; 144 symmetric_key skey;
123 145
124 out = fopen("cipher_tv.txt", "w"); 146 out = fopen("cipher_tv.txt", "w");
125 147
136 switch (y) { 158 switch (y) {
137 case 0: kl = cipher_descriptor[x].min_key_length; break; 159 case 0: kl = cipher_descriptor[x].min_key_length; break;
138 case 1: kl = (cipher_descriptor[x].min_key_length + cipher_descriptor[x].max_key_length)/2; break; 160 case 1: kl = (cipher_descriptor[x].min_key_length + cipher_descriptor[x].max_key_length)/2; break;
139 case 2: kl = cipher_descriptor[x].max_key_length; break; 161 case 2: kl = cipher_descriptor[x].max_key_length; break;
140 } 162 }
141 cipher_descriptor[x].keysize(&kl); 163 if ((err = cipher_descriptor[x].keysize(&kl)) != CRYPT_OK) {
164 printf("keysize error: %s\n", error_to_string(err));
165 exit(EXIT_FAILURE);
166 }
142 if (kl == lastkl) break; 167 if (kl == lastkl) break;
143 lastkl = kl; 168 lastkl = kl;
144 fprintf(out, "Key Size: %d bytes\n", kl); 169 fprintf(out, "Key Size: %d bytes\n", kl);
145 170
171 key = XMALLOC(kl);
172 if (key == NULL) {
173 perror("can't malloc memory");
174 exit(EXIT_FAILURE);
175 }
176
146 for (z = 0; (int)z < kl; z++) { 177 for (z = 0; (int)z < kl; z++) {
147 key[z] = (unsigned char)z; 178 key[z] = (unsigned char)z;
148 } 179 }
149 cipher_descriptor[x].setup(key, kl, 0, &skey); 180 if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
181 printf("setup error: %s\n", error_to_string(err));
182 exit(EXIT_FAILURE);
183 }
150 184
151 for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) { 185 for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
152 pt[z] = (unsigned char)z; 186 pt[z] = (unsigned char)z;
153 } 187 }
154 for (w = 0; w < 50; w++) { 188 for (w = 0; w < 50; w++) {
161 195
162 /* reschedule a new key */ 196 /* reschedule a new key */
163 for (z = 0; z < (unsigned long)kl; z++) { 197 for (z = 0; z < (unsigned long)kl; z++) {
164 key[z] = pt[z % cipher_descriptor[x].block_length]; 198 key[z] = pt[z % cipher_descriptor[x].block_length];
165 } 199 }
166 cipher_descriptor[x].setup(key, kl, 0, &skey); 200 if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
167 } 201 printf("cipher setup2 error: %s\n", error_to_string(err));
168 fprintf(out, "\n"); 202 exit(EXIT_FAILURE);
203 }
204 }
205 fprintf(out, "\n");
206 XFREE(key);
169 } 207 }
170 fprintf(out, "\n"); 208 fprintf(out, "\n");
171 } 209 }
172 fclose(out); 210 fclose(out);
173 } 211 }
174 212
175 void hmac_gen(void) 213 void hmac_gen(void)
176 { 214 {
177 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2]; 215 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], *input;
178 int x, y, z, kl, err; 216 int x, y, z, err;
179 FILE *out; 217 FILE *out;
180 unsigned long len; 218 unsigned long len;
181 219
182 out = fopen("hmac_tv.txt", "w"); 220 out = fopen("hmac_tv.txt", "w");
183 221
191 229
192 /* initial key */ 230 /* initial key */
193 for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) { 231 for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) {
194 key[y] = (y&255); 232 key[y] = (y&255);
195 } 233 }
234
235 input = XMALLOC(hash_descriptor[x].blocksize * 2 + 1);
236 if (input == NULL) {
237 perror("Can't malloc memory");
238 exit(EXIT_FAILURE);
239 }
196 240
197 for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) { 241 for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) {
198 for (z = 0; z < y; z++) { 242 for (z = 0; z < y; z++) {
199 input[z] = (unsigned char)(z & 255); 243 input[z] = (unsigned char)(z & 255);
200 } 244 }
210 fprintf(out, "\n"); 254 fprintf(out, "\n");
211 255
212 /* forward the key */ 256 /* forward the key */
213 memcpy(key, output, hash_descriptor[x].hashsize); 257 memcpy(key, output, hash_descriptor[x].hashsize);
214 } 258 }
259 XFREE(input);
215 fprintf(out, "\n"); 260 fprintf(out, "\n");
216 } 261 }
217 fclose(out); 262 fclose(out);
218 } 263 }
219 264