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

SecretKey::to_bytes shouldn't be unsafe #8

Open
sosthene-nitrokey opened this issue Sep 14, 2022 · 1 comment
Open

SecretKey::to_bytes shouldn't be unsafe #8

sosthene-nitrokey opened this issue Sep 14, 2022 · 1 comment

Comments

@sosthene-nitrokey
Copy link
Contributor

unsafe refers to memory safety, not generally "dangerous" APIs. I don't think it should be use for this kind of use cases. For example, neither the secrecy or untrusted use unsafe this way.

To warn against exporting the secret key, I'd recommend removing unsafe and renaming it to subtle_to_bytes or something similar.

@szszszsz
Copy link

#[allow(unused_unsafe)]
/// Convert endianness to obtain the big-endian representation of the secret scalar as 32 bytes.
///
/// "unsafe" because the caller is responsible for keeping the value secret.
pub unsafe fn to_bytes(&self) -> [u8; 32] {
let mut big_endian = [0u8; 32];
unsafe { p256_cortex_m4_sys::p256_convert_endianness(
&mut big_endian[0] as *mut u8 as *mut _,
&self.0[0] as *const u32 as *const _,
32,
) };
big_endian
}

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

No branches or pull requests

2 participants