noise.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  4. */
  5. #ifndef _WG_NOISE_H
  6. #define _WG_NOISE_H
  7. #include "messages.h"
  8. #include "peerlookup.h"
  9. #include <linux/types.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/atomic.h>
  12. #include <linux/rwsem.h>
  13. #include <linux/mutex.h>
  14. #include <linux/kref.h>
  15. struct noise_replay_counter {
  16. u64 counter;
  17. spinlock_t lock;
  18. unsigned long backtrack[COUNTER_BITS_TOTAL / BITS_PER_LONG];
  19. };
  20. struct noise_symmetric_key {
  21. u8 key[NOISE_SYMMETRIC_KEY_LEN];
  22. u64 birthdate;
  23. bool is_valid;
  24. };
  25. struct noise_keypair {
  26. struct index_hashtable_entry entry;
  27. struct noise_symmetric_key sending;
  28. atomic64_t sending_counter;
  29. struct noise_symmetric_key receiving;
  30. struct noise_replay_counter receiving_counter;
  31. __le32 remote_index;
  32. bool i_am_the_initiator;
  33. struct kref refcount;
  34. struct rcu_head rcu;
  35. u64 internal_id;
  36. };
  37. struct noise_keypairs {
  38. struct noise_keypair __rcu *current_keypair;
  39. struct noise_keypair __rcu *previous_keypair;
  40. struct noise_keypair __rcu *next_keypair;
  41. spinlock_t keypair_update_lock;
  42. };
  43. struct noise_static_identity {
  44. u8 static_public[NOISE_PUBLIC_KEY_LEN];
  45. u8 static_private[NOISE_PUBLIC_KEY_LEN];
  46. struct rw_semaphore lock;
  47. bool has_identity;
  48. };
  49. enum noise_handshake_state {
  50. HANDSHAKE_ZEROED,
  51. HANDSHAKE_CREATED_INITIATION,
  52. HANDSHAKE_CONSUMED_INITIATION,
  53. HANDSHAKE_CREATED_RESPONSE,
  54. HANDSHAKE_CONSUMED_RESPONSE
  55. };
  56. struct noise_handshake {
  57. struct index_hashtable_entry entry;
  58. enum noise_handshake_state state;
  59. u64 last_initiation_consumption;
  60. struct noise_static_identity *static_identity;
  61. u8 ephemeral_private[NOISE_PUBLIC_KEY_LEN];
  62. u8 remote_static[NOISE_PUBLIC_KEY_LEN];
  63. u8 remote_ephemeral[NOISE_PUBLIC_KEY_LEN];
  64. u8 precomputed_static_static[NOISE_PUBLIC_KEY_LEN];
  65. u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN];
  66. u8 hash[NOISE_HASH_LEN];
  67. u8 chaining_key[NOISE_HASH_LEN];
  68. u8 latest_timestamp[NOISE_TIMESTAMP_LEN];
  69. __le32 remote_index;
  70. /* Protects all members except the immutable (after noise_handshake_
  71. * init): remote_static, precomputed_static_static, static_identity.
  72. */
  73. struct rw_semaphore lock;
  74. };
  75. struct wg_device;
  76. void wg_noise_init(void);
  77. void wg_noise_handshake_init(struct noise_handshake *handshake,
  78. struct noise_static_identity *static_identity,
  79. const u8 peer_public_key[NOISE_PUBLIC_KEY_LEN],
  80. const u8 peer_preshared_key[NOISE_SYMMETRIC_KEY_LEN],
  81. struct wg_peer *peer);
  82. void wg_noise_handshake_clear(struct noise_handshake *handshake);
  83. static inline void wg_noise_reset_last_sent_handshake(atomic64_t *handshake_ns)
  84. {
  85. atomic64_set(handshake_ns, ktime_get_coarse_boottime_ns() -
  86. (u64)(REKEY_TIMEOUT + 1) * NSEC_PER_SEC);
  87. }
  88. void wg_noise_keypair_put(struct noise_keypair *keypair, bool unreference_now);
  89. struct noise_keypair *wg_noise_keypair_get(struct noise_keypair *keypair);
  90. void wg_noise_keypairs_clear(struct noise_keypairs *keypairs);
  91. bool wg_noise_received_with_keypair(struct noise_keypairs *keypairs,
  92. struct noise_keypair *received_keypair);
  93. void wg_noise_expire_current_peer_keypairs(struct wg_peer *peer);
  94. void wg_noise_set_static_identity_private_key(
  95. struct noise_static_identity *static_identity,
  96. const u8 private_key[NOISE_PUBLIC_KEY_LEN]);
  97. void wg_noise_precompute_static_static(struct wg_peer *peer);
  98. bool
  99. wg_noise_handshake_create_initiation(struct message_handshake_initiation *dst,
  100. struct noise_handshake *handshake);
  101. struct wg_peer *
  102. wg_noise_handshake_consume_initiation(struct message_handshake_initiation *src,
  103. struct wg_device *wg);
  104. bool wg_noise_handshake_create_response(struct message_handshake_response *dst,
  105. struct noise_handshake *handshake);
  106. struct wg_peer *
  107. wg_noise_handshake_consume_response(struct message_handshake_response *src,
  108. struct wg_device *wg);
  109. bool wg_noise_handshake_begin_session(struct noise_handshake *handshake,
  110. struct noise_keypairs *keypairs);
  111. #endif /* _WG_NOISE_H */