Skip to content

Commit cb183f0

Browse files
committed
use python int_to_bytes to handle arbitrary llen sizes properly
1 parent 0e3599d commit cb183f0

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/rust/src/backend/kdf.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::backend::hmac::Hmac;
1414
use crate::buf::{CffiBuf, CffiMutBuf};
1515
use crate::error::{CryptographyError, CryptographyResult};
1616
use crate::exceptions;
17+
use crate::types;
1718

1819
// NO-COVERAGE-START
1920
#[pyo3::pyclass(
@@ -1709,14 +1710,6 @@ struct KbkdfHmac {
17091710
used: bool,
17101711
}
17111712

1712-
fn int_to_bytes(value: usize, length: usize) -> Vec<u8> {
1713-
let mut bytes = Vec::with_capacity(length);
1714-
for i in (0..length).rev() {
1715-
bytes.push(((value >> (i * 8)) & 0xff) as u8);
1716-
}
1717-
bytes
1718-
}
1719-
17201713
#[allow(clippy::enum_variant_names)]
17211714
#[derive(PartialEq)]
17221715
enum CounterLocation {
@@ -1877,9 +1870,10 @@ impl KbkdfHmac {
18771870
for i in 1..=rounds {
18781871
let mut hmac = Hmac::new_bytes(py, key_material, algorithm_bound)?;
18791872

1880-
let counter = int_to_bytes(i, self.params.rlen);
1873+
let py_counter = types::INT_TO_BYTES.get(py)?.call1((i, self.params.rlen))?;
1874+
let counter = py_counter.extract::<&[u8]>()?;
18811875
hmac.update_bytes(data_before_ctr)?;
1882-
hmac.update_bytes(&counter)?;
1876+
hmac.update_bytes(counter)?;
18831877
hmac.update_bytes(data_after_ctr)?;
18841878

18851879
let result = hmac.finalize_bytes()?;
@@ -1898,15 +1892,18 @@ impl KbkdfHmac {
18981892
}
18991893

19001894
// llen will exist if fixed data is not provided
1901-
let l_val = int_to_bytes(self.length * 8, self.params.llen.unwrap());
1895+
let py_l_val = types::INT_TO_BYTES
1896+
.get(py)?
1897+
.call1((self.length * 8, self.params.llen.unwrap()))?;
1898+
let l_val = py_l_val.extract::<&[u8]>()?;
19021899

19031900
let mut result = Vec::new();
19041901
let label: &[u8] = self.params.label.as_ref().map_or(b"", |l| l.as_bytes(py));
19051902
result.extend_from_slice(label);
19061903
result.push(0x00);
19071904
let context: &[u8] = self.params.context.as_ref().map_or(b"", |l| l.as_bytes(py));
19081905
result.extend_from_slice(context);
1909-
result.extend_from_slice(&l_val);
1906+
result.extend_from_slice(l_val);
19101907

19111908
Ok(result)
19121909
}

src/rust/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub static DEPRECATED_IN_42: LazyPyImport =
4747
pub static DEPRECATED_IN_43: LazyPyImport =
4848
LazyPyImport::new("cryptography.utils", &["DeprecatedIn43"]);
4949

50+
pub static INT_TO_BYTES: LazyPyImport = LazyPyImport::new("cryptography.utils", &["int_to_bytes"]);
51+
5052
pub static ENCODING: LazyPyImport = LazyPyImport::new(
5153
"cryptography.hazmat.primitives.serialization",
5254
&["Encoding"],

0 commit comments

Comments
 (0)