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