oxedyne/fe2o3/fe2o3_iop_crypto/src/enc.rs
8.2 KiB, 26 runs
created by r1870400018:379, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::keys::KeyManager; |
| 2 | |
| 3 | use oxedyne_fe2o3_core::{ |
| 4 | prelude::*, |
| 5 | alt::{ |
| 6 | Alt, |
| 7 | DefAlt, |
| 8 | Override, |
| 9 | }, |
| 10 | }; |
| 11 | use oxedyne_fe2o3_namex::id::{ |
| 12 | LocalId, |
| 13 | InNamex, |
| 14 | NamexId, |
| 15 | }; |
| 16 | |
| 17 | pub trait Encrypter: |
| 18 | KeyManager |
| 19 | + Clone |
| 20 | + std::fmt::Debug |
| 21 | + InNamex |
| 22 | + Send |
| 23 | + Sync |
| 24 | { |
| 25 | /// Encrypt the given data to a new vector, possibly with included metadata. |
| 26 | fn encrypt(&self, data: &[u8]) -> Outcome<Vec<u8>>; |
| 27 | /// Decrypt the given data to a new vector, possibly requiring the presence of included metadata. |
| 28 | fn decrypt(&self, data: &[u8]) -> Outcome<Vec<u8>>; |
| 29 | fn is_identity(&self) -> bool; |
| 30 | } |
| 31 | |
| 32 | impl Encrypter for () { |
| 33 | fn encrypt(&self, data: &[u8]) -> Outcome<Vec<u8>> { Ok(data.to_vec()) } |
| 34 | fn decrypt(&self, data: &[u8]) -> Outcome<Vec<u8>> { Ok(data.to_vec()) } |
| 35 | fn is_identity(&self) -> bool { true } |
| 36 | } |
| 37 | |
| 38 | #[derive(Clone, Debug, Default)] |
| 39 | pub struct EncrypterDefAlt< |
| 40 | D: Encrypter, |
| 41 | G: Encrypter, |
| 42 | >( |
| 43 | pub DefAlt<D, G>, |
| 44 | ); |
| 45 | |
| 46 | impl< |
| 47 | D: Encrypter, |
| 48 | G: Encrypter, |
| 49 | > |
| 50 | std::ops::Deref for EncrypterDefAlt<D, G> |
| 51 | { |
| 52 | type Target = DefAlt<D, G>; |
| 53 | fn deref(&self) -> &Self::Target { &self.0 } |
| 54 | } |
| 55 | |
| 56 | impl< |
| 57 | D: Encrypter, |
| 58 | G: Encrypter, |
| 59 | > |
| 60 | From<Option<G>> for EncrypterDefAlt<D, G> |
| 61 | { |
| 62 | fn from(opt: Option<G>) -> Self { |
| 63 | Self( |
| 64 | DefAlt::from(opt), |
| 65 | ) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl< |
| 70 | D: Encrypter, |
| 71 | G: Encrypter, |
| 72 | > |
| 73 | From<Alt<G>> for EncrypterDefAlt<D, G> |
| 74 | { |
| 75 | fn from(alt: Alt<G>) -> Self { |
| 76 | Self( |
| 77 | DefAlt::from(alt), |
| 78 | ) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl< |
| 83 | D: Encrypter, |
| 84 | G: Encrypter, |
| 85 | > |
| 86 | InNamex for EncrypterDefAlt<D, G> |
| 87 | { |
| 88 | fn name_id(&self) -> Outcome<NamexId> { |
| 89 | match &self.0 { |
| 90 | DefAlt::Default(inner) => inner.name_id(), |
| 91 | DefAlt::Given(inner) => inner.name_id(), |
| 92 | DefAlt::None => Err(err!( |
| 93 | "No Namex id can be specified for DefAlt::None."; |
| 94 | Missing, Bug)), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fn local_id(&self) -> LocalId { |
| 99 | match &self.0 { |
| 100 | DefAlt::Default(inner) => inner.local_id(), |
| 101 | DefAlt::Given(inner) => inner.local_id(), |
| 102 | DefAlt::None => LocalId::default(), |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | fn assoc_names_base64( |
| 107 | gname: &'static str, |
| 108 | ) |
| 109 | -> Outcome<Option<Vec<( |
| 110 | &'static str, |
| 111 | &'static str, |
| 112 | )>>> |
| 113 | { |
| 114 | match res!(D::assoc_names_base64(gname)) { |
| 115 | Some(mut vd) => match res!(G::assoc_names_base64(gname)) { |
| 116 | Some(vg) => { |
| 117 | vd.extend(vg); |
| 118 | Ok(Some(vd)) |
| 119 | }, |
| 120 | None => Ok(Some(vd)), |
| 121 | }, |
| 122 | None => match res!(G::assoc_names_base64(gname)) { |
| 123 | Some(vg) => Ok(Some(vg)), |
| 124 | None => Ok(None), |
| 125 | }, |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl< |
| 131 | D: Encrypter, |
| 132 | G: Encrypter, |
| 133 | > |
| 134 | Encrypter for EncrypterDefAlt<D, G> |
| 135 | { |
| 136 | |
| 137 | fn encrypt(&self, data: &[u8]) -> Outcome<Vec<u8>> { |
| 138 | match &self.0 { |
| 139 | DefAlt::Default(inner) => inner.encrypt(data), |
| 140 | DefAlt::Given(inner) => inner.encrypt(data), |
| 141 | DefAlt::None => Err(err!( |
| 142 | "Can't encrypt, encrypter not specified."; |
| 143 | Configuration, Missing)), |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn decrypt(&self, data: &[u8]) -> Outcome<Vec<u8>> { |
| 148 | match &self.0 { |
| 149 | DefAlt::Default(inner) => inner.decrypt(data), |
| 150 | DefAlt::Given(inner) => inner.decrypt(data), |
| 151 | DefAlt::None => Err(err!( |
| 152 | "Can't decrypt, encrypter not specified."; |
| 153 | Configuration, Missing)), |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn is_identity(&self) -> bool { |
| 158 | match &self.0 { |
| 159 | DefAlt::Default(inner) => inner.is_identity(), |
| 160 | DefAlt::Given(inner) => inner.is_identity(), |
| 161 | DefAlt::None => true, |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | impl< |
| 167 | G: Encrypter, |
| 168 | D: Encrypter, |
| 169 | > |
| 170 | KeyManager for EncrypterDefAlt<D, G> |
| 171 | { |
| 172 | fn clone_with_keys(&self, pk: Option<&[u8]>, sk: Option<&[u8]>) -> Outcome<Self> { |
| 173 | Ok(match &self.0 { |
| 174 | DefAlt::Default(inner) => Self( |
| 175 | DefAlt::Default(res!(inner.clone_with_keys(pk, sk))), |
| 176 | ), |
| 177 | DefAlt::Given(inner) => Self( |
| 178 | DefAlt::Given(res!(inner.clone_with_keys(pk, sk))), |
| 179 | ), |
| 180 | DefAlt::None => Self( |
| 181 | DefAlt::None, // TODO should this be an error? |
| 182 | ), |
| 183 | }) |
| 184 | } |
| 185 | |
| 186 | fn get_public_key(&self) -> Outcome<Option<&[u8]>> { |
| 187 | match &self.0 { |
| 188 | DefAlt::Default(inner) => inner.get_public_key(), |
| 189 | DefAlt::Given(inner) => inner.get_public_key(), |
| 190 | DefAlt::None => Err(err!( |
| 191 | "Can't get public key, signature not specified."; |
| 192 | Configuration, Missing)), |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | fn get_secret_key(&self) -> Outcome<Option<&[u8]>> { |
| 197 | match &self.0 { |
| 198 | DefAlt::Default(inner) => inner.get_secret_key(), |
| 199 | DefAlt::Given(inner) => inner.get_secret_key(), |
| 200 | DefAlt::None => Err(err!( |
| 201 | "Can't get secret key, signature not specified."; |
| 202 | Configuration, Missing)), |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | fn set_public_key(self, pk: Option<&[u8]>) -> Outcome<Self> { |
| 207 | match self.0 { |
| 208 | DefAlt::Default(inner) => Ok(Self( |
| 209 | DefAlt::Default(res!(inner.set_public_key(pk))), |
| 210 | )), |
| 211 | DefAlt::Given(inner) => Ok(Self( |
| 212 | DefAlt::Given(res!(inner.set_public_key(pk))), |
| 213 | )), |
| 214 | DefAlt::None => Err(err!( |
| 215 | "Can't set public key, signature not specified."; |
| 216 | Configuration, Missing)), |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | fn set_secret_key(self, sk: Option<&[u8]>) -> Outcome<Self> { |
| 221 | match self.0 { |
| 222 | DefAlt::Default(inner) => Ok(Self( |
| 223 | DefAlt::Default(res!(inner.set_secret_key(sk))), |
| 224 | )), |
| 225 | DefAlt::Given(inner) => Ok(Self( |
| 226 | DefAlt::Given(res!(inner.set_secret_key(sk))), |
| 227 | )), |
| 228 | DefAlt::None => Err(err!( |
| 229 | "Can't set secret key, signature not specified."; |
| 230 | Configuration, Missing)), |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | impl< |
| 235 | D: Encrypter, |
| 236 | G: Encrypter, |
| 237 | > |
| 238 | EncrypterDefAlt<D, G> |
| 239 | { |
| 240 | /// Possibly override the hasher in `EncrypterDefAlt`. |
| 241 | pub fn or_encrypt( |
| 242 | &self, |
| 243 | data: &[u8], |
| 244 | or: Option<&Override<D, G>>, |
| 245 | ) |
| 246 | -> Outcome<Vec<u8>> |
| 247 | { |
| 248 | match or { |
| 249 | None | Some(Override::PassThrough) => self.encrypt(data), |
| 250 | Some(Override::Default(inner)) => inner.encrypt(data), |
| 251 | Some(Override::Given(inner)) => inner.encrypt(data), |
| 252 | Some(Override::None) => ().encrypt(data), |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | pub fn or_decrypt( |
| 257 | &self, |
| 258 | data: &[u8], |
| 259 | or: Option<&Override<D, G>>, |
| 260 | ) |
| 261 | -> Outcome<Vec<u8>> |
| 262 | { |
| 263 | match or { |
| 264 | None | Some(Override::PassThrough) => self.decrypt(data), |
| 265 | Some(Override::Default(inner)) => inner.decrypt(data), |
| 266 | Some(Override::Given(inner)) => inner.decrypt(data), |
| 267 | Some(Override::None) => ().decrypt(data), |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | pub fn or_is_identity( |
| 272 | &self, |
| 273 | or: Option<&Override<D, G>>, |
| 274 | ) |
| 275 | -> bool |
| 276 | { |
| 277 | match or { |
| 278 | None | Some(Override::PassThrough) => self.is_identity(), |
| 279 | Some(Override::Default(inner)) => inner.is_identity(), |
| 280 | Some(Override::Given(inner)) => inner.is_identity(), |
| 281 | Some(Override::None) => ().is_identity(), |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | pub fn clone_from_or( |
| 286 | &self, |
| 287 | or: Option<&Override<D, G>>, |
| 288 | ) |
| 289 | -> Self |
| 290 | { |
| 291 | match or { |
| 292 | None | Some(Override::PassThrough) => self.clone(), |
| 293 | Some(Override::Default(inner)) => Self(DefAlt::Default(inner.clone())), |
| 294 | Some(Override::Given(inner)) => Self(DefAlt::Given(inner.clone())), |
| 295 | Some(Override::None) => Self(DefAlt::None), |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | pub fn or_debug( |
| 300 | &self, |
| 301 | or: Option<&Override<D, G>>, |
| 302 | ) |
| 303 | -> String |
| 304 | { |
| 305 | match or { |
| 306 | None | Some(Override::PassThrough) => fmt!("{:?}", self), |
| 307 | _ => fmt!("{:?}", or), |
| 308 | } |
| 309 | } |
| 310 | } |