ltcrypt.c 5.4 KB

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