#![no_std]
pub use heapless_bytes::{consts, Bytes};
use serde::Serialize;
use serde_repr::{Deserialize_repr, Serialize_repr};
#[repr(i8)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
enum Label {
Kty = 1,
Alg = 3,
Crv = -1,
X = -2,
Y = -3,
}
#[repr(i8)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
enum Kty {
Okp = 1,
Ec2 = 2,
Symmetric = 4,
}
#[repr(i8)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
enum Alg {
Es256 = -7,
EdDsa = -8,
EcdhEsHkdf256 = -25,
}
#[repr(i8)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
enum Crv {
P256 = 1,
X25519 = 4,
Ed25519 = 6,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(untagged)]
pub enum PublicKey {
P256Key(P256PublicKey),
Ed25519Key(Ed25519PublicKey),
}
impl From<P256PublicKey> for PublicKey {
fn from(key: P256PublicKey) -> Self {
PublicKey::P256Key(key)
}
}
impl From<Ed25519PublicKey> for PublicKey {
fn from(key: Ed25519PublicKey) -> Self {
PublicKey::Ed25519Key(key)
}
}
trait PublicKeyConstants {
const KTY: Kty;
const ALG: Alg;
const CRV: Crv;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct P256PublicKey {
pub x: Bytes<consts::U32>,
pub y: Bytes<consts::U32>,
}
impl PublicKeyConstants for P256PublicKey {
const KTY: Kty = Kty::Ec2;
const ALG: Alg = Alg::Es256;
const CRV: Crv = Crv::P256;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ed25519PublicKey {
pub x: Bytes<consts::U32>,
}
impl PublicKeyConstants for Ed25519PublicKey {
const KTY: Kty = Kty::Okp;
const ALG: Alg = Alg::EdDsa;
const CRV: Crv = Crv::Ed25519;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct X25519PublicKey {
pub pub_key: Bytes<consts::U32>,
}
impl serde::Serialize for P256PublicKey {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(5))?;
map.serialize_entry(&(Label::Kty as i8), &(Self::KTY as i8))?;
map.serialize_entry(&(Label::Alg as i8), &(Self::ALG as i8))?;
map.serialize_entry(&(Label::Crv as i8), &(Self::CRV as i8))?;
map.serialize_entry(&(Label::X as i8), &self.x)?;
map.serialize_entry(&(Label::Y as i8), &self.y)?;
map.end()
}
}
impl serde::Serialize for Ed25519PublicKey {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(4))?;
map.serialize_entry(&(Label::Kty as i8), &(Self::KTY as i8))?;
map.serialize_entry(&(Label::Alg as i8), &(Self::ALG as i8))?;
map.serialize_entry(&(Label::Crv as i8), &(Self::CRV as i8))?;
map.serialize_entry(&(Label::X as i8), &self.x)?;
map.end()
}
}
impl<'de> serde::Deserialize<'de> for P256PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct IndexedVisitor;
impl<'de> serde::de::Visitor<'de> for IndexedVisitor {
type Value = P256PublicKey;
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("P256PublicKey")
}
fn visit_map<V>(self, mut map: V) -> Result<P256PublicKey, V::Error>
where
V: serde::de::MapAccess<'de>,
{
match (map.next_key()?, map.next_value()?) {
(Some(Label::Kty), Some(P256PublicKey::KTY)) => {}
_ => {
return Err(serde::de::Error::missing_field("kty"));
}
}
match (map.next_key()?, map.next_value()?) {
(Some(Label::Alg), Some(P256PublicKey::ALG)) => {}
_ => {
return Err(serde::de::Error::missing_field("alg"));
}
}
match (map.next_key()?, map.next_value()?) {
(Some(Label::Crv), Some(P256PublicKey::CRV)) => {}
_ => {
return Err(serde::de::Error::missing_field("crv"));
}
}
let x = match (map.next_key()?, map.next_value()?) {
(Some(Label::X), Some(bytes)) => bytes,
_ => {
return Err(serde::de::Error::missing_field("x"));
}
};
let y = match (map.next_key()?, map.next_value()?) {
(Some(Label::Y), Some(bytes)) => bytes,
_ => {
return Err(serde::de::Error::missing_field("y"));
}
};
Ok(P256PublicKey { x, y })
}
}
deserializer.deserialize_map(IndexedVisitor {})
}
}
impl<'de> serde::Deserialize<'de> for Ed25519PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct IndexedVisitor;
impl<'de> serde::de::Visitor<'de> for IndexedVisitor {
type Value = Ed25519PublicKey;
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("Ed25519PublicKey")
}
fn visit_map<V>(self, mut map: V) -> Result<Ed25519PublicKey, V::Error>
where
V: serde::de::MapAccess<'de>,
{
match (map.next_key()?, map.next_value()?) {
(Some(Label::Kty), Some(Ed25519PublicKey::KTY)) => {}
_ => {
return Err(serde::de::Error::missing_field("kty"));
}
}
match (map.next_key()?, map.next_value()?) {
(Some(Label::Alg), Some(Ed25519PublicKey::ALG)) => {}
_ => {
return Err(serde::de::Error::missing_field("alg"));
}
}
match (map.next_key()?, map.next_value()?) {
(Some(Label::Crv), Some(Ed25519PublicKey::CRV)) => {}
_ => {
return Err(serde::de::Error::missing_field("crv"));
}
}
let x = match (map.next_key()?, map.next_value()?) {
(Some(Label::X), Some(bytes)) => bytes,
_ => {
return Err(serde::de::Error::missing_field("x"));
}
};
Ok(Ed25519PublicKey { x })
}
}
deserializer.deserialize_map(IndexedVisitor {})
}
}