oxedyne/fe2o3/fe2o3_shield/src/srv/schemes.rs
6.3 KiB, 140 runs
created by r1870400018:888, 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 oxedyne_fe2o3_core::{ |
| 2 | prelude::*, |
| 3 | alt::{ |
| 4 | Alt, |
| 5 | DefAlt, |
| 6 | }, |
| 7 | }; |
| 8 | |
| 9 | use oxedyne_fe2o3_crypto::{ |
| 10 | enc::EncryptionScheme, |
| 11 | sign::SignatureScheme, |
| 12 | }; |
| 13 | use oxedyne_fe2o3_iop_crypto::{ |
| 14 | enc::{ |
| 15 | Encrypter, |
| 16 | EncrypterDefAlt, |
| 17 | }, |
| 18 | sign::{ |
| 19 | Signer, |
| 20 | SignerDefAlt, |
| 21 | }, |
| 22 | }; |
| 23 | use oxedyne_fe2o3_jdat::{ |
| 24 | chunk::{ |
| 25 | //Chunker, |
| 26 | ChunkConfig, |
| 27 | }, |
| 28 | //daticle::Daticle, |
| 29 | }; |
| 30 | use oxedyne_fe2o3_hash::{ |
| 31 | csum::{ |
| 32 | ChecksummerDefAlt, |
| 33 | ChecksumScheme, |
| 34 | }, |
| 35 | hash::{ |
| 36 | HasherDefAlt, |
| 37 | HashScheme, |
| 38 | }, |
| 39 | //kdf::{ |
| 40 | // KeyDerivationScheme, |
| 41 | // KeyDeriverDefAlt, |
| 42 | //}, |
| 43 | }; |
| 44 | use oxedyne_fe2o3_iop_hash::{ |
| 45 | api::Hasher, |
| 46 | csum::Checksummer, |
| 47 | //kdf::KeyDeriver, |
| 48 | }; |
| 49 | |
| 50 | use std::fmt; |
| 51 | |
| 52 | |
| 53 | pub trait WireSchemeTypes: Clone + fmt::Debug { |
| 54 | type ENC: Encrypter; |
| 55 | type CS: Checksummer; |
| 56 | type POWH: Hasher; |
| 57 | type SGN: Signer; |
| 58 | type HS: Encrypter; // Handshake encryption. |
| 59 | } |
| 60 | |
| 61 | #[derive(Clone, Debug)] |
| 62 | pub struct DefaultWireSchemes; |
| 63 | |
| 64 | impl WireSchemeTypes for DefaultWireSchemes { |
| 65 | type ENC = EncryptionScheme; |
| 66 | type CS = ChecksumScheme; |
| 67 | type POWH = HashScheme; |
| 68 | type SGN = SignatureScheme; |
| 69 | type HS = EncryptionScheme; |
| 70 | } |
| 71 | |
| 72 | #[derive(Clone, Debug)] |
| 73 | pub struct WireSchemesInput<W: WireSchemeTypes> { |
| 74 | pub enc: Alt<W::ENC>, |
| 75 | pub csum: Alt<W::CS>, |
| 76 | pub powh: Alt<W::POWH>, |
| 77 | pub sign: Alt<W::SGN>, |
| 78 | pub hsenc: Alt<W::HS>, |
| 79 | pub chnk: Option<ChunkConfig>, |
| 80 | } |
| 81 | |
| 82 | impl<W: WireSchemeTypes> |
| 83 | Default for WireSchemesInput<W> |
| 84 | { |
| 85 | fn default() -> Self { |
| 86 | Self { |
| 87 | enc: Alt::Unspecified, |
| 88 | csum: Alt::Unspecified, |
| 89 | powh: Alt::Unspecified, |
| 90 | sign: Alt::Unspecified, |
| 91 | hsenc: Alt::Unspecified, |
| 92 | chnk: None, |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl<W: WireSchemeTypes> |
| 98 | WireSchemesInput<W> |
| 99 | { |
| 100 | pub fn ref_encrypter(&self) -> &Alt<W::ENC> { &self.enc } |
| 101 | pub fn ref_checksummer(&self) -> &Alt<W::CS> { &self.csum } |
| 102 | pub fn ref_pow_hasher(&self) -> &Alt<W::POWH> { &self.powh } |
| 103 | pub fn ref_signer(&self) -> &Alt<W::SGN> { &self.sign } |
| 104 | pub fn ref_handshake_encrypter(&self) -> &Alt<W::HS> { &self.hsenc } |
| 105 | pub fn ref_chunk_config(&self) -> &Option<ChunkConfig> { &self.chnk } |
| 106 | |
| 107 | pub fn own_encrypter(&mut self) -> Alt<W::ENC> { std::mem::replace(&mut self.enc, Alt::Unspecified) } |
| 108 | pub fn own_checksummer(&mut self) -> Alt<W::CS> { std::mem::replace(&mut self.csum, Alt::Unspecified) } |
| 109 | pub fn own_pow_hasher(&mut self) -> Alt<W::POWH> { std::mem::replace(&mut self.powh, Alt::Unspecified) } |
| 110 | pub fn own_signer(&mut self) -> Alt<W::SGN> { std::mem::replace(&mut self.sign, Alt::Unspecified) } |
| 111 | pub fn own_handshake_encrypter(&mut self) -> Alt<W::HS> { std::mem::replace(&mut self.hsenc, Alt::Unspecified) } |
| 112 | pub fn own_chunk_config(&mut self) -> Option<ChunkConfig> { std::mem::replace(&mut self.chnk, None) } |
| 113 | |
| 114 | pub fn clone_encrypter(&self) -> Alt<W::ENC> { self.enc.clone() } |
| 115 | pub fn clone_checksummer(&self) -> Alt<W::CS> { self.csum.clone() } |
| 116 | pub fn clone_pow_hasher(&self) -> Alt<W::POWH> { self.powh.clone() } |
| 117 | pub fn clone_signer(&self) -> Alt<W::SGN> { self.sign.clone() } |
| 118 | pub fn clone_handshake_encrypter(&self) -> Alt<W::HS> { self.hsenc.clone() } |
| 119 | pub fn clone_chunk_config(&self) -> Option<ChunkConfig> { self.chnk.clone() } |
| 120 | |
| 121 | pub fn set_signer(mut self, signer: Option<W::SGN>) -> Self { |
| 122 | self.sign = Alt::Specific(signer); |
| 123 | self |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[derive(Clone, Debug)] |
| 128 | pub struct WireSchemes<W: WireSchemeTypes> { |
| 129 | pub enc: EncrypterDefAlt<EncryptionScheme, W::ENC>, |
| 130 | pub csum: ChecksummerDefAlt<ChecksumScheme, W::CS>, |
| 131 | pub powh: HasherDefAlt<HashScheme, W::POWH>, |
| 132 | pub sign: SignerDefAlt<SignatureScheme, W::SGN>, |
| 133 | pub hsenc: EncrypterDefAlt<EncryptionScheme, W::HS>, |
| 134 | pub chnk: ChunkConfig, |
| 135 | } |
| 136 | |
| 137 | impl<W: WireSchemeTypes> |
| 138 | Default for WireSchemes<W> |
| 139 | { |
| 140 | fn default() -> Self { |
| 141 | Self { |
| 142 | enc: EncrypterDefAlt(DefAlt::None), |
| 143 | csum: ChecksummerDefAlt(DefAlt::None), |
| 144 | powh: HasherDefAlt(DefAlt::None), |
| 145 | sign: SignerDefAlt(DefAlt::None), |
| 146 | hsenc: EncrypterDefAlt(DefAlt::None), |
| 147 | chnk: ChunkConfig::default(), |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | impl<W: WireSchemeTypes> |
| 153 | From<WireSchemesInput<W>> for WireSchemes<W> |
| 154 | { |
| 155 | fn from(input: WireSchemesInput<W>) -> Self { |
| 156 | Self { |
| 157 | enc: EncrypterDefAlt::from(input.enc), |
| 158 | csum: ChecksummerDefAlt::from(input.csum), |
| 159 | powh: HasherDefAlt::from(input.powh), |
| 160 | sign: SignerDefAlt::from(input.sign), |
| 161 | hsenc: EncrypterDefAlt::from(input.hsenc), |
| 162 | chnk: match input.chnk { |
| 163 | Some(cfg) => cfg, |
| 164 | None => ChunkConfig::default(), |
| 165 | }, |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl<W: WireSchemeTypes> |
| 171 | WireSchemes<W> |
| 172 | { |
| 173 | pub fn ref_encrypter(&self) -> &EncrypterDefAlt<EncryptionScheme, W::ENC> { &self.enc } |
| 174 | pub fn ref_checksummer(&self) -> &ChecksummerDefAlt<ChecksumScheme, W::CS> { &self.csum } |
| 175 | pub fn ref_pow_hasher(&self) -> &HasherDefAlt<HashScheme, W::POWH> { &self.powh } |
| 176 | pub fn ref_signer(&self) -> &SignerDefAlt<SignatureScheme, W::SGN> { &self.sign } |
| 177 | pub fn ref_handshake_encrypter(&self) -> &EncrypterDefAlt<EncryptionScheme, W::HS> { &self.hsenc } |
| 178 | pub fn ref_chunk_config(&self) -> &ChunkConfig { &self.chnk } |
| 179 | |
| 180 | pub fn clone_encrypter(&self) -> EncrypterDefAlt<EncryptionScheme, W::ENC> { self.enc.clone() } |
| 181 | pub fn clone_checksummer(&self) -> ChecksummerDefAlt<ChecksumScheme, W::CS> { self.csum.clone() } |
| 182 | pub fn clone_pow_hasher(&self) -> HasherDefAlt<HashScheme, W::POWH> { self.powh.clone() } |
| 183 | pub fn clone_signer(&self) -> SignerDefAlt<SignatureScheme, W::SGN> { self.sign.clone() } |
| 184 | pub fn clone_handshake_encrypter(&self) -> EncrypterDefAlt<EncryptionScheme, W::HS> { self.hsenc.clone() } |
| 185 | pub fn clone_chunk_config(&self) -> ChunkConfig { self.chnk.clone() } |
| 186 | |
| 187 | //pub fn set_chunk_config(mut self, chnk: Option<ChunkConfig>) -> Self { |
| 188 | // self.chnk = chnk; |
| 189 | // self |
| 190 | //} |
| 191 | } |