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

Rename KeyType -> KeyAlgorithm #34

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions crates/crypto/src/key/key.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ssi_jwk::JWK;
use ssi_jws::Error as JWSError;

/// Enum defining all supported cryptographic key types.
pub enum KeyType {
/// Enum defining all supported cryptographic algorithms for a [`Key`].
pub enum KeyAlgorithm {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The values for this enum appear to me to be Elliptic Curves (as defined in https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2.1.1 and who's possible values are in the registry located on https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve). How about we call this Curve?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supporting RSA 😄

Copy link
Contributor

@andresuribe87 andresuribe87 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious what thoughts are here, specially from @mistermoe and @decentralgabe

Secp256k1,
Secp256r1,
Ed25519,
Expand Down
6 changes: 3 additions & 3 deletions crates/crypto/src/key_manager/key_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::key::{KeyError, KeyType, PublicKey};
use crate::key::{KeyAlgorithm, KeyError, PublicKey};
use crate::key_manager::key_store::KeyStoreError;
use ssi_jwk::Error as JWKError;

Expand All @@ -21,10 +21,10 @@ pub enum KeyManagerError {
/// Systems (KMS), such as AWS KMS, Google Cloud KMD, Hardware Security Modules (HSM), or simple
/// in-memory storage, each adhering to the same consistent API for usage within applications.
pub trait KeyManager: Send + Sync {
/// Generates and securely stores a private key based on the provided `key_type`,
/// Generates and securely stores a private key based on the provided `key_algorithm`,
/// returning a unique alias that can be utilized to reference the generated key for future
/// operations.
fn generate_private_key(&self, key_type: KeyType) -> Result<String, KeyManagerError>;
fn generate_private_key(&self, key_algorithm: KeyAlgorithm) -> Result<String, KeyManagerError>;

/// Returns the public key associated with the provided `key_alias`, if one exists.
fn get_public_key(&self, key_alias: &str) -> Result<Option<PublicKey>, KeyManagerError>;
Expand Down
30 changes: 18 additions & 12 deletions crates/crypto/src/key_manager/local_key_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::key::{KeyType, PrivateKey, PublicKey};
use crate::key::{KeyAlgorithm, PrivateKey, PublicKey};
use crate::key_manager::key_store::{InMemoryKeyStore, KeyStore};
use crate::key_manager::{KeyManager, KeyManagerError};
use ssi_jwk::JWK;
Expand Down Expand Up @@ -26,11 +26,11 @@ impl LocalKeyManager {
}

impl KeyManager for LocalKeyManager {
fn generate_private_key(&self, key_type: KeyType) -> Result<String, KeyManagerError> {
let jwk = match key_type {
KeyType::Secp256k1 => JWK::generate_secp256k1(),
KeyType::Secp256r1 => JWK::generate_p256(),
KeyType::Ed25519 => JWK::generate_ed25519(),
fn generate_private_key(&self, key_algorithm: KeyAlgorithm) -> Result<String, KeyManagerError> {
let jwk = match key_algorithm {
KeyAlgorithm::Secp256k1 => JWK::generate_secp256k1(),
KeyAlgorithm::Secp256r1 => JWK::generate_p256(),
KeyAlgorithm::Ed25519 => JWK::generate_ed25519(),
}?;

let private_key = PrivateKey(jwk);
Expand Down Expand Up @@ -75,23 +75,25 @@ mod tests {
let key_manager = LocalKeyManager::new_in_memory();

key_manager
.generate_private_key(KeyType::Ed25519)
.generate_private_key(KeyAlgorithm::Ed25519)
.expect("Failed to generate Ed25519 key");

key_manager
.generate_private_key(KeyType::Secp256k1)
.generate_private_key(KeyAlgorithm::Secp256k1)
.expect("Failed to generate secp256k1 key");

key_manager
.generate_private_key(KeyType::Secp256r1)
.generate_private_key(KeyAlgorithm::Secp256r1)
.expect("Failed to generate secp256r1 key");
}

#[test]
fn test_get_public_key() {
let key_manager = LocalKeyManager::new_in_memory();

let key_alias = key_manager.generate_private_key(KeyType::Ed25519).unwrap();
let key_alias = key_manager
.generate_private_key(KeyAlgorithm::Ed25519)
.unwrap();

key_manager
.get_public_key(&key_alias)
Expand All @@ -102,7 +104,9 @@ mod tests {
#[test]
fn test_sign() {
let key_manager = LocalKeyManager::new_in_memory();
let key_alias = key_manager.generate_private_key(KeyType::Ed25519).unwrap();
let key_alias = key_manager
.generate_private_key(KeyAlgorithm::Ed25519)
.unwrap();

// Sign a payload
let payload: &[u8] = b"hello world";
Expand All @@ -117,7 +121,9 @@ mod tests {
#[test]
fn test_alias() {
let key_manager = LocalKeyManager::new_in_memory();
let key_alias = key_manager.generate_private_key(KeyType::Ed25519).unwrap();
let key_alias = key_manager
.generate_private_key(KeyAlgorithm::Ed25519)
.unwrap();

let public_key = key_manager.get_public_key(&key_alias).unwrap().unwrap();
let alias = key_manager.alias(&public_key).unwrap();
Expand Down