Skip to content

Commit 0c79072

Browse files
authored
Remove duplication of already_finalized_error (pyca#11513)
1 parent 7b5c7fe commit 0c79072

File tree

7 files changed

+24
-26
lines changed

7 files changed

+24
-26
lines changed

src/rust/src/backend/ciphers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ struct PyAEADDecryptionContext {
259259
aad_bytes_remaining: u64,
260260
}
261261

262-
fn get_mut_ctx(ctx: Option<&mut CipherContext>) -> pyo3::PyResult<&mut CipherContext> {
263-
ctx.ok_or_else(|| exceptions::AlreadyFinalized::new_err("Context was already finalized."))
262+
fn get_mut_ctx(ctx: Option<&mut CipherContext>) -> CryptographyResult<&mut CipherContext> {
263+
ctx.ok_or_else(exceptions::already_finalized_error)
264264
}
265265

266266
#[pyo3::pymethods]

src/rust/src/backend/cmac.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// for complete details.
44

55
use crate::backend::cipher_registry;
6-
use crate::backend::hashes::already_finalized_error;
76
use crate::buf::CffiBuf;
87
use crate::error::{CryptographyError, CryptographyResult};
98
use crate::{exceptions, types};
@@ -22,14 +21,14 @@ impl Cmac {
2221
if let Some(ctx) = self.ctx.as_ref() {
2322
return Ok(ctx);
2423
};
25-
Err(already_finalized_error())
24+
Err(exceptions::already_finalized_error())
2625
}
2726

2827
fn get_mut_ctx(&mut self) -> CryptographyResult<&mut cryptography_openssl::cmac::Cmac> {
2928
if let Some(ctx) = self.ctx.as_mut() {
3029
return Ok(ctx);
3130
}
32-
Err(already_finalized_error())
31+
Err(exceptions::already_finalized_error())
3332
}
3433
}
3534

src/rust/src/backend/hashes.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,19 @@ pub(crate) struct Hash {
1717
ctx: Option<openssl::hash::Hasher>,
1818
}
1919

20-
pub(crate) fn already_finalized_error() -> CryptographyError {
21-
CryptographyError::from(exceptions::AlreadyFinalized::new_err(
22-
"Context was already finalized.",
23-
))
24-
}
25-
2620
impl Hash {
2721
fn get_ctx(&self) -> CryptographyResult<&openssl::hash::Hasher> {
2822
if let Some(ctx) = self.ctx.as_ref() {
2923
return Ok(ctx);
3024
};
31-
Err(already_finalized_error())
25+
Err(exceptions::already_finalized_error())
3226
}
3327

3428
fn get_mut_ctx(&mut self) -> CryptographyResult<&mut openssl::hash::Hasher> {
3529
if let Some(ctx) = self.ctx.as_mut() {
3630
return Ok(ctx);
3731
}
38-
Err(already_finalized_error())
32+
Err(exceptions::already_finalized_error())
3933
}
4034
}
4135

src/rust/src/backend/hmac.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
// for complete details.
44

5-
use crate::backend::hashes::{already_finalized_error, message_digest_from_algorithm};
5+
use crate::backend::hashes::message_digest_from_algorithm;
66
use crate::buf::CffiBuf;
77
use crate::error::{CryptographyError, CryptographyResult};
88
use crate::exceptions;
@@ -47,14 +47,14 @@ impl Hmac {
4747
if let Some(ctx) = self.ctx.as_ref() {
4848
return Ok(ctx);
4949
};
50-
Err(already_finalized_error())
50+
Err(exceptions::already_finalized_error())
5151
}
5252

5353
fn get_mut_ctx(&mut self) -> CryptographyResult<&mut cryptography_openssl::hmac::Hmac> {
5454
if let Some(ctx) = self.ctx.as_mut() {
5555
return Ok(ctx);
5656
}
57-
Err(already_finalized_error())
57+
Err(exceptions::already_finalized_error())
5858
}
5959
}
6060

src/rust/src/backend/poly1305.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
// for complete details.
44

5-
use crate::backend::hashes::already_finalized_error;
65
use crate::buf::CffiBuf;
76
use crate::error::{CryptographyError, CryptographyResult};
87
use crate::exceptions;
@@ -136,7 +135,9 @@ impl Poly1305 {
136135
fn update(&mut self, data: CffiBuf<'_>) -> CryptographyResult<()> {
137136
self.inner
138137
.as_mut()
139-
.map_or(Err(already_finalized_error()), |b| b.update(data))
138+
.map_or(Err(exceptions::already_finalized_error()), |b| {
139+
b.update(data)
140+
})
140141
}
141142

142143
fn finalize<'p>(
@@ -146,7 +147,9 @@ impl Poly1305 {
146147
let res = self
147148
.inner
148149
.as_mut()
149-
.map_or(Err(already_finalized_error()), |b| b.finalize(py));
150+
.map_or(Err(exceptions::already_finalized_error()), |b| {
151+
b.finalize(py)
152+
});
150153
self.inner = None;
151154

152155
res

src/rust/src/exceptions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
// for complete details.
44

5+
use crate::error::CryptographyError;
6+
57
#[pyo3::pyclass(
68
frozen,
79
eq,
@@ -37,6 +39,10 @@ pyo3::import_exception_bound!(cryptography.x509, DuplicateExtension);
3739
pyo3::import_exception_bound!(cryptography.x509, UnsupportedGeneralNameType);
3840
pyo3::import_exception_bound!(cryptography.x509, InvalidVersion);
3941

42+
pub(crate) fn already_finalized_error() -> CryptographyError {
43+
CryptographyError::from(AlreadyFinalized::new_err("Context was already finalized."))
44+
}
45+
4046
#[pyo3::pymodule]
4147
pub(crate) mod exceptions {
4248
#[pymodule_export]

src/rust/src/padding.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// for complete details.
44

55
use crate::buf::CffiBuf;
6-
use crate::error::{CryptographyError, CryptographyResult};
6+
use crate::error::CryptographyResult;
77
use crate::exceptions;
88

99
/// Returns the value of the input with the most-significant-bit copied to all
@@ -92,9 +92,7 @@ impl PKCS7PaddingContext {
9292
*v += buf.as_bytes().len();
9393
Ok(buf.into_pyobj())
9494
}
95-
None => Err(CryptographyError::from(
96-
exceptions::AlreadyFinalized::new_err("Context was already finalized."),
97-
)),
95+
None => Err(exceptions::already_finalized_error()),
9896
}
9997
}
10098

@@ -108,9 +106,7 @@ impl PKCS7PaddingContext {
108106
let pad = vec![pad_size as u8; pad_size];
109107
Ok(pyo3::types::PyBytes::new_bound(py, &pad))
110108
}
111-
None => Err(CryptographyError::from(
112-
exceptions::AlreadyFinalized::new_err("Context was already finalized."),
113-
)),
109+
None => Err(exceptions::already_finalized_error()),
114110
}
115111
}
116112
}

0 commit comments

Comments
 (0)