ltcrypt.c 5.3 KB

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