Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
wolfSSL
Date:
Fri Jun 26 00:39:20 2015 +0000
Revision:
0:d92f9d21154c
wolfSSL 3.6.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:d92f9d21154c 1 /*
wolfSSL 0:d92f9d21154c 2 BLAKE2 reference source code package - reference C implementations
wolfSSL 0:d92f9d21154c 3
wolfSSL 0:d92f9d21154c 4 Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
wolfSSL 0:d92f9d21154c 5
wolfSSL 0:d92f9d21154c 6 To the extent possible under law, the author(s) have dedicated all copyright
wolfSSL 0:d92f9d21154c 7 and related and neighboring rights to this software to the public domain
wolfSSL 0:d92f9d21154c 8 worldwide. This software is distributed without any warranty.
wolfSSL 0:d92f9d21154c 9
wolfSSL 0:d92f9d21154c 10 You should have received a copy of the CC0 Public Domain Dedication along with
wolfSSL 0:d92f9d21154c 11 this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
wolfSSL 0:d92f9d21154c 12 */
wolfSSL 0:d92f9d21154c 13 /* blake2-int.h
wolfSSL 0:d92f9d21154c 14 *
wolfSSL 0:d92f9d21154c 15 * Copyright (C) 2006-2015 wolfSSL Inc.
wolfSSL 0:d92f9d21154c 16 *
wolfSSL 0:d92f9d21154c 17 * This file is part of wolfSSL. (formerly known as CyaSSL)
wolfSSL 0:d92f9d21154c 18 *
wolfSSL 0:d92f9d21154c 19 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 0:d92f9d21154c 20 * it under the terms of the GNU General Public License as published by
wolfSSL 0:d92f9d21154c 21 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:d92f9d21154c 22 * (at your option) any later version.
wolfSSL 0:d92f9d21154c 23 *
wolfSSL 0:d92f9d21154c 24 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 0:d92f9d21154c 25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:d92f9d21154c 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:d92f9d21154c 27 * GNU General Public License for more details.
wolfSSL 0:d92f9d21154c 28 *
wolfSSL 0:d92f9d21154c 29 * You should have received a copy of the GNU General Public License
wolfSSL 0:d92f9d21154c 30 * along with this program; if not, write to the Free Software
wolfSSL 0:d92f9d21154c 31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:d92f9d21154c 32 */
wolfSSL 0:d92f9d21154c 33
wolfSSL 0:d92f9d21154c 34
wolfSSL 0:d92f9d21154c 35
wolfSSL 0:d92f9d21154c 36 #ifndef WOLFCRYPT_BLAKE2_INT_H
wolfSSL 0:d92f9d21154c 37 #define WOLFCRYPT_BLAKE2_INT_H
wolfSSL 0:d92f9d21154c 38
wolfSSL 0:d92f9d21154c 39 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 0:d92f9d21154c 40
wolfSSL 0:d92f9d21154c 41
wolfSSL 0:d92f9d21154c 42 #if defined(_MSC_VER)
wolfSSL 0:d92f9d21154c 43 #define ALIGN(x) __declspec(align(x))
wolfSSL 0:d92f9d21154c 44 #elif defined(__GNUC__)
wolfSSL 0:d92f9d21154c 45 #define ALIGN(x) __attribute__((aligned(x)))
wolfSSL 0:d92f9d21154c 46 #else
wolfSSL 0:d92f9d21154c 47 #define ALIGN(x)
wolfSSL 0:d92f9d21154c 48 #endif
wolfSSL 0:d92f9d21154c 49
wolfSSL 0:d92f9d21154c 50
wolfSSL 0:d92f9d21154c 51 #if defined(__cplusplus)
wolfSSL 0:d92f9d21154c 52 extern "C" {
wolfSSL 0:d92f9d21154c 53 #endif
wolfSSL 0:d92f9d21154c 54
wolfSSL 0:d92f9d21154c 55 enum blake2s_constant
wolfSSL 0:d92f9d21154c 56 {
wolfSSL 0:d92f9d21154c 57 BLAKE2S_BLOCKBYTES = 64,
wolfSSL 0:d92f9d21154c 58 BLAKE2S_OUTBYTES = 32,
wolfSSL 0:d92f9d21154c 59 BLAKE2S_KEYBYTES = 32,
wolfSSL 0:d92f9d21154c 60 BLAKE2S_SALTBYTES = 8,
wolfSSL 0:d92f9d21154c 61 BLAKE2S_PERSONALBYTES = 8
wolfSSL 0:d92f9d21154c 62 };
wolfSSL 0:d92f9d21154c 63
wolfSSL 0:d92f9d21154c 64 enum blake2b_constant
wolfSSL 0:d92f9d21154c 65 {
wolfSSL 0:d92f9d21154c 66 BLAKE2B_BLOCKBYTES = 128,
wolfSSL 0:d92f9d21154c 67 BLAKE2B_OUTBYTES = 64,
wolfSSL 0:d92f9d21154c 68 BLAKE2B_KEYBYTES = 64,
wolfSSL 0:d92f9d21154c 69 BLAKE2B_SALTBYTES = 16,
wolfSSL 0:d92f9d21154c 70 BLAKE2B_PERSONALBYTES = 16
wolfSSL 0:d92f9d21154c 71 };
wolfSSL 0:d92f9d21154c 72
wolfSSL 0:d92f9d21154c 73 #pragma pack(push, 1)
wolfSSL 0:d92f9d21154c 74 typedef struct __blake2s_param
wolfSSL 0:d92f9d21154c 75 {
wolfSSL 0:d92f9d21154c 76 byte digest_length; /* 1 */
wolfSSL 0:d92f9d21154c 77 byte key_length; /* 2 */
wolfSSL 0:d92f9d21154c 78 byte fanout; /* 3 */
wolfSSL 0:d92f9d21154c 79 byte depth; /* 4 */
wolfSSL 0:d92f9d21154c 80 word32 leaf_length; /* 8 */
wolfSSL 0:d92f9d21154c 81 byte node_offset[6];/* 14 */
wolfSSL 0:d92f9d21154c 82 byte node_depth; /* 15 */
wolfSSL 0:d92f9d21154c 83 byte inner_length; /* 16 */
wolfSSL 0:d92f9d21154c 84 /* byte reserved[0]; */
wolfSSL 0:d92f9d21154c 85 byte salt[BLAKE2B_SALTBYTES]; /* 24 */
wolfSSL 0:d92f9d21154c 86 byte personal[BLAKE2S_PERSONALBYTES]; /* 32 */
wolfSSL 0:d92f9d21154c 87 } blake2s_param;
wolfSSL 0:d92f9d21154c 88
wolfSSL 0:d92f9d21154c 89 ALIGN( 64 ) typedef struct __blake2s_state
wolfSSL 0:d92f9d21154c 90 {
wolfSSL 0:d92f9d21154c 91 word32 h[8];
wolfSSL 0:d92f9d21154c 92 word32 t[2];
wolfSSL 0:d92f9d21154c 93 word32 f[2];
wolfSSL 0:d92f9d21154c 94 byte buf[2 * BLAKE2S_BLOCKBYTES];
wolfSSL 0:d92f9d21154c 95 word64 buflen;
wolfSSL 0:d92f9d21154c 96 byte last_node;
wolfSSL 0:d92f9d21154c 97 } blake2s_state ;
wolfSSL 0:d92f9d21154c 98
wolfSSL 0:d92f9d21154c 99 typedef struct __blake2b_param
wolfSSL 0:d92f9d21154c 100 {
wolfSSL 0:d92f9d21154c 101 byte digest_length; /* 1 */
wolfSSL 0:d92f9d21154c 102 byte key_length; /* 2 */
wolfSSL 0:d92f9d21154c 103 byte fanout; /* 3 */
wolfSSL 0:d92f9d21154c 104 byte depth; /* 4 */
wolfSSL 0:d92f9d21154c 105 word32 leaf_length; /* 8 */
wolfSSL 0:d92f9d21154c 106 word64 node_offset; /* 16 */
wolfSSL 0:d92f9d21154c 107 byte node_depth; /* 17 */
wolfSSL 0:d92f9d21154c 108 byte inner_length; /* 18 */
wolfSSL 0:d92f9d21154c 109 byte reserved[14]; /* 32 */
wolfSSL 0:d92f9d21154c 110 byte salt[BLAKE2B_SALTBYTES]; /* 48 */
wolfSSL 0:d92f9d21154c 111 byte personal[BLAKE2B_PERSONALBYTES]; /* 64 */
wolfSSL 0:d92f9d21154c 112 } blake2b_param;
wolfSSL 0:d92f9d21154c 113
wolfSSL 0:d92f9d21154c 114 ALIGN( 64 ) typedef struct __blake2b_state
wolfSSL 0:d92f9d21154c 115 {
wolfSSL 0:d92f9d21154c 116 word64 h[8];
wolfSSL 0:d92f9d21154c 117 word64 t[2];
wolfSSL 0:d92f9d21154c 118 word64 f[2];
wolfSSL 0:d92f9d21154c 119 byte buf[2 * BLAKE2B_BLOCKBYTES];
wolfSSL 0:d92f9d21154c 120 word64 buflen;
wolfSSL 0:d92f9d21154c 121 byte last_node;
wolfSSL 0:d92f9d21154c 122 } blake2b_state;
wolfSSL 0:d92f9d21154c 123
wolfSSL 0:d92f9d21154c 124 typedef struct __blake2sp_state
wolfSSL 0:d92f9d21154c 125 {
wolfSSL 0:d92f9d21154c 126 blake2s_state S[8][1];
wolfSSL 0:d92f9d21154c 127 blake2s_state R[1];
wolfSSL 0:d92f9d21154c 128 byte buf[8 * BLAKE2S_BLOCKBYTES];
wolfSSL 0:d92f9d21154c 129 word64 buflen;
wolfSSL 0:d92f9d21154c 130 } blake2sp_state;
wolfSSL 0:d92f9d21154c 131
wolfSSL 0:d92f9d21154c 132 typedef struct __blake2bp_state
wolfSSL 0:d92f9d21154c 133 {
wolfSSL 0:d92f9d21154c 134 blake2b_state S[4][1];
wolfSSL 0:d92f9d21154c 135 blake2b_state R[1];
wolfSSL 0:d92f9d21154c 136 byte buf[4 * BLAKE2B_BLOCKBYTES];
wolfSSL 0:d92f9d21154c 137 word64 buflen;
wolfSSL 0:d92f9d21154c 138 } blake2bp_state;
wolfSSL 0:d92f9d21154c 139 #pragma pack(pop)
wolfSSL 0:d92f9d21154c 140
wolfSSL 0:d92f9d21154c 141 /* Streaming API */
wolfSSL 0:d92f9d21154c 142 int blake2s_init( blake2s_state *S, const byte outlen );
wolfSSL 0:d92f9d21154c 143 int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key, const byte keylen );
wolfSSL 0:d92f9d21154c 144 int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
wolfSSL 0:d92f9d21154c 145 int blake2s_update( blake2s_state *S, const byte *in, word64 inlen );
wolfSSL 0:d92f9d21154c 146 int blake2s_final( blake2s_state *S, byte *out, byte outlen );
wolfSSL 0:d92f9d21154c 147
wolfSSL 0:d92f9d21154c 148 int blake2b_init( blake2b_state *S, const byte outlen );
wolfSSL 0:d92f9d21154c 149 int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, const byte keylen );
wolfSSL 0:d92f9d21154c 150 int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
wolfSSL 0:d92f9d21154c 151 int blake2b_update( blake2b_state *S, const byte *in, word64 inlen );
wolfSSL 0:d92f9d21154c 152 int blake2b_final( blake2b_state *S, byte *out, byte outlen );
wolfSSL 0:d92f9d21154c 153
wolfSSL 0:d92f9d21154c 154 int blake2sp_init( blake2sp_state *S, const byte outlen );
wolfSSL 0:d92f9d21154c 155 int blake2sp_init_key( blake2sp_state *S, const byte outlen, const void *key, const byte keylen );
wolfSSL 0:d92f9d21154c 156 int blake2sp_update( blake2sp_state *S, const byte *in, word64 inlen );
wolfSSL 0:d92f9d21154c 157 int blake2sp_final( blake2sp_state *S, byte *out, byte outlen );
wolfSSL 0:d92f9d21154c 158
wolfSSL 0:d92f9d21154c 159 int blake2bp_init( blake2bp_state *S, const byte outlen );
wolfSSL 0:d92f9d21154c 160 int blake2bp_init_key( blake2bp_state *S, const byte outlen, const void *key, const byte keylen );
wolfSSL 0:d92f9d21154c 161 int blake2bp_update( blake2bp_state *S, const byte *in, word64 inlen );
wolfSSL 0:d92f9d21154c 162 int blake2bp_final( blake2bp_state *S, byte *out, byte outlen );
wolfSSL 0:d92f9d21154c 163
wolfSSL 0:d92f9d21154c 164 /* Simple API */
wolfSSL 0:d92f9d21154c 165 int blake2s( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
wolfSSL 0:d92f9d21154c 166 int blake2b( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
wolfSSL 0:d92f9d21154c 167
wolfSSL 0:d92f9d21154c 168 int blake2sp( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
wolfSSL 0:d92f9d21154c 169 int blake2bp( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
wolfSSL 0:d92f9d21154c 170
wolfSSL 0:d92f9d21154c 171 static inline int blake2( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen )
wolfSSL 0:d92f9d21154c 172 {
wolfSSL 0:d92f9d21154c 173 return blake2b( out, in, key, outlen, inlen, keylen );
wolfSSL 0:d92f9d21154c 174 }
wolfSSL 0:d92f9d21154c 175
wolfSSL 0:d92f9d21154c 176
wolfSSL 0:d92f9d21154c 177
wolfSSL 0:d92f9d21154c 178 #if defined(__cplusplus)
wolfSSL 0:d92f9d21154c 179 }
wolfSSL 0:d92f9d21154c 180 #endif
wolfSSL 0:d92f9d21154c 181
wolfSSL 0:d92f9d21154c 182 #endif /* WOLFCRYPT_BLAKE2_INT_H */
wolfSSL 0:d92f9d21154c 183
wolfSSL 0:d92f9d21154c 184