int blake2s( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_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( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1; if( keylen > BLAKE2S_KEYBYTES ) return -1; if( keylen > 0 ) { if( blake2s_init_key( S, outlen, key, keylen ) < 0 ) return -1; } else { if( blake2s_init( S, outlen ) < 0 ) return -1; } if( blake2s_update( S, ( uint8_t * )in, inlen ) < 0) return -1; return blake2s_final( S, out, outlen ); }
void __init wg_noise_init(void) { struct blake2s_state blake; blake2s(handshake_init_chaining_key, handshake_name, NULL, NOISE_HASH_LEN, sizeof(handshake_name), 0); blake2s_init(&blake, NOISE_HASH_LEN); blake2s_update(&blake, handshake_init_chaining_key, NOISE_HASH_LEN); blake2s_update(&blake, identifier_name, sizeof(identifier_name)); blake2s_final(&blake, handshake_init_hash); }
inline void blake2s_hash(void *output, const void *input) { unsigned char hash[128] = { 0 }; blake2s_state blake2_ctx; blake2s_init(&blake2_ctx, BLAKE2S_OUTBYTES); blake2s_update(&blake2_ctx, input, 80); blake2s_final(&blake2_ctx, hash, BLAKE2S_OUTBYTES); memcpy(output, hash, 32); }
/* convenience function for all-in-one computation */ static int blake2s( void * out, HB_SIZE outlen, const void * key, HB_SIZE keylen, const void * in, HB_SIZE inlen ) { blake2s_ctx ctx; if( blake2s_init( &ctx, outlen, key, keylen ) ) return -1; blake2s_update( &ctx, in, inlen ); blake2s_final( &ctx, out ); return 0; }
int blake2s(void *out, size_t outlen, const void *key, size_t keylen, const void *in, size_t inlen) { blake2s_ctx ctx; if (blake2s_init(&ctx, outlen, key, keylen)) return -1; blake2s_update(&ctx, in, inlen); blake2s_final(&ctx, out); return 0; }
int main( int argc, char **argv ){ printf("PHI Version\n"); unsigned char hash[BLAKE2S_OUTBYTES] = {0}; int pf[4]; uint64_t fileLength[4]; const uint8_t * stream[4] = {NULL,NULL,NULL,NULL}; char * FileNames[4]; size_t fileNum=1; blake2s_state S[1]; if ( argc == 1 ) usage( argv ); // show usage upon no-argument while(1){ //prime streams for(size_t x=0;x<4;x++){ if(stream[x]==NULL && fileNum<argc){ FileNames[x] = argv[fileNum]; pf[x] = open( FileNames[x] , O_RDONLY); fileLength[x] = GetLength(FileNames[x]); stream [x] = mmap(NULL, fileLength[x], PROT_READ, MAP_PRIVATE , pf[x], 0); fileNum++; //fprintf(stderr,"ADDED %s,%u\n",FileNames[x],fileLength[x]); //Can Do Key Stuff Here blake2s_init( S, BLAKE2S_OUTBYTES, x); } } if(stream[0] == NULL && stream[1] == NULL && stream[2] == NULL && stream[3] == NULL) return 0; //fprintf(stderr,"STATUS: %u, %u, %u, %u\n",stream[3],stream[2],stream[1],stream[0]); //Run Hash int CHANNEL = blake2s_update( S, stream, fileLength); //fprintf(stderr,"Done CHAN: %u\n",CHANNEL); blake2s_final( S, hash, BLAKE2S_OUTBYTES, CHANNEL); //output hash then filename for( size_t j = 0; j < BLAKE2S_OUTBYTES; ++j ) printf( "%02x", hash[j] ); printf( " %s\n", FileNames[CHANNEL] ); munmap((void*)stream[CHANNEL], fileLength[CHANNEL]); close(pf[CHANNEL]); stream[CHANNEL]=NULL; fileLength[CHANNEL]=-1; } }
/* This will help compatibility with coreutils */ int blake2s_stream( FILE *stream, void *resstream ) { int ret = -1; size_t sum, n; blake2s_state S[1]; static const size_t buffer_length = 32768; uint8_t *buffer = ( uint8_t * )malloc( buffer_length ); if( !buffer ) return -1; blake2s_init( S, BLAKE2S_OUTBYTES ); while( 1 ) { sum = 0; while( 1 ) { n = fread( buffer + sum, 1, buffer_length - sum, stream ); sum += n; if( buffer_length == sum ) break; if( 0 == n ) { if( ferror( stream ) ) goto cleanup_buffer; goto final_process; } if( feof( stream ) ) goto final_process; } blake2s_update( S, buffer, buffer_length ); } final_process:; if( sum > 0 ) blake2s_update( S, buffer, sum ); blake2s_final( S, resstream, BLAKE2S_OUTBYTES ); ret = 0; cleanup_buffer: free( buffer ); return ret; }
void sccd_random_bytes(uint8_t *data, size_t length) { #if 0 int fd = open("/dev/urandom", O_RDONLY); ssize_t result = read(fd, data, length); if (result != length) { // error, unable to read /dev/random assert(0); } close(fd); #else while (length > 0) { size_t copy_n = length < 32 ? length : 32; memcpy(data, random_state, copy_n); data = data + copy_n; length = length - copy_n; blake2s_state S[1]; blake2s_init(S, 32); blake2s_update(S, random_state, sizeof(random_state)); blake2s_final(S, random_state, 32); } #endif }
int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ) { blake2s_state S[1]; /* Verify parameters */ if ( NULL == in ) return -1; if ( NULL == out ) return -1; if ( NULL == key ) keylen = 0; /* Fail here instead if keylen != 0 and key == NULL? */ if( keylen > 0 ) { if( blake2s_init_key( S, outlen, key, keylen ) < 0 ) return -1; } else { if( blake2s_init( S, outlen ) < 0 ) return -1; } blake2s_update( S, ( uint8_t * )in, inlen ); blake2s_final( S, out, outlen ); return 0; }
static NAN_METHOD(New) { if(!info.IsConstructCall()) { return Nan::ThrowError("Constructor must be called with new"); } Hash *obj = new Hash(); obj->Wrap(info.This()); if(info.Length() < 1 || !info[0]->IsString()) { return Nan::ThrowError(v8::Exception::TypeError(Nan::New<v8::String>("First argument must be a string with algorithm name").ToLocalChecked())); } std::string algo = std::string(*v8::String::Utf8Value(info[0]->ToString())); const char *key_data = nullptr; size_t key_length; if(algo != "bypass" && info.Length() >= 2) { if(!node::Buffer::HasInstance(info[1])) { return Nan::ThrowError(v8::Exception::TypeError(Nan::New<v8::String>("If key argument is given, it must be a Buffer").ToLocalChecked())); } key_data = node::Buffer::Data(info[1]); key_length = node::Buffer::Length(info[1]); } if(algo == "bypass") { // Initialize nothing - .copy() will set up all the state } else if(algo == "blake2b") { if(!key_data) { if(blake2b_init(reinterpret_cast<blake2b_state*>(&obj->state), BLAKE2B_OUTBYTES) != 0) { return Nan::ThrowError("blake2b_init failure"); } } else { if(key_length > BLAKE2B_KEYBYTES) { return Nan::ThrowError("Key must be 64 bytes or smaller"); } if(blake2b_init_key(reinterpret_cast<blake2b_state*>(&obj->state), BLAKE2B_OUTBYTES, key_data, key_length) != 0) { return Nan::ThrowError("blake2b_init_key failure"); } } obj->outbytes = 512 / 8; obj->any_blake2_update = BLAKE_FN_CAST(blake2b_update); obj->any_blake2_final = BLAKE_FN_CAST(blake2b_final); obj->initialized_ = true; } else if(algo == "blake2bp") { if(!key_data) { if(blake2bp_init(reinterpret_cast<blake2bp_state*>(&obj->state), BLAKE2B_OUTBYTES) != 0) { return Nan::ThrowError("blake2bp_init failure"); } } else { if(key_length > BLAKE2B_KEYBYTES) { return Nan::ThrowError("Key must be 64 bytes or smaller"); } if(blake2bp_init_key(reinterpret_cast<blake2bp_state*>(&obj->state), BLAKE2B_OUTBYTES, key_data, key_length) != 0) { return Nan::ThrowError("blake2bp_init_key failure"); } } obj->outbytes = 512 / 8; obj->any_blake2_update = BLAKE_FN_CAST(blake2bp_update); obj->any_blake2_final = BLAKE_FN_CAST(blake2bp_final); obj->initialized_ = true; } else if(algo == "blake2s") { if(!key_data) { if(blake2s_init(reinterpret_cast<blake2s_state*>(&obj->state), BLAKE2S_OUTBYTES) != 0) { return Nan::ThrowError("blake2bs_init failure"); } } else { if(key_length > BLAKE2S_KEYBYTES) { return Nan::ThrowError("Key must be 32 bytes or smaller"); } if(blake2s_init_key(reinterpret_cast<blake2s_state*>(&obj->state), BLAKE2S_OUTBYTES, key_data, key_length) != 0) { return Nan::ThrowError("blake2s_init_key failure"); } } obj->outbytes = 256 / 8; obj->any_blake2_update = BLAKE_FN_CAST(blake2s_update); obj->any_blake2_final = BLAKE_FN_CAST(blake2s_final); obj->initialized_ = true; } else if(algo == "blake2sp") { if(!key_data) { if(blake2sp_init(reinterpret_cast<blake2sp_state*>(&obj->state), BLAKE2S_OUTBYTES) != 0) { return Nan::ThrowError("blake2sp_init failure"); } } else { if(key_length > BLAKE2S_KEYBYTES) { return Nan::ThrowError("Key must be 32 bytes or smaller"); } if(blake2sp_init_key(reinterpret_cast<blake2sp_state*>(&obj->state), BLAKE2S_OUTBYTES, key_data, key_length) != 0) { return Nan::ThrowError("blake2sp_init_key failure"); } } obj->outbytes = 256 / 8; obj->any_blake2_update = BLAKE_FN_CAST(blake2sp_update); obj->any_blake2_final = BLAKE_FN_CAST(blake2sp_final); obj->initialized_ = true; } else { return Nan::ThrowError("Algorithm must be blake2b, blake2s, blake2bp, or blake2sp"); } info.GetReturnValue().Set(info.This()); }
/* Blake2s Low Level Interface */ int blake2s_low_init(blake2s_state *context) { blake2s_init(context, BLAKE2S_OUTBYTES); return 0; }
int blake2s_256_init(hash_state *md) { return blake2s_init(md, 32, NULL, 0); }
int blake2s_224_init(hash_state *md) { return blake2s_init(md, 28, NULL, 0); }
int blake2s_160_init(hash_state *md) { return blake2s_init(md, 20, NULL, 0); }
int blake2s_128_init(hash_state *md) { return blake2s_init(md, 16, NULL, 0); }
void sccd_random_init() { blake2s_state S[1]; blake2s_init(S, 32); blake2s_update(S, (const uint8_t*)random_iv, strlen(random_iv)); blake2s_final(S, random_state, 32); }