Skip to content

Commit

Permalink
Merge pull request #3 from Andrepuel/main
Browse files Browse the repository at this point in the history
Fixes the CRC32 calculation for FINGERPRINT
  • Loading branch information
Rain authored Jul 5, 2021
2 parents 720327a + 95b5e8c commit d283cd9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/stun/src/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::checks::*;
use crate::message::*;

use anyhow::Result;
use crc::{Crc, CRC_32_ISCSI};
use crc::{Crc, CRC_32_ISO_HDLC};

// FingerprintAttr represents FINGERPRINT attribute.
//
Expand All @@ -31,7 +31,7 @@ const FINGERPRINT_SIZE: usize = 4; // 32 bit
// the 32-bit value 0x5354554e (the XOR helps in cases where an
// application packet is also using CRC-32 in it).
pub fn fingerprint_value(b: &[u8]) -> u32 {
let checksum = Crc::<u32>::new(&CRC_32_ISCSI).checksum(b);
let checksum = Crc::<u32>::new(&CRC_32_ISO_HDLC).checksum(b);
checksum ^ FINGERPRINT_XOR_VALUE // XOR
}

Expand Down
21 changes: 21 additions & 0 deletions crates/stun/src/fingerprint/fingerprint_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ use crate::textattrs::TextAttribute;

use crate::attributes::ATTR_SOFTWARE;

#[test]
fn fingerprint_uses_crc_32_iso_hdlc() -> Result<()> {
let mut m = Message::new();

let a = TextAttribute {
attr: ATTR_SOFTWARE,
text: "software".to_owned(),
};
a.add_to(&mut m)?;
m.write_header();

FINGERPRINT.add_to(&mut m)?;
m.write_header();

assert_eq!(&m.raw[0..m.raw.len()-8], b"\x00\x00\x00\x14\x21\x12\xA4\x42\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x22\x00\x08\x73\x6F\x66\x74\x77\x61\x72\x65");

assert_eq!(m.raw[m.raw.len() - 4..], [0xe4, 0x4c, 0x33, 0xd9]);

Ok(())
}

#[test]
fn test_fingerprint_check() -> Result<()> {
let mut m = Message::new();
Expand Down

0 comments on commit d283cd9

Please sign in to comment.