aboutsummaryrefslogtreecommitdiff
path: root/openssl/src
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2017-12-03 15:08:51 -0800
committerGitHub <[email protected]>2017-12-03 15:08:51 -0800
commit13a13727e8c52d3a7e952ac7ac34a3b2ebb3420c (patch)
treeada8963165768adca5c3f4f8cff56f3a0550b93f /openssl/src
parentMerge pull request #784 from P-E-Meunier/master (diff)
parentSimplifying finish_into (diff)
downloadrust-openssl-13a13727e8c52d3a7e952ac7ac34a3b2ebb3420c.tar.xz
rust-openssl-13a13727e8c52d3a7e952ac7ac34a3b2ebb3420c.zip
Merge pull request #785 from P-E-Meunier/split-signer-finish
Splitting the sign::Signer::finish function, to avoid allocations
Diffstat (limited to 'openssl/src')
-rw-r--r--openssl/src/sign.rs32
1 files changed, 27 insertions, 5 deletions
diff --git a/openssl/src/sign.rs b/openssl/src/sign.rs
index f8735526..c074de0b 100644
--- a/openssl/src/sign.rs
+++ b/openssl/src/sign.rs
@@ -139,7 +139,8 @@ impl<'a> Signer<'a> {
}
}
- pub fn finish(&self) -> Result<Vec<u8>, ErrorStack> {
+ /// Computes an upper bound on the signature length.
+ pub fn finish_len(&self) -> Result<usize, ErrorStack> {
unsafe {
let mut len = 0;
cvt(ffi::EVP_DigestSignFinal(
@@ -147,17 +148,38 @@ impl<'a> Signer<'a> {
ptr::null_mut(),
&mut len,
))?;
- let mut buf = vec![0; len];
+ Ok(len)
+ }
+ }
+
+ /// Outputs the signature into the provided buffer, returning the
+ /// length of that buffer.
+ ///
+ /// This method will fail if the buffer is not large enough for
+ /// the signature, one can use `finish_len` to get an upper bound
+ /// on the required size.
+ pub fn finish_into(&self, buf: &mut [u8]) -> Result<usize, ErrorStack> {
+ unsafe {
+ let mut len = buf.len();
cvt(ffi::EVP_DigestSignFinal(
self.md_ctx,
buf.as_mut_ptr() as *mut _,
&mut len,
))?;
- // The advertised length is not always equal to the real length for things like DSA
- buf.truncate(len);
- Ok(buf)
+ Ok(len)
}
}
+
+ /// Combines `self.finish_len()` and `self.finish_into()`,
+ /// allocating a vector of the correct size for the signature.
+ pub fn finish(&self) -> Result<Vec<u8>, ErrorStack> {
+ let mut buf = vec![0; self.finish_len()?];
+ let len = self.finish_into(&mut buf)?;
+ // The advertised length is not always equal to the real
+ // length for things like DSA
+ buf.truncate(len);
+ Ok(buf)
+ }
}
impl<'a> Write for Signer<'a> {