Simplifying error handling (#232)
This commit is contained in:
@@ -3,16 +3,16 @@
|
||||
// This source code is licensed under the MIT license found in the
|
||||
// LICENSE file in the root directory of this source tree.
|
||||
|
||||
use crate::errors::PakeError;
|
||||
use crate::errors::ProtocolError;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
// Corresponds to the I2OSP() function from RFC8017
|
||||
pub(crate) fn i2osp(input: usize, length: usize) -> Result<alloc::vec::Vec<u8>, PakeError> {
|
||||
pub(crate) fn i2osp(input: usize, length: usize) -> Result<alloc::vec::Vec<u8>, ProtocolError> {
|
||||
let sizeof_usize = core::mem::size_of::<usize>();
|
||||
|
||||
// Check if input >= 256^length
|
||||
if (sizeof_usize as u32 - input.leading_zeros() / 8) > length as u32 {
|
||||
return Err(PakeError::SerializationError);
|
||||
return Err(ProtocolError::SerializationError);
|
||||
}
|
||||
|
||||
if length <= sizeof_usize {
|
||||
@@ -28,9 +28,9 @@ pub(crate) fn i2osp(input: usize, length: usize) -> Result<alloc::vec::Vec<u8>,
|
||||
}
|
||||
|
||||
// Corresponds to the OS2IP() function from RFC8017
|
||||
pub(crate) fn os2ip(input: &[u8]) -> Result<usize, PakeError> {
|
||||
pub(crate) fn os2ip(input: &[u8]) -> Result<usize, ProtocolError> {
|
||||
if input.len() > core::mem::size_of::<usize>() {
|
||||
return Err(PakeError::SerializationError);
|
||||
return Err(ProtocolError::SerializationError);
|
||||
}
|
||||
|
||||
let mut output_array = [0u8; core::mem::size_of::<usize>()];
|
||||
@@ -39,20 +39,23 @@ pub(crate) fn os2ip(input: &[u8]) -> Result<usize, PakeError> {
|
||||
}
|
||||
|
||||
// Computes I2OSP(len(input), max_bytes) || input
|
||||
pub(crate) fn serialize(input: &[u8], max_bytes: usize) -> Result<Vec<u8>, PakeError> {
|
||||
pub(crate) fn serialize(input: &[u8], max_bytes: usize) -> Result<Vec<u8>, ProtocolError> {
|
||||
Ok([&i2osp(input.len(), max_bytes)?, input].concat())
|
||||
}
|
||||
|
||||
// Tokenizes an input of the format I2OSP(len(input), max_bytes) || input, outputting
|
||||
// (input, remainder)
|
||||
pub(crate) fn tokenize(input: &[u8], size_bytes: usize) -> Result<(Vec<u8>, Vec<u8>), PakeError> {
|
||||
pub(crate) fn tokenize(
|
||||
input: &[u8],
|
||||
size_bytes: usize,
|
||||
) -> Result<(Vec<u8>, Vec<u8>), ProtocolError> {
|
||||
if size_bytes > core::mem::size_of::<usize>() || input.len() < size_bytes {
|
||||
return Err(PakeError::SerializationError);
|
||||
return Err(ProtocolError::SerializationError);
|
||||
}
|
||||
|
||||
let size = os2ip(&input[..size_bytes])?;
|
||||
if size_bytes + size > input.len() {
|
||||
return Err(PakeError::SerializationError);
|
||||
return Err(ProtocolError::SerializationError);
|
||||
}
|
||||
|
||||
Ok((
|
||||
|
||||
Reference in New Issue
Block a user