LCOV - code coverage report
Current view: top level - proxy/src/scram - pbkdf2.rs (source / functions) Coverage Total Hit
Test: a43a77853355b937a79c57b07a8f05607cf29e6c.info Lines: 100.0 % 56 56
Test Date: 2024-09-19 12:04:32 Functions: 100.0 % 4 4

            Line data    Source code
       1              : use hmac::{
       2              :     digest::{consts::U32, generic_array::GenericArray},
       3              :     Hmac, Mac,
       4              : };
       5              : use sha2::Sha256;
       6              : 
       7              : pub(crate) struct Pbkdf2 {
       8              :     hmac: Hmac<Sha256>,
       9              :     prev: GenericArray<u8, U32>,
      10              :     hi: GenericArray<u8, U32>,
      11              :     iterations: u32,
      12              : }
      13              : 
      14              : // inspired from <https://github.com/neondatabase/rust-postgres/blob/20031d7a9ee1addeae6e0968e3899ae6bf01cee2/postgres-protocol/src/authentication/sasl.rs#L36-L61>
      15              : impl Pbkdf2 {
      16            6 :     pub(crate) fn start(str: &[u8], salt: &[u8], iterations: u32) -> Self {
      17            6 :         let hmac =
      18            6 :             Hmac::<Sha256>::new_from_slice(str).expect("HMAC is able to accept all key sizes");
      19            6 : 
      20            6 :         let prev = hmac
      21            6 :             .clone()
      22            6 :             .chain_update(salt)
      23            6 :             .chain_update(1u32.to_be_bytes())
      24            6 :             .finalize()
      25            6 :             .into_bytes();
      26            6 : 
      27            6 :         Self {
      28            6 :             hmac,
      29            6 :             // one consumed for the hash above
      30            6 :             iterations: iterations - 1,
      31            6 :             hi: prev,
      32            6 :             prev,
      33            6 :         }
      34            6 :     }
      35              : 
      36            8 :     pub(crate) fn cost(&self) -> u32 {
      37            8 :         (self.iterations).clamp(0, 4096)
      38            8 :     }
      39              : 
      40           20 :     pub(crate) fn turn(&mut self) -> std::task::Poll<[u8; 32]> {
      41           20 :         let Self {
      42           20 :             hmac,
      43           20 :             prev,
      44           20 :             hi,
      45           20 :             iterations,
      46           20 :         } = self;
      47           20 : 
      48           20 :         // only do 4096 iterations per turn before sharing the thread for fairness
      49           20 :         let n = (*iterations).clamp(0, 4096);
      50           20 :         for _ in 0..n {
      51        80474 :             *prev = hmac.clone().chain_update(*prev).finalize().into_bytes();
      52              : 
      53      2575168 :             for (hi, prev) in hi.iter_mut().zip(*prev) {
      54      2575168 :                 *hi ^= prev;
      55      2575168 :             }
      56              :         }
      57              : 
      58           20 :         *iterations -= n;
      59           20 :         if *iterations == 0 {
      60            6 :             std::task::Poll::Ready((*hi).into())
      61              :         } else {
      62           14 :             std::task::Poll::Pending
      63              :         }
      64           20 :     }
      65              : }
      66              : 
      67              : #[cfg(test)]
      68              : mod tests {
      69              :     use super::Pbkdf2;
      70              :     use pbkdf2::pbkdf2_hmac_array;
      71              :     use sha2::Sha256;
      72              : 
      73              :     #[test]
      74            1 :     fn works() {
      75            1 :         let salt = b"sodium chloride";
      76            1 :         let pass = b"Ne0n_!5_50_C007";
      77            1 : 
      78            1 :         let mut job = Pbkdf2::start(pass, salt, 60000);
      79            1 :         let hash = loop {
      80           15 :             let std::task::Poll::Ready(hash) = job.turn() else {
      81           14 :                 continue;
      82              :             };
      83            1 :             break hash;
      84            1 :         };
      85            1 : 
      86            1 :         let expected = pbkdf2_hmac_array::<Sha256, 32>(pass, salt, 60000);
      87            1 :         assert_eq!(hash, expected);
      88            1 :     }
      89              : }
        

Generated by: LCOV version 2.1-beta