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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Implementation of [PRIds](https://spec.dsnp.org/DSNP/UserData.html#private-connection-prids) defined in DSNP spec
use crate::dsnp::{
	dsnp_configs::{PublicKeyType, SecretKeyType},
	dsnp_types::{DsnpPrid, DsnpUserId},
};
use dryoc::{
	classic::{
		crypto_box::crypto_box_beforenm,
		crypto_secretbox::{crypto_secretbox_detached, Nonce},
	},
	constants::CRYPTO_SECRETBOX_MACBYTES,
	kdf::{Key, StackKdf},
	types::ByteArray,
};
use dsnp_graph_config::errors::{DsnpGraphError, DsnpGraphResult};
use log::Level;
use log_result_proc_macro::log_result_err;
use std::ops::Deref;
use zeroize::Zeroizing;

const PRI_CONTEXT: &[u8] = b"PRIdCtx0";

/// a trait that implements the PRI related algorithm
pub trait PridProvider {
	/// Return type of Prid
	type DsnpPrid;

	/// creates PRId from A -> B
	fn create_prid(
		a: DsnpUserId,
		b: DsnpUserId,
		a_secret_key: &SecretKeyType,
		b_public_key: &PublicKeyType,
	) -> DsnpGraphResult<Self::DsnpPrid>;

	/// creates shared context from A -> B
	fn create_shared_context(
		b: DsnpUserId,
		a_secret_key: &SecretKeyType,
		b_public_key: &PublicKeyType,
	) -> DsnpGraphResult<Key>;
}

impl PridProvider for DsnpPrid {
	type DsnpPrid = DsnpPrid;

	#[log_result_err(Level::Info)]
	fn create_prid(
		a: DsnpUserId,
		b: DsnpUserId,
		a_secret_key: &SecretKeyType,
		b_public_key: &PublicKeyType,
	) -> DsnpGraphResult<Self::DsnpPrid> {
		let id_a = a.to_le_bytes();
		let id_b = b.to_le_bytes();
		let shared_context = Self::create_shared_context(b, a_secret_key, b_public_key)?;

		// setting nonce with `a` for encryption
		let mut nonce = Nonce::default();
		nonce[..8].copy_from_slice(&id_a[..]);

		// encrypting `b` using nonce and derived key
		let mut result = vec![0u8; id_b.len()];
		let mut _mac = [0u8; CRYPTO_SECRETBOX_MACBYTES];
		crypto_secretbox_detached(
			&mut result,
			&mut _mac,
			&id_b,
			&nonce,
			shared_context.deref().as_array(),
		);

		Ok(Self::DsnpPrid::new(&result))
	}

	#[log_result_err(Level::Info)]
	fn create_shared_context(
		b: DsnpUserId,
		a_secret_key: &SecretKeyType,
		b_public_key: &PublicKeyType,
	) -> DsnpGraphResult<Key> {
		// calculate shared secret
		let root_shared = match (a_secret_key, b_public_key) {
			(SecretKeyType::Version1_0(a_pair), PublicKeyType::Version1_0(b_public)) =>
				Zeroizing::new(crypto_box_beforenm(
					b_public.as_array(),
					a_pair.secret_key.as_array(),
				)),
		};

		// // derive a new key form pri context
		let kdf =
			StackKdf::from_parts(Key::from(root_shared.deref()), PRI_CONTEXT.as_array().into());
		let derived_key: Key = kdf
			.derive_subkey(b)
			.map_err(|e| DsnpGraphError::KeyDerivationError(e.to_string()))?;

		Ok(derived_key)
	}
}

#[cfg(test)]
mod test {
	use super::*;
	use dryoc::{
		dryocsecretbox::Key,
		keypair::{PublicKey, SecretKey, StackKeyPair},
	};

	#[test]
	fn generated_pri_should_be_the_same_calculated_from_both_sides() {
		let a = 2576367222u64;
		let b = 826378782u64;
		let key_pair_a = StackKeyPair::gen();
		let key_pair_b = StackKeyPair::gen();

		let pri_a_to_b = DsnpPrid::create_prid(
			a,
			b,
			&SecretKeyType::Version1_0(key_pair_a.clone()),
			&PublicKeyType::Version1_0(key_pair_b.public_key.clone()),
		)
		.expect("should create pri");
		let pri_a_to_b_2 = DsnpPrid::create_prid(
			a,
			b,
			&SecretKeyType::Version1_0(key_pair_b),
			&PublicKeyType::Version1_0(key_pair_a.public_key.clone()),
		)
		.expect("should create pri");

		assert_eq!(pri_a_to_b, pri_a_to_b_2);
	}

	#[test]
	fn generated_prid_should_be_compatible_with_test_vector() {
		let alice = 42;
		let bob = 478;
		let alice_secret = SecretKey::try_from(
			hex::decode("c9432ed5c0c5c24e8a4ff190619893918b4d1265a67d123895023fa7324b43e0")
				.expect("should decode")
				.as_array(),
		)
		.unwrap();
		let alice_public = PublicKey::try_from(
			hex::decode("0fea2cafabdc83752be36fa5349640da2c828add0a290df13cd2d8173eb2496f")
				.expect("should decode")
				.as_array(),
		)
		.unwrap();
		let bob_secret = SecretKey::try_from(
			hex::decode("dc106e1371293ee9536956e1253f43f8941d4a5c4e40f15968d24b75512b6920")
				.expect("should decode")
				.as_array(),
		)
		.unwrap();
		let bob_public = PublicKey::try_from(
			hex::decode("d0d4eb21db1df63369c147e63b2573816dd4b3fe513e95bf87f7ed1835407e62")
				.expect("should decode")
				.as_array(),
		)
		.unwrap();
		let expected_alice_to_bob =
			DsnpPrid::new(&hex::decode("ace4d2995b1a829c").expect("should decode"));
		let expected_ctx_alice_to_bob = Key::from(
			hex::decode("37cb1a870f0c1dce06f5116faf145ac2cf7a2f7d30136be4eea70c324932e6d2")
				.expect("should decode")
				.as_array(),
		);
		let expected_bob_to_alice =
			DsnpPrid::new(&hex::decode("1a53b02a26503600").expect("should decode"));
		let expected_ctx_bob_to_alice = Key::from(
			hex::decode("32c45c49fcfe12f9db60e74fa66416c5a05832c298814d82032a6783a4b1fca0")
				.expect("should decode")
				.as_array(),
		);

		let pri_alice_to_bob = DsnpPrid::create_prid(
			alice,
			bob,
			&SecretKeyType::Version1_0(StackKeyPair::from_secret_key(alice_secret.clone())),
			&PublicKeyType::Version1_0(bob_public.clone()),
		)
		.expect("should create pri");
		let ctx_alice_to_bob = DsnpPrid::create_shared_context(
			bob,
			&&SecretKeyType::Version1_0(StackKeyPair::from_secret_key(alice_secret)),
			&PublicKeyType::Version1_0(bob_public),
		)
		.expect("should create ctx");

		let pri_bob_to_alice = DsnpPrid::create_prid(
			bob,
			alice,
			&SecretKeyType::Version1_0(StackKeyPair::from_secret_key(bob_secret.clone())),
			&PublicKeyType::Version1_0(alice_public.clone()),
		)
		.expect("should create pri");
		let ctx_bob_to_alice = DsnpPrid::create_shared_context(
			alice,
			&SecretKeyType::Version1_0(StackKeyPair::from_secret_key(bob_secret)),
			&PublicKeyType::Version1_0(alice_public),
		)
		.expect("should create ctx");

		assert_eq!(pri_alice_to_bob, expected_alice_to_bob);
		assert_eq!(ctx_alice_to_bob, expected_ctx_alice_to_bob);

		assert_eq!(pri_bob_to_alice, expected_bob_to_alice);
		assert_eq!(ctx_bob_to_alice, expected_ctx_bob_to_alice);
	}
}