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 pub key and seed to api. Tests fix #189

Merged
merged 4 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions native/src/oberon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ use rand::prelude::*;
use std::convert::TryInto;

impl crate::Oberon {
pub fn key<'a>(_request: &CreateOberonKeyRequest) -> Result<CreateOberonKeyReply, Error<'a>> {
let rng = thread_rng();

let sk = oberon::SecretKey::new(rng);
pub fn key<'a>(request: &CreateOberonKeyRequest) -> Result<CreateOberonKeyReply, Error<'a>> {
let sk = if request.seed.len() == 0 {
let rng = thread_rng();
oberon::SecretKey::new(rng)
} else {
oberon::SecretKey::hash(&request.seed)
};
let pk = oberon::PublicKey::from(&sk);

Ok(CreateOberonKeyReply { key: sk.to_bytes().to_vec() })
Ok(CreateOberonKeyReply {
sk: sk.to_bytes().to_vec(),
pk: pk.to_bytes().to_vec(),
})
}

pub fn token<'a>(request: &CreateOberonTokenRequest) -> Result<CreateOberonTokenReply, Error<'a>> {
Expand Down
10 changes: 8 additions & 2 deletions native/src/proto/okapi_security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
#[derive(::serde::Serialize, ::serde::Deserialize)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CreateOberonKeyRequest {
/// optional seed to generate deterministic keys
#[prost(bytes="vec", tag="1")]
pub seed: ::prost::alloc::vec::Vec<u8>,
}
/// Contains the oberon secret key bytes
#[derive(::serde::Serialize, ::serde::Deserialize)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CreateOberonKeyReply {
/// raw key bytes
/// raw secret key bytes
#[prost(bytes="vec", tag="2")]
pub key: ::prost::alloc::vec::Vec<u8>,
pub sk: ::prost::alloc::vec::Vec<u8>,
/// raw public key bytes
#[prost(bytes="vec", tag="3")]
pub pk: ::prost::alloc::vec::Vec<u8>,
}
/// Create a new oberon token
#[derive(::serde::Serialize, ::serde::Deserialize)]
Expand Down
4 changes: 2 additions & 2 deletions native/src/tests/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use fluid::prelude::*;

#[theory]
#[case(KeyType::X25519, 32)]
#[case(KeyType::P256, 65)]
#[case(KeyType::P256, 33)]
#[case(KeyType::Ed25519, 32)]
fn test_generate_key_no_seed(key_type: KeyType, public_key_size: usize) {
let request = GenerateKeyRequest {
Expand All @@ -30,7 +30,7 @@ fn test_generate_key_no_seed(key_type: KeyType, public_key_size: usize) {
#[test]
fn test_generate_key_no_seed_1() {
let key_type = KeyType::P256;
let public_key_size: usize = 65;
let public_key_size: usize = 33;

let request = GenerateKeyRequest {
seed: vec![],
Expand Down
12 changes: 11 additions & 1 deletion native/src/tests/oberon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,17 @@ fn test_unblind_token() {

#[test]
fn test_create_key() {
let req = CreateOberonKeyRequest {};
let req = CreateOberonKeyRequest { seed: vec![] };

crate::Oberon::key(&req).unwrap();
}

#[test]
fn test_create_key_with_seed() {
let req = CreateOberonKeyRequest {
seed: b"super secret seed".to_vec(),
};

let result = crate::Oberon::key(&req);
assert!(result.is_ok())
}
4 changes: 3 additions & 1 deletion proto/security.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ option java_package = "trinsic.okapi";

// Create an Oberon Compatible Secret Key
message CreateOberonKeyRequest {
bytes seed = 1; // optional seed to generate deterministic keys
}

// Contains the oberon secret key bytes
message CreateOberonKeyReply {
bytes key = 2; // raw key bytes
bytes sk = 2; // raw secret key bytes
bytes pk = 3; // raw public key bytes
}

// Create a new oberon token
Expand Down