1 /*
2    BLAKE2 reference source code package - reference C implementations
3 
4    Copyright 2012, Samuel Neves <sneves@dei.uc.pt>.  You may use this under the
5    terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6    your option.  The terms of these licenses can be found at:
7 
8    - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9    - OpenSSL license   : https://www.openssl.org/source/license.html
10    - Apache 2.0        : http://www.apache.org/licenses/LICENSE-2.0
11 
12    More information about the BLAKE2 hash function can be found at
13    https://blake2.net.
14 */
15 #pragma once
16 
17 #include <stdint.h>
18 #include <string.h>
19 
20 #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
21   #if   defined(_MSC_VER)
22     #define BLAKE2_INLINE __inline
23   #elif defined(__GNUC__)
24     #define BLAKE2_INLINE __inline__
25   #else
26     #define BLAKE2_INLINE
27   #endif
28 #else
29   #define BLAKE2_INLINE inline
30 #endif
31 
32 static BLAKE2_INLINE uint32_t load32( const void *src )
33 {
34 #if defined(NATIVE_LITTLE_ENDIAN)
35   uint32_t w;
36   memcpy(&w, src, sizeof w);
37   return w;
38 #else
39   const uint8_t *p = ( const uint8_t * )src;
40   return (( uint32_t )( p[0] ) <<  0) |
41          (( uint32_t )( p[1] ) <<  8) |
42          (( uint32_t )( p[2] ) << 16) |
43          (( uint32_t )( p[3] ) << 24) ;
44 #endif
45 }
46 
47 static BLAKE2_INLINE uint64_t load64( const void *src )
48 {
49 #if defined(NATIVE_LITTLE_ENDIAN)
50   uint64_t w;
51   memcpy(&w, src, sizeof w);
52   return w;
53 #else
54   const uint8_t *p = ( const uint8_t * )src;
55   return (( uint64_t )( p[0] ) <<  0) |
56          (( uint64_t )( p[1] ) <<  8) |
57          (( uint64_t )( p[2] ) << 16) |
58          (( uint64_t )( p[3] ) << 24) |
59          (( uint64_t )( p[4] ) << 32) |
60          (( uint64_t )( p[5] ) << 40) |
61          (( uint64_t )( p[6] ) << 48) |
62          (( uint64_t )( p[7] ) << 56) ;
63 #endif
64 }
65 
66 static BLAKE2_INLINE uint16_t load16( const void *src )
67 {
68 #if defined(NATIVE_LITTLE_ENDIAN)
69   uint16_t w;
70   memcpy(&w, src, sizeof w);
71   return w;
72 #else
73   const uint8_t *p = ( const uint8_t * )src;
74   return ( uint16_t )((( uint32_t )( p[0] ) <<  0) |
75                       (( uint32_t )( p[1] ) <<  8));
76 #endif
77 }
78 
79 static BLAKE2_INLINE void store16( void *dst, uint16_t w )
80 {
81 #if defined(NATIVE_LITTLE_ENDIAN)
82   memcpy(dst, &w, sizeof w);
83 #else
84   uint8_t *p = ( uint8_t * )dst;
85   *p++ = ( uint8_t )w; w >>= 8;
86   *p++ = ( uint8_t )w;
87 #endif
88 }
89 
90 static BLAKE2_INLINE void store32( void *dst, uint32_t w )
91 {
92 #if defined(NATIVE_LITTLE_ENDIAN)
93   memcpy(dst, &w, sizeof w);
94 #else
95   uint8_t *p = ( uint8_t * )dst;
96   p[0] = (uint8_t)(w >>  0);
97   p[1] = (uint8_t)(w >>  8);
98   p[2] = (uint8_t)(w >> 16);
99   p[3] = (uint8_t)(w >> 24);
100 #endif
101 }
102 
103 static BLAKE2_INLINE void store64( void *dst, uint64_t w )
104 {
105 #if defined(NATIVE_LITTLE_ENDIAN)
106   memcpy(dst, &w, sizeof w);
107 #else
108   uint8_t *p = ( uint8_t * )dst;
109   p[0] = (uint8_t)(w >>  0);
110   p[1] = (uint8_t)(w >>  8);
111   p[2] = (uint8_t)(w >> 16);
112   p[3] = (uint8_t)(w >> 24);
113   p[4] = (uint8_t)(w >> 32);
114   p[5] = (uint8_t)(w >> 40);
115   p[6] = (uint8_t)(w >> 48);
116   p[7] = (uint8_t)(w >> 56);
117 #endif
118 }
119 
120 static BLAKE2_INLINE uint64_t load48( const void *src )
121 {
122   const uint8_t *p = ( const uint8_t * )src;
123   return (( uint64_t )( p[0] ) <<  0) |
124          (( uint64_t )( p[1] ) <<  8) |
125          (( uint64_t )( p[2] ) << 16) |
126          (( uint64_t )( p[3] ) << 24) |
127          (( uint64_t )( p[4] ) << 32) |
128          (( uint64_t )( p[5] ) << 40) ;
129 }
130 
131 static BLAKE2_INLINE void store48( void *dst, uint64_t w )
132 {
133   uint8_t *p = ( uint8_t * )dst;
134   p[0] = (uint8_t)(w >>  0);
135   p[1] = (uint8_t)(w >>  8);
136   p[2] = (uint8_t)(w >> 16);
137   p[3] = (uint8_t)(w >> 24);
138   p[4] = (uint8_t)(w >> 32);
139   p[5] = (uint8_t)(w >> 40);
140 }
141 
142 static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c )
143 {
144   return ( w >> c ) | ( w << ( 32 - c ) );
145 }
146 
147 static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c )
148 {
149   return ( w >> c ) | ( w << ( 64 - c ) );
150 }
151 
152 #if defined(_MSC_VER)
153 #define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
154 #else
155 #define BLAKE2_PACKED(x) x __attribute__((packed))
156 #endif
157 
158 enum blake2b_constant
159 {
160     BLAKE2B_BLOCKBYTES = 128,
161     BLAKE2B_OUTBYTES   = 64,
162     BLAKE2B_KEYBYTES   = 64,
163     BLAKE2B_SALTBYTES  = 16,
164     BLAKE2B_PERSONALBYTES = 16
165 };
166 
167 typedef struct blake2b_state__
168 {
169     uint64_t h[8];
170     uint64_t t[2];
171     uint64_t f[2];
172     uint8_t  buf[BLAKE2B_BLOCKBYTES];
173     size_t   buflen;
174     size_t   outlen;
175     uint8_t  last_node;
176 } blake2b_state;
177 
178 BLAKE2_PACKED(struct blake2b_param__
179 {
180     uint8_t  digest_length; /* 1 */
181     uint8_t  key_length;    /* 2 */
182     uint8_t  fanout;        /* 3 */
183     uint8_t  depth;         /* 4 */
184     uint32_t leaf_length;   /* 8 */
185     uint32_t node_offset;   /* 12 */
186     uint32_t xof_length;    /* 16 */
187     uint8_t  node_depth;    /* 17 */
188     uint8_t  inner_length;  /* 18 */
189     uint8_t  reserved[14];  /* 32 */
190     uint8_t  salt[BLAKE2B_SALTBYTES]; /* 48 */
191     uint8_t  personal[BLAKE2B_PERSONALBYTES];  /* 64 */
192 });
193 
194 typedef struct blake2b_param__ blake2b_param;
195