Welcome to mirror list, hosted at ThFree Co, Russian Federation.

blake2xs-ref.c « ref - github.com/BLAKE2/BLAKE2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c6145e245aa275ce9c925cb607e9bcaacc22358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include "blake2.h"
#include "blake2-impl.h"


int blake2xs_init( blake2s_state *S, const uint16_t outlen, const void *key, const uint8_t keylen)
{
  blake2s_param P[1];

  if ( !outlen ) return -1;

  P->digest_length = BLAKE2S_OUTBYTES;
  P->key_length    = keylen;
  P->fanout        = 1;
  P->depth         = 1;
  store32( &P->leaf_length, 0 );
  store32( &P->node_offset, 0 );
  store16( &P->xof_length, outlen );
  P->node_depth    = 0;
  P->inner_length  = 0;
  memset( P->salt,     0, sizeof( P->salt ) );
  memset( P->personal, 0, sizeof( P->personal ) );

  if( blake2s_init_param( S, P ) < 0 ) return -1;

    if (keylen) 
    {
        if ( !key || keylen > BLAKE2S_KEYBYTES ) return -1;
        uint8_t block[BLAKE2S_BLOCKBYTES];
        memset( block, 0, BLAKE2S_BLOCKBYTES );
        memcpy( block, key, keylen );
        blake2s_update( S, block, BLAKE2S_BLOCKBYTES );
        secure_zero_memory( block, BLAKE2S_BLOCKBYTES );
    }
  return 0;
}


int blake2xs_update( blake2s_state *S, const uint8_t *in, uint64_t inlen )
{
    if ( !in ) return -1;

    return blake2s_update( S, in, inlen );

}

int blake2xs_final( blake2s_state *S, uint8_t *out, const uint16_t outlen ) {

    blake2s_state C[1];
    blake2s_param P[1];
    int i;
    uint16_t last_index = outlen/BLAKE2S_OUTBYTES;
    uint8_t root[BLAKE2S_BLOCKBYTES];

    if ( !out ) return 1;

    /* Finalize the root hash */
    if (blake2s_final( S, root, BLAKE2S_OUTBYTES ) < 0 )
        return -1;

    /* Set common block structure values */
    P->digest_length = BLAKE2S_OUTBYTES;
    P->key_length    = 0;
    P->fanout        = 0; 
    P->depth         = 0;
    store32( &P->leaf_length, BLAKE2S_OUTBYTES );
    store16( &P->xof_length, outlen );
    P->inner_length  = 0;
    P->node_depth    = 0;
    memset( P->salt,     0, sizeof( P->salt ) );
    memset( P->personal, 0, sizeof( P->personal ) );

    for( i = 0; i < last_index; ++ i ) {
        /* Initialize state */
        store32( &P->node_offset, i );
        if( blake2s_init_param( C, P ) < 0 ) return -1;
        /* Process key if needed */
        blake2s_update( C, root, BLAKE2S_OUTBYTES);
        blake2s_final( C, out + i*BLAKE2S_OUTBYTES, BLAKE2S_OUTBYTES );
    }
    /* Last instance */
    store32( &P->node_offset, last_index);
    P->digest_length = outlen % BLAKE2S_OUTBYTES;
    if( blake2s_init_param( C, P ) < 0 ) return -1;
    blake2s_update( C, root, BLAKE2S_OUTBYTES);
    blake2s_final( C, out + last_index*BLAKE2S_OUTBYTES, outlen % BLAKE2S_OUTBYTES );
     
     return 0;
}

int blake2xs( uint8_t *out, const void *in, const void *key, const
uint16_t outlen, const uint64_t inlen, const uint8_t keylen ) 
{
    blake2s_state S[1];

    /* Verify parameters */
    if ( NULL == in && inlen > 0 ) return -1;

    if ( NULL == out ) return -1;

    if ( NULL == key && keylen > 0) return -1;

    if( keylen > BLAKE2S_KEYBYTES ) return -1;

    if( !outlen ) return -1;

    /* Initialize the root block structure */
    if ( blake2xs_init( S, outlen, key, keylen ) < 0 ) {
        return -1;
    }

    /* Compute the root of the tree */
    if ( blake2xs_update( S, ( const uint8_t * )in, inlen ) < 0 )
        return -1;

    /* Compute the final hash using the counter construction */
    blake2xs_final( S, out, outlen );

    return 0;
}

#if defined(BLAKE2XS_SELFTEST)
#include <string.h>
#include "blake2-kat.h"
int main( int argc, char **argv )
{
  uint8_t key[BLAKE2S_KEYBYTES];
  uint8_t buf[BLAKE2_KAT_LENGTH];

  for( size_t i = 0; i < BLAKE2S_KEYBYTES; ++i )
    key[i] = ( uint8_t )i;

  for( size_t i = 0; i < BLAKE2_KAT_LENGTH; ++i )
    buf[i] = ( uint8_t )i;

  for( size_t i = 0; i < BLAKE2_KAT_LENGTH; ++i )
  {
      uint8_t hash[BLAKE2_KAT_LENGTH];
      blake2xs( hash, buf, key, i, BLAKE2_KAT_LENGTH, BLAKE2S_KEYBYTES );

      for( size_t j = 0; j < i; ++j ) {
          printf("%02x", hash[j]);
      }
      printf("\n");
  }

  //puts( "ok" );
  return 0;
}
#endif