1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
use super::internal::{Mode, Phase}; use super::*; #[derive(Clone, Debug)] pub enum XoodyakAny { Hash(XoodyakHash), Keyed(XoodyakKeyed), } impl internal::XoodyakCommon for XoodyakAny { fn state(&mut self) -> &mut Xoodoo { match self { XoodyakAny::Hash(x) => x.state(), XoodyakAny::Keyed(x) => x.state(), } } fn mode(&self) -> Mode { match self { XoodyakAny::Hash(x) => x.mode(), XoodyakAny::Keyed(x) => x.mode(), } } fn phase(&self) -> Phase { match self { XoodyakAny::Hash(x) => x.phase(), XoodyakAny::Keyed(x) => x.phase(), } } fn set_phase(&mut self, phase: Phase) { match self { XoodyakAny::Hash(x) => x.set_phase(phase), XoodyakAny::Keyed(x) => x.set_phase(phase), } } fn absorb_rate(&self) -> usize { match self { XoodyakAny::Hash(x) => x.absorb_rate(), XoodyakAny::Keyed(x) => x.absorb_rate(), } } fn squeeze_rate(&self) -> usize { match self { XoodyakAny::Hash(x) => x.squeeze_rate(), XoodyakAny::Keyed(x) => x.squeeze_rate(), } } } impl XoodyakAny { #[inline] fn keyed(&mut self) -> Result<&mut XoodyakKeyed, Error> { match self { XoodyakAny::Hash(_) => Err(Error::KeyRequired), XoodyakAny::Keyed(ref mut x) => Ok(x), } } #[inline] pub fn absorb_key_and_nonce( &mut self, key: &[u8], key_id: Option<&[u8]>, nonce: Option<&[u8]>, counter: Option<&[u8]>, ) -> Result<(), Error> { self.keyed()? .absorb_key_and_nonce(key, key_id, nonce, counter) } #[inline] pub fn ratchet(&mut self) -> Result<(), Error> { Ok(self.keyed()?.ratchet()) } #[inline] pub fn encrypt(&mut self, out: &mut [u8], bin: &[u8]) -> Result<(), Error> { self.keyed()?.encrypt(out, bin) } #[inline] pub fn decrypt(&mut self, out: &mut [u8], bin: &[u8]) -> Result<(), Error> { self.keyed()?.decrypt(out, bin) } #[inline] pub fn encrypt_in_place(&mut self, in_out: &mut [u8]) -> Result<(), Error> { Ok(self.keyed()?.encrypt_in_place(in_out)) } #[inline] pub fn decrypt_in_place(&mut self, in_out: &mut [u8]) -> Result<(), Error> { Ok(self.keyed()?.decrypt_in_place(in_out)) } #[inline] pub fn aead_encrypt_detached( &mut self, out: &mut [u8], bin: Option<&[u8]>, ) -> Result<Tag, Error> { self.keyed()?.aead_encrypt_detached(out, bin) } #[inline] pub fn aead_encrypt(&mut self, out: &mut [u8], bin: Option<&[u8]>) -> Result<(), Error> { self.keyed()?.aead_encrypt(out, bin) } #[inline] pub fn aead_decrypt_detached( &mut self, out: &mut [u8], auth_tag: &Tag, bin: Option<&[u8]>, ) -> Result<(), Error> { self.keyed()?.aead_decrypt_detached(out, auth_tag, bin) } #[inline] pub fn aead_decrypt(&mut self, out: &mut [u8], bin: &[u8]) -> Result<(), Error> { self.keyed()?.aead_decrypt(out, bin) } #[inline] pub fn aead_encrypt_in_place_detached(&mut self, in_out: &mut [u8]) -> Result<Tag, Error> { Ok(self.keyed()?.aead_encrypt_in_place_detached(in_out)) } #[inline] pub fn aead_encrypt_in_place(&mut self, in_out: &mut [u8]) -> Result<(), Error> { self.keyed()?.aead_encrypt_in_place(in_out) } #[inline] pub fn aead_decrypt_in_place_detached( &mut self, in_out: &mut [u8], auth_tag: &Tag, ) -> Result<(), Error> { self.keyed()? .aead_decrypt_in_place_detached(in_out, auth_tag) } #[inline] pub fn aead_decrypt_in_place<'t>( &mut self, in_out: &'t mut [u8], ) -> Result<&'t mut [u8], Error> { self.keyed()?.aead_decrypt_in_place(in_out) } #[cfg(feature = "std")] #[inline] pub fn encrypt_to_vec(&mut self, bin: &[u8]) -> Result<Vec<u8>, Error> { self.keyed()?.encrypt_to_vec(bin) } #[cfg(feature = "std")] #[inline] pub fn decrypt_to_vec(&mut self, bin: &[u8]) -> Result<Vec<u8>, Error> { self.keyed()?.decrypt_to_vec(bin) } #[cfg(feature = "std")] #[inline] pub fn aead_encrypt_to_vec_detached( &mut self, bin: Option<&[u8]>, ) -> Result<(Vec<u8>, Tag), Error> { self.keyed()?.aead_encrypt_to_vec_detached(bin) } #[cfg(feature = "std")] #[inline] pub fn aead_encrypt_to_vec(&mut self, bin: Option<&[u8]>) -> Result<Vec<u8>, Error> { self.keyed()?.aead_encrypt_to_vec(bin) } #[cfg(feature = "std")] #[inline] pub fn aead_encrypt_in_place_to_vec(&mut self, in_out: Vec<u8>) -> Result<Vec<u8>, Error> { Ok(self.keyed()?.aead_encrypt_in_place_to_vec(in_out)) } #[cfg(feature = "std")] #[inline] pub fn aead_decrypt_to_vec_detached( &mut self, auth_tag: Tag, bin: Option<&[u8]>, ) -> Result<Vec<u8>, Error> { self.keyed()?.aead_decrypt_to_vec_detached(auth_tag, bin) } #[cfg(feature = "std")] #[inline] pub fn aead_decrypt_to_vec(&mut self, bin: &[u8]) -> Result<Vec<u8>, Error> { self.keyed()?.aead_decrypt_to_vec(bin) } #[cfg(feature = "std")] #[inline] pub fn aead_decrypt_in_place_to_vec(&mut self, in_out: Vec<u8>) -> Result<Vec<u8>, Error> { self.keyed()?.aead_decrypt_in_place_to_vec(in_out) } } impl XoodyakCommon for XoodyakAny {}