blake2-int.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-int.h
14  *
15  * Copyright (C) 2006-2020 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 
37 #ifndef WOLFCRYPT_BLAKE2_INT_H
38 #define WOLFCRYPT_BLAKE2_INT_H
39 
41 
42 
43 #if defined(_MSC_VER)
44  #define ALIGN(x) __declspec(align(x))
45 #elif defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
46  #define ALIGN(x) __attribute__((aligned(x)))
47 #else
48  #define ALIGN(x)
49 #endif
50 
51 
52 #if defined(__cplusplus)
53  extern "C" {
54 #endif
55 
56  enum blake2s_constant
57  {
58  BLAKE2S_BLOCKBYTES = 64,
59  BLAKE2S_OUTBYTES = 32,
60  BLAKE2S_KEYBYTES = 32,
61  BLAKE2S_SALTBYTES = 8,
62  BLAKE2S_PERSONALBYTES = 8
63  };
64 
65  enum blake2b_constant
66  {
67  BLAKE2B_BLOCKBYTES = 128,
68  BLAKE2B_OUTBYTES = 64,
69  BLAKE2B_KEYBYTES = 64,
70  BLAKE2B_SALTBYTES = 16,
71  BLAKE2B_PERSONALBYTES = 16
72  };
73 
74 #pragma pack(push, 1)
75  typedef struct __blake2s_param
76  {
77  byte digest_length; /* 1 */
78  byte key_length; /* 2 */
79  byte fanout; /* 3 */
80  byte depth; /* 4 */
81  word32 leaf_length; /* 8 */
82  byte node_offset[6];/* 14 */
83  byte node_depth; /* 15 */
84  byte inner_length; /* 16 */
85  /* byte reserved[0]; */
86  byte salt[BLAKE2B_SALTBYTES]; /* 24 */
87  byte personal[BLAKE2S_PERSONALBYTES]; /* 32 */
88  } blake2s_param;
89 
90  ALIGN( 32 ) typedef struct __blake2s_state
91  {
92  word32 h[8];
93  word32 t[2];
94  word32 f[2];
95  byte buf[2 * BLAKE2S_BLOCKBYTES];
96  word32 buflen;
97  byte last_node;
98  } blake2s_state ;
99 
100  typedef struct __blake2b_param
101  {
102  byte digest_length; /* 1 */
103  byte key_length; /* 2 */
104  byte fanout; /* 3 */
105  byte depth; /* 4 */
106  word32 leaf_length; /* 8 */
107  word64 node_offset; /* 16 */
108  byte node_depth; /* 17 */
109  byte inner_length; /* 18 */
110  byte reserved[14]; /* 32 */
111  byte salt[BLAKE2B_SALTBYTES]; /* 48 */
112  byte personal[BLAKE2B_PERSONALBYTES]; /* 64 */
113  } blake2b_param;
114 
115  ALIGN( 64 ) typedef struct __blake2b_state
116  {
117  word64 h[8];
118  word64 t[2];
119  word64 f[2];
120  byte buf[2 * BLAKE2B_BLOCKBYTES];
121  word64 buflen;
122  byte last_node;
123  } blake2b_state;
124 
125  typedef struct __blake2sp_state
126  {
127  blake2s_state S[8][1];
128  blake2s_state R[1];
129  byte buf[8 * BLAKE2S_BLOCKBYTES];
130  word32 buflen;
131  } blake2sp_state;
132 
133  typedef struct __blake2bp_state
134  {
135  blake2b_state S[4][1];
136  blake2b_state R[1];
137  byte buf[4 * BLAKE2B_BLOCKBYTES];
138  word64 buflen;
139  } blake2bp_state;
140 #pragma pack(pop)
141 
142  /* Streaming API */
143  int blake2s_init( blake2s_state *S, const byte outlen );
144  int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key, const byte keylen );
145  int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
146  int blake2s_update( blake2s_state *S, const byte *in, word32 inlen );
147  int blake2s_final( blake2s_state *S, byte *out, byte outlen );
148 
149  int blake2b_init( blake2b_state *S, const byte outlen );
150  int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, const byte keylen );
151  int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
152  int blake2b_update( blake2b_state *S, const byte *in, word64 inlen );
153  int blake2b_final( blake2b_state *S, byte *out, byte outlen );
154 
155  int blake2sp_init( blake2sp_state *S, const byte outlen );
156  int blake2sp_init_key( blake2sp_state *S, const byte outlen, const void *key, const byte keylen );
157  int blake2sp_update( blake2sp_state *S, const byte *in, word32 inlen );
158  int blake2sp_final( blake2sp_state *S, byte *out, byte outlen );
159 
160  int blake2bp_init( blake2bp_state *S, const byte outlen );
161  int blake2bp_init_key( blake2bp_state *S, const byte outlen, const void *key, const byte keylen );
162  int blake2bp_update( blake2bp_state *S, const byte *in, word64 inlen );
163  int blake2bp_final( blake2bp_state *S, byte *out, byte outlen );
164 
165  /* Simple API */
166  int blake2s( byte *out, const void *in, const void *key, const byte outlen, const word32 inlen, byte keylen );
167  int blake2b( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
168 
169  int blake2sp( byte *out, const void *in, const void *key, const byte outlen, const word32 inlen, byte keylen );
170  int blake2bp( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
171 
172  static WC_INLINE int blake2( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen )
173  {
174  return blake2b( out, in, key, outlen, inlen, keylen );
175  }
176 
177 
178 
179 #if defined(__cplusplus)
180  }
181 #endif
182 
183 #endif /* WOLFCRYPT_BLAKE2_INT_H */
184 
Definition: blake2-int.h:75
Definition: blake2-int.h:100
Definition: blake2-int.h:133
Definition: blake2-int.h:125