Repo of no-std crates for my personal embedded projects
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Improve mix_nonce

+18 -19
+18 -19
sachy-crypto/src/lib.rs
··· 143 143 msg: &mut alloc::vec::Vec<u8>, 144 144 associated_data: &[u8], 145 145 ) -> Result<(), ProtoError> { 146 + if self.counter.ct_eq(&TransportState::COUNTER_MAX).into() { 147 + return Err(ProtoError); 148 + } 149 + 146 150 let counter = self.counter.to_be_bytes(); 147 151 148 152 self.transport.aead.encrypt_in_place( ··· 153 157 154 158 self.counter = self.counter.wrapping_add(TransportState::COUNTER_INCR); 155 159 156 - if self.counter.ct_eq(&TransportState::COUNTER_MAX).into() { 157 - Err(ProtoError) 158 - } else { 159 - Ok(()) 160 - } 160 + Ok(()) 161 161 } 162 162 } 163 163 ··· 172 172 msg: &mut alloc::vec::Vec<u8>, 173 173 associated_data: &[u8], 174 174 ) -> Result<(), ProtoError> { 175 + if self.counter.ct_eq(&TransportState::COUNTER_MAX).into() { 176 + return Err(ProtoError); 177 + } 178 + 175 179 let counter = self.counter.to_be_bytes(); 176 180 177 181 self.transport.aead.decrypt_in_place( ··· 182 186 183 187 self.counter = self.counter.wrapping_add(TransportState::COUNTER_INCR); 184 188 185 - if self.counter.ct_eq(&TransportState::COUNTER_MAX).into() { 186 - Err(ProtoError) 187 - } else { 188 - Ok(()) 189 - } 189 + Ok(()) 190 190 } 191 191 } 192 192 ··· 272 272 273 273 let epstein = self.select_nonce_context(send); 274 274 275 - let (head, tail) = trump.split_at_mut(position.len()); 276 - let (first, second) = epstein.split_at(position.len()); 275 + let trump_len = trump.len(); 277 276 278 - // XOR the base nonce with position bytes, copying them to the derived nonce 279 - head.iter_mut() 280 - .zip(first) 281 - .zip(position) 282 - .for_each(|((head, ep), pos)| *head = ep ^ pos); 277 + // Copy position bytes into BE format onto derived nonce 278 + trump[trump_len - position.len()..].copy_from_slice(position); 283 279 284 - // Copy rest of base nonce into derived nonce 285 - tail.copy_from_slice(second); 280 + // XOR the base nonce onto the derived nonce bytes 281 + trump 282 + .iter_mut() 283 + .zip(epstein) 284 + .for_each(|(trump, epstein)| *trump ^= epstein); 286 285 287 286 trump 288 287 }