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