Skip to content

Commit

Permalink
Merge pull request #36 from andrewwhitehead/0.2.4-updates
Browse files Browse the repository at this point in the history
Accept EC key type for BLS keys
  • Loading branch information
andrewwhitehead authored Jan 18, 2022
2 parents d7a7d61 + fcda36c commit a92d360
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["askar-bbs", "askar-crypto"]

[package]
name = "aries-askar"
version = "0.2.3"
version = "0.2.4"
authors = ["Hyperledger Aries Contributors <[email protected]>"]
edition = "2018"
description = "Hyperledger Aries Askar secure storage"
Expand Down Expand Up @@ -43,7 +43,7 @@ async-stream = "0.3"
bs58 = "0.4"
chrono = "0.4"
digest = "0.10"
env_logger = { version = "0.7", optional = true }
env_logger = { version = "0.9", optional = true }
ffi-support = { version = "0.4", optional = true }
futures-lite = "1.11"
hex = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion askar-crypto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "askar-crypto"
version = "0.2.3"
version = "0.2.4"
authors = ["Hyperledger Aries Contributors <[email protected]>"]
edition = "2018"
description = "Hyperledger Aries Askar cryptography"
Expand Down
12 changes: 8 additions & 4 deletions askar-crypto/src/alg/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl AnyKey {
}

#[inline]
fn key_type_id(&self) -> TypeId {
pub fn key_type_id(&self) -> TypeId {
self.0.as_any().type_id()
}
}
Expand Down Expand Up @@ -524,11 +524,15 @@ fn from_jwk_any<R: AllocKey>(jwk: JwkParts<'_>) -> Result<R, Error> {
X25519KeyPair::from_jwk_parts(jwk).map(R::alloc_key)
}
#[cfg(feature = "bls")]
("OKP", c) if c == G1::JWK_CURVE => BlsKeyPair::<G1>::from_jwk_parts(jwk).map(R::alloc_key),
("OKP" | "EC", c) if c == G1::JWK_CURVE => {
BlsKeyPair::<G1>::from_jwk_parts(jwk).map(R::alloc_key)
}
#[cfg(feature = "bls")]
("OKP", c) if c == G2::JWK_CURVE => BlsKeyPair::<G2>::from_jwk_parts(jwk).map(R::alloc_key),
("OKP" | "EC", c) if c == G2::JWK_CURVE => {
BlsKeyPair::<G2>::from_jwk_parts(jwk).map(R::alloc_key)
}
#[cfg(feature = "bls")]
("OKP", c) if c == G1G2::JWK_CURVE => {
("OKP" | "EC", c) if c == G1G2::JWK_CURVE => {
BlsKeyPair::<G1G2>::from_jwk_parts(jwk).map(R::alloc_key)
}
#[cfg(feature = "k256")]
Expand Down
28 changes: 27 additions & 1 deletion askar-crypto/src/alg/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ impl<Pk: BlsPublicKeyType> ToJwk for BlsKeyPair<Pk> {

impl<Pk: BlsPublicKeyType> FromJwk for BlsKeyPair<Pk> {
fn from_jwk_parts(jwk: JwkParts<'_>) -> Result<Self, Error> {
if jwk.kty != JWK_KEY_TYPE {
if jwk.kty != JWK_KEY_TYPE &&
/* compatibility with previous version */
jwk.kty != "EC"
{
return Err(err_msg!(InvalidKeyData, "Unsupported key type"));
}
if jwk.crv != Pk::JWK_CURVE {
Expand Down Expand Up @@ -557,4 +560,27 @@ mod tests {
// sk_load.to_keypair_bytes().unwrap()
// );
}

#[cfg(feature = "any_key")]
#[test]
// test loading of a key with the EC key type
fn g1_jwk_any_compat() {
use crate::alg::{any::AnyKey, BlsCurves, KeyAlg};
use alloc::boxed::Box;

let test_jwk_compat = r#"
{
"crv": "BLS12381_G1",
"kty": "EC",
"x": "osl1NIZnkmrPEvPuywBQROCKept9lfML0oG1VEUQc2ei5dBVi-eUPIvRP5oacDb7"
}"#;
let key = Box::<AnyKey>::from_jwk(test_jwk_compat).expect("Error decoding BLS key JWK");
assert_eq!(key.algorithm(), KeyAlg::Bls12_381(BlsCurves::G1));
let as_bls = key
.downcast_ref::<BlsKeyPair<G1>>()
.expect("Error downcasting BLS key");
let _ = as_bls
.to_jwk_public(None)
.expect("Error converting key to JWK");
}
}
2 changes: 1 addition & 1 deletion src/ffi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use once_cell::sync::Lazy;
static LAST_ERROR: Lazy<RwLock<Option<Error>>> = Lazy::new(|| RwLock::new(None));

#[derive(Debug, PartialEq, Copy, Clone, Serialize)]
#[repr(usize)]
#[repr(i64)]
pub enum ErrorCode {
Success = 0,
Backend = 1,
Expand Down
2 changes: 2 additions & 0 deletions wrappers/python/aries_askar/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ def _cb(id: int, err: int, result=None):
def do_call(fn_name, *args):
"""Perform a synchronous library function call."""
lib_fn = getattr(get_library(), fn_name)
lib_fn.restype = c_int64
result = lib_fn(*args)
if result:
raise get_current_error(True)
Expand All @@ -536,6 +537,7 @@ def do_call_async(
) -> asyncio.Future:
"""Perform an asynchronous library function call."""
lib_fn = getattr(get_library(), fn_name)
lib_fn.restype = c_int64
loop = asyncio.get_event_loop()
fut = loop.create_future()
cf_args = [None, c_int64, c_int64]
Expand Down
2 changes: 1 addition & 1 deletion wrappers/python/aries_askar/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""aries_askar library wrapper version."""

__version__ = "0.2.3"
__version__ = "0.2.4"

0 comments on commit a92d360

Please sign in to comment.