-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
391 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/ic_cose_types/src/crypto.rs → src/ic_cose_types/src/cose/aes.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use coset::{ | ||
cwt::{ClaimName, ClaimsSet, Timestamp}, | ||
iana, CborSerializable, | ||
}; | ||
use num_traits::ToPrimitive; | ||
|
||
const CLOCK_SKEW: i64 = 5 * 60; // 5 minutes | ||
pub static SCOPE_NAME: ClaimName = ClaimName::Assigned(iana::CwtClaimName::Scope); | ||
|
||
pub fn cwt_from_cwt(data: &[u8], now_sec: i64) -> Result<ClaimsSet, String> { | ||
let claims = ClaimsSet::from_slice(data).map_err(|err| format!("invalid claims: {}", err))?; | ||
if let Some(ref exp) = claims.expiration_time { | ||
let exp = match exp { | ||
Timestamp::WholeSeconds(v) => *v, | ||
Timestamp::FractionalSeconds(v) => (*v).to_i64().unwrap_or_default(), | ||
}; | ||
if exp < now_sec - CLOCK_SKEW { | ||
return Err("token expired".to_string()); | ||
} | ||
} | ||
if let Some(ref nbf) = claims.not_before { | ||
let nbf = match nbf { | ||
Timestamp::WholeSeconds(v) => *v, | ||
Timestamp::FractionalSeconds(v) => (*v).to_i64().unwrap_or_default(), | ||
}; | ||
if nbf > now_sec + CLOCK_SKEW { | ||
return Err("token not yet valid".to_string()); | ||
} | ||
} | ||
|
||
Ok(claims) | ||
} | ||
|
||
pub fn get_scope(claims: &ClaimsSet) -> Result<String, String> { | ||
let scope = claims | ||
.rest | ||
.iter() | ||
.find(|(key, _)| key == &SCOPE_NAME) | ||
.ok_or("missing scope")?; | ||
let scope = scope.1.as_text().ok_or("invalid scope text")?; | ||
Ok(scope.to_string()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use x25519_dalek::{PublicKey, SharedSecret, StaticSecret}; | ||
|
||
pub fn ecdh_x25519(secret: [u8; 32], their_public: [u8; 32]) -> (SharedSecret, PublicKey) { | ||
let secret = StaticSecret::from(secret); | ||
let public = PublicKey::from(&secret); | ||
( | ||
secret.diffie_hellman(&PublicKey::from(their_public)), | ||
public, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use serde_bytes::ByteBuf; | ||
|
||
use super::sha256; | ||
use crate::format_error; | ||
|
||
pub use k256::ecdsa::{ | ||
signature::hazmat::{PrehashSigner, PrehashVerifier}, | ||
Signature, SigningKey, VerifyingKey, | ||
}; | ||
|
||
pub fn secp256k1_verify_any( | ||
public_keys: &[ByteBuf], | ||
message: &[u8], | ||
signature: &[u8], | ||
) -> Result<(), String> { | ||
let keys: Vec<VerifyingKey> = public_keys | ||
.iter() | ||
.map(|key| VerifyingKey::from_sec1_bytes(key).map_err(format_error)) | ||
.collect::<Result<_, _>>()?; | ||
let sig = Signature::try_from(signature).map_err(format_error)?; | ||
let digest = sha256(message); | ||
match keys | ||
.iter() | ||
.any(|key| key.verify_prehash(&digest, &sig).is_ok()) | ||
{ | ||
true => Ok(()), | ||
false => Err("secp256k1 signature verification failed".to_string()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::{format_error, ByteN}; | ||
|
||
pub use ed25519_dalek::{Signature, SigningKey, VerifyingKey}; | ||
|
||
pub fn ed25519_verify_any( | ||
public_keys: &[ByteN<32>], | ||
message: &[u8], | ||
signature: &[u8], | ||
) -> Result<(), String> { | ||
let keys: Vec<VerifyingKey> = public_keys | ||
.iter() | ||
.map(|key| VerifyingKey::from_bytes(key).map_err(format_error)) | ||
.collect::<Result<_, _>>()?; | ||
let sig = Signature::from_slice(signature).map_err(format_error)?; | ||
|
||
match keys | ||
.iter() | ||
.any(|key| key.verify_strict(message, &sig).is_ok()) | ||
{ | ||
true => Ok(()), | ||
false => Err("ed25519 signature verification failed".to_string()), | ||
} | ||
} |
Oops, something went wrong.