/*
- BLAKE2 reference source code package - reference C implementations
-
- Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
- terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
- your option. The terms of these licenses can be found at:
-
- - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
- - OpenSSL license : https://www.openssl.org/source/license.html
- - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
-
- More information about the BLAKE2 hash function can be found at
- https://blake2.net.
-*/
+ * public domain blake2 implementation adapted from the reference
+ * implementation by Samuel Neves
+ * More information about the BLAKE2 hash function can be found at
+ * https://blake2.net.
+ */
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "blake2.h"
-#include "blake2-impl.h"
+
+static uint64_t load64( const void *src )
+{
+#if defined(NATIVE_LITTLE_ENDIAN)
+ uint64_t w;
+ memcpy(&w, src, sizeof w);
+ return w;
+#else
+ const uint8_t *p = ( const uint8_t * )src;
+ return (( uint64_t )( p[0] ) << 0) |
+ (( uint64_t )( p[1] ) << 8) |
+ (( uint64_t )( p[2] ) << 16) |
+ (( uint64_t )( p[3] ) << 24) |
+ (( uint64_t )( p[4] ) << 32) |
+ (( uint64_t )( p[5] ) << 40) |
+ (( uint64_t )( p[6] ) << 48) |
+ (( uint64_t )( p[7] ) << 56) ;
+#endif
+}
+
+static void store32( void *dst, uint32_t w )
+{
+#if defined(NATIVE_LITTLE_ENDIAN)
+ memcpy(dst, &w, sizeof w);
+#else
+ uint8_t *p = ( uint8_t * )dst;
+ p[0] = (uint8_t)(w >> 0);
+ p[1] = (uint8_t)(w >> 8);
+ p[2] = (uint8_t)(w >> 16);
+ p[3] = (uint8_t)(w >> 24);
+#endif
+}
+
+static void store64( void *dst, uint64_t w )
+{
+#if defined(NATIVE_LITTLE_ENDIAN)
+ memcpy(dst, &w, sizeof w);
+#else
+ uint8_t *p = ( uint8_t * )dst;
+ p[0] = (uint8_t)(w >> 0);
+ p[1] = (uint8_t)(w >> 8);
+ p[2] = (uint8_t)(w >> 16);
+ p[3] = (uint8_t)(w >> 24);
+ p[4] = (uint8_t)(w >> 32);
+ p[5] = (uint8_t)(w >> 40);
+ p[6] = (uint8_t)(w >> 48);
+ p[7] = (uint8_t)(w >> 56);
+#endif
+}
+
+static uint64_t rotr64( const uint64_t w, const unsigned c )
+{
+ return ( w >> c ) | ( w << ( 64 - c ) );
+}
+
+/* prevents compiler optimizing out memset() */
+static void secure_zero_memory(void *v, size_t n)
+{
+ static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
+ memset_v(v, 0, n);
+}
static const uint64_t blake2b_IV[8] =
{
}
/* 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 )
int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ) {
return blake2b(out, outlen, in, inlen, key, keylen);
}
-
-#if defined(SUPERCOP)
-int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen )
-{
- return blake2b( out, BLAKE2B_OUTBYTES, in, inlen, NULL, 0 );
-}
-#endif
-
-#if defined(BLAKE2B_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;
-
- for( i = 0; i < BLAKE2B_KEYBYTES; ++i )
- key[i] = ( uint8_t )i;
-
- for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
- buf[i] = ( uint8_t )i;
-
- /* Test simple API */
- for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
- {
- uint8_t hash[BLAKE2B_OUTBYTES];
- blake2b( hash, BLAKE2B_OUTBYTES, buf, i, key, BLAKE2B_KEYBYTES );
-
- if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )
- {
- goto fail;
- }
- }
-
- /* Test streaming API */
- for(step = 1; step < BLAKE2B_BLOCKBYTES; ++step) {
- for (i = 0; i < BLAKE2_KAT_LENGTH; ++i) {
- uint8_t hash[BLAKE2B_OUTBYTES];
- blake2b_state S;
- uint8_t * p = buf;
- size_t mlen = i;
- int err = 0;
-
- if( (err = blake2b_init_key(&S, BLAKE2B_OUTBYTES, key, BLAKE2B_KEYBYTES)) < 0 ) {
- goto fail;
- }
-
- while (mlen >= step) {
- if ( (err = blake2b_update(&S, p, step)) < 0 ) {
- goto fail;
- }
- mlen -= step;
- p += step;
- }
- if ( (err = blake2b_update(&S, p, mlen)) < 0) {
- goto fail;
- }
- if ( (err = blake2b_final(&S, hash, BLAKE2B_OUTBYTES)) < 0) {
- goto fail;
- }
-
- if (0 != memcmp(hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES)) {
- goto fail;
- }
- }
- }
-
- puts( "ok" );
- return 0;
-fail:
- puts("error");
- return -1;
-}
-#endif