Skip to content

Commit

Permalink
Add Dilithium public key types
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKotowick committed Oct 1, 2024
1 parent 0fd493a commit c262154
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ readme = "README.md"
edition = "2021"

[dependencies]
cfg-if = "1.0.0"
heapless-bytes = "0.3.0"
pqcrypto-dilithium = { version = "0.5.0", optional = true}
serde-big-array = "0.5.1"
serde_repr = "0.1"

[dependencies.serde]
Expand All @@ -26,3 +29,9 @@ hex = "0.4.3"
itertools = "0.12.0"
quickcheck = "1.0.3"
serde = "1"

[features]
backend-dilithium = ["dep:pqcrypto-dilithium"]
backend-dilithium2 = ["backend-dilithium"]
backend-dilithium3 = ["backend-dilithium"]
backend-dilithium5 = ["backend-dilithium"]
69 changes: 69 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@

use core::fmt::{self, Formatter};
pub use heapless_bytes::Bytes;
#[cfg(feature = "backend-dilithium")]
use pqcrypto_dilithium::ffi;
use serde::{
de::{Error as _, Expected, MapAccess, Unexpected},
Deserialize, Serialize,
};
#[cfg(feature = "backend-dilithium")]
use serde_big_array::BigArray;
use serde_repr::{Deserialize_repr, Serialize_repr};

#[repr(i8)]
Expand Down Expand Up @@ -101,6 +105,13 @@ enum Alg {
EdDsa = -8,
Totp = -9, // Unassigned, we use it for TOTP

#[cfg(feature = "backend-dilithium2")]
Dilithium2 = -87,
#[cfg(feature = "backend-dilithium3")]
Dilithium3 = -88,
#[cfg(feature = "backend-dilithium5")]
Dilithium5 = -89,

// MAC
// Hs256 = 5,
// Hs512 = 7,
Expand Down Expand Up @@ -149,6 +160,12 @@ pub enum PublicKey {
EcdhEsHkdf256Key(EcdhEsHkdf256PublicKey),
Ed25519Key(Ed25519PublicKey),
TotpKey(TotpPublicKey),
#[cfg(feature = "backend-dilithium2")]
Dilithium2(Dilithium2PublicKey),
#[cfg(feature = "backend-dilithium3")]
Dilithium3(Dilithium3PublicKey),
#[cfg(feature = "backend-dilithium5")]
Dilithium5(Dilithium5PublicKey),
}

impl From<P256PublicKey> for PublicKey {
Expand All @@ -175,6 +192,26 @@ impl From<TotpPublicKey> for PublicKey {
}
}

#[cfg(feature = "backend-dilithium2")]
impl From<Dilithium2PublicKey> for PublicKey {
fn from(key: Dilithium2PublicKey) -> Self {
PublicKey::Dilithium2(key)
}
}

#[cfg(feature = "backend-dilithium3")]
impl From<Dilithium3PublicKey> for PublicKey {
fn from(key: Dilithium3PublicKey) -> Self {
PublicKey::Dilithium3(key)
}
}
#[cfg(feature = "backend-dilithium5")]
impl From<Dilithium5PublicKey> for PublicKey {
fn from(key: Dilithium5PublicKey) -> Self {
PublicKey::Dilithium5(key)
}
}

#[derive(Clone, Debug, Default)]
struct RawPublicKey {
kty: Option<Kty>,
Expand Down Expand Up @@ -491,3 +528,35 @@ impl<'de> serde::Deserialize<'de> for Ed25519PublicKey {
Ok(Self { x })
}
}

macro_rules! dilithium_public_key {
($type_name: ident, $size: expr) => {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct $type_name(#[serde(with = "BigArray")] [u8; $size]);
};
}

cfg_if::cfg_if! {
if #[cfg(feature = "backend-dilithium2")] {
dilithium_public_key!(
Dilithium2PublicKey,
ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_PUBLICKEYBYTES
);
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "backend-dilithium3")] {
dilithium_public_key!(
Dilithium3PublicKey,
ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_PUBLICKEYBYTES
);
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "backend-dilithium5")] {
dilithium_public_key!(
Dilithium5PublicKey,
ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_PUBLICKEYBYTES
);
}
}

0 comments on commit c262154

Please sign in to comment.