dedup

data deduplication program
git clone git://git.2f30.org/dedup.git
Log | Files | Refs | README | LICENSE

blake2sp-ref.c (9286B)


      1 /*
      2    BLAKE2 reference source code package - reference C implementations
      3 
      4    Copyright 2012, Samuel Neves <sneves@dei.uc.pt>.  You may use this under the
      5    terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
      6    your option.  The terms of these licenses can be found at:
      7 
      8    - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
      9    - OpenSSL license   : https://www.openssl.org/source/license.html
     10    - Apache 2.0        : http://www.apache.org/licenses/LICENSE-2.0
     11 
     12    More information about the BLAKE2 hash function can be found at
     13    https://blake2.net.
     14 */
     15 
     16 #include <stdlib.h>
     17 #include <string.h>
     18 #include <stdio.h>
     19 
     20 #if defined(_OPENMP)
     21 #include <omp.h>
     22 #endif
     23 
     24 #include "blake2.h"
     25 #include "blake2-impl.h"
     26 
     27 #define PARALLELISM_DEGREE 8
     28 
     29 /*
     30   blake2sp_init_param defaults to setting the expecting output length
     31   from the digest_length parameter block field.
     32 
     33   In some cases, however, we do not want this, as the output length
     34   of these instances is given by inner_length instead.
     35 */
     36 static int blake2sp_init_leaf_param( blake2s_state *S, const blake2s_param *P )
     37 {
     38   int err = blake2s_init_param(S, P);
     39   S->outlen = P->inner_length;
     40   return err;
     41 }
     42 
     43 static int blake2sp_init_leaf( blake2s_state *S, size_t outlen, size_t keylen, uint64_t offset )
     44 {
     45   blake2s_param P[1];
     46   P->digest_length = (uint8_t)outlen;
     47   P->key_length = (uint8_t)keylen;
     48   P->fanout = PARALLELISM_DEGREE;
     49   P->depth = 2;
     50   store32( &P->leaf_length, 0 );
     51   store32( &P->node_offset, offset );
     52   store16( &P->xof_length, 0 );
     53   P->node_depth = 0;
     54   P->inner_length = BLAKE2S_OUTBYTES;
     55   memset( P->salt, 0, sizeof( P->salt ) );
     56   memset( P->personal, 0, sizeof( P->personal ) );
     57   return blake2sp_init_leaf_param( S, P );
     58 }
     59 
     60 static int blake2sp_init_root( blake2s_state *S, size_t outlen, size_t keylen )
     61 {
     62   blake2s_param P[1];
     63   P->digest_length = (uint8_t)outlen;
     64   P->key_length = (uint8_t)keylen;
     65   P->fanout = PARALLELISM_DEGREE;
     66   P->depth = 2;
     67   store32( &P->leaf_length, 0 );
     68   store32( &P->node_offset, 0 );
     69   store16( &P->xof_length, 0 );
     70   P->node_depth = 1;
     71   P->inner_length = BLAKE2S_OUTBYTES;
     72   memset( P->salt, 0, sizeof( P->salt ) );
     73   memset( P->personal, 0, sizeof( P->personal ) );
     74   return blake2s_init_param( S, P );
     75 }
     76 
     77 
     78 int blake2sp_init( blake2sp_state *S, size_t outlen )
     79 {
     80   size_t i;
     81 
     82   if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
     83 
     84   memset( S->buf, 0, sizeof( S->buf ) );
     85   S->buflen = 0;
     86   S->outlen = outlen;
     87 
     88   if( blake2sp_init_root( S->R, outlen, 0 ) < 0 )
     89     return -1;
     90 
     91   for( i = 0; i < PARALLELISM_DEGREE; ++i )
     92     if( blake2sp_init_leaf( S->S[i], outlen, 0, i ) < 0 ) return -1;
     93 
     94   S->R->last_node = 1;
     95   S->S[PARALLELISM_DEGREE - 1]->last_node = 1;
     96   return 0;
     97 }
     98 
     99 int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen )
    100 {
    101   size_t i;
    102 
    103   if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
    104 
    105   if( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return -1;
    106 
    107   memset( S->buf, 0, sizeof( S->buf ) );
    108   S->buflen = 0;
    109   S->outlen = outlen;
    110 
    111   if( blake2sp_init_root( S->R, outlen, keylen ) < 0 )
    112     return -1;
    113 
    114   for( i = 0; i < PARALLELISM_DEGREE; ++i )
    115     if( blake2sp_init_leaf( S->S[i], outlen, keylen, i ) < 0 ) return -1;
    116 
    117   S->R->last_node = 1;
    118   S->S[PARALLELISM_DEGREE - 1]->last_node = 1;
    119   {
    120     uint8_t block[BLAKE2S_BLOCKBYTES];
    121     memset( block, 0, BLAKE2S_BLOCKBYTES );
    122     memcpy( block, key, keylen );
    123 
    124     for( i = 0; i < PARALLELISM_DEGREE; ++i )
    125       blake2s_update( S->S[i], block, BLAKE2S_BLOCKBYTES );
    126 
    127     secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */
    128   }
    129   return 0;
    130 }
    131 
    132 
    133 int blake2sp_update( blake2sp_state *S, const void *pin, size_t inlen )
    134 {
    135   const unsigned char * in = (const unsigned char *)pin;
    136   size_t left = S->buflen;
    137   size_t fill = sizeof( S->buf ) - left;
    138   size_t i;
    139 
    140   if( left && inlen >= fill )
    141   {
    142     memcpy( S->buf + left, in, fill );
    143 
    144     for( i = 0; i < PARALLELISM_DEGREE; ++i )
    145       blake2s_update( S->S[i], S->buf + i * BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES );
    146 
    147     in += fill;
    148     inlen -= fill;
    149     left = 0;
    150   }
    151 
    152 #if defined(_OPENMP)
    153   #pragma omp parallel shared(S), num_threads(PARALLELISM_DEGREE)
    154 #else
    155   for( i = 0; i < PARALLELISM_DEGREE; ++i )
    156 #endif
    157   {
    158 #if defined(_OPENMP)
    159     size_t      i = omp_get_thread_num();
    160 #endif
    161     size_t inlen__ = inlen;
    162     const unsigned char *in__ = ( const unsigned char * )in;
    163     in__ += i * BLAKE2S_BLOCKBYTES;
    164 
    165     while( inlen__ >= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES )
    166     {
    167       blake2s_update( S->S[i], in__, BLAKE2S_BLOCKBYTES );
    168       in__ += PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
    169       inlen__ -= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
    170     }
    171   }
    172 
    173   in += inlen - inlen % ( PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES );
    174   inlen %= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
    175 
    176   if( inlen > 0 )
    177     memcpy( S->buf + left, in, inlen );
    178 
    179   S->buflen = left + inlen;
    180   return 0;
    181 }
    182 
    183 
    184 int blake2sp_final( blake2sp_state *S, void *out, size_t outlen )
    185 {
    186   uint8_t hash[PARALLELISM_DEGREE][BLAKE2S_OUTBYTES];
    187   size_t i;
    188 
    189   if(out == NULL || outlen < S->outlen) {
    190     return -1;
    191   }
    192 
    193   for( i = 0; i < PARALLELISM_DEGREE; ++i )
    194   {
    195     if( S->buflen > i * BLAKE2S_BLOCKBYTES )
    196     {
    197       size_t left = S->buflen - i * BLAKE2S_BLOCKBYTES;
    198 
    199       if( left > BLAKE2S_BLOCKBYTES ) left = BLAKE2S_BLOCKBYTES;
    200 
    201       blake2s_update( S->S[i], S->buf + i * BLAKE2S_BLOCKBYTES, left );
    202     }
    203 
    204     blake2s_final( S->S[i], hash[i], BLAKE2S_OUTBYTES );
    205   }
    206 
    207   for( i = 0; i < PARALLELISM_DEGREE; ++i )
    208     blake2s_update( S->R, hash[i], BLAKE2S_OUTBYTES );
    209 
    210   return blake2s_final( S->R, out, S->outlen );
    211 }
    212 
    213 
    214 int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen )
    215 {
    216   uint8_t hash[PARALLELISM_DEGREE][BLAKE2S_OUTBYTES];
    217   blake2s_state S[PARALLELISM_DEGREE][1];
    218   blake2s_state FS[1];
    219   size_t i;
    220 
    221   /* Verify parameters */
    222   if ( NULL == in && inlen > 0 ) return -1;
    223 
    224   if ( NULL == out ) return -1;
    225 
    226   if ( NULL == key && keylen > 0) return -1;
    227 
    228   if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
    229 
    230   if( keylen > BLAKE2S_KEYBYTES ) return -1;
    231 
    232   for( i = 0; i < PARALLELISM_DEGREE; ++i )
    233     if( blake2sp_init_leaf( S[i], outlen, keylen, i ) < 0 ) return -1;
    234 
    235   S[PARALLELISM_DEGREE - 1]->last_node = 1; /* mark last node */
    236 
    237   if( keylen > 0 )
    238   {
    239     uint8_t block[BLAKE2S_BLOCKBYTES];
    240     memset( block, 0, BLAKE2S_BLOCKBYTES );
    241     memcpy( block, key, keylen );
    242 
    243     for( i = 0; i < PARALLELISM_DEGREE; ++i )
    244       blake2s_update( S[i], block, BLAKE2S_BLOCKBYTES );
    245 
    246     secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */
    247   }
    248 
    249 #if defined(_OPENMP)
    250   #pragma omp parallel shared(S,hash), num_threads(PARALLELISM_DEGREE)
    251 #else
    252 
    253   for( i = 0; i < PARALLELISM_DEGREE; ++i )
    254 #endif
    255   {
    256 #if defined(_OPENMP)
    257     size_t      i = omp_get_thread_num();
    258 #endif
    259     size_t inlen__ = inlen;
    260     const unsigned char *in__ = ( const unsigned char * )in;
    261     in__ += i * BLAKE2S_BLOCKBYTES;
    262 
    263     while( inlen__ >= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES )
    264     {
    265       blake2s_update( S[i], in__, BLAKE2S_BLOCKBYTES );
    266       in__ += PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
    267       inlen__ -= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
    268     }
    269 
    270     if( inlen__ > i * BLAKE2S_BLOCKBYTES )
    271     {
    272       const size_t left = inlen__ - i * BLAKE2S_BLOCKBYTES;
    273       const size_t len = left <= BLAKE2S_BLOCKBYTES ? left : BLAKE2S_BLOCKBYTES;
    274       blake2s_update( S[i], in__, len );
    275     }
    276 
    277     blake2s_final( S[i], hash[i], BLAKE2S_OUTBYTES );
    278   }
    279 
    280   if( blake2sp_init_root( FS, outlen, keylen ) < 0 )
    281     return -1;
    282 
    283   FS->last_node = 1;
    284 
    285   for( i = 0; i < PARALLELISM_DEGREE; ++i )
    286     blake2s_update( FS, hash[i], BLAKE2S_OUTBYTES );
    287 
    288   return blake2s_final( FS, out, outlen );
    289 }
    290 
    291 
    292 
    293 #if defined(BLAKE2SP_SELFTEST)
    294 #include <string.h>
    295 #include "blake2-kat.h"
    296 int main( void )
    297 {
    298   uint8_t key[BLAKE2S_KEYBYTES];
    299   uint8_t buf[BLAKE2_KAT_LENGTH];
    300   size_t i, step;
    301 
    302   for( i = 0; i < BLAKE2S_KEYBYTES; ++i )
    303     key[i] = ( uint8_t )i;
    304 
    305   for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
    306     buf[i] = ( uint8_t )i;
    307 
    308   /* Test simple API */
    309   for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
    310   {
    311     uint8_t hash[BLAKE2S_OUTBYTES];
    312     blake2sp( hash, BLAKE2S_OUTBYTES, buf, i, key, BLAKE2S_KEYBYTES );
    313 
    314     if( 0 != memcmp( hash, blake2sp_keyed_kat[i], BLAKE2S_OUTBYTES ) )
    315     {
    316       goto fail;
    317     }
    318   }
    319 
    320   /* Test streaming API */
    321   for(step = 1; step < BLAKE2S_BLOCKBYTES; ++step) {
    322     for (i = 0; i < BLAKE2_KAT_LENGTH; ++i) {
    323       uint8_t hash[BLAKE2S_OUTBYTES];
    324       blake2sp_state S;
    325       uint8_t * p = buf;
    326       size_t mlen = i;
    327       int err = 0;
    328 
    329       if( (err = blake2sp_init_key(&S, BLAKE2S_OUTBYTES, key, BLAKE2S_KEYBYTES)) < 0 ) {
    330         goto fail;
    331       }
    332 
    333       while (mlen >= step) {
    334         if ( (err = blake2sp_update(&S, p, step)) < 0 ) {
    335           goto fail;
    336         }
    337         mlen -= step;
    338         p += step;
    339       }
    340       if ( (err = blake2sp_update(&S, p, mlen)) < 0) {
    341         goto fail;
    342       }
    343       if ( (err = blake2sp_final(&S, hash, BLAKE2S_OUTBYTES)) < 0) {
    344         goto fail;
    345       }
    346 
    347       if (0 != memcmp(hash, blake2sp_keyed_kat[i], BLAKE2S_OUTBYTES)) {
    348         goto fail;
    349       }
    350     }
    351   }
    352 
    353   puts( "ok" );
    354   return 0;
    355 fail:
    356   puts("error");
    357   return -1;
    358 }
    359 #endif