CryptoHeader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef CryptoHeader_H
  16. #define CryptoHeader_H
  17. #include "util/Assert.h"
  18. #include "util/Endian.h"
  19. #include <stdint.h>
  20. /**
  21. * Header for nodes authenticating to one another.
  22. *
  23. * 1 2 3
  24. * 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
  25. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  26. * 0 | Auth Type | |
  27. * +-+-+-+-+-+-+-+-+ Hash Code +
  28. * 4 | |
  29. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  30. * 8 |A| Derivations |S| Additional |
  31. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  32. *
  33. * Bits A and S and fields Derivitives and Additional are deprecated, they will always be ignored.
  34. * Historically A means "authenticate", the bit is set to request Poly1305 authentication which
  35. * is now enabled all of the time.
  36. * S meant that the packet was used as part of session setup, this is a carry-over from a time
  37. * when it was possible to initiate a session with someone whose key you do not know. The bit
  38. * indicated that the packet should be "suppressed".
  39. * Derivations was intended to be used for exchanging secrets between nodes. Alice and Bob
  40. * having a shared secret (password) would allow Alice to give *something* to charlie which
  41. * would not allow him to athenticate with Bob as if he was Alice but would allow him to
  42. * to make a crypto session with Bob which was secured additionally by the shared secret between
  43. * Alice and Bob which was (presumably) transferred to Charlie along a secure channel.
  44. * The field Additional was never used but was intended to be for more information included
  45. * depending on the authType.
  46. *
  47. * The Auth Type and Hash Code combined make a lookup key which can be used to scan a hashtable
  48. * to see if the given password is known. It can be thought of as the "username" although it is
  49. * a derivative of the password.
  50. */
  51. union CryptoHeader_Challenge
  52. {
  53. struct {
  54. uint8_t type;
  55. uint8_t lookup[7];
  56. /**
  57. * High 1 bit is whether to require poly1305 packet authentication.
  58. * low 15 bits is number of derivations.
  59. */
  60. uint16_t requirePacketAuthAndDerivationCount;
  61. uint16_t additional;
  62. } challenge;
  63. uint8_t bytes[12];
  64. uint32_t ints[3];
  65. };
  66. /** Total size of the auth structure. */
  67. #define CryptoHeader_Challenge_SIZE 12
  68. Assert_compileTime(sizeof(union CryptoHeader_Challenge) == CryptoHeader_Challenge_SIZE);
  69. /** The number of bytes from the beginning which identify the auth for looking up the secret. */
  70. #define CryptoHeader_Challenge_KEYSIZE 8
  71. static inline uint16_t CryptoHeader_getAuthChallengeDerivations(union CryptoHeader_Challenge* ac)
  72. {
  73. return Endian_bigEndianToHost16(ac->challenge.requirePacketAuthAndDerivationCount)
  74. & (((uint16_t)~0)>>1);
  75. }
  76. static inline void CryptoHeader_setAuthChallengeDerivations(union CryptoHeader_Challenge* ac,
  77. uint16_t derivations)
  78. {
  79. ac->challenge.requirePacketAuthAndDerivationCount = Endian_hostToBigEndian16(derivations);
  80. }
  81. /**
  82. * This is a handshake header packet, there are 2 required to begin an encrypted connection.
  83. *
  84. * 1 2 3
  85. * 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
  86. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  87. * 0 | Session State |
  88. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  89. * 4 | |
  90. * + +
  91. * 8 | Auth Challenge |
  92. * + +
  93. * 12 | |
  94. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  95. * 16 | |
  96. * + +
  97. * 20 | |
  98. * + +
  99. * 24 | |
  100. * + Random Nonce +
  101. * 28 | |
  102. * + +
  103. * 32 | |
  104. * + +
  105. * 36 | |
  106. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  107. * 40 | |
  108. * + +
  109. * 44 | |
  110. * + +
  111. * 48 | |
  112. * + +
  113. * 52 | |
  114. * + Permanent Public Key +
  115. * 56 | |
  116. * + +
  117. * 60 | |
  118. * + +
  119. * 64 | |
  120. * + +
  121. * 68 | |
  122. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  123. * 72 | |
  124. * + +
  125. * 76 | |
  126. * + Poly1305 Authenticator +
  127. * 80 | |
  128. * + +
  129. * 84 | |
  130. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  131. * 88 | |
  132. * + +
  133. * 92 | |
  134. * + +
  135. * 96 | |
  136. * + +
  137. * 100 | |
  138. * + Encrypted/Authenticated Temporary Public Key +
  139. * 104 | |
  140. * + +
  141. * 108 | |
  142. * + +
  143. * 112 | |
  144. * + +
  145. * 116 | |
  146. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  147. * | |
  148. * + Variable Length Encrypted/Authenticated Content +
  149. * | |
  150. *
  151. * If "Session State" is equal to the bitwise complement of zero, the sender is requesting
  152. * that the recipient begin a connection with him, this is done in cases when the initiator
  153. * of the connection does not know the key for the recipient. If the entire header is not
  154. * present the recipient MUST drop the packet silently, the only field which is read in the
  155. * packet is the "Permanent Public Key" field, all others SHOULD be ignored, specifically,
  156. * content MUST not be passed on because it cannot be authenticated. The recipient of such a
  157. * packet SHOULD send back a "hello" packet if there is no established connection.
  158. * If there is already a connection over the interface, the recipient SHOULD NOT respond
  159. * but MAY allow the connection to time out faster.
  160. *
  161. * If the "Session State" field is equal to the one or two, the packet is a "hello" packet.
  162. * or a repeated hello packet. If no connection is present, one should be established and the
  163. * recipient MAY send a "key" packet in response but it is RECOMMENDED that he wait until
  164. * he has data to send first. A node who has sent a hello packet and gotten no response and
  165. * now wishes to send more data MUST send that data as more (repeat) hello packets.
  166. *
  167. * If the "Session State" field is equal to two or three, the packet is a "key" packet.
  168. * Key packets are responses to hello packets. Once a node receives a key packet it may begin
  169. * sending data packets. A node who has received a hello packet, sent a key packet and gotten
  170. * no further response who now wishes to send more data MUST send that data as more (repeat)
  171. * key packets.
  172. */
  173. union CryptoHeader
  174. {
  175. uint32_t nonce;
  176. struct {
  177. /**
  178. * Numbers one through three are interpreted as handshake packets, UINT32_MAX is
  179. * a connectToMe packet and anything else is a nonce in a traffic packet.
  180. */
  181. uint32_t handshakeStage;
  182. /** Used for authenticating routers to one another. */
  183. union CryptoHeader_Challenge auth;
  184. /** Random nonce for the handshake. */
  185. uint8_t nonce[24];
  186. /** This node's permanent public key. */
  187. uint8_t publicKey[32];
  188. /** This is filled in when the tempKey is encrypted. */
  189. uint8_t authenticator[16];
  190. /** The public key to use for this session, encrypted with the private key. */
  191. uint8_t encryptedTempKey[32];
  192. } handshake;
  193. };
  194. #define CryptoHeader_SIZE 120
  195. Assert_compileTime(sizeof(union CryptoHeader) == CryptoHeader_SIZE);
  196. #endif