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 16 /* 17 Minor modifications to the original file have been made and marked 18 as `EDIT: ...`. The sole purpose of these edits is to silence misleading 19 warnings in Visual Studio. 20 */ 21 22 #ifndef BLAKE2_H 23 #define BLAKE2_H 24 25 #include <stddef.h> 26 #include <stdint.h> 27 28 #if defined(_MSC_VER) 29 #define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop)) 30 #else 31 #define BLAKE2_PACKED(x) x __attribute__((packed)) 32 #endif 33 34 #if defined(__cplusplus) 35 extern "C" { 36 #endif 37 38 enum blake2s_constant 39 { 40 BLAKE2S_BLOCKBYTES = 64, 41 BLAKE2S_OUTBYTES = 32, 42 BLAKE2S_KEYBYTES = 32, 43 BLAKE2S_SALTBYTES = 8, 44 BLAKE2S_PERSONALBYTES = 8 45 }; 46 47 enum blake2b_constant 48 { 49 BLAKE2B_BLOCKBYTES = 128, 50 BLAKE2B_OUTBYTES = 64, 51 BLAKE2B_KEYBYTES = 64, 52 BLAKE2B_SALTBYTES = 16, 53 BLAKE2B_PERSONALBYTES = 16 54 }; 55 56 typedef struct blake2s_state__ 57 { 58 uint32_t h[8]; 59 uint32_t t[2]; 60 uint32_t f[2]; 61 uint8_t buf[BLAKE2S_BLOCKBYTES]; 62 size_t buflen; 63 size_t outlen; 64 uint8_t last_node; 65 } blake2s_state; 66 67 typedef struct blake2b_state__ 68 { 69 uint64_t h[8]; 70 uint64_t t[2]; 71 uint64_t f[2]; 72 uint8_t buf[BLAKE2B_BLOCKBYTES]; 73 size_t buflen; 74 size_t outlen; 75 uint8_t last_node; 76 } blake2b_state; 77 78 typedef struct blake2sp_state__ 79 { 80 blake2s_state S[8][1]; 81 blake2s_state R[1]; 82 uint8_t buf[8 * BLAKE2S_BLOCKBYTES]; 83 size_t buflen; 84 size_t outlen; 85 } blake2sp_state; 86 87 typedef struct blake2bp_state__ 88 { 89 blake2b_state S[4][1]; 90 blake2b_state R[1]; 91 uint8_t buf[4 * BLAKE2B_BLOCKBYTES]; 92 size_t buflen; 93 size_t outlen; 94 } blake2bp_state; 95 96 97 BLAKE2_PACKED(struct blake2s_param__ 98 { 99 uint8_t digest_length; /* 1 */ 100 uint8_t key_length; /* 2 */ 101 uint8_t fanout; /* 3 */ 102 uint8_t depth; /* 4 */ 103 uint32_t leaf_length; /* 8 */ 104 uint32_t node_offset; /* 12 */ 105 uint16_t xof_length; /* 14 */ 106 uint8_t node_depth; /* 15 */ 107 uint8_t inner_length; /* 16 */ 108 /* uint8_t reserved[0]; */ 109 uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */ 110 uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */ 111 }); 112 113 typedef struct blake2s_param__ blake2s_param; 114 115 BLAKE2_PACKED(struct blake2b_param__ 116 { 117 uint8_t digest_length; /* 1 */ 118 uint8_t key_length; /* 2 */ 119 uint8_t fanout; /* 3 */ 120 uint8_t depth; /* 4 */ 121 uint32_t leaf_length; /* 8 */ 122 uint32_t node_offset; /* 12 */ 123 uint32_t xof_length; /* 16 */ 124 uint8_t node_depth; /* 17 */ 125 uint8_t inner_length; /* 18 */ 126 uint8_t reserved[14]; /* 32 */ 127 uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */ 128 uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */ 129 }); 130 131 typedef struct blake2b_param__ blake2b_param; 132 133 typedef struct blake2xs_state__ 134 { 135 blake2s_state S[1]; 136 blake2s_param P[1]; 137 } blake2xs_state; 138 139 typedef struct blake2xb_state__ 140 { 141 blake2b_state S[1]; 142 blake2b_param P[1]; 143 } blake2xb_state; 144 145 /* Padded structs result in a compile-time error */ 146 /* 147 EDIT: explicit cast to int 148 */ 149 enum { 150 BLAKE2_DUMMY_1 = 1/(int)(sizeof(blake2s_param) == BLAKE2S_OUTBYTES), 151 BLAKE2_DUMMY_2 = 1/(int)(sizeof(blake2b_param) == BLAKE2B_OUTBYTES) 152 }; 153 154 /* Streaming API */ 155 int blake2s_init( blake2s_state *S, size_t outlen ); 156 int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen ); 157 int blake2s_init_param( blake2s_state *S, const blake2s_param *P ); 158 int blake2s_update( blake2s_state *S, const void *in, size_t inlen ); 159 int blake2s_final( blake2s_state *S, void *out, size_t outlen ); 160 161 int blake2b_init( blake2b_state *S, size_t outlen ); 162 int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen ); 163 int blake2b_init_param( blake2b_state *S, const blake2b_param *P ); 164 int blake2b_update( blake2b_state *S, const void *in, size_t inlen ); 165 int blake2b_final( blake2b_state *S, void *out, size_t outlen ); 166 167 int blake2sp_init( blake2sp_state *S, size_t outlen ); 168 int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen ); 169 int blake2sp_update( blake2sp_state *S, const void *in, size_t inlen ); 170 int blake2sp_final( blake2sp_state *S, void *out, size_t outlen ); 171 172 int blake2bp_init( blake2bp_state *S, size_t outlen ); 173 int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen ); 174 int blake2bp_update( blake2bp_state *S, const void *in, size_t inlen ); 175 int blake2bp_final( blake2bp_state *S, void *out, size_t outlen ); 176 177 /* Variable output length API */ 178 int blake2xs_init( blake2xs_state *S, const size_t outlen ); 179 int blake2xs_init_key( blake2xs_state *S, const size_t outlen, const void *key, size_t keylen ); 180 int blake2xs_update( blake2xs_state *S, const void *in, size_t inlen ); 181 int blake2xs_final(blake2xs_state *S, void *out, size_t outlen); 182 183 int blake2xb_init( blake2xb_state *S, const size_t outlen ); 184 int blake2xb_init_key( blake2xb_state *S, const size_t outlen, const void *key, size_t keylen ); 185 int blake2xb_update( blake2xb_state *S, const void *in, size_t inlen ); 186 int blake2xb_final(blake2xb_state *S, void *out, size_t outlen); 187 188 /* Simple API */ 189 int blake2s( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); 190 int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); 191 192 int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); 193 int blake2bp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); 194 195 int blake2xs( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); 196 int blake2xb( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); 197 198 /* This is simply an alias for blake2b */ 199 int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); 200 201 #if defined(__cplusplus) 202 } 203 #endif 204 205 #endif 206