noise.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  4. */
  5. #include "noise.h"
  6. #include "device.h"
  7. #include "peer.h"
  8. #include "messages.h"
  9. #include "queueing.h"
  10. #include "peerlookup.h"
  11. #include <linux/rcupdate.h>
  12. #include <linux/slab.h>
  13. #include <linux/bitmap.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/highmem.h>
  16. #include <crypto/algapi.h>
  17. /* This implements Noise_IKpsk2:
  18. *
  19. * <- s
  20. * ******
  21. * -> e, es, s, ss, {t}
  22. * <- e, ee, se, psk, {}
  23. */
  24. static const u8 handshake_name[37] = "Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s";
  25. static const u8 identifier_name[34] = "WireGuard v1 zx2c4 [email protected]";
  26. static u8 handshake_init_hash[NOISE_HASH_LEN] __ro_after_init;
  27. static u8 handshake_init_chaining_key[NOISE_HASH_LEN] __ro_after_init;
  28. static atomic64_t keypair_counter = ATOMIC64_INIT(0);
  29. void __init wg_noise_init(void)
  30. {
  31. struct blake2s_state blake;
  32. blake2s(handshake_init_chaining_key, handshake_name, NULL,
  33. NOISE_HASH_LEN, sizeof(handshake_name), 0);
  34. blake2s_init(&blake, NOISE_HASH_LEN);
  35. blake2s_update(&blake, handshake_init_chaining_key, NOISE_HASH_LEN);
  36. blake2s_update(&blake, identifier_name, sizeof(identifier_name));
  37. blake2s_final(&blake, handshake_init_hash);
  38. }
  39. /* Must hold peer->handshake.static_identity->lock */
  40. void wg_noise_precompute_static_static(struct wg_peer *peer)
  41. {
  42. down_write(&peer->handshake.lock);
  43. if (!peer->handshake.static_identity->has_identity ||
  44. !curve25519(peer->handshake.precomputed_static_static,
  45. peer->handshake.static_identity->static_private,
  46. peer->handshake.remote_static))
  47. memset(peer->handshake.precomputed_static_static, 0,
  48. NOISE_PUBLIC_KEY_LEN);
  49. up_write(&peer->handshake.lock);
  50. }
  51. void wg_noise_handshake_init(struct noise_handshake *handshake,
  52. struct noise_static_identity *static_identity,
  53. const u8 peer_public_key[NOISE_PUBLIC_KEY_LEN],
  54. const u8 peer_preshared_key[NOISE_SYMMETRIC_KEY_LEN],
  55. struct wg_peer *peer)
  56. {
  57. memset(handshake, 0, sizeof(*handshake));
  58. init_rwsem(&handshake->lock);
  59. handshake->entry.type = INDEX_HASHTABLE_HANDSHAKE;
  60. handshake->entry.peer = peer;
  61. memcpy(handshake->remote_static, peer_public_key, NOISE_PUBLIC_KEY_LEN);
  62. if (peer_preshared_key)
  63. memcpy(handshake->preshared_key, peer_preshared_key,
  64. NOISE_SYMMETRIC_KEY_LEN);
  65. handshake->static_identity = static_identity;
  66. handshake->state = HANDSHAKE_ZEROED;
  67. wg_noise_precompute_static_static(peer);
  68. }
  69. static void handshake_zero(struct noise_handshake *handshake)
  70. {
  71. memset(&handshake->ephemeral_private, 0, NOISE_PUBLIC_KEY_LEN);
  72. memset(&handshake->remote_ephemeral, 0, NOISE_PUBLIC_KEY_LEN);
  73. memset(&handshake->hash, 0, NOISE_HASH_LEN);
  74. memset(&handshake->chaining_key, 0, NOISE_HASH_LEN);
  75. handshake->remote_index = 0;
  76. handshake->state = HANDSHAKE_ZEROED;
  77. }
  78. void wg_noise_handshake_clear(struct noise_handshake *handshake)
  79. {
  80. down_write(&handshake->lock);
  81. wg_index_hashtable_remove(
  82. handshake->entry.peer->device->index_hashtable,
  83. &handshake->entry);
  84. handshake_zero(handshake);
  85. up_write(&handshake->lock);
  86. }
  87. static struct noise_keypair *keypair_create(struct wg_peer *peer)
  88. {
  89. struct noise_keypair *keypair = kzalloc(sizeof(*keypair), GFP_KERNEL);
  90. if (unlikely(!keypair))
  91. return NULL;
  92. spin_lock_init(&keypair->receiving_counter.lock);
  93. keypair->internal_id = atomic64_inc_return(&keypair_counter);
  94. keypair->entry.type = INDEX_HASHTABLE_KEYPAIR;
  95. keypair->entry.peer = peer;
  96. kref_init(&keypair->refcount);
  97. return keypair;
  98. }
  99. static void keypair_free_rcu(struct rcu_head *rcu)
  100. {
  101. kfree_sensitive(container_of(rcu, struct noise_keypair, rcu));
  102. }
  103. static void keypair_free_kref(struct kref *kref)
  104. {
  105. struct noise_keypair *keypair =
  106. container_of(kref, struct noise_keypair, refcount);
  107. net_dbg_ratelimited("%s: Keypair %llu destroyed for peer %llu\n",
  108. keypair->entry.peer->device->dev->name,
  109. keypair->internal_id,
  110. keypair->entry.peer->internal_id);
  111. wg_index_hashtable_remove(keypair->entry.peer->device->index_hashtable,
  112. &keypair->entry);
  113. call_rcu(&keypair->rcu, keypair_free_rcu);
  114. }
  115. void wg_noise_keypair_put(struct noise_keypair *keypair, bool unreference_now)
  116. {
  117. if (unlikely(!keypair))
  118. return;
  119. if (unlikely(unreference_now))
  120. wg_index_hashtable_remove(
  121. keypair->entry.peer->device->index_hashtable,
  122. &keypair->entry);
  123. kref_put(&keypair->refcount, keypair_free_kref);
  124. }
  125. struct noise_keypair *wg_noise_keypair_get(struct noise_keypair *keypair)
  126. {
  127. RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held(),
  128. "Taking noise keypair reference without holding the RCU BH read lock");
  129. if (unlikely(!keypair || !kref_get_unless_zero(&keypair->refcount)))
  130. return NULL;
  131. return keypair;
  132. }
  133. void wg_noise_keypairs_clear(struct noise_keypairs *keypairs)
  134. {
  135. struct noise_keypair *old;
  136. spin_lock_bh(&keypairs->keypair_update_lock);
  137. /* We zero the next_keypair before zeroing the others, so that
  138. * wg_noise_received_with_keypair returns early before subsequent ones
  139. * are zeroed.
  140. */
  141. old = rcu_dereference_protected(keypairs->next_keypair,
  142. lockdep_is_held(&keypairs->keypair_update_lock));
  143. RCU_INIT_POINTER(keypairs->next_keypair, NULL);
  144. wg_noise_keypair_put(old, true);
  145. old = rcu_dereference_protected(keypairs->previous_keypair,
  146. lockdep_is_held(&keypairs->keypair_update_lock));
  147. RCU_INIT_POINTER(keypairs->previous_keypair, NULL);
  148. wg_noise_keypair_put(old, true);
  149. old = rcu_dereference_protected(keypairs->current_keypair,
  150. lockdep_is_held(&keypairs->keypair_update_lock));
  151. RCU_INIT_POINTER(keypairs->current_keypair, NULL);
  152. wg_noise_keypair_put(old, true);
  153. spin_unlock_bh(&keypairs->keypair_update_lock);
  154. }
  155. void wg_noise_expire_current_peer_keypairs(struct wg_peer *peer)
  156. {
  157. struct noise_keypair *keypair;
  158. wg_noise_handshake_clear(&peer->handshake);
  159. wg_noise_reset_last_sent_handshake(&peer->last_sent_handshake);
  160. spin_lock_bh(&peer->keypairs.keypair_update_lock);
  161. keypair = rcu_dereference_protected(peer->keypairs.next_keypair,
  162. lockdep_is_held(&peer->keypairs.keypair_update_lock));
  163. if (keypair)
  164. keypair->sending.is_valid = false;
  165. keypair = rcu_dereference_protected(peer->keypairs.current_keypair,
  166. lockdep_is_held(&peer->keypairs.keypair_update_lock));
  167. if (keypair)
  168. keypair->sending.is_valid = false;
  169. spin_unlock_bh(&peer->keypairs.keypair_update_lock);
  170. }
  171. static void add_new_keypair(struct noise_keypairs *keypairs,
  172. struct noise_keypair *new_keypair)
  173. {
  174. struct noise_keypair *previous_keypair, *next_keypair, *current_keypair;
  175. spin_lock_bh(&keypairs->keypair_update_lock);
  176. previous_keypair = rcu_dereference_protected(keypairs->previous_keypair,
  177. lockdep_is_held(&keypairs->keypair_update_lock));
  178. next_keypair = rcu_dereference_protected(keypairs->next_keypair,
  179. lockdep_is_held(&keypairs->keypair_update_lock));
  180. current_keypair = rcu_dereference_protected(keypairs->current_keypair,
  181. lockdep_is_held(&keypairs->keypair_update_lock));
  182. if (new_keypair->i_am_the_initiator) {
  183. /* If we're the initiator, it means we've sent a handshake, and
  184. * received a confirmation response, which means this new
  185. * keypair can now be used.
  186. */
  187. if (next_keypair) {
  188. /* If there already was a next keypair pending, we
  189. * demote it to be the previous keypair, and free the
  190. * existing current. Note that this means KCI can result
  191. * in this transition. It would perhaps be more sound to
  192. * always just get rid of the unused next keypair
  193. * instead of putting it in the previous slot, but this
  194. * might be a bit less robust. Something to think about
  195. * for the future.
  196. */
  197. RCU_INIT_POINTER(keypairs->next_keypair, NULL);
  198. rcu_assign_pointer(keypairs->previous_keypair,
  199. next_keypair);
  200. wg_noise_keypair_put(current_keypair, true);
  201. } else /* If there wasn't an existing next keypair, we replace
  202. * the previous with the current one.
  203. */
  204. rcu_assign_pointer(keypairs->previous_keypair,
  205. current_keypair);
  206. /* At this point we can get rid of the old previous keypair, and
  207. * set up the new keypair.
  208. */
  209. wg_noise_keypair_put(previous_keypair, true);
  210. rcu_assign_pointer(keypairs->current_keypair, new_keypair);
  211. } else {
  212. /* If we're the responder, it means we can't use the new keypair
  213. * until we receive confirmation via the first data packet, so
  214. * we get rid of the existing previous one, the possibly
  215. * existing next one, and slide in the new next one.
  216. */
  217. rcu_assign_pointer(keypairs->next_keypair, new_keypair);
  218. wg_noise_keypair_put(next_keypair, true);
  219. RCU_INIT_POINTER(keypairs->previous_keypair, NULL);
  220. wg_noise_keypair_put(previous_keypair, true);
  221. }
  222. spin_unlock_bh(&keypairs->keypair_update_lock);
  223. }
  224. bool wg_noise_received_with_keypair(struct noise_keypairs *keypairs,
  225. struct noise_keypair *received_keypair)
  226. {
  227. struct noise_keypair *old_keypair;
  228. bool key_is_new;
  229. /* We first check without taking the spinlock. */
  230. key_is_new = received_keypair ==
  231. rcu_access_pointer(keypairs->next_keypair);
  232. if (likely(!key_is_new))
  233. return false;
  234. spin_lock_bh(&keypairs->keypair_update_lock);
  235. /* After locking, we double check that things didn't change from
  236. * beneath us.
  237. */
  238. if (unlikely(received_keypair !=
  239. rcu_dereference_protected(keypairs->next_keypair,
  240. lockdep_is_held(&keypairs->keypair_update_lock)))) {
  241. spin_unlock_bh(&keypairs->keypair_update_lock);
  242. return false;
  243. }
  244. /* When we've finally received the confirmation, we slide the next
  245. * into the current, the current into the previous, and get rid of
  246. * the old previous.
  247. */
  248. old_keypair = rcu_dereference_protected(keypairs->previous_keypair,
  249. lockdep_is_held(&keypairs->keypair_update_lock));
  250. rcu_assign_pointer(keypairs->previous_keypair,
  251. rcu_dereference_protected(keypairs->current_keypair,
  252. lockdep_is_held(&keypairs->keypair_update_lock)));
  253. wg_noise_keypair_put(old_keypair, true);
  254. rcu_assign_pointer(keypairs->current_keypair, received_keypair);
  255. RCU_INIT_POINTER(keypairs->next_keypair, NULL);
  256. spin_unlock_bh(&keypairs->keypair_update_lock);
  257. return true;
  258. }
  259. /* Must hold static_identity->lock */
  260. void wg_noise_set_static_identity_private_key(
  261. struct noise_static_identity *static_identity,
  262. const u8 private_key[NOISE_PUBLIC_KEY_LEN])
  263. {
  264. memcpy(static_identity->static_private, private_key,
  265. NOISE_PUBLIC_KEY_LEN);
  266. curve25519_clamp_secret(static_identity->static_private);
  267. static_identity->has_identity = curve25519_generate_public(
  268. static_identity->static_public, private_key);
  269. }
  270. static void hmac(u8 *out, const u8 *in, const u8 *key, const size_t inlen, const size_t keylen)
  271. {
  272. struct blake2s_state state;
  273. u8 x_key[BLAKE2S_BLOCK_SIZE] __aligned(__alignof__(u32)) = { 0 };
  274. u8 i_hash[BLAKE2S_HASH_SIZE] __aligned(__alignof__(u32));
  275. int i;
  276. if (keylen > BLAKE2S_BLOCK_SIZE) {
  277. blake2s_init(&state, BLAKE2S_HASH_SIZE);
  278. blake2s_update(&state, key, keylen);
  279. blake2s_final(&state, x_key);
  280. } else
  281. memcpy(x_key, key, keylen);
  282. for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
  283. x_key[i] ^= 0x36;
  284. blake2s_init(&state, BLAKE2S_HASH_SIZE);
  285. blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
  286. blake2s_update(&state, in, inlen);
  287. blake2s_final(&state, i_hash);
  288. for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
  289. x_key[i] ^= 0x5c ^ 0x36;
  290. blake2s_init(&state, BLAKE2S_HASH_SIZE);
  291. blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
  292. blake2s_update(&state, i_hash, BLAKE2S_HASH_SIZE);
  293. blake2s_final(&state, i_hash);
  294. memcpy(out, i_hash, BLAKE2S_HASH_SIZE);
  295. memzero_explicit(x_key, BLAKE2S_BLOCK_SIZE);
  296. memzero_explicit(i_hash, BLAKE2S_HASH_SIZE);
  297. }
  298. /* This is Hugo Krawczyk's HKDF:
  299. * - https://eprint.iacr.org/2010/264.pdf
  300. * - https://tools.ietf.org/html/rfc5869
  301. */
  302. static void kdf(u8 *first_dst, u8 *second_dst, u8 *third_dst, const u8 *data,
  303. size_t first_len, size_t second_len, size_t third_len,
  304. size_t data_len, const u8 chaining_key[NOISE_HASH_LEN])
  305. {
  306. u8 output[BLAKE2S_HASH_SIZE + 1];
  307. u8 secret[BLAKE2S_HASH_SIZE];
  308. WARN_ON(IS_ENABLED(DEBUG) &&
  309. (first_len > BLAKE2S_HASH_SIZE ||
  310. second_len > BLAKE2S_HASH_SIZE ||
  311. third_len > BLAKE2S_HASH_SIZE ||
  312. ((second_len || second_dst || third_len || third_dst) &&
  313. (!first_len || !first_dst)) ||
  314. ((third_len || third_dst) && (!second_len || !second_dst))));
  315. /* Extract entropy from data into secret */
  316. hmac(secret, data, chaining_key, data_len, NOISE_HASH_LEN);
  317. if (!first_dst || !first_len)
  318. goto out;
  319. /* Expand first key: key = secret, data = 0x1 */
  320. output[0] = 1;
  321. hmac(output, output, secret, 1, BLAKE2S_HASH_SIZE);
  322. memcpy(first_dst, output, first_len);
  323. if (!second_dst || !second_len)
  324. goto out;
  325. /* Expand second key: key = secret, data = first-key || 0x2 */
  326. output[BLAKE2S_HASH_SIZE] = 2;
  327. hmac(output, output, secret, BLAKE2S_HASH_SIZE + 1, BLAKE2S_HASH_SIZE);
  328. memcpy(second_dst, output, second_len);
  329. if (!third_dst || !third_len)
  330. goto out;
  331. /* Expand third key: key = secret, data = second-key || 0x3 */
  332. output[BLAKE2S_HASH_SIZE] = 3;
  333. hmac(output, output, secret, BLAKE2S_HASH_SIZE + 1, BLAKE2S_HASH_SIZE);
  334. memcpy(third_dst, output, third_len);
  335. out:
  336. /* Clear sensitive data from stack */
  337. memzero_explicit(secret, BLAKE2S_HASH_SIZE);
  338. memzero_explicit(output, BLAKE2S_HASH_SIZE + 1);
  339. }
  340. static void derive_keys(struct noise_symmetric_key *first_dst,
  341. struct noise_symmetric_key *second_dst,
  342. const u8 chaining_key[NOISE_HASH_LEN])
  343. {
  344. u64 birthdate = ktime_get_coarse_boottime_ns();
  345. kdf(first_dst->key, second_dst->key, NULL, NULL,
  346. NOISE_SYMMETRIC_KEY_LEN, NOISE_SYMMETRIC_KEY_LEN, 0, 0,
  347. chaining_key);
  348. first_dst->birthdate = second_dst->birthdate = birthdate;
  349. first_dst->is_valid = second_dst->is_valid = true;
  350. }
  351. static bool __must_check mix_dh(u8 chaining_key[NOISE_HASH_LEN],
  352. u8 key[NOISE_SYMMETRIC_KEY_LEN],
  353. const u8 private[NOISE_PUBLIC_KEY_LEN],
  354. const u8 public[NOISE_PUBLIC_KEY_LEN])
  355. {
  356. u8 dh_calculation[NOISE_PUBLIC_KEY_LEN];
  357. if (unlikely(!curve25519(dh_calculation, private, public)))
  358. return false;
  359. kdf(chaining_key, key, NULL, dh_calculation, NOISE_HASH_LEN,
  360. NOISE_SYMMETRIC_KEY_LEN, 0, NOISE_PUBLIC_KEY_LEN, chaining_key);
  361. memzero_explicit(dh_calculation, NOISE_PUBLIC_KEY_LEN);
  362. return true;
  363. }
  364. static bool __must_check mix_precomputed_dh(u8 chaining_key[NOISE_HASH_LEN],
  365. u8 key[NOISE_SYMMETRIC_KEY_LEN],
  366. const u8 precomputed[NOISE_PUBLIC_KEY_LEN])
  367. {
  368. static u8 zero_point[NOISE_PUBLIC_KEY_LEN];
  369. if (unlikely(!crypto_memneq(precomputed, zero_point, NOISE_PUBLIC_KEY_LEN)))
  370. return false;
  371. kdf(chaining_key, key, NULL, precomputed, NOISE_HASH_LEN,
  372. NOISE_SYMMETRIC_KEY_LEN, 0, NOISE_PUBLIC_KEY_LEN,
  373. chaining_key);
  374. return true;
  375. }
  376. static void mix_hash(u8 hash[NOISE_HASH_LEN], const u8 *src, size_t src_len)
  377. {
  378. struct blake2s_state blake;
  379. blake2s_init(&blake, NOISE_HASH_LEN);
  380. blake2s_update(&blake, hash, NOISE_HASH_LEN);
  381. blake2s_update(&blake, src, src_len);
  382. blake2s_final(&blake, hash);
  383. }
  384. static void mix_psk(u8 chaining_key[NOISE_HASH_LEN], u8 hash[NOISE_HASH_LEN],
  385. u8 key[NOISE_SYMMETRIC_KEY_LEN],
  386. const u8 psk[NOISE_SYMMETRIC_KEY_LEN])
  387. {
  388. u8 temp_hash[NOISE_HASH_LEN];
  389. kdf(chaining_key, temp_hash, key, psk, NOISE_HASH_LEN, NOISE_HASH_LEN,
  390. NOISE_SYMMETRIC_KEY_LEN, NOISE_SYMMETRIC_KEY_LEN, chaining_key);
  391. mix_hash(hash, temp_hash, NOISE_HASH_LEN);
  392. memzero_explicit(temp_hash, NOISE_HASH_LEN);
  393. }
  394. static void handshake_init(u8 chaining_key[NOISE_HASH_LEN],
  395. u8 hash[NOISE_HASH_LEN],
  396. const u8 remote_static[NOISE_PUBLIC_KEY_LEN])
  397. {
  398. memcpy(hash, handshake_init_hash, NOISE_HASH_LEN);
  399. memcpy(chaining_key, handshake_init_chaining_key, NOISE_HASH_LEN);
  400. mix_hash(hash, remote_static, NOISE_PUBLIC_KEY_LEN);
  401. }
  402. static void message_encrypt(u8 *dst_ciphertext, const u8 *src_plaintext,
  403. size_t src_len, u8 key[NOISE_SYMMETRIC_KEY_LEN],
  404. u8 hash[NOISE_HASH_LEN])
  405. {
  406. chacha20poly1305_encrypt(dst_ciphertext, src_plaintext, src_len, hash,
  407. NOISE_HASH_LEN,
  408. 0 /* Always zero for Noise_IK */, key);
  409. mix_hash(hash, dst_ciphertext, noise_encrypted_len(src_len));
  410. }
  411. static bool message_decrypt(u8 *dst_plaintext, const u8 *src_ciphertext,
  412. size_t src_len, u8 key[NOISE_SYMMETRIC_KEY_LEN],
  413. u8 hash[NOISE_HASH_LEN])
  414. {
  415. if (!chacha20poly1305_decrypt(dst_plaintext, src_ciphertext, src_len,
  416. hash, NOISE_HASH_LEN,
  417. 0 /* Always zero for Noise_IK */, key))
  418. return false;
  419. mix_hash(hash, src_ciphertext, src_len);
  420. return true;
  421. }
  422. static void message_ephemeral(u8 ephemeral_dst[NOISE_PUBLIC_KEY_LEN],
  423. const u8 ephemeral_src[NOISE_PUBLIC_KEY_LEN],
  424. u8 chaining_key[NOISE_HASH_LEN],
  425. u8 hash[NOISE_HASH_LEN])
  426. {
  427. if (ephemeral_dst != ephemeral_src)
  428. memcpy(ephemeral_dst, ephemeral_src, NOISE_PUBLIC_KEY_LEN);
  429. mix_hash(hash, ephemeral_src, NOISE_PUBLIC_KEY_LEN);
  430. kdf(chaining_key, NULL, NULL, ephemeral_src, NOISE_HASH_LEN, 0, 0,
  431. NOISE_PUBLIC_KEY_LEN, chaining_key);
  432. }
  433. static void tai64n_now(u8 output[NOISE_TIMESTAMP_LEN])
  434. {
  435. struct timespec64 now;
  436. ktime_get_real_ts64(&now);
  437. /* In order to prevent some sort of infoleak from precise timers, we
  438. * round down the nanoseconds part to the closest rounded-down power of
  439. * two to the maximum initiations per second allowed anyway by the
  440. * implementation.
  441. */
  442. now.tv_nsec = ALIGN_DOWN(now.tv_nsec,
  443. rounddown_pow_of_two(NSEC_PER_SEC / INITIATIONS_PER_SECOND));
  444. /* https://cr.yp.to/libtai/tai64.html */
  445. *(__be64 *)output = cpu_to_be64(0x400000000000000aULL + now.tv_sec);
  446. *(__be32 *)(output + sizeof(__be64)) = cpu_to_be32(now.tv_nsec);
  447. }
  448. bool
  449. wg_noise_handshake_create_initiation(struct message_handshake_initiation *dst,
  450. struct noise_handshake *handshake)
  451. {
  452. u8 timestamp[NOISE_TIMESTAMP_LEN];
  453. u8 key[NOISE_SYMMETRIC_KEY_LEN];
  454. bool ret = false;
  455. /* We need to wait for crng _before_ taking any locks, since
  456. * curve25519_generate_secret uses get_random_bytes_wait.
  457. */
  458. wait_for_random_bytes();
  459. down_read(&handshake->static_identity->lock);
  460. down_write(&handshake->lock);
  461. if (unlikely(!handshake->static_identity->has_identity))
  462. goto out;
  463. dst->header.type = cpu_to_le32(MESSAGE_HANDSHAKE_INITIATION);
  464. handshake_init(handshake->chaining_key, handshake->hash,
  465. handshake->remote_static);
  466. /* e */
  467. curve25519_generate_secret(handshake->ephemeral_private);
  468. if (!curve25519_generate_public(dst->unencrypted_ephemeral,
  469. handshake->ephemeral_private))
  470. goto out;
  471. message_ephemeral(dst->unencrypted_ephemeral,
  472. dst->unencrypted_ephemeral, handshake->chaining_key,
  473. handshake->hash);
  474. /* es */
  475. if (!mix_dh(handshake->chaining_key, key, handshake->ephemeral_private,
  476. handshake->remote_static))
  477. goto out;
  478. /* s */
  479. message_encrypt(dst->encrypted_static,
  480. handshake->static_identity->static_public,
  481. NOISE_PUBLIC_KEY_LEN, key, handshake->hash);
  482. /* ss */
  483. if (!mix_precomputed_dh(handshake->chaining_key, key,
  484. handshake->precomputed_static_static))
  485. goto out;
  486. /* {t} */
  487. tai64n_now(timestamp);
  488. message_encrypt(dst->encrypted_timestamp, timestamp,
  489. NOISE_TIMESTAMP_LEN, key, handshake->hash);
  490. dst->sender_index = wg_index_hashtable_insert(
  491. handshake->entry.peer->device->index_hashtable,
  492. &handshake->entry);
  493. handshake->state = HANDSHAKE_CREATED_INITIATION;
  494. ret = true;
  495. out:
  496. up_write(&handshake->lock);
  497. up_read(&handshake->static_identity->lock);
  498. memzero_explicit(key, NOISE_SYMMETRIC_KEY_LEN);
  499. return ret;
  500. }
  501. struct wg_peer *
  502. wg_noise_handshake_consume_initiation(struct message_handshake_initiation *src,
  503. struct wg_device *wg)
  504. {
  505. struct wg_peer *peer = NULL, *ret_peer = NULL;
  506. struct noise_handshake *handshake;
  507. bool replay_attack, flood_attack;
  508. u8 key[NOISE_SYMMETRIC_KEY_LEN];
  509. u8 chaining_key[NOISE_HASH_LEN];
  510. u8 hash[NOISE_HASH_LEN];
  511. u8 s[NOISE_PUBLIC_KEY_LEN];
  512. u8 e[NOISE_PUBLIC_KEY_LEN];
  513. u8 t[NOISE_TIMESTAMP_LEN];
  514. u64 initiation_consumption;
  515. down_read(&wg->static_identity.lock);
  516. if (unlikely(!wg->static_identity.has_identity))
  517. goto out;
  518. handshake_init(chaining_key, hash, wg->static_identity.static_public);
  519. /* e */
  520. message_ephemeral(e, src->unencrypted_ephemeral, chaining_key, hash);
  521. /* es */
  522. if (!mix_dh(chaining_key, key, wg->static_identity.static_private, e))
  523. goto out;
  524. /* s */
  525. if (!message_decrypt(s, src->encrypted_static,
  526. sizeof(src->encrypted_static), key, hash))
  527. goto out;
  528. /* Lookup which peer we're actually talking to */
  529. peer = wg_pubkey_hashtable_lookup(wg->peer_hashtable, s);
  530. if (!peer)
  531. goto out;
  532. handshake = &peer->handshake;
  533. /* ss */
  534. if (!mix_precomputed_dh(chaining_key, key,
  535. handshake->precomputed_static_static))
  536. goto out;
  537. /* {t} */
  538. if (!message_decrypt(t, src->encrypted_timestamp,
  539. sizeof(src->encrypted_timestamp), key, hash))
  540. goto out;
  541. down_read(&handshake->lock);
  542. replay_attack = memcmp(t, handshake->latest_timestamp,
  543. NOISE_TIMESTAMP_LEN) <= 0;
  544. flood_attack = (s64)handshake->last_initiation_consumption +
  545. NSEC_PER_SEC / INITIATIONS_PER_SECOND >
  546. (s64)ktime_get_coarse_boottime_ns();
  547. up_read(&handshake->lock);
  548. if (replay_attack || flood_attack)
  549. goto out;
  550. /* Success! Copy everything to peer */
  551. down_write(&handshake->lock);
  552. memcpy(handshake->remote_ephemeral, e, NOISE_PUBLIC_KEY_LEN);
  553. if (memcmp(t, handshake->latest_timestamp, NOISE_TIMESTAMP_LEN) > 0)
  554. memcpy(handshake->latest_timestamp, t, NOISE_TIMESTAMP_LEN);
  555. memcpy(handshake->hash, hash, NOISE_HASH_LEN);
  556. memcpy(handshake->chaining_key, chaining_key, NOISE_HASH_LEN);
  557. handshake->remote_index = src->sender_index;
  558. initiation_consumption = ktime_get_coarse_boottime_ns();
  559. if ((s64)(handshake->last_initiation_consumption - initiation_consumption) < 0)
  560. handshake->last_initiation_consumption = initiation_consumption;
  561. handshake->state = HANDSHAKE_CONSUMED_INITIATION;
  562. up_write(&handshake->lock);
  563. ret_peer = peer;
  564. out:
  565. memzero_explicit(key, NOISE_SYMMETRIC_KEY_LEN);
  566. memzero_explicit(hash, NOISE_HASH_LEN);
  567. memzero_explicit(chaining_key, NOISE_HASH_LEN);
  568. up_read(&wg->static_identity.lock);
  569. if (!ret_peer)
  570. wg_peer_put(peer);
  571. return ret_peer;
  572. }
  573. bool wg_noise_handshake_create_response(struct message_handshake_response *dst,
  574. struct noise_handshake *handshake)
  575. {
  576. u8 key[NOISE_SYMMETRIC_KEY_LEN];
  577. bool ret = false;
  578. /* We need to wait for crng _before_ taking any locks, since
  579. * curve25519_generate_secret uses get_random_bytes_wait.
  580. */
  581. wait_for_random_bytes();
  582. down_read(&handshake->static_identity->lock);
  583. down_write(&handshake->lock);
  584. if (handshake->state != HANDSHAKE_CONSUMED_INITIATION)
  585. goto out;
  586. dst->header.type = cpu_to_le32(MESSAGE_HANDSHAKE_RESPONSE);
  587. dst->receiver_index = handshake->remote_index;
  588. /* e */
  589. curve25519_generate_secret(handshake->ephemeral_private);
  590. if (!curve25519_generate_public(dst->unencrypted_ephemeral,
  591. handshake->ephemeral_private))
  592. goto out;
  593. message_ephemeral(dst->unencrypted_ephemeral,
  594. dst->unencrypted_ephemeral, handshake->chaining_key,
  595. handshake->hash);
  596. /* ee */
  597. if (!mix_dh(handshake->chaining_key, NULL, handshake->ephemeral_private,
  598. handshake->remote_ephemeral))
  599. goto out;
  600. /* se */
  601. if (!mix_dh(handshake->chaining_key, NULL, handshake->ephemeral_private,
  602. handshake->remote_static))
  603. goto out;
  604. /* psk */
  605. mix_psk(handshake->chaining_key, handshake->hash, key,
  606. handshake->preshared_key);
  607. /* {} */
  608. message_encrypt(dst->encrypted_nothing, NULL, 0, key, handshake->hash);
  609. dst->sender_index = wg_index_hashtable_insert(
  610. handshake->entry.peer->device->index_hashtable,
  611. &handshake->entry);
  612. handshake->state = HANDSHAKE_CREATED_RESPONSE;
  613. ret = true;
  614. out:
  615. up_write(&handshake->lock);
  616. up_read(&handshake->static_identity->lock);
  617. memzero_explicit(key, NOISE_SYMMETRIC_KEY_LEN);
  618. return ret;
  619. }
  620. struct wg_peer *
  621. wg_noise_handshake_consume_response(struct message_handshake_response *src,
  622. struct wg_device *wg)
  623. {
  624. enum noise_handshake_state state = HANDSHAKE_ZEROED;
  625. struct wg_peer *peer = NULL, *ret_peer = NULL;
  626. struct noise_handshake *handshake;
  627. u8 key[NOISE_SYMMETRIC_KEY_LEN];
  628. u8 hash[NOISE_HASH_LEN];
  629. u8 chaining_key[NOISE_HASH_LEN];
  630. u8 e[NOISE_PUBLIC_KEY_LEN];
  631. u8 ephemeral_private[NOISE_PUBLIC_KEY_LEN];
  632. u8 static_private[NOISE_PUBLIC_KEY_LEN];
  633. u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN];
  634. down_read(&wg->static_identity.lock);
  635. if (unlikely(!wg->static_identity.has_identity))
  636. goto out;
  637. handshake = (struct noise_handshake *)wg_index_hashtable_lookup(
  638. wg->index_hashtable, INDEX_HASHTABLE_HANDSHAKE,
  639. src->receiver_index, &peer);
  640. if (unlikely(!handshake))
  641. goto out;
  642. down_read(&handshake->lock);
  643. state = handshake->state;
  644. memcpy(hash, handshake->hash, NOISE_HASH_LEN);
  645. memcpy(chaining_key, handshake->chaining_key, NOISE_HASH_LEN);
  646. memcpy(ephemeral_private, handshake->ephemeral_private,
  647. NOISE_PUBLIC_KEY_LEN);
  648. memcpy(preshared_key, handshake->preshared_key,
  649. NOISE_SYMMETRIC_KEY_LEN);
  650. up_read(&handshake->lock);
  651. if (state != HANDSHAKE_CREATED_INITIATION)
  652. goto fail;
  653. /* e */
  654. message_ephemeral(e, src->unencrypted_ephemeral, chaining_key, hash);
  655. /* ee */
  656. if (!mix_dh(chaining_key, NULL, ephemeral_private, e))
  657. goto fail;
  658. /* se */
  659. if (!mix_dh(chaining_key, NULL, wg->static_identity.static_private, e))
  660. goto fail;
  661. /* psk */
  662. mix_psk(chaining_key, hash, key, preshared_key);
  663. /* {} */
  664. if (!message_decrypt(NULL, src->encrypted_nothing,
  665. sizeof(src->encrypted_nothing), key, hash))
  666. goto fail;
  667. /* Success! Copy everything to peer */
  668. down_write(&handshake->lock);
  669. /* It's important to check that the state is still the same, while we
  670. * have an exclusive lock.
  671. */
  672. if (handshake->state != state) {
  673. up_write(&handshake->lock);
  674. goto fail;
  675. }
  676. memcpy(handshake->remote_ephemeral, e, NOISE_PUBLIC_KEY_LEN);
  677. memcpy(handshake->hash, hash, NOISE_HASH_LEN);
  678. memcpy(handshake->chaining_key, chaining_key, NOISE_HASH_LEN);
  679. handshake->remote_index = src->sender_index;
  680. handshake->state = HANDSHAKE_CONSUMED_RESPONSE;
  681. up_write(&handshake->lock);
  682. ret_peer = peer;
  683. goto out;
  684. fail:
  685. wg_peer_put(peer);
  686. out:
  687. memzero_explicit(key, NOISE_SYMMETRIC_KEY_LEN);
  688. memzero_explicit(hash, NOISE_HASH_LEN);
  689. memzero_explicit(chaining_key, NOISE_HASH_LEN);
  690. memzero_explicit(ephemeral_private, NOISE_PUBLIC_KEY_LEN);
  691. memzero_explicit(static_private, NOISE_PUBLIC_KEY_LEN);
  692. memzero_explicit(preshared_key, NOISE_SYMMETRIC_KEY_LEN);
  693. up_read(&wg->static_identity.lock);
  694. return ret_peer;
  695. }
  696. bool wg_noise_handshake_begin_session(struct noise_handshake *handshake,
  697. struct noise_keypairs *keypairs)
  698. {
  699. struct noise_keypair *new_keypair;
  700. bool ret = false;
  701. down_write(&handshake->lock);
  702. if (handshake->state != HANDSHAKE_CREATED_RESPONSE &&
  703. handshake->state != HANDSHAKE_CONSUMED_RESPONSE)
  704. goto out;
  705. new_keypair = keypair_create(handshake->entry.peer);
  706. if (!new_keypair)
  707. goto out;
  708. new_keypair->i_am_the_initiator = handshake->state ==
  709. HANDSHAKE_CONSUMED_RESPONSE;
  710. new_keypair->remote_index = handshake->remote_index;
  711. if (new_keypair->i_am_the_initiator)
  712. derive_keys(&new_keypair->sending, &new_keypair->receiving,
  713. handshake->chaining_key);
  714. else
  715. derive_keys(&new_keypair->receiving, &new_keypair->sending,
  716. handshake->chaining_key);
  717. handshake_zero(handshake);
  718. rcu_read_lock_bh();
  719. if (likely(!READ_ONCE(container_of(handshake, struct wg_peer,
  720. handshake)->is_dead))) {
  721. add_new_keypair(keypairs, new_keypair);
  722. net_dbg_ratelimited("%s: Keypair %llu created for peer %llu\n",
  723. handshake->entry.peer->device->dev->name,
  724. new_keypair->internal_id,
  725. handshake->entry.peer->internal_id);
  726. ret = wg_index_hashtable_replace(
  727. handshake->entry.peer->device->index_hashtable,
  728. &handshake->entry, &new_keypair->entry);
  729. } else {
  730. kfree_sensitive(new_keypair);
  731. }
  732. rcu_read_unlock_bh();
  733. out:
  734. up_write(&handshake->lock);
  735. return ret;
  736. }