Skip to content

Commit

Permalink
back to old OKP repr
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoSvg committed Jan 9, 2024
1 parent 4f5d62e commit 4ea978f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "json-proof-token"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
authors = ["LINKS Foundation"]
repository = "https://github.com/Cybersecurity-LINKS/json-proof-token"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Add this to your Cargo.toml:

```
[dependencies]
json-proof-token = "0.3.1"
json-proof-token = "0.3.2"
```

### Example
Expand Down
21 changes: 9 additions & 12 deletions src/jpa/bbs_plus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ impl BBSplusAlgorithm {
if check_alg_curve_compatibility(Algorithm::Proof(alg.clone()), key_params.crv.clone()) == false {
Err(CustomError::ProofGenerationError("key is not compatible".to_string()))
} else {
let x: [u8; 96] = base64url_decode(&key_params.x).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let y: [u8; 96] = base64url_decode(&key_params.y).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let pk = BBSplusPublicKey::from_coordinates(&x, &y);
let dec_pk: [u8; 96] = base64url_decode(&key_params.x).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;

let pk = BBSplusPublicKey::from_bytes(&dec_pk);
let sk = BBSplusSecretKey::from_bytes(&base64url_decode(key_params.d.as_ref().unwrap()));

let proof = match alg {
Expand Down Expand Up @@ -81,9 +81,8 @@ impl BBSplusAlgorithm {
if check_alg_curve_compatibility(Algorithm::Proof(alg.clone()), key_params.crv.clone()) == false {
Err(CustomError::ProofGenerationError("key is not compatible".to_string()))
} else {
let x: [u8; 96] = base64url_decode(&key_params.x).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let y: [u8; 96] = base64url_decode(&key_params.y).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let pk = BBSplusPublicKey::from_coordinates(&x, &y);
let dec_pk: [u8; 96] = base64url_decode(&key_params.x).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let pk = BBSplusPublicKey::from_bytes(&dec_pk);
let proof = BBSplusSignature::from_bytes(proof.try_into().unwrap()).unwrap();
let check = match alg {
ProofAlgorithm::BLS12381_SHA256 => {
Expand Down Expand Up @@ -129,9 +128,8 @@ impl BBSplusAlgorithm {
if check_presentation_alg_curve_compatibility(alg, key_params.crv.clone()) == false {
Err(CustomError::ProofGenerationError("key is not compatible".to_string()))
} else {
let x: [u8; 96] = base64url_decode(&key_params.x).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let y: [u8; 96] = base64url_decode(&key_params.y).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let pk = BBSplusPublicKey::from_coordinates(&x, &y);
let dec_pk: [u8; 96] = base64url_decode(&key_params.x).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let pk = BBSplusPublicKey::from_bytes(&dec_pk);
let revealed_message_indexes = payloads.get_disclosed_indexes();
let signature = BBSplusSignature::from_bytes(signature.try_into().unwrap()).unwrap();

Expand Down Expand Up @@ -172,9 +170,8 @@ impl BBSplusAlgorithm {
if check_presentation_alg_curve_compatibility(alg, key_params.crv.clone()) == false {
Err(CustomError::ProofGenerationError("key is not compatible".to_string()))
} else {
let x: [u8; 96] = base64url_decode(&key_params.x).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let y: [u8; 96] = base64url_decode(&key_params.y).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let pk = BBSplusPublicKey::from_coordinates(&x, &y);
let dec_pk: [u8; 96] = base64url_decode(&key_params.x).try_into().map_err(|_| CustomError::ProofGenerationError("key is not compatible".to_string()))?;
let pk = BBSplusPublicKey::from_bytes(&dec_pk);
let disclosed_indexes = payloads.get_disclosed_indexes();
let proof = BBSplusPoKSignature::from_bytes(proof.try_into().unwrap());
let check = match alg {
Expand Down
88 changes: 76 additions & 12 deletions src/jwk/alg_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ impl JwkAlgorithmParameters {

/// Octect Key Pair representation of BLS keys
///
/// Barreto-Lynn-Scott Elliptic Curve Key Representations for JOSE and COSE
/// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03)
/// For now using this representation https://www.rfc-editor.org/rfc/rfc8037
///
/// Maybe in future change to [this](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03)
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct JwkOctetKeyPairParameters {
pub kty: KeyType,
Expand All @@ -86,14 +87,10 @@ pub struct JwkOctetKeyPairParameters {
///
/// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03#curve-parameter-registration)
pub crv: EllipticCurveTypes,
/// Represents the base64url encoded x coordinate of the curve point for the public key
///
/// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03#section-2.2.1)
pub x: String, // Public Key's x-coordinate
/// Represents the base64url encoded y coordinate of the curve point for the public key
/// Represents the base64url encoded public key
///
/// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03#section-2.2.1)
pub y: String, // Public Key's y-coordinate
pub x: String, // Public Key
/// The "d" parameter contains the base64url encoded private key
///
/// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03#section-2.2.1)
Expand All @@ -103,13 +100,12 @@ pub struct JwkOctetKeyPairParameters {

impl JwkOctetKeyPairParameters {

pub fn new<T: AsRef<[u8]>>(crv: EllipticCurveTypes, x: T, y: T, d: Option<T> ) -> Self{
pub fn new<T: AsRef<[u8]>>(crv: EllipticCurveTypes, x: T, d: Option<T> ) -> Self{

Self{
kty: KeyType::OctetKeyPair,
crv: crv,
x: base64url_encode(x),
y: base64url_encode(y),
d: match d {
Some(d) => Some(base64url_encode(d)),
None => None
Expand All @@ -124,7 +120,6 @@ impl JwkOctetKeyPairParameters {
kty: KeyType::OctetKeyPair,
crv: self.crv.clone(),
x: self.x.clone(),
y: self.y.clone(),
d: None,
}
}
Expand All @@ -138,4 +133,73 @@ impl JwkOctetKeyPairParameters {
pub fn is_private(&self) -> bool {
self.d.is_some()
}
}
}




// /// Octect Key Pair representation of BLS keys
// ///
// /// Barreto-Lynn-Scott Elliptic Curve Key Representations for JOSE and COSE
// /// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03)
// #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
// pub struct JwkOctetKeyPairParameters {
// pub kty: KeyType,
// /// The "crv" (curve) parameter identifies the cryptographic curve used
// /// with the key.
// ///
// /// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03#curve-parameter-registration)
// pub crv: EllipticCurveTypes,
// /// Represents the base64url encoded x coordinate of the curve point for the public key
// ///
// /// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03#section-2.2.1)
// pub x: String, // Public Key's x-coordinate
// /// Represents the base64url encoded y coordinate of the curve point for the public key
// ///
// /// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03#section-2.2.1)
// pub y: String, // Public Key's y-coordinate
// /// The "d" parameter contains the base64url encoded private key
// ///
// /// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03#section-2.2.1)
// #[serde(skip_serializing_if = "Option::is_none")]
// pub d: Option<String>,
// }

// impl JwkOctetKeyPairParameters {

// pub fn new<T: AsRef<[u8]>>(crv: EllipticCurveTypes, x: T, y: T, d: Option<T> ) -> Self{

// Self{
// kty: KeyType::OctetKeyPair,
// crv: crv,
// x: base64url_encode(x),
// y: base64url_encode(y),
// d: match d {
// Some(d) => Some(base64url_encode(d)),
// None => None
// }
// }

// }

// /// Returns a clone without private key.
// pub fn to_public(&self) -> Self {
// Self {
// kty: KeyType::OctetKeyPair,
// crv: self.crv.clone(),
// x: self.x.clone(),
// y: self.y.clone(),
// d: None,
// }
// }

// /// Returns `true` if _all_ private key components of the key are unset, `false` otherwise.
// pub fn is_public(&self) -> bool {
// self.d.is_none()
// }

// /// Returns `true` if _all_ private key components of the key are set, `false` otherwise.
// pub fn is_private(&self) -> bool {
// self.d.is_some()
// }
// }
10 changes: 4 additions & 6 deletions src/jwk/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,17 @@ impl Jwk {
match key_type {
KeyPairSubtype::BLS12381SHA256 => {
let keypair = KeyPair::<BBS_BLS12381_SHA256>::generate(None, None);
let pk = keypair.public_key();
let pk = keypair.public_key().to_bytes();
let sk = keypair.private_key().to_bytes();
let (x,y) = pk.to_coordinates();
let okp_params = JwkOctetKeyPairParameters::new(super::curves::EllipticCurveTypes::Bls12381G2, x.as_ref(), y.as_ref(), Some(sk.as_ref()));
let okp_params = JwkOctetKeyPairParameters::new(super::curves::EllipticCurveTypes::Bls12381G2, pk.as_ref(), Some(sk.as_ref()));
let jwk_params = JwkAlgorithmParameters::OctetKeyPair(okp_params);
Ok(Self{kid: None, pk_use: None, key_ops: None, alg: None, x5u: None, x5c: None, x5t: None, key_params: jwk_params })
},
KeyPairSubtype::BLS12381SHAKE256 => {
let keypair = KeyPair::<BBS_BLS12381_SHAKE256>::generate(None, None);
let pk = keypair.public_key();
let pk = keypair.public_key().to_bytes();
let sk = keypair.private_key().to_bytes();
let (x,y) = pk.to_coordinates();
let okp_params = JwkOctetKeyPairParameters::new(super::curves::EllipticCurveTypes::Bls12381G2, x.as_ref(), y.as_ref(), Some(sk.as_ref()));
let okp_params = JwkOctetKeyPairParameters::new(super::curves::EllipticCurveTypes::Bls12381G2, pk.as_ref(), Some(sk.as_ref()));
let jwk_params = JwkAlgorithmParameters::OctetKeyPair(okp_params);
Ok(Self{kid: None, pk_use: None, key_ops: None, alg: None, x5u: None, x5c: None, x5t: None, key_params: jwk_params })
},
Expand Down

0 comments on commit 4ea978f

Please sign in to comment.