comparison demos/encrypt.c @ 0:d7da3b1e1540 libtomcrypt

put back the 0.95 makefile which was inadvertently merged over
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:21:40 +0000
parents
children 1c15b283127b
comparison
equal deleted inserted replaced
-1:000000000000 0:d7da3b1e1540
1 /* encrypt V1.1 Fri Oct 18 04:28:03 NZDT 2002 */
2 /* File de/encryption, using libtomcrypt */
3 /* Written by Daniel Richards <[email protected]> */
4 /* Help from Tom St Denis with various bits */
5 /* This code is public domain, no rights reserved. */
6 /* Encrypts by default, -d flag enables decryption */
7 /* ie: ./encrypt blowfish story.txt story.ct */
8 /* ./encrypt -d blowfish story.ct story.pt */
9
10 #include <mycrypt.h>
11
12 int errno;
13
14 int usage(char *name)
15 {
16 int x;
17
18 printf("Usage: %s [-d](ecrypt) cipher infile outfile\nCiphers:\n", name);
19 for (x = 0; cipher_descriptor[x].name != NULL; x++) {
20 printf("%s\n",cipher_descriptor[x].name);
21 }
22 exit(1);
23 }
24
25 void register_algs(void)
26 {
27 int x;
28
29 #ifdef RIJNDAEL
30 register_cipher (&aes_desc);
31 #endif
32 #ifdef BLOWFISH
33 register_cipher (&blowfish_desc);
34 #endif
35 #ifdef XTEA
36 register_cipher (&xtea_desc);
37 #endif
38 #ifdef RC5
39 register_cipher (&rc5_desc);
40 #endif
41 #ifdef RC6
42 register_cipher (&rc6_desc);
43 #endif
44 #ifdef SAFERP
45 register_cipher (&saferp_desc);
46 #endif
47 #ifdef TWOFISH
48 register_cipher (&twofish_desc);
49 #endif
50 #ifdef SAFER
51 register_cipher (&safer_k64_desc);
52 register_cipher (&safer_sk64_desc);
53 register_cipher (&safer_k128_desc);
54 register_cipher (&safer_sk128_desc);
55 #endif
56 #ifdef RC2
57 register_cipher (&rc2_desc);
58 #endif
59 #ifdef DES
60 register_cipher (&des_desc);
61 register_cipher (&des3_desc);
62 #endif
63 #ifdef CAST5
64 register_cipher (&cast5_desc);
65 #endif
66 #ifdef NOEKEON
67 register_cipher (&noekeon_desc);
68 #endif
69 #ifdef SKIPJACK
70 register_cipher (&skipjack_desc);
71 #endif
72
73 if (register_hash(&sha256_desc) == -1) {
74 printf("Error registering SHA256\n");
75 exit(-1);
76 }
77
78 if (register_prng(&yarrow_desc) == -1) {
79 printf("Error registering yarrow PRNG\n");
80 exit(-1);
81 }
82
83 if (register_prng(&sprng_desc) == -1) {
84 printf("Error registering sprng PRNG\n");
85 exit(-1);
86 }
87 }
88
89 int main(int argc, char *argv[])
90 {
91 unsigned char plaintext[512],ciphertext[512];
92 unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
93 unsigned char inbuf[512]; /* i/o block size */
94 unsigned long outlen, y, ivsize, x, decrypt;
95 symmetric_CTR ctr;
96 int cipher_idx, hash_idx, ks;
97 char *infile, *outfile, *cipher;
98 prng_state prng;
99 FILE *fdin, *fdout;
100
101 /* register algs, so they can be printed */
102 register_algs();
103
104 if (argc < 4) {
105 return usage(argv[0]);
106 }
107
108 if (!strcmp(argv[1], "-d")) {
109 decrypt = 1;
110 cipher = argv[2];
111 infile = argv[3];
112 outfile = argv[4];
113 } else {
114 decrypt = 0;
115 cipher = argv[1];
116 infile = argv[2];
117 outfile = argv[3];
118 }
119
120 /* file handles setup */
121 fdin = fopen(infile,"rb");
122 if (fdin == NULL) {
123 perror("Can't open input for reading");
124 exit(-1);
125 }
126
127 fdout = fopen(outfile,"wb");
128 if (fdout == NULL) {
129 perror("Can't open output for writing");
130 exit(-1);
131 }
132
133 cipher_idx = find_cipher(cipher);
134 if (cipher_idx == -1) {
135 printf("Invalid cipher entered on command line.\n");
136 exit(-1);
137 }
138
139 hash_idx = find_hash("sha256");
140 if (hash_idx == -1) {
141 printf("SHA256 not found...?\n");
142 exit(-1);
143 }
144
145 ivsize = cipher_descriptor[cipher_idx].block_length;
146 ks = hash_descriptor[hash_idx].hashsize;
147 if (cipher_descriptor[cipher_idx].keysize(&ks) != CRYPT_OK) {
148 printf("Invalid keysize???\n");
149 exit(-1);
150 }
151
152 printf("\nEnter key: ");
153 fgets((char *)tmpkey,sizeof(tmpkey), stdin);
154 outlen = sizeof(key);
155 if ((errno = hash_memory(hash_idx,tmpkey,strlen((char *)tmpkey),key,&outlen)) != CRYPT_OK) {
156 printf("Error hashing key: %s\n", error_to_string(errno));
157 exit(-1);
158 }
159
160 if (decrypt) {
161 /* Need to read in IV */
162 if (fread(IV,1,ivsize,fdin) != ivsize) {
163 printf("Error reading IV from input.\n");
164 exit(-1);
165 }
166
167 if ((errno = ctr_start(cipher_idx,IV,key,ks,0,&ctr)) != CRYPT_OK) {
168 printf("ctr_start error: %s\n",error_to_string(errno));
169 exit(-1);
170 }
171
172 /* IV done */
173 do {
174 y = fread(inbuf,1,sizeof(inbuf),fdin);
175
176 if ((errno = ctr_decrypt(inbuf,plaintext,y,&ctr)) != CRYPT_OK) {
177 printf("ctr_decrypt error: %s\n", error_to_string(errno));
178 exit(-1);
179 }
180
181 if (fwrite(plaintext,1,y,fdout) != y) {
182 printf("Error writing to file.\n");
183 exit(-1);
184 }
185 } while (y == sizeof(inbuf));
186 fclose(fdin);
187 fclose(fdout);
188
189 } else { /* encrypt */
190 /* Setup yarrow for random bytes for IV */
191
192 if ((errno = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
193 printf("Error setting up PRNG, %s\n", error_to_string(errno));
194 }
195
196 /* You can use rng_get_bytes on platforms that support it */
197 /* x = rng_get_bytes(IV,ivsize,NULL);*/
198 x = yarrow_read(IV,ivsize,&prng);
199 if (x != ivsize) {
200 printf("Error reading PRNG for IV required.\n");
201 exit(-1);
202 }
203
204 if (fwrite(IV,1,ivsize,fdout) != ivsize) {
205 printf("Error writing IV to output.\n");
206 exit(-1);
207 }
208
209 if ((errno = ctr_start(cipher_idx,IV,key,ks,0,&ctr)) != CRYPT_OK) {
210 printf("ctr_start error: %s\n",error_to_string(errno));
211 exit(-1);
212 }
213
214 do {
215 y = fread(inbuf,1,sizeof(inbuf),fdin);
216
217 if ((errno = ctr_encrypt(inbuf,ciphertext,y,&ctr)) != CRYPT_OK) {
218 printf("ctr_encrypt error: %s\n", error_to_string(errno));
219 exit(-1);
220 }
221
222 if (fwrite(ciphertext,1,y,fdout) != y) {
223 printf("Error writing to output.\n");
224 exit(-1);
225 }
226 } while (y == sizeof(inbuf));
227 fclose(fdout);
228 fclose(fdin);
229 }
230 return 0;
231 }