@ -1123,45 +1123,43 @@ bool generic_salt_decode (MAYBE_UNUSED const hashconfig_t *hashconfig, const u8
{
{
u32 tmp_u32 [ ( 64 * 2 ) + 1 ] = { 0 } ;
u32 tmp_u32 [ ( 64 * 2 ) + 1 ] = { 0 } ;
if ( in_len > 512 ) return false ; // 512 = 2 * 256 -- (2 * because of hex), 256 because of maximum salt length in salt_t
u8 * tmp_u8 = ( u8 * ) tmp_u32 ;
if ( hashconfig - > opts_type & OPTS_TYPE_ST_HEX )
{
if ( in_len < ( int ) ( hashconfig - > salt_min * 2 ) ) return false ;
if ( in_len > ( int ) ( hashconfig - > salt_max * 2 ) ) return false ;
}
else
{
if ( in_len < ( int ) hashconfig - > salt_min ) return false ;
if ( in_len > ( int ) hashconfig - > salt_max ) return false ;
}
u8 * tmp_buf = ( u8 * ) tmp_u32 ;
if ( in_len > 512 ) return false ; // 512 = 2 * 256 -- (2 * because of hex), 256 because of maximum salt length in salt_t
int tmp_len = 0 ;
int tmp_len = 0 ;
if ( hashconfig - > opts_type & OPTS_TYPE_ST_HEX )
if ( hashconfig - > opts_type & OPTS_TYPE_ST_HEX )
{
{
if ( tmp_len & 1 ) return false ;
if ( in_len < ( int ) ( hashconfig - > salt_min * 2 ) ) return false ;
if ( in_len > ( int ) ( hashconfig - > salt_max * 2 ) ) return false ;
tmp_len = in_len / 2 ;
if ( in_len & 1 ) return false ;
for ( int i = 0 , j = 0 ; i < tmp _len; i + = 1 , j + = 2 )
for ( int i = 0 , j = 0 ; j < in _len; i + = 1 , j + = 2 )
{
{
u8 p0 = in_buf [ j + 0 ] ;
u8 p0 = in_buf [ j + 0 ] ;
u8 p1 = in_buf [ j + 1 ] ;
u8 p1 = in_buf [ j + 1 ] ;
tmp_ buf [ i ] = hex_convert ( p1 ) < < 0 ;
tmp_ u8 [ i ] = hex_convert ( p1 ) < < 0 ;
tmp_ buf [ i ] | = hex_convert ( p0 ) < < 4 ;
tmp_ u8 [ i ] | = hex_convert ( p0 ) < < 4 ;
}
}
tmp_len = in_len / 2 ;
}
}
else if ( hashconfig - > opts_type & OPTS_TYPE_ST_BASE64 )
else if ( hashconfig - > opts_type & OPTS_TYPE_ST_BASE64 )
{
{
tmp_len = base64_decode ( base64_to_int , ( const u8 * ) in_buf , in_len , tmp_buf ) ;
if ( in_len < ( int ) ( ( hashconfig - > salt_min * 8 ) / 6 ) ) return false ;
if ( in_len > ( int ) ( ( hashconfig - > salt_max * 8 ) / 6 ) ) return false ;
tmp_len = base64_decode ( base64_to_int , ( const u8 * ) in_buf , in_len , tmp_u8 ) ;
}
}
else
else
{
{
if ( in_len ) memcpy ( tmp_buf , in_buf , in_len ) ;
if ( in_len < ( int ) hashconfig - > salt_min ) return false ;
if ( in_len > ( int ) hashconfig - > salt_max ) return false ;
memcpy ( tmp_u8 , in_buf , in_len ) ;
tmp_len = in_len ;
tmp_len = in_len ;
}
}
@ -1183,12 +1181,12 @@ bool generic_salt_decode (MAYBE_UNUSED const hashconfig_t *hashconfig, const u8
if ( hashconfig - > opts_type & OPTS_TYPE_ST_LOWER )
if ( hashconfig - > opts_type & OPTS_TYPE_ST_LOWER )
{
{
lowercase ( tmp_ buf , tmp_len ) ;
lowercase ( tmp_ u8 , tmp_len ) ;
}
}
if ( hashconfig - > opts_type & OPTS_TYPE_ST_UPPER )
if ( hashconfig - > opts_type & OPTS_TYPE_ST_UPPER )
{
{
uppercase ( tmp_ buf , tmp_len ) ;
uppercase ( tmp_ u8 , tmp_len ) ;
}
}
int tmp2_len = tmp_len ;
int tmp2_len = tmp_len ;
@ -1197,40 +1195,62 @@ bool generic_salt_decode (MAYBE_UNUSED const hashconfig_t *hashconfig, const u8
{
{
if ( tmp2_len > = 256 ) return false ;
if ( tmp2_len > = 256 ) return false ;
tmp_ buf [ tmp2_len + + ] = 0x80 ;
tmp_ u8 [ tmp2_len + + ] = 0x80 ;
}
}
if ( hashconfig - > opts_type & OPTS_TYPE_ST_ADD01 )
if ( hashconfig - > opts_type & OPTS_TYPE_ST_ADD01 )
{
{
if ( tmp2_len > = 256 ) return false ;
if ( tmp2_len > = 256 ) return false ;
tmp_ buf [ tmp2_len + + ] = 0x01 ;
tmp_ u8 [ tmp2_len + + ] = 0x01 ;
}
}
if ( hashconfig - > opts_type & OPTS_TYPE_ST_GENERATE_BE )
memcpy ( out_buf , tmp_u8 , tmp2_len ) ;
{
u32 max = tmp2_len / 4 ;
if ( tmp2_len % 4 ) max + + ;
* out_len = tmp_len ;
for ( u32 i = 0 ; i < max ; i + + )
return true ;
}
int generic_salt_encode ( MAYBE_UNUSED const hashconfig_t * hashconfig , const u8 * in_buf , const int in_len , u8 * out_buf )
{
u32 tmp_u32 [ ( 64 * 2 ) + 1 ] = { 0 } ;
u8 * tmp_u8 = ( u8 * ) tmp_u32 ;
memcpy ( tmp_u8 , in_buf , in_len ) ;
int tmp_len = in_len ;
if ( hashconfig - > opts_type & OPTS_TYPE_ST_UTF16LE )
{
for ( int i = 0 , j = 0 ; j < in_len ; i + = 1 , j + = 2 )
{
{
tmp_u32 [ i ] = byte_swap_32 ( tmp_u32 [ i ] ) ;
const u8 p = tmp_u8 [ j ] ;
tmp_u8 [ i ] = p ;
}
}
// Important: we may need to increase the length of memcpy since
tmp_len = tmp_len / 2 ;
// we don't want to "loose" some swapped bytes (could happen if
}
// they do not perfectly fit in the 4-byte blocks)
// Memcpy does always copy the bytes in the BE order, but since
if ( hashconfig - > opts_type & OPTS_TYPE_ST_HEX )
// we swapped them, some important bytes could be in positions
{
// we normally skip with the original len
for ( int i = 0 , j = 0 ; i < in_len ; i + = 1 , j + = 2 )
{
const u8 p = in_buf [ i ] ;
if ( tmp2_len % 4 ) tmp2_len + = 4 - ( tmp2_len % 4 ) ;
u8_to_hex ( in_buf [ i ] , tmp_u8 + j ) ;
}
}
memcpy ( out_buf , tmp_buf , tmp2_len ) ;
tmp_len = in_len * 2 ;
}
else if ( hashconfig - > opts_type & OPTS_TYPE_ST_BASE64 )
{
tmp_len = base64_encode ( int_to_base64 , in_buf , in_len , tmp_u8 ) ;
}
* out_len = tmp_len ;
memcpy ( out_buf , tmp_u8 , tmp_len ) ;
return true ;
return tmp_len ;
}
}