1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-03 12:00:59 +00:00

style(crypto): format blake and groestl

This commit is contained in:
Ondřej Vejpustek 2024-09-24 14:27:29 +02:00
parent 332c0bf365
commit 8bf02cf0a2
10 changed files with 1075 additions and 1216 deletions

View File

@ -7,8 +7,9 @@
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
You should have received a copy of the CC0 Public Domain Dedication along
with this software. If not, see
<http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "blake256.h"
@ -16,72 +17,64 @@
#define U8TO32_BIG(p) \
(((uint32_t)((p)[0]) << 24) | ((uint32_t)((p)[1]) << 16) | \
((uint32_t)((p)[2]) << 8) | ((uint32_t)((p)[3]) ))
((uint32_t)((p)[2]) << 8) | ((uint32_t)((p)[3])))
#define U32TO8_BIG(p, v) \
(p)[0] = (uint8_t)((v) >> 24); (p)[1] = (uint8_t)((v) >> 16); \
(p)[2] = (uint8_t)((v) >> 8); (p)[3] = (uint8_t)((v) );
(p)[0] = (uint8_t)((v) >> 24); \
(p)[1] = (uint8_t)((v) >> 16); \
(p)[2] = (uint8_t)((v) >> 8); \
(p)[3] = (uint8_t)((v));
static const uint8_t sigma[][16] =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
{12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
{13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
{10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }
};
static const uint8_t sigma[][16] = {
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
{7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
{9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
{2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
{12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
{13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
{6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
{10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
{7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
{9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
{2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9}};
static const uint32_t u256[16] =
{
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917
};
static const uint32_t u256[16] = {
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0,
0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917};
static const uint8_t padding[129] =
{
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static const uint8_t padding[129] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static void blake256_compress( BLAKE256_CTX *S, const uint8_t *block )
{
static void blake256_compress(BLAKE256_CTX *S, const uint8_t *block) {
uint32_t v[16] = {0}, m[16] = {0}, i = 0;
#define ROT(x,n) (((x)<<(32-n))|( (x)>>(n)))
#define G(a,b,c,d,e) \
v[a] += (m[sigma[i][e]] ^ u256[sigma[i][e+1]]) + v[b]; \
v[d] = ROT( v[d] ^ v[a],16); \
#define ROT(x, n) (((x) << (32 - n)) | ((x) >> (n)))
#define G(a, b, c, d, e) \
v[a] += (m[sigma[i][e]] ^ u256[sigma[i][e + 1]]) + v[b]; \
v[d] = ROT(v[d] ^ v[a], 16); \
v[c] += v[d]; \
v[b] = ROT( v[b] ^ v[c],12); \
v[a] += (m[sigma[i][e+1]] ^ u256[sigma[i][e]])+v[b]; \
v[d] = ROT( v[d] ^ v[a], 8); \
v[b] = ROT(v[b] ^ v[c], 12); \
v[a] += (m[sigma[i][e + 1]] ^ u256[sigma[i][e]]) + v[b]; \
v[d] = ROT(v[d] ^ v[a], 8); \
v[c] += v[d]; \
v[b] = ROT( v[b] ^ v[c], 7);
v[b] = ROT(v[b] ^ v[c], 7);
for( i = 0; i < 16; ++i ) m[i] = U8TO32_BIG( block + i * 4 );
for (i = 0; i < 16; ++i) m[i] = U8TO32_BIG(block + i * 4);
for( i = 0; i < 8; ++i ) v[i] = S->h[i];
for (i = 0; i < 8; ++i) v[i] = S->h[i];
v[ 8] = S->s[0] ^ u256[0];
v[ 9] = S->s[1] ^ u256[1];
v[8] = S->s[0] ^ u256[0];
v[9] = S->s[1] ^ u256[1];
v[10] = S->s[2] ^ u256[2];
v[11] = S->s[3] ^ u256[3];
v[12] = u256[4];
@ -90,36 +83,32 @@ static void blake256_compress( BLAKE256_CTX *S, const uint8_t *block )
v[15] = u256[7];
/* don't xor t when the block is only padding */
if ( !S->nullt )
{
if (!S->nullt) {
v[12] ^= S->t[0];
v[13] ^= S->t[0];
v[14] ^= S->t[1];
v[15] ^= S->t[1];
}
for( i = 0; i < 14; ++i )
{
for (i = 0; i < 14; ++i) {
/* column step */
G( 0, 4, 8, 12, 0 );
G( 1, 5, 9, 13, 2 );
G( 2, 6, 10, 14, 4 );
G( 3, 7, 11, 15, 6 );
G(0, 4, 8, 12, 0);
G(1, 5, 9, 13, 2);
G(2, 6, 10, 14, 4);
G(3, 7, 11, 15, 6);
/* diagonal step */
G( 0, 5, 10, 15, 8 );
G( 1, 6, 11, 12, 10 );
G( 2, 7, 8, 13, 12 );
G( 3, 4, 9, 14, 14 );
G(0, 5, 10, 15, 8);
G(1, 6, 11, 12, 10);
G(2, 7, 8, 13, 12);
G(3, 4, 9, 14, 14);
}
for( i = 0; i < 16; ++i ) S->h[i % 8] ^= v[i];
for (i = 0; i < 16; ++i) S->h[i % 8] ^= v[i];
for( i = 0; i < 8 ; ++i ) S->h[i] ^= S->s[i % 4];
for (i = 0; i < 8; ++i) S->h[i] ^= S->s[i % 4];
}
void blake256_Init( BLAKE256_CTX *S )
{
void blake256_Init(BLAKE256_CTX *S) {
S->h[0] = 0x6a09e667;
S->h[1] = 0xbb67ae85;
S->h[2] = 0x3c6ef372;
@ -132,103 +121,90 @@ void blake256_Init( BLAKE256_CTX *S )
S->s[0] = S->s[1] = S->s[2] = S->s[3] = 0;
}
void blake256_Update( BLAKE256_CTX *S, const uint8_t *in, size_t inlen )
{
void blake256_Update(BLAKE256_CTX *S, const uint8_t *in, size_t inlen) {
size_t left = S->buflen;
size_t fill = 64 - left;
/* data left and data received fill a block */
if( left && ( inlen >= fill ) )
{
memcpy( ( void * ) ( S->buf + left ), ( void * ) in, fill );
if (left && (inlen >= fill)) {
memcpy((void *)(S->buf + left), (void *)in, fill);
S->t[0] += 512;
if ( S->t[0] == 0 ) S->t[1]++;
if (S->t[0] == 0) S->t[1]++;
blake256_compress( S, S->buf );
blake256_compress(S, S->buf);
in += fill;
inlen -= fill;
left = 0;
}
/* compress blocks of data received */
while( inlen >= 64 )
{
while (inlen >= 64) {
S->t[0] += 512;
if ( S->t[0] == 0 ) S->t[1]++;
if (S->t[0] == 0) S->t[1]++;
blake256_compress( S, in );
blake256_compress(S, in);
in += 64;
inlen -= 64;
}
/* store any data left */
if( inlen > 0 )
{
memcpy( ( void * ) ( S->buf + left ), \
( void * ) in, ( size_t ) inlen );
if (inlen > 0) {
memcpy((void *)(S->buf + left), (void *)in, (size_t)inlen);
}
S->buflen = left + inlen;
}
void blake256_Final( BLAKE256_CTX *S, uint8_t *out )
{
void blake256_Final(BLAKE256_CTX *S, uint8_t *out) {
uint8_t msglen[8] = {0}, zo = 0x01, oo = 0x81;
uint32_t lo = S->t[0] + ( S->buflen << 3 ), hi = S->t[1];
uint32_t lo = S->t[0] + (S->buflen << 3), hi = S->t[1];
/* support for hashing more than 2^32 bits */
if ( lo < ( S->buflen << 3 ) ) hi++;
if (lo < (S->buflen << 3)) hi++;
U32TO8_BIG( msglen + 0, hi );
U32TO8_BIG( msglen + 4, lo );
U32TO8_BIG(msglen + 0, hi);
U32TO8_BIG(msglen + 4, lo);
if ( S->buflen == 55 ) /* one padding byte */
if (S->buflen == 55) /* one padding byte */
{
S->t[0] -= 8;
blake256_Update( S, &oo, 1 );
}
else
blake256_Update(S, &oo, 1);
} else {
if (S->buflen < 55) /* enough space to fill the block */
{
if ( S->buflen < 55 ) /* enough space to fill the block */
{
if ( !S->buflen ) S->nullt = 1;
if (!S->buflen) S->nullt = 1;
S->t[0] -= 440 - ( S->buflen << 3 );
blake256_Update( S, padding, 55 - S->buflen );
}
else /* need 2 compressions */
S->t[0] -= 440 - (S->buflen << 3);
blake256_Update(S, padding, 55 - S->buflen);
} else /* need 2 compressions */
{
S->t[0] -= 512 - ( S->buflen << 3 );
blake256_Update( S, padding, 64 - S->buflen );
S->t[0] -= 512 - (S->buflen << 3);
blake256_Update(S, padding, 64 - S->buflen);
S->t[0] -= 440;
blake256_Update( S, padding + 1, 55 );
blake256_Update(S, padding + 1, 55);
S->nullt = 1;
}
blake256_Update( S, &zo, 1 );
blake256_Update(S, &zo, 1);
S->t[0] -= 8;
}
S->t[0] -= 64;
blake256_Update( S, msglen, 8 );
U32TO8_BIG( out + 0, S->h[0] );
U32TO8_BIG( out + 4, S->h[1] );
U32TO8_BIG( out + 8, S->h[2] );
U32TO8_BIG( out + 12, S->h[3] );
U32TO8_BIG( out + 16, S->h[4] );
U32TO8_BIG( out + 20, S->h[5] );
U32TO8_BIG( out + 24, S->h[6] );
U32TO8_BIG( out + 28, S->h[7] );
blake256_Update(S, msglen, 8);
U32TO8_BIG(out + 0, S->h[0]);
U32TO8_BIG(out + 4, S->h[1]);
U32TO8_BIG(out + 8, S->h[2]);
U32TO8_BIG(out + 12, S->h[3]);
U32TO8_BIG(out + 16, S->h[4]);
U32TO8_BIG(out + 20, S->h[5]);
U32TO8_BIG(out + 24, S->h[6]);
U32TO8_BIG(out + 28, S->h[7]);
}
void blake256( const uint8_t *in, size_t inlen, uint8_t *out )
{
void blake256(const uint8_t *in, size_t inlen, uint8_t *out) {
BLAKE256_CTX S = {0};
blake256_Init( &S );
blake256_Update( &S, in, inlen );
blake256_Final( &S, out );
blake256_Init(&S);
blake256_Update(&S, in, inlen);
blake256_Final(&S, out);
}

View File

@ -2,37 +2,43 @@
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// 1. Redistributions of source code must retain the above copyright notice,
// this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list
// of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote
// developers
#ifndef __BLAKE256_H__
#define __BLAKE256_H__
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
#define BLAKE256_DIGEST_LENGTH 32
#define BLAKE256_BLOCK_LENGTH 64

View File

@ -15,13 +15,12 @@
#include <string.h>
#include "blake2b.h"
#include "blake2_common.h"
#include "blake2b.h"
#include "memzero.h"
#include "options.h"
typedef struct blake2b_param__
{
typedef struct blake2b_param__ {
uint8_t digest_length; /* 1 */
uint8_t key_length; /* 2 */
uint8_t fanout; /* 3 */
@ -36,158 +35,139 @@ typedef struct blake2b_param__
uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
} __attribute__((packed)) blake2b_param;
static const uint64_t blake2b_IV[8] =
{
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
};
static const uint64_t blake2b_IV[8] = {
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL,
0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
static const uint8_t blake2b_sigma[12][16] =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
};
static const uint8_t blake2b_sigma[12][16] = {
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
{7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
{9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
{2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
{12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
{13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
{6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
{10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}};
static void blake2b_set_lastnode( blake2b_state *S )
{
S->f[1] = (uint64_t)-1;
}
static void blake2b_set_lastnode(blake2b_state *S) { S->f[1] = (uint64_t)-1; }
/* Some helper functions, not necessarily useful */
static int blake2b_is_lastblock( const blake2b_state *S )
{
return S->f[0] != 0;
}
static int blake2b_is_lastblock(const blake2b_state *S) { return S->f[0] != 0; }
static void blake2b_set_lastblock( blake2b_state *S )
{
if( S->last_node ) blake2b_set_lastnode( S );
static void blake2b_set_lastblock(blake2b_state *S) {
if (S->last_node) blake2b_set_lastnode(S);
S->f[0] = (uint64_t)-1;
}
static void blake2b_increment_counter( blake2b_state *S, const uint64_t inc )
{
static void blake2b_increment_counter(blake2b_state *S, const uint64_t inc) {
S->t[0] += inc;
S->t[1] += ( S->t[0] < inc );
S->t[1] += (S->t[0] < inc);
}
static void blake2b_init0( blake2b_state *S )
{
static void blake2b_init0(blake2b_state *S) {
size_t i = 0;
memzero( S, sizeof( blake2b_state ) );
memzero(S, sizeof(blake2b_state));
for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
for (i = 0; i < 8; ++i) S->h[i] = blake2b_IV[i];
}
/* init xors IV with input parameter block */
int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
{
const uint8_t *p = ( const uint8_t * )( P );
int blake2b_init_param(blake2b_state *S, const blake2b_param *P) {
const uint8_t *p = (const uint8_t *)(P);
size_t i = 0;
blake2b_init0( S );
blake2b_init0(S);
/* IV XOR ParamBlock */
for( i = 0; i < 8; ++i )
S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
for (i = 0; i < 8; ++i) S->h[i] ^= load64(p + sizeof(S->h[i]) * i);
S->outlen = P->digest_length;
return 0;
}
/* Sequential blake2b initialization */
int blake2b_Init( blake2b_state *S, size_t outlen )
{
int blake2b_Init(blake2b_state *S, size_t outlen) {
blake2b_param P[1] = {0};
if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
if ((!outlen) || (outlen > BLAKE2B_OUTBYTES)) return -1;
P->digest_length = (uint8_t)outlen;
P->key_length = 0;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store32( &P->node_offset, 0 );
store32( &P->xof_length, 0 );
store32(&P->leaf_length, 0);
store32(&P->node_offset, 0);
store32(&P->xof_length, 0);
P->node_depth = 0;
P->inner_length = 0;
memzero( P->reserved, sizeof( P->reserved ) );
memzero( P->salt, sizeof( P->salt ) );
memzero( P->personal, sizeof( P->personal ) );
return blake2b_init_param( S, P );
memzero(P->reserved, sizeof(P->reserved));
memzero(P->salt, sizeof(P->salt));
memzero(P->personal, sizeof(P->personal));
return blake2b_init_param(S, P);
}
int blake2b_InitPersonal( blake2b_state *S, size_t outlen, const void *personal, size_t personal_len)
{
int blake2b_InitPersonal(blake2b_state *S, size_t outlen, const void *personal,
size_t personal_len) {
blake2b_param P[1] = {0};
if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
if ( ( !personal ) || ( personal_len != BLAKE2B_PERSONALBYTES ) ) return -1;
if ((!outlen) || (outlen > BLAKE2B_OUTBYTES)) return -1;
if ((!personal) || (personal_len != BLAKE2B_PERSONALBYTES)) return -1;
P->digest_length = (uint8_t)outlen;
P->key_length = 0;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store32( &P->node_offset, 0 );
store32( &P->xof_length, 0 );
store32(&P->leaf_length, 0);
store32(&P->node_offset, 0);
store32(&P->xof_length, 0);
P->node_depth = 0;
P->inner_length = 0;
memzero( P->reserved, sizeof( P->reserved ) );
memzero( P->salt, sizeof( P->salt ) );
memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES );
return blake2b_init_param( S, P );
memzero(P->reserved, sizeof(P->reserved));
memzero(P->salt, sizeof(P->salt));
memcpy(P->personal, personal, BLAKE2B_PERSONALBYTES);
return blake2b_init_param(S, P);
}
int blake2b_InitKey( blake2b_state *S, size_t outlen, const void *key, size_t keylen )
{
int blake2b_InitKey(blake2b_state *S, size_t outlen, const void *key,
size_t keylen) {
blake2b_param P[1] = {0};
if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
if ((!outlen) || (outlen > BLAKE2B_OUTBYTES)) return -1;
if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
if (!key || !keylen || keylen > BLAKE2B_KEYBYTES) return -1;
P->digest_length = (uint8_t)outlen;
P->key_length = (uint8_t)keylen;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store32( &P->node_offset, 0 );
store32( &P->xof_length, 0 );
store32(&P->leaf_length, 0);
store32(&P->node_offset, 0);
store32(&P->xof_length, 0);
P->node_depth = 0;
P->inner_length = 0;
memzero( P->reserved, sizeof( P->reserved ) );
memzero( P->salt, sizeof( P->salt ) );
memzero( P->personal, sizeof( P->personal ) );
memzero(P->reserved, sizeof(P->reserved));
memzero(P->salt, sizeof(P->salt));
memzero(P->personal, sizeof(P->personal));
if( blake2b_init_param( S, P ) < 0 ) return -1;
if (blake2b_init_param(S, P) < 0) return -1;
{
uint8_t block[BLAKE2B_BLOCKBYTES] = {0};
memzero( block, BLAKE2B_BLOCKBYTES );
memcpy( block, key, keylen );
blake2b_Update( S, block, BLAKE2B_BLOCKBYTES );
memzero( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
memzero(block, BLAKE2B_BLOCKBYTES);
memcpy(block, key, keylen);
blake2b_Update(S, block, BLAKE2B_BLOCKBYTES);
memzero(block, BLAKE2B_BLOCKBYTES); /* Burn the key from stack */
}
return 0;
}
#define G(m,r,i,a,b,c,d) \
#define G(m, r, i, a, b, c, d) \
do { \
*(a) = *(a) + *(b) + m[blake2b_sigma[r][2 * i + 0]]; \
*(d) = rotr64(*(d) ^ *(a), 32); \
@ -197,45 +177,45 @@ int blake2b_InitKey( blake2b_state *S, size_t outlen, const void *key, size_t ke
*(d) = rotr64(*(d) ^ *(a), 16); \
*(c) = *(c) + *(d); \
*(b) = rotr64(*(b) ^ *(c), 63); \
} while(0)
} while (0)
#if OPTIMIZE_SIZE_BLAKE2B
static void g(uint64_t *m, int r, int i, uint64_t *a, uint64_t *b, uint64_t *c,
uint64_t *d) {
G(m,r,i,a,b,c,d);
G(m, r, i, a, b, c, d);
}
#else
#define g(m,r,i,a,b,c,d) G(m,r,i,a,b,c,d)
#define g(m, r, i, a, b, c, d) G(m, r, i, a, b, c, d)
#endif
#define ROUND(m,v,r) \
#define ROUND(m, v, r) \
do { \
g(m,r,0,v + 0,v + 4,v + 8,v + 12); \
g(m,r,1,v + 1,v + 5,v + 9,v + 13); \
g(m,r,2,v + 2,v + 6,v + 10,v + 14); \
g(m,r,3,v + 3,v + 7,v + 11,v + 15); \
g(m,r,4,v + 0,v + 5,v + 10,v + 15); \
g(m,r,5,v + 1,v + 6,v + 11,v + 12); \
g(m,r,6,v + 2,v + 7,v + 8,v + 13); \
g(m,r,7,v + 3,v + 4,v + 9,v + 14); \
} while(0)
g(m, r, 0, v + 0, v + 4, v + 8, v + 12); \
g(m, r, 1, v + 1, v + 5, v + 9, v + 13); \
g(m, r, 2, v + 2, v + 6, v + 10, v + 14); \
g(m, r, 3, v + 3, v + 7, v + 11, v + 15); \
g(m, r, 4, v + 0, v + 5, v + 10, v + 15); \
g(m, r, 5, v + 1, v + 6, v + 11, v + 12); \
g(m, r, 6, v + 2, v + 7, v + 8, v + 13); \
g(m, r, 7, v + 3, v + 4, v + 9, v + 14); \
} while (0)
static void blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] )
{
static void blake2b_compress(blake2b_state *S,
const uint8_t block[BLAKE2B_BLOCKBYTES]) {
uint64_t m[16] = {0};
uint64_t v[16] = {0};
size_t i = 0;
for( i = 0; i < 16; ++i ) {
m[i] = load64( block + i * sizeof( m[i] ) );
for (i = 0; i < 16; ++i) {
m[i] = load64(block + i * sizeof(m[i]));
}
for( i = 0; i < 8; ++i ) {
for (i = 0; i < 8; ++i) {
v[i] = S->h[i];
}
v[ 8] = blake2b_IV[0];
v[ 9] = blake2b_IV[1];
v[8] = blake2b_IV[0];
v[9] = blake2b_IV[1];
v[10] = blake2b_IV[2];
v[11] = blake2b_IV[3];
v[12] = blake2b_IV[4] ^ S->t[0];
@ -243,27 +223,26 @@ static void blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOC
v[14] = blake2b_IV[6] ^ S->f[0];
v[15] = blake2b_IV[7] ^ S->f[1];
#if OPTIMIZE_SIZE_BLAKE2B
for (int r = 0; r < 12; r++) {
ROUND(m, v, r);
}
#else
ROUND( m, v, 0 );
ROUND( m, v, 1 );
ROUND( m, v, 2 );
ROUND( m, v, 3 );
ROUND( m, v, 4 );
ROUND( m, v, 5 );
ROUND( m, v, 6 );
ROUND( m, v, 7 );
ROUND( m, v, 8 );
ROUND( m, v, 9 );
ROUND( m, v, 10 );
ROUND( m, v, 11 );
ROUND(m, v, 0);
ROUND(m, v, 1);
ROUND(m, v, 2);
ROUND(m, v, 3);
ROUND(m, v, 4);
ROUND(m, v, 5);
ROUND(m, v, 6);
ROUND(m, v, 7);
ROUND(m, v, 8);
ROUND(m, v, 9);
ROUND(m, v, 10);
ROUND(m, v, 11);
#endif
for( i = 0; i < 8; ++i ) {
for (i = 0; i < 8; ++i) {
S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
}
}
@ -271,59 +250,53 @@ static void blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOC
#undef G
#undef ROUND
int blake2b_Update( blake2b_state *S, const void *pin, size_t inlen )
{
const unsigned char * in = (const unsigned char *)pin;
if( inlen > 0 )
{
int blake2b_Update(blake2b_state *S, const void *pin, size_t inlen) {
const unsigned char *in = (const unsigned char *)pin;
if (inlen > 0) {
size_t left = S->buflen;
size_t fill = BLAKE2B_BLOCKBYTES - left;
if( inlen > fill )
{
if (inlen > fill) {
S->buflen = 0;
memcpy( S->buf + left, in, fill ); /* Fill buffer */
blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
blake2b_compress( S, S->buf ); /* Compress */
in += fill; inlen -= fill;
while(inlen > BLAKE2B_BLOCKBYTES) {
memcpy(S->buf + left, in, fill); /* Fill buffer */
blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
blake2b_compress( S, in );
blake2b_compress(S, S->buf); /* Compress */
in += fill;
inlen -= fill;
while (inlen > BLAKE2B_BLOCKBYTES) {
blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
blake2b_compress(S, in);
in += BLAKE2B_BLOCKBYTES;
inlen -= BLAKE2B_BLOCKBYTES;
}
}
memcpy( S->buf + S->buflen, in, inlen );
memcpy(S->buf + S->buflen, in, inlen);
S->buflen += inlen;
}
return 0;
}
int blake2b_Final( blake2b_state *S, void *out, size_t outlen )
{
int blake2b_Final(blake2b_state *S, void *out, size_t outlen) {
uint8_t buffer[BLAKE2B_OUTBYTES] = {0};
size_t i = 0;
if( out == NULL || outlen < S->outlen )
return -1;
if (out == NULL || outlen < S->outlen) return -1;
if( blake2b_is_lastblock( S ) )
return -1;
if (blake2b_is_lastblock(S)) return -1;
blake2b_increment_counter( S, S->buflen );
blake2b_set_lastblock( S );
memzero( S->buf + S->buflen, BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */
blake2b_compress( S, S->buf );
blake2b_increment_counter(S, S->buflen);
blake2b_set_lastblock(S);
memzero(S->buf + S->buflen, BLAKE2B_BLOCKBYTES - S->buflen); /* Padding */
blake2b_compress(S, S->buf);
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
for (i = 0; i < 8; ++i) /* Output full hash to temp buffer */
store64(buffer + sizeof(S->h[i]) * i, S->h[i]);
memcpy( out, buffer, S->outlen );
memcpy(out, buffer, S->outlen);
memzero(buffer, sizeof(buffer));
return 0;
}
int blake2b(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen)
{
int blake2b(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen) {
BLAKE2B_CTX ctx;
if (0 != blake2b_Init(&ctx, outlen)) return -1;
if (0 != blake2b_Update(&ctx, msg, msg_len)) return -1;
@ -331,8 +304,8 @@ int blake2b(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen)
return 0;
}
int blake2b_Key(const uint8_t *msg, uint32_t msg_len, const void *key, size_t keylen, void *out, size_t outlen)
{
int blake2b_Key(const uint8_t *msg, uint32_t msg_len, const void *key,
size_t keylen, void *out, size_t outlen) {
BLAKE2B_CTX ctx;
if (0 != blake2b_InitKey(&ctx, outlen, key, keylen)) return -1;
if (0 != blake2b_Update(&ctx, msg, msg_len)) return -1;

View File

@ -1,11 +1,10 @@
#ifndef __BLAKE2B_H__
#define __BLAKE2B_H__
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
enum blake2b_constant
{
enum blake2b_constant {
BLAKE2B_BLOCKBYTES = 128,
BLAKE2B_OUTBYTES = 64,
BLAKE2B_KEYBYTES = 64,
@ -13,8 +12,7 @@ enum blake2b_constant
BLAKE2B_PERSONALBYTES = 16
};
typedef struct __blake2b_state
{
typedef struct __blake2b_state {
uint64_t h[8];
uint64_t t[2];
uint64_t f[2];
@ -30,12 +28,15 @@ typedef struct __blake2b_state
#define BLAKE2B_KEY_LENGTH BLAKE2B_KEYBYTES
int blake2b_Init(blake2b_state *S, size_t outlen);
int blake2b_InitKey(blake2b_state *S, size_t outlen, const void *key, size_t keylen);
int blake2b_InitPersonal(blake2b_state *S, size_t outlen, const void *personal, size_t personal_len);
int blake2b_InitKey(blake2b_state *S, size_t outlen, const void *key,
size_t keylen);
int blake2b_InitPersonal(blake2b_state *S, size_t outlen, const void *personal,
size_t personal_len);
int blake2b_Update(blake2b_state *S, const void *pin, size_t inlen);
int blake2b_Final(blake2b_state *S, void *out, size_t outlen);
int blake2b(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen);
int blake2b_Key(const uint8_t *msg, uint32_t msg_len, const void *key, size_t keylen, void *out, size_t outlen);
int blake2b_Key(const uint8_t *msg, uint32_t msg_len, const void *key,
size_t keylen, void *out, size_t outlen);
#endif

View File

@ -15,13 +15,12 @@
#include <string.h>
#include "blake2s.h"
#include "blake2_common.h"
#include "blake2s.h"
#include "memzero.h"
#include "options.h"
typedef struct blake2s_param__
{
typedef struct blake2s_param__ {
uint8_t digest_length; /* 1 */
uint8_t key_length; /* 2 */
uint8_t fanout; /* 3 */
@ -36,202 +35,185 @@ typedef struct blake2s_param__
uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */
} __attribute__((packed)) blake2s_param;
static const uint32_t blake2s_IV[8] =
{
0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL
static const uint32_t blake2s_IV[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL,
0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL,
0x1F83D9ABUL, 0x5BE0CD19UL};
static const uint8_t blake2s_sigma[10][16] = {
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
{7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
{9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
{2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
{12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
{13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
{6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
{10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
};
static const uint8_t blake2s_sigma[10][16] =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
};
static void blake2s_set_lastnode( blake2s_state *S )
{
S->f[1] = (uint32_t)-1;
}
static void blake2s_set_lastnode(blake2s_state *S) { S->f[1] = (uint32_t)-1; }
/* Some helper functions, not necessarily useful */
static int blake2s_is_lastblock( const blake2s_state *S )
{
return S->f[0] != 0;
}
static int blake2s_is_lastblock(const blake2s_state *S) { return S->f[0] != 0; }
static void blake2s_set_lastblock( blake2s_state *S )
{
if( S->last_node ) blake2s_set_lastnode( S );
static void blake2s_set_lastblock(blake2s_state *S) {
if (S->last_node) blake2s_set_lastnode(S);
S->f[0] = (uint32_t)-1;
}
static void blake2s_increment_counter( blake2s_state *S, const uint32_t inc )
{
static void blake2s_increment_counter(blake2s_state *S, const uint32_t inc) {
S->t[0] += inc;
S->t[1] += ( S->t[0] < inc );
S->t[1] += (S->t[0] < inc);
}
static void blake2s_init0( blake2s_state *S )
{
static void blake2s_init0(blake2s_state *S) {
size_t i = 0;
memzero( S, sizeof( blake2s_state ) );
memzero(S, sizeof(blake2s_state));
for( i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i];
for (i = 0; i < 8; ++i) S->h[i] = blake2s_IV[i];
}
/* init2 xors IV with input parameter block */
int blake2s_init_param( blake2s_state *S, const blake2s_param *P )
{
const unsigned char *p = ( const unsigned char * )( P );
int blake2s_init_param(blake2s_state *S, const blake2s_param *P) {
const unsigned char *p = (const unsigned char *)(P);
size_t i = 0;
blake2s_init0( S );
blake2s_init0(S);
/* IV XOR ParamBlock */
for( i = 0; i < 8; ++i )
S->h[i] ^= load32( &p[i * 4] );
for (i = 0; i < 8; ++i) S->h[i] ^= load32(&p[i * 4]);
S->outlen = P->digest_length;
return 0;
}
/* Sequential blake2s initialization */
int blake2s_Init( blake2s_state *S, size_t outlen )
{
int blake2s_Init(blake2s_state *S, size_t outlen) {
blake2s_param P[1] = {0};
if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
if ((!outlen) || (outlen > BLAKE2S_OUTBYTES)) return -1;
P->digest_length = (uint8_t)outlen;
P->key_length = 0;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store32( &P->node_offset, 0 );
store16( &P->xof_length, 0 );
store32(&P->leaf_length, 0);
store32(&P->node_offset, 0);
store16(&P->xof_length, 0);
P->node_depth = 0;
P->inner_length = 0;
/* memzero(P->reserved, sizeof(P->reserved) ); */
memzero( P->salt, sizeof( P->salt ) );
memzero( P->personal, sizeof( P->personal ) );
return blake2s_init_param( S, P );
memzero(P->salt, sizeof(P->salt));
memzero(P->personal, sizeof(P->personal));
return blake2s_init_param(S, P);
}
int blake2s_InitPersonal( blake2s_state *S, size_t outlen, const void *personal, size_t personal_len)
{
int blake2s_InitPersonal(blake2s_state *S, size_t outlen, const void *personal,
size_t personal_len) {
blake2s_param P[1] = {0};
if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
if ( ( !personal ) || ( personal_len != BLAKE2S_PERSONALBYTES ) ) return -1;
if ((!outlen) || (outlen > BLAKE2S_OUTBYTES)) return -1;
if ((!personal) || (personal_len != BLAKE2S_PERSONALBYTES)) return -1;
P->digest_length = (uint8_t)outlen;
P->key_length = 0;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store32( &P->node_offset, 0 );
store16( &P->xof_length, 0 );
store32(&P->leaf_length, 0);
store32(&P->node_offset, 0);
store16(&P->xof_length, 0);
P->node_depth = 0;
P->inner_length = 0;
/* memzero(P->reserved, sizeof(P->reserved) ); */
memzero( P->salt, sizeof( P->salt ) );
memcpy( P->personal, personal, BLAKE2S_PERSONALBYTES );
return blake2s_init_param( S, P );
memzero(P->salt, sizeof(P->salt));
memcpy(P->personal, personal, BLAKE2S_PERSONALBYTES);
return blake2s_init_param(S, P);
}
int blake2s_InitKey( blake2s_state *S, size_t outlen, const void *key, size_t keylen )
{
int blake2s_InitKey(blake2s_state *S, size_t outlen, const void *key,
size_t keylen) {
blake2s_param P[1] = {0};
if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
if ((!outlen) || (outlen > BLAKE2S_OUTBYTES)) return -1;
if ( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return -1;
if (!key || !keylen || keylen > BLAKE2S_KEYBYTES) return -1;
P->digest_length = (uint8_t)outlen;
P->key_length = (uint8_t)keylen;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store32( &P->node_offset, 0 );
store16( &P->xof_length, 0 );
store32(&P->leaf_length, 0);
store32(&P->node_offset, 0);
store16(&P->xof_length, 0);
P->node_depth = 0;
P->inner_length = 0;
/* memzero(P->reserved, sizeof(P->reserved) ); */
memzero( P->salt, sizeof( P->salt ) );
memzero( P->personal, sizeof( P->personal ) );
memzero(P->salt, sizeof(P->salt));
memzero(P->personal, sizeof(P->personal));
if( blake2s_init_param( S, P ) < 0 ) return -1;
if (blake2s_init_param(S, P) < 0) return -1;
{
uint8_t block[BLAKE2S_BLOCKBYTES] = {0};
memzero( block, BLAKE2S_BLOCKBYTES );
memcpy( block, key, keylen );
blake2s_Update( S, block, BLAKE2S_BLOCKBYTES );
memzero( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */
memzero(block, BLAKE2S_BLOCKBYTES);
memcpy(block, key, keylen);
blake2s_Update(S, block, BLAKE2S_BLOCKBYTES);
memzero(block, BLAKE2S_BLOCKBYTES); /* Burn the key from stack */
}
return 0;
}
#define G(m,r,i,a,b,c,d) \
#define G(m, r, i, a, b, c, d) \
do { \
*(a) = *(a) + *(b) + m[blake2s_sigma[r][2*i+0]]; \
*(a) = *(a) + *(b) + m[blake2s_sigma[r][2 * i + 0]]; \
*(d) = rotr32(*(d) ^ *(a), 16); \
*(c) = *(c) + *(d); \
*(b) = rotr32(*(b) ^ *(c), 12); \
*(a) = *(a) + *(b) + m[blake2s_sigma[r][2*i+1]]; \
*(a) = *(a) + *(b) + m[blake2s_sigma[r][2 * i + 1]]; \
*(d) = rotr32(*(d) ^ *(a), 8); \
*(c) = *(c) + *(d); \
*(b) = rotr32(*(b) ^ *(c), 7); \
} while(0)
} while (0)
#if OPTIMIZE_SIZE_BLAKE2S
static void g(uint32_t *m, int r, int i, uint32_t *a, uint32_t *b, uint32_t *c,
uint32_t *d) {
G(m,r,i,a,b,c,d);
G(m, r, i, a, b, c, d);
}
#else
#define g(m,r,i,a,b,c,d) G(m,r,i,a,b,c,d)
#define g(m, r, i, a, b, c, d) G(m, r, i, a, b, c, d)
#endif
#define ROUND(m,v,r) \
#define ROUND(m, v, r) \
do { \
g(m,r,0,v + 0,v + 4,v + 8,v + 12); \
g(m,r,1,v + 1,v + 5,v + 9,v + 13); \
g(m,r,2,v + 2,v + 6,v + 10,v + 14); \
g(m,r,3,v + 3,v + 7,v + 11,v + 15); \
g(m,r,4,v + 0,v + 5,v + 10,v + 15); \
g(m,r,5,v + 1,v + 6,v + 11,v + 12); \
g(m,r,6,v + 2,v + 7,v + 8,v + 13); \
g(m,r,7,v + 3,v + 4,v + 9,v + 14); \
} while(0)
g(m, r, 0, v + 0, v + 4, v + 8, v + 12); \
g(m, r, 1, v + 1, v + 5, v + 9, v + 13); \
g(m, r, 2, v + 2, v + 6, v + 10, v + 14); \
g(m, r, 3, v + 3, v + 7, v + 11, v + 15); \
g(m, r, 4, v + 0, v + 5, v + 10, v + 15); \
g(m, r, 5, v + 1, v + 6, v + 11, v + 12); \
g(m, r, 6, v + 2, v + 7, v + 8, v + 13); \
g(m, r, 7, v + 3, v + 4, v + 9, v + 14); \
} while (0)
static void blake2s_compress( blake2s_state *S, const uint8_t in[BLAKE2S_BLOCKBYTES] )
{
static void blake2s_compress(blake2s_state *S,
const uint8_t in[BLAKE2S_BLOCKBYTES]) {
uint32_t m[16] = {0};
uint32_t v[16] = {0};
size_t i = 0;
for( i = 0; i < 16; ++i ) {
m[i] = load32( in + i * sizeof( m[i] ) );
for (i = 0; i < 16; ++i) {
m[i] = load32(in + i * sizeof(m[i]));
}
for( i = 0; i < 8; ++i ) {
for (i = 0; i < 8; ++i) {
v[i] = S->h[i];
}
v[ 8] = blake2s_IV[0];
v[ 9] = blake2s_IV[1];
v[8] = blake2s_IV[0];
v[9] = blake2s_IV[1];
v[10] = blake2s_IV[2];
v[11] = blake2s_IV[3];
v[12] = S->t[0] ^ blake2s_IV[4];
@ -244,19 +226,19 @@ static void blake2s_compress( blake2s_state *S, const uint8_t in[BLAKE2S_BLOCKBY
ROUND(m, v, r);
}
#else
ROUND( m, v, 0 );
ROUND( m, v, 1 );
ROUND( m, v, 2 );
ROUND( m, v, 3 );
ROUND( m, v, 4 );
ROUND( m, v, 5 );
ROUND( m, v, 6 );
ROUND( m, v, 7 );
ROUND( m, v, 8 );
ROUND( m, v, 9 );
ROUND(m, v, 0);
ROUND(m, v, 1);
ROUND(m, v, 2);
ROUND(m, v, 3);
ROUND(m, v, 4);
ROUND(m, v, 5);
ROUND(m, v, 6);
ROUND(m, v, 7);
ROUND(m, v, 8);
ROUND(m, v, 9);
#endif
for( i = 0; i < 8; ++i ) {
for (i = 0; i < 8; ++i) {
S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
}
}
@ -264,59 +246,53 @@ static void blake2s_compress( blake2s_state *S, const uint8_t in[BLAKE2S_BLOCKBY
#undef G
#undef ROUND
int blake2s_Update( blake2s_state *S, const void *pin, size_t inlen )
{
const unsigned char * in = (const unsigned char *)pin;
if( inlen > 0 )
{
int blake2s_Update(blake2s_state *S, const void *pin, size_t inlen) {
const unsigned char *in = (const unsigned char *)pin;
if (inlen > 0) {
size_t left = S->buflen;
size_t fill = BLAKE2S_BLOCKBYTES - left;
if( inlen > fill )
{
if (inlen > fill) {
S->buflen = 0;
memcpy( S->buf + left, in, fill ); /* Fill buffer */
blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
blake2s_compress( S, S->buf ); /* Compress */
in += fill; inlen -= fill;
while(inlen > BLAKE2S_BLOCKBYTES) {
memcpy(S->buf + left, in, fill); /* Fill buffer */
blake2s_increment_counter(S, BLAKE2S_BLOCKBYTES);
blake2s_compress( S, in );
blake2s_compress(S, S->buf); /* Compress */
in += fill;
inlen -= fill;
while (inlen > BLAKE2S_BLOCKBYTES) {
blake2s_increment_counter(S, BLAKE2S_BLOCKBYTES);
blake2s_compress(S, in);
in += BLAKE2S_BLOCKBYTES;
inlen -= BLAKE2S_BLOCKBYTES;
}
}
memcpy( S->buf + S->buflen, in, inlen );
memcpy(S->buf + S->buflen, in, inlen);
S->buflen += inlen;
}
return 0;
}
int blake2s_Final( blake2s_state *S, void *out, size_t outlen )
{
int blake2s_Final(blake2s_state *S, void *out, size_t outlen) {
uint8_t buffer[BLAKE2S_OUTBYTES] = {0};
size_t i = 0;
if( out == NULL || outlen < S->outlen )
return -1;
if (out == NULL || outlen < S->outlen) return -1;
if( blake2s_is_lastblock( S ) )
return -1;
if (blake2s_is_lastblock(S)) return -1;
blake2s_increment_counter( S, ( uint32_t )S->buflen );
blake2s_set_lastblock( S );
memzero( S->buf + S->buflen, BLAKE2S_BLOCKBYTES - S->buflen ); /* Padding */
blake2s_compress( S, S->buf );
blake2s_increment_counter(S, (uint32_t)S->buflen);
blake2s_set_lastblock(S);
memzero(S->buf + S->buflen, BLAKE2S_BLOCKBYTES - S->buflen); /* Padding */
blake2s_compress(S, S->buf);
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
store32( buffer + sizeof( S->h[i] ) * i, S->h[i] );
for (i = 0; i < 8; ++i) /* Output full hash to temp buffer */
store32(buffer + sizeof(S->h[i]) * i, S->h[i]);
memcpy( out, buffer, outlen );
memcpy(out, buffer, outlen);
memzero(buffer, sizeof(buffer));
return 0;
}
int blake2s(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen)
{
int blake2s(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen) {
BLAKE2S_CTX ctx;
if (0 != blake2s_Init(&ctx, outlen)) return -1;
if (0 != blake2s_Update(&ctx, msg, msg_len)) return -1;
@ -324,8 +300,8 @@ int blake2s(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen)
return 0;
}
int blake2s_Key(const uint8_t *msg, uint32_t msg_len, const void *key, size_t keylen, void *out, size_t outlen)
{
int blake2s_Key(const uint8_t *msg, uint32_t msg_len, const void *key,
size_t keylen, void *out, size_t outlen) {
BLAKE2S_CTX ctx;
if (0 != blake2s_InitKey(&ctx, outlen, key, keylen)) return -1;
if (0 != blake2s_Update(&ctx, msg, msg_len)) return -1;

View File

@ -1,11 +1,10 @@
#ifndef __BLAKE2S_H__
#define __BLAKE2S_H__
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
enum blake2s_constant
{
enum blake2s_constant {
BLAKE2S_BLOCKBYTES = 64,
BLAKE2S_OUTBYTES = 32,
BLAKE2S_KEYBYTES = 32,
@ -13,8 +12,7 @@ enum blake2s_constant
BLAKE2S_PERSONALBYTES = 8
};
typedef struct __blake2s_state
{
typedef struct __blake2s_state {
uint32_t h[8];
uint32_t t[2];
uint32_t f[2];
@ -30,12 +28,15 @@ typedef struct __blake2s_state
#define BLAKE2S_KEY_LENGTH BLAKE2S_KEYBYTES
int blake2s_Init(blake2s_state *S, size_t outlen);
int blake2s_InitKey(blake2s_state *S, size_t outlen, const void *key, size_t keylen);
int blake2s_InitPersonal(blake2s_state *S, size_t outlen, const void *personal, size_t personal_len);
int blake2s_InitKey(blake2s_state *S, size_t outlen, const void *key,
size_t keylen);
int blake2s_InitPersonal(blake2s_state *S, size_t outlen, const void *personal,
size_t personal_len);
int blake2s_Update(blake2s_state *S, const void *pin, size_t inlen);
int blake2s_Final(blake2s_state *S, void *out, size_t outlen);
int blake2s(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen);
int blake2s_Key(const uint8_t *msg, uint32_t msg_len, const void *key, size_t keylen, void *out, size_t outlen);
int blake2s_Key(const uint8_t *msg, uint32_t msg_len, const void *key,
size_t keylen, void *out, size_t outlen);
#endif

View File

@ -34,15 +34,14 @@
#include <stddef.h>
#include <string.h>
#include "groestl_internal.h"
#include "groestl.h"
#include "groestl_internal.h"
#include "memzero.h"
#define C32e(x) ((SPH_C32(x) >> 24) \
| ((SPH_C32(x) >> 8) & SPH_C32(0x0000FF00)) \
| ((SPH_C32(x) << 8) & SPH_C32(0x00FF0000)) \
| ((SPH_C32(x) << 24) & SPH_C32(0xFF000000)))
#define C32e(x) \
((SPH_C32(x) >> 24) | ((SPH_C32(x) >> 8) & SPH_C32(0x0000FF00)) | \
((SPH_C32(x) << 8) & SPH_C32(0x00FF0000)) | \
((SPH_C32(x) << 24) & SPH_C32(0xFF000000)))
#define dec32e_aligned sph_dec32le_aligned
#define enc32e sph_enc32le
#define B32_0(x) ((x) & 0xFF)
@ -58,7 +57,6 @@
#define QC32up(j, r) SPH_C32(0xFFFFFFFF)
#define QC32dn(j, r) (((sph_u32)(r) << 24) ^ SPH_T32(~((sph_u32)(j) << 24)))
static const sph_u32 T0up[] = {
C32e(0xc632f4a5), C32e(0xf86f9784), C32e(0xee5eb099), C32e(0xf67a8c8d),
C32e(0xffe8170d), C32e(0xd60adcbd), C32e(0xde16c8b1), C32e(0x916dfc54),
@ -123,8 +121,7 @@ static const sph_u32 T0up[] = {
C32e(0x03898a8f), C32e(0x594a13f8), C32e(0x09929b80), C32e(0x1a233917),
C32e(0x651075da), C32e(0xd7845331), C32e(0x84d551c6), C32e(0xd003d3b8),
C32e(0x82dc5ec3), C32e(0x29e2cbb0), C32e(0x5ac39977), C32e(0x1e2d3311),
C32e(0x7b3d46cb), C32e(0xa8b71ffc), C32e(0x6d0c61d6), C32e(0x2c624e3a)
};
C32e(0x7b3d46cb), C32e(0xa8b71ffc), C32e(0x6d0c61d6), C32e(0x2c624e3a)};
static const sph_u32 T0dn[] = {
C32e(0xf497a5c6), C32e(0x97eb84f8), C32e(0xb0c799ee), C32e(0x8cf78df6),
@ -190,8 +187,7 @@ static const sph_u32 T0dn[] = {
C32e(0x8a068f03), C32e(0x13b2f859), C32e(0x9b128009), C32e(0x3934171a),
C32e(0x75cada65), C32e(0x53b531d7), C32e(0x5113c684), C32e(0xd3bbb8d0),
C32e(0x5e1fc382), C32e(0xcb52b029), C32e(0x99b4775a), C32e(0x333c111e),
C32e(0x46f6cb7b), C32e(0x1f4bfca8), C32e(0x61dad66d), C32e(0x4e583a2c)
};
C32e(0x46f6cb7b), C32e(0x1f4bfca8), C32e(0x61dad66d), C32e(0x4e583a2c)};
static const sph_u32 T1up[] = {
C32e(0xc6c632f4), C32e(0xf8f86f97), C32e(0xeeee5eb0), C32e(0xf6f67a8c),
@ -257,8 +253,7 @@ static const sph_u32 T1up[] = {
C32e(0x0303898a), C32e(0x59594a13), C32e(0x0909929b), C32e(0x1a1a2339),
C32e(0x65651075), C32e(0xd7d78453), C32e(0x8484d551), C32e(0xd0d003d3),
C32e(0x8282dc5e), C32e(0x2929e2cb), C32e(0x5a5ac399), C32e(0x1e1e2d33),
C32e(0x7b7b3d46), C32e(0xa8a8b71f), C32e(0x6d6d0c61), C32e(0x2c2c624e)
};
C32e(0x7b7b3d46), C32e(0xa8a8b71f), C32e(0x6d6d0c61), C32e(0x2c2c624e)};
static const sph_u32 T1dn[] = {
C32e(0xa5f497a5), C32e(0x8497eb84), C32e(0x99b0c799), C32e(0x8d8cf78d),
@ -324,22 +319,20 @@ static const sph_u32 T1dn[] = {
C32e(0x8f8a068f), C32e(0xf813b2f8), C32e(0x809b1280), C32e(0x17393417),
C32e(0xda75cada), C32e(0x3153b531), C32e(0xc65113c6), C32e(0xb8d3bbb8),
C32e(0xc35e1fc3), C32e(0xb0cb52b0), C32e(0x7799b477), C32e(0x11333c11),
C32e(0xcb46f6cb), C32e(0xfc1f4bfc), C32e(0xd661dad6), C32e(0x3a4e583a)
};
C32e(0xcb46f6cb), C32e(0xfc1f4bfc), C32e(0xd661dad6), C32e(0x3a4e583a)};
#define DECL_STATE_BIG \
sph_u32 H[32] = {0};
#define DECL_STATE_BIG sph_u32 H[32] = {0};
#define READ_STATE_BIG(sc) do { \
#define READ_STATE_BIG(sc) \
do { \
memcpy(H, (sc)->state.narrow, sizeof H); \
} while (0)
#define WRITE_STATE_BIG(sc) do { \
#define WRITE_STATE_BIG(sc) \
do { \
memcpy((sc)->state.narrow, H, sizeof H); \
} while (0)
static void RBTT(size_t d0, size_t d1, sph_u32 *a, size_t b0, size_t b1,
size_t b2, size_t b3, size_t b4, size_t b5, size_t b6,
size_t b7, sph_u32 *t) {
@ -407,59 +400,52 @@ static void ROUND_BIG_Q(sph_u32 *a, int r) {
memcpy(a, t, sizeof(t));
}
#define PERM_BIG_P(a) do { \
#define PERM_BIG_P(a) \
do { \
int r; \
for (r = 0; r < 14; r ++) \
ROUND_BIG_P(a, r); \
for (r = 0; r < 14; r++) ROUND_BIG_P(a, r); \
} while (0)
#define PERM_BIG_Q(a) do { \
#define PERM_BIG_Q(a) \
do { \
int r; \
for (r = 0; r < 14; r ++) \
ROUND_BIG_Q(a, r); \
for (r = 0; r < 14; r++) ROUND_BIG_Q(a, r); \
} while (0)
#define COMPRESS_BIG do { \
#define COMPRESS_BIG \
do { \
sph_u32 g[32], m[32]; \
size_t uu; \
for (uu = 0; uu < 32; uu ++) { \
for (uu = 0; uu < 32; uu++) { \
m[uu] = dec32e_aligned(buf + (uu << 2)); \
g[uu] = m[uu] ^ H[uu]; \
} \
PERM_BIG_P(g); \
PERM_BIG_Q(m); \
for (uu = 0; uu < 32; uu ++) \
H[uu] ^= g[uu] ^ m[uu]; \
for (uu = 0; uu < 32; uu++) H[uu] ^= g[uu] ^ m[uu]; \
} while (0)
#define FINAL_BIG do { \
#define FINAL_BIG \
do { \
sph_u32 x[32]; \
size_t uu; \
memcpy(x, H, sizeof x); \
PERM_BIG_P(x); \
for (uu = 0; uu < 32; uu ++) \
H[uu] ^= x[uu]; \
for (uu = 0; uu < 32; uu++) H[uu] ^= x[uu]; \
} while (0)
static void
groestl_big_init(sph_groestl_big_context *sc, unsigned out_size)
{
static void groestl_big_init(sph_groestl_big_context *sc, unsigned out_size) {
size_t u = 0;
sc->ptr = 0;
for (u = 0; u < 31; u ++)
sc->state.narrow[u] = 0;
sc->state.narrow[31] = ((sph_u32)(out_size & 0xFF) << 24)
| ((sph_u32)(out_size & 0xFF00) << 8);
for (u = 0; u < 31; u++) sc->state.narrow[u] = 0;
sc->state.narrow[31] =
((sph_u32)(out_size & 0xFF) << 24) | ((sph_u32)(out_size & 0xFF00) << 8);
sc->count = 0;
}
static void
groestl_big_core(sph_groestl_big_context *sc, const void *data, size_t len)
{
static void groestl_big_core(sph_groestl_big_context *sc, const void *data,
size_t len) {
if (len == 0) {
return;
}
@ -482,15 +468,14 @@ groestl_big_core(sph_groestl_big_context *sc, const void *data, size_t len)
size_t clen = 0;
clen = (sizeof sc->buf) - ptr;
if (clen > len)
clen = len;
if (clen > len) clen = len;
memcpy(buf + ptr, data, clen);
ptr += clen;
data = (const unsigned char *)data + clen;
len -= clen;
if (ptr == sizeof sc->buf) {
COMPRESS_BIG;
sc->count ++;
sc->count++;
ptr = 0;
}
}
@ -498,10 +483,8 @@ groestl_big_core(sph_groestl_big_context *sc, const void *data, size_t len)
sc->ptr = ptr;
}
static void
groestl_big_close(sph_groestl_big_context *sc,
unsigned ub, unsigned n, void *dst, size_t out_len)
{
static void groestl_big_close(sph_groestl_big_context *sc, unsigned ub,
unsigned n, void *dst, size_t out_len) {
unsigned char pad[136] = {0};
size_t ptr = 0, pad_len = 0, u2 = 0;
sph_u64 count = 0;
@ -523,33 +506,24 @@ groestl_big_close(sph_groestl_big_context *sc,
groestl_big_core(sc, pad, pad_len);
READ_STATE_BIG(sc);
FINAL_BIG;
for (u2 = 0; u2 < 16; u2 ++)
enc32e(pad + (u2 << 2), H[u2 + 16]);
for (u2 = 0; u2 < 16; u2++) enc32e(pad + (u2 << 2), H[u2 + 16]);
memcpy(dst, pad + 64 - out_len, out_len);
groestl_big_init(sc, (unsigned)out_len << 3);
}
void
groestl512_Init(void *cc)
{
void groestl512_Init(void *cc) {
groestl_big_init((sph_groestl_big_context *)cc, 512);
}
void
groestl512_Update(void *cc, const void *data, size_t len)
{
void groestl512_Update(void *cc, const void *data, size_t len) {
groestl_big_core((sph_groestl_big_context *)cc, data, len);
}
void
groestl512_Final(void *cc, void *dst)
{
void groestl512_Final(void *cc, void *dst) {
groestl_big_close((sph_groestl_big_context *)cc, 0, 0, dst, 64);
}
void
groestl512_DoubleTrunc(void *cc, void *dst)
{
void groestl512_DoubleTrunc(void *cc, void *dst) {
char buf[64] = {0};
groestl512_Final(cc, buf);

View File

@ -116,7 +116,6 @@ typedef int64_t sph_s64;
#endif
#if defined SPH_DETECT_LITTLE_ENDIAN && !defined SPH_LITTLE_ENDIAN
#define SPH_LITTLE_ENDIAN SPH_DETECT_LITTLE_ENDIAN
#endif
@ -124,12 +123,9 @@ typedef int64_t sph_s64;
#define SPH_BIG_ENDIAN SPH_DETECT_BIG_ENDIAN
#endif
static inline sph_u32
sph_bswap32(sph_u32 x)
{
static inline sph_u32 sph_bswap32(sph_u32 x) {
x = SPH_T32((x << 16) | (x >> 16));
x = ((x & SPH_C32(0xFF00FF00)) >> 8)
| ((x & SPH_C32(0x00FF00FF)) << 8);
x = ((x & SPH_C32(0xFF00FF00)) >> 8) | ((x & SPH_C32(0x00FF00FF)) << 8);
return x;
}
@ -139,43 +135,33 @@ sph_bswap32(sph_u32 x)
* @param x the input value
* @return the byte-swapped value
*/
static inline sph_u64
sph_bswap64(sph_u64 x)
{
static inline sph_u64 sph_bswap64(sph_u64 x) {
x = SPH_T64((x << 32) | (x >> 32));
x = ((x & SPH_C64(0xFFFF0000FFFF0000)) >> 16)
| ((x & SPH_C64(0x0000FFFF0000FFFF)) << 16);
x = ((x & SPH_C64(0xFF00FF00FF00FF00)) >> 8)
| ((x & SPH_C64(0x00FF00FF00FF00FF)) << 8);
x = ((x & SPH_C64(0xFFFF0000FFFF0000)) >> 16) |
((x & SPH_C64(0x0000FFFF0000FFFF)) << 16);
x = ((x & SPH_C64(0xFF00FF00FF00FF00)) >> 8) |
((x & SPH_C64(0x00FF00FF00FF00FF)) << 8);
return x;
}
static inline void
sph_enc16be(void *dst, unsigned val)
{
static inline void sph_enc16be(void *dst, unsigned val) {
((unsigned char *)dst)[0] = (val >> 8);
((unsigned char *)dst)[1] = val;
}
static inline unsigned
sph_dec16be(const void *src)
{
return ((unsigned)(((const unsigned char *)src)[0]) << 8)
| (unsigned)(((const unsigned char *)src)[1]);
static inline unsigned sph_dec16be(const void *src) {
return ((unsigned)(((const unsigned char *)src)[0]) << 8) |
(unsigned)(((const unsigned char *)src)[1]);
}
static inline void
sph_enc16le(void *dst, unsigned val)
{
static inline void sph_enc16le(void *dst, unsigned val) {
((unsigned char *)dst)[0] = val;
((unsigned char *)dst)[1] = val >> 8;
}
static inline unsigned
sph_dec16le(const void *src)
{
return (unsigned)(((const unsigned char *)src)[0])
| ((unsigned)(((const unsigned char *)src)[1]) << 8);
static inline unsigned sph_dec16le(const void *src) {
return (unsigned)(((const unsigned char *)src)[0]) |
((unsigned)(((const unsigned char *)src)[1]) << 8);
}
/**
@ -184,9 +170,7 @@ sph_dec16le(const void *src)
* @param dst the destination buffer
* @param val the 32-bit value to encode
*/
static inline void
sph_enc32be(void *dst, sph_u32 val)
{
static inline void sph_enc32be(void *dst, sph_u32 val) {
((unsigned char *)dst)[0] = (val >> 24);
((unsigned char *)dst)[1] = (val >> 16);
((unsigned char *)dst)[2] = (val >> 8);
@ -200,9 +184,7 @@ sph_enc32be(void *dst, sph_u32 val)
* @param dst the destination buffer (32-bit aligned)
* @param val the value to encode
*/
static inline void
sph_enc32be_aligned(void *dst, sph_u32 val)
{
static inline void sph_enc32be_aligned(void *dst, sph_u32 val) {
#if SPH_LITTLE_ENDIAN
*(sph_u32 *)dst = sph_bswap32(val);
#elif SPH_BIG_ENDIAN
@ -221,13 +203,11 @@ sph_enc32be_aligned(void *dst, sph_u32 val)
* @param src the source buffer
* @return the decoded value
*/
static inline sph_u32
sph_dec32be(const void *src)
{
return ((sph_u32)(((const unsigned char *)src)[0]) << 24)
| ((sph_u32)(((const unsigned char *)src)[1]) << 16)
| ((sph_u32)(((const unsigned char *)src)[2]) << 8)
| (sph_u32)(((const unsigned char *)src)[3]);
static inline sph_u32 sph_dec32be(const void *src) {
return ((sph_u32)(((const unsigned char *)src)[0]) << 24) |
((sph_u32)(((const unsigned char *)src)[1]) << 16) |
((sph_u32)(((const unsigned char *)src)[2]) << 8) |
(sph_u32)(((const unsigned char *)src)[3]);
}
/**
@ -237,18 +217,16 @@ sph_dec32be(const void *src)
* @param src the source buffer (32-bit aligned)
* @return the decoded value
*/
static inline sph_u32
sph_dec32be_aligned(const void *src)
{
static inline sph_u32 sph_dec32be_aligned(const void *src) {
#if SPH_LITTLE_ENDIAN
return sph_bswap32(*(const sph_u32 *)src);
#elif SPH_BIG_ENDIAN
return *(const sph_u32 *)src;
#else
return ((sph_u32)(((const unsigned char *)src)[0]) << 24)
| ((sph_u32)(((const unsigned char *)src)[1]) << 16)
| ((sph_u32)(((const unsigned char *)src)[2]) << 8)
| (sph_u32)(((const unsigned char *)src)[3]);
return ((sph_u32)(((const unsigned char *)src)[0]) << 24) |
((sph_u32)(((const unsigned char *)src)[1]) << 16) |
((sph_u32)(((const unsigned char *)src)[2]) << 8) |
(sph_u32)(((const unsigned char *)src)[3]);
#endif
}
@ -258,9 +236,7 @@ sph_dec32be_aligned(const void *src)
* @param dst the destination buffer
* @param val the 32-bit value to encode
*/
static inline void
sph_enc32le(void *dst, sph_u32 val)
{
static inline void sph_enc32le(void *dst, sph_u32 val) {
((unsigned char *)dst)[0] = val;
((unsigned char *)dst)[1] = (val >> 8);
((unsigned char *)dst)[2] = (val >> 16);
@ -274,9 +250,7 @@ sph_enc32le(void *dst, sph_u32 val)
* @param dst the destination buffer (32-bit aligned)
* @param val the value to encode
*/
static inline void
sph_enc32le_aligned(void *dst, sph_u32 val)
{
static inline void sph_enc32le_aligned(void *dst, sph_u32 val) {
#if SPH_LITTLE_ENDIAN
*(sph_u32 *)dst = val;
#elif SPH_BIG_ENDIAN
@ -295,13 +269,11 @@ sph_enc32le_aligned(void *dst, sph_u32 val)
* @param src the source buffer
* @return the decoded value
*/
static inline sph_u32
sph_dec32le(const void *src)
{
return (sph_u32)(((const unsigned char *)src)[0])
| ((sph_u32)(((const unsigned char *)src)[1]) << 8)
| ((sph_u32)(((const unsigned char *)src)[2]) << 16)
| ((sph_u32)(((const unsigned char *)src)[3]) << 24);
static inline sph_u32 sph_dec32le(const void *src) {
return (sph_u32)(((const unsigned char *)src)[0]) |
((sph_u32)(((const unsigned char *)src)[1]) << 8) |
((sph_u32)(((const unsigned char *)src)[2]) << 16) |
((sph_u32)(((const unsigned char *)src)[3]) << 24);
}
/**
@ -311,18 +283,16 @@ sph_dec32le(const void *src)
* @param src the source buffer (32-bit aligned)
* @return the decoded value
*/
static inline sph_u32
sph_dec32le_aligned(const void *src)
{
static inline sph_u32 sph_dec32le_aligned(const void *src) {
#if SPH_LITTLE_ENDIAN
return *(const sph_u32 *)src;
#elif SPH_BIG_ENDIAN
return sph_bswap32(*(const sph_u32 *)src);
#else
return (sph_u32)(((const unsigned char *)src)[0])
| ((sph_u32)(((const unsigned char *)src)[1]) << 8)
| ((sph_u32)(((const unsigned char *)src)[2]) << 16)
| ((sph_u32)(((const unsigned char *)src)[3]) << 24);
return (sph_u32)(((const unsigned char *)src)[0]) |
((sph_u32)(((const unsigned char *)src)[1]) << 8) |
((sph_u32)(((const unsigned char *)src)[2]) << 16) |
((sph_u32)(((const unsigned char *)src)[3]) << 24);
#endif
}
@ -332,9 +302,7 @@ sph_dec32le_aligned(const void *src)
* @param dst the destination buffer
* @param val the 64-bit value to encode
*/
static inline void
sph_enc64be(void *dst, sph_u64 val)
{
static inline void sph_enc64be(void *dst, sph_u64 val) {
((unsigned char *)dst)[0] = (val >> 56);
((unsigned char *)dst)[1] = (val >> 48);
((unsigned char *)dst)[2] = (val >> 40);
@ -352,9 +320,7 @@ sph_enc64be(void *dst, sph_u64 val)
* @param dst the destination buffer (64-bit aligned)
* @param val the value to encode
*/
static inline void
sph_enc64be_aligned(void *dst, sph_u64 val)
{
static inline void sph_enc64be_aligned(void *dst, sph_u64 val) {
#if SPH_LITTLE_ENDIAN
*(sph_u64 *)dst = sph_bswap64(val);
#elif SPH_BIG_ENDIAN
@ -377,17 +343,15 @@ sph_enc64be_aligned(void *dst, sph_u64 val)
* @param src the source buffer
* @return the decoded value
*/
static inline sph_u64
sph_dec64be(const void *src)
{
return ((sph_u64)(((const unsigned char *)src)[0]) << 56)
| ((sph_u64)(((const unsigned char *)src)[1]) << 48)
| ((sph_u64)(((const unsigned char *)src)[2]) << 40)
| ((sph_u64)(((const unsigned char *)src)[3]) << 32)
| ((sph_u64)(((const unsigned char *)src)[4]) << 24)
| ((sph_u64)(((const unsigned char *)src)[5]) << 16)
| ((sph_u64)(((const unsigned char *)src)[6]) << 8)
| (sph_u64)(((const unsigned char *)src)[7]);
static inline sph_u64 sph_dec64be(const void *src) {
return ((sph_u64)(((const unsigned char *)src)[0]) << 56) |
((sph_u64)(((const unsigned char *)src)[1]) << 48) |
((sph_u64)(((const unsigned char *)src)[2]) << 40) |
((sph_u64)(((const unsigned char *)src)[3]) << 32) |
((sph_u64)(((const unsigned char *)src)[4]) << 24) |
((sph_u64)(((const unsigned char *)src)[5]) << 16) |
((sph_u64)(((const unsigned char *)src)[6]) << 8) |
(sph_u64)(((const unsigned char *)src)[7]);
}
/**
@ -397,22 +361,20 @@ sph_dec64be(const void *src)
* @param src the source buffer (64-bit aligned)
* @return the decoded value
*/
static inline sph_u64
sph_dec64be_aligned(const void *src)
{
static inline sph_u64 sph_dec64be_aligned(const void *src) {
#if SPH_LITTLE_ENDIAN
return sph_bswap64(*(const sph_u64 *)src);
#elif SPH_BIG_ENDIAN
return *(const sph_u64 *)src;
#else
return ((sph_u64)(((const unsigned char *)src)[0]) << 56)
| ((sph_u64)(((const unsigned char *)src)[1]) << 48)
| ((sph_u64)(((const unsigned char *)src)[2]) << 40)
| ((sph_u64)(((const unsigned char *)src)[3]) << 32)
| ((sph_u64)(((const unsigned char *)src)[4]) << 24)
| ((sph_u64)(((const unsigned char *)src)[5]) << 16)
| ((sph_u64)(((const unsigned char *)src)[6]) << 8)
| (sph_u64)(((const unsigned char *)src)[7]);
return ((sph_u64)(((const unsigned char *)src)[0]) << 56) |
((sph_u64)(((const unsigned char *)src)[1]) << 48) |
((sph_u64)(((const unsigned char *)src)[2]) << 40) |
((sph_u64)(((const unsigned char *)src)[3]) << 32) |
((sph_u64)(((const unsigned char *)src)[4]) << 24) |
((sph_u64)(((const unsigned char *)src)[5]) << 16) |
((sph_u64)(((const unsigned char *)src)[6]) << 8) |
(sph_u64)(((const unsigned char *)src)[7]);
#endif
}
@ -422,9 +384,7 @@ sph_dec64be_aligned(const void *src)
* @param dst the destination buffer
* @param val the 64-bit value to encode
*/
static inline void
sph_enc64le(void *dst, sph_u64 val)
{
static inline void sph_enc64le(void *dst, sph_u64 val) {
((unsigned char *)dst)[0] = val;
((unsigned char *)dst)[1] = (val >> 8);
((unsigned char *)dst)[2] = (val >> 16);
@ -442,9 +402,7 @@ sph_enc64le(void *dst, sph_u64 val)
* @param dst the destination buffer (64-bit aligned)
* @param val the value to encode
*/
static inline void
sph_enc64le_aligned(void *dst, sph_u64 val)
{
static inline void sph_enc64le_aligned(void *dst, sph_u64 val) {
#if SPH_LITTLE_ENDIAN
*(sph_u64 *)dst = val;
#elif SPH_BIG_ENDIAN
@ -467,17 +425,15 @@ sph_enc64le_aligned(void *dst, sph_u64 val)
* @param src the source buffer
* @return the decoded value
*/
static inline sph_u64
sph_dec64le(const void *src)
{
return (sph_u64)(((const unsigned char *)src)[0])
| ((sph_u64)(((const unsigned char *)src)[1]) << 8)
| ((sph_u64)(((const unsigned char *)src)[2]) << 16)
| ((sph_u64)(((const unsigned char *)src)[3]) << 24)
| ((sph_u64)(((const unsigned char *)src)[4]) << 32)
| ((sph_u64)(((const unsigned char *)src)[5]) << 40)
| ((sph_u64)(((const unsigned char *)src)[6]) << 48)
| ((sph_u64)(((const unsigned char *)src)[7]) << 56);
static inline sph_u64 sph_dec64le(const void *src) {
return (sph_u64)(((const unsigned char *)src)[0]) |
((sph_u64)(((const unsigned char *)src)[1]) << 8) |
((sph_u64)(((const unsigned char *)src)[2]) << 16) |
((sph_u64)(((const unsigned char *)src)[3]) << 24) |
((sph_u64)(((const unsigned char *)src)[4]) << 32) |
((sph_u64)(((const unsigned char *)src)[5]) << 40) |
((sph_u64)(((const unsigned char *)src)[6]) << 48) |
((sph_u64)(((const unsigned char *)src)[7]) << 56);
}
/**
@ -487,22 +443,20 @@ sph_dec64le(const void *src)
* @param src the source buffer (64-bit aligned)
* @return the decoded value
*/
static inline sph_u64
sph_dec64le_aligned(const void *src)
{
static inline sph_u64 sph_dec64le_aligned(const void *src) {
#if SPH_LITTLE_ENDIAN
return *(const sph_u64 *)src;
#elif SPH_BIG_ENDIAN
return sph_bswap64(*(const sph_u64 *)src);
#else
return (sph_u64)(((const unsigned char *)src)[0])
| ((sph_u64)(((const unsigned char *)src)[1]) << 8)
| ((sph_u64)(((const unsigned char *)src)[2]) << 16)
| ((sph_u64)(((const unsigned char *)src)[3]) << 24)
| ((sph_u64)(((const unsigned char *)src)[4]) << 32)
| ((sph_u64)(((const unsigned char *)src)[5]) << 40)
| ((sph_u64)(((const unsigned char *)src)[6]) << 48)
| ((sph_u64)(((const unsigned char *)src)[7]) << 56);
return (sph_u64)(((const unsigned char *)src)[0]) |
((sph_u64)(((const unsigned char *)src)[1]) << 8) |
((sph_u64)(((const unsigned char *)src)[2]) << 16) |
((sph_u64)(((const unsigned char *)src)[3]) << 24) |
((sph_u64)(((const unsigned char *)src)[4]) << 32) |
((sph_u64)(((const unsigned char *)src)[5]) << 40) |
((sph_u64)(((const unsigned char *)src)[6]) << 48) |
((sph_u64)(((const unsigned char *)src)[7]) << 56);
#endif
}

View File

@ -3,9 +3,7 @@
^\./crypto/chacha20poly1305/
^\./crypto/ed25519-donna/
^\./crypto/gui/
^\./crypto/blake2
^\./crypto/check_mem
^\./crypto/groestl
^\./crypto/ripemd160
^\./crypto/segwit_addr
^\./crypto/sha2