From 0106165088d13635cd2ead04b1d20375546e376a Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Thu, 10 Nov 2022 10:33:31 +0100 Subject: [PATCH] Expose `num` parameter of the cipher --- openssl-sys/src/evp.rs | 5 +++++ openssl-sys/src/handwritten/evp.rs | 3 +++ openssl/src/cipher_ctx.rs | 16 ++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/openssl-sys/src/evp.rs b/openssl-sys/src/evp.rs index 1e1a4dd93d..fc3003f7bd 100644 --- a/openssl-sys/src/evp.rs +++ b/openssl-sys/src/evp.rs @@ -97,6 +97,11 @@ cfg_if! { pub unsafe fn EVP_CIPHER_CTX_iv_length(ctx: *const EVP_CIPHER_CTX) -> c_int { EVP_CIPHER_CTX_get_iv_length(ctx) } + + #[inline] + pub unsafe fn EVP_CIPHER_CTX_num(ctx: *const EVP_CIPHER_CTX) -> c_int { + EVP_CIPHER_CTX_get_num(ctx) + } } else { pub unsafe fn EVP_MD_CTX_size(ctx: *const EVP_MD_CTX) -> c_int { EVP_MD_size(EVP_MD_CTX_md(ctx)) diff --git a/openssl-sys/src/handwritten/evp.rs b/openssl-sys/src/handwritten/evp.rs index ffb0a0819d..a85d628ade 100644 --- a/openssl-sys/src/handwritten/evp.rs +++ b/openssl-sys/src/handwritten/evp.rs @@ -26,6 +26,7 @@ cfg_if! { pub fn EVP_CIPHER_CTX_get_key_length(ctx: *const EVP_CIPHER_CTX) -> c_int; pub fn EVP_CIPHER_CTX_get_iv_length(ctx: *const EVP_CIPHER_CTX) -> c_int; pub fn EVP_CIPHER_CTX_get_tag_length(ctx: *const EVP_CIPHER_CTX) -> c_int; + pub fn EVP_CIPHER_CTX_get_num(ctx: *const EVP_CIPHER_CTX) -> c_int; } } else { extern "C" { @@ -44,6 +45,8 @@ cfg_if! { pub fn EVP_CIPHER_CTX_block_size(ctx: *const EVP_CIPHER_CTX) -> c_int; pub fn EVP_CIPHER_CTX_key_length(ctx: *const EVP_CIPHER_CTX) -> c_int; pub fn EVP_CIPHER_CTX_iv_length(ctx: *const EVP_CIPHER_CTX) -> c_int; + #[cfg(ossl110)] + pub fn EVP_CIPHER_CTX_num(ctx: *const EVP_CIPHER_CTX) -> c_int; } } } diff --git a/openssl/src/cipher_ctx.rs b/openssl/src/cipher_ctx.rs index 8e017115b1..a4d1c461c5 100644 --- a/openssl/src/cipher_ctx.rs +++ b/openssl/src/cipher_ctx.rs @@ -363,6 +363,22 @@ impl CipherCtxRef { unsafe { ffi::EVP_CIPHER_CTX_iv_length(self.as_ptr()) as usize } } + /// Returns the `num` parameter of the cipher. + /// + /// Built-in ciphers typically use this to track how much of the + /// current underlying block has been "used" already. + /// + /// # Panics + /// + /// Panics if the context has not been initialized with a cipher. + #[corresponds(EVP_CIPHER_CTX_num)] + #[cfg(ossl110)] + pub fn num(&self) -> usize { + self.assert_cipher(); + + unsafe { ffi::EVP_CIPHER_CTX_num(self.as_ptr()) as usize } + } + /// Sets the length of the IV expected by this context. /// /// Only some ciphers support configurable IV lengths.