"Das U-Boot" Source Tree
at master 163 lines 4.2 kB view raw
1/* SPDX-License-Identifier: CC0-1.0 */ 2/* 3 BLAKE2 reference source code package - reference C implementations 4 5 Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the 6 terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at 7 your option. The terms of these licenses can be found at: 8 9 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 10 - OpenSSL license : https://www.openssl.org/source/license.html 11 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 12 13 More information about the BLAKE2 hash function can be found at 14 https://blake2.net. 15*/ 16 17/* 18 * Cross-ported from BLAKE2 (https://github.com/BLAKE2/BLAKE2). 19 * Modifications includes: 20 * 21 * - Remove unsupported compilers like MSC/CPP 22 * - Use u-boot library functions/macros 23 */ 24#ifndef BLAKE2_IMPL_H 25#define BLAKE2_IMPL_H 26 27#include <stdint.h> 28#include <string.h> 29 30#define BLAKE2_INLINE inline 31 32#ifdef __LITTLE_ENDIAN 33# define NATIVE_LITTLE_ENDIAN 34#endif 35 36static BLAKE2_INLINE uint32_t load32( const void *src ) 37{ 38#if defined(NATIVE_LITTLE_ENDIAN) 39 uint32_t w; 40 memcpy(&w, src, sizeof w); 41 return w; 42#else 43 const uint8_t *p = ( const uint8_t * )src; 44 return (( uint32_t )( p[0] ) << 0) | 45 (( uint32_t )( p[1] ) << 8) | 46 (( uint32_t )( p[2] ) << 16) | 47 (( uint32_t )( p[3] ) << 24) ; 48#endif 49} 50 51static BLAKE2_INLINE uint64_t load64( const void *src ) 52{ 53#if defined(NATIVE_LITTLE_ENDIAN) 54 uint64_t w; 55 memcpy(&w, src, sizeof w); 56 return w; 57#else 58 const uint8_t *p = ( const uint8_t * )src; 59 return (( uint64_t )( p[0] ) << 0) | 60 (( uint64_t )( p[1] ) << 8) | 61 (( uint64_t )( p[2] ) << 16) | 62 (( uint64_t )( p[3] ) << 24) | 63 (( uint64_t )( p[4] ) << 32) | 64 (( uint64_t )( p[5] ) << 40) | 65 (( uint64_t )( p[6] ) << 48) | 66 (( uint64_t )( p[7] ) << 56) ; 67#endif 68} 69 70static BLAKE2_INLINE uint16_t load16( const void *src ) 71{ 72#if defined(NATIVE_LITTLE_ENDIAN) 73 uint16_t w; 74 memcpy(&w, src, sizeof w); 75 return w; 76#else 77 const uint8_t *p = ( const uint8_t * )src; 78 return ( uint16_t )((( uint32_t )( p[0] ) << 0) | 79 (( uint32_t )( p[1] ) << 8)); 80#endif 81} 82 83static BLAKE2_INLINE void store16( void *dst, uint16_t w ) 84{ 85#if defined(NATIVE_LITTLE_ENDIAN) 86 memcpy(dst, &w, sizeof w); 87#else 88 uint8_t *p = ( uint8_t * )dst; 89 *p++ = ( uint8_t )w; w >>= 8; 90 *p++ = ( uint8_t )w; 91#endif 92} 93 94static BLAKE2_INLINE void store32( void *dst, uint32_t w ) 95{ 96#if defined(NATIVE_LITTLE_ENDIAN) 97 memcpy(dst, &w, sizeof w); 98#else 99 uint8_t *p = ( uint8_t * )dst; 100 p[0] = (uint8_t)(w >> 0); 101 p[1] = (uint8_t)(w >> 8); 102 p[2] = (uint8_t)(w >> 16); 103 p[3] = (uint8_t)(w >> 24); 104#endif 105} 106 107static BLAKE2_INLINE void store64( void *dst, uint64_t w ) 108{ 109#if defined(NATIVE_LITTLE_ENDIAN) 110 memcpy(dst, &w, sizeof w); 111#else 112 uint8_t *p = ( uint8_t * )dst; 113 p[0] = (uint8_t)(w >> 0); 114 p[1] = (uint8_t)(w >> 8); 115 p[2] = (uint8_t)(w >> 16); 116 p[3] = (uint8_t)(w >> 24); 117 p[4] = (uint8_t)(w >> 32); 118 p[5] = (uint8_t)(w >> 40); 119 p[6] = (uint8_t)(w >> 48); 120 p[7] = (uint8_t)(w >> 56); 121#endif 122} 123 124static BLAKE2_INLINE uint64_t load48( const void *src ) 125{ 126 const uint8_t *p = ( const uint8_t * )src; 127 return (( uint64_t )( p[0] ) << 0) | 128 (( uint64_t )( p[1] ) << 8) | 129 (( uint64_t )( p[2] ) << 16) | 130 (( uint64_t )( p[3] ) << 24) | 131 (( uint64_t )( p[4] ) << 32) | 132 (( uint64_t )( p[5] ) << 40) ; 133} 134 135static BLAKE2_INLINE void store48( void *dst, uint64_t w ) 136{ 137 uint8_t *p = ( uint8_t * )dst; 138 p[0] = (uint8_t)(w >> 0); 139 p[1] = (uint8_t)(w >> 8); 140 p[2] = (uint8_t)(w >> 16); 141 p[3] = (uint8_t)(w >> 24); 142 p[4] = (uint8_t)(w >> 32); 143 p[5] = (uint8_t)(w >> 40); 144} 145 146static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c ) 147{ 148 return ( w >> c ) | ( w << ( 32 - c ) ); 149} 150 151static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c ) 152{ 153 return ( w >> c ) | ( w << ( 64 - c ) ); 154} 155 156/* prevents compiler optimizing out memset() */ 157static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n) 158{ 159 static void *(*const volatile memset_v)(void *, int, size_t) = &memset; 160 memset_v(v, 0, n); 161} 162 163#endif