1#![no_std]
2#[cfg(feature = "std")]
3extern crate std;
4
5extern crate alloc;
6use core::convert::Infallible;
7use core::marker::PhantomData;
8
9use alloc::vec;
10use alloc::vec::Vec;
11use sha3::digest::XofReader;
12#[repr(transparent)]
13#[derive(Clone,Copy,Debug,Default)]
14pub struct Digest<X,E>{
15 pub wrapped: X,
16 pub errors: PhantomData<E>,
17}
18impl<X: XofReader,E: embedded_io::Error> embedded_io::ErrorType for Digest<X,E>{
19 type Error = E;
20}
21impl<X: XofReader,E: embedded_io::Error> embedded_io::Read for Digest<X,E>{
22 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
23 self.wrapped.read(buf);
24 Ok(buf.len())
25 }
26}
27pub struct Otp<A,B>{
28 pub wrapped: A,
29 pub pad: B,
30}
31#[cfg(feature = "std")]
32impl<A: std::io::Read,B: embedded_io::Read> std::io::Read for Otp<A,B> where std::io::Error: From<B::Error>{
33 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
34 let x = self.wrapped.read(buf)?;
35 let mut b = vec![0u8; x];
36 let y = self.pad.read(&mut b)?;
37 for (a,b) in b.iter().zip(buf.iter_mut()){
38 *b ^= *a;
39 }
40 return Ok(y);
41 }
42}
43#[cfg(feature = "std")]
44impl<A: std::io::Write,B: embedded_io::Read> std::io::Write for Otp<A,B> where std::io::Error: From<B::Error>{
45 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
46 let mut n = vec![0u8; buf.len()];
47 let y = self.pad.read(&mut n)?;
48 let buf: Vec<_> = buf[..y].iter().zip(n.iter()).map(|(a,b)|*a ^ *b).collect();
49 return self.wrapped.write(&buf);
50 }
51
52 fn flush(&mut self) -> std::io::Result<()> {
53 self.wrapped.flush()
54 }
55}
56
57impl<A: embedded_io_async::Read,B: embedded_io::Read> embedded_io_async::Read for Otp<A,B> where A::Error: From<B::Error>{
58 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
59 let x = self.wrapped.read(buf).await?;
60 let mut b = vec![0u8; x];
61 let y = self.pad.read(&mut b)?;
62 for (a,b) in b.iter().zip(buf.iter_mut()){
63 *b ^= *a;
64 }
65 return Ok(y);
66 }
67}
68impl<A: embedded_io_async::Write,B: embedded_io::Read> embedded_io_async::Write for Otp<A,B> where A::Error: From<B::Error>{
69 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
70 let mut n = vec![0u8; buf.len()];
71 let y = self.pad.read(&mut n)?;
72 let buf: Vec<_> = buf[..y].iter().zip(n.iter()).map(|(a,b)|*a ^ *b).collect();
73 return self.wrapped.write(&buf).await;
74 }
75}
76impl<A: embedded_io::ErrorType,B: embedded_io::Read> embedded_io::ErrorType for Otp<A,B>{
77 type Error = A::Error;
78}
79impl<A: embedded_io::Read,B: embedded_io::Read> embedded_io::Read for Otp<A,B> where A::Error: From<B::Error>{
80 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
81 let x = self.wrapped.read(buf)?;
82 let mut b = vec![0u8; x];
83 let y = self.pad.read(&mut b)?;
84 for (a,b) in b.iter().zip(buf.iter_mut()){
85 *b ^= *a;
86 }
87 return Ok(y);
88 }
89}
90impl<A: embedded_io::Write,B: embedded_io::Read> embedded_io::Write for Otp<A,B> where A::Error: From<B::Error>{
91 fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
92 let mut n = vec![0u8; buf.len()];
93 let y = self.pad.read(&mut n)?;
94 let buf: Vec<_> = buf[..y].iter().zip(n.iter()).map(|(a,b)|*a ^ *b).collect();
95 return self.wrapped.write(&buf);
96 }
97
98 fn flush(&mut self) -> Result<(), Self::Error> {
99 self.wrapped.flush()
100 }
101}