Skip to content

Commit

Permalink
base32ct: fix broken encoding of unpadded base32 (#1421)
Browse files Browse the repository at this point in the history
Fixes #943 #1420
  • Loading branch information
dodomorandi authored May 28, 2024
1 parent 57d08ba commit ae4e6a1
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
73 changes: 72 additions & 1 deletion base32ct/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl<T: Alphabet> Encoding for T {
} else if Self::PADDED {
((bytes.len() - 1) / 5 + 1) * 8
} else {
(bytes.len() * 8) / 5 + 1
(bytes.len() * 8 + 4) / 5
}
}
}
Expand Down Expand Up @@ -258,3 +258,74 @@ fn remove_padding(mut input: &[u8]) -> Result<&[u8]> {

Ok(input)
}

#[cfg(all(test, feature = "alloc"))]
mod tests {
use crate::{Base32, Base32Unpadded, Encoding};

struct LenData {
fourty_bit_groups_len: usize,
last_group_len: usize,
padding_len: usize,
}

fn get_len_data(data_len: usize) -> LenData {
// More information about the calculation can be found at
// https://www.rfc-editor.org/rfc/rfc4648#section-6
let fourty_bit_groups_len = data_len / 5 * 8;
let (last_group_len, padding_len) = match data_len % 5 {
0 => (0, 0),
1 => (2, 6),
2 => (4, 4),
3 => (5, 3),
4 => (7, 1),
_ => unreachable!(),
};

LenData {
fourty_bit_groups_len,
last_group_len,
padding_len,
}
}

#[test]
fn unpadded_encoded_len() {
let mut buf = vec![];
assert_eq!(Base32Unpadded::encoded_len(&buf), 0);

for _ in 0..10 {
buf.push(b'a');
let LenData {
fourty_bit_groups_len,
last_group_len,
padding_len: _,
} = get_len_data(buf.len());

assert_eq!(
Base32Unpadded::encoded_len(&buf),
fourty_bit_groups_len + last_group_len
);
}
}

#[test]
fn padded_encoded_len() {
let mut buf = vec![];
assert_eq!(Base32::encoded_len(&buf), 0);

for _ in 0..10 {
buf.push(b'a');
let LenData {
fourty_bit_groups_len,
last_group_len,
padding_len,
} = get_len_data(buf.len());

assert_eq!(
Base32::encoded_len(&buf),
fourty_bit_groups_len + last_group_len + padding_len,
);
}
}
}
48 changes: 48 additions & 0 deletions base32ct/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ const LOWER_PADDED_VECTORS: &[TestVector] = &[
decoded: &[32, 7],
encoded: "eadq====",
},
TestVector {
decoded: &[0x12, 0x34, 0x56],
encoded: "ci2fm===",
},
TestVector {
decoded: &[0x12, 0x34, 0x56, 0x78, 0x9a],
encoded: "ci2fm6e2",
},
TestVector {
decoded: &[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc],
encoded: "ci2fm6e2xq======",
},
];

const LOWER_UNPADDED_VECTORS: &[TestVector] = &[
Expand All @@ -38,6 +50,18 @@ const LOWER_UNPADDED_VECTORS: &[TestVector] = &[
decoded: &[32, 7],
encoded: "eadq",
},
TestVector {
decoded: &[0x12, 0x34, 0x56],
encoded: "ci2fm",
},
TestVector {
decoded: &[0x12, 0x34, 0x56, 0x78, 0x9a],
encoded: "ci2fm6e2",
},
TestVector {
decoded: &[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc],
encoded: "ci2fm6e2xq",
},
];

const UPPER_PADDED_VECTORS: &[TestVector] = &[
Expand All @@ -53,6 +77,18 @@ const UPPER_PADDED_VECTORS: &[TestVector] = &[
decoded: &[32, 7],
encoded: "EADQ====",
},
TestVector {
decoded: &[0x12, 0x34, 0x56],
encoded: "CI2FM===",
},
TestVector {
decoded: &[0x12, 0x34, 0x56, 0x78, 0x9a],
encoded: "CI2FM6E2",
},
TestVector {
decoded: &[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc],
encoded: "CI2FM6E2XQ======",
},
];

const UPPER_UNPADDED_VECTORS: &[TestVector] = &[
Expand All @@ -68,6 +104,18 @@ const UPPER_UNPADDED_VECTORS: &[TestVector] = &[
decoded: &[32, 7],
encoded: "EADQ",
},
TestVector {
decoded: &[0x12, 0x34, 0x56],
encoded: "CI2FM",
},
TestVector {
decoded: &[0x12, 0x34, 0x56, 0x78, 0x9a],
encoded: "CI2FM6E2",
},
TestVector {
decoded: &[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc],
encoded: "CI2FM6E2XQ",
},
];

#[test]
Expand Down

0 comments on commit ae4e6a1

Please sign in to comment.