Skip to content

Commit

Permalink
new OKP representation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoSvg committed Jan 9, 2024
1 parent 8478599 commit 4f5d62e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 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.0"
version = "0.3.1"
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.0"
json-proof-token = "0.3.1"
```

### Example
Expand Down
21 changes: 12 additions & 9 deletions src/jpa/bbs_plus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +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 dec_pk = base64url_decode(&key_params.x);
let pk = BBSplusPublicKey::from_bytes(&dec_pk);
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 sk = BBSplusSecretKey::from_bytes(&base64url_decode(key_params.d.as_ref().unwrap()));

let proof = match alg {
Expand Down Expand Up @@ -80,9 +81,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 dec_pk = base64url_decode(&key_params.x);
let pk = BBSplusPublicKey::from_bytes(&dec_pk);

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 proof = BBSplusSignature::from_bytes(proof.try_into().unwrap()).unwrap();
let check = match alg {
ProofAlgorithm::BLS12381_SHA256 => {
Expand Down Expand Up @@ -128,8 +129,9 @@ 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 dec_pk = base64url_decode(&key_params.x);
let pk = BBSplusPublicKey::from_bytes(&dec_pk);
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 revealed_message_indexes = payloads.get_disclosed_indexes();
let signature = BBSplusSignature::from_bytes(signature.try_into().unwrap()).unwrap();

Expand Down Expand Up @@ -170,8 +172,9 @@ 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 dec_pk = base64url_decode(&key_params.x);
let pk = BBSplusPublicKey::from_bytes(&dec_pk);
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 disclosed_indexes = payloads.get_disclosed_indexes();
let proof = BBSplusPoKSignature::from_bytes(proof.try_into().unwrap());
let check = match alg {
Expand Down
24 changes: 19 additions & 5 deletions src/jwk/alg_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,42 @@ impl JwkAlgorithmParameters {



/// For know thir implementation refers to this: https://www.rfc-editor.org/rfc/rfc8037.html
/// But later could be changed to this: https://datatracker.ietf.org/doc/html/draft-ietf-cose-bls-key-representations-03
/// 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,
/// The "x" parameter contains the base64url encoded public key
pub x: String,
/// 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, d: Option<T> ) -> Self{
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
Expand All @@ -111,6 +124,7 @@ impl JwkOctetKeyPairParameters {
kty: KeyType::OctetKeyPair,
crv: self.crv.clone(),
x: self.x.clone(),
y: self.y.clone(),
d: None,
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/jwk/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ impl Jwk {
match key_type {
KeyPairSubtype::BLS12381SHA256 => {
let keypair = KeyPair::<BBS_BLS12381_SHA256>::generate(None, None);
let pk = keypair.public_key().to_bytes();
let pk = keypair.public_key();
let sk = keypair.private_key().to_bytes();
let okp_params = JwkOctetKeyPairParameters::new(super::curves::EllipticCurveTypes::Bls12381G2, pk.to_vec(), Some(sk.to_vec()));
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 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().to_bytes();
let pk = keypair.public_key();
let sk = keypair.private_key().to_bytes();
let okp_params = JwkOctetKeyPairParameters::new(super::curves::EllipticCurveTypes::Bls12381G2, pk.to_vec(), Some(sk.to_vec()));
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 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 4f5d62e

Please sign in to comment.