messages.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4. */
  5. #ifndef _WG_MESSAGES_H
  6. #define _WG_MESSAGES_H
  7. #include <crypto/curve25519.h>
  8. #include <crypto/chacha20poly1305.h>
  9. #include <crypto/blake2s.h>
  10. #include <linux/kernel.h>
  11. #include <linux/param.h>
  12. #include <linux/skbuff.h>
  13. enum noise_lengths {
  14. NOISE_PUBLIC_KEY_LEN = CURVE25519_KEY_SIZE,
  15. NOISE_SYMMETRIC_KEY_LEN = CHACHA20POLY1305_KEY_SIZE,
  16. NOISE_TIMESTAMP_LEN = sizeof(u64) + sizeof(u32),
  17. NOISE_AUTHTAG_LEN = CHACHA20POLY1305_AUTHTAG_SIZE,
  18. NOISE_HASH_LEN = BLAKE2S_HASH_SIZE
  19. };
  20. #define noise_encrypted_len(plain_len) ((plain_len) + NOISE_AUTHTAG_LEN)
  21. enum cookie_values {
  22. COOKIE_SECRET_MAX_AGE = 2 * 60,
  23. COOKIE_SECRET_LATENCY = 5,
  24. COOKIE_NONCE_LEN = XCHACHA20POLY1305_NONCE_SIZE,
  25. COOKIE_LEN = 16
  26. };
  27. enum counter_values {
  28. COUNTER_BITS_TOTAL = 8192,
  29. COUNTER_REDUNDANT_BITS = BITS_PER_LONG,
  30. COUNTER_WINDOW_SIZE = COUNTER_BITS_TOTAL - COUNTER_REDUNDANT_BITS
  31. };
  32. enum limits {
  33. REKEY_AFTER_MESSAGES = 1ULL << 60,
  34. REJECT_AFTER_MESSAGES = U64_MAX - COUNTER_WINDOW_SIZE - 1,
  35. REKEY_TIMEOUT = 5,
  36. REKEY_TIMEOUT_JITTER_MAX_JIFFIES = HZ / 3,
  37. REKEY_AFTER_TIME = 120,
  38. REJECT_AFTER_TIME = 180,
  39. INITIATIONS_PER_SECOND = 50,
  40. MAX_PEERS_PER_DEVICE = 1U << 20,
  41. KEEPALIVE_TIMEOUT = 10,
  42. MAX_TIMER_HANDSHAKES = 90 / REKEY_TIMEOUT,
  43. MAX_QUEUED_INCOMING_HANDSHAKES = 4096, /* TODO: replace this with DQL */
  44. MAX_STAGED_PACKETS = 128,
  45. MAX_QUEUED_PACKETS = 1024 /* TODO: replace this with DQL */
  46. };
  47. enum message_type {
  48. MESSAGE_INVALID = 0,
  49. MESSAGE_HANDSHAKE_INITIATION = 1,
  50. MESSAGE_HANDSHAKE_RESPONSE = 2,
  51. MESSAGE_HANDSHAKE_COOKIE = 3,
  52. MESSAGE_DATA = 4
  53. };
  54. struct message_header {
  55. /* The actual layout of this that we want is:
  56. * u8 type
  57. * u8 reserved_zero[3]
  58. *
  59. * But it turns out that by encoding this as little endian,
  60. * we achieve the same thing, and it makes checking faster.
  61. */
  62. __le32 type;
  63. };
  64. struct message_macs {
  65. u8 mac1[COOKIE_LEN];
  66. u8 mac2[COOKIE_LEN];
  67. };
  68. struct message_handshake_initiation {
  69. struct message_header header;
  70. __le32 sender_index;
  71. u8 unencrypted_ephemeral[NOISE_PUBLIC_KEY_LEN];
  72. u8 encrypted_static[noise_encrypted_len(NOISE_PUBLIC_KEY_LEN)];
  73. u8 encrypted_timestamp[noise_encrypted_len(NOISE_TIMESTAMP_LEN)];
  74. struct message_macs macs;
  75. };
  76. struct message_handshake_response {
  77. struct message_header header;
  78. __le32 sender_index;
  79. __le32 receiver_index;
  80. u8 unencrypted_ephemeral[NOISE_PUBLIC_KEY_LEN];
  81. u8 encrypted_nothing[noise_encrypted_len(0)];
  82. struct message_macs macs;
  83. };
  84. struct message_handshake_cookie {
  85. struct message_header header;
  86. __le32 receiver_index;
  87. u8 nonce[COOKIE_NONCE_LEN];
  88. u8 encrypted_cookie[noise_encrypted_len(COOKIE_LEN)];
  89. };
  90. struct message_data {
  91. struct message_header header;
  92. __le32 key_idx;
  93. __le64 counter;
  94. u8 encrypted_data[];
  95. };
  96. #define message_data_len(plain_len) \
  97. (noise_encrypted_len(plain_len) + sizeof(struct message_data))
  98. enum message_alignments {
  99. MESSAGE_PADDING_MULTIPLE = 16,
  100. MESSAGE_MINIMUM_LENGTH = message_data_len(0)
  101. };
  102. #define SKB_HEADER_LEN \
  103. (max(sizeof(struct iphdr), sizeof(struct ipv6hdr)) + \
  104. sizeof(struct udphdr) + NET_SKB_PAD)
  105. #define DATA_PACKET_HEAD_ROOM \
  106. ALIGN(sizeof(struct message_data) + SKB_HEADER_LEN, 4)
  107. enum { HANDSHAKE_DSCP = 0x88 /* AF41, plus 00 ECN */ };
  108. #endif /* _WG_MESSAGES_H */