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 );
}
Exemple #2
0
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;
}
Exemple #3
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());
	}
Exemple #4
0
int blake2s_low_init_key(blake2s_state *context, const unsigned char *key, size_t key_len) {
	return blake2s_init_key(context, BLAKE2S_OUTBYTES, key, key_len);
}