Exemplo n.º 1
0
int main( int argc, char **argv )
{
  uint8_t key[BLAKE2B_KEYBYTES];
  uint8_t buf[KAT_LENGTH];

  for( size_t i = 0; i < BLAKE2B_KEYBYTES; ++i )
    key[i] = ( uint8_t )i;

  for( size_t i = 0; i < KAT_LENGTH; ++i )
    buf[i] = ( uint8_t )i;

  for( size_t i = 0; i < KAT_LENGTH; ++i )
  {
    uint8_t hash[BLAKE2B_OUTBYTES];
    //blake2bp( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES );
    blake2bp_state S[1];
    blake2bp_init_key( S, BLAKE2B_OUTBYTES, key, BLAKE2B_KEYBYTES );
    blake2bp_update( S, buf, i );
    blake2bp_final( S, hash, BLAKE2B_OUTBYTES );

    if( 0 != memcmp( hash, blake2bp_keyed_kat[i], BLAKE2B_OUTBYTES ) )
    {
      puts( "error" );
      return -1;
    }
  }

  puts( "ok" );
  return 0;
}
Exemplo n.º 2
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());
	}