encrypt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 */
  9. #include <tomcrypt.h>
  10. int errno;
  11. int usage(char *name)
  12. {
  13. int x;
  14. printf("Usage: %s [-d](ecrypt) cipher infile outfile\nCiphers:\n", name);
  15. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  16. printf("%s\n",cipher_descriptor[x].name);
  17. }
  18. exit(1);
  19. }
  20. void register_algs(void)
  21. {
  22. int x;
  23. #ifdef LTC_RIJNDAEL
  24. register_cipher (&aes_desc);
  25. #endif
  26. #ifdef LTC_BLOWFISH
  27. register_cipher (&blowfish_desc);
  28. #endif
  29. #ifdef LTC_XTEA
  30. register_cipher (&xtea_desc);
  31. #endif
  32. #ifdef LTC_RC5
  33. register_cipher (&rc5_desc);
  34. #endif
  35. #ifdef LTC_RC6
  36. register_cipher (&rc6_desc);
  37. #endif
  38. #ifdef LTC_SAFERP
  39. register_cipher (&saferp_desc);
  40. #endif
  41. #ifdef LTC_TWOFISH
  42. register_cipher (&twofish_desc);
  43. #endif
  44. #ifdef LTC_SAFER
  45. register_cipher (&safer_k64_desc);
  46. register_cipher (&safer_sk64_desc);
  47. register_cipher (&safer_k128_desc);
  48. register_cipher (&safer_sk128_desc);
  49. #endif
  50. #ifdef LTC_RC2
  51. register_cipher (&rc2_desc);
  52. #endif
  53. #ifdef LTC_DES
  54. register_cipher (&des_desc);
  55. register_cipher (&des3_desc);
  56. #endif
  57. #ifdef LTC_CAST5
  58. register_cipher (&cast5_desc);
  59. #endif
  60. #ifdef LTC_NOEKEON
  61. register_cipher (&noekeon_desc);
  62. #endif
  63. #ifdef LTC_SKIPJACK
  64. register_cipher (&skipjack_desc);
  65. #endif
  66. #ifdef LTC_KHAZAD
  67. register_cipher (&khazad_desc);
  68. #endif
  69. #ifdef LTC_ANUBIS
  70. register_cipher (&anubis_desc);
  71. #endif
  72. if (register_hash(&sha256_desc) == -1) {
  73. printf("Error registering LTC_SHA256\n");
  74. exit(-1);
  75. }
  76. if (register_prng(&yarrow_desc) == -1) {
  77. printf("Error registering yarrow PRNG\n");
  78. exit(-1);
  79. }
  80. if (register_prng(&sprng_desc) == -1) {
  81. printf("Error registering sprng PRNG\n");
  82. exit(-1);
  83. }
  84. }
  85. int main(int argc, char *argv[])
  86. {
  87. unsigned char plaintext[512],ciphertext[512];
  88. unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
  89. unsigned char inbuf[512]; /* i/o block size */
  90. unsigned long outlen, y, ivsize, x, decrypt;
  91. symmetric_CTR ctr;
  92. int cipher_idx, hash_idx, ks;
  93. char *infile, *outfile, *cipher;
  94. prng_state prng;
  95. FILE *fdin, *fdout;
  96. /* register algs, so they can be printed */
  97. register_algs();
  98. if (argc < 4) {
  99. return usage(argv[0]);
  100. }
  101. if (!strcmp(argv[1], "-d")) {
  102. decrypt = 1;
  103. cipher = argv[2];
  104. infile = argv[3];
  105. outfile = argv[4];
  106. } else {
  107. decrypt = 0;
  108. cipher = argv[1];
  109. infile = argv[2];
  110. outfile = argv[3];
  111. }
  112. /* file handles setup */
  113. fdin = fopen(infile,"rb");
  114. if (fdin == NULL) {
  115. perror("Can't open input for reading");
  116. exit(-1);
  117. }
  118. fdout = fopen(outfile,"wb");
  119. if (fdout == NULL) {
  120. perror("Can't open output for writing");
  121. exit(-1);
  122. }
  123. cipher_idx = find_cipher(cipher);
  124. if (cipher_idx == -1) {
  125. printf("Invalid cipher entered on command line.\n");
  126. exit(-1);
  127. }
  128. hash_idx = find_hash("sha256");
  129. if (hash_idx == -1) {
  130. printf("LTC_SHA256 not found...?\n");
  131. exit(-1);
  132. }
  133. ivsize = cipher_descriptor[cipher_idx].block_length;
  134. ks = hash_descriptor[hash_idx].hashsize;
  135. if (cipher_descriptor[cipher_idx].keysize(&ks) != CRYPT_OK) {
  136. printf("Invalid keysize???\n");
  137. exit(-1);
  138. }
  139. printf("\nEnter key: ");
  140. fgets((char *)tmpkey,sizeof(tmpkey), stdin);
  141. outlen = sizeof(key);
  142. if ((errno = hash_memory(hash_idx,tmpkey,strlen((char *)tmpkey),key,&outlen)) != CRYPT_OK) {
  143. printf("Error hashing key: %s\n", error_to_string(errno));
  144. exit(-1);
  145. }
  146. if (decrypt) {
  147. /* Need to read in IV */
  148. if (fread(IV,1,ivsize,fdin) != ivsize) {
  149. printf("Error reading IV from input.\n");
  150. exit(-1);
  151. }
  152. if ((errno = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
  153. printf("ctr_start error: %s\n",error_to_string(errno));
  154. exit(-1);
  155. }
  156. /* IV done */
  157. do {
  158. y = fread(inbuf,1,sizeof(inbuf),fdin);
  159. if ((errno = ctr_decrypt(inbuf,plaintext,y,&ctr)) != CRYPT_OK) {
  160. printf("ctr_decrypt error: %s\n", error_to_string(errno));
  161. exit(-1);
  162. }
  163. if (fwrite(plaintext,1,y,fdout) != y) {
  164. printf("Error writing to file.\n");
  165. exit(-1);
  166. }
  167. } while (y == sizeof(inbuf));
  168. fclose(fdin);
  169. fclose(fdout);
  170. } else { /* encrypt */
  171. /* Setup yarrow for random bytes for IV */
  172. if ((errno = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
  173. printf("Error setting up PRNG, %s\n", error_to_string(errno));
  174. }
  175. /* You can use rng_get_bytes on platforms that support it */
  176. /* x = rng_get_bytes(IV,ivsize,NULL);*/
  177. x = yarrow_read(IV,ivsize,&prng);
  178. if (x != ivsize) {
  179. printf("Error reading PRNG for IV required.\n");
  180. exit(-1);
  181. }
  182. if (fwrite(IV,1,ivsize,fdout) != ivsize) {
  183. printf("Error writing IV to output.\n");
  184. exit(-1);
  185. }
  186. if ((errno = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
  187. printf("ctr_start error: %s\n",error_to_string(errno));
  188. exit(-1);
  189. }
  190. do {
  191. y = fread(inbuf,1,sizeof(inbuf),fdin);
  192. if ((errno = ctr_encrypt(inbuf,ciphertext,y,&ctr)) != CRYPT_OK) {
  193. printf("ctr_encrypt error: %s\n", error_to_string(errno));
  194. exit(-1);
  195. }
  196. if (fwrite(ciphertext,1,y,fdout) != y) {
  197. printf("Error writing to output.\n");
  198. exit(-1);
  199. }
  200. } while (y == sizeof(inbuf));
  201. fclose(fdout);
  202. fclose(fdin);
  203. }
  204. return 0;
  205. }
  206. /* $Source$ */
  207. /* $Revision$ */
  208. /* $Date$ */