ltcrypt.c 5.7 KB

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