1 /* encrypt V1.1 Fri Oct 18 04:28:03 NZDT 2002 */
2 /* File de/encryption, using libtomcrypt */
3 /* Written by Daniel Richards <kyhwana@world-net.co.nz> */
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 */
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);
25 void register_algs(void)
30 register_cipher (&aes_desc);
33 register_cipher (&blowfish_desc);
36 register_cipher (&xtea_desc);
39 register_cipher (&rc5_desc);
42 register_cipher (&rc6_desc);
45 register_cipher (&saferp_desc);
48 register_cipher (&twofish_desc);
51 register_cipher (&safer_k64_desc);
52 register_cipher (&safer_sk64_desc);
53 register_cipher (&safer_k128_desc);
54 register_cipher (&safer_sk128_desc);
57 register_cipher (&rc2_desc);
60 register_cipher (&des_desc);
61 register_cipher (&des3_desc);
64 register_cipher (&cast5_desc);
67 register_cipher (&noekeon_desc);
70 register_cipher (&skipjack_desc);
73 register_cipher (&khazad_desc);
76 register_cipher (&anubis_desc);
79 if (register_hash(&sha256_desc) == -1) {
80 printf("Error registering SHA256\n");
84 if (register_prng(&yarrow_desc) == -1) {
85 printf("Error registering yarrow PRNG\n");
89 if (register_prng(&sprng_desc) == -1) {
90 printf("Error registering sprng PRNG\n");
95 int main(int argc, char *argv[])
97 unsigned char plaintext[512],ciphertext[512];
98 unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
99 unsigned char inbuf[512]; /* i/o block size */
100 unsigned long outlen, y, ivsize, x, decrypt;
102 int cipher_idx, hash_idx, ks;
103 char *infile, *outfile, *cipher;
107 /* register algs, so they can be printed */
111 return usage(argv[0]);
114 if (!strcmp(argv[1], "-d")) {
126 /* file handles setup */
127 fdin = fopen(infile,"rb");
129 perror("Can't open input for reading");
133 fdout = fopen(outfile,"wb");
135 perror("Can't open output for writing");
139 cipher_idx = find_cipher(cipher);
140 if (cipher_idx == -1) {
141 printf("Invalid cipher entered on command line.\n");
145 hash_idx = find_hash("sha256");
146 if (hash_idx == -1) {
147 printf("SHA256 not found...?\n");
151 ivsize = cipher_descriptor[cipher_idx].block_length;
152 ks = hash_descriptor[hash_idx].hashsize;
153 if (cipher_descriptor[cipher_idx].keysize(&ks) != CRYPT_OK) {
154 printf("Invalid keysize???\n");
158 printf("\nEnter key: ");
159 fgets((char *)tmpkey,sizeof(tmpkey), stdin);
160 outlen = sizeof(key);
161 if ((errno = hash_memory(hash_idx,tmpkey,strlen((char *)tmpkey),key,&outlen)) != CRYPT_OK) {
162 printf("Error hashing key: %s\n", error_to_string(errno));
167 /* Need to read in IV */
168 if (fread(IV,1,ivsize,fdin) != ivsize) {
169 printf("Error reading IV from input.\n");
173 if ((errno = ctr_start(cipher_idx,IV,key,ks,0,&ctr)) != CRYPT_OK) {
174 printf("ctr_start error: %s\n",error_to_string(errno));
180 y = fread(inbuf,1,sizeof(inbuf),fdin);
182 if ((errno = ctr_decrypt(inbuf,plaintext,y,&ctr)) != CRYPT_OK) {
183 printf("ctr_decrypt error: %s\n", error_to_string(errno));
187 if (fwrite(plaintext,1,y,fdout) != y) {
188 printf("Error writing to file.\n");
191 } while (y == sizeof(inbuf));
195 } else { /* encrypt */
196 /* Setup yarrow for random bytes for IV */
198 if ((errno = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
199 printf("Error setting up PRNG, %s\n", error_to_string(errno));
202 /* You can use rng_get_bytes on platforms that support it */
203 /* x = rng_get_bytes(IV,ivsize,NULL);*/
204 x = yarrow_read(IV,ivsize,&prng);
206 printf("Error reading PRNG for IV required.\n");
210 if (fwrite(IV,1,ivsize,fdout) != ivsize) {
211 printf("Error writing IV to output.\n");
215 if ((errno = ctr_start(cipher_idx,IV,key,ks,0,&ctr)) != CRYPT_OK) {
216 printf("ctr_start error: %s\n",error_to_string(errno));
221 y = fread(inbuf,1,sizeof(inbuf),fdin);
223 if ((errno = ctr_encrypt(inbuf,ciphertext,y,&ctr)) != CRYPT_OK) {
224 printf("ctr_encrypt error: %s\n", error_to_string(errno));
228 if (fwrite(ciphertext,1,y,fdout) != y) {
229 printf("Error writing to output.\n");
232 } while (y == sizeof(inbuf));
239 /* $Source: /cvs/libtom/libtomcrypt/demos/encrypt.c,v $ */
240 /* $Revision: 1.2 $ */
241 /* $Date: 2005/05/05 14:35:56 $ */