Skip to content

Commit 5b8d9aa

Browse files
committed
Reuse librespot-core's Diffie Hellman in discovery
1 parent 3876139 commit 5b8d9aa

File tree

3 files changed

+9
-20
lines changed

3 files changed

+9
-20
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

connect/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ futures-util = { version = "0.3", default_features = false }
1616
hmac = "0.10"
1717
hyper = { version = "0.14", features = ["server", "http1", "tcp"] }
1818
log = "0.4"
19-
num-bigint = "0.3"
2019
protobuf = "~2.14.0"
2120
rand = "0.8"
2221
serde = { version = "1.0", features = ["derive"] }

connect/src/discovery.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use futures_core::Stream;
55
use hmac::{Hmac, Mac, NewMac};
66
use hyper::service::{make_service_fn, service_fn};
77
use hyper::{Body, Method, Request, Response, StatusCode};
8-
use num_bigint::BigUint;
98
use serde_json::json;
109
use sha1::{Digest, Sha1};
1110
use tokio::sync::{mpsc, oneshot};
@@ -18,8 +17,7 @@ use libmdns;
1817

1918
use librespot_core::authentication::Credentials;
2019
use librespot_core::config::ConnectConfig;
21-
use librespot_core::diffie_hellman::{DH_GENERATOR, DH_PRIME};
22-
use librespot_core::util;
20+
use librespot_core::diffie_hellman::DHLocalKeys;
2321

2422
use std::borrow::Cow;
2523
use std::collections::BTreeMap;
@@ -37,8 +35,7 @@ struct Discovery(Arc<DiscoveryInner>);
3735
struct DiscoveryInner {
3836
config: ConnectConfig,
3937
device_id: String,
40-
private_key: BigUint,
41-
public_key: BigUint,
38+
keys: DHLocalKeys,
4239
tx: mpsc::UnboundedSender<Credentials>,
4340
}
4441

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

52-
let key_data = util::rand_vec(&mut rand::thread_rng(), 95);
53-
let private_key = BigUint::from_bytes_be(&key_data);
54-
let public_key = util::powm(&DH_GENERATOR, &private_key, &DH_PRIME);
55-
5649
let discovery = Discovery(Arc::new(DiscoveryInner {
5750
config: config,
5851
device_id: device_id,
59-
private_key: private_key,
60-
public_key: public_key,
52+
keys: DHLocalKeys::random(&mut rand::thread_rng()),
6153
tx: tx,
6254
}));
6355

6456
(discovery, rx)
6557
}
6658

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

7162
let result = json!({
7263
"status": 101,
@@ -101,16 +92,16 @@ impl Discovery {
10192

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

104-
let client_key = base64::decode(client_key.as_bytes()).unwrap();
105-
let client_key = BigUint::from_bytes_be(&client_key);
106-
107-
let shared_key = util::powm(&client_key, &self.0.private_key, &DH_PRIME);
95+
let shared_key = self
96+
.0
97+
.keys
98+
.shared_secret(&base64::decode(client_key.as_bytes()).unwrap());
10899

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

113-
let base_key = Sha1::digest(&shared_key.to_bytes_be());
104+
let base_key = Sha1::digest(&shared_key);
114105
let base_key = &base_key[..16];
115106

116107
let checksum_key = {

0 commit comments

Comments
 (0)