xref: /webrtc/stun/src/checks.rs (revision ffe74184)
1 use crate::attributes::*;
2 use crate::error::*;
3 
4 use subtle::ConstantTimeEq;
5 
6 // check_size returns ErrAttrSizeInvalid if got is not equal to expected.
check_size(_at: AttrType, got: usize, expected: usize) -> Result<()>7 pub fn check_size(_at: AttrType, got: usize, expected: usize) -> Result<()> {
8     if got == expected {
9         Ok(())
10     } else {
11         Err(Error::ErrAttributeSizeInvalid)
12     }
13 }
14 
15 // is_attr_size_invalid returns true if error means that attribute size is invalid.
is_attr_size_invalid(err: &Error) -> bool16 pub fn is_attr_size_invalid(err: &Error) -> bool {
17     Error::ErrAttributeSizeInvalid == *err
18 }
19 
check_hmac(got: &[u8], expected: &[u8]) -> Result<()>20 pub(crate) fn check_hmac(got: &[u8], expected: &[u8]) -> Result<()> {
21     if got.ct_eq(expected).unwrap_u8() != 1 {
22         Err(Error::ErrIntegrityMismatch)
23     } else {
24         Ok(())
25     }
26 }
27 
check_fingerprint(got: u32, expected: u32) -> Result<()>28 pub(crate) fn check_fingerprint(got: u32, expected: u32) -> Result<()> {
29     if got == expected {
30         Ok(())
31     } else {
32         Err(Error::ErrFingerprintMismatch)
33     }
34 }
35 
36 // check_overflow returns ErrAttributeSizeOverflow if got is bigger that max.
check_overflow(_at: AttrType, got: usize, max: usize) -> Result<()>37 pub fn check_overflow(_at: AttrType, got: usize, max: usize) -> Result<()> {
38     if got <= max {
39         Ok(())
40     } else {
41         Err(Error::ErrAttributeSizeOverflow)
42     }
43 }
44 
45 // is_attr_size_overflow returns true if error means that attribute size is too big.
is_attr_size_overflow(err: &Error) -> bool46 pub fn is_attr_size_overflow(err: &Error) -> bool {
47     Error::ErrAttributeSizeOverflow == *err
48 }
49