diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-07-13 23:15:01 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-07-13 23:15:01 +0200 |
commit | 7805fd9f596870046c3bc607f5e5199b738a02cb (patch) | |
tree | b47a20e6e1736c5beafd473748d3b00f9bb7f847 | |
parent | Add peers and psks to device (diff) | |
download | wireguard-rs-7805fd9f596870046c3bc607f5e5199b738a02cb.tar.xz wireguard-rs-7805fd9f596870046c3bc607f5e5199b738a02cb.zip |
Begin work on creating initiation
-rw-r--r-- | Cargo.lock | 220 | ||||
-rw-r--r-- | Cargo.toml | 6 | ||||
-rw-r--r-- | src/machine.rs | 26 | ||||
-rw-r--r-- | src/messages.rs | 48 | ||||
-rw-r--r-- | src/noise.rs | 114 | ||||
-rw-r--r-- | src/types.rs | 25 |
6 files changed, 353 insertions, 86 deletions
@@ -6,25 +6,45 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "byteorder" -version = "1.3.1" +name = "bitflags" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "c2-chacha" -version = "0.2.2" +name = "blake2" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] +name = "byte-tools" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byteorder" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "cc" version = "1.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "chacha20-poly1305-aead" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "clear_on_drop" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -33,6 +53,28 @@ dependencies = [ ] [[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "constant_time_eq" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "crypto-mac" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "curve25519-dalek" version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -53,6 +95,11 @@ dependencies = [ ] [[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "generic-array" version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -61,20 +108,20 @@ dependencies = [ ] [[package]] -name = "getrandom" -version = "0.1.6" +name = "generic-array" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.59 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "lazy_static" -version = "1.3.0" +name = "hmac" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -83,30 +130,35 @@ version = "0.2.59" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "ppv-lite86" -version = "0.2.5" +name = "opaque-debug" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "rand" -version = "0.7.0" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "getrandom 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.59 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_chacha 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_jitter 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rand_chacha" -version = "0.2.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -123,24 +175,72 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "rand_core" -version = "0.5.0" +name = "rand_hc" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "getrandom 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "rand_hc" -version = "0.2.0" +name = "rand_isaac" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_jitter" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.59 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_os" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.59 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_pcg" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_xorshift" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "spin" -version = "0.5.0" +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "subtle" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -157,11 +257,34 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "wg-handshake" version = "0.1.0" dependencies = [ - "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "blake2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "chacha20-poly1305-aead 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", + "hmac 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "x25519-dalek 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] +name = "winapi" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "x25519-dalek" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -173,24 +296,39 @@ dependencies = [ [metadata] "checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf" +"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" +"checksum blake2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "91721a6330935673395a0607df4d49a9cb90ae12d259f1b3e0a3f6e1d486872e" +"checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" -"checksum c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7d64d04786e0f528460fc884753cf8dddcc466be308f6026f8e355c41a0e4101" "checksum cc 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)" = "ad0daef304fa0b4238f5f7ed7178774b43b06f6a9b6509f6642bef4ff1f7b9b2" +"checksum chacha20-poly1305-aead 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77d2058ba29594f69c75e8a9018e0485e3914ca5084e3613cd64529042f5423b" "checksum clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "97276801e127ffb46b66ce23f35cc96bd454fa311294bced4bbace7baa8b1d17" +"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" +"checksum crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5" "checksum curve25519-dalek 1.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1f8a6fc0376eb52dc18af94915cc04dfdf8353746c0e8c550ae683a0815e5c1" "checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c" +"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592" -"checksum getrandom 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e65cce4e5084b14874c4e7097f38cab54f47ee554f9194673456ea379dcc4c55" -"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" +"checksum generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0ed1e761351b56f54eb9dcd0cfaca9fd0daecf93918e1cfc01c8a3d26ee7adcd" +"checksum hmac 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5dcb5e64cda4c23119ab41ba960d1e170a774c8e4b9d9e6a9bc18aabf5e59695" "checksum libc 0.2.59 (registry+https://github.com/rust-lang/crates.io-index)" = "3262021842bf00fe07dbd6cf34ff25c99d7a7ebef8deea84db72be3ea3bb0aff" -"checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b" -"checksum rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d47eab0e83d9693d40f825f86948aa16eff6750ead4bdffc4ab95b8b3a7f052c" -"checksum rand_chacha 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e193067942ef6f485a349a113329140d0ab9e2168ce92274499bb0e9a4190d9d" +"checksum opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "93f5bb2e8e8dec81642920ccff6b61f1eb94fa3020c5a325c9851ff604152409" +"checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" +"checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" "checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" "checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0" -"checksum rand_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "615e683324e75af5d43d8f7a39ffe3ee4a9dc42c5c701167a71dc59c3a493aca" -"checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -"checksum spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44363f6f51401c34e7be73db0db371c04705d35efbe9f7d6082e03a921a32c55" +"checksum rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" +"checksum rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" +"checksum rand_jitter 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" +"checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +"checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" +"checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" +"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +"checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" "checksum subtle 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "702662512f3ddeb74a64ce2fbbf3707ee1b6bb663d28bb054e0779bbc720d926" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" +"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum x25519-dalek 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4aca1ba6bec2719576bd20dfe5b24d9359552e616d10bff257e50cd85f745d17" @@ -5,7 +5,11 @@ authors = ["Mathias Hall-Andersen <mathias@hall-andersen.dk>"] edition = "2018" [dependencies] -rand = "0.7.0" +rand = "0.6.5" +blake2 = "0.8.0" +hmac = "0.7.1" +chacha20-poly1305-aead = "0.1.2" +generic-array = "0.13" [dependencies.x25519-dalek] version = "^0.5" diff --git a/src/machine.rs b/src/machine.rs index 8cb6fba..a056d0c 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -6,30 +6,10 @@ use rand::rngs::OsRng; use x25519_dalek::PublicKey; use x25519_dalek::StaticSecret; -use x25519_dalek::SharedSecret; -use crate::noise; -use crate::types; - -pub struct Output ( - Option<types::KeyPair>, // resulting key-pair of successful handshake - Option<Vec<u8>> // message to send -); -pub struct Peer { - // mutable state - m : Mutex<State>, - - // constant state - pk : PublicKey, // public key of peer - ss : SharedSecret, // precomputed DH(static, static) - psk : [u8; 32] // psk of peer -} - -enum State { - Reset, - InitiationSent, -} +use crate::noise; +use crate::types::*; struct Device { sk : StaticSecret, // static secret key @@ -162,7 +142,7 @@ impl Device { impl Device { // allocate a new index (id), for peer with idx fn allocate(&self, idx : usize) -> u32 { - let mut rng = OsRng; + let mut rng = OsRng::new().unwrap(); let mut table = self.ids.lock().unwrap(); loop { let id = rng.gen(); diff --git a/src/messages.rs b/src/messages.rs index 1ec50bf..92a5a3e 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -5,10 +5,7 @@ const SIZE_TAG : usize = 16; const SIZE_X25519_POINT : usize = 32; const SIZE_TIMESTAMP : usize = 12; -pub const SIZE_MESSAGE_INITIATE : usize = 116; -pub const SIZE_MESSAGE_RESPONSE : usize = 116; - -pub const TYPE_INITIATE : u8 = 1; +pub const TYPE_INITIATION : u8 = 1; pub const TYPE_RESPONSE : u8 = 2; /* Wireguard handshake initiation message @@ -16,16 +13,16 @@ pub const TYPE_RESPONSE : u8 = 2; */ #[repr(C)] #[derive(Copy, Clone)] -struct MessageInitiate { - f_type : u8, - f_reserved : [u8; 3], - f_sender : u32, - f_ephemeral : [u8; SIZE_X25519_POINT], - f_static : [u8; SIZE_X25519_POINT + SIZE_TAG], - f_timestamp : [u8; SIZE_TIMESTAMP + SIZE_TAG], +pub struct Initiation { + pub f_type : u8, + f_reserved : [u8; 3], + pub f_sender : u32, + pub f_ephemeral : [u8; SIZE_X25519_POINT], + pub f_static : [u8; SIZE_X25519_POINT + SIZE_TAG], + pub f_timestamp : [u8; SIZE_TIMESTAMP + SIZE_TAG], } -impl From<&[u8]> for MessageInitiate { +impl From<&[u8]> for Initiation { fn from(b: &[u8]) -> Self { // create owned copy let mut owned = [0u8; mem::size_of::<Self>()]; @@ -44,7 +41,7 @@ impl From<&[u8]> for MessageInitiate { } } -impl Into<Vec<u8>> for MessageInitiate { +impl Into<Vec<u8>> for Initiation { fn into(self) -> Vec<u8> { // correct endianness let mut msg = self; @@ -61,7 +58,7 @@ impl Into<Vec<u8>> for MessageInitiate { } } -impl fmt::Debug for MessageInitiate { +impl fmt::Debug for Initiation { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "MessageInitiate {{ type = {} }}", @@ -70,8 +67,21 @@ impl fmt::Debug for MessageInitiate { } } +impl Default for Initiation { + fn default() -> Self { + Self { + f_type : TYPE_INITIATION, + f_reserved : [0u8; 3], + f_sender : 0, + f_ephemeral : [0u8; SIZE_X25519_POINT], + f_static : [0u8; SIZE_X25519_POINT + SIZE_TAG], + f_timestamp : [0u8; SIZE_TIMESTAMP + SIZE_TAG], + } + } +} + #[cfg(test)] -impl PartialEq for MessageInitiate { +impl PartialEq for Initiation { fn eq(&self, other: &Self) -> bool { self.f_type == other.f_type && self.f_reserved == other.f_reserved && @@ -83,7 +93,7 @@ impl PartialEq for MessageInitiate { } #[cfg(test)] -impl Eq for MessageInitiate {} +impl Eq for Initiation {} /* Wireguard handshake responder message @@ -91,7 +101,7 @@ impl Eq for MessageInitiate {} */ #[repr(C)] #[derive(Copy, Clone)] -struct MessageResponse { +pub struct MessageResponse { f_type : u8, f_reserved : [u8; 3], f_sender : u32, @@ -190,7 +200,7 @@ mod tests { #[test] fn message_initiate_identity() { - let msg = MessageInitiate { + let msg = Initiation { f_type : TYPE_RESPONSE, f_reserved : [0u8; 3], f_sender : 575757, @@ -224,6 +234,6 @@ mod tests { }; let buf : Vec<u8> = msg.into(); - assert_eq!(msg, MessageInitiate::from(&buf[..])); + assert_eq!(msg, Initiation::from(&buf[..])); } } diff --git a/src/noise.rs b/src/noise.rs index 2d4c122..6d3850c 100644 --- a/src/noise.rs +++ b/src/noise.rs @@ -1,6 +1,116 @@ -use crate::machine::Peer; -use crate::machine::Output; +use hmac::{Mac, Hmac}; +use blake2::{Blake2s, Digest}; + +use x25519_dalek::PublicKey; +use x25519_dalek::StaticSecret; +use x25519_dalek::SharedSecret; + +use rand::rngs::OsRng; + +use generic_array::*; + +use crate::types::*; +use crate::messages; + +type HMACBlake2s = Hmac<Blake2s>; + +/* Internal functions for processing and creating noise messages */ + +const IDENTIFIER : &[u8] = b"WireGuard v1 zx2c4 Jason@zx2c4.com"; +const CONSTRUCTION : &[u8] = b"Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s"; + +const SIZE_CK : usize = 32; +const SIZE_HS : usize = 32; + +// C := Hash(Construction) +const INITIAL_CK : [u8; SIZE_CK] = [ + 0x60, 0xe2, 0x6d, 0xae, 0xf3, 0x27, 0xef, 0xc0, + 0x2e, 0xc3, 0x35, 0xe2, 0xa0, 0x25, 0xd2, 0xd0, + 0x16, 0xeb, 0x42, 0x06, 0xf8, 0x72, 0x77, 0xf5, + 0x2d, 0x38, 0xd1, 0x98, 0x8b, 0x78, 0xcd, 0x36 +]; + +// H := Hash(C || Identifier) +const INITIAL_HS : [u8; SIZE_HS] = [ + 0x22, 0x11, 0xb3, 0x61, 0x08, 0x1a, 0xc5, 0x66, + 0x69, 0x12, 0x43, 0xdb, 0x45, 0x8a, 0xd5, 0x32, + 0x2d, 0x9c, 0x6c, 0x66, 0x22, 0x93, 0xe8, 0xb7, + 0x0e, 0xe1, 0x9c, 0x65, 0xba, 0x07, 0x9e, 0xf3 +]; + +macro_rules! HASH { + ($input1:expr, $input2:expr) => { + { + let mut hsh = <Blake2s as Digest>::new(); + Digest::input(&mut hsh, $input1); + Digest::input(&mut hsh, $input2); + Digest::result(hsh) + } + }; +} + +macro_rules! HMAC { + ($key:expr, $input:expr) => { + HMACBlake2s::new($key).hash($input).result() + }; + + ($key:expr, $input1:expr, $input2:expr) => { + HMACBlake2s::new($key).hash($input2).hash($input2).result() + }; +} + +macro_rules! KDF1 { + ($ck:expr, $input:expr) => { + { + let t0 = HMAC!($ck, $input); + t0 + } + } +} + +macro_rules! KDF2 { + ($ck:expr, $input:expr) => { + + } +} + +macro_rules! KDF2 { + ($ck:expr, $input:expr) => { + + } +} pub fn create_initiation(peer : &Peer, id : u32) -> Result<Vec<u8>, ()> { + let mut rng = OsRng::new().unwrap(); + let mut msg : messages::Initiation = Default::default(); + + // initialize state + + let ck = INITIAL_CK; + let hs = INITIAL_HS; + let hs = HASH!(&hs, peer.pk.as_bytes()); + + msg.f_sender = id; + + // token : e + + let sk = StaticSecret::new(&mut rng); + let pk = PublicKey::from(&sk); + + msg.f_ephemeral = *pk.as_bytes(); + + // let ck = KDF1!(&ck, pk.as_bytes()); + + // token : es + + // token : s + + // token : ss + Ok(vec![]) } + +pub fn process_initiation(peer : &Peer) -> Result<Output, ()> { + Err(()) +} + diff --git a/src/types.rs b/src/types.rs index 3729555..391b029 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,3 +1,8 @@ +use std::sync::Mutex; + +use x25519_dalek::PublicKey; +use x25519_dalek::SharedSecret; + struct Key { key : [u8; 32], id : u32 @@ -8,3 +13,23 @@ pub struct KeyPair { send : Key, // key for outbound messages recv : Key // key for inbound messages } + +pub struct Output ( + Option<KeyPair>, // resulting key-pair of successful handshake + Option<Vec<u8>> // message to send +); + +pub struct Peer { + // mutable state + pub m : Mutex<State>, + + // constant state + pub pk : PublicKey, // public key of peer + pub ss : SharedSecret, // precomputed DH(static, static) + pub psk : [u8; 32] // psk of peer +} + +pub enum State { + Reset, + InitiationSent, +} |