encrypt.c 5.7 KB

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