]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/cyassl/ctaocrypt/blake2-impl.h
Final commit before tagging - cosmetic changes only.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / cyassl / ctaocrypt / blake2-impl.h
1 /*
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-2014 wolfSSL Inc.
16  *
17  * This file is part of CyaSSL.
18  *
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.
23  *
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.
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-1301, USA
32  */
33
34
35 #ifndef CTAOCRYPT_BLAKE2_IMPL_H
36 #define CTAOCRYPT_BLAKE2_IMPL_H
37
38 #include <cyassl/ctaocrypt/types.h>
39
40 static inline word32 load32( const void *src )
41 {
42 #if defined(LITTLE_ENDIAN_ORDER)
43   return *( word32 * )( src );
44 #else
45   const byte *p = ( byte * )src;
46   word32 w = *p++;
47   w |= ( word32 )( *p++ ) <<  8;
48   w |= ( word32 )( *p++ ) << 16;
49   w |= ( word32 )( *p++ ) << 24;
50   return w;
51 #endif
52 }
53
54 static inline word64 load64( const void *src )
55 {
56 #if defined(LITTLE_ENDIAN_ORDER)
57   return *( word64 * )( src );
58 #else
59   const byte *p = ( byte * )src;
60   word64 w = *p++;
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;
68   return w;
69 #endif
70 }
71
72 static inline void store32( void *dst, word32 w )
73 {
74 #if defined(LITTLE_ENDIAN_ORDER)
75   *( word32 * )( dst ) = w;
76 #else
77   byte *p = ( byte * )dst;
78   *p++ = ( byte )w; w >>= 8;
79   *p++ = ( byte )w; w >>= 8;
80   *p++ = ( byte )w; w >>= 8;
81   *p++ = ( byte )w;
82 #endif
83 }
84
85 static inline void store64( void *dst, word64 w )
86 {
87 #if defined(LITTLE_ENDIAN_ORDER)
88   *( word64 * )( dst ) = w;
89 #else
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;
98   *p++ = ( byte )w;
99 #endif
100 }
101
102 static inline word64 load48( const void *src )
103 {
104   const byte *p = ( const byte * )src;
105   word64 w = *p++;
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;
111   return w;
112 }
113
114 static inline void store48( void *dst, word64 w )
115 {
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;
122   *p++ = ( byte )w;
123 }
124
125 static inline word32 rotl32( const word32 w, const unsigned c )
126 {
127   return ( w << c ) | ( w >> ( 32 - c ) );
128 }
129
130 static inline word64 rotl64( const word64 w, const unsigned c )
131 {
132   return ( w << c ) | ( w >> ( 64 - c ) );
133 }
134
135 static inline word32 rotr32( const word32 w, const unsigned c )
136 {
137   return ( w >> c ) | ( w << ( 32 - c ) );
138 }
139
140 static inline word64 rotr64( const word64 w, const unsigned c )
141 {
142   return ( w >> c ) | ( w << ( 64 - c ) );
143 }
144
145 /* prevents compiler optimizing out memset() */
146 static inline void secure_zero_memory( void *v, word64 n )
147 {
148   volatile byte *p = ( volatile byte * )v;
149
150   while( n-- ) *p++ = 0;
151 }
152
153 #endif  /* CTAOCRYPT_BLAKE2_IMPL_H */
154