tagging.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * This file contains callback functions and all necessary helper functions to
  3. * tag flows for use with the Slitheen decoy routing system
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdint.h>
  8. #include <openssl/rand.h>
  9. #include <openssl/sha.h>
  10. #include <openssl/ssl.h>
  11. #include <openssl/err.h>
  12. #include <openssl/ec.h>
  13. #include <openssl/bn.h>
  14. #include "ptwist.h"
  15. #include "tagging.h"
  16. //look for key list
  17. byte maingen[PTWIST_BYTES];
  18. byte twistgen[PTWIST_BYTES];
  19. byte mainpub[PTWIST_BYTES];
  20. byte twistpub[PTWIST_BYTES];
  21. static void gen_tag(byte *tag, byte stored_key[16],
  22. const byte *context, size_t context_len)
  23. {
  24. byte seckey[PTWIST_BYTES];
  25. byte sharedsec[PTWIST_BYTES+context_len];
  26. byte usetwist;
  27. byte taghashout[32];
  28. #if PTWIST_PUZZLE_STRENGTH > 0
  29. size_t puzzle_len = 16+PTWIST_RESP_BYTES;
  30. byte value_to_hash[puzzle_len];
  31. byte hashout[32];
  32. bn_t Rbn, Hbn;
  33. int i, len, sign;
  34. #endif
  35. memset(tag, 0xAA, PTWIST_TAG_BYTES);
  36. /* Use the main or the twist curve? */
  37. RAND_bytes(&usetwist, 1);
  38. usetwist &= 1;
  39. /* Create seckey*G and seckey*Y */
  40. RAND_bytes(seckey, PTWIST_BYTES);
  41. ptwist_pointmul(tag, usetwist ? twistgen : maingen, seckey);
  42. ptwist_pointmul(sharedsec, usetwist ? twistpub : mainpub, seckey);
  43. /* Create the tag hash keys */
  44. memmove(sharedsec+PTWIST_BYTES, context, context_len);
  45. SHA256(sharedsec, PTWIST_BYTES, taghashout);
  46. #if PTWIST_PUZZLE_STRENGTH > 0
  47. /* The puzzle is to find a response R such that SHA256(K || R)
  48. starts with PTWIST_PUZZLE_STRENGTH bits of 0s. K is the first
  49. 128 bits of the above hash tag keys. */
  50. /* Construct our response to the puzzle. Start looking for R in a
  51. * random place. */
  52. memmove(value_to_hash, taghashout, 16);
  53. RAND_bytes(value_to_hash+16, PTWIST_RESP_BYTES);
  54. value_to_hash[16+PTWIST_RESP_BYTES-1] &= PTWIST_RESP_MASK;
  55. while(1) {
  56. unsigned int firstbits;
  57. md_map_sh256(hashout, value_to_hash, puzzle_len);
  58. #if PTWIST_PUZZLE_STRENGTH < 32
  59. /* This assumes that you're on an architecture that doesn't care
  60. * about alignment, and is little endian. */
  61. firstbits = *(unsigned int*)hashout;
  62. if ((firstbits & PTWIST_PUZZLE_MASK) == 0) {
  63. break;
  64. }
  65. /* Increment R and try again. */
  66. for(i=0;i<PTWIST_RESP_BYTES;++i) {
  67. if (++value_to_hash[16+i]) break;
  68. }
  69. value_to_hash[16+PTWIST_RESP_BYTES-1] &= PTWIST_RESP_MASK;
  70. #else
  71. #error "Code assumes PTWIST_PUZZLE_STRENGTH < 32"
  72. #endif
  73. }
  74. /* When we get here, we have solved the puzzle. R is in
  75. * value_to_hash[16..16+PTWIST_RESP_BYTES-1], the hash output
  76. * hashout starts with PTWIST_PUZZLE_STRENGTH bits of 0s, and we'll
  77. * want to copy out H (the next PTWIST_HASH_SHOWBITS bits of the
  78. * hash output). The final tag is [seckey*G]_x || R || H . */
  79. bn_new(Rbn);
  80. bn_new(Hbn);
  81. bn_read_bin(Rbn, value_to_hash+16, PTWIST_RESP_BYTES, BN_POS);
  82. hashout[PTWIST_HASH_TOTBYTES-1] &= PTWIST_HASH_MASK;
  83. bn_read_bin(Hbn, hashout, PTWIST_HASH_TOTBYTES, BN_POS);
  84. bn_lsh(Hbn, Hbn, PTWIST_RESP_BITS-PTWIST_PUZZLE_STRENGTH);
  85. bn_add(Hbn, Hbn, Rbn);
  86. len = PTWIST_TAG_BYTES-PTWIST_BYTES;
  87. bn_write_bin(tag+PTWIST_BYTES, &len, &sign, Hbn);
  88. bn_free(Rbn);
  89. bn_free(Hbn);
  90. #elif PTWIST_HASH_SHOWBITS <= 128
  91. /* We're not using a client puzzle, so the tag is [seckey*G]_x || H
  92. * where H is the first PTWIST_HASH_SHOWBITS bits of the above hash
  93. * output. The key generated is the last 128 bits of that output.
  94. * If there's no client puzzle, PTWIST_HASH_SHOWBITS must be a multiple
  95. * of 8. */
  96. memmove(tag+PTWIST_BYTES, taghashout, PTWIST_HASH_SHOWBITS/8);
  97. #else
  98. #error "No client puzzle used, but PWTIST_HASH_SHOWBITS > 128"
  99. #endif
  100. memmove(stored_key, taghashout+16, 16);
  101. }
  102. int generate_slitheen_id(byte *slitheen_id, byte stored_key[16])
  103. {
  104. FILE *fp;
  105. int res;
  106. byte *tag;
  107. /* Create the generators */
  108. memset(maingen, 0, PTWIST_BYTES);
  109. maingen[0] = 2;
  110. memset(twistgen, 0, PTWIST_BYTES);
  111. /* Read the public keys */
  112. fp = fopen("pubkey", "rb");
  113. if (fp == NULL) {
  114. perror("fopen");
  115. exit(1);
  116. }
  117. res = fread(mainpub, PTWIST_BYTES, 1, fp);
  118. if (res < 1) {
  119. perror("fread");
  120. exit(1);
  121. }
  122. res = fread(twistpub, PTWIST_BYTES, 1, fp);
  123. if (res < 1) {
  124. perror("fread");
  125. exit(1);
  126. }
  127. fclose(fp);
  128. tag = slitheen_id;
  129. gen_tag(tag, stored_key, (const byte *)"context", 7);
  130. return 0;
  131. }