Commit 6ed35b28 authored by Paul Kehrer's avatar Paul Kehrer
Browse files

review feedback, support OPENSSL_NO_ARGON2

parent 2d340cb6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@
use std::env;

fn main() {
    println!("cargo:rustc-check-cfg=cfg(osslconf, values(\"OPENSSL_NO_OCB\", \"OPENSSL_NO_SM4\", \"OPENSSL_NO_SEED\", \"OPENSSL_NO_CHACHA\", \"OPENSSL_NO_CAST\", \"OPENSSL_NO_IDEA\", \"OPENSSL_NO_CAMELLIA\", \"OPENSSL_NO_RC4\", \"OPENSSL_NO_BF\", \"OPENSSL_NO_PSK\", \"OPENSSL_NO_DEPRECATED_3_0\", \"OPENSSL_NO_SCRYPT\", \"OPENSSL_NO_SM3\", \"OPENSSL_NO_RMD160\", \"OPENSSL_NO_EC2M\", \"OPENSSL_NO_OCSP\", \"OPENSSL_NO_CMS\", \"OPENSSL_NO_EC\"))");
    println!("cargo:rustc-check-cfg=cfg(osslconf, values(\"OPENSSL_NO_OCB\", \"OPENSSL_NO_SM4\", \"OPENSSL_NO_SEED\", \"OPENSSL_NO_CHACHA\", \"OPENSSL_NO_CAST\", \"OPENSSL_NO_IDEA\", \"OPENSSL_NO_CAMELLIA\", \"OPENSSL_NO_RC4\", \"OPENSSL_NO_BF\", \"OPENSSL_NO_PSK\", \"OPENSSL_NO_DEPRECATED_3_0\", \"OPENSSL_NO_SCRYPT\", \"OPENSSL_NO_SM3\", \"OPENSSL_NO_RMD160\", \"OPENSSL_NO_EC2M\", \"OPENSSL_NO_OCSP\", \"OPENSSL_NO_CMS\", \"OPENSSL_NO_EC\", \"OPENSSL_NO_ARGON2\"))");

    println!("cargo:rustc-check-cfg=cfg(libressl)");
    println!("cargo:rustc-check-cfg=cfg(boringssl)");
+44 −26
Original line number Diff line number Diff line
use libc::c_void;
use std::ffi::CStr;
use std::ffi::c_void;
use std::ptr;

use crate::error::ErrorStack;
use crate::{cvt, cvt_p};

struct EvpKdf {
    kdf: *mut ffi::EVP_KDF,
}

impl Drop for EvpKdf {
    fn drop(&mut self) {
        unsafe {
            ffi::EVP_KDF_free(self.kdf);
        }
    }
}

struct EvpKdfCtx {
    ctx: *mut ffi::EVP_KDF_CTX,
}

impl Drop for EvpKdfCtx {
    fn drop(&mut self) {
        unsafe {
            ffi::EVP_KDF_CTX_free(self.ctx);
        }
    }
}

/// Derives a key using the argon2id algorithm.
///
/// Requires OpenSSL 3.2.0 or newer.
@@ -23,33 +46,24 @@ pub fn argon2id(
    // We only support single-threaded operation for now since rust-openssl doesn't
    // bind OSSL_set_max_threads
    assert!(threads == 1);
    let pass_field = CStr::from_bytes_with_nul(b"pass\0").unwrap();
    let salt_field = CStr::from_bytes_with_nul(b"salt\0").unwrap();
    let ad_field = CStr::from_bytes_with_nul(b"ad\0").unwrap();
    let secret_field = CStr::from_bytes_with_nul(b"secret\0").unwrap();
    let iter_field = CStr::from_bytes_with_nul(b"iter\0").unwrap();
    let size_field = CStr::from_bytes_with_nul(b"size\0").unwrap();
    let threads_field = CStr::from_bytes_with_nul(b"threads\0").unwrap();
    let lanes_field = CStr::from_bytes_with_nul(b"lanes\0").unwrap();
    let memcost_field = CStr::from_bytes_with_nul(b"memcost\0").unwrap();
    unsafe {
        ffi::init();
        let mut params = vec![];
        let param_pass = ffi::OSSL_PARAM_construct_octet_string(
            pass_field.as_ptr(),
            b"pass\0".as_ptr() as *const i8,
            pass.as_ptr() as *mut c_void,
            pass.len(),
        );
        params.push(param_pass);
        let param_salt = ffi::OSSL_PARAM_construct_octet_string(
            salt_field.as_ptr(),
            b"salt\0".as_ptr() as *const i8,
            salt.as_ptr() as *mut c_void,
            salt.len(),
        );
        params.push(param_salt);
        if let Some(ad) = ad {
            let param_ad = ffi::OSSL_PARAM_construct_octet_string(
                ad_field.as_ptr(),
                b"ad\0".as_ptr() as *const i8,
                ad.as_ptr() as *mut c_void,
                ad.len(),
            );
@@ -57,35 +71,39 @@ pub fn argon2id(
        }
        if let Some(secret) = secret {
            let param_secret = ffi::OSSL_PARAM_construct_octet_string(
                secret_field.as_ptr(),
                b"secret\0".as_ptr() as *const i8,
                secret.as_ptr() as *mut c_void,
                secret.len(),
            );
            params.push(param_secret);
        }
        let param_threads = ffi::OSSL_PARAM_construct_uint(threads_field.as_ptr(), &mut threads);
        let param_threads =
            ffi::OSSL_PARAM_construct_uint(b"threads\0".as_ptr() as *const i8, &mut threads);
        params.push(param_threads);
        let param_lanes = ffi::OSSL_PARAM_construct_uint(lanes_field.as_ptr(), &mut lanes);
        let param_lanes =
            ffi::OSSL_PARAM_construct_uint(b"lanes\0".as_ptr() as *const i8, &mut lanes);
        params.push(param_lanes);
        let param_memcost = ffi::OSSL_PARAM_construct_uint(memcost_field.as_ptr(), &mut memcost);
        let param_memcost =
            ffi::OSSL_PARAM_construct_uint(b"memcost\0".as_ptr() as *const i8, &mut memcost);
        params.push(param_memcost);
        let param_iter = ffi::OSSL_PARAM_construct_uint(iter_field.as_ptr(), &mut iter);
        let param_iter = ffi::OSSL_PARAM_construct_uint(b"iter\0".as_ptr() as *const i8, &mut iter);
        params.push(param_iter);
        let mut size = out.len() as u32;
        let param_size = ffi::OSSL_PARAM_construct_uint(size_field.as_ptr(), &mut size);
        let param_size = ffi::OSSL_PARAM_construct_uint(b"size\0".as_ptr() as *const i8, &mut size);
        params.push(param_size);
        let param_end = ffi::OSSL_PARAM_construct_end();
        params.push(param_end);

        let argon2id_field = CStr::from_bytes_with_nul(b"ARGON2ID\0").unwrap();
        let argon2 = cvt_p(ffi::EVP_KDF_fetch(
        let argon2_p = cvt_p(ffi::EVP_KDF_fetch(
            ptr::null_mut(),
            argon2id_field.as_ptr(),
            b"ARGON2ID\0".as_ptr() as *const i8,
            ptr::null(),
        ))?; // This needs to be freed
        let ctx = cvt_p(ffi::EVP_KDF_CTX_new(argon2))?; // this also needs to be freed
        ))?;
        let argon2 = EvpKdf { kdf: argon2_p };
        let ctx_p = cvt_p(ffi::EVP_KDF_CTX_new(argon2.kdf))?;
        let ctx = EvpKdfCtx { ctx: ctx_p };
        cvt(ffi::EVP_KDF_derive(
            ctx,
            ctx.ctx,
            out.as_mut_ptr(),
            out.len(),
            params.as_ptr(),
+1 −0
Original line number Diff line number Diff line
@@ -148,6 +148,7 @@ mod bio;
mod util;
pub mod aes;
#[cfg(ossl320)]
#[cfg(all(ossl320, not(osslconf = "OPENSSL_NO_ARGON2")))]
pub mod argon2;
pub mod asn1;
pub mod base64;