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 #ifndef __BLAKE2_H__ 17 #define __BLAKE2_H__ 18 19 #include <stddef.h> 20 #include <stdint.h> 21 22 #ifdef BLAKE2_NO_INLINE 23 #define BLAKE2_LOCAL_INLINE(type) static type 24 #endif 25 26 #ifndef BLAKE2_LOCAL_INLINE 27 #define BLAKE2_LOCAL_INLINE(type) static inline type 28 #endif 29 30 #if defined(__cplusplus) 31 extern "C" { 32 #endif 33 34 enum blake2s_constant 35 { 36 BLAKE2S_BLOCKBYTES = 64, 37 BLAKE2S_OUTBYTES = 32, 38 BLAKE2S_KEYBYTES = 32, 39 BLAKE2S_SALTBYTES = 8, 40 BLAKE2S_PERSONALBYTES = 8 41 }; 42 43 enum blake2b_constant 44 { 45 BLAKE2B_BLOCKBYTES = 128, 46 BLAKE2B_OUTBYTES = 64, 47 BLAKE2B_KEYBYTES = 64, 48 BLAKE2B_SALTBYTES = 16, 49 BLAKE2B_PERSONALBYTES = 16 50 }; 51 52 typedef struct __blake2s_state 53 { 54 uint32_t h[8]; 55 uint32_t t[2]; 56 uint32_t f[2]; 57 uint8_t buf[2 * BLAKE2S_BLOCKBYTES]; 58 size_t buflen; 59 uint8_t last_node; 60 } blake2s_state; 61 62 typedef struct __blake2b_state 63 { 64 uint64_t h[8]; 65 uint64_t t[2]; 66 uint64_t f[2]; 67 uint8_t buf[2 * BLAKE2B_BLOCKBYTES]; 68 size_t buflen; 69 uint8_t last_node; 70 } blake2b_state; 71 72 typedef struct __blake2sp_state 73 { 74 blake2s_state S[8][1]; 75 blake2s_state R[1]; 76 uint8_t buf[8 * BLAKE2S_BLOCKBYTES]; 77 size_t buflen; 78 } blake2sp_state; 79 80 typedef struct __blake2bp_state 81 { 82 blake2b_state S[4][1]; 83 blake2b_state R[1]; 84 uint8_t buf[4 * BLAKE2B_BLOCKBYTES]; 85 size_t buflen; 86 } blake2bp_state; 87 88 89 #pragma pack(push, 1) 90 typedef struct __blake2s_param 91 { 92 uint8_t digest_length; /* 1 */ 93 uint8_t key_length; /* 2 */ 94 uint8_t fanout; /* 3 */ 95 uint8_t depth; /* 4 */ 96 uint32_t leaf_length; /* 8 */ 97 uint8_t node_offset[6];// 14 98 uint8_t node_depth; /* 15 */ 99 uint8_t inner_length; /* 16 */ 100 /* uint8_t reserved[0]; */ 101 uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */ 102 uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */ 103 } blake2s_param; 104 105 typedef struct __blake2b_param 106 { 107 uint8_t digest_length; /* 1 */ 108 uint8_t key_length; /* 2 */ 109 uint8_t fanout; /* 3 */ 110 uint8_t depth; /* 4 */ 111 uint32_t leaf_length; /* 8 */ 112 uint64_t node_offset; /* 16 */ 113 uint8_t node_depth; /* 17 */ 114 uint8_t inner_length; /* 18 */ 115 uint8_t reserved[14]; /* 32 */ 116 uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */ 117 uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */ 118 } blake2b_param; 119 #pragma pack(pop) 120 121 /* Streaming API */ 122 int blake2s_init( blake2s_state *S, const uint8_t outlen ); 123 int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ); 124 int blake2s_init_param( blake2s_state *S, const blake2s_param *P ); 125 int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen ); 126 int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen ); 127 128 int blake2b_init( blake2b_state *S, const uint8_t outlen ); 129 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ); 130 int blake2b_init_param( blake2b_state *S, const blake2b_param *P ); 131 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen ); 132 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen ); 133 134 int blake2sp_init( blake2sp_state *S, const uint8_t outlen ); 135 int blake2sp_init_key( blake2sp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ); 136 int blake2sp_update( blake2sp_state *S, const uint8_t *in, uint64_t inlen ); 137 int blake2sp_final( blake2sp_state *S, uint8_t *out, uint8_t outlen ); 138 139 int blake2bp_init( blake2bp_state *S, const uint8_t outlen ); 140 int blake2bp_init_key( blake2bp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ); 141 int blake2bp_update( blake2bp_state *S, const uint8_t *in, uint64_t inlen ); 142 int blake2bp_final( blake2bp_state *S, uint8_t *out, uint8_t outlen ); 143 144 /* Simple API */ 145 int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ); 146 int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ); 147 148 int blake2sp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ); 149 int blake2bp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ); 150 blake2(uint8_t * out,const void * in,const void * key,const uint8_t outlen,const uint64_t inlen,uint8_t keylen)151 static inline int blake2( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ) 152 { 153 return blake2b( out, in, key, outlen, inlen, keylen ); 154 } 155 156 #if defined(__cplusplus) 157 } 158 #endif 159 160 #endif 161 162