@@ -14,6 +14,7 @@ use crate::backend::hmac::Hmac;
1414use crate :: buf:: { CffiBuf , CffiMutBuf } ;
1515use crate :: error:: { CryptographyError , CryptographyResult } ;
1616use crate :: exceptions;
17+ use crate :: types;
1718
1819// NO-COVERAGE-START
1920#[ pyo3:: pyclass(
@@ -1709,14 +1710,6 @@ struct KbkdfHmac {
17091710 used : bool ,
17101711}
17111712
1712- fn int_to_bytes ( value : usize , length : usize ) -> Vec < u8 > {
1713- let mut bytes = Vec :: with_capacity ( length) ;
1714- for i in ( 0 ..length) . rev ( ) {
1715- bytes. push ( ( ( value >> ( i * 8 ) ) & 0xff ) as u8 ) ;
1716- }
1717- bytes
1718- }
1719-
17201713#[ allow( clippy:: enum_variant_names) ]
17211714#[ derive( PartialEq ) ]
17221715enum CounterLocation {
@@ -1877,9 +1870,10 @@ impl KbkdfHmac {
18771870 for i in 1 ..=rounds {
18781871 let mut hmac = Hmac :: new_bytes ( py, key_material, algorithm_bound) ?;
18791872
1880- let counter = int_to_bytes ( i, self . params . rlen ) ;
1873+ let py_counter = types:: INT_TO_BYTES . get ( py) ?. call1 ( ( i, self . params . rlen ) ) ?;
1874+ let counter = py_counter. extract :: < & [ u8 ] > ( ) ?;
18811875 hmac. update_bytes ( data_before_ctr) ?;
1882- hmac. update_bytes ( & counter) ?;
1876+ hmac. update_bytes ( counter) ?;
18831877 hmac. update_bytes ( data_after_ctr) ?;
18841878
18851879 let result = hmac. finalize_bytes ( ) ?;
@@ -1898,15 +1892,18 @@ impl KbkdfHmac {
18981892 }
18991893
19001894 // llen will exist if fixed data is not provided
1901- let l_val = int_to_bytes ( self . length * 8 , self . params . llen . unwrap ( ) ) ;
1895+ let py_l_val = types:: INT_TO_BYTES
1896+ . get ( py) ?
1897+ . call1 ( ( self . length * 8 , self . params . llen . unwrap ( ) ) ) ?;
1898+ let l_val = py_l_val. extract :: < & [ u8 ] > ( ) ?;
19021899
19031900 let mut result = Vec :: new ( ) ;
19041901 let label: & [ u8 ] = self . params . label . as_ref ( ) . map_or ( b"" , |l| l. as_bytes ( py) ) ;
19051902 result. extend_from_slice ( label) ;
19061903 result. push ( 0x00 ) ;
19071904 let context: & [ u8 ] = self . params . context . as_ref ( ) . map_or ( b"" , |l| l. as_bytes ( py) ) ;
19081905 result. extend_from_slice ( context) ;
1909- result. extend_from_slice ( & l_val) ;
1906+ result. extend_from_slice ( l_val) ;
19101907
19111908 Ok ( result)
19121909 }
0 commit comments