@ -49,16 +49,9 @@ namespace crypto {
# if defined(__cplusplus)
# if defined(__cplusplus)
}
}
# pragma pack(push, 1)
using chacha8_key = tools : : scrubbed_arr < uint8_t , CHACHA8_KEY_SIZE > ;
struct chacha8_key {
uint8_t data [ CHACHA8_KEY_SIZE ] ;
~ chacha8_key ( )
{
memwipe ( data , sizeof ( data ) ) ;
}
} ;
# pragma pack(push, 1)
// MS VC 2012 doesn't interpret `class chacha8_iv` as POD in spite of [9.0.10], so it is a struct
// MS VC 2012 doesn't interpret `class chacha8_iv` as POD in spite of [9.0.10], so it is a struct
struct chacha8_iv {
struct chacha8_iv {
uint8_t data [ CHACHA8_IV_SIZE ] ;
uint8_t data [ CHACHA8_IV_SIZE ] ;
@ -68,15 +61,14 @@ namespace crypto {
static_assert ( sizeof ( chacha8_key ) = = CHACHA8_KEY_SIZE & & sizeof ( chacha8_iv ) = = CHACHA8_IV_SIZE , " Invalid structure size " ) ;
static_assert ( sizeof ( chacha8_key ) = = CHACHA8_KEY_SIZE & & sizeof ( chacha8_iv ) = = CHACHA8_IV_SIZE , " Invalid structure size " ) ;
inline void chacha8 ( const void * data , std : : size_t length , const chacha8_key & key , const chacha8_iv & iv , char * cipher ) {
inline void chacha8 ( const void * data , std : : size_t length , const chacha8_key & key , const chacha8_iv & iv , char * cipher ) {
chacha8 ( data , length , reinterpret_cast< const uint8_t * > ( & key ) , reinterpret_cast < const uint8_t * > ( & iv ) , cipher ) ;
chacha8 ( data , length , key. data ( ) , reinterpret_cast < const uint8_t * > ( & iv ) , cipher ) ;
}
}
inline void generate_chacha8_key ( const void * data , size_t size , chacha8_key & key ) {
inline void generate_chacha8_key ( const void * data , size_t size , chacha8_key & key ) {
static_assert ( sizeof ( chacha8_key ) < = sizeof ( hash ) , " Size of hash must be at least that of chacha8_key " ) ;
static_assert ( sizeof ( chacha8_key ) < = sizeof ( hash ) , " Size of hash must be at least that of chacha8_key " ) ;
char pwd_hash [ HASH_SIZE ] ;
tools : : scrubbed_arr < char , HASH_SIZE > pwd_hash ;
crypto : : cn_slow_hash ( data , size , pwd_hash ) ;
crypto : : cn_slow_hash ( data , size , pwd_hash . data ( ) ) ;
memcpy ( & key , pwd_hash , sizeof ( key ) ) ;
memcpy ( & key , pwd_hash . data ( ) , sizeof ( key ) ) ;
memwipe ( pwd_hash , sizeof ( pwd_hash ) ) ;
}
}
inline void generate_chacha8_key ( std : : string password , chacha8_key & key ) {
inline void generate_chacha8_key ( std : : string password , chacha8_key & key ) {