Repository: novifinancial/opaque-ke
Pull request #93 (2020-11-30 14:25:24)

Removing protocol message bytes
Following https://github.com/cfrg/draft-irtf-cfrg-opaque/commit/a274e8e6f7fbefeaaae762510bb63c7869e73c63


Patch:
diff --git a/src/messages.rs b/src/messages.rs
index 68d63cf..5bda26a 100644
--- a/src/messages.rs
+++ b/src/messages.rs
@@ -16,9 +16,7 @@ use crate::{
     hash::Hash,
     key_exchange::traits::{KeyExchange, ToBytes},
     keypair::{KeyPair, SizedBytesExt},
-    serialization::{
-        serialize, tokenize, u8_to_credential_type, CredentialType, ProtocolMessageType,
-    },
+    serialization::{serialize, tokenize, u8_to_credential_type, CredentialType},
 };
 use generic_array::{typenum::Unsigned, GenericArray};
 use generic_bytes::SizedBytes;
@@ -55,29 +53,12 @@ impl<Grp: Group> RegisterFirstMessage<Grp> {
 
     /// Serialization into bytes
     pub fn serialize(&self) -> Vec<u8> {
-        let mut registration_request: Vec<u8> = Vec::new();
-        registration_request.extend_from_slice(&serialize(&self.alpha.to_arr(), 2));
-
-        let mut output: Vec<u8> = Vec::new();
-        output.push(ProtocolMessageType::from(self) as u8 + 1);
-        output.extend_from_slice(&serialize(&registration_request, 3));
-        output
+        serialize(&self.alpha.to_arr(), 2)
     }
 
     /// Deserialization from bytes
     pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
-        if input.is_empty()
-            || input.is_empty()
-            || input[0] != ProtocolMessageType::RegistrationRequest as u8 + 1
-        {
-            return Err(PakeError::SerializationError.into());
-        }
-
-        let (data, remainder) = tokenize(&input[1..], 3)?;
-        if !remainder.is_empty() {
-            return Err(PakeError::SerializationError.into());
-        }
-        let (alpha_bytes, remainder) = tokenize(&data, 2)?;
+        let (alpha_bytes, remainder) = tokenize(&input, 2)?;
 
         if !remainder.is_empty() {
             return Err(PakeError::SerializationError.into());
@@ -171,24 +152,12 @@ where
         .concat();
         registration_response.extend_from_slice(&ecf_serialized);
 
-        let mut output: Vec<u8> = Vec::new();
-        output.push(ProtocolMessageType::from(self) as u8 + 1);
-        output.extend_from_slice(&serialize(&registration_response, 3));
-        output
+        registration_response
     }
 
     /// Deserialization from bytes
     pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
-        if input.is_empty() || input[0] != ProtocolMessageType::RegistrationResponse as u8 + 1 {
-            return Err(PakeError::SerializationError.into());
-        }
-
-        let (data, remainder) = tokenize(&input[1..], 3)?;
-        if !remainder.is_empty() {
-            return Err(PakeError::SerializationError.into());
-        }
-
-        let (beta_bytes, remainder) = tokenize(&data, 2)?;
+        let (beta_bytes, remainder) = tokenize(&input, 2)?;
         let (server_s_pk, remainder) = tokenize(&remainder, 2)?;
 
         // Handle ecf deserialization
@@ -270,25 +239,12 @@ where
         let mut registration_upload: Vec<u8> = Vec::new();
         registration_upload.extend_from_slice(&self.envelope.serialize());
         registration_upload.extend_from_slice(&serialize(&self.client_s_pk.to_arr(), 2));
-
-        let mut output: Vec<u8> = Vec::new();
-        output.push(ProtocolMessageType::from(self) as u8 + 1);
-        output.extend_from_slice(&serialize(&registration_upload, 3));
-        output
+        registration_upload
     }
 
     /// Deserialization from bytes
     pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
