Skip to content

Commit

Permalink
try CRYSTALS Dilithium
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncolburne committed Aug 13, 2023
1 parent 2b2a39f commit 7ba4349
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cesride"
version = "0.6.1"
version = "0.7.0"
edition = "2021"
description = "Cryptographic primitives for use with Composable Event Streaming Representation (CESR)"
license = "Apache-2.0"
Expand All @@ -9,12 +9,13 @@ keywords = ["cesr", "keri", "acdc"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "~1"
anyhow = { version = "~1", features = ["backtrace"] }
argon2 = "~0.5"
base64 = "~0.21"
blake2 = "~0.10"
blake3 = "~1"
chrono = { version = "~0.4", default-features = false, features = ["clock"] }
crystals-dilithium = "~1"
ed25519-dalek = { version = "2.0.0-rc.2", features = ["rand_core"] }
indexmap = "~1"
k256 = "~0.13"
Expand Down
1 change: 1 addition & 0 deletions src/core/cigar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn validate_code(code: &str) -> Result<()> {
matter::Codex::Ed25519_Sig,
matter::Codex::ECDSA_256k1_Sig,
matter::Codex::ECDSA_256r1_Sig,
matter::Codex::CRYSTALS_Dilithium_Sig,
// matter::Codex::Ed448_Sig,
];

Expand Down
20 changes: 20 additions & 0 deletions src/core/indexer/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub mod Codex {
pub const ECDSA_256k1_Crt: &str = "D"; // ECDSA secp256k1 sig appears in current list.
pub const ECDSA_256r1: &str = "E"; // ECDSA secp256r1 sig appears same in both lists if any.
pub const ECDSA_256r1_Crt: &str = "F"; // ECDSA secp256r1 sig appears in current list.
pub const CRYSTALS_Dilithium_Big: &str = "G"; // CRYSTALS Dilithium sig appears in both lists.
pub const CRYSTALS_Dilithium_Big_Crt: &str = "H"; // CRYSTALS Dilithium sig appears in current list only.
pub const Ed448: &str = "0A"; // Ed448 signature appears in both lists.
pub const Ed448_Crt: &str = "0B"; // Ed448 signature appears in current list only.
pub const Ed25519_Big: &str = "2A"; // Ed25519 sig appears in both lists.
Expand All @@ -41,6 +43,8 @@ pub mod Codex {
pub const TBD0: &str = "0z"; // Test of Var len label L=N*4 <= 4095 char quadlets includes code
pub const TBD1: &str = "1z"; // Test of index sig lead 1
pub const TBD4: &str = "4z"; // Test of index sig lead 1 big
pub const CRYSTALS_Dilithium: &str = "1AAA"; // CRYSTALS Dilithium sig appears same in both lists if any.
pub const CRYSTALS_Dilithium_Crt: &str = "1AAB"; // CRYSTALS Dilithium sig appears in current list.
}

/// SigCodex is all indexed signature derivation codes
Expand All @@ -53,6 +57,8 @@ pub mod SigCodex {
pub const ECDSA_256k1_Crt: &str = "D"; // ECDSA secp256k1 sig appears in current list.
pub const ECDSA_256r1: &str = "E"; // ECDSA secp256r1 sig appears same in both lists if any.
pub const ECDSA_256r1_Crt: &str = "F"; // ECDSA secp256r1 sig appears in current list.
pub const CRYSTALS_Dilithium_Big: &str = "G"; // CRYSTALS Dilithium sig appears in both lists.
pub const CRYSTALS_Dilithium_Big_Crt: &str = "H"; // CRYSTALS Dilithium sig appears in current list only.
pub const Ed448: &str = "0A"; // Ed448 signature appears in both lists.
pub const Ed448_Crt: &str = "0B"; // Ed448 signature appears in current list only.
pub const Ed25519_Big: &str = "2A"; // Ed25519 sig appears in both lists.
Expand All @@ -63,6 +69,8 @@ pub mod SigCodex {
pub const ECDSA_256r1_Big_Crt: &str = "2F"; // ECDSA secp256r1 sig appears in current list only.
pub const Ed448_Big: &str = "3A"; // Ed448 signature appears in both lists.
pub const Ed448_Big_Crt: &str = "3B"; // Ed448 signature appears in current list only.
pub const CRYSTALS_Dilithium: &str = "1AAA"; // CRYSTALS Dilithium sig appears same in both lists if any.
pub const CRYSTALS_Dilithium_Crt: &str = "1AAB"; // CRYSTALS Dilithium sig appears in current list.
}

/// CurrentSigCodex is codex indexed signature codes for current list.
Expand All @@ -72,22 +80,26 @@ pub mod CurrentSigCodex {
pub const Ed25519_Crt: &str = "B"; // Ed25519 sig appears in current list only.
pub const ECDSA_256k1_Crt: &str = "D"; // ECDSA secp256k1 sig appears in current list only.
pub const ECDSA_256r1_Crt: &str = "F"; // ECDSA secp256r1 sig appears in current list.
pub const CRYSTALS_Dilithium_Big_Crt: &str = "H"; // CRYSTALS Dilithium sig appears in current list only.
pub const Ed448_Crt: &str = "0B"; // Ed448 signature appears in current list only.
pub const Ed25519_Big_Crt: &str = "2B"; // Ed25519 sig appears in current list only.
pub const ECDSA_256k1_Big_Crt: &str = "2D"; // ECDSA secp256k1 sig appears in current list only.
pub const ECDSA_256r1_Big_Crt: &str = "2F"; // ECDSA secp256r1 sig appears in current list only.
pub const Ed448_Big_Crt: &str = "3B"; // Ed448 signature appears in current list only.
pub const CRYSTALS_Dilithium_Crt: &str = "1AAB"; // CRYSTALS Dilithium sig appears in current list.

pub(crate) fn has_code(code: &str) -> bool {
const CODES: &[&str] = &[
Ed25519_Crt,
ECDSA_256k1_Crt,
ECDSA_256r1_Crt,
CRYSTALS_Dilithium_Crt,
Ed448_Crt,
Ed25519_Big_Crt,
ECDSA_256k1_Big_Crt,
ECDSA_256r1_Big_Crt,
Ed448_Big_Crt,
CRYSTALS_Dilithium_Big_Crt,
];

CODES.contains(&code)
Expand All @@ -100,21 +112,25 @@ pub mod BothSigCodex {
pub const Ed25519: &str = "A"; // Ed25519 sig appears same in both lists if any.
pub const ECDSA_256k1: &str = "C"; // ECDSA secp256k1 sig appears same in both lists if any.
pub const ECDSA_256r1: &str = "E"; // ECDSA secp256r1 sig appears same in both lists if any.
pub const CRYSTALS_Dilithium_Big: &str = "G"; // CRYSTALS Dilithium sig appears in both lists.
pub const Ed448: &str = "0A"; // Ed448 signature appears in both lists.
pub const Ed25519_Big: &str = "2A"; // Ed25519 sig appears in both lists.
pub const ECDSA_256k1_Big: &str = "2C"; // ECDSA secp256k1 sig appears in both lists.
pub const ECDSA_256r1_Big: &str = "2E"; // ECDSA secp256r1 sig appears in both lists.
pub const Ed448_Big: &str = "3A"; // Ed448 signature appears in both lists.
pub const CRYSTALS_Dilithium: &str = "1AAA"; // CRYSTALS Dilithium sig appears same in both lists if any.

pub(crate) fn has_code(code: &str) -> bool {
const CODES: &[&str] = &[
Ed25519,
ECDSA_256k1,
ECDSA_256r1,
CRYSTALS_Dilithium,
Ed448,
Ed25519_Big,
ECDSA_256k1_Big,
ECDSA_256r1_Big,
CRYSTALS_Dilithium_Big,
Ed448_Big,
];

Expand Down Expand Up @@ -144,6 +160,8 @@ pub(crate) fn sizage(s: &str) -> Result<Sizage> {
"D" => Sizage { hs: 1, ss: 1, os: 0, fs: 88, ls: 0 },
"E" => Sizage { hs: 1, ss: 1, os: 0, fs: 88, ls: 0 },
"F" => Sizage { hs: 1, ss: 1, os: 0, fs: 88, ls: 0 },
"G" => Sizage { hs: 1, ss: 4, os: 2, fs: 4396, ls: 0 },
"H" => Sizage { hs: 1, ss: 4, os: 2, fs: 4396, ls: 0 },
"0A" => Sizage { hs: 2, ss: 2, os: 1, fs: 156, ls: 0 },
"0B" => Sizage { hs: 2, ss: 2, os: 1, fs: 156, ls: 0 },
"2A" => Sizage { hs: 2, ss: 4, os: 2, fs: 92, ls: 0 },
Expand All @@ -157,6 +175,8 @@ pub(crate) fn sizage(s: &str) -> Result<Sizage> {
"0z" => Sizage { hs: 2, ss: 2, os: 0, fs: u32::MAX, ls: 0 },
"1z" => Sizage { hs: 2, ss: 2, os: 1, fs: 76, ls: 1 },
"4z" => Sizage { hs: 2, ss: 6, os: 3, fs: 80, ls: 1 },
"1AAA" => Sizage { hs: 4, ss: 1, os: 0, fs: 4396, ls: 0 },
"1AAB" => Sizage { hs: 4, ss: 1, os: 0, fs: 4396, ls: 0 },
_ => return err!(Error::UnknownSizage(s.to_string())),
})
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/matter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub trait Matter: Default {
tables::Codex::ECDSA_256k1N,
tables::Codex::Ed448N,
tables::Codex::ECDSA_256r1N,
tables::Codex::CRYSTALS_DilithiumN,
];

!CODES.contains(&self.code().as_str())
Expand Down Expand Up @@ -822,9 +823,13 @@ mod test {
#[case(TestMatter::new_with_code_and_raw(matter::Codex::Ed25519, b"00000000000000000000000000000000").unwrap(), true)]
#[case(TestMatter::new_with_code_and_raw(matter::Codex::ECDSA_256k1, b"000000000000000000000000000000000").unwrap(), true)]
#[case(TestMatter::new_with_code_and_raw(matter::Codex::ECDSA_256r1, b"000000000000000000000000000000000").unwrap(), true)]
#[case(TestMatter::new_with_code_and_raw(matter::Codex::Ed448, &[0u8; 57]).unwrap(), true)]
#[case(TestMatter::new_with_code_and_raw(matter::Codex::CRYSTALS_Dilithium, &[0u8; 4000]).unwrap(), true)]
#[case(TestMatter::new_with_code_and_raw(matter::Codex::Ed25519N, b"00000000000000000000000000000000").unwrap(), false)]
#[case(TestMatter::new_with_code_and_raw(matter::Codex::ECDSA_256k1N, b"000000000000000000000000000000000").unwrap(), false)]
#[case(TestMatter::new_with_code_and_raw(matter::Codex::ECDSA_256r1N, b"000000000000000000000000000000000").unwrap(), false)]
#[case(TestMatter::new_with_code_and_raw(matter::Codex::Ed448N, &[0u8; 57]).unwrap(), false)]
#[case(TestMatter::new_with_code_and_raw(matter::Codex::CRYSTALS_DilithiumN, &[0u8; 4000]).unwrap(), false)]
fn transferable(#[case] matter: TestMatter, #[case] result: bool) {
assert_eq!(matter.transferable(), result);
}
Expand Down
8 changes: 8 additions & 0 deletions src/core/matter/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub(crate) fn sizage(s: &str) -> Result<Sizage> {
"O" => Sizage { hs: 1, ss: 0, fs: 44, ls: 0 },
"P" => Sizage { hs: 1, ss: 0, fs: 124, ls: 0 },
"Q" => Sizage { hs: 1, ss: 0, fs: 44, ls: 0 },
"R" => Sizage { hs: 1, ss: 0, fs: 2604, ls: 0 },
"S" => Sizage { hs: 1, ss: 0, fs: 2604, ls: 0 },
"T" => Sizage { hs: 1, ss: 0, fs: 44, ls: 0 },
"U" => Sizage { hs: 1, ss: 0, fs: 4392, ls: 0 },
"0A" => Sizage { hs: 2, ss: 0, fs: 24, ls: 0 },
"0B" => Sizage { hs: 2, ss: 0, fs: 88, ls: 0 },
"0C" => Sizage { hs: 2, ss: 0, fs: 88, ls: 0 },
Expand Down Expand Up @@ -121,6 +125,10 @@ pub mod Codex {
pub const X25519_Private: &str = "O"; // X25519 private decryption key converted from Ed25519
pub const X25519_Cipher_Seed: &str = "P"; // X25519 124 char b64 Cipher of 44 char qb64 Seed
pub const ECDSA_256r1_Seed: &str = "Q"; // ECDSA secp256r1 256 bit random Seed for private key
pub const CRYSTALS_DilithiumN: &str = "R"; // CRYSTALS Dilithium verficiation key, non-transferable.
pub const CRYSTALS_Dilithium: &str = "S"; // CRYSTALS Dilithium verficiation key.
pub const CRYSTALS_Dilithium_Seed: &str = "T"; // CRYSTALS Dilithium private key seed. This one is actually a seed.
pub const CRYSTALS_Dilithium_Sig: &str = "U"; // CRYSTALS Dilithium signature.
pub const Salt_128: &str = "0A"; // 128 bit random salt or 128 bit number (see Huge)
pub const Ed25519_Sig: &str = "0B"; // Ed25519 signature.
pub const ECDSA_256k1_Sig: &str = "0C"; // ECDSA secp256k1 signature.
Expand Down
4 changes: 4 additions & 0 deletions src/core/siger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ fn validate_code(code: &str) -> Result<()> {
indexer::Codex::ECDSA_256k1_Crt,
indexer::Codex::ECDSA_256r1,
indexer::Codex::ECDSA_256r1_Crt,
indexer::Codex::CRYSTALS_Dilithium,
indexer::Codex::CRYSTALS_Dilithium_Crt,
// indexer::Codex::Ed448,
// indexer::Codex::Ed448_Crt,
indexer::Codex::Ed25519_Big,
Expand All @@ -39,6 +41,8 @@ fn validate_code(code: &str) -> Result<()> {
indexer::Codex::ECDSA_256k1_Big_Crt,
indexer::Codex::ECDSA_256r1_Big,
indexer::Codex::ECDSA_256r1_Big_Crt,
indexer::Codex::CRYSTALS_Dilithium_Big,
indexer::Codex::CRYSTALS_Dilithium_Big_Crt,
// indexer::Codex::Ed448_Big,
// indexer::Codex::Ed448_Big_Crt,
];
Expand Down
Loading

0 comments on commit 7ba4349

Please sign in to comment.