Skip to main content

embedded_cal_software_demo/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// SPDX-FileCopyrightText: Inria-AIO, Cryspen, and Christian Amsüss
3//! Minimal stand-in for the libcrux based implementation and polyfills.
4//!
5//! Currently, this demonstrates how that layer would work on top of a hardware implementation that
6//! only does the hard work of the SHA hashes and not the clerical buffering / padding.
7#![no_std]
8
9use embedded_cal::{Cal, accessor::*, plumbing::Plumbing};
10
11mod hash;
12mod hash_plumbing;
13mod hkdf;
14mod hmac;
15mod rng;
16
17pub trait ExtenderConfig {
18    const IMPLEMENT_SHA2SHORT: bool;
19    const IMPLEMENT_SHA2SHORT_PLUMBING: bool;
20
21    type Base: Cal + Plumbing;
22}
23
24impl<EC: ExtenderConfig> Extender<EC> {
25    pub fn new(base: EC::Base) -> Self {
26        Self(base)
27    }
28
29    pub fn destruct(self) -> EC::Base {
30        self.0
31    }
32}
33
34pub struct Extender<EC: ExtenderConfig>(EC::Base);
35
36// All the required trait impls come from the modules.
37impl<EC: ExtenderConfig> embedded_cal::Cal for Extender<EC> {
38    type DhProvider = DhProviderOf<EC::Base>;
39    type AeadProvider = AeadProviderOf<EC::Base>;
40    type HashProvider = Self;
41    type HmacProvider = Self;
42
43    fn dh(&mut self) -> &mut Self::DhProvider {
44        self.0.dh()
45    }
46
47    fn aead(&mut self) -> &mut Self::AeadProvider {
48        self.0.aead()
49    }
50
51    fn hash(&mut self) -> &mut Self::HashProvider {
52        self
53    }
54
55    fn hmac(&mut self) -> &mut Self::HmacProvider {
56        self
57    }
58}
59
60impl<EC: ExtenderConfig> embedded_cal::plumbing::ec::Ec for Extender<EC> {
61    const MAX_SCALAR_LENGTH: usize =
62        <EC::Base as embedded_cal::plumbing::ec::Ec>::MAX_SCALAR_LENGTH;
63
64    type PrimitivesP256 = <EC::Base as embedded_cal::plumbing::ec::Ec>::PrimitivesP256;
65    type PrimitivesX25519 = <EC::Base as embedded_cal::plumbing::ec::Ec>::PrimitivesX25519;
66    type PrimitivesX448 = <EC::Base as embedded_cal::plumbing::ec::Ec>::PrimitivesX448;
67
68    fn p256(&mut self) -> &mut Self::PrimitivesP256 {
69        self.0.p256()
70    }
71
72    fn x25519(&mut self) -> &mut Self::PrimitivesX25519 {
73        self.0.x25519()
74    }
75
76    fn x448(&mut self) -> &mut Self::PrimitivesX448 {
77        self.0.x448()
78    }
79}
80
81impl<EC: ExtenderConfig> embedded_cal::plumbing::hash::Hash for Extender<EC> {}
82
83impl<EC: ExtenderConfig> embedded_cal::plumbing::Plumbing for Extender<EC> {}