Skip to content

Commit

Permalink
Reuse librespot-core's Diffie Hellman in discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannesd3 committed Mar 10, 2021
1 parent 3876139 commit 5b8d9aa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion connect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ futures-util = { version = "0.3", default_features = false }
hmac = "0.10"
hyper = { version = "0.14", features = ["server", "http1", "tcp"] }
log = "0.4"
num-bigint = "0.3"
protobuf = "~2.14.0"
rand = "0.8"
serde = { version = "1.0", features = ["derive"] }
Expand Down
27 changes: 9 additions & 18 deletions connect/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use futures_core::Stream;
use hmac::{Hmac, Mac, NewMac};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, StatusCode};
use num_bigint::BigUint;
use serde_json::json;
use sha1::{Digest, Sha1};
use tokio::sync::{mpsc, oneshot};
Expand All @@ -18,8 +17,7 @@ use libmdns;

use librespot_core::authentication::Credentials;
use librespot_core::config::ConnectConfig;
use librespot_core::diffie_hellman::{DH_GENERATOR, DH_PRIME};
use librespot_core::util;
use librespot_core::diffie_hellman::DHLocalKeys;

use std::borrow::Cow;
use std::collections::BTreeMap;
Expand All @@ -37,8 +35,7 @@ struct Discovery(Arc<DiscoveryInner>);
struct DiscoveryInner {
config: ConnectConfig,
device_id: String,
private_key: BigUint,
public_key: BigUint,
keys: DHLocalKeys,
tx: mpsc::UnboundedSender<Credentials>,
}

Expand All @@ -49,24 +46,18 @@ impl Discovery {
) -> (Discovery, mpsc::UnboundedReceiver<Credentials>) {
let (tx, rx) = mpsc::unbounded_channel();

let key_data = util::rand_vec(&mut rand::thread_rng(), 95);
let private_key = BigUint::from_bytes_be(&key_data);
let public_key = util::powm(&DH_GENERATOR, &private_key, &DH_PRIME);

let discovery = Discovery(Arc::new(DiscoveryInner {
config: config,
device_id: device_id,
private_key: private_key,
public_key: public_key,
keys: DHLocalKeys::random(&mut rand::thread_rng()),
tx: tx,
}));

(discovery, rx)
}

fn handle_get_info(&self, _: BTreeMap<Cow<'_, str>, Cow<'_, str>>) -> Response<hyper::Body> {
let public_key = self.0.public_key.to_bytes_be();
let public_key = base64::encode(&public_key);
let public_key = base64::encode(&self.0.keys.public_key());

let result = json!({
"status": 101,
Expand Down Expand Up @@ -101,16 +92,16 @@ impl Discovery {

let encrypted_blob = base64::decode(encrypted_blob.as_bytes()).unwrap();

let client_key = base64::decode(client_key.as_bytes()).unwrap();
let client_key = BigUint::from_bytes_be(&client_key);

let shared_key = util::powm(&client_key, &self.0.private_key, &DH_PRIME);
let shared_key = self
.0
.keys
.shared_secret(&base64::decode(client_key.as_bytes()).unwrap());

let iv = &encrypted_blob[0..16];
let encrypted = &encrypted_blob[16..encrypted_blob.len() - 20];
let cksum = &encrypted_blob[encrypted_blob.len() - 20..encrypted_blob.len()];

let base_key = Sha1::digest(&shared_key.to_bytes_be());
let base_key = Sha1::digest(&shared_key);
let base_key = &base_key[..16];

let checksum_key = {
Expand Down

0 comments on commit 5b8d9aa

Please sign in to comment.