encrypt.c 5.3 KB

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