encrypt.c 6.7 KB

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