Skip to content

Commit

Permalink
Expose num parameter of the cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-k committed Nov 14, 2022
1 parent b30313a commit 32e63c8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions openssl-sys/src/evp.rs
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions openssl-sys/src/handwritten/evp.rs
Expand Up @@ -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" {
Expand All @@ -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;
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions openssl/src/cipher_ctx.rs
Expand Up @@ -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.
Expand Down

0 comments on commit 32e63c8

Please sign in to comment.