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()); }
int main( void ) { uint8_t key[BLAKE2S_KEYBYTES]; uint8_t buf[BLAKE2_KAT_LENGTH]; size_t i, step; for( i = 0; i < BLAKE2S_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[BLAKE2S_OUTBYTES]; blake2sp( hash, BLAKE2S_OUTBYTES, buf, i, key, BLAKE2S_KEYBYTES ); if( 0 != memcmp( hash, blake2sp_keyed_kat[i], BLAKE2S_OUTBYTES ) ) { goto fail; } } /* Test streaming API */ for(step = 1; step < BLAKE2S_BLOCKBYTES; ++step) { for (i = 0; i < BLAKE2_KAT_LENGTH; ++i) { uint8_t hash[BLAKE2S_OUTBYTES]; blake2sp_state S; uint8_t * p = buf; size_t mlen = i; int err = 0; if( (err = blake2sp_init_key(&S, BLAKE2S_OUTBYTES, key, BLAKE2S_KEYBYTES)) < 0 ) { goto fail; } while (mlen >= step) { if ( (err = blake2sp_update(&S, p, step)) < 0 ) { goto fail; } mlen -= step; p += step; } if ( (err = blake2sp_update(&S, p, mlen)) < 0) { goto fail; } if ( (err = blake2sp_final(&S, hash, BLAKE2S_OUTBYTES)) < 0) { goto fail; } if (0 != memcmp(hash, blake2sp_keyed_kat[i], BLAKE2S_OUTBYTES)) { goto fail; } } } puts( "ok" ); return 0; fail: puts("error"); return -1; }