1 //
2 // Copyright 2020 Signal Messenger, LLC.
3 // SPDX-License-Identifier: AGPL-3.0-only
4 //
5 
6 use libsignal_protocol::*;
7 
8 #[test]
test_ratcheting_session_as_bob() -> Result<(), SignalProtocolError>9 fn test_ratcheting_session_as_bob() -> Result<(), SignalProtocolError> {
10     let bob_ephemeral_public =
11         hex::decode("052cb49776b8770205745a3a6e24f579cdb4ba7a89041005928ebbadc9c05ad458")
12             .expect("valid hex");
13 
14     let bob_ephemeral_private =
15         hex::decode("a1cab48f7c893fafa9880a28c3b4999d28d6329562d27a4ea4e22e9ff1bdd65a")
16             .expect("valid hex");
17 
18     let bob_identity_public =
19         hex::decode("05f1f43874f6966956c2dd473f8fa15adeb71d1cb991b2341692324cefb1c5e626")
20             .expect("valid hex");
21 
22     let bob_identity_private =
23         hex::decode("4875cc69ddf8ea0719ec947d61081135868d5fd801f02c0225e516df2156605e")
24             .expect("valid hex");
25 
26     let alice_base_public =
27         hex::decode("05472d1fb1a9862c3af6beaca8920277e2b26f4a79213ec7c906aeb35e03cf8950")
28             .expect("valid hex");
29 
30     let alice_identity_public =
31         hex::decode("05b4a8455660ada65b401007f615e654041746432e3339c6875149bceefcb42b4a")
32             .expect("valid hex");
33 
34     let bob_signed_prekey_public =
35         hex::decode("05ac248a8f263be6863576eb0362e28c828f0107a3379d34bab1586bf8c770cd67")
36             .expect("valid hex");
37 
38     let bob_signed_prekey_private =
39         hex::decode("583900131fb727998b7803fe6ac22cc591f342e4e42a8c8d5d78194209b8d253")
40             .expect("valid hex");
41 
42     let expected_sender_chain = "9797caca53c989bbe229a40ca7727010eb2604fc14945d77958a0aeda088b44d";
43 
44     let bob_identity_key_public = IdentityKey::decode(&bob_identity_public)?;
45 
46     let bob_identity_key_private = PrivateKey::deserialize(&bob_identity_private)?;
47 
48     let bob_identity_key_pair =
49         IdentityKeyPair::new(bob_identity_key_public, bob_identity_key_private);
50 
51     let bob_ephemeral_pair =
52         KeyPair::from_public_and_private(&bob_ephemeral_public, &bob_ephemeral_private)?;
53 
54     let bob_signed_prekey_pair =
55         KeyPair::from_public_and_private(&bob_signed_prekey_public, &bob_signed_prekey_private)?;
56 
57     let alice_base_public_key = PublicKey::deserialize(&alice_base_public)?;
58 
59     let bob_parameters = BobSignalProtocolParameters::new(
60         bob_identity_key_pair,
61         bob_signed_prekey_pair,
62         None, // one time pre key pair
63         bob_ephemeral_pair,
64         IdentityKey::decode(&alice_identity_public)?,
65         alice_base_public_key,
66     );
67 
68     let bob_record = initialize_bob_session_record(&bob_parameters)?;
69 
70     assert_eq!(
71         hex::encode(bob_record.local_identity_key_bytes()?),
72         hex::encode(bob_identity_public)
73     );
74     assert_eq!(
75         hex::encode(
76             bob_record
77                 .remote_identity_key_bytes()?
78                 .expect("value exists")
79         ),
80         hex::encode(alice_identity_public)
81     );
82     assert_eq!(
83         hex::encode(bob_record.get_sender_chain_key_bytes()?),
84         expected_sender_chain
85     );
86 
87     Ok(())
88 }
89 
90 #[test]
test_ratcheting_session_as_alice() -> Result<(), SignalProtocolError>91 fn test_ratcheting_session_as_alice() -> Result<(), SignalProtocolError> {
92     let bob_ephemeral_public =
93         hex::decode("052cb49776b8770205745a3a6e24f579cdb4ba7a89041005928ebbadc9c05ad458")
94             .expect("valid hex");
95 
96     let bob_identity_public =
97         hex::decode("05f1f43874f6966956c2dd473f8fa15adeb71d1cb991b2341692324cefb1c5e626")
98             .expect("valid hex");
99 
100     let alice_base_public =
101         hex::decode("05472d1fb1a9862c3af6beaca8920277e2b26f4a79213ec7c906aeb35e03cf8950")
102             .expect("valid hex");
103 
104     let alice_base_private =
105         hex::decode("11ae7c64d1e61cd596b76a0db5012673391cae66edbfcf073b4da80516a47449")
106             .expect("valid hex");
107 
108     let bob_signed_prekey_public =
109         hex::decode("05ac248a8f263be6863576eb0362e28c828f0107a3379d34bab1586bf8c770cd67")
110             .expect("valid hex");
111 
112     let alice_identity_public =
113         hex::decode("05b4a8455660ada65b401007f615e654041746432e3339c6875149bceefcb42b4a")
114             .expect("valid hex");
115 
116     let alice_identity_private =
117         hex::decode("9040f0d4e09cf38f6dc7c13779c908c015a1da4fa78737a080eb0a6f4f5f8f58")
118             .expect("valid hex");
119 
120     // This differs from the Java test and needs investigation
121     let expected_receiver_chain =
122         "ab9be50e5cb22a925446ab90ee5670545f4fd32902459ec274b6ad0ae5d6031a";
123 
124     let alice_identity_key_public = IdentityKey::decode(&alice_identity_public)?;
125 
126     let bob_ephemeral_public = PublicKey::deserialize(&bob_ephemeral_public)?;
127 
128     let alice_identity_key_private = PrivateKey::deserialize(&alice_identity_private)?;
129 
130     let bob_signed_prekey_public = PublicKey::deserialize(&bob_signed_prekey_public)?;
131 
132     let alice_identity_key_pair =
133         IdentityKeyPair::new(alice_identity_key_public, alice_identity_key_private);
134 
135     let alice_base_key = KeyPair::from_public_and_private(&alice_base_public, &alice_base_private)?;
136 
137     let alice_parameters = AliceSignalProtocolParameters::new(
138         alice_identity_key_pair,
139         alice_base_key,
140         IdentityKey::decode(&bob_identity_public)?,
141         bob_signed_prekey_public,
142         None, // one-time prekey
143         bob_ephemeral_public,
144     );
145 
146     let mut csprng = rand::rngs::OsRng;
147     let alice_record = initialize_alice_session_record(&alice_parameters, &mut csprng)?;
148 
149     assert_eq!(
150         hex::encode(alice_record.local_identity_key_bytes()?),
151         hex::encode(alice_identity_public),
152     );
153     assert_eq!(
154         hex::encode(
155             alice_record
156                 .remote_identity_key_bytes()?
157                 .expect("value exists")
158         ),
159         hex::encode(bob_identity_public)
160     );
161 
162     assert_eq!(
163         hex::encode(
164             alice_record
165                 .get_receiver_chain_key(&bob_ephemeral_public)?
166                 .expect("value exists")
167                 .key()
168         ),
169         expected_receiver_chain
170     );
171 
172     Ok(())
173 }
174