2 BLAKE2 reference source code package - reference C implementations
4 Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
6 To the extent possible under law, the author(s) have dedicated all copyright
7 and related and neighboring rights to this software to the public domain
8 worldwide. This software is distributed without any warranty.
10 You should have received a copy of the CC0 Public Domain Dedication along with
11 this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
15 * Copyright (C) 2006-2014 wolfSSL Inc.
17 * This file is part of CyaSSL.
19 * CyaSSL is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * CyaSSL is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
35 #ifndef CTAOCRYPT_BLAKE2_IMPL_H
36 #define CTAOCRYPT_BLAKE2_IMPL_H
38 #include <cyassl/ctaocrypt/types.h>
40 static inline word32 load32( const void *src )
42 #if defined(LITTLE_ENDIAN_ORDER)
43 return *( word32 * )( src );
45 const byte *p = ( byte * )src;
47 w |= ( word32 )( *p++ ) << 8;
48 w |= ( word32 )( *p++ ) << 16;
49 w |= ( word32 )( *p++ ) << 24;
54 static inline word64 load64( const void *src )
56 #if defined(LITTLE_ENDIAN_ORDER)
57 return *( word64 * )( src );
59 const byte *p = ( byte * )src;
61 w |= ( word64 )( *p++ ) << 8;
62 w |= ( word64 )( *p++ ) << 16;
63 w |= ( word64 )( *p++ ) << 24;
64 w |= ( word64 )( *p++ ) << 32;
65 w |= ( word64 )( *p++ ) << 40;
66 w |= ( word64 )( *p++ ) << 48;
67 w |= ( word64 )( *p++ ) << 56;
72 static inline void store32( void *dst, word32 w )
74 #if defined(LITTLE_ENDIAN_ORDER)
75 *( word32 * )( dst ) = w;
77 byte *p = ( byte * )dst;
78 *p++ = ( byte )w; w >>= 8;
79 *p++ = ( byte )w; w >>= 8;
80 *p++ = ( byte )w; w >>= 8;
85 static inline void store64( void *dst, word64 w )
87 #if defined(LITTLE_ENDIAN_ORDER)
88 *( word64 * )( dst ) = w;
90 byte *p = ( byte * )dst;
91 *p++ = ( byte )w; w >>= 8;
92 *p++ = ( byte )w; w >>= 8;
93 *p++ = ( byte )w; w >>= 8;
94 *p++ = ( byte )w; w >>= 8;
95 *p++ = ( byte )w; w >>= 8;
96 *p++ = ( byte )w; w >>= 8;
97 *p++ = ( byte )w; w >>= 8;
102 static inline word64 load48( const void *src )
104 const byte *p = ( const byte * )src;
106 w |= ( word64 )( *p++ ) << 8;
107 w |= ( word64 )( *p++ ) << 16;
108 w |= ( word64 )( *p++ ) << 24;
109 w |= ( word64 )( *p++ ) << 32;
110 w |= ( word64 )( *p++ ) << 40;
114 static inline void store48( void *dst, word64 w )
116 byte *p = ( byte * )dst;
117 *p++ = ( byte )w; w >>= 8;
118 *p++ = ( byte )w; w >>= 8;
119 *p++ = ( byte )w; w >>= 8;
120 *p++ = ( byte )w; w >>= 8;
121 *p++ = ( byte )w; w >>= 8;
125 static inline word32 rotl32( const word32 w, const unsigned c )
127 return ( w << c ) | ( w >> ( 32 - c ) );
130 static inline word64 rotl64( const word64 w, const unsigned c )
132 return ( w << c ) | ( w >> ( 64 - c ) );
135 static inline word32 rotr32( const word32 w, const unsigned c )
137 return ( w >> c ) | ( w << ( 32 - c ) );
140 static inline word64 rotr64( const word64 w, const unsigned c )
142 return ( w >> c ) | ( w << ( 64 - c ) );
145 /* prevents compiler optimizing out memset() */
146 static inline void secure_zero_memory( void *v, word64 n )
148 volatile byte *p = ( volatile byte * )v;
150 while( n-- ) *p++ = 0;
153 #endif /* CTAOCRYPT_BLAKE2_IMPL_H */