You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.5 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <stdint.h>
  5. #include "encrypt.h"
  6. #define OFFSET_ROL(p, o) ((u_int64_t)(*(p + o)) << (8 * o))
  7. static void md5(const unsigned char *text, unsigned char *digest) {
  8. md5_state_t state;
  9. md5_init(&state);
  10. md5_append(&state, text, strlen((char*)text));
  11. md5_finish(&state, digest);
  12. }
  13. static int random_compare(const void *_x, const void *_y) {
  14. uint32_t i = _i;
  15. uint64_t a = _a;
  16. uint8_t x = *((uint8_t *) _x);
  17. uint8_t y = *((uint8_t*) _y);
  18. return (a % (x + i) - a % (y + i));
  19. }
  20. static void merge(uint8_t *left, int llength, uint8_t *right, int rlength)
  21. {
  22. /* Temporary memory locations for the 2 segments of the array to merge. */
  23. uint8_t *ltmp = (uint8_t *) malloc(llength * sizeof(uint8_t));
  24. uint8_t *rtmp = (uint8_t *) malloc(rlength * sizeof(uint8_t));
  25. /*
  26. * Pointers to the elements being sorted in the temporary memory locations.
  27. */
  28. uint8_t *ll = ltmp;
  29. uint8_t *rr = rtmp;
  30. uint8_t *result = left;
  31. /*
  32. * Copy the segment of the array to be merged into the temporary memory
  33. * locations.
  34. */
  35. memcpy(ltmp, left, llength * sizeof(uint8_t));
  36. memcpy(rtmp, right, rlength * sizeof(uint8_t));
  37. while (llength > 0 && rlength > 0) {
  38. if (random_compare(ll, rr) <= 0) {
  39. /*
  40. * Merge the first element from the left back into the main array
  41. * if it is smaller or equal to the one on the right.
  42. */
  43. *result = *ll;
  44. ++ll;
  45. --llength;
  46. } else {
  47. /*
  48. * Merge the first element from the right back into the main array
  49. * if it is smaller than the one on the left.
  50. */
  51. *result = *rr;
  52. ++rr;
  53. --rlength;
  54. }
  55. ++result;
  56. }
  57. /*
  58. * All the elements from either the left or the right temporary array
  59. * segment have been merged back into the main array. Append the remaining
  60. * elements from the other temporary array back into the main array.
  61. */
  62. if (llength > 0)
  63. while (llength > 0) {
  64. /* Appending the rest of the left temporary array. */
  65. *result = *ll;
  66. ++result;
  67. ++ll;
  68. --llength;
  69. }
  70. else
  71. while (rlength > 0) {
  72. /* Appending the rest of the right temporary array. */
  73. *result = *rr;
  74. ++result;
  75. ++rr;
  76. --rlength;
  77. }
  78. /* Release the memory used for the temporary arrays. */
  79. free(ltmp);
  80. free(rtmp);
  81. }
  82. static void merge_sort(uint8_t array[], int length)
  83. {
  84. /* This is the middle index and also the length of the right array. */
  85. uint8_t middle;
  86. /*
  87. * Pointers to the beginning of the left and right segment of the array
  88. * to be merged.
  89. */
  90. uint8_t *left, *right;
  91. /* Length of the left segment of the array to be merged. */
  92. int llength;
  93. if (length <= 1)
  94. return;
  95. /* Let integer division truncate the value. */
  96. middle = length / 2;
  97. llength = length - middle;
  98. /*
  99. * Set the pointers to the appropriate segments of the array to be merged.
  100. */
  101. left = array;
  102. right = array + llength;
  103. merge_sort(left, llength);
  104. merge_sort(right, middle);
  105. merge(left, llength, right, middle);
  106. }
  107. void encrypt_ctx(char *buf, int len, struct rc4_state *ctx) {
  108. if (ctx != NULL) {
  109. rc4_crypt(ctx, (unsigned char*) buf, (unsigned char*) buf, len);
  110. } else {
  111. char *end = buf + len;
  112. while (buf < end) {
  113. *buf = (char)enc_ctx.table.encrypt_table[(uint8_t)*buf];
  114. buf++;
  115. }
  116. }
  117. }
  118. void decrypt_ctx(char *buf, int len, struct rc4_state *ctx) {
  119. if (ctx != NULL) {
  120. rc4_crypt(ctx, (unsigned char*) buf, (unsigned char*) buf, len);
  121. } else {
  122. char *end = buf + len;
  123. while (buf < end) {
  124. *buf = (char)enc_ctx.table.decrypt_table[(uint8_t)*buf];
  125. buf++;
  126. }
  127. }
  128. }
  129. void enc_ctx_init(struct rc4_state *ctx, int enc) {
  130. uint8_t *key = enc_ctx.rc4.key;
  131. int key_len = enc_ctx.rc4.key_len;
  132. rc4_init(ctx, key, key_len);
  133. }
  134. void enc_key_init(const char *pass) {
  135. enc_ctx.rc4.key_len = 16;
  136. enc_ctx.rc4.key = malloc(16);
  137. md5((const unsigned char*)pass, enc_ctx.rc4.key);
  138. }
  139. void get_table(const char *pass) {
  140. uint8_t *enc_table = malloc(256);
  141. uint8_t *dec_table = malloc(256);
  142. uint8_t digest[16];
  143. uint32_t i;
  144. md5((const unsigned char*)pass, digest);
  145. for (i = 0; i < 8; i++) {
  146. _a += OFFSET_ROL(digest, i);
  147. }
  148. for(i = 0; i < 256; ++i) {
  149. enc_table[i] = i;
  150. }
  151. for(i = 1; i < 1024; ++i) {
  152. _i = i;
  153. merge_sort(enc_table, 256);
  154. }
  155. for(i = 0; i < 256; ++i) {
  156. // gen decrypt table from encrypt table
  157. dec_table[enc_table[i]] = i;
  158. }
  159. enc_ctx.table.encrypt_table = enc_table;
  160. enc_ctx.table.decrypt_table = dec_table;
  161. }