embedded_cal_rustcrypto/
lib.rs1mod aead;
5mod dh;
6mod hash;
7mod hmac;
8mod rng;
9
10#[cfg(any(feature = "standalone", test))]
11mod standalone;
12
13#[cfg(any(feature = "standalone", test))]
14pub use standalone::Standalone;
15
16use digest::Digest;
17use embedded_cal::accessor::*;
18
19pub struct RustcryptoCalExtender<Base> {
20 #[cfg(not(feature = "alloc"))]
21 aead_buffer: [u8; 1024],
22 _private: (),
23 base: Base,
24}
25
26impl<Base> RustcryptoCalExtender<Base> {
27 pub const fn new_extending(base: Base) -> Self {
28 Self {
29 #[cfg(not(feature = "alloc"))]
30 aead_buffer: [0; _],
31 _private: (),
32 base,
33 }
34 }
35
36 fn collect_aad(&mut self, aad: impl embedded_cal::AadGenerator) -> impl AsRef<[u8]> {
37 #[cfg(feature = "alloc")]
38 {
39 aad.items().flatten().copied().collect::<Vec<_>>()
40 }
41
42 #[cfg(not(feature = "alloc"))]
43 {
44 let mut cursor = 0;
45 for slice in aad.items() {
46 self.aead_buffer[cursor..][..slice.len()].copy_from_slice(slice);
47 cursor += slice.len();
48 }
49 &self.aead_buffer[..cursor]
50 }
51 }
52}
53
54impl<Base> embedded_cal::Cal for RustcryptoCalExtender<Base>
55where
56 Base: embedded_cal::Cal + rand_core::TryCryptoRng<Error = core::convert::Infallible>,
57{
58 type DhProvider = Self;
59 type AeadProvider = Self;
60 type HashProvider = Self;
61 type HmacProvider = Self;
62
63 fn dh(&mut self) -> &mut Self::DhProvider {
64 self
65 }
66 fn aead(&mut self) -> &mut Self::AeadProvider {
67 self
68 }
69 fn hash(&mut self) -> &mut Self::HashProvider {
70 self
71 }
72 fn hmac(&mut self) -> &mut Self::HmacProvider {
73 self
74 }
75}
76
77impl<Base: embedded_cal::Cal + embedded_cal::plumbing::hash::Sha2Short>
78 embedded_cal::plumbing::hash::Sha2Short for RustcryptoCalExtender<Base>
79{
80 const SUPPORTED: bool = Base::SUPPORTED;
81 const SEND_PADDING: bool = Base::SEND_PADDING;
82 const FIRST_CHUNK_SIZE: usize = Base::FIRST_CHUNK_SIZE;
83 const UPDATE_MULTICHUNK: bool = Base::UPDATE_MULTICHUNK;
84
85 type State = Base::State;
86
87 fn init(&mut self, variant: embedded_cal::plumbing::hash::Sha2ShortVariant) -> Self::State {
88 self.base.init(variant)
89 }
90
91 fn update(&mut self, instance: &mut Self::State, data: &[u8]) {
92 self.base.update(instance, data)
93 }
94
95 fn finalize(&mut self, instance: Self::State, last_chunk: &[u8], target: &mut [u8]) {
96 self.base.finalize(instance, last_chunk, target)
97 }
98}
99
100impl<Base: embedded_cal::Cal + embedded_cal::plumbing::hash::Hash>
101 embedded_cal::plumbing::hash::Hash for RustcryptoCalExtender<Base>
102{
103}
104
105impl<Base: embedded_cal::Cal + embedded_cal::plumbing::ec::Ec> embedded_cal::plumbing::ec::Ec
106 for RustcryptoCalExtender<Base>
107{
108 const MAX_SCALAR_LENGTH: usize = Base::MAX_SCALAR_LENGTH;
109
110 type PrimitivesP256 = Base::PrimitivesP256;
111 type PrimitivesX25519 = Base::PrimitivesX25519;
112 type PrimitivesX448 = Base::PrimitivesX448;
113
114 fn p256(&mut self) -> &mut Self::PrimitivesP256 {
115 self.base.p256()
116 }
117
118 fn x25519(&mut self) -> &mut Self::PrimitivesX25519 {
119 self.base.x25519()
120 }
121
122 fn x448(&mut self) -> &mut Self::PrimitivesX448 {
123 self.base.x448()
124 }
125}
126
127impl<Base: embedded_cal::Cal + embedded_cal::plumbing::Plumbing> embedded_cal::plumbing::Plumbing
128 for RustcryptoCalExtender<Base>
129{
130}
131
132#[cfg(test)]
133mod tests {
134 use embedded_cal_testvectors as testvectors;
135
136 use super::*;
137
138 #[test]
139 fn test_hash_algorithm_sha256() {
140 let mut cal = Standalone::standalone();
141
142 embedded_cal::test_hash_algorithm_sha256::<HashAlgorithmOf<Standalone>>();
143 testvectors::test_hash_algorithm_sha256(&mut cal);
144 }
145
146 #[test]
147 fn test_hmac_sha256() {
148 let mut cal = Standalone::standalone();
149
150 embedded_cal::test_hmac_algorithm_hmacsha256::<HmacAlgorithmOf<Standalone>>();
151 testvectors::test_hmac_sha256(&mut cal);
152 }
153
154 #[test]
155 fn test_hkdf_sha256() {
156 let mut cal = Standalone::standalone();
157
158 testvectors::test_hkdf_sha256(&mut cal);
159 }
160
161 #[test]
162 fn test_aead_aesccm_16_64_128() {
163 let mut cal = Standalone::standalone();
164
165 testvectors::aead::aes_ccm::test_16_64_128(&mut cal);
166 }
167
168 #[test]
169 fn test_dh() {
170 use embedded_cal::DhAlgorithm;
171
172 let mut cal = Standalone::standalone();
173
174 embedded_cal::test_dh_algorithm_ecdh_p256::<Standalone>();
175
176 let p256 = DhAlgorithm::from_cose_ecdh(1).unwrap();
179 let x25519 = DhAlgorithm::from_cose_ecdh(4).unwrap();
180
181 embedded_cal::test_dh_selftest(&mut cal, p256);
182 embedded_cal::test_dh_selftest(&mut cal, x25519);
183
184 for vec in testvectors::dh::RFC7748_X25519 {
185 vec.test_with(&mut cal);
186 }
187
188 for vec in testvectors::dh::RFC5903_P256 {
189 vec.test_with(&mut cal);
190 }
191 }
192
193 #[test]
194 fn test_aead_aesccm_16_64_256() {
195 let mut cal = Standalone::standalone();
196
197 testvectors::aead::aes_ccm::test_16_64_256(&mut cal);
198 }
199
200 #[test]
201 fn test_plumbing_passon() {
202 fn f(_cal: impl embedded_cal::plumbing::Plumbing) {}
203 f(RustcryptoCalExtender::new_extending(
204 embedded_cal::empty::EmptyCal,
205 ))
206 }
207}