Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to MagicInput are documented here. The project follows [Semantic Versioning](https://semver.org).

## [Unreleased]

### Changed

- **Hardened device pairing; both computers need this version.** `magicinput status` can't tell whether a nearby computer uses the same code until it connects, so its `nearby` entries drop `same_code` and gain `compatible` (same protocol version).

### Compatibility

- Both computers need this version: it doesn't connect to 0.6.2 or older, and says to update MagicInput on both computers.

## [0.6.2] - 2026-10-25

### Changed
Expand Down
67 changes: 67 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ toml = "1.1.8"
tray-icon = "0.26.1"
window-vibrancy = "0.8.1"
wry = "0.57.0"
spake2 = "0.4"
hkdf = "0.13"

[target."cfg(windows)".dependencies]
windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_UI_WindowsAndMessaging", "Win32_UI_Input_KeyboardAndMouse", "Win32_Graphics_Gdi", "Win32_System_LibraryLoader", "Win32_UI_HiDpi", "Win32_System_Threading", "Win32_System_SystemInformation", "Win32_UI_Shell", "Win32_System_Registry", "Win32_Storage_Packaging_Appx"] }
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ It installs MagicInput, works out whether this is the first or second computer,

Each computer runs the same small background service. When the pointer reaches the edge that leads to the other computer, the service hides and freezes the local pointer and forwards your keyboard, mouse and scroll input over the network; the other computer replays it as if it came from its own devices. Crossing back hands control home again.

- **Discovery:** computers advertise themselves on the local network (Bonjour/mDNS) and only talk to ones using the same pairing code.
- **Encryption:** every connection uses the Noise protocol (X25519, ChaCha20-Poly1305) with a key derived from the pairing code. A wrong code can't connect, and the code itself is never sent.
- **Discovery:** computers advertise themselves on the local network (Bonjour/mDNS) with a random id and their name, nothing derived from the pairing code. They only connect once pairing with the same code succeeds.
- **Encryption:** computers pair with a password-authenticated key exchange (SPAKE2) on the pairing code, then every connection uses the Noise protocol (X25519, ChaCha20-Poly1305) with the key it produced. A wrong code can't connect, the code itself is never sent, and failed attempts are slowed down.
- **Local only:** connections from outside your own subnet (internet, VPNs, Tailscale) are refused.
- **Keys:** keystrokes travel as layout-independent USB key codes, so each computer applies its own keyboard layout.
- **Smoothness:** each pointer update is timestamped; updates that Wi-Fi delivers late are replayed as a short glide.
Expand Down
6 changes: 4 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ Include the MagicInput, macOS and Windows versions, your network setup, reproduc
## Security boundaries

- There is no MagicInput server; the two computers talk directly.
- Every connection is encrypted with the Noise protocol (`Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s`); the pre-shared key is derived from the pairing code, which is never transmitted. Without the code a device can't complete the handshake.
- Pairing uses a password-authenticated key exchange (symmetric SPAKE2 over Ed25519, bound to the protocol version). The pairing code is never transmitted, and recording a connection doesn't let anyone test guesses of the code offline: each connection attempt can check at most one guess.
- The key from that exchange, through HKDF-SHA256 over the exchange transcript, is the pre-shared key of the Noise protocol (`Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s`) that encrypts everything afterwards. Without the code a device can't complete the handshake.
- Discovery (mDNS) advertises only a random computer id, the computer's name and the protocol version; nothing derived from the pairing code.
- Failed pairing attempts are paced: a few per address, then a growing wait between attempts, plus an overall limit per minute for addresses that never paired. Failed attempts are logged without the code.
- Only peers on the same local subnet are accepted or dialed; connections from the internet, VPNs and Tailscale addresses are refused.
- The pairing code is short (about 39 bits) so it's easy to type. Someone on your local network who records a MagicInput connection could recover it by offline guessing and then connect. The local-network restriction is the main boundary; on untrusted networks (cafés, shared offices), generate a new code afterwards. Moving to a password-authenticated key exchange (PAKE), which removes offline guessing, is planned.
- Configuration (including the pairing code) is stored unencrypted in your user profile.
- Users are responsible for the security of their operating system accounts and local network.

Expand Down
9 changes: 5 additions & 4 deletions docs/AI_SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ The output looks like this:
| `role` | Meaning | What to do |
| --- | --- | --- |
| `first` | No other computer running MagicInput was found. | Give the person this computer's `pairing_code` and tell them to install MagicInput on the other computer (they can paste the prompt from the README into an assistant there). On the other computer, pairing is `magicinput pair <this code>`. |
| `second` | Another computer runs MagicInput (`nearby`) but with a different code. | Get that computer's code and pair this one with it (below). |
| `second` | Another computer runs MagicInput (`nearby`) but isn't connected. Whether it uses the same code is only known once they connect. | Get that computer's code and pair this one with it (below). |
| `paired` | Already connected (`connected_to`). | Skip to step 5. |

`next_step` always says the next thing to do in plain words. `service` is `running`, `waiting_for_permission` (Mac permissions, step 2) or `not_running` (run `install` again).
Expand Down Expand Up @@ -153,7 +153,7 @@ PowerShell:
while (-not ((& $MI status --json | Out-String | ConvertFrom-Json).connected_to)) { Start-Sleep 3 }; & $MI status
```

Usually this takes a few seconds. If it hasn't connected after about 30 seconds, see Troubleshooting.
Usually this takes a few seconds. After a few attempts with a wrong code, the computers wait up to ten seconds between tries, so give it half a minute after pairing. If it still hasn't connected, see Troubleshooting.

## 7. Tell the person how to use it

Expand All @@ -173,8 +173,9 @@ If the person tells you where the other screen is, you can pin it without the pa
| `service: "waiting_for_permission"` (Mac) | The person still needs to turn on MagicInput under Accessibility **and** Input Monitoring (step 2). |
| `service: "not_running"` | Run `install` again. On Windows check the task: `schtasks /query /tn magicinput`. |
| `nearby` is empty on **both** computers | They're on different networks, or the network blocks discovery. Find the other computer's IP address and point this one at it: `magicinput peer 192.168.1.20`. |
| `nearby` shows the other computer with `same_code: true` but `connected_to` stays empty | Usually the Windows firewall. In PowerShell run `Get-NetConnectionProfile`; if the network is `Public`, MagicInput's rule (private networks) doesn't apply. Ask the person before changing it: `Set-NetConnectionProfile -InterfaceAlias "<name>" -NetworkCategory Private`. |
| `nearby` shows `same_code: false` | The codes differ: pair again (step 4) using the other computer's code. |
| `nearby` shows the other computer, both show the same `pairing_code`, but `connected_to` stays empty after half a minute | Usually the Windows firewall. In PowerShell run `Get-NetConnectionProfile`; if the network is `Public`, MagicInput's rule (private networks) doesn't apply. Ask the person before changing it: `Set-NetConnectionProfile -InterfaceAlias "<name>" -NetworkCategory Private`. |
| `nearby` shows the other computer and the two `pairing_code`s differ | Pair again (step 4) using the other computer's code. |
| `nearby` shows `compatible: false` | The other computer runs a different version of MagicInput: update MagicInput on both computers. |
| Connected, but the pointer doesn't cross | In Auto mode push **firmly**. With a pinned arrangement the pointer only crosses where the screens touch in Displays. |
| Can't click into some Windows apps | Apps running as administrator need MagicInput to have admin rights too; the installer sets that up. Reinstall with the admin prompt accepted. |
| Pointer stutters over Wi-Fi | See the Tips section of the README (5 GHz channel 149). |
Expand Down
5 changes: 3 additions & 2 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ Bursts of movement are merged before sending. On the receiving side, movement th

## Discovery and security

- Each computer advertises `_magicinput._tcp` over mDNS with a random id and a short tag derived from the pairing code; it only dials peers with the same tag. Of two peers, the one with the smaller id dials.
- Connections use Noise `NNpsk0_25519_ChaChaPoly_BLAKE2s` over TCP (port 24842, `TCP_NODELAY`; on macOS marked as interactive traffic for Wi-Fi). The pre-shared key is SHA-256 of the normalized pairing code. See [SECURITY.md](../SECURITY.md) for the limits of a short code.
- Each computer advertises `_magicinput._tcp` over mDNS with a random id, its name and the protocol version (`id`, `n`, `v`); nothing derived from the pairing code. It dials other MagicInput computers with the same protocol version (up to 16) and lets pairing decide whether they belong together. Of two peers, the one with the smaller id dials.
- Connections run over TCP (port 24842, `TCP_NODELAY`; on macOS marked as interactive traffic for Wi-Fi). Each side first sends `MagicInp` plus its protocol version, so a computer on another version gets a clear "update MagicInput on both computers" error. Then both run symmetric SPAKE2 (Ed25519, identity `magicinput/pake/v<version>`) on the normalized pairing code. HKDF-SHA256 of the SPAKE2 key, salted with the transcript so far, becomes the pre-shared key for Noise `NNpsk0_25519_ChaChaPoly_BLAKE2s`, which also takes the transcript as its prologue and carries all further traffic. With different codes the Noise handshake fails.
- Pairing attempts are paced per address in both directions: four in a row, then a wait that doubles up to ten seconds (forgotten after ten quiet minutes, cleared on success or when the local code changes). Incoming attempts from addresses that never paired also share a budget of 30 per minute, and at most 8 incoming connections pair at once.
- Only addresses in the subnet of one of the computer's own network interfaces are accepted or dialed; CGNAT/Tailscale ranges are excluded.

## Settings panel
Expand Down
57 changes: 37 additions & 20 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::geom::{Placement, Rect};
use crate::proto::Side;
Expand Down Expand Up @@ -146,17 +145,6 @@ impl Config {
eprintln!("Could not save {}: {e}", path.display());
}
}

/// 32-byte pre-shared key for the encrypted channel.
pub fn psk(&self) -> [u8; 32] {
Sha256::digest(format!("magicinput/v1/psk/{}", normalize_code(&self.code))).into()
}

/// Short public tag so computers only try to talk to others paired with the same code.
pub fn group(&self) -> String {
let h = Sha256::digest(format!("magicinput/v1/group/{}", normalize_code(&self.code)));
h[..4].iter().map(|b| format!("{b:02x}")).collect()
}
}

pub fn new_code() -> String {
Expand All @@ -177,11 +165,21 @@ pub fn normalize_code(code: &str) -> String {
}

fn random_string(len: usize, alphabet: &[u8]) -> String {
let mut buf = vec![0u8; len];
getrandom::fill(&mut buf).expect("no system randomness");
buf.iter()
.map(|b| alphabet[*b as usize % alphabet.len()] as char)
.collect()
let mut out = String::with_capacity(len);
let mut buf = [0u8; 32];
while out.len() < len {
getrandom::fill(&mut buf).expect("no system randomness");
out.extend(buf.iter().filter_map(|&b| pick(b, alphabet)).take(len - out.len()));
}
out
}

/// The character for one random byte, or `None` for the few top byte values that would make
/// some characters more likely than others (they are skipped and another byte is drawn).
fn pick(b: u8, alphabet: &[u8]) -> Option<char> {
let n = alphabet.len();
let usable = 256 - 256 % n;
((b as usize) < usable).then(|| alphabet[b as usize % n] as char)
}

fn hostname() -> String {
Expand Down Expand Up @@ -224,8 +222,27 @@ mod tests {
fn codes_normalize_the_same() {
let a = Config { code: "k7qp-3mzd".into(), ..Default::default() };
let b = Config { code: "K7QP 3MZD".into(), ..Default::default() };
assert_eq!(a.psk(), b.psk());
assert_eq!(a.group(), b.group());
assert_eq!(new_code().len(), 9);
assert_eq!(normalize_code(&a.code), normalize_code(&b.code));
assert_eq!(normalize_code("o0-il1"), "00111");
let code = new_code();
assert_eq!(code.len(), 9);
assert_eq!(code.as_bytes()[4], b'-');
assert!(code.bytes().filter(|&c| c != b'-').all(|c| CODE_ALPHABET.contains(&c)));
}

#[test]
fn every_code_character_is_equally_likely() {
for alphabet in [CODE_ALPHABET, b"abcdefghijklmnopqrstuvwxyz0123456789".as_slice()] {
let mut counts = std::collections::HashMap::new();
for b in 0..=255u8 {
if let Some(c) = pick(b, alphabet) {
*counts.entry(c).or_insert(0) += 1;
}
}
assert_eq!(counts.len(), alphabet.len());
let per_char = 256 / alphabet.len();
assert!(counts.values().all(|&n| n == per_char), "{counts:?}");
}
assert_eq!(random_string(500, CODE_ALPHABET).len(), 500);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn run(mut cfg: Config) {
std::process::exit(0);
});

log::info!("magicinput {} running as \"{}\" (code {})", env!("CARGO_PKG_VERSION"), cfg.name, cfg.code);
log::info!("magicinput {} running as \"{}\"", env!("CARGO_PKG_VERSION"), cfg.name);
log::info!("Looking for your other computer… (Ctrl+Alt+Esc / Control+Option+Esc brings the pointer back)");
if let Err(e) = platform::run_capture() {
exit(&e);
Expand Down
Loading
Loading