|
| 1 | +// Buttplug Rust Source Code File - See https://buttplug.io for more info. |
| 2 | +// |
| 3 | +// Copyright 2016-2026 Nonpolynomial Labs LLC. All rights reserved. |
| 4 | +// |
| 5 | +// Licensed under the BSD 3-Clause license. See LICENSE file in the project root |
| 6 | +// for full license information. |
| 7 | + |
| 8 | +use crate::device::{ |
| 9 | + hardware::{Hardware, HardwareCommand, HardwareEvent, HardwareSubscribeCmd, HardwareWriteCmd}, |
| 10 | + protocol::{ |
| 11 | + ProtocolHandler, |
| 12 | + ProtocolIdentifier, |
| 13 | + ProtocolInitializer, |
| 14 | + generic_protocol_initializer_setup, |
| 15 | + }, |
| 16 | +}; |
| 17 | +use aes::Aes128; |
| 18 | +use async_trait::async_trait; |
| 19 | +use buttplug_core::errors::ButtplugDeviceError; |
| 20 | +use buttplug_server_device_config::Endpoint; |
| 21 | +use buttplug_server_device_config::{ |
| 22 | + ProtocolCommunicationSpecifier, |
| 23 | + ServerDeviceDefinition, |
| 24 | + UserDeviceIdentifier, |
| 25 | +}; |
| 26 | +use ecb::cipher::block_padding::Pkcs7; |
| 27 | +use ecb::cipher::{BlockDecryptMut, BlockEncryptMut, KeyInit}; |
| 28 | +use std::sync::{ |
| 29 | + Arc, |
| 30 | + atomic::{AtomicU8, Ordering}, |
| 31 | +}; |
| 32 | +use uuid::{Uuid, uuid}; |
| 33 | + |
| 34 | +use rand::distr::Alphanumeric; |
| 35 | +use rand::RngExt; |
| 36 | +use regex::Regex; |
| 37 | +use sha2::{Digest, Sha256}; |
| 38 | + |
| 39 | +type Aes128EcbEnc = ecb::Encryptor<Aes128>; |
| 40 | +type Aes128EcbDec = ecb::Decryptor<Aes128>; |
| 41 | + |
| 42 | +const VIBIO_PROTOCOL_UUID: Uuid = uuid!("b8c76c9e-cb42-4a94-99f4-7c2a8e5d3b2a"); |
| 43 | +const VIBIO_KEY: [u8; 16] = *b"jdk#vib%y5fir21a"; |
| 44 | + |
| 45 | +generic_protocol_initializer_setup!(Vibio, "vibio"); |
| 46 | + |
| 47 | +#[derive(Default)] |
| 48 | +pub struct VibioInitializer {} |
| 49 | + |
| 50 | +fn encrypt(command: String) -> Vec<u8> { |
| 51 | + let enc = Aes128EcbEnc::new(&VIBIO_KEY.into()); |
| 52 | + let res = enc.encrypt_padded_vec_mut::<Pkcs7>(command.as_bytes()); |
| 53 | + |
| 54 | + info!("Encoded {} to {:?}", command, res); |
| 55 | + res |
| 56 | +} |
| 57 | + |
| 58 | +fn decrypt(data: Vec<u8>) -> String { |
| 59 | + let dec = Aes128EcbDec::new(&VIBIO_KEY.into()); |
| 60 | + let res = String::from_utf8(dec.decrypt_padded_vec_mut::<Pkcs7>(&data).unwrap()).unwrap(); |
| 61 | + |
| 62 | + info!("Decoded {} from {:?}", res, data); |
| 63 | + res |
| 64 | +} |
| 65 | + |
| 66 | +#[async_trait] |
| 67 | +impl ProtocolInitializer for VibioInitializer { |
| 68 | + async fn initialize( |
| 69 | + &mut self, |
| 70 | + hardware: Arc<Hardware>, |
| 71 | + _: &ServerDeviceDefinition, |
| 72 | + ) -> Result<Arc<dyn ProtocolHandler>, ButtplugDeviceError> { |
| 73 | + let mut event_receiver = hardware.event_stream(); |
| 74 | + hardware |
| 75 | + .subscribe(&HardwareSubscribeCmd::new( |
| 76 | + VIBIO_PROTOCOL_UUID, |
| 77 | + Endpoint::Rx, |
| 78 | + )) |
| 79 | + .await?; |
| 80 | + |
| 81 | + let auth_str = rand::rng() |
| 82 | + .sample_iter(&Alphanumeric) |
| 83 | + .take(8) |
| 84 | + .map(char::from) |
| 85 | + .collect::<String>(); |
| 86 | + let auth_msg = format!("Auth:{};", auth_str); |
| 87 | + hardware |
| 88 | + .write_value(&HardwareWriteCmd::new( |
| 89 | + &[VIBIO_PROTOCOL_UUID], |
| 90 | + Endpoint::Tx, |
| 91 | + encrypt(auth_msg), |
| 92 | + false, |
| 93 | + )) |
| 94 | + .await?; |
| 95 | + |
| 96 | + loop { |
| 97 | + let event = event_receiver.recv().await; |
| 98 | + if let Ok(HardwareEvent::Notification(_, _, n)) = event { |
| 99 | + let decoded = decrypt(n); |
| 100 | + if decoded.eq("OK;") { |
| 101 | + debug!("Vibio authenticated!"); |
| 102 | + return Ok(Arc::new(Vibio::default())); |
| 103 | + } |
| 104 | + let challenge = Regex::new(r"^([0-9A-Fa-f]{4}):([^;]+);$") |
| 105 | + .expect("This is static and should always compile"); |
| 106 | + if let Some(parts) = challenge.captures(decoded.as_str()) { |
| 107 | + debug!("Vibio challenge {:?}", parts); |
| 108 | + if let Some(to_hash) = parts.get(2) { |
| 109 | + debug!("Vibio to hash {:?}", to_hash); |
| 110 | + let mut sha256 = Sha256::new(); |
| 111 | + sha256.update(to_hash.as_str().as_bytes()); |
| 112 | + let result = &sha256.finalize(); |
| 113 | + |
| 114 | + let auth_msg = format!("Auth:{:02x}{:02x};", result[0], result[1]); |
| 115 | + hardware |
| 116 | + .write_value(&HardwareWriteCmd::new( |
| 117 | + &[VIBIO_PROTOCOL_UUID], |
| 118 | + Endpoint::Tx, |
| 119 | + encrypt(auth_msg), |
| 120 | + false, |
| 121 | + )) |
| 122 | + .await?; |
| 123 | + } else { |
| 124 | + return Err(ButtplugDeviceError::ProtocolSpecificError( |
| 125 | + "Vibio".to_owned(), |
| 126 | + "Vibio didn't provide a valid security handshake".to_owned(), |
| 127 | + )); |
| 128 | + } |
| 129 | + } else { |
| 130 | + return Err(ButtplugDeviceError::ProtocolSpecificError( |
| 131 | + "Vibio".to_owned(), |
| 132 | + "Vibio didn't provide a valid security handshake".to_owned(), |
| 133 | + )); |
| 134 | + } |
| 135 | + } else { |
| 136 | + return Err(ButtplugDeviceError::ProtocolSpecificError( |
| 137 | + "Vibio".to_owned(), |
| 138 | + "Vibio didn't provide a valid security handshake".to_owned(), |
| 139 | + )); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +#[derive(Default)] |
| 146 | +pub struct Vibio { |
| 147 | + speeds: [AtomicU8; 2], |
| 148 | +} |
| 149 | + |
| 150 | +impl ProtocolHandler for Vibio { |
| 151 | + fn handle_output_vibrate_cmd( |
| 152 | + &self, |
| 153 | + feature_index: u32, |
| 154 | + feature_id: uuid::Uuid, |
| 155 | + speed: u32, |
| 156 | + ) -> Result<Vec<HardwareCommand>, ButtplugDeviceError> { |
| 157 | + self.speeds[feature_index as usize].store(speed as u8, Ordering::Relaxed); |
| 158 | + |
| 159 | + Ok(vec![ |
| 160 | + HardwareWriteCmd::new( |
| 161 | + &[feature_id], |
| 162 | + Endpoint::Tx, |
| 163 | + encrypt(format!( |
| 164 | + "MtInt:{:02}{:02};", |
| 165 | + self.speeds[0].load(Ordering::Relaxed), |
| 166 | + self.speeds[1].load(Ordering::Relaxed) |
| 167 | + )), |
| 168 | + false, |
| 169 | + ) |
| 170 | + .into(), |
| 171 | + ]) |
| 172 | + } |
| 173 | +} |
0 commit comments