Path: blob/master/Utilities/cmlibarchive/libarchive/archive_blake2_impl.h
3153 views
/*1BLAKE2 reference source code package - reference C implementations23Copyright 2012, Samuel Neves <[email protected]>. You may use this under the4terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at5your option. The terms of these licenses can be found at:67- CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.08- OpenSSL license : https://www.openssl.org/source/license.html9- Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.01011More information about the BLAKE2 hash function can be found at12https://blake2.net.13*/1415#ifndef ARCHIVE_BLAKE2_IMPL_H16#define ARCHIVE_BLAKE2_IMPL_H1718#include <stdint.h>19#include <string.h>2021#if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)22#if defined(_MSC_VER)23#define BLAKE2_INLINE __inline24#elif defined(__GNUC__)25#define BLAKE2_INLINE __inline__26#else27#define BLAKE2_INLINE28#endif29#else30#define BLAKE2_INLINE inline31#endif3233static BLAKE2_INLINE uint32_t load32( const void *src )34{35#if defined(NATIVE_LITTLE_ENDIAN)36uint32_t w;37memcpy(&w, src, sizeof w);38return w;39#else40const uint8_t *p = ( const uint8_t * )src;41return (( uint32_t )( p[0] ) << 0) |42(( uint32_t )( p[1] ) << 8) |43(( uint32_t )( p[2] ) << 16) |44(( uint32_t )( p[3] ) << 24) ;45#endif46}4748static BLAKE2_INLINE uint64_t load64( const void *src )49{50#if defined(NATIVE_LITTLE_ENDIAN)51uint64_t w;52memcpy(&w, src, sizeof w);53return w;54#else55const uint8_t *p = ( const uint8_t * )src;56return (( uint64_t )( p[0] ) << 0) |57(( uint64_t )( p[1] ) << 8) |58(( uint64_t )( p[2] ) << 16) |59(( uint64_t )( p[3] ) << 24) |60(( uint64_t )( p[4] ) << 32) |61(( uint64_t )( p[5] ) << 40) |62(( uint64_t )( p[6] ) << 48) |63(( uint64_t )( p[7] ) << 56) ;64#endif65}6667static BLAKE2_INLINE uint16_t load16( const void *src )68{69#if defined(NATIVE_LITTLE_ENDIAN)70uint16_t w;71memcpy(&w, src, sizeof w);72return w;73#else74const uint8_t *p = ( const uint8_t * )src;75return ( uint16_t )((( uint32_t )( p[0] ) << 0) |76(( uint32_t )( p[1] ) << 8));77#endif78}7980static BLAKE2_INLINE void store16( void *dst, uint16_t w )81{82#if defined(NATIVE_LITTLE_ENDIAN)83memcpy(dst, &w, sizeof w);84#else85uint8_t *p = ( uint8_t * )dst;86*p++ = ( uint8_t )w; w >>= 8;87*p++ = ( uint8_t )w;88#endif89}9091static BLAKE2_INLINE void store32( void *dst, uint32_t w )92{93#if defined(NATIVE_LITTLE_ENDIAN)94memcpy(dst, &w, sizeof w);95#else96uint8_t *p = ( uint8_t * )dst;97p[0] = (uint8_t)(w >> 0);98p[1] = (uint8_t)(w >> 8);99p[2] = (uint8_t)(w >> 16);100p[3] = (uint8_t)(w >> 24);101#endif102}103104static BLAKE2_INLINE void store64( void *dst, uint64_t w )105{106#if defined(NATIVE_LITTLE_ENDIAN)107memcpy(dst, &w, sizeof w);108#else109uint8_t *p = ( uint8_t * )dst;110p[0] = (uint8_t)(w >> 0);111p[1] = (uint8_t)(w >> 8);112p[2] = (uint8_t)(w >> 16);113p[3] = (uint8_t)(w >> 24);114p[4] = (uint8_t)(w >> 32);115p[5] = (uint8_t)(w >> 40);116p[6] = (uint8_t)(w >> 48);117p[7] = (uint8_t)(w >> 56);118#endif119}120121static BLAKE2_INLINE uint64_t load48( const void *src )122{123const uint8_t *p = ( const uint8_t * )src;124return (( uint64_t )( p[0] ) << 0) |125(( uint64_t )( p[1] ) << 8) |126(( uint64_t )( p[2] ) << 16) |127(( uint64_t )( p[3] ) << 24) |128(( uint64_t )( p[4] ) << 32) |129(( uint64_t )( p[5] ) << 40) ;130}131132static BLAKE2_INLINE void store48( void *dst, uint64_t w )133{134uint8_t *p = ( uint8_t * )dst;135p[0] = (uint8_t)(w >> 0);136p[1] = (uint8_t)(w >> 8);137p[2] = (uint8_t)(w >> 16);138p[3] = (uint8_t)(w >> 24);139p[4] = (uint8_t)(w >> 32);140p[5] = (uint8_t)(w >> 40);141}142143static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c )144{145return ( w >> c ) | ( w << ( 32 - c ) );146}147148static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c )149{150return ( w >> c ) | ( w << ( 64 - c ) );151}152153/* prevents compiler optimizing out memset() */154static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n)155{156static void *(__LA_LIBC_CC *const volatile memset_v)(void *, int, size_t) = &memset;157memset_v(v, 0, n);158}159160#endif161162163