|
| 1 | +use digest::OutputSizeUser; |
| 2 | +use generic_array::GenericArray; |
| 3 | +use generic_array::sequence::{Concat, Split}; |
| 4 | +use generic_array::typenum::Sum; |
| 5 | +use opaque_ke::ksf::Identity; |
| 6 | +use opaque_ke::{CipherSuite, ClientLogin, ServerLogin, ServerRegistration, ServerSetup, TripleDh}; |
| 7 | +use opaque_ke_3::key_exchange::group::KeGroup as v3KeGroup; |
| 8 | +use opaque_ke_3::key_exchange::tripledh::TripleDh as v3TripleDh; |
| 9 | +use opaque_ke_3::keypair::KeyPair; |
| 10 | +use opaque_ke_3::ksf::Identity as v3Identity; |
| 11 | +use opaque_ke_3::{ |
| 12 | + CipherSuite as v3CipherSuite, ClientRegistration as v3ClientRegistration, |
| 13 | + ServerRegistration as v3ServerRegistration, ServerSetup as v3ServerSetup, |
| 14 | +}; |
| 15 | +use p256::NistP256; |
| 16 | +use rand::rngs::OsRng; |
| 17 | +use sha2::Sha256; |
| 18 | + |
| 19 | +const PASSWORD: &[u8] = b"test password"; |
| 20 | +const CLIENT_IDENTIFIER: &[u8] = b"test client identifier"; |
| 21 | + |
| 22 | +struct OldCipherSuite; |
| 23 | + |
| 24 | +impl v3CipherSuite for OldCipherSuite { |
| 25 | + type OprfCs = NistP256; |
| 26 | + type KeGroup = NistP256; |
| 27 | + type KeyExchange = v3TripleDh; |
| 28 | + type Ksf = v3Identity; |
| 29 | +} |
| 30 | + |
| 31 | +struct NewCipherSuite; |
| 32 | + |
| 33 | +impl CipherSuite for NewCipherSuite { |
| 34 | + type OprfCs = NistP256; |
| 35 | + type KeyExchange = TripleDh<NistP256, Sha256>; |
| 36 | + type Ksf = Identity; |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn registration_upload() { |
| 41 | + // V3 registration. |
| 42 | + let result = v3ClientRegistration::<OldCipherSuite>::start(&mut OsRng, PASSWORD).unwrap(); |
| 43 | + let client = result.state; |
| 44 | + |
| 45 | + let old_server_setup = v3ServerSetup::<OldCipherSuite>::new(&mut OsRng); |
| 46 | + let response = |
| 47 | + v3ServerRegistration::start(&old_server_setup, result.message, CLIENT_IDENTIFIER) |
| 48 | + .unwrap() |
| 49 | + .message; |
| 50 | + |
| 51 | + let upload = client |
| 52 | + .finish(&mut OsRng, PASSWORD, response, Default::default()) |
| 53 | + .unwrap() |
| 54 | + .message; |
| 55 | + |
| 56 | + let old_registration = v3ServerRegistration::finish(upload); |
| 57 | + |
| 58 | + // `ServerSetup` migration. |
| 59 | + let server_setup = { |
| 60 | + let old_serialized = old_server_setup.serialize(); |
| 61 | + |
| 62 | + type OldSeedLen = <<<OldCipherSuite as v3CipherSuite>::OprfCs as voprf::CipherSuite>::Hash as OutputSizeUser>::OutputSize; |
| 63 | + type OldSkLen = <<OldCipherSuite as v3CipherSuite>::KeGroup as v3KeGroup>::SkLen; |
| 64 | + let (old_serialied_rest, old_fake_keypair_serialized): ( |
| 65 | + GenericArray<u8, Sum<OldSeedLen, OldSkLen>>, |
| 66 | + _, |
| 67 | + ) = old_serialized.split(); |
| 68 | + let old_fake_keypair = |
| 69 | + KeyPair::<<OldCipherSuite as v3CipherSuite>::KeGroup>::from_private_key_slice( |
| 70 | + &old_fake_keypair_serialized, |
| 71 | + ) |
| 72 | + .unwrap(); |
| 73 | + let old_fake_pk_serialized = old_fake_keypair.public().serialize(); |
| 74 | + |
| 75 | + let new_serialized = old_serialied_rest.concat(old_fake_pk_serialized); |
| 76 | + ServerSetup::<NewCipherSuite>::deserialize(&new_serialized).unwrap() |
| 77 | + }; |
| 78 | + |
| 79 | + // `ServerRegistration` migration. |
| 80 | + let old_registration_serialized = old_registration.serialize(); |
| 81 | + let registration = |
| 82 | + ServerRegistration::<NewCipherSuite>::deserialize(&old_registration_serialized).unwrap(); |
| 83 | + |
| 84 | + // Check if new `ServerRegistration` still works. |
| 85 | + let result = ClientLogin::<NewCipherSuite>::start(&mut OsRng, PASSWORD).unwrap(); |
| 86 | + let client = result.state; |
| 87 | + |
| 88 | + let result = ServerLogin::start( |
| 89 | + &mut OsRng, |
| 90 | + &server_setup, |
| 91 | + Some(registration), |
| 92 | + result.message, |
| 93 | + CLIENT_IDENTIFIER, |
| 94 | + Default::default(), |
| 95 | + ) |
| 96 | + .unwrap(); |
| 97 | + let server = result.state; |
| 98 | + |
| 99 | + let result = client |
| 100 | + .finish(&mut OsRng, PASSWORD, result.message, Default::default()) |
| 101 | + .unwrap(); |
| 102 | + |
| 103 | + server.finish(result.message, Default::default()).unwrap(); |
| 104 | +} |
0 commit comments