I2C toy code
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

205 rindas
5.7 KiB

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