nss/
secport.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use crate::error::*;
6use crate::util::assert_nss_initialized;
7use std::os::raw::c_void;
8
9pub fn secure_memcmp(a: &[u8], b: &[u8]) -> Result<bool> {
10    assert_nss_initialized();
11    // NSS_SecureMemcmp will compare N elements fron our slices,
12    // so make sure they are the same length first.
13    if a.len() != b.len() {
14        return Ok(false);
15    }
16    let result = unsafe {
17        nss_sys::NSS_SecureMemcmp(
18            a.as_ptr() as *const c_void,
19            b.as_ptr() as *const c_void,
20            a.len(),
21        )
22    };
23    Ok(result == 0)
24}