ref: 51929bdcc9e09ac10c2d21aea6142111d4d43ab7
parent: 1ebef3229f78a771d2acf5a310efac225e13d826
author: Jacob Moody <moody@posixcafe.org>
date: Sat May 17 20:14:15 EDT 2025
port blake2sp, blake2bp, and b2sum
--- a/README
+++ b/README
@@ -1,5 +1,11 @@
A port of the standard reference implementation of BLAKE2 to native 9front.
+This repo builds the 'b2sum' command line utility and can run the standard tests.
-# Building
-; cd ref
-; mk run # compile and run tests
+# Building b2sum
+; mk
+
+# Installing b2sum
+; mk install
+
+# Running self tests
+; mk test
--- a/b2sum/b2sum.c
+++ b/b2sum/b2sum.c
@@ -13,27 +13,19 @@
https://blake2.net.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include <errno.h>
+#include <u.h>
+#include <libc.h>
-#include <ctype.h>
-#include <unistd.h>
-#include <getopt.h>
-#include <stdbool.h>
-
#include "blake2.h"
/* This will help compatibility with coreutils */
-int blake2s_stream( FILE *stream, void *resstream, size_t outbytes )
+int blake2s_stream( int stream, void *resstream, int outbytes )
{
int ret = -1;
- size_t sum, n;
+ int sum, n;
blake2s_state S[1];
- static const size_t buffer_length = 32768;
- uint8_t *buffer = ( uint8_t * )malloc( buffer_length );
+ static const int buffer_length = 32768;
+ u8int *buffer = ( u8int * )malloc( buffer_length );
if( !buffer ) return -1;
@@ -45,22 +37,17 @@
while( 1 )
{
- n = fread( buffer + sum, 1, buffer_length - sum, stream );
+ n = read( stream, buffer + sum, buffer_length - sum );
sum += n;
if( buffer_length == sum )
break;
- if( 0 == n )
- {
- if( ferror( stream ) )
- goto cleanup_buffer;
+ if( n < 0 )
+ goto cleanup_buffer;
+ if( 0 == n )
goto final_process;
- }
-
- if( feof( stream ) )
- goto final_process;
}
blake2s_update( S, buffer, buffer_length );
@@ -77,13 +64,13 @@
return ret;
}
-int blake2b_stream( FILE *stream, void *resstream, size_t outbytes )
+int blake2b_stream( int stream, void *resstream, int outbytes )
{
int ret = -1;
- size_t sum, n;
+ int sum, n;
blake2b_state S[1];
- static const size_t buffer_length = 32768;
- uint8_t *buffer = ( uint8_t * )malloc( buffer_length );
+ static const int buffer_length = 32768;
+ u8int *buffer = ( u8int * )malloc( buffer_length );
if( !buffer ) return -1;
@@ -95,22 +82,17 @@
while( 1 )
{
- n = fread( buffer + sum, 1, buffer_length - sum, stream );
+ n = read( stream, buffer + sum, buffer_length - sum );
sum += n;
if( buffer_length == sum )
break;
- if( 0 == n )
- {
- if( ferror( stream ) )
- goto cleanup_buffer;
+ if( n < 0 )
+ goto cleanup_buffer;
+ if( 0 == n )
goto final_process;
- }
-
- if( feof( stream ) )
- goto final_process;
}
blake2b_update( S, buffer, buffer_length );
@@ -127,13 +109,13 @@
return ret;
}
-int blake2sp_stream( FILE *stream, void *resstream, size_t outbytes )
+int blake2sp_stream( int stream, void *resstream, int outbytes )
{
int ret = -1;
- size_t sum, n;
+ int sum, n;
blake2sp_state S[1];
- static const size_t buffer_length = 16 * ( 1UL << 20 );
- uint8_t *buffer = ( uint8_t * )malloc( buffer_length );
+ static const int buffer_length = 16 * ( 1UL << 20 );
+ u8int *buffer = ( u8int * )malloc( buffer_length );
if( !buffer ) return -1;
@@ -145,22 +127,17 @@
while( 1 )
{
- n = fread( buffer + sum, 1, buffer_length - sum, stream );
+ n = read( stream, buffer + sum, buffer_length - sum );
sum += n;
if( buffer_length == sum )
break;
- if( 0 == n )
- {
- if( ferror( stream ) )
- goto cleanup_buffer;
+ if( n < 0 )
+ goto cleanup_buffer;
+ if( 0 == n )
goto final_process;
- }
-
- if( feof( stream ) )
- goto final_process;
}
blake2sp_update( S, buffer, buffer_length );
@@ -178,13 +155,13 @@
}
-int blake2bp_stream( FILE *stream, void *resstream, size_t outbytes )
+int blake2bp_stream( int stream, void *resstream, int outbytes )
{
int ret = -1;
- size_t sum, n;
+ int sum, n;
blake2bp_state S[1];
- static const size_t buffer_length = 16 * ( 1UL << 20 );
- uint8_t *buffer = ( uint8_t * )malloc( buffer_length );
+ static const int buffer_length = 16 * ( 1UL << 20 );
+ u8int *buffer = ( u8int * )malloc( buffer_length );
if( !buffer ) return -1;
@@ -196,22 +173,17 @@
while( 1 )
{
- n = fread( buffer + sum, 1, buffer_length - sum, stream );
+ n = read( stream, buffer + sum, buffer_length - sum );
sum += n;
if( buffer_length == sum )
break;
- if( 0 == n )
- {
- if( ferror( stream ) )
- goto cleanup_buffer;
+ if( n < 0 )
+ goto cleanup_buffer;
+ if( 0 == n )
goto final_process;
- }
-
- if( feof( stream ) )
- goto final_process;
}
blake2bp_update( S, buffer, buffer_length );
@@ -228,26 +200,44 @@
return ret;
}
-typedef int ( *blake2fn )( FILE *, void *, size_t );
+typedef int ( *blake2fn )( int, void *, int );
-static void usage( char **argv, int errcode )
+static void usage( void )
{
- FILE *out = errcode ? stderr : stdout;
- fprintf( out, "Usage: %s [OPTION]... [FILE]...\n", argv[0] );
- fprintf( out, "\n" );
- fprintf( out, "With no FILE, or when FILE is -, read standard input.\n" );
- fprintf( out, "\n" );
- fprintf( out, " -a <algo> hash algorithm (blake2b is default): \n"
+ fprint( 2, "Usage: %s [OPTION]... [FILE]...\n", argv0 );
+ fprint( 2, "\n" );
+ fprint( 2, "With no FILE, or when FILE is -, read standard input.\n" );
+ fprint( 2, "\n" );
+ fprint( 2, " -a <algo> hash algorithm (blake2b is default): \n"
" [blake2b|blake2s|blake2bp|blake2sp]\n" );
- fprintf( out, " -l <length> digest length in bits, must not exceed the maximum for\n"
+ fprint( 2, " -l <length> digest length in bits, must not exceed the maximum for\n"
" the selected algorithm and must be a multiple of 8\n" );
- fprintf( out, " --tag create a BSD-style checksum\n" );
- fprintf( out, " --help display this help and exit\n" );
- exit( errcode );
+ fprint( 2, " --tag create a BSD-style checksum\n" );
+ fprint( 2, " --help display this help and exit\n" );
+ exits("usage");
}
+void hash( char *name, int fd, blake2fn fn, int outbytes )
+{
+ unsigned char hash[BLAKE2B_OUTBYTES] = {0};
+
+ if( fn( fd, hash, outbytes ) < 0 )
+ {
+ fprint( 2, "failed to hash '%s'\n", name);
+ return;
+ }
+ else
+ {
+ int j;
+ for( j = 0; j < outbytes; ++j )
+ print( "%02x", hash[j] );
+
+ print("\n");
+ }
+}
+
int main( int argc, char **argv )
{
blake2fn blake2_stream = blake2b_stream;
@@ -254,134 +244,75 @@
unsigned long maxbytes = BLAKE2B_OUTBYTES;
const char *algorithm = "BLAKE2b";
unsigned long outbytes = 0;
- unsigned char hash[BLAKE2B_OUTBYTES] = {0};
- bool bsdstyle = false;
- int c, i;
- opterr = 1;
+ int i;
+ char *algo;
+ ulong outbits;
- while( 1 )
- {
- int option_index = 0;
- char *end = NULL;
- unsigned long outbits;
- static struct option long_options[] = {
- { "help", no_argument, 0, 0 },
- { "tag", no_argument, 0, 0 },
- { NULL, 0, NULL, 0 }
- };
-
- c = getopt_long( argc, argv, "a:l:", long_options, &option_index );
- if( c == -1 ) break;
- switch( c )
+ ARGBEGIN {
+ case 'a':
+ algo = EARGF(usage());
+ if( 0 == strcmp( algo, "blake2b" ) )
{
- case 'a':
- if( 0 == strcmp( optarg, "blake2b" ) )
- {
- blake2_stream = blake2b_stream;
- maxbytes = BLAKE2B_OUTBYTES;
- algorithm = "BLAKE2b";
- }
- else if ( 0 == strcmp( optarg, "blake2s" ) )
- {
- blake2_stream = blake2s_stream;
- maxbytes = BLAKE2S_OUTBYTES;
- algorithm = "BLAKE2s";
- }
- else if ( 0 == strcmp( optarg, "blake2bp" ) )
- {
- blake2_stream = blake2bp_stream;
- maxbytes = BLAKE2B_OUTBYTES;
- algorithm = "BLAKE2bp";
- }
- else if ( 0 == strcmp( optarg, "blake2sp" ) )
- {
- blake2_stream = blake2sp_stream;
- maxbytes = BLAKE2S_OUTBYTES;
- algorithm = "BLAKE2sp";
- }
- else
- {
- printf( "Invalid function name: `%s'\n", optarg );
- usage( argv, 111 );
- }
-
- break;
-
- case 'l':
- outbits = strtoul(optarg, &end, 10);
- if( !end || *end != '\0' || outbits % 8 != 0)
- {
- printf( "Invalid length argument: `%s'\n", optarg);
- usage( argv, 111 );
- }
- outbytes = outbits / 8;
- break;
-
- case 0:
- if( 0 == strcmp( "help", long_options[option_index].name ) )
- usage( argv, 0 );
- else if( 0 == strcmp( "tag", long_options[option_index].name ) )
- bsdstyle = true;
- break;
-
- case '?':
- usage( argv, 1 );
- break;
+ blake2_stream = blake2b_stream;
+ maxbytes = BLAKE2B_OUTBYTES;
+ algorithm = "BLAKE2b";
}
- }
+ else if ( 0 == strcmp( algo, "blake2s" ) )
+ {
+ blake2_stream = blake2s_stream;
+ maxbytes = BLAKE2S_OUTBYTES;
+ algorithm = "BLAKE2s";
+ }
+ else if ( 0 == strcmp( algo, "blake2bp" ) )
+ {
+ blake2_stream = blake2bp_stream;
+ maxbytes = BLAKE2B_OUTBYTES;
+ algorithm = "BLAKE2bp";
+ }
+ else if ( 0 == strcmp( algo, "blake2sp" ) )
+ {
+ blake2_stream = blake2sp_stream;
+ maxbytes = BLAKE2S_OUTBYTES;
+ algorithm = "BLAKE2sp";
+ }
+ else
+ {
+ fprint( 2, "Invalid function name: `%s'\n", algo );
+ usage();
+ }
+ break;
+ case 'l':
+ outbits = strtoul(EARGF(usage()), nil, 0);
+ outbytes = outbits/8;
+ break;
+ default:
+ usage();
+ } ARGEND
if(outbytes > maxbytes)
{
- printf( "Invalid length argument: %lu\n", outbytes * 8 );
- printf( "Maximum digest length for %s is %lu\n", algorithm, maxbytes * 8 );
- usage( argv, 111 );
+ print( "Invalid length argument: %lud\n", outbytes * 8 );
+ print( "Maximum digest length for %s is %lud\n", algorithm, maxbytes * 8 );
+ usage();
}
else if( outbytes == 0 )
outbytes = maxbytes;
- if( optind == argc )
- argv[argc++] = (char *) "-";
-
- for( i = optind; i < argc; ++i )
+ if(argc == 0)
{
- FILE *f = NULL;
- if( argv[i][0] == '-' && argv[i][1] == '\0' )
- f = stdin;
- else
- f = fopen( argv[i], "rb" );
-
- if( !f )
+ hash("<stdin>", 0, blake2_stream, outbytes);
+ }
+ else for(i = 0; i < argc; i++)
+ {
+ int fd = open(argv[i], OREAD);
+ if(fd < 0)
{
- fprintf( stderr, "Could not open `%s': %s\n", argv[i], strerror( errno ) );
+ fprint(2, "could not open '%s': %r\n", argv[i]);
continue;
}
-
- if( blake2_stream( f, hash, outbytes ) < 0 )
- {
- fprintf( stderr, "Failed to hash `%s'\n", argv[i] );
- }
- else
- {
- size_t j;
- if( bsdstyle )
- {
- if( outbytes < maxbytes )
- printf( "%s-%lu (%s) = ", algorithm, outbytes * 8, argv[i] );
- else
- printf( "%s (%s) = ", algorithm, argv[i] );
- }
-
- for( j = 0; j < outbytes; ++j )
- printf( "%02x", hash[j] );
-
- if( bsdstyle )
- printf( "\n" );
- else
- printf( " %s\n", argv[i] );
- }
-
- if( f != stdin ) fclose( f );
+ hash(argv[i], fd, blake2_stream, outbytes);
+ close(fd);
}
- return 0;
+ exits(nil);
}
--- /dev/null
+++ b/b2sum/mkfile
@@ -1,0 +1,17 @@
+</$objtype/mkfile
+
+BIN=$home/bin/$objtype
+TARG=b2sum
+OFILES=\
+ b2sum.$O\
+ blake2b-ref.$O\
+ blake2s-ref.$O\
+ blake2bp-ref.$O\
+ blake2sp-ref.$O\
+
+CFLAGS=$CFLAGS -p -D__plan9__ -I../ref
+
+</sys/src/cmd/mkone
+
+blake2%.$O: ../ref/blake2%.c
+ $CC $CFLAGS -o $target $prereq
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,12 @@
+b2sum:VQ:
+ cd b2sum && mk $MKFLAGS
+
+install:VQ:
+ cd b2sum && mk $MKFLAGS install
+
+test:VQ:
+ cd ref && mk run
+
+clean:VQ:
+ @{ cd b2sum && mk $MKFLAGS clean }
+ @{ cd ref && mk $MKFLAGS clean }
--- a/ref/blake2bp-ref.c
+++ b/ref/blake2bp-ref.c
@@ -13,10 +13,8 @@
https://blake2.net.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
+#include <u.h>
+#include <libc.h>
#if defined(_OPENMP)
#include <omp.h>
@@ -41,11 +39,11 @@
return err;
}
-static int blake2bp_init_leaf( blake2b_state *S, size_t outlen, size_t keylen, uint64_t offset )
+static int blake2bp_init_leaf( blake2b_state *S, int outlen, int keylen, u64int offset )
{
blake2b_param P[1];
- P->digest_length = (uint8_t)outlen;
- P->key_length = (uint8_t)keylen;
+ P->digest_length = (u8int)outlen;
+ P->key_length = (u8int)keylen;
P->fanout = PARALLELISM_DEGREE;
P->depth = 2;
store32( &P->leaf_length, 0 );
@@ -59,11 +57,11 @@
return blake2bp_init_leaf_param( S, P );
}
-static int blake2bp_init_root( blake2b_state *S, size_t outlen, size_t keylen )
+static int blake2bp_init_root( blake2b_state *S, int outlen, int keylen )
{
blake2b_param P[1];
- P->digest_length = (uint8_t)outlen;
- P->key_length = (uint8_t)keylen;
+ P->digest_length = (u8int)outlen;
+ P->key_length = (u8int)keylen;
P->fanout = PARALLELISM_DEGREE;
P->depth = 2;
store32( &P->leaf_length, 0 );
@@ -78,9 +76,9 @@
}
-int blake2bp_init( blake2bp_state *S, size_t outlen )
+int blake2bp_init( blake2bp_state *S, int outlen )
{
- size_t i;
+ int i;
if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
@@ -99,9 +97,9 @@
return 0;
}
-int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen )
+int blake2bp_init_key( blake2bp_state *S, int outlen, const void *key, int keylen )
{
- size_t i;
+ int i;
if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
@@ -120,7 +118,7 @@
S->R->last_node = 1;
S->S[PARALLELISM_DEGREE - 1]->last_node = 1;
{
- uint8_t block[BLAKE2B_BLOCKBYTES];
+ u8int block[BLAKE2B_BLOCKBYTES];
memset( block, 0, BLAKE2B_BLOCKBYTES );
memcpy( block, key, keylen );
@@ -133,12 +131,12 @@
}
-int blake2bp_update( blake2bp_state *S, const void *pin, size_t inlen )
+int blake2bp_update( blake2bp_state *S, const void *pin, int inlen )
{
const unsigned char * in = (const unsigned char *)pin;
- size_t left = S->buflen;
- size_t fill = sizeof( S->buf ) - left;
- size_t i;
+ int left = S->buflen;
+ int fill = sizeof( S->buf ) - left;
+ int i;
if( left && inlen >= fill )
{
@@ -160,9 +158,9 @@
#endif
{
#if defined(_OPENMP)
- size_t i = omp_get_thread_num();
+ int i = omp_get_thread_num();
#endif
- size_t inlen__ = inlen;
+ int inlen__ = inlen;
const unsigned char *in__ = ( const unsigned char * )in;
in__ += i * BLAKE2B_BLOCKBYTES;
@@ -184,12 +182,12 @@
return 0;
}
-int blake2bp_final( blake2bp_state *S, void *out, size_t outlen )
+int blake2bp_final( blake2bp_state *S, void *out, int outlen )
{
- uint8_t hash[PARALLELISM_DEGREE][BLAKE2B_OUTBYTES];
- size_t i;
+ u8int hash[PARALLELISM_DEGREE][BLAKE2B_OUTBYTES];
+ int i;
- if(out == NULL || outlen < S->outlen) {
+ if(out == nil || outlen < S->outlen) {
return -1;
}
@@ -197,7 +195,7 @@
{
if( S->buflen > i * BLAKE2B_BLOCKBYTES )
{
- size_t left = S->buflen - i * BLAKE2B_BLOCKBYTES;
+ int left = S->buflen - i * BLAKE2B_BLOCKBYTES;
if( left > BLAKE2B_BLOCKBYTES ) left = BLAKE2B_BLOCKBYTES;
@@ -213,19 +211,19 @@
return blake2b_final( S->R, out, S->outlen );
}
-int blake2bp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen )
+int blake2bp( void *out, int outlen, const void *in, int inlen, const void *key, int keylen )
{
- uint8_t hash[PARALLELISM_DEGREE][BLAKE2B_OUTBYTES];
+ u8int hash[PARALLELISM_DEGREE][BLAKE2B_OUTBYTES];
blake2b_state S[PARALLELISM_DEGREE][1];
blake2b_state FS[1];
- size_t i;
+ int i;
/* Verify parameters */
- if ( NULL == in && inlen > 0 ) return -1;
+ if ( nil == in && inlen > 0 ) return -1;
- if ( NULL == out ) return -1;
+ if ( nil == out ) return -1;
- if( NULL == key && keylen > 0 ) return -1;
+ if( nil == key && keylen > 0 ) return -1;
if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
@@ -238,7 +236,7 @@
if( keylen > 0 )
{
- uint8_t block[BLAKE2B_BLOCKBYTES];
+ u8int block[BLAKE2B_BLOCKBYTES];
memset( block, 0, BLAKE2B_BLOCKBYTES );
memcpy( block, key, keylen );
@@ -256,9 +254,9 @@
#endif
{
#if defined(_OPENMP)
- size_t i = omp_get_thread_num();
+ int i = omp_get_thread_num();
#endif
- size_t inlen__ = inlen;
+ int inlen__ = inlen;
const unsigned char *in__ = ( const unsigned char * )in;
in__ += i * BLAKE2B_BLOCKBYTES;
@@ -271,8 +269,8 @@
if( inlen__ > i * BLAKE2B_BLOCKBYTES )
{
- const size_t left = inlen__ - i * BLAKE2B_BLOCKBYTES;
- const size_t len = left <= BLAKE2B_BLOCKBYTES ? left : BLAKE2B_BLOCKBYTES;
+ const int left = inlen__ - i * BLAKE2B_BLOCKBYTES;
+ const int len = left <= BLAKE2B_BLOCKBYTES ? left : BLAKE2B_BLOCKBYTES;
blake2b_update( S[i], in__, len );
}
@@ -291,24 +289,23 @@
}
#if defined(BLAKE2BP_SELFTEST)
-#include <string.h>
#include "blake2-kat.h"
int main( void )
{
- uint8_t key[BLAKE2B_KEYBYTES];
- uint8_t buf[BLAKE2_KAT_LENGTH];
- size_t i, step;
+ u8int key[BLAKE2B_KEYBYTES];
+ u8int buf[BLAKE2_KAT_LENGTH];
+ int i, step;
for( i = 0; i < BLAKE2B_KEYBYTES; ++i )
- key[i] = ( uint8_t )i;
+ key[i] = ( u8int )i;
for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
- buf[i] = ( uint8_t )i;
+ buf[i] = ( u8int )i;
/* Test simple API */
for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
{
- uint8_t hash[BLAKE2B_OUTBYTES];
+ u8int hash[BLAKE2B_OUTBYTES];
blake2bp( hash, BLAKE2B_OUTBYTES, buf, i, key, BLAKE2B_KEYBYTES );
if( 0 != memcmp( hash, blake2bp_keyed_kat[i], BLAKE2B_OUTBYTES ) )
@@ -320,10 +317,10 @@
/* Test streaming API */
for(step = 1; step < BLAKE2B_BLOCKBYTES; ++step) {
for (i = 0; i < BLAKE2_KAT_LENGTH; ++i) {
- uint8_t hash[BLAKE2B_OUTBYTES];
+ u8int hash[BLAKE2B_OUTBYTES];
blake2bp_state S;
- uint8_t * p = buf;
- size_t mlen = i;
+ u8int * p = buf;
+ int mlen = i;
int err = 0;
if( (err = blake2bp_init_key(&S, BLAKE2B_OUTBYTES, key, BLAKE2B_KEYBYTES)) < 0 ) {
@@ -350,10 +347,9 @@
}
}
- puts( "ok" );
- return 0;
+ print( "ok\n" );
+ exits(nil);
fail:
- puts("error");
- return -1;
+ sysfatal("error");
}
#endif
--- a/ref/blake2sp-ref.c
+++ b/ref/blake2sp-ref.c
@@ -13,9 +13,8 @@
https://blake2.net.
*/
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
+#include <u.h>
+#include <libc.h>
#if defined(_OPENMP)
#include <omp.h>
@@ -40,11 +39,11 @@
return err;
}
-static int blake2sp_init_leaf( blake2s_state *S, size_t outlen, size_t keylen, uint64_t offset )
+static int blake2sp_init_leaf( blake2s_state *S, int outlen, int keylen, u64int offset )
{
blake2s_param P[1];
- P->digest_length = (uint8_t)outlen;
- P->key_length = (uint8_t)keylen;
+ P->digest_length = (u8int)outlen;
+ P->key_length = (u8int)keylen;
P->fanout = PARALLELISM_DEGREE;
P->depth = 2;
store32( &P->leaf_length, 0 );
@@ -57,11 +56,11 @@
return blake2sp_init_leaf_param( S, P );
}
-static int blake2sp_init_root( blake2s_state *S, size_t outlen, size_t keylen )
+static int blake2sp_init_root( blake2s_state *S, int outlen, int keylen )
{
blake2s_param P[1];
- P->digest_length = (uint8_t)outlen;
- P->key_length = (uint8_t)keylen;
+ P->digest_length = (u8int)outlen;
+ P->key_length = (u8int)keylen;
P->fanout = PARALLELISM_DEGREE;
P->depth = 2;
store32( &P->leaf_length, 0 );
@@ -75,9 +74,9 @@
}
-int blake2sp_init( blake2sp_state *S, size_t outlen )
+int blake2sp_init( blake2sp_state *S, int outlen )
{
- size_t i;
+ int i;
if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
@@ -96,9 +95,9 @@
return 0;
}
-int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen )
+int blake2sp_init_key( blake2sp_state *S, int outlen, const void *key, int keylen )
{
- size_t i;
+ int i;
if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
@@ -117,7 +116,7 @@
S->R->last_node = 1;
S->S[PARALLELISM_DEGREE - 1]->last_node = 1;
{
- uint8_t block[BLAKE2S_BLOCKBYTES];
+ u8int block[BLAKE2S_BLOCKBYTES];
memset( block, 0, BLAKE2S_BLOCKBYTES );
memcpy( block, key, keylen );
@@ -130,12 +129,12 @@
}
-int blake2sp_update( blake2sp_state *S, const void *pin, size_t inlen )
+int blake2sp_update( blake2sp_state *S, const void *pin, int inlen )
{
const unsigned char * in = (const unsigned char *)pin;
- size_t left = S->buflen;
- size_t fill = sizeof( S->buf ) - left;
- size_t i;
+ int left = S->buflen;
+ int fill = sizeof( S->buf ) - left;
+ int i;
if( left && inlen >= fill )
{
@@ -156,9 +155,9 @@
#endif
{
#if defined(_OPENMP)
- size_t i = omp_get_thread_num();
+ int i = omp_get_thread_num();
#endif
- size_t inlen__ = inlen;
+ int inlen__ = inlen;
const unsigned char *in__ = ( const unsigned char * )in;
in__ += i * BLAKE2S_BLOCKBYTES;
@@ -181,12 +180,12 @@
}
-int blake2sp_final( blake2sp_state *S, void *out, size_t outlen )
+int blake2sp_final( blake2sp_state *S, void *out, int outlen )
{
- uint8_t hash[PARALLELISM_DEGREE][BLAKE2S_OUTBYTES];
- size_t i;
+ u8int hash[PARALLELISM_DEGREE][BLAKE2S_OUTBYTES];
+ int i;
- if(out == NULL || outlen < S->outlen) {
+ if(out == nil || outlen < S->outlen) {
return -1;
}
@@ -194,7 +193,7 @@
{
if( S->buflen > i * BLAKE2S_BLOCKBYTES )
{
- size_t left = S->buflen - i * BLAKE2S_BLOCKBYTES;
+ int left = S->buflen - i * BLAKE2S_BLOCKBYTES;
if( left > BLAKE2S_BLOCKBYTES ) left = BLAKE2S_BLOCKBYTES;
@@ -211,19 +210,19 @@
}
-int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen )
+int blake2sp( void *out, int outlen, const void *in, int inlen, const void *key, int keylen )
{
- uint8_t hash[PARALLELISM_DEGREE][BLAKE2S_OUTBYTES];
+ u8int hash[PARALLELISM_DEGREE][BLAKE2S_OUTBYTES];
blake2s_state S[PARALLELISM_DEGREE][1];
blake2s_state FS[1];
- size_t i;
+ int i;
/* Verify parameters */
- if ( NULL == in && inlen > 0 ) return -1;
+ if ( nil == in && inlen > 0 ) return -1;
- if ( NULL == out ) return -1;
+ if ( nil == out ) return -1;
- if ( NULL == key && keylen > 0) return -1;
+ if ( nil == key && keylen > 0) return -1;
if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
@@ -236,7 +235,7 @@
if( keylen > 0 )
{
- uint8_t block[BLAKE2S_BLOCKBYTES];
+ u8int block[BLAKE2S_BLOCKBYTES];
memset( block, 0, BLAKE2S_BLOCKBYTES );
memcpy( block, key, keylen );
@@ -254,9 +253,9 @@
#endif
{
#if defined(_OPENMP)
- size_t i = omp_get_thread_num();
+ int i = omp_get_thread_num();
#endif
- size_t inlen__ = inlen;
+ int inlen__ = inlen;
const unsigned char *in__ = ( const unsigned char * )in;
in__ += i * BLAKE2S_BLOCKBYTES;
@@ -269,8 +268,8 @@
if( inlen__ > i * BLAKE2S_BLOCKBYTES )
{
- const size_t left = inlen__ - i * BLAKE2S_BLOCKBYTES;
- const size_t len = left <= BLAKE2S_BLOCKBYTES ? left : BLAKE2S_BLOCKBYTES;
+ const int left = inlen__ - i * BLAKE2S_BLOCKBYTES;
+ const int len = left <= BLAKE2S_BLOCKBYTES ? left : BLAKE2S_BLOCKBYTES;
blake2s_update( S[i], in__, len );
}
@@ -291,24 +290,23 @@
#if defined(BLAKE2SP_SELFTEST)
-#include <string.h>
#include "blake2-kat.h"
int main( void )
{
- uint8_t key[BLAKE2S_KEYBYTES];
- uint8_t buf[BLAKE2_KAT_LENGTH];
- size_t i, step;
+ u8int key[BLAKE2S_KEYBYTES];
+ u8int buf[BLAKE2_KAT_LENGTH];
+ int i, step;
for( i = 0; i < BLAKE2S_KEYBYTES; ++i )
- key[i] = ( uint8_t )i;
+ key[i] = ( u8int )i;
for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
- buf[i] = ( uint8_t )i;
+ buf[i] = ( u8int )i;
/* Test simple API */
for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
{
- uint8_t hash[BLAKE2S_OUTBYTES];
+ u8int hash[BLAKE2S_OUTBYTES];
blake2sp( hash, BLAKE2S_OUTBYTES, buf, i, key, BLAKE2S_KEYBYTES );
if( 0 != memcmp( hash, blake2sp_keyed_kat[i], BLAKE2S_OUTBYTES ) )
@@ -320,10 +318,10 @@
/* Test streaming API */
for(step = 1; step < BLAKE2S_BLOCKBYTES; ++step) {
for (i = 0; i < BLAKE2_KAT_LENGTH; ++i) {
- uint8_t hash[BLAKE2S_OUTBYTES];
+ u8int hash[BLAKE2S_OUTBYTES];
blake2sp_state S;
- uint8_t * p = buf;
- size_t mlen = i;
+ u8int * p = buf;
+ int mlen = i;
int err = 0;
if( (err = blake2sp_init_key(&S, BLAKE2S_OUTBYTES, key, BLAKE2S_KEYBYTES)) < 0 ) {
@@ -350,10 +348,9 @@
}
}
- puts( "ok" );
- return 0;
+ print( "ok\n" );
+ exits(nil);
fail:
- puts("error");
- return -1;
+ sysfatal("error");
}
#endif
--- a/ref/mkfile
+++ b/ref/mkfile
@@ -2,17 +2,39 @@
TARG=\
blake2s-ref\
blake2b-ref\
+ blake2sp-ref\
+ blake2bp-ref\
CFLAGS=$CFLAGS -p -D__plan9__ -I../testvectors/
</sys/src/cmd/mkmany
-blake2s-ref.$O: blake2s-ref.c
- $CC $CFLAGS -D BLAKE2S_SELFTEST $prereq
+blake2s-ref-main.$O: blake2s-ref.c
+ $CC $CFLAGS -D BLAKE2S_SELFTEST -o $target $prereq
-blake2b-ref.$O: blake2b-ref.c
- $CC $CFLAGS -D BLAKE2B_SELFTEST $prereq
+blake2b-ref-main.$O: blake2b-ref.c
+ $CC $CFLAGS -D BLAKE2B_SELFTEST -o $target $prereq
+blake2sp-ref-main.$O: blake2sp-ref.c
+ $CC $CFLAGS -D BLAKE2SP_SELFTEST -o $target $prereq
+
+blake2bp-ref-main.$O: blake2bp-ref.c
+ $CC $CFLAGS -D BLAKE2BP_SELFTEST -o $target $prereq
+
+$O.blake2s-ref: blake2s-ref-main.$O
+ $LD $LDFLAGS -o $target $prereq
+
+$O.blake2b-ref: blake2b-ref-main.$O
+ $LD $LDFLAGS -o $target $prereq
+
+$O.blake2sp-ref: blake2sp-ref-main.$O blake2s-ref.$O
+ $LD $LDFLAGS -o $target $prereq
+
+$O.blake2bp-ref: blake2bp-ref-main.$O blake2b-ref.$O
+ $LD $LDFLAGS -o $target $prereq
+
run:V: $PROGS
- $O.blake2s-ref
- $O.blake2b-ref
+ ./$O.blake2s-ref
+ ./$O.blake2b-ref
+ ./$O.blake2sp-ref
+ ./$O.blake2bp-ref
--
⑨