Skip to content

Commit 4427698

Browse files
committed
alternate solution
1 parent cb183f0 commit 4427698

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/rust/src/asn1.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,38 @@ pub(crate) fn py_uint_to_big_endian_bytes<'p>(
7171
py: pyo3::Python<'p>,
7272
v: pyo3::Bound<'p, pyo3::types::PyInt>,
7373
) -> pyo3::PyResult<PyBackedBytes> {
74-
if v.lt(0)? {
75-
return Err(pyo3::exceptions::PyValueError::new_err(
76-
"Negative integers are not supported",
77-
));
78-
}
79-
74+
reject_negative_integer(&v)?;
8075
// Round the length up so that we prefix an extra \x00. This ensures that
8176
// integers that'd have the high bit set in their first octet are not
8277
// encoded as negative in DER.
83-
let n = v
78+
let length = v
8479
.call_method0(pyo3::intern!(py, "bit_length"))?
8580
.extract::<usize>()?
8681
/ 8
8782
+ 1;
88-
Ok(v.call_method1(pyo3::intern!(py, "to_bytes"), (n, "big"))?
89-
.extract()?)
83+
py_uint_to_be_bytes_with_length(py, v, length)
84+
}
85+
86+
fn reject_negative_integer(v: &pyo3::Bound<'_, pyo3::types::PyInt>) -> pyo3::PyResult<()> {
87+
if v.lt(0)? {
88+
Err(pyo3::exceptions::PyValueError::new_err(
89+
"Negative integers are not supported",
90+
))
91+
} else {
92+
Ok(())
93+
}
94+
}
95+
96+
pub(crate) fn py_uint_to_be_bytes_with_length<'p>(
97+
py: pyo3::Python<'p>,
98+
v: pyo3::Bound<'p, pyo3::types::PyInt>,
99+
length: usize,
100+
) -> pyo3::PyResult<PyBackedBytes> {
101+
reject_negative_integer(&v)?;
102+
Ok(
103+
v.call_method1(pyo3::intern!(py, "to_bytes"), (length, "big"))?
104+
.extract()?,
105+
)
90106
}
91107

92108
pub(crate) fn encode_der_data<'p>(

src/rust/src/backend/kdf.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use base64::engine::Engine;
99
use cryptography_crypto::constant_time;
1010
use pyo3::types::{PyAnyMethods, PyBytesMethods};
1111

12+
use crate::asn1::py_uint_to_be_bytes_with_length;
1213
use crate::backend::hashes;
1314
use crate::backend::hmac::Hmac;
1415
use crate::buf::{CffiBuf, CffiMutBuf};
1516
use crate::error::{CryptographyError, CryptographyResult};
1617
use crate::exceptions;
17-
use crate::types;
1818

1919
// NO-COVERAGE-START
2020
#[pyo3::pyclass(
@@ -1870,10 +1870,10 @@ impl KbkdfHmac {
18701870
for i in 1..=rounds {
18711871
let mut hmac = Hmac::new_bytes(py, key_material, algorithm_bound)?;
18721872

1873-
let py_counter = types::INT_TO_BYTES.get(py)?.call1((i, self.params.rlen))?;
1874-
let counter = py_counter.extract::<&[u8]>()?;
1873+
let py_i = pyo3::types::PyInt::new(py, i);
1874+
let counter = py_uint_to_be_bytes_with_length(py, py_i, self.params.rlen)?;
18751875
hmac.update_bytes(data_before_ctr)?;
1876-
hmac.update_bytes(counter)?;
1876+
hmac.update_bytes(counter.as_ref())?;
18771877
hmac.update_bytes(data_after_ctr)?;
18781878

18791879
let result = hmac.finalize_bytes()?;
@@ -1892,18 +1892,18 @@ impl KbkdfHmac {
18921892
}
18931893

18941894
// llen will exist if fixed data is not provided
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]>()?;
1895+
let py_bitlength = pyo3::types::PyInt::new(py, self.length)
1896+
.mul(8)?
1897+
.extract::<pyo3::Bound<'_, pyo3::types::PyInt>>()?;
1898+
let l_val = py_uint_to_be_bytes_with_length(py, py_bitlength, self.params.llen.unwrap())?;
18991899

19001900
let mut result = Vec::new();
19011901
let label: &[u8] = self.params.label.as_ref().map_or(b"", |l| l.as_bytes(py));
19021902
result.extend_from_slice(label);
19031903
result.push(0x00);
19041904
let context: &[u8] = self.params.context.as_ref().map_or(b"", |l| l.as_bytes(py));
19051905
result.extend_from_slice(context);
1906-
result.extend_from_slice(l_val);
1906+
result.extend_from_slice(l_val.as_ref());
19071907

19081908
Ok(result)
19091909
}

src/rust/src/types.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ 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-
5250
pub static ENCODING: LazyPyImport = LazyPyImport::new(
5351
"cryptography.hazmat.primitives.serialization",
5452
&["Encoding"],

0 commit comments

Comments
 (0)