onion.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * onion.h
  3. * Routines for creating/manipulating onions.
  4. *
  5. * Matej Pfajfar <mp292@cam.ac.uk>
  6. */
  7. /*
  8. * Changes :
  9. * $Log$
  10. * Revision 1.1 2002/06/26 22:45:50 arma
  11. * Initial revision
  12. *
  13. * Revision 1.17 2002/04/02 14:27:11 badbytes
  14. * Final finishes.
  15. *
  16. * Revision 1.16 2002/03/25 09:11:23 badbytes
  17. * Added a list of onions being tracked for replay attacks.
  18. *
  19. * Revision 1.15 2002/01/26 19:24:29 mp292
  20. * Reviewed according to Secure-Programs-HOWTO.
  21. *
  22. * Revision 1.14 2002/01/18 20:40:40 mp292
  23. * Fixed a bug in en/decrypt_onion() functions.
  24. *
  25. * Revision 1.13 2002/01/17 23:48:31 mp292
  26. * Added some extra debugging messages to fix a bug in encrypt_onion() which
  27. * seems to corrupt the routent *route list.
  28. *
  29. * Revision 1.12 2002/01/11 15:47:17 badbytes
  30. * *** empty log message ***
  31. *
  32. * Revision 1.11 2002/01/09 07:55:23 badbytes
  33. * Ciphers got out of sync. Hopefully fixed.
  34. *
  35. * Revision 1.10 2002/01/04 13:48:54 badbytes
  36. * Changed unsigned short/long to uint16_t and uint32_t respectively.
  37. *
  38. * Revision 1.9 2001/12/19 08:29:00 badbytes
  39. * Macro DEFAULT_CIPHER now holds the default crypto algorithm
  40. *
  41. * Revision 1.8 2001/12/18 10:37:47 badbytes
  42. * Header files now only apply if they were not previously included from somewhere else.
  43. *
  44. * Revision 1.7 2001/12/18 07:26:47 badbytes
  45. * Added a new definition of onion_layer_t, depending on the byte order.
  46. *
  47. * Revision 1.6 2001/12/17 13:35:17 badbytes
  48. * Still writing handle_connection()
  49. *
  50. * Revision 1.5 2001/12/14 14:44:37 badbytes
  51. * chooselen() tested
  52. *
  53. * Revision 1.4 2001/12/14 13:31:08 badbytes
  54. * peel_onion() was redundant, removed it
  55. *
  56. * Revision 1.3 2001/12/14 13:14:03 badbytes
  57. * Split types.h into routent.h and ss.h. Keeping them all in one file created unnecesary dependencies.
  58. *
  59. * Revision 1.2 2001/12/14 12:44:47 badbytes
  60. * Minor modifications to reflect new paths ...
  61. *
  62. * Revision 1.1 2001/12/14 12:41:12 badbytes
  63. * Moved from op/ as it will be reused by other modules.
  64. *
  65. * Revision 1.1 2001/12/13 15:15:10 badbytes
  66. * Started coding the onion proxy.
  67. *
  68. */
  69. #ifndef __ONION_H
  70. #include <endian.h>
  71. #include <stdint.h>
  72. #include <openssl/rsa.h>
  73. #include <openssl/evp.h>
  74. #include "routent.h"
  75. #include "version.h"
  76. /* available cipher functions */
  77. #define ONION_CIPHER_IDENTITY 0
  78. #define ONION_CIPHER_DES 1
  79. #define ONION_CIPHER_RC4 2
  80. /* default cipher function */
  81. #define ONION_DEFAULT_CIPHER ONION_CIPHER_DES
  82. typedef struct
  83. {
  84. int zero:1;
  85. int version:7;
  86. int backf:4;
  87. int forwf:4;
  88. uint16_t port;
  89. uint32_t addr;
  90. time_t expire;
  91. unsigned char keyseed[16];
  92. } onion_layer_t;
  93. typedef struct
  94. {
  95. unsigned int forwf;
  96. unsigned int backf;
  97. char digest2[20]; /* second SHA output for onion_layer_t.keyseed */
  98. char digest3[20]; /* third SHA output for onion_layer_t.keyseed */
  99. /* IVs */
  100. char f_iv[16];
  101. char b_iv[16];
  102. /* cipher contexts */
  103. EVP_CIPHER_CTX f_ctx;
  104. EVP_CIPHER_CTX b_ctx;
  105. } crypt_path_t;
  106. typedef struct
  107. {
  108. time_t expire;
  109. char digest[20]; /* SHA digest of the onion */
  110. void *prev;
  111. void *next;
  112. } tracked_onion_t;
  113. /* returns an array of indexes into a router array that define a new route through the OR network
  114. * int cw is the coin weight to use when choosing the route
  115. * order of routers is from last to first */
  116. unsigned int *new_route(double cw, routent_t **rarray, size_t rarray_len, size_t *rlen);
  117. /* creates a new onion from route, stores it and its length into bufp and lenp respectively */
  118. /* if cpathp not NULL then also compute the corresponding crypt_path */
  119. unsigned char *create_onion(routent_t **rarray, size_t rarray_len, unsigned int *route, size_t routelen, size_t *lenp, crypt_path_t **cpathp);
  120. /* encrypts 128 bytes of the onion with the specified public key, the rest with
  121. * DES OFB with the key as defined in the outter layer */
  122. unsigned char *encrypt_onion(onion_layer_t *onion, uint32_t onionlen, RSA *pkey);
  123. /* decrypts the onion */
  124. unsigned char *decrypt_onion(onion_layer_t *onion, uint32_t onionlen, RSA *prkey);
  125. /* deletes the first n bytes of the onion and pads the end with n bytes of random data */
  126. void pad_onion(unsigned char *onion, uint32_t onionlen, size_t n);
  127. /* create a new tracked_onion entry */
  128. tracked_onion_t *new_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion);
  129. /* delete a tracked onion entry */
  130. void remove_tracked_onion(tracked_onion_t *to, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion);
  131. /* find a tracked onion in the linked list of tracked onions */
  132. tracked_onion_t *id_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t *tracked_onions);
  133. #define __ONION_H
  134. #endif