LCOV - code coverage report
Current view: top level - proxy/src/scram - pbkdf2.rs (source / functions) Coverage Total Hit
Test: 52d9d4a58355424a48c56cb9ba9670a073f618b9.info Lines: 100.0 % 56 56
Test Date: 2024-11-21 08:31:22 Functions: 100.0 % 4 4

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