summaryrefslogtreecommitdiff
path: root/wpa-psk/src/lib.rs
blob: d0ffbc42f3bfd353fd6fad94561eb3ee11991750 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Compute the WPA-PSK of a Wi-Fi SSID and passphrase.
//!
//! # Example
//!
//! Compute and print the WPA-PSK of a valid SSID and passphrase:
//!
//! ```
//! # use wpa_psk::{Ssid, Passphrase, wpa_psk, bytes_to_hex};
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let ssid = Ssid::try_from("home")?;
//! let passphrase = Passphrase::try_from("0123-4567-89")?;
//! let psk = wpa_psk(&ssid, &passphrase);
//! assert_eq!(bytes_to_hex(&psk), "150c047b6fad724512a17fa431687048ee503d14c1ea87681d4f241beb04f5ee");
//! # Ok(())
//! # }
//! ```
//!
//! Compute the WPA-PSK of possibly invalid raw bytes:
//!
//! ```
//! # use wpa_psk::{wpa_psk_unchecked, bytes_to_hex};
//! let ssid = "bar".as_bytes();
//! let passphrase = "2short".as_bytes();
//! let psk = wpa_psk_unchecked(&ssid, &passphrase);
//! assert_eq!(bytes_to_hex(&psk), "cb5de4e4d23b2ab0bf5b9ba0fe8132c1e2af3bb52298ec801af8ad520cea3437");
//! ```

#![forbid(unsafe_code)]
#![deny(missing_docs)]

use std::{error::Error, fmt::Display};

use pbkdf2::pbkdf2_hmac;
use sha1::Sha1;

/// An SSID consisting of 1 up to 32 arbitrary bytes.
#[derive(Debug)]
pub struct Ssid<'a>(&'a [u8]);

impl<'a> TryFrom<&'a [u8]> for Ssid<'a> {
    type Error = ValidateSsidError;

    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
        if value.is_empty() {
            Err(ValidateSsidError::TooShort)
        } else if value.len() > 32 {
            Err(ValidateSsidError::TooLong)
        } else {
            Ok(Ssid(value))
        }
    }
}

impl<'a> TryFrom<&'a str> for Ssid<'a> {
    type Error = ValidateSsidError;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        Self::try_from(value.as_bytes())
    }
}

/// SSID validation error.
#[derive(Debug, PartialEq, Eq)]
pub enum ValidateSsidError {
    /// SSID is too short.
    TooShort,
    /// SSID is too long.
    TooLong,
}

impl Error for ValidateSsidError {}

impl Display for ValidateSsidError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let msg = match self {
            ValidateSsidError::TooShort => "SSID must have at least one byte",
            ValidateSsidError::TooLong => "SSID must have at most 32 bytes",
        };
        write!(f, "{msg}")
    }
}

/// A passphrase consisting of 8 up to 63 printable ASCII characters.
#[derive(Debug)]
pub struct Passphrase<'a>(&'a [u8]);

impl<'a> TryFrom<&'a [u8]> for Passphrase<'a> {
    type Error = ValidatePassphraseError;

    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
        if value.len() < 8 {
            Err(ValidatePassphraseError::TooShort)
        } else if value.len() > 63 {
            Err(ValidatePassphraseError::TooLong)
        } else if value.iter().any(|i| !matches!(i, 32u8..=126)) {
            Err(ValidatePassphraseError::InvalidByte)
        } else {
            Ok(Passphrase(value))
        }
    }
}

impl<'a> TryFrom<&'a str> for Passphrase<'a> {
    type Error = ValidatePassphraseError;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        Self::try_from(value.as_bytes())
    }
}

impl Display for Passphrase<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", std::str::from_utf8(self.0).unwrap())
    }
}

/// Passphrase validation error.
#[derive(Debug, PartialEq, Eq)]
pub enum ValidatePassphraseError {
    /// Passphrase is too short.
    TooShort,
    /// Passphrase is too long.
    TooLong,
    /// Passphrase contains a invalid byte.
    InvalidByte,
}

impl Error for ValidatePassphraseError {}

impl Display for ValidatePassphraseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let msg = match self {
            ValidatePassphraseError::TooShort => "passphrase must have at least 8 bytes",
            ValidatePassphraseError::TooLong => "passphrase must have at most 63 bytes",
            ValidatePassphraseError::InvalidByte => {
                "passphrase must consist of printable ASCII characters"
            }
        };
        write!(f, "{msg}")
    }
}

/// Returns the WPA-PSK of the given SSID and passphrase.
pub fn wpa_psk(ssid: &Ssid, passphrase: &Passphrase) -> [u8; 32] {
    wpa_psk_unchecked(ssid.0, passphrase.0)
}

/// Unchecked WPA-PSK.
/// See [`wpa_psk`].
pub fn wpa_psk_unchecked(ssid: &[u8], passphrase: &[u8]) -> [u8; 32] {
    let mut buf = [0u8; 32];
    pbkdf2_hmac::<Sha1>(passphrase, ssid, 4096, &mut buf);
    buf
}

/// Returns the hexdecimal representation of the given bytes.
pub fn bytes_to_hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn special_characters() {
        let ssid = Ssid::try_from("123abcABC.,-").unwrap();
        let passphrase = Passphrase::try_from("456defDEF *<:D").unwrap();
        assert_eq!(
            bytes_to_hex(&wpa_psk(&ssid, &passphrase)),
            "8a366e5bc51cd5d8fbbeffacc5f1af23fac30e3ac93cdcc368fafbbf63a1085c"
        );
    }

    #[test]
    fn passphrase_too_short() {
        assert_eq!(
            Passphrase::try_from("foobar").unwrap_err(),
            ValidatePassphraseError::TooShort
        );
    }

    #[test]
    fn display_passphrase() {
        assert_eq!(
            format!("{}", Passphrase::try_from("foobarbuzz").unwrap()),
            "foobarbuzz"
        );
    }
}
Generated by cgit. See skreutz.com for my tech blog and contact information.