Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. void * safe_realloc(void * p, size_t size) {
  6. p = realloc(p, size);
  7. if (p == NULL) exit(1);
  8. return p;
  9. }
  10.  
  11. /* This has been pre bit reversed */
  12. static const sbox[64] = {0x3, 0xb, 0xd, 0x5, 0x8, 0xd, 0xc, 0xd, 0xe, 0x6, 0x5, 0x0, 0xf, 0xb, 0x4, 0x9, 0xc, 0x9, 0xb, 0x2, 0x3, 0x9, 0xa, 0x0, 0x4, 0x8, 0x1, 0xf, 0x6, 0xa, 0xf, 0x5, 0xe, 0x8, 0x4, 0x7, 0x1, 0x6, 0x3, 0x7, 0x0, 0x4, 0x8, 0x7, 0xc, 0x3, 0xa, 0xe, 0xc, 0x2, 0xa, 0x2, 0x5, 0x6, 0x1, 0xf, 0x0, 0x9, 0x2, 0x7, 0xe, 0xb, 0x1, 0xd };
  13.  
  14. static uint8_t rf(uint8_t in, uint16_t rk) {
  15. uint16_t ex = ((uint16_t)(in & 0x07) | ((uint16_t)(in & 0x1c) << 1) | ((uint16_t)(in & 0x70) << 2) | ((uint16_t)(in & 0xc0) << 3) | ((uint16_t)(in & 0x01) << 11)) ^ rk;
  16. return (sbox[ex & 0x3f] << 4) | sbox[ex >> 6];
  17. }
  18.  
  19. static uint16_t fre(uint16_t in, uint16_t rk) {
  20. return (in << 8) | ((in >> 8) ^ ((uint16_t) rf(in, rk)));
  21. }
  22.  
  23. static uint16_t frd(uint16_t in, uint16_t rk) {
  24. return ((in ^ ((uint16_t) rf(in >> 8, rk))) << 8) | (in >> 8);
  25. }
  26.  
  27. static uint16_t enc(uint16_t key, uint16_t data) {
  28. // fprintf(stderr, "Key: %04hx Data: %04hx\n", key, data);
  29. key = ((key << 3) & 0xf8f8) | ((key >> 5) & 0x0707);
  30. data = fre(data, ((key >> 4) & 0x0fc0) | ((key >> 2) & 0x003f));
  31. key = ((key << 3) & 0xf8f8) | ((key >> 5) & 0x0707);
  32. data = fre(data, ((key >> 4) & 0x0fc0) | ((key >> 2) & 0x003f));
  33. key = ((key << 3) & 0xf8f8) | ((key >> 5) & 0x0707);
  34. data = fre(data, ((key >> 4) & 0x0fc0) | ((key >> 2) & 0x003f));
  35. key = ((key << 3) & 0xf8f8) | ((key >> 5) & 0x0707);
  36. data = fre(data, ((key >> 4) & 0x0fc0) | ((key >> 2) & 0x003f));
  37. // fprintf(stderr, "enc: %04hx\n", data);
  38. return data;
  39. }
  40.  
  41. static uint16_t dec(uint16_t dkey, uint16_t ddata) {
  42. // fprintf(stderr, "Key: %04hx Data: %04hx\n", dkey, ddata);
  43. dkey = ((dkey << 4) & 0xf0f0) | ((dkey >> 4) & 0x0f0f);
  44. ddata = frd(ddata, ((dkey >> 4) & 0x0fc0) | ((dkey >> 2) & 0x003f));
  45. dkey = ((dkey << 5) & 0xe0e0) | ((dkey >> 3) & 0x1f1f);
  46. ddata = frd(ddata, ((dkey >> 4) & 0x0fc0) | ((dkey >> 2) & 0x003f));
  47. dkey = ((dkey << 5) & 0xe0e0) | ((dkey >> 3) & 0x1f1f);
  48. ddata = frd(ddata, ((dkey >> 4) & 0x0fc0) | ((dkey >> 2) & 0x003f));
  49. dkey = ((dkey << 5) & 0xe0e0) | ((dkey >> 3) & 0x1f1f);
  50. ddata = frd(ddata, ((dkey >> 4) & 0x0fc0) | ((dkey >> 2) & 0x003f));
  51. // fprintf(stderr, "dec: %04hx\n", ddata);
  52. }
  53.  
  54. struct middle {
  55. size_t count;
  56. uint16_t * keys;
  57. };
  58.  
  59. int main (int argc, char ** argv) {
  60. uint16_t m1, c1, m2, c2;
  61. uint16_t k1, k2, i;
  62. uint16_t r;
  63. int encs = 0, decs = 0, j;
  64. struct middle a[1<<16];
  65. if (argc < 4) {
  66. fprintf(stderr, "Usage: %s m1 c1 m2 c2\n", argv[0]);
  67. exit(1);
  68. }
  69. if (sscanf(argv[1], "%04hx", &m1) != 1) {
  70. fprintf(stderr, "Couldn't parse m1\n");
  71. exit(1);
  72. }
  73. if (sscanf(argv[2], "%04hx", &c1) != 1) {
  74. fprintf(stderr, "Couldn't parse c1\n");
  75. exit(1);
  76. }
  77. if (sscanf(argv[3], "%04hx", &m2) != 1) {
  78. fprintf(stderr, "Couldn't parse m2\n");
  79. exit(1);
  80. }
  81. if (sscanf(argv[4], "%04hx", &c2) != 1) {
  82. fprintf(stderr, "Couldn't parse c2\n");
  83. exit(1);
  84. }
  85.  
  86. i = 0;
  87. do {
  88. a[i].count = 0;
  89. a[i].keys = NULL;
  90. i++;
  91. } while (i != 0);
  92.  
  93. k1 = 0;
  94. do {
  95. r = enc(k1, m1);
  96. encs++;
  97. a[r].count++;
  98. a[r].keys = safe_realloc(a[r].keys, a[r].count * sizeof(uint16_t));
  99. a[r].keys[a[r].count-1] = k1;
  100. k1++;
  101. } while (k1 != 0);
  102.  
  103. k2 = 0;
  104. do {
  105. r = dec(k2, c1);
  106. decs++;
  107. for (j = 0; j < a[r].count; j++) {
  108. k1 = a[r].keys[j];
  109. // printf("Possible MITM: k1=%04hx k2=%04hx\n", k1, k2);
  110. if (enc(k2, enc(k1, m2)) == c2) {
  111. printf("MITM: k1=%04hx k2=%04hx\n", k1, k2);
  112. }
  113. encs += 2;
  114. }
  115. k2++;
  116. } while (k2 != 0);
  117. printf("Enc was run %d times and Dec was run %d times.\n", encs, decs);
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement