Skip to content

Commit 2c9e705

Browse files
authored
Use the newer openssl cipher module (#10085)
It supports SM4-GCM
1 parent bee00f9 commit 2c9e705

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/rust/cryptography-openssl/src/cmac.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ unsafe impl Sync for Cmac {}
2121
unsafe impl Send for Cmac {}
2222

2323
impl Cmac {
24-
pub fn new(key: &[u8], cipher: &openssl::symm::Cipher) -> OpenSSLResult<Cmac> {
24+
pub fn new(key: &[u8], cipher: &openssl::cipher::CipherRef) -> OpenSSLResult<Cmac> {
2525
// SAFETY: All FFI conditions are handled.
2626
unsafe {
2727
let ctx = Cmac::from_ptr(cvt_p(ffi::CMAC_CTX_new())?);

src/rust/src/backend/cipher_registry.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use crate::error::CryptographyResult;
66
use crate::types;
7-
use openssl::symm::Cipher;
7+
use openssl::cipher::Cipher;
88
use std::collections::HashMap;
99

1010
struct RegistryKey {
@@ -54,7 +54,7 @@ impl std::hash::Hash for RegistryKey {
5454

5555
struct RegisteryBuilder<'p> {
5656
py: pyo3::Python<'p>,
57-
m: HashMap<RegistryKey, openssl::symm::Cipher>,
57+
m: HashMap<RegistryKey, &'static openssl::cipher::CipherRef>,
5858
}
5959

6060
impl<'p> RegisteryBuilder<'p> {
@@ -70,7 +70,7 @@ impl<'p> RegisteryBuilder<'p> {
7070
algorithm: &pyo3::PyAny,
7171
mode: &pyo3::PyAny,
7272
key_size: Option<u16>,
73-
cipher: openssl::symm::Cipher,
73+
cipher: &'static openssl::cipher::CipherRef,
7474
) -> CryptographyResult<()> {
7575
self.m.insert(
7676
RegistryKey::new(self.py, algorithm.into(), mode.into(), key_size)?,
@@ -80,16 +80,17 @@ impl<'p> RegisteryBuilder<'p> {
8080
Ok(())
8181
}
8282

83-
fn build(self) -> HashMap<RegistryKey, openssl::symm::Cipher> {
83+
fn build(self) -> HashMap<RegistryKey, &'static openssl::cipher::CipherRef> {
8484
self.m
8585
}
8686
}
8787

8888
fn get_cipher_registry(
8989
py: pyo3::Python<'_>,
90-
) -> CryptographyResult<&HashMap<RegistryKey, openssl::symm::Cipher>> {
91-
static REGISTRY: pyo3::sync::GILOnceCell<HashMap<RegistryKey, openssl::symm::Cipher>> =
92-
pyo3::sync::GILOnceCell::new();
90+
) -> CryptographyResult<&HashMap<RegistryKey, &'static openssl::cipher::CipherRef>> {
91+
static REGISTRY: pyo3::sync::GILOnceCell<
92+
HashMap<RegistryKey, &'static openssl::cipher::CipherRef>,
93+
> = pyo3::sync::GILOnceCell::new();
9394

9495
REGISTRY.get_or_try_init(py, || {
9596
let mut m = RegisteryBuilder::new(py);
@@ -123,11 +124,11 @@ fn get_cipher_registry(
123124
m.add(triple_des, cbc, Some(192), Cipher::des_ede3_cbc())?;
124125

125126
#[cfg(not(CRYPTOGRAPHY_OSSLCONF = "OPENSSL_NO_CAMELLIA"))]
126-
m.add(camellia, cbc, Some(128), Cipher::camellia_128_cbc())?;
127+
m.add(camellia, cbc, Some(128), Cipher::camellia128_cbc())?;
127128
#[cfg(not(CRYPTOGRAPHY_OSSLCONF = "OPENSSL_NO_CAMELLIA"))]
128-
m.add(camellia, cbc, Some(192), Cipher::camellia_192_cbc())?;
129+
m.add(camellia, cbc, Some(192), Cipher::camellia192_cbc())?;
129130
#[cfg(not(CRYPTOGRAPHY_OSSLCONF = "OPENSSL_NO_CAMELLIA"))]
130-
m.add(camellia, cbc, Some(256), Cipher::camellia_256_cbc())?;
131+
m.add(camellia, cbc, Some(256), Cipher::camellia256_cbc())?;
131132

132133
#[cfg(not(CRYPTOGRAPHY_OSSLCONF = "OPENSSL_NO_SM4"))]
133134
m.add(sm4, cbc, Some(128), Cipher::sm4_cbc())?;
@@ -148,11 +149,11 @@ fn get_cipher_registry(
148149
})
149150
}
150151

151-
pub(crate) fn get_cipher(
152+
pub(crate) fn get_cipher<'a>(
152153
py: pyo3::Python<'_>,
153154
algorithm: &pyo3::PyAny,
154155
mode_cls: &pyo3::PyAny,
155-
) -> CryptographyResult<Option<openssl::symm::Cipher>> {
156+
) -> CryptographyResult<Option<&'a openssl::cipher::CipherRef>> {
156157
let registry = get_cipher_registry(py)?;
157158

158159
let key_size = algorithm

src/rust/src/backend/cmac.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Cmac {
6161
let key = algorithm
6262
.getattr(pyo3::intern!(py, "key"))?
6363
.extract::<CffiBuf<'_>>()?;
64-
let ctx = cryptography_openssl::cmac::Cmac::new(key.as_bytes(), &cipher)?;
64+
let ctx = cryptography_openssl::cmac::Cmac::new(key.as_bytes(), cipher)?;
6565
Ok(Cmac { ctx: Some(ctx) })
6666
}
6767

0 commit comments

Comments
 (0)