2
0

crypt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #ifdef LTC_RIJNDAEL
  25. register_cipher (&aes_desc);
  26. #endif
  27. #ifdef LTC_BLOWFISH
  28. register_cipher (&blowfish_desc);
  29. #endif
  30. #ifdef LTC_XTEA
  31. register_cipher (&xtea_desc);
  32. #endif
  33. #ifdef LTC_RC5
  34. register_cipher (&rc5_desc);
  35. #endif
  36. #ifdef LTC_RC6
  37. register_cipher (&rc6_desc);
  38. #endif
  39. #ifdef LTC_SAFERP
  40. register_cipher (&saferp_desc);
  41. #endif
  42. #ifdef LTC_TWOFISH
  43. register_cipher (&twofish_desc);
  44. #endif
  45. #ifdef LTC_SAFER
  46. register_cipher (&safer_k64_desc);
  47. register_cipher (&safer_sk64_desc);
  48. register_cipher (&safer_k128_desc);
  49. register_cipher (&safer_sk128_desc);
  50. #endif
  51. #ifdef LTC_RC2
  52. register_cipher (&rc2_desc);
  53. #endif
  54. #ifdef LTC_DES
  55. register_cipher (&des_desc);
  56. register_cipher (&des3_desc);
  57. #endif
  58. #ifdef LTC_CAST5
  59. register_cipher (&cast5_desc);
  60. #endif
  61. #ifdef LTC_NOEKEON
  62. register_cipher (&noekeon_desc);
  63. #endif
  64. #ifdef LTC_SKIPJACK
  65. register_cipher (&skipjack_desc);
  66. #endif
  67. #ifdef LTC_KHAZAD
  68. register_cipher (&khazad_desc);
  69. #endif
  70. #ifdef LTC_ANUBIS
  71. register_cipher (&anubis_desc);
  72. #endif
  73. if (register_hash(&sha256_desc) == -1) {
  74. printf("Error registering LTC_SHA256\n");
  75. exit(-1);
  76. }
  77. if (register_prng(&yarrow_desc) == -1) {
  78. printf("Error registering yarrow PRNG\n");
  79. exit(-1);
  80. }
  81. if (register_prng(&sprng_desc) == -1) {
  82. printf("Error registering sprng PRNG\n");
  83. exit(-1);
  84. }
  85. }
  86. int main(int argc, char *argv[])
  87. {
  88. unsigned char plaintext[512],ciphertext[512];
  89. unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
  90. unsigned char inbuf[512]; /* i/o block size */
  91. unsigned long outlen, y, ivsize, x, decrypt;
  92. symmetric_CTR ctr;
  93. int cipher_idx, hash_idx, ks;
  94. char *infile, *outfile, *cipher;
  95. prng_state prng;
  96. FILE *fdin, *fdout;
  97. /* register algs, so they can be printed */
  98. register_algs();
  99. if (argc < 4) {
  100. if ((argc > 2) && (!strcmp(argv[1], "-t"))) {
  101. cipher = argv[2];
  102. cipher_idx = find_cipher(cipher);
  103. if (cipher_idx == -1) {
  104. printf("Invalid cipher %s entered on command line.\n", cipher);
  105. exit(-1);
  106. } /* if */
  107. if (cipher_descriptor[cipher_idx].test)
  108. {
  109. if (cipher_descriptor[cipher_idx].test() != CRYPT_OK)
  110. {
  111. printf("Error when testing cipher %s.\n", cipher);
  112. exit(-1);
  113. }
  114. else
  115. {
  116. printf("Testing cipher %s succeeded.\n", cipher);
  117. exit(0);
  118. } /* if ... else */
  119. } /* if */
  120. }
  121. return usage(argv[0]);
  122. }
  123. if (!strcmp(argv[1], "-d")) {
  124. decrypt = 1;
  125. cipher = argv[2];
  126. infile = argv[3];
  127. outfile = argv[4];
  128. } else {
  129. decrypt = 0;
  130. cipher = argv[1];
  131. infile = argv[2];
  132. outfile = argv[3];
  133. }
  134. /* file handles setup */
  135. fdin = fopen(infile,"rb");
  136. if (fdin == NULL) {
  137. perror("Can't open input for reading");
  138. exit(-1);
  139. }
  140. fdout = fopen(outfile,"wb");
  141. if (fdout == NULL) {
  142. perror("Can't open output for writing");
  143. exit(-1);
  144. }
  145. cipher_idx = find_cipher(cipher);
  146. if (cipher_idx == -1) {
  147. printf("Invalid cipher entered on command line.\n");
  148. exit(-1);
  149. }
  150. hash_idx = find_hash("sha256");
  151. if (hash_idx == -1) {
  152. printf("LTC_SHA256 not found...?\n");
  153. exit(-1);
  154. }
  155. ivsize = cipher_descriptor[cipher_idx].block_length;
  156. ks = hash_descriptor[hash_idx].hashsize;
  157. if (cipher_descriptor[cipher_idx].keysize(&ks) != CRYPT_OK) {
  158. printf("Invalid keysize???\n");
  159. exit(-1);
  160. }
  161. printf("\nEnter key: ");
  162. if(fgets((char *)tmpkey,sizeof(tmpkey), stdin) == NULL)
  163. exit(-1);
  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$ */