Skip to content

Commit

Permalink
Make SecretKey::try_from_be_bytes() take just a slice reference
Browse files Browse the repository at this point in the history
Let the caller worry about wrapping it in a zeroized struct.
  • Loading branch information
fjarri committed Jan 28, 2024
1 parent b3eaa7a commit 8d9e0ac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.12.0] - In development

### Changed

- `SecretKey::try_from_be_bytes()` takes just a slice reference instead of a `SecretBox`. ([#134])


[#134]: https://github.com/nucypher/rust-umbral/pull/134


## [0.11.0] - 2023-08-01

### Added
Expand Down
8 changes: 2 additions & 6 deletions umbral-pre/src/bindings_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use pyo3::wrap_pyfunction;
use sha2::{digest::Update, Digest, Sha256};

use crate as umbral_pre;
use crate::{curve::ScalarSize, DefaultDeserialize, DefaultSerialize, SecretBox};
use crate::{DefaultDeserialize, DefaultSerialize};

fn map_py_value_err<T: fmt::Display>(err: T) -> PyErr {
PyValueError::new_err(format!("{err}"))
Expand Down Expand Up @@ -101,11 +101,7 @@ impl SecretKey {

#[staticmethod]
pub fn from_be_bytes(data: &[u8]) -> PyResult<Self> {
let arr = SecretBox::new(
GenericArray::<u8, ScalarSize>::from_exact_iter(data.iter().cloned())
.ok_or_else(|| map_py_value_err("Invalid length of a curve scalar"))?,
);
umbral_pre::SecretKey::try_from_be_bytes(&arr)
umbral_pre::SecretKey::try_from_be_bytes(data)
.map_err(map_py_value_err)
.map(Self::from)
}
Expand Down
9 changes: 2 additions & 7 deletions umbral-pre/src/bindings_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;

use generic_array::GenericArray;
use js_sys::{Error, Uint8Array};
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};
use wasm_bindgen::JsCast;
use wasm_bindgen_derive::TryFromJsValue;

use crate as umbral_pre;
use crate::{curve::ScalarSize, DefaultDeserialize, DefaultSerialize, SecretBox};
use crate::{DefaultDeserialize, DefaultSerialize};

#[wasm_bindgen]
extern "C" {
Expand Down Expand Up @@ -104,11 +103,7 @@ impl SecretKey {

#[wasm_bindgen(js_name = fromBEBytes)]
pub fn from_be_bytes(data: &[u8]) -> Result<SecretKey, Error> {
let arr = SecretBox::new(
GenericArray::<u8, ScalarSize>::from_exact_iter(data.iter().cloned())
.ok_or_else(|| map_js_err("Invalid length of a curve scalar"))?,
);
umbral_pre::SecretKey::try_from_be_bytes(&arr)
umbral_pre::SecretKey::try_from_be_bytes(data)
.map(Self)
.map_err(map_js_err)
}
Expand Down
10 changes: 6 additions & 4 deletions umbral-pre/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,12 @@ impl SecretKey {
}

/// Deserializes the secret key from a scalar in the big-endian representation.
pub fn try_from_be_bytes(
bytes: &SecretBox<GenericArray<u8, ScalarSize>>,
) -> Result<Self, String> {
BackendSecretKey::<CurveType>::from_bytes(bytes.as_secret())
pub fn try_from_be_bytes(bytes: &[u8]) -> Result<Self, String> {
let arr = SecretBox::new(
GenericArray::<u8, ScalarSize>::from_exact_iter(bytes.iter().cloned())
.ok_or("Invalid length of a curve scalar")?,
);
BackendSecretKey::<CurveType>::from_bytes(arr.as_secret())
.map(Self::new)
.map_err(|err| format!("{err}"))
}
Expand Down

0 comments on commit 8d9e0ac

Please sign in to comment.