Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / ukey2_rs / src / tests.rs
tests.rs229 lines · 8.7 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 | use std::collections::hash_set; |
| 16 | |
| 17 | use rand::rngs::StdRng; |
| 18 | use rand::{Rng, SeedableRng}; |
| 19 | use sha2::Digest; |
| 20 | |
| 21 | use crate::{ |
| 22 | proto_adapter::{IntoAdapter as _, MessageType, ToWrappedMessage as _}, |
| 23 | ukey2_handshake::HandshakeCipher, |
| 24 | HandshakeImplementation, NextProtocol, StateMachine, Ukey2ClientStage1, Ukey2ServerStage1, |
| 25 | }; |
| 26 | use crypto_provider::elliptic_curve::{EcdhProvider, EphemeralSecret, PublicKey}; |
| 27 | use crypto_provider::p256::P256; |
| 28 | use crypto_provider::x25519::X25519; |
| 29 | use crypto_provider::{CryptoProvider, CryptoRng}; |
| 30 | use crypto_provider_default::CryptoProviderImpl; |
| 31 | use ukey2_proto::protobuf::Message; |
| 32 | use ukey2_proto::ukey2_all_proto::ukey; |
| 33 | |
| 34 | type X25519EphemeralSecret = |
| 35 | <<CryptoProviderImpl as CryptoProvider>::X25519 as EcdhProvider<X25519>>::EphemeralSecret; |
| 36 | type X25519PublicKey = |
| 37 | <<CryptoProviderImpl as CryptoProvider>::X25519 as EcdhProvider<X25519>>::PublicKey; |
| 38 | type P256EphemeralSecret = |
| 39 | <<CryptoProviderImpl as CryptoProvider>::P256 as EcdhProvider<P256>>::EphemeralSecret; |
| 40 | |
| 41 | #[test] |
| 42 | #[allow(clippy::unwrap_used)] |
| 43 | fn advance_from_init_to_finish_client_test() { |
| 44 | let mut rng = StdRng::from_entropy(); |
| 45 | let client1 = Ukey2ClientStage1::<CryptoProviderImpl>::from( |
| 46 | &mut rng, |
| 47 | vec![NextProtocol::Aes256CbcHmacSha256], |
| 48 | HandshakeImplementation::Spec, |
| 49 | ); |
| 50 | |
| 51 | let secret = |
| 52 | X25519EphemeralSecret::generate_random(&mut <X25519EphemeralSecret as EphemeralSecret< |
| 53 | X25519, |
| 54 | >>::Rng::new()); |
| 55 | let public_key = X25519PublicKey::from_bytes(secret.public_key_bytes().as_ref()).unwrap(); |
| 56 | let random: [u8; 32] = rng.gen(); |
| 57 | let message_data: ukey::Ukey2ServerInit = ukey::Ukey2ServerInit { |
| 58 | version: Some(1), |
| 59 | random: Some(random.to_vec()), |
| 60 | handshake_cipher: Some(ukey::Ukey2HandshakeCipher::CURVE25519_SHA512.into()), |
| 61 | public_key: Some(public_key.to_bytes().as_ref().to_vec()), |
| 62 | ..Default::default() |
| 63 | }; |
| 64 | |
| 65 | let _client = client1 |
| 66 | .advance_state(&mut rng, &message_data.to_wrapped_msg().write_to_bytes().unwrap()) |
| 67 | .unwrap(); |
| 68 | // TODO assertions on client state |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | #[allow(clippy::unwrap_used)] |
| 73 | fn advance_from_init_to_complete_server_x25519_test() { |
| 74 | let mut rng = StdRng::from_entropy(); |
| 75 | let mut next_protocols = hash_set::HashSet::new(); |
| 76 | let _ = next_protocols.insert("AES_256_CBC-HMAC_SHA256".to_string()); |
| 77 | let server1 = Ukey2ServerStage1::<CryptoProviderImpl>::from( |
| 78 | next_protocols, |
| 79 | HandshakeImplementation::Spec, |
| 80 | ); |
| 81 | // We construct a ClientInit message for the server to get it into the state to handle |
| 82 | // ClientFinish messages. |
| 83 | let secret = |
| 84 | X25519EphemeralSecret::generate_random(&mut <X25519EphemeralSecret as EphemeralSecret< |
| 85 | X25519, |
| 86 | >>::Rng::new()); |
| 87 | let client_finished_msg = { |
| 88 | let mut msg = ukey::Ukey2ClientFinished::default(); |
| 89 | msg.set_public_key(secret.public_key_bytes().as_ref().to_vec()); |
| 90 | msg.to_wrapped_msg() |
| 91 | }; |
| 92 | let client_finished_bytes = client_finished_msg.write_to_bytes().unwrap(); |
| 93 | let mut hasher = sha2::Sha512::new(); |
| 94 | hasher.update(&client_finished_bytes); |
| 95 | let client_finished_hash = hasher.finalize().to_vec(); |
| 96 | let cipher = HandshakeCipher::Curve25519Sha512; |
| 97 | let client_random = rng.gen::<[u8; 32]>(); |
| 98 | let client_init_framed = { |
| 99 | let commitment = ukey::ukey2client_init::CipherCommitment { |
| 100 | handshake_cipher: Some(cipher.as_proto().into()), |
| 101 | commitment: Some(client_finished_hash), |
| 102 | ..Default::default() |
| 103 | }; |
| 104 | let client_init = ukey::Ukey2ClientInit { |
| 105 | version: Some(1), |
| 106 | random: Some(client_random.to_vec()), |
| 107 | cipher_commitments: vec![commitment], |
| 108 | next_protocol: Some("AES_256_CBC-HMAC_SHA256".to_string()), |
| 109 | ..Default::default() |
| 110 | }; |
| 111 | client_init.to_wrapped_msg() |
| 112 | }; |
| 113 | let server2 = |
| 114 | server1.advance_state(&mut rng, &client_init_framed.write_to_bytes().unwrap()).unwrap(); |
| 115 | assert!( |
| 116 | !server2.server_init_msg().windows(client_random.len()).any(|w| w == client_random), |
| 117 | "Server init msg should not contain the client's random" |
| 118 | ); |
| 119 | // TODO assertions on server2 state |
| 120 | // We now move the server to the post-ClientFinished state |
| 121 | let _server = server2.advance_state(&mut rng, &client_finished_bytes).unwrap(); |
| 122 | // TODO assertions on server state |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | #[allow(clippy::unwrap_used)] |
| 127 | fn advance_from_init_to_complete_server_p256_test() { |
| 128 | let mut rng = StdRng::from_entropy(); |
| 129 | let mut next_protocols = hash_set::HashSet::new(); |
| 130 | let _ = next_protocols.insert("AES_256_CBC-HMAC_SHA256".to_string()); |
| 131 | let server1 = Ukey2ServerStage1::<CryptoProviderImpl>::from( |
| 132 | next_protocols, |
| 133 | HandshakeImplementation::Spec, |
| 134 | ); |
| 135 | // We construct a ClientInit message for the server to get it into the state to handle |
| 136 | // ClientFinish messages. |
| 137 | let secret = P256EphemeralSecret::generate_random( |
| 138 | &mut <P256EphemeralSecret as EphemeralSecret<P256>>::Rng::new(), |
| 139 | ); |
| 140 | let client_finished_msg = { |
| 141 | let mut msg = ukey::Ukey2ClientFinished::default(); |
| 142 | msg.set_public_key(secret.public_key_bytes().as_ref().to_vec()); |
| 143 | msg.to_wrapped_msg() |
| 144 | }; |
| 145 | let client_finished_bytes = client_finished_msg.write_to_bytes().unwrap(); |
| 146 | let mut hasher = sha2::Sha512::new(); |
| 147 | hasher.update(&client_finished_bytes); |
| 148 | let client_finished_hash = hasher.finalize().to_vec(); |
| 149 | let cipher = HandshakeCipher::P256Sha512; |
| 150 | let client_init_framed = { |
| 151 | let commitment = ukey::ukey2client_init::CipherCommitment { |
| 152 | handshake_cipher: Some(cipher.as_proto().into()), |
| 153 | commitment: Some(client_finished_hash), |
| 154 | ..Default::default() |
| 155 | }; |
| 156 | ukey::Ukey2ClientInit { |
| 157 | version: Some(1), |
| 158 | random: Some(rng.gen::<[u8; 32]>().to_vec()), |
| 159 | cipher_commitments: vec![commitment], |
| 160 | next_protocol: Some("AES_256_CBC-HMAC_SHA256".to_string()), |
| 161 | ..Default::default() |
| 162 | } |
| 163 | .to_wrapped_msg() |
| 164 | }; |
| 165 | let server2 = |
| 166 | server1.advance_state(&mut rng, &client_init_framed.write_to_bytes().unwrap()).unwrap(); |
| 167 | // TODO assertions on server2 state |
| 168 | let _server = |
| 169 | server2.advance_state(&mut rng, &client_finished_msg.write_to_bytes().unwrap()).unwrap(); |
| 170 | // TODO assertions on server state |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn message_type_discriminant() { |
| 175 | assert_eq!(1, ukey::ukey2message::Type::ALERT as i32); |
| 176 | assert_eq!(2, ukey::ukey2message::Type::CLIENT_INIT as i32); |
| 177 | assert_eq!(3, ukey::ukey2message::Type::SERVER_INIT as i32); |
| 178 | assert_eq!(4, ukey::ukey2message::Type::CLIENT_FINISH as i32); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn cipher_type_discriminant() { |
| 183 | assert_eq!(100, ukey::Ukey2HandshakeCipher::P256_SHA512 as i32); |
| 184 | assert_eq!(200, ukey::Ukey2HandshakeCipher::CURVE25519_SHA512 as i32); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | #[allow(clippy::unwrap_used)] |
| 189 | fn convert_to_message_type() { |
| 190 | assert_eq!( |
| 191 | MessageType::ClientInit, |
| 192 | ukey::ukey2message::Type::CLIENT_INIT.into_adapter().unwrap() |
| 193 | ); |
| 194 | assert_eq!( |
| 195 | MessageType::ServerInit, |
| 196 | ukey::ukey2message::Type::SERVER_INIT.into_adapter().unwrap() |
| 197 | ); |
| 198 | assert_eq!( |
| 199 | MessageType::ClientFinish, |
| 200 | ukey::ukey2message::Type::CLIENT_FINISH.into_adapter().unwrap() |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | #[allow(clippy::unwrap_used)] |
| 206 | fn convert_to_cipher_type() { |
| 207 | assert_eq!(HandshakeCipher::P256Sha512, 100.into_adapter().unwrap()); |
| 208 | assert_eq!(HandshakeCipher::Curve25519Sha512, 200.into_adapter().unwrap()); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn convert_next_protocols() { |
| 213 | assert_eq!( |
| 214 | (&"AES_256_CBC-HMAC_SHA256".to_string()).try_into(), |
| 215 | Ok(NextProtocol::Aes256CbcHmacSha256) |
| 216 | ); |
| 217 | assert_eq!((&"AES_256_GCM_SIV".to_string()).try_into(), Ok(NextProtocol::Aes256GcmSiv)); |
| 218 | assert_eq!( |
| 219 | TryInto::<NextProtocol>::try_into(&"Random protocol".to_string()), |
| 220 | Err(ukey::ukey2alert::AlertType::BAD_NEXT_PROTOCOL) |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn sort_next_protocols() { |
| 226 | let mut next_protocols = [NextProtocol::Aes256CbcHmacSha256, NextProtocol::Aes256GcmSiv]; |
| 227 | next_protocols.sort(); |
| 228 | assert_eq!(next_protocols, [NextProtocol::Aes256GcmSiv, NextProtocol::Aes256CbcHmacSha256]) |
| 229 | } |