blob: c6a4becd23b4c4e094cd53f7de81f0b1b268aac0 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 BLAKE2 reference source code package - reference C implementations
3
4 Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
5
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.
9
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/>.
12*/
13/* blake2-impl.h
14 *
15 * Copyright (C) 2006-2021 wolfSSL Inc.
16 *
17 * This file is part of wolfSSL.
18 *
19 * wolfSSL 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.
23 *
24 * wolfSSL 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.
28 *
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-1335, USA
32 */
33
34
35
36#ifndef WOLFCRYPT_BLAKE2_IMPL_H
37#define WOLFCRYPT_BLAKE2_IMPL_H
38
39#include <wolfssl/wolfcrypt/types.h>
40
41static WC_INLINE word32 load32( const void *src )
42{
43#if defined(LITTLE_ENDIAN_ORDER)
44 return *( word32 * )( src );
45#else
46 const byte *p = ( byte * )src;
47 word32 w = *p++;
48 w |= ( word32 )( *p++ ) << 8;
49 w |= ( word32 )( *p++ ) << 16;
50 w |= ( word32 )( *p++ ) << 24;
51 return w;
52#endif
53}
54
55static WC_INLINE word64 load64( const void *src )
56{
57#if defined(LITTLE_ENDIAN_ORDER)
58 return *( word64 * )( src );
59#else
60 const byte *p = ( byte * )src;
61 word64 w = *p++;
62 w |= ( word64 )( *p++ ) << 8;
63 w |= ( word64 )( *p++ ) << 16;
64 w |= ( word64 )( *p++ ) << 24;
65 w |= ( word64 )( *p++ ) << 32;
66 w |= ( word64 )( *p++ ) << 40;
67 w |= ( word64 )( *p++ ) << 48;
68 w |= ( word64 )( *p++ ) << 56;
69 return w;
70#endif
71}
72
73static WC_INLINE void store32( void *dst, word32 w )
74{
75#if defined(LITTLE_ENDIAN_ORDER)
76 *( word32 * )( dst ) = w;
77#else
78 byte *p = ( byte * )dst;
79 *p++ = ( byte )w; w >>= 8;
80 *p++ = ( byte )w; w >>= 8;
81 *p++ = ( byte )w; w >>= 8;
82 *p++ = ( byte )w;
83#endif
84}
85
86static WC_INLINE void store64( void *dst, word64 w )
87{
88#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_GENERAL_ALIGNMENT)
89 *( word64 * )( dst ) = w;
90#else
91 byte *p = ( byte * )dst;
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;
98 *p++ = ( byte )w; w >>= 8;
99 *p++ = ( byte )w;
100#endif
101}
102
103static WC_INLINE word64 load48( const void *src )
104{
105 const byte *p = ( const byte * )src;
106 word64 w = *p++;
107 w |= ( word64 )( *p++ ) << 8;
108 w |= ( word64 )( *p++ ) << 16;
109 w |= ( word64 )( *p++ ) << 24;
110 w |= ( word64 )( *p++ ) << 32;
111 w |= ( word64 )( *p++ ) << 40;
112 return w;
113}
114
115static WC_INLINE void store48( void *dst, word64 w )
116{
117 byte *p = ( byte * )dst;
118 *p++ = ( byte )w; w >>= 8;
119 *p++ = ( byte )w; w >>= 8;
120 *p++ = ( byte )w; w >>= 8;
121 *p++ = ( byte )w; w >>= 8;
122 *p++ = ( byte )w; w >>= 8;
123 *p++ = ( byte )w;
124}
125
126static WC_INLINE word32 rotl32( const word32 w, const unsigned c )
127{
128 return ( w << c ) | ( w >> ( 32 - c ) );
129}
130
131static WC_INLINE word64 rotl64( const word64 w, const unsigned c )
132{
133 return ( w << c ) | ( w >> ( 64 - c ) );
134}
135
136static WC_INLINE word32 rotr32( const word32 w, const unsigned c )
137{
138 return ( w >> c ) | ( w << ( 32 - c ) );
139}
140
141static WC_INLINE word64 rotr64( const word64 w, const unsigned c )
142{
143 return ( w >> c ) | ( w << ( 64 - c ) );
144}
145
146/* prevents compiler optimizing out memset() */
147static WC_INLINE void secure_zero_memory( void *v, word64 n )
148{
149 volatile byte *p = ( volatile byte * )v;
150
151 while( n-- ) *p++ = 0;
152}
153
154#endif /* WOLFCRYPT_BLAKE2_IMPL_H */
155