ltcrypt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 usage(char *name)
  13. {
  14. int x;
  15. printf("Usage encrypt: %s cipher infile outfile\n", name);
  16. printf("Usage decrypt: %s -d cipher infile outfile\n", name);
  17. printf("Usage test: %s -t cipher\nCiphers:\n", name);
  18. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  19. printf("%s\n",cipher_descriptor[x].name);
  20. }
  21. exit(1);
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. unsigned char plaintext[512],ciphertext[512];
  26. unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
  27. unsigned char inbuf[512]; /* i/o block size */
  28. unsigned long outlen, y, ivsize, x, decrypt;
  29. symmetric_CTR ctr;
  30. int cipher_idx, hash_idx, ks;
  31. char *infile, *outfile, *cipher;
  32. prng_state prng;
  33. FILE *fdin, *fdout;
  34. int err;
  35. /* register algs, so they can be printed */
  36. register_all_ciphers();
  37. register_all_hashes();
  38. register_all_prngs();
  39. if (argc < 4) {
  40. if ((argc > 2) && (!strcmp(argv[1], "-t"))) {
  41. cipher = argv[2];
  42. cipher_idx = find_cipher(cipher);
  43. if (cipher_idx == -1) {
  44. printf("Invalid cipher %s entered on command line.\n", cipher);
  45. exit(-1);
  46. } /* if */
  47. if (cipher_descriptor[cipher_idx].test)
  48. {
  49. if (cipher_descriptor[cipher_idx].test() != CRYPT_OK)
  50. {
  51. printf("Error when testing cipher %s.\n", cipher);
  52. exit(-1);
  53. }
  54. else
  55. {
  56. printf("Testing cipher %s succeeded.\n", cipher);
  57. exit(0);
  58. } /* if ... else */
  59. } /* if */
  60. }
  61. return usage(argv[0]);
  62. }
  63. if (!strcmp(argv[1], "-d")) {
  64. decrypt = 1;
  65. cipher = argv[2];
  66. infile = argv[3];
  67. outfile = argv[4];
  68. } else {
  69. decrypt = 0;
  70. cipher = argv[1];
  71. infile = argv[2];
  72. outfile = argv[3];
  73. }
  74. /* file handles setup */
  75. fdin = fopen(infile,"rb");
  76. if (fdin == NULL) {
  77. perror("Can't open input for reading");
  78. exit(-1);
  79. }
  80. fdout = fopen(outfile,"wb");
  81. if (fdout == NULL) {
  82. perror("Can't open output for writing");
  83. exit(-1);
  84. }
  85. cipher_idx = find_cipher(cipher);
  86. if (cipher_idx == -1) {
  87. printf("Invalid cipher entered on command line.\n");
  88. exit(-1);
  89. }
  90. hash_idx = find_hash("sha256");
  91. if (hash_idx == -1) {
  92. printf("LTC_SHA256 not found...?\n");
  93. exit(-1);
  94. }
  95. ivsize = cipher_descriptor[cipher_idx].block_length;
  96. ks = hash_descriptor[hash_idx].hashsize;
  97. if (cipher_descriptor[cipher_idx].keysize(&ks) != CRYPT_OK) {
  98. printf("Invalid keysize???\n");
  99. exit(-1);
  100. }
  101. printf("\nEnter key: ");
  102. if(fgets((char *)tmpkey,sizeof(tmpkey), stdin) == NULL)
  103. exit(-1);
  104. outlen = sizeof(key);
  105. if ((err = hash_memory(hash_idx,tmpkey,XSTRLEN((char *)tmpkey),key,&outlen)) != CRYPT_OK) {
  106. printf("Error hashing key: %s\n", error_to_string(err));
  107. exit(-1);
  108. }
  109. if (decrypt) {
  110. /* Need to read in IV */
  111. if (fread(IV,1,ivsize,fdin) != ivsize) {
  112. printf("Error reading IV from input.\n");
  113. exit(-1);
  114. }
  115. if ((err = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
  116. printf("ctr_start error: %s\n",error_to_string(err));
  117. exit(-1);
  118. }
  119. /* IV done */
  120. do {
  121. y = fread(inbuf,1,sizeof(inbuf),fdin);
  122. if ((err = ctr_decrypt(inbuf,plaintext,y,&ctr)) != CRYPT_OK) {
  123. printf("ctr_decrypt error: %s\n", error_to_string(err));
  124. exit(-1);
  125. }
  126. if (fwrite(plaintext,1,y,fdout) != y) {
  127. printf("Error writing to file.\n");
  128. exit(-1);
  129. }
  130. } while (y == sizeof(inbuf));
  131. fclose(fdin);
  132. fclose(fdout);
  133. } else { /* encrypt */
  134. /* Setup yarrow for random bytes for IV */
  135. if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
  136. printf("Error setting up PRNG, %s\n", error_to_string(err));
  137. }
  138. /* You can use rng_get_bytes on platforms that support it */
  139. /* x = rng_get_bytes(IV,ivsize,NULL);*/
  140. x = yarrow_read(IV,ivsize,&prng);
  141. if (x != ivsize) {
  142. printf("Error reading PRNG for IV required.\n");
  143. exit(-1);
  144. }
  145. if (fwrite(IV,1,ivsize,fdout) != ivsize) {
  146. printf("Error writing IV to output.\n");
  147. exit(-1);
  148. }
  149. if ((err = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
  150. printf("ctr_start error: %s\n",error_to_string(err));
  151. exit(-1);
  152. }
  153. do {
  154. y = fread(inbuf,1,sizeof(inbuf),fdin);
  155. if ((err = ctr_encrypt(inbuf,ciphertext,y,&ctr)) != CRYPT_OK) {
  156. printf("ctr_encrypt error: %s\n", error_to_string(err));
  157. exit(-1);
  158. }
  159. if (fwrite(ciphertext,1,y,fdout) != y) {
  160. printf("Error writing to output.\n");
  161. exit(-1);
  162. }
  163. } while (y == sizeof(inbuf));
  164. fclose(fdout);
  165. fclose(fdin);
  166. }
  167. return 0;
  168. }