2019-11-08 13:22:28 +01:00
// SPDX-License-Identifier: GPL-2.0 OR MIT
/*
* Copyright ( C ) 2015 - 2019 Jason A . Donenfeld < Jason @ zx2c4 . com > . All Rights Reserved .
*
* This is an implementation of the BLAKE2s hash and PRF functions .
*
* Information : https : //blake2.net/
*
*/
# include <crypto/internal/blake2s.h>
# include <linux/types.h>
# include <linux/string.h>
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/init.h>
# include <linux/bug.h>
2020-12-23 00:09:53 -08:00
2022-05-28 21:44:07 +02:00
static inline void blake2s_set_lastblock ( struct blake2s_state * state )
{
state - > f [ 0 ] = - 1 ;
}
2019-11-08 13:22:28 +01:00
void blake2s_update ( struct blake2s_state * state , const u8 * in , size_t inlen )
{
2022-05-28 21:44:07 +02:00
const size_t fill = BLAKE2S_BLOCK_SIZE - state - > buflen ;
if ( unlikely ( ! inlen ) )
return ;
if ( inlen > fill ) {
memcpy ( state - > buf + state - > buflen , in , fill ) ;
blake2s_compress ( state , state - > buf , 1 , BLAKE2S_BLOCK_SIZE ) ;
state - > buflen = 0 ;
in + = fill ;
inlen - = fill ;
}
if ( inlen > BLAKE2S_BLOCK_SIZE ) {
const size_t nblocks = DIV_ROUND_UP ( inlen , BLAKE2S_BLOCK_SIZE ) ;
blake2s_compress ( state , in , nblocks - 1 , BLAKE2S_BLOCK_SIZE ) ;
in + = BLAKE2S_BLOCK_SIZE * ( nblocks - 1 ) ;
inlen - = BLAKE2S_BLOCK_SIZE * ( nblocks - 1 ) ;
}
memcpy ( state - > buf + state - > buflen , in , inlen ) ;
state - > buflen + = inlen ;
2019-11-08 13:22:28 +01:00
}
EXPORT_SYMBOL ( blake2s_update ) ;
void blake2s_final ( struct blake2s_state * state , u8 * out )
{
WARN_ON ( IS_ENABLED ( DEBUG ) & & ! out ) ;
2022-05-28 21:44:07 +02:00
blake2s_set_lastblock ( state ) ;
memset ( state - > buf + state - > buflen , 0 ,
BLAKE2S_BLOCK_SIZE - state - > buflen ) ; /* Padding */
blake2s_compress ( state , state - > buf , 1 , state - > buflen ) ;
cpu_to_le32_array ( state - > h , ARRAY_SIZE ( state - > h ) ) ;
memcpy ( out , state - > h , state - > outlen ) ;
2019-11-08 13:22:28 +01:00
memzero_explicit ( state , sizeof ( * state ) ) ;
}
EXPORT_SYMBOL ( blake2s_final ) ;
2021-07-11 15:31:45 -07:00
static int __init blake2s_mod_init ( void )
2019-11-08 13:22:28 +01:00
{
if ( ! IS_ENABLED ( CONFIG_CRYPTO_MANAGER_DISABLE_TESTS ) & &
WARN_ON ( ! blake2s_selftest ( ) ) )
return - ENODEV ;
return 0 ;
}
2021-07-11 15:31:45 -07:00
module_init ( blake2s_mod_init ) ;
2019-11-08 13:22:28 +01:00
MODULE_LICENSE ( " GPL v2 " ) ;
MODULE_DESCRIPTION ( " BLAKE2s hash function " ) ;
MODULE_AUTHOR ( " Jason A. Donenfeld <Jason@zx2c4.com> " ) ;