-        if input.is_empty() || input[0] != ProtocolMessageType::RegistrationUpload as u8 + 1 {
-            return Err(PakeError::SerializationError.into());
-        }
-
-        let (data, remainder) = tokenize(&input[1..], 3)?;
-        if !remainder.is_empty() {
-            return Err(PakeError::SerializationError.into());
-        }
-
-        let (envelope, remainder) = Envelope::<D>::deserialize(&data)?;
+        let (envelope, remainder) = Envelope::<D>::deserialize(&input)?;
         let (client_s_pk, remainder) = tokenize(&remainder, 2)?;
 
         if !remainder.is_empty() {
@@ -326,26 +282,13 @@ impl<CS: CipherSuite> LoginFirstMessage<CS> {
     pub fn serialize(&self) -> Vec<u8> {
         let mut credential_request: Vec<u8> = Vec::new();
         credential_request.extend_from_slice(&serialize(&self.alpha.to_arr(), 2));
-
-        let mut output: Vec<u8> = Vec::new();
-        output.push(ProtocolMessageType::from(self) as u8 + 1);
-        output.extend_from_slice(&serialize(&credential_request, 3));
-        output.extend_from_slice(&self.ke1_message.to_bytes());
-        output
+        credential_request.extend_from_slice(&self.ke1_message.to_bytes());
+        credential_request
     }
 
     /// Deserialization from bytes
     pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
-        if input.is_empty() || input[0] != ProtocolMessageType::CredentialRequest as u8 + 1 {
-            return Err(PakeError::SerializationError.into());
-        }
-
-        let (data, ke1m) = tokenize(&input[1..], 3)?;
-        let (alpha_bytes, remainder) = tokenize(&data, 2)?;
-
-        if !remainder.is_empty() {
-            return Err(PakeError::SerializationError.into());
-        }
+        let (alpha_bytes, ke1m) = tokenize(&input, 2)?;
 
         let elem_len = <CS::Group as Group>::ElemLen::to_usize();
         let checked_slice = check_slice_size(&alpha_bytes, elem_len, "login_first_message_bytes")?;
@@ -377,24 +320,14 @@ impl<CS: CipherSuite> LoginSecondMessage<CS> {
         let mut credential_response: Vec<u8> = Vec::new();
         credential_response.extend_from_slice(&serialize(&self.beta.to_arr(), 2));
         credential_response.extend_from_slice(&self.envelope.to_bytes());
-
-        let mut output: Vec<u8> = Vec::new();
-        output.push(ProtocolMessageType::from(self) as u8 + 1);
-        output.extend_from_slice(&serialize(&credential_response, 3));
-        output.extend_from_slice(&self.ke2_message.to_bytes());
-        output
+        credential_response.extend_from_slice(&self.ke2_message.to_bytes());
+        credential_response
     }
 
     /// Deserialization from bytes
     pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
-        if input.is_empty() || input[0] != ProtocolMessageType::CredentialResponse as u8 + 1 {
-            return Err(PakeError::SerializationError.into());
-        }
-
-        let (data, ke2m) = tokenize(&input[1..], 3)?;
-        let (beta_bytes, envelope_bytes) = tokenize(&data, 2)?;
-
-        let concatenated = [&beta_bytes[..], &envelope_bytes[..], &ke2m[..]].concat();
+        let (beta_bytes, envelope_and_ke2m_bytes) = tokenize(&input, 2)?;
+        let concatenated = [&beta_bytes[..], &envelope_and_ke2m_bytes[..]].concat();
         Self::try_from(&concatenated[..])
     }
 }
@@ -449,19 +382,12 @@ impl<CS: CipherSuite> TryFrom<&[u8]> for LoginThirdMessage<CS> {
 impl<CS: CipherSuite> LoginThirdMessage<CS> {
     /// Serialization into bytes
     pub fn serialize(&self) -> Vec<u8> {
-        let mut output: Vec<u8> = Vec::new();
-        output.push(ProtocolMessageType::from(self) as u8 + 1);
-        output.extend_from_slice(&self.ke3_message.to_bytes());
-        output
+        self.ke3_message.to_bytes()
     }
 
     /// Deserialization from bytes
     pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
-        if input.is_empty() || input[0] != ProtocolMessageType::KeyExchange as u8 + 1 {
-            return Err(PakeError::SerializationError.into());
-        }
-
-        Self::try_from(&input[1..])
+        Self::try_from(&input[..])
     }
 
     /// byte representation for the login finalization
diff --git a/src/serialization/mod.rs b/src/serialization/mod.rs
index 0e2d77d..aa8582e 100644
--- a/src/serialization/mod.rs
+++ b/src/serialization/mod.rs
@@ -5,20 +5,6 @@
 
 use crate::errors::PakeError;
 
-use crate::{
-    ciphersuite::CipherSuite, hash::Hash, keypair::KeyPair, LoginFirstMessage, LoginSecondMessage,
-    LoginThirdMessage, RegisterFirstMessage, RegisterSecondMessage, RegisterThirdMessage,
-};
-
-pub enum ProtocolMessageType {
-    RegistrationRequest,
-    RegistrationResponse,
-    RegistrationUpload,
-    CredentialRequest,
-    CredentialResponse,
-    KeyExchange,
-}
-
 #[derive(Copy, Clone, Eq, Hash, PartialEq)]
 pub enum CredentialType {
     SkU,
@@ -39,42 +25,6 @@ pub(crate) fn u8_to_credential_type(x: u8) -> Option<CredentialType> {
     }
 }
 
-impl<T> From<&RegisterFirstMessage<T>> for ProtocolMessageType {
-    fn from(_mt: &RegisterFirstMessage<T>) -> Self {
-        ProtocolMessageType::RegistrationRequest
-    }
-}
-
-impl<T> From<&RegisterSecondMessage<T>> for ProtocolMessageType {
-    fn from(_mt: &RegisterSecondMessage<T>) -> Self {
-        ProtocolMessageType::RegistrationResponse
-    }
-}
-
-impl<T: KeyPair, U: Hash> From<&RegisterThirdMessage<T, U>> for ProtocolMessageType {
-    fn from(_mt: &RegisterThirdMessage<T, U>) -> Self {
-        ProtocolMessageType::RegistrationUpload
-    }
-}
-
-impl<T: CipherSuite> From<&LoginFirstMessage<T>> for ProtocolMessageType {
-    fn from(_mt: &LoginFirstMessage<T>) -> Self {
-        ProtocolMessageType::CredentialRequest
-    }
-}
-
-impl<T: CipherSuite> From<&LoginSecondMessage<T>> for ProtocolMessageType {
-    fn from(_mt: &LoginSecondMessage<T>) -> Self {
-        ProtocolMessageType::CredentialResponse
-    }
-}
-
-impl<T: CipherSuite> From<&LoginThirdMessage<T>> for ProtocolMessageType {
-    fn from(_mt: &LoginThirdMessage<T>) -> Self {
-        ProtocolMessageType::KeyExchange
-    }
-}
-
 // Corresponds to the I2OSP() function from RFC8017
 pub(crate) fn i2osp(input: usize, length: usize) -> Vec<u8> {
     if length <= std::mem::size_of::<usize>() {


Test patch:
diff --git a/src/serialization/tests.rs b/src/serialization/tests.rs
index 77bacce..fd6e0e1 100644
--- a/src/serialization/tests.rs
+++ b/src/serialization/tests.rs
@@ -13,7 +13,7 @@ use crate::{
     },
     keypair::{KeyPair, X25519KeyPair},
     opaque::*,
-    serialization::{i2osp, os2ip, serialize, ProtocolMessageType},
+    serialization::{i2osp, os2ip, serialize},
     *,
 };
 
@@ -116,11 +116,8 @@ fn register_first_message_roundtrip() {
     let pt_bytes = pt.to_arr().to_vec();
 
     let alpha_length: usize = 32;
-    let total_length: usize = alpha_length + 2;
 
     let mut input = Vec::new();
-    input.extend_from_slice(&[ProtocolMessageType::RegistrationRequest as u8 + 1]);
-    input.extend_from_slice(&total_length.to_be_bytes()[std::mem::size_of::<usize>() - 3..]);
     input.extend_from_slice(&alpha_length.to_be_bytes()[std::mem::size_of::<usize>() - 2..]);
     input.extend_from_slice(pt_bytes.as_slice());
 
@@ -140,11 +137,8 @@ fn register_second_message_roundtrip() {
 
     let beta_length: usize = beta_bytes.len();
     let pubkey_length: usize = pubkey_bytes.len();
-    let total_length: usize = beta_length + pubkey_length + credential_types.len() + 4;
 
     let mut input = Vec::new();
-    input.extend_from_slice(&[ProtocolMessageType::RegistrationResponse as u8 + 1]);
-    input.extend_from_slice(&total_length.to_be_bytes()[std::mem::size_of::<usize>() - 3..]);
     input.extend_from_slice(&beta_length.to_be_bytes()[std::mem::size_of::<usize>() - 2..]);
     input.extend_from_slice(beta_bytes.as_slice());
     input.extend_from_slice(&pubkey_length.to_be_bytes()[std::mem::size_of::<usize>() - 2..]);
@@ -173,11 +167,8 @@ fn register_third_message_roundtrip() {
     let envelope_bytes = envelope.serialize();
 
     let pubkey_length: usize = pubkey_bytes.len();
-    let total_length: usize = pubkey_length + envelope_bytes.len() + 2;
 
     let mut input = Vec::new();
-    input.extend_from_slice(&[ProtocolMessageType::RegistrationUpload as u8 + 1]);
-    input.extend_from_slice(&total_length.to_be_bytes()[std::mem::size_of::<usize>() - 3..]);
     input.extend_from_slice(&envelope_bytes);
     input.extend_from_slice(&pubkey_length.to_be_bytes()[std::mem::size_of::<usize>() - 2..]);
     input.extend_from_slice(&pubkey_bytes[..]);
@@ -208,13 +199,8 @@ fn login_first_message_roundtrip() {
     .concat();
 
     let alpha_length = alpha_bytes.len();
-    let total_length_without_ke1m: usize = alpha_length + 2;
 
     let mut input = Vec::new();
-    input.extend_from_slice(&[ProtocolMessageType::CredentialRequest as u8 + 1]);
-    input.extend_from_slice(
-        &total_length_without_ke1m.to_be_bytes()[std::mem::size_of::<usize>() - 3..],
-    );
     input.extend_from_slice(&alpha_length.to_be_bytes()[std::mem::size_of::<usize>() - 2..]);
     input.extend_from_slice(&alpha_bytes);
     input.extend_from_slice(&ke1m[..]);
@@ -262,16 +248,10 @@ fn login_second_message_roundtrip() {
     ]
     .concat();
 
-    let total_length_without_ke2m = pt_bytes.len() + envelope.to_bytes().len() + 2;
-
     let mut input = Vec::new();
-    input.extend_from_slice(&[ProtocolMessageType::CredentialResponse as u8 + 1]);
-    input.extend_from_slice(
-        &total_length_without_ke2m.to_be_bytes()[std::mem::size_of::<usize>() - 3..],
-    );
     input.extend_from_slice(&pt_bytes.len().to_be_bytes()[std::mem::size_of::<usize>() - 2..]);
     input.extend_from_slice(pt_bytes.as_slice());
-    input.extend_from_slice(&envelope.to_bytes());
+    input.extend_from_slice(&envelope.serialize());
     input.extend_from_slice(&ke2m[..]);
 
     let l2 = LoginSecondMessage::<Default>::deserialize(&input).unwrap();
@@ -289,17 +269,13 @@ fn login_third_message_roundtrip() {
     let mut mac = [0u8; 32];
     rng.fill_bytes(&mut mac);
 
-    let ke3m: Vec<u8> = [
+    let input: Vec<u8> = [
         &serialize(&info.to_vec(), 2),
         &serialize(&e_info.to_vec(), 2),
         &mac[..],
     ]
     .concat();
 
-    let mut input = Vec::new();
-    input.extend_from_slice(&[ProtocolMessageType::KeyExchange as u8 + 1]);
-    input.extend_from_slice(&ke3m[..]);
-
     let l3 = LoginThirdMessage::<Default>::deserialize(&input).unwrap();
     let l3_bytes = l3.serialize();
     assert_eq!(input, l3_bytes);
diff --git a/src/tests/opaque_ke_test.rs b/src/tests/opaque_ke_test.rs
index b510f77..c606f3c 100644
--- a/src/tests/opaque_ke_test.rs
+++ b/src/tests/opaque_ke_test.rs
@@ -93,11 +93,11 @@ static TEST_VECTOR: &str = r#"
     "einfo2": "65696e666f32",
     "info3": "696e666f33",
     "einfo3": "65696e666f33",
-    "r1": "0100002200204e61b9b1f2efbc4da7f114b9b8585ece25b3faec9c45e49ff60f310f61586417",
-    "r2": "020000480020fe43bff51e3868ecc913c97774e070e68acf5532f1862da20e554450f43856a2002096118ab2892cb29ab29af9e8f0cf7d4d336e757e4f56bbe6afde887714118e1201010103",
-    "r3": "030000ae491dc86966491b649bab099c1dd3a7927ea0ecb74b88d5d08c7267f151e9bf67002351e93c57b3c307d92ae0d38cbc9c66636511a3fa97bb34947e85c79dbafd670a22152a002303002096118ab2892cb29ab29af9e8f0cf7d4d336e757e4f56bbe6afde887714118e120020626884cb268cfbb7af1edd23ebe12c0cc995a95dffcf225ad9db330185846e9400209e1a7d68007f9db6f2cc515c9e42ce52eee78a6e4d6bca5a16d49bbfa4cd462e",
-    "l1": "0400002200204e61b9b1f2efbc4da7f114b9b8585ece25b3faec9c45e49ff60f310f61586417741fbafaf51abbeab80434c1d1afccc81059b356b84ab704447d9632d5696d910005696e666f315abe58c49c4fe4b3c82f6313857969cc9a2bc913865026ff39b0235636434844",
-    "l2": "050000ae0020fe43bff51e3868ecc913c97774e070e68acf5532f1862da20e554450f43856a2491dc86966491b649bab099c1dd3a7927ea0ecb74b88d5d08c7267f151e9bf67002351e93c57b3c307d92ae0d38cbc9c66636511a3fa97bb34947e85c79dbafd670a22152a002303002096118ab2892cb29ab29af9e8f0cf7d4d336e757e4f56bbe6afde887714118e120020626884cb268cfbb7af1edd23ebe12c0cc995a95dffcf225ad9db330185846e942021fce09d946296e0a0671555d7552c7f1f69ed9d116cc3b6605449abcf69510005696e666f329fe1377a64db22c3ae9ebe7d6016b5cbb8060448d8b5582548566f41202f7f33000665696e666f32317af5691eb732163f54e2e497b5a883598baafce9a3a87a710f374b611aa3d5",
+    "r1": "00204e61b9b1f2efbc4da7f114b9b8585ece25b3faec9c45e49ff60f310f61586417",
+    "r2": "0020fe43bff51e3868ecc913c97774e070e68acf5532f1862da20e554450f43856a2002096118ab2892cb29ab29af9e8f0cf7d4d336e757e4f56bbe6afde887714118e1201010103",
+    "r3": "491dc86966491b649bab099c1dd3a7927ea0ecb74b88d5d08c7267f151e9bf67002351e93c57b3c307d92ae0d38cbc9c66636511a3fa97bb34947e85c79dbafd670a22152a002303002096118ab2892cb29ab29af9e8f0cf7d4d336e757e4f56bbe6afde887714118e120020626884cb268cfbb7af1edd23ebe12c0cc995a95dffcf225ad9db330185846e9400209e1a7d68007f9db6f2cc515c9e42ce52eee78a6e4d6bca5a16d49bbfa4cd462e",
+    "l1": "00204e61b9b1f2efbc4da7f114b9b8585ece25b3faec9c45e49ff60f310f61586417741fbafaf51abbeab80434c1d1afccc81059b356b84ab704447d9632d5696d910005696e666f315abe58c49c4fe4b3c82f6313857969cc9a2bc913865026ff39b0235636434844",
+    "l2": "0020fe43bff51e3868ecc913c97774e070e68acf5532f1862da20e554450f43856a2491dc86966491b649bab099c1dd3a7927ea0ecb74b88d5d08c7267f151e9bf67002351e93c57b3c307d92ae0d38cbc9c66636511a3fa97bb34947e85c79dbafd670a22152a002303002096118ab2892cb29ab29af9e8f0cf7d4d336e757e4f56bbe6afde887714118e120020626884cb268cfbb7af1edd23ebe12c0cc995a95dffcf225ad9db330185846e942021fce09d946296e0a0671555d7552c7f1f69ed9d116cc3b6605449abcf69510005696e666f329fe1377a64db22c3ae9ebe7d6016b5cbb8060448d8b5582548566f41202f7f33000665696e666f32317af5691eb732163f54e2e497b5a883598baafce9a3a87a710f374b611aa3d5",
     "l3": "0005696e666f33000665696e666f339ca8f1957db30ea986d9d29d9efc458476959dde2efeb4308bcde0e807152987",
     "client_registration_state": "000369645500036964532d33e5917ed8ed3b4318dc15d6844fa7fae0b84210ac588350fccf2aa7e26b0370617373776f7264",
     "client_login_state": "000369645500036964532d33e5917ed8ed3b4318dc15d6844fa7fae0b84210ac588350fccf2aa7e26b0378a41d08c2cb8e1dd3818a494136f5ee321ea2478ba161f0bcbc282c68467a5a741fbafaf51abbeab80434c1d1afccc81059b356b84ab704447d9632d5696d91c346939f310305b1a7e4a3d0958c3c8ecb62ba654f99a3f291aa17f665db721070617373776f7264",