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