Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cx_hkdf functions #43

Open
ndcroos opened this issue Sep 14, 2022 · 2 comments
Open

Add support for cx_hkdf functions #43

ndcroos opened this issue Sep 14, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@ndcroos
Copy link

ndcroos commented Sep 14, 2022

Bindings for the hkdf methods defined here are currently missing in the Rust SDK.
I've written the following bindings. Does this seem good and should I submit this as a pull request (for example in a separate file named 'hkdf.rs')?

use nanos_sdk::bindings::*;

extern "C" {
    pub fn cx_hkdf_extract(hash_id: cx_md_t, ikm: &[u8], ikm_len: u32, 
        salt: &[u8], salt_len: u32, prk: &[u8]);

    pub fn cx_hkdf_expand(hash_id: cx_md_t, prk: &[u8], prk_len: u32, info: &[u8], 
        info_len: u32, okm: &[u8], okm_len: u32);
}

/// Wrapper for cx_hkdf_extract
/// 
/// Extract entropy 
/// 
/// In: 
/// ikm: input key material (acting as data)
/// salt: salt acting as a key
/// prk: pseudorandom key
pub fn hkdf_extract(ikm: &[u8], salt: &mut [u8], prk: &mut [u8]) {
    unsafe {
        cx_hkdf_extract(
            CX_SHA256,
            ikm.as_ptr(),
            ikm.len() as u32,
            salt.as_mut_ptr(),
            salt.len() as u32,
            prk.as_mut_ptr()
        );
    }
}

/// Wrapper for cx_hkdf_expand
/// 
/// Expand generated output of an already reasonably random input 
/// 
/// In: 
/// prk: pseudorandom key
/// info: 
/// okm: output key material
pub fn hkdf_expand(prk: &[u8], info: &[u8], okm: &mut [u8]) {
    unsafe {
        cx_hkdf_expand(
            CX_SHA256,
            prk.as_ptr(),
            prk.len() as u32,
            info.as_mut_ptr(),
            info.len() as u32,
            okm.as_mut_ptr(),
            okm.len() as u32
        );
    }
}
@ndcroos ndcroos changed the title Add support for hkdf functions. Add support for cx_hkdf functions. Sep 14, 2022
@ndcroos ndcroos changed the title Add support for cx_hkdf functions. Add support for cx_hkdf functions Sep 14, 2022
@yhql yhql added the enhancement New feature or request label Jan 20, 2023
@yeti-g
Copy link

yeti-g commented Feb 27, 2023

@ndcroos Nice! PR would be great.

@yhql
Copy link
Contributor

yhql commented Aug 8, 2023

Hi, thanks for providing code!

These bindings would be invalid however, as the extern C part should use only raw pointers, and use mut when necessary:
salt: &[u8] should be alt: *mut u8 for example.
(read-only should be *const u8)

This "extern "C"" prototype could be generated automatically by adding libcxng/src/cx_hkdf.c to the list of compiled sources in the build.rs script, and libcxng/src/cx_hkdf.h to the bindgen headers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants