Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / third_party / betocore / ukey2_rs / tests / tests.rs

tests.rs168 lines · 6.5 KiBRaw
1// Copyright 2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![allow(clippy::unwrap_used, missing_docs)]
16
17use crypto_provider_default::CryptoProviderImpl;
18use rand::{rngs::StdRng, SeedableRng};
19use std::collections::hash_set;
20use ukey2_rs::*;
21
22#[test]
23fn full_integration_state_machine() {
24 let next_protocol = NextProtocol::Aes256CbcHmacSha256;
25 let next_protocols = hash_set::HashSet::from([next_protocol.to_string()]);
26 let server1 = Ukey2ServerStage1::<CryptoProviderImpl>::from(
27 next_protocols,
28 HandshakeImplementation::Spec,
29 );
30 let mut rng = StdRng::from_entropy();
31 let client1 = Ukey2ClientStage1::<CryptoProviderImpl>::from(
32 &mut rng,
33 vec![next_protocol],
34 HandshakeImplementation::Spec,
35 );
36 let server2 = server1.advance_state(&mut rng, client1.client_init_msg()).unwrap();
37
38 let client2 = client1.advance_state(&mut rng, server2.server_init_msg()).unwrap();
39
40 let server3 = server2.advance_state(&mut rng, client2.client_finished_msg()).unwrap();
41
42 assert_eq!(
43 server3.completed_handshake().auth_string::<CryptoProviderImpl>().derive_array::<32>(),
44 client2.completed_handshake().auth_string::<CryptoProviderImpl>().derive_array::<32>()
45 );
46 assert_eq!(
47 server3
48 .completed_handshake()
49 .next_protocol_secret::<CryptoProviderImpl>()
50 .derive_array::<32>(),
51 client2
52 .completed_handshake()
53 .next_protocol_secret::<CryptoProviderImpl>()
54 .derive_array::<32>()
55 );
56}
57
58#[test]
59fn full_integration_state_machine_public_key_in_protobuf() {
60 let next_protocol = NextProtocol::Aes256CbcHmacSha256;
61 let next_protocols = hash_set::HashSet::from([next_protocol.to_string()]);
62 let server1 = Ukey2ServerStage1::<CryptoProviderImpl>::from(
63 next_protocols,
64 HandshakeImplementation::PublicKeyInProtobuf,
65 );
66 let mut rng = StdRng::from_entropy();
67 let client1 = Ukey2ClientStage1::<CryptoProviderImpl>::from(
68 &mut rng,
69 vec![next_protocol],
70 HandshakeImplementation::PublicKeyInProtobuf,
71 );
72 let server2 = server1.advance_state(&mut rng, client1.client_init_msg()).unwrap();
73
74 let client2 = client1.advance_state(&mut rng, server2.server_init_msg()).unwrap();
75
76 let server3 = server2.advance_state(&mut rng, client2.client_finished_msg()).unwrap();
77
78 assert_eq!(
79 server3.completed_handshake().auth_string::<CryptoProviderImpl>().derive_array::<32>(),
80 client2.completed_handshake().auth_string::<CryptoProviderImpl>().derive_array::<32>()
81 );
82 assert_eq!(
83 server3
84 .completed_handshake()
85 .next_protocol_secret::<CryptoProviderImpl>()
86 .derive_array::<32>(),
87 client2
88 .completed_handshake()
89 .next_protocol_secret::<CryptoProviderImpl>()
90 .derive_array::<32>()
91 );
92}
93
94#[test]
95fn full_integration_state_machine_multiple_next_protocols_sort() {
96 let raw_next_protocols = [NextProtocol::Aes256CbcHmacSha256, NextProtocol::Aes256GcmSiv];
97 let next_protocols = hash_set::HashSet::from(raw_next_protocols.map(|p| p.to_string()));
98 let server1 = Ukey2ServerStage1::<CryptoProviderImpl>::from(
99 next_protocols,
100 HandshakeImplementation::Spec,
101 );
102 let mut rng = StdRng::from_entropy();
103 let client1 = Ukey2ClientStage1::<CryptoProviderImpl>::from(
104 &mut rng,
105 raw_next_protocols.to_vec(),
106 HandshakeImplementation::Spec,
107 );
108 let server2 = server1.advance_state(&mut rng, client1.client_init_msg()).unwrap();
109
110 let client2 = client1.advance_state(&mut rng, server2.server_init_msg()).unwrap();
111
112 let server3 = server2.advance_state(&mut rng, client2.client_finished_msg()).unwrap();
113
114 assert_eq!(
115 server3.completed_handshake().auth_string::<CryptoProviderImpl>().derive_array::<32>(),
116 client2.completed_handshake().auth_string::<CryptoProviderImpl>().derive_array::<32>()
117 );
118 assert_eq!(
119 server3
120 .completed_handshake()
121 .next_protocol_secret::<CryptoProviderImpl>()
122 .derive_array::<32>(),
123 client2
124 .completed_handshake()
125 .next_protocol_secret::<CryptoProviderImpl>()
126 .derive_array::<32>()
127 );
128 assert_eq!(server3.completed_handshake().next_protocol, NextProtocol::Aes256GcmSiv);
129 assert_eq!(client2.completed_handshake().next_protocol, NextProtocol::Aes256GcmSiv);
130}
131
132#[test]
133fn full_integration_state_machine_multiple_next_protocols_client() {
134 let raw_next_protocols = [NextProtocol::Aes256CbcHmacSha256, NextProtocol::Aes256GcmSiv];
135 let next_protocols = hash_set::HashSet::from(raw_next_protocols.map(|p| p.to_string()));
136 let server1 = Ukey2ServerStage1::<CryptoProviderImpl>::from(
137 next_protocols,
138 HandshakeImplementation::Spec,
139 );
140 let mut rng = StdRng::from_entropy();
141 let client1 = Ukey2ClientStage1::<CryptoProviderImpl>::from(
142 &mut rng,
143 [NextProtocol::Aes256CbcHmacSha256].to_vec(),
144 HandshakeImplementation::Spec,
145 );
146 let server2 = server1.advance_state(&mut rng, client1.client_init_msg()).unwrap();
147
148 let client2 = client1.advance_state(&mut rng, server2.server_init_msg()).unwrap();
149
150 let server3 = server2.advance_state(&mut rng, client2.client_finished_msg()).unwrap();
151
152 assert_eq!(
153 server3.completed_handshake().auth_string::<CryptoProviderImpl>().derive_array::<32>(),
154 client2.completed_handshake().auth_string::<CryptoProviderImpl>().derive_array::<32>()
155 );
156 assert_eq!(
157 server3
158 .completed_handshake()
159 .next_protocol_secret::<CryptoProviderImpl>()
160 .derive_array::<32>(),
161 client2
162 .completed_handshake()
163 .next_protocol_secret::<CryptoProviderImpl>()
164 .derive_array::<32>()
165 );
166 assert_eq!(server3.completed_handshake().next_protocol, NextProtocol::Aes256CbcHmacSha256);
167 assert_eq!(client2.completed_handshake().next_protocol, NextProtocol::Aes256CbcHmacSha256);
168}