-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathvibio.rs
More file actions
173 lines (156 loc) · 4.89 KB
/
vibio.rs
File metadata and controls
173 lines (156 loc) · 4.89 KB
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
// Buttplug Rust Source Code File - See https://buttplug.io for more info.
//
// Copyright 2016-2026 Nonpolynomial Labs LLC. All rights reserved.
//
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.
use crate::device::{
hardware::{Hardware, HardwareCommand, HardwareEvent, HardwareSubscribeCmd, HardwareWriteCmd},
protocol::{
ProtocolHandler,
ProtocolIdentifier,
ProtocolInitializer,
generic_protocol_initializer_setup,
},
};
use aes::Aes128;
use async_trait::async_trait;
use buttplug_core::errors::ButtplugDeviceError;
use buttplug_server_device_config::Endpoint;
use buttplug_server_device_config::{
ProtocolCommunicationSpecifier,
ServerDeviceDefinition,
UserDeviceIdentifier,
};
use ecb::cipher::block_padding::Pkcs7;
use ecb::cipher::{BlockDecryptMut, BlockEncryptMut, KeyInit};
use std::sync::{
Arc,
atomic::{AtomicU8, Ordering},
};
use uuid::{Uuid, uuid};
use rand::distr::Alphanumeric;
use rand::RngExt;
use regex::Regex;
use sha2::{Digest, Sha256};
type Aes128EcbEnc = ecb::Encryptor<Aes128>;
type Aes128EcbDec = ecb::Decryptor<Aes128>;
const VIBIO_PROTOCOL_UUID: Uuid = uuid!("b8c76c9e-cb42-4a94-99f4-7c2a8e5d3b2a");
const VIBIO_KEY: [u8; 16] = *b"jdk#vib%y5fir21a";
generic_protocol_initializer_setup!(Vibio, "vibio");
#[derive(Default)]
pub struct VibioInitializer {}
fn encrypt(command: String) -> Vec<u8> {
let enc = Aes128EcbEnc::new(&VIBIO_KEY.into());
let res = enc.encrypt_padded_vec_mut::<Pkcs7>(command.as_bytes());
info!("Encoded {} to {:?}", command, res);
res
}
fn decrypt(data: Vec<u8>) -> String {
let dec = Aes128EcbDec::new(&VIBIO_KEY.into());
let res = String::from_utf8(dec.decrypt_padded_vec_mut::<Pkcs7>(&data).unwrap()).unwrap();
info!("Decoded {} from {:?}", res, data);
res
}
#[async_trait]
impl ProtocolInitializer for VibioInitializer {
async fn initialize(
&mut self,
hardware: Arc<Hardware>,
_: &ServerDeviceDefinition,
) -> Result<Arc<dyn ProtocolHandler>, ButtplugDeviceError> {
let mut event_receiver = hardware.event_stream();
hardware
.subscribe(&HardwareSubscribeCmd::new(
VIBIO_PROTOCOL_UUID,
Endpoint::Rx,
))
.await?;
let auth_str = rand::rng()
.sample_iter(&Alphanumeric)
.take(8)
.map(char::from)
.collect::<String>();
let auth_msg = format!("Auth:{};", auth_str);
hardware
.write_value(&HardwareWriteCmd::new(
&[VIBIO_PROTOCOL_UUID],
Endpoint::Tx,
encrypt(auth_msg),
false,
))
.await?;
loop {
let event = event_receiver.recv().await;
if let Ok(HardwareEvent::Notification(_, _, n)) = event {
let decoded = decrypt(n);
if decoded.eq("OK;") {
debug!("Vibio authenticated!");
return Ok(Arc::new(Vibio::default()));
}
let challenge = Regex::new(r"^([0-9A-Fa-f]{4}):([^;]+);$")
.expect("This is static and should always compile");
if let Some(parts) = challenge.captures(decoded.as_str()) {
debug!("Vibio challenge {:?}", parts);
if let Some(to_hash) = parts.get(2) {
debug!("Vibio to hash {:?}", to_hash);
let mut sha256 = Sha256::new();
sha256.update(to_hash.as_str().as_bytes());
let result = &sha256.finalize();
let auth_msg = format!("Auth:{:02x}{:02x};", result[0], result[1]);
hardware
.write_value(&HardwareWriteCmd::new(
&[VIBIO_PROTOCOL_UUID],
Endpoint::Tx,
encrypt(auth_msg),
false,
))
.await?;
} else {
return Err(ButtplugDeviceError::ProtocolSpecificError(
"Vibio".to_owned(),
"Vibio didn't provide a valid security handshake".to_owned(),
));
}
} else {
return Err(ButtplugDeviceError::ProtocolSpecificError(
"Vibio".to_owned(),
"Vibio didn't provide a valid security handshake".to_owned(),
));
}
} else {
return Err(ButtplugDeviceError::ProtocolSpecificError(
"Vibio".to_owned(),
"Vibio didn't provide a valid security handshake".to_owned(),
));
}
}
}
}
#[derive(Default)]
pub struct Vibio {
speeds: [AtomicU8; 2],
}
impl ProtocolHandler for Vibio {
fn handle_output_vibrate_cmd(
&self,
feature_index: u32,
feature_id: uuid::Uuid,
speed: u32,
) -> Result<Vec<HardwareCommand>, ButtplugDeviceError> {
self.speeds[feature_index as usize].store(speed as u8, Ordering::Relaxed);
Ok(vec![
HardwareWriteCmd::new(
&[feature_id],
Endpoint::Tx,
encrypt(format!(
"MtInt:{:02}{:02};",
self.speeds[0].load(Ordering::Relaxed),
self.speeds[1].load(Ordering::Relaxed)
)),
false,
)
.into(),
])
}
}