2
0

crypt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. /* encrypt V1.1 Fri Oct 18 04:28:03 NZDT 2002 */
  4. /* File de/encryption, using libtomcrypt */
  5. /* Written by Daniel Richards <[email protected]> */
  6. /* Help from Tom St Denis with various bits */
  7. /* This code is public domain, no rights reserved. */
  8. /* Encrypts by default, -d flag enables decryption */
  9. /* ie: ./encrypt blowfish story.txt story.ct */
  10. /* ./encrypt -d blowfish story.ct story.pt */
  11. #include <tomcrypt.h>
  12. static int LTC_NORETURN die(int status)
  13. {
  14. int x, w, tot = 0;
  15. FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
  16. fprintf(o,
  17. "Usage encrypt: crypt <cipher> <infile> <outfile>\n"
  18. "Usage decrypt: crypt -d <cipher> <infile> <outfile>\n"
  19. "Usage test: crypt -t <cipher>\n"
  20. "This help: crypt -h\n\nCiphers:\n\t");
  21. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  22. w = fprintf(o, "%-14s",cipher_descriptor[x].name);
  23. if (w < 0) {
  24. status = EXIT_FAILURE;
  25. break;
  26. }
  27. tot += w;
  28. if (tot >= 70) {
  29. fprintf(o, "\n\t");
  30. tot = 0;
  31. }
  32. }
  33. if (tot != 0) fprintf(o, "\n");
  34. exit(status);
  35. }
  36. int main(int argc, char *argv[])
  37. {
  38. unsigned char plaintext[512],ciphertext[512];
  39. unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
  40. unsigned char inbuf[512]; /* i/o block size */
  41. unsigned long outlen, y, ivsize, x, decrypt;
  42. symmetric_CTR ctr;
  43. int cipher_idx, hash_idx, ks;
  44. char *infile, *outfile, *cipher;
  45. prng_state prng;
  46. FILE *fdin, *fdout;
  47. int err;
  48. /* register algs, so they can be printed */
  49. register_all_ciphers();
  50. register_all_hashes();
  51. register_all_prngs();
  52. if (argc < 4) {
  53. if ((argc > 2) && (!strcmp(argv[1], "-t"))) {
  54. cipher = argv[2];
  55. cipher_idx = find_cipher(cipher);
  56. if (cipher_idx == -1) {
  57. fprintf(stderr, "Invalid cipher %s entered on command line.\n", cipher);
  58. die(EXIT_FAILURE);
  59. } /* if */
  60. if (cipher_descriptor[cipher_idx].test) {
  61. if (cipher_descriptor[cipher_idx].test() != CRYPT_OK) {
  62. fprintf(stderr, "Error when testing cipher %s.\n", cipher);
  63. die(EXIT_FAILURE);
  64. }
  65. else {
  66. printf("Testing cipher %s succeeded.\n", cipher);
  67. exit(EXIT_SUCCESS);
  68. }
  69. } else {
  70. fprintf(stderr, "Cipher %s has no tests.\n", cipher);
  71. exit(EXIT_SUCCESS);
  72. }
  73. }
  74. return die(argc > 1 && strstr(argv[1], "-h") != NULL ? EXIT_SUCCESS : EXIT_FAILURE);
  75. }
  76. if (!strcmp(argv[1], "-d")) {
  77. decrypt = 1;
  78. cipher = argv[2];
  79. infile = argv[3];
  80. outfile = argv[4];
  81. } else {
  82. decrypt = 0;
  83. cipher = argv[1];
  84. infile = argv[2];
  85. outfile = argv[3];
  86. }
  87. /* file handles setup */
  88. fdin = fopen(infile,"rb");
  89. if (fdin == NULL) {
  90. perror("Can't open input for reading");
  91. exit(-1);
  92. }
  93. fdout = fopen(outfile,"wb");
  94. if (fdout == NULL) {
  95. perror("Can't open output for writing");
  96. exit(-1);
  97. }
  98. cipher_idx = find_cipher(cipher);
  99. if (cipher_idx == -1) {
  100. printf("Invalid cipher entered on command line.\n");
  101. exit(-1);
  102. }
  103. hash_idx = find_hash("sha256");
  104. if (hash_idx == -1) {
  105. printf("LTC_SHA256 not found...?\n");
  106. exit(-1);
  107. }
  108. ivsize = cipher_descriptor[cipher_idx].block_length;
  109. ks = hash_descriptor[hash_idx].hashsize;
  110. if (cipher_descriptor[cipher_idx].keysize(&ks) != CRYPT_OK) {
  111. printf("Invalid keysize???\n");
  112. exit(-1);
  113. }
  114. printf("\nEnter key: ");
  115. if(fgets((char *)tmpkey,sizeof(tmpkey), stdin) == NULL)
  116. exit(-1);
  117. outlen = sizeof(key);
  118. if ((err = hash_memory(hash_idx,tmpkey,XSTRLEN((char *)tmpkey),key,&outlen)) != CRYPT_OK) {
  119. printf("Error hashing key: %s\n", error_to_string(err));
  120. exit(-1);
  121. }
  122. if (decrypt) {
  123. /* Need to read in IV */
  124. if (fread(IV,1,ivsize,fdin) != ivsize) {
  125. printf("Error reading IV from input.\n");
  126. exit(-1);
  127. }
  128. if ((err = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
  129. printf("ctr_start error: %s\n",error_to_string(err));
  130. exit(-1);
  131. }
  132. /* IV done */
  133. do {
  134. y = fread(inbuf,1,sizeof(inbuf),fdin);
  135. if ((err = ctr_decrypt(inbuf,plaintext,y,&ctr)) != CRYPT_OK) {
  136. printf("ctr_decrypt error: %s\n", error_to_string(err));
  137. exit(-1);
  138. }
  139. if (fwrite(plaintext,1,y,fdout) != y) {
  140. printf("Error writing to file.\n");
  141. exit(-1);
  142. }
  143. } while (y == sizeof(inbuf));
  144. fclose(fdin);
  145. fclose(fdout);
  146. } else { /* encrypt */
  147. /* Setup yarrow for random bytes for IV */
  148. if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
  149. printf("Error setting up PRNG, %s\n", error_to_string(err));
  150. }
  151. /* You can use rng_get_bytes on platforms that support it */
  152. /* x = rng_get_bytes(IV,ivsize,NULL);*/
  153. x = yarrow_read(IV,ivsize,&prng);
  154. if (x != ivsize) {
  155. printf("Error reading PRNG for IV required.\n");
  156. exit(-1);
  157. }
  158. if (fwrite(IV,1,ivsize,fdout) != ivsize) {
  159. printf("Error writing IV to output.\n");
  160. exit(-1);
  161. }
  162. if ((err = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
  163. printf("ctr_start error: %s\n",error_to_string(err));
  164. exit(-1);
  165. }
  166. do {
  167. y = fread(inbuf,1,sizeof(inbuf),fdin);
  168. if ((err = ctr_encrypt(inbuf,ciphertext,y,&ctr)) != CRYPT_OK) {
  169. printf("ctr_encrypt error: %s\n", error_to_string(err));
  170. exit(-1);
  171. }
  172. if (fwrite(ciphertext,1,y,fdout) != y) {
  173. printf("Error writing to output.\n");
  174. exit(-1);
  175. }
  176. } while (y == sizeof(inbuf));
  177. fclose(fdout);
  178. fclose(fdin);
  179. }
  180. return 0;
  181. }