diff options
| author | Chris Cole <[email protected]> | 2014-11-29 19:47:09 -0500 |
|---|---|---|
| committer | Chris Cole <[email protected]> | 2014-11-29 19:47:09 -0500 |
| commit | 5f76f1cb62c70af4bbf064ea5c27c69544f04cea (patch) | |
| tree | 6c90d43f6bfdf1b2b83094c1bfc559bfbdecf554 /src/crypto/memcmp.rs | |
| parent | Added mod_mul. (diff) | |
| parent | Make SslStream Cloneable (diff) | |
| download | rust-openssl-5f76f1cb62c70af4bbf064ea5c27c69544f04cea.tar.xz rust-openssl-5f76f1cb62c70af4bbf064ea5c27c69544f04cea.zip | |
Merge remote-tracking branch 'upstream/master'
Conflicts:
src/bn/mod.rs
Diffstat (limited to 'src/crypto/memcmp.rs')
| -rw-r--r-- | src/crypto/memcmp.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/crypto/memcmp.rs b/src/crypto/memcmp.rs new file mode 100644 index 00000000..299effa9 --- /dev/null +++ b/src/crypto/memcmp.rs @@ -0,0 +1,39 @@ +use libc::size_t; +use ffi; + +/// Returns `true` iff `a` and `b` contain the same bytes. +/// +/// This operation takes an amount of time dependent on the length of the two +/// arrays given, but is independent of the contents of a and b. +/// +/// # Failure +/// +/// This function will panic the current task if `a` and `b` do not have the same +/// length. +pub fn eq(a: &[u8], b: &[u8]) -> bool { + assert!(a.len() == b.len()); + let ret = unsafe { + ffi::CRYPTO_memcmp(a.as_ptr() as *const _, + b.as_ptr() as *const _, + a.len() as size_t) + }; + ret == 0 +} + +#[cfg(test)] +mod tests { + use super::eq; + + #[test] + fn test_eq() { + assert!(eq(&[], &[])); + assert!(eq(&[1], &[1])); + assert!(!eq(&[1, 2, 3], &[1, 2, 4])); + } + + #[test] + #[should_fail] + fn test_diff_lens() { + eq(&[], &[1]); + } +} |