CryptoHeader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <https://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. struct CryptoHeader_Challenge
  52. {
  53. uint8_t type;
  54. uint8_t lookup[7];
  55. /**
  56. * High 1 bit is whether to require poly1305 packet authentication.
  57. * low 15 bits is number of derivations.
  58. */
  59. uint16_t requirePacketAuthAndDerivationCount;
  60. uint16_t additional;
  61. };
  62. /** Total size of the auth structure. */
  63. #define CryptoHeader_Challenge_SIZE 12
  64. Assert_compileTime(sizeof(struct CryptoHeader_Challenge) == CryptoHeader_Challenge_SIZE);
  65. /** The number of bytes from the beginning which identify the auth for looking up the secret. */
  66. #define CryptoHeader_Challenge_KEYSIZE 8
  67. static inline uint16_t CryptoHeader_getAuthChallengeDerivations(struct CryptoHeader_Challenge* ac)
  68. {
  69. return Endian_bigEndianToHost16(ac->requirePacketAuthAndDerivationCount)
  70. & (((uint16_t)~0)>>1);
  71. }
  72. static inline void CryptoHeader_setAuthChallengeDerivations(struct CryptoHeader_Challenge* ac,
  73. uint16_t derivations)
  74. {
  75. ac->requirePacketAuthAndDerivationCount = Endian_hostToBigEndian16(derivations);
  76. }
  77. /**
  78. * This is a handshake header packet, there are 2 required to begin an encrypted connection.
  79. *
  80. * 1 2 3
  81. * 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
  82. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  83. * 0 | Session State |
  84. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  85. * 4 | |
  86. * + +
  87. * 8 | Auth Challenge |
  88. * + +
  89. * 12 | |
  90. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  91. * 16 | |
  92. * + +
  93. * 20 | |
  94. * + +
  95. * 24 | |
  96. * + Random Nonce +
  97. * 28 | |
  98. * + +
  99. * 32 | |
  100. * + +
  101. * 36 | |
  102. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  103. * 40 | |
  104. * + +
  105. * 44 | |
  106. * + +
  107. * 48 | |
  108. * + +
  109. * 52 | |
  110. * + Permanent Public Key +
  111. * 56 | |
  112. * + +
  113. * 60 | |
  114. * + +
  115. * 64 | |
  116. * + +
  117. * 68 | |
  118. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  119. * 72 | |
  120. * + +
  121. * 76 | |
  122. * + Poly1305 Authenticator +
  123. * 80 | |
  124. * + +
  125. * 84 | |
  126. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  127. * 88 | |
  128. * + +
  129. * 92 | |
  130. * + +
  131. * 96 | |
  132. * + +
  133. * 100 | |
  134. * + Encrypted/Authenticated Temporary Public Key +
  135. * 104 | |
  136. * + +
  137. * 108 | |
  138. * + +
  139. * 112 | |
  140. * + +
  141. * 116 | |
  142. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  143. * | |
  144. * + Variable Length Encrypted/Authenticated Content +
  145. * | |
  146. *
  147. * If "Session State" is equal to the bitwise complement of zero, the sender is requesting
  148. * that the recipient begin a connection with him, this is done in cases when the initiator
  149. * of the connection does not know the key for the recipient. If the entire header is not
  150. * present the recipient MUST drop the packet silently, the only field which is read in the
  151. * packet is the "Permanent Public Key" field, all others SHOULD be ignored, specifically,
  152. * content MUST not be passed on because it cannot be authenticated. The recipient of such a
  153. * packet SHOULD send back a "hello" packet if there is no established connection.
  154. * If there is already a connection over the interface, the recipient SHOULD NOT respond
  155. * but MAY allow the connection to time out faster.
  156. *
  157. * If the "Session State" field is equal to the one or two, the packet is a "hello" packet.
  158. * or a repeated hello packet. If no connection is present, one should be established and the
  159. * recipient MAY send a "key" packet in response but it is RECOMMENDED that he wait until
  160. * he has data to send first. A node who has sent a hello packet and gotten no response and
  161. * now wishes to send more data MUST send that data as more (repeat) hello packets.
  162. *
  163. * If the "Session State" field is equal to two or three, the packet is a "key" packet.
  164. * Key packets are responses to hello packets. Once a node receives a key packet it may begin
  165. * sending data packets. A node who has received a hello packet, sent a key packet and gotten
  166. * no further response who now wishes to send more data MUST send that data as more (repeat)
  167. * key packets.
  168. */
  169. struct CryptoHeader
  170. {
  171. /**
  172. * Numbers one through three are interpreted as handshake packets, UINT32_MAX is
  173. * a connectToMe packet and anything else is a nonce in a traffic packet.
  174. */
  175. uint32_t nonce;
  176. /** Used for authenticating routers to one another. */
  177. struct CryptoHeader_Challenge auth;
  178. /** Random nonce for the handshake. */
  179. uint8_t handshakeNonce[24];
  180. /** This node's permanent public key. */
  181. uint8_t publicKey[32];
  182. /** This is filled in when the tempKey is encrypted. */
  183. uint8_t authenticator[16];
  184. /** The public key to use for this session, encrypted with the private key. */
  185. uint8_t encryptedTempKey[32];
  186. };
  187. #define CryptoHeader_SIZE 120
  188. Assert_compileTime(sizeof(struct CryptoHeader) == CryptoHeader_SIZE);
  189. #endif