Ejemplo n.º 1
0
int pass2hash160(unsigned char *pass, size_t pass_sz) {
  /* only initialize stuff once */
  static int bwc_is_init = 0;
  if (!bwc_is_init) {
    /* initialize buffers */
    mem = malloc(4096);

    /* initialize hashs */
    sha256_ctx    = malloc(sizeof(*sha256_ctx));
    ripemd160_ctx = malloc(sizeof(*ripemd160_ctx));

    /* set the flag */
    bwc_is_init = 1;
  }

  unsigned char *pub_chr = mem;
  int pub_chr_sz;

  SHA256_Init(sha256_ctx);
  SHA256_Update(sha256_ctx, pass, pass_sz);
  SHA256_Final(hash256, sha256_ctx);

  secp256k1_ecdsa_pubkey_create(pub_chr, &pub_chr_sz, hash256, 0);

#if 0
  i = 0;
  for (i = 0; i < pub_chr_sz; i++) {
    printf("%02x", pub_chr[i]);
  }
  printf("\n");
#endif

  /* yo dawg, i heard you like hashes... */
  SHA256_Init(sha256_ctx);
  SHA256_Update(sha256_ctx, pub_chr, pub_chr_sz);
  SHA256_Final(hash256, sha256_ctx);

  /* ...so i put a hash in your hash */
  RIPEMD160_Init(ripemd160_ctx);
  RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH);
  RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx);
  memcpy(hash160_uncmp.uc, hash160_tmp.uc, 20);

  /* ugly key compression hack */
  pub_chr[0] = 0x02 | (pub_chr[64] & 0x01);

  /* yo dawg, i heard you like hashes... */
  SHA256_Init(sha256_ctx);
  SHA256_Update(sha256_ctx, pub_chr, 33);
  SHA256_Final(hash256, sha256_ctx);

  /* ...so i put a hash in your hash */
  RIPEMD160_Init(ripemd160_ctx);
  RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH);
  RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx);
  memcpy(hash160_compr.uc, hash160_tmp.uc, 20);

  return 0;
}
Ejemplo n.º 2
0
inline static int priv2hash160(unsigned char *priv) {
	/* only initialize stuff once */
	if (!brainflayer_is_init) {
		/* initialize buffers */
		mem = malloc(4096);

		/* initialize hashs */
		sha256_ctx = malloc(sizeof(*sha256_ctx));
		ripemd160_ctx = malloc(sizeof(*ripemd160_ctx));

		/* set the flag */
		brainflayer_is_init = 1;
	}

	unsigned char *pub_chr = mem;
	int pub_chr_sz;

	secp256k1_ecdsa_pubkey_create(pub_chr, &pub_chr_sz, priv, 0);

#if 0
	i = 0;
	for (i = 0; i < pub_chr_sz; i++) {
		printf("%02x", pub_chr[i]);
	}
	printf("\n");
#endif

	/* compute hash160 for uncompressed public key */
	/* sha256(pub) */
	SHA256_Init(sha256_ctx);
	SHA256_Update(sha256_ctx, pub_chr, pub_chr_sz);
	SHA256_Final(hash256, sha256_ctx);
	/* ripemd160(sha256(pub)) */
	RIPEMD160_Init(ripemd160_ctx);
	RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH);
	RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx);

	/* save result to global struct */
	memcpy(hash160_uncmp.uc, hash160_tmp.uc, 20);

	/* quick and dirty public key compression */
	pub_chr[0] = 0x02 | (pub_chr[64] & 0x01);

	/* compute hash160 for compressed public key */
	/* sha256(pub) */
	SHA256_Init(sha256_ctx);
	SHA256_Update(sha256_ctx, pub_chr, 33);
	SHA256_Final(hash256, sha256_ctx);
	/* ripemd160(sha256(pub)) */
	RIPEMD160_Init(ripemd160_ctx);
	RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH);
	RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx);

	/* save result to global struct */
	memcpy(hash160_compr.uc, hash160_tmp.uc, 20);

	return 0;
}
Ejemplo n.º 3
0
static int authcheck_ripemd160(aClient *cptr, anAuthStruct *as, char *para)
{
char buf[512];
int i, r;
char *saltstr, *hashstr;

	if (!para)
		return -1;
	r = parsepass(as->data, &saltstr, &hashstr);
	if (r)
	{
		/* New method with salt: b64(RIPEMD160(RIPEMD160(<pass>)+salt)) */
		char result1[MAXSALTLEN+20+1];
		char result2[20];
		char rsalt[MAXSALTLEN+1];
		int rsaltlen;
		RIPEMD160_CTX hash;
		
		/* First, decode the salt to something real... */
		rsaltlen = b64_decode(saltstr, rsalt, sizeof(rsalt));
		if (rsaltlen <= 0)
			return -1;

		/* Then hash the password (1st round)... */
		RIPEMD160_Init(&hash);
		RIPEMD160_Update(&hash, para, strlen(para));
		RIPEMD160_Final(result1, &hash);
		/* Add salt to result */
		memcpy(result1+20, rsalt, rsaltlen); /* b64_decode already made sure bounds are ok */

		/* Then hash it all together again (2nd round)... */
		RIPEMD160_Init(&hash);
		RIPEMD160_Update(&hash, result1, rsaltlen+20);
		RIPEMD160_Final(result2, &hash);
		/* Then base64 encode it all and we are done... */
		if ((i = b64_encode(result2, sizeof(result2), buf, sizeof(buf))))
		{
			if (!strcmp(buf, hashstr))
				return 2;
			else
				return -1;
		} else
			return -1;
	} else {
		/* OLD auth */
		if ((i = b64_encode(RIPEMD160(para, strlen(para), NULL), 20, buf, sizeof(buf))))
		{
			if (!strcmp(buf, as->data))
				return 2;
			else
				return -1;
		} else
			return -1;
	}
}
Ejemplo n.º 4
0
static char *mkpass_ripemd160(char *para)
{
static char buf[128];
char result1[20+REALSALTLEN];
char result2[20];
char saltstr[REALSALTLEN]; /* b64 encoded printable string*/
char saltraw[RAWSALTLEN];  /* raw binary */
char xresult[64];
RIPEMD160_CTX hash;
int i;

	if (!para) return NULL;

	/* generate a random salt... */
	for (i=0; i < RAWSALTLEN; i++)
		saltraw[i] = getrandom8();

	i = b64_encode(saltraw, RAWSALTLEN, saltstr, REALSALTLEN);
	if (!i) return NULL;

	/* b64(RIPEMD160(RIPEMD160(<pass>)+salt))
	 *         ^^^^^^^^^^^
	 *           step 1
	 *     ^^^^^^^^^^^^^^^^^^^^^
	 *     step 2
	 * ^^^^^^^^^^^^^^^^^^^^^^^^^^
	 * step 3
	 */

	/* STEP 1 */
	RIPEMD160_Init(&hash);
	RIPEMD160_Update(&hash, para, strlen(para));
	RIPEMD160_Final(result1, &hash);

	/* STEP 2 */
	/* add salt to result */
	memcpy(result1+20, saltraw, RAWSALTLEN);
	/* Then hash it all together */
	RIPEMD160_Init(&hash);
	RIPEMD160_Update(&hash, result1, RAWSALTLEN+20);
	RIPEMD160_Final(result2, &hash);

	/* STEP 3 */
	/* Then base64 encode it all together.. */
	i = b64_encode(result2, sizeof(result2), xresult, sizeof(xresult));
	if (!i) return NULL;

	/* Good.. now create the whole string:
	 * $<saltb64d>$<totalhashb64d>
	 */
	ircsprintf(buf, "$%s$%s", saltstr, xresult);
	return buf;
}
Ejemplo n.º 5
0
static int
__archive_libmd_ripemd160update(archive_rmd160_ctx *ctx, const void *indata,
    size_t insize)
{
  RIPEMD160_Update(ctx, indata, insize);
  return (ARCHIVE_OK);
}
Ejemplo n.º 6
0
void
openssl_ripemd160_hasher::operator()(void const* data,
    std::size_t size) noexcept
{
    auto const ctx = reinterpret_cast<
        RIPEMD160_CTX*>(ctx_);
    RIPEMD160_Update(ctx, data, size);
}
Ejemplo n.º 7
0
void rmd160(
          uint8_t *result,
    const uint8_t *data,
           size_t len
) {
    RIPEMD160_CTX ctx;
    RIPEMD160_Init(&ctx);
    RIPEMD160_Update(&ctx, data, len);
    RIPEMD160_Final(result, &ctx);
}
Ejemplo n.º 8
0
static void S2KItSaltedRIPEMD160Generator(char *password, unsigned char *key, int length)
{
	unsigned char keybuf[KEYBUFFER_LENGTH];
	RIPEMD160_CTX ctx;
	int i, j;
	int32_t tl;
	int32_t mul;
	int32_t bs;
	uint8_t *bptr;
	int32_t n;

	uint32_t numHashes = (length + RIPEMD160_DIGEST_LENGTH - 1) / RIPEMD160_DIGEST_LENGTH;
	memcpy(keybuf, cur_salt->salt, SALT_LENGTH);

	// TODO: This is not very efficient with multiple hashes
	for (i = 0; i < numHashes; i++) {
		RIPEMD160_Init(&ctx);
		for (j = 0; j < i; j++) {
			RIPEMD160_Update(&ctx, "\0", 1);
		}
		// Find multiplicator
		tl = strlen(password) + SALT_LENGTH;
		mul = 1;
		while (mul < tl && ((64 * mul) % tl)) {
			++mul;
		}
		// Try to feed the hash function with 64-byte blocks
		bs = mul * 64;
		bptr = keybuf + tl;
		n = bs / tl;
		memcpy(keybuf + SALT_LENGTH, password, strlen(password));
		while (n-- > 1) {
			memcpy(bptr, keybuf, tl);
			bptr += tl;
		}
		n = cur_salt->count / bs;
		while (n-- > 0) {
			RIPEMD160_Update(&ctx, keybuf, bs);
		}
		RIPEMD160_Update(&ctx, keybuf, cur_salt->count % bs);
		RIPEMD160_Final(key + (i * RIPEMD160_DIGEST_LENGTH), &ctx);
	}
}
Ejemplo n.º 9
0
int main(void)
{
    
    RIPEMD160_CTX   context;
    unsigned char   hash[RIPEMD160_DIGEST_LENGTH];
    BIO*            bio_out;

    char*       data_to_hash = "The worthwhile problems are the ones you can"
                               "really solve or help solve, the ones you can"
                               "really contribute something to. ... No "
                               "problem is too small or too trivial if we "
                               "can really do something about it."
                               "- Richard Feynman";

    int         length = (int)strlen(data_to_hash);
    int         i      = 0;


    RIPEMD160_Init(&context);

    while (i < length)
    {
        if ((length - i) < DATA_LENGTH)
            RIPEMD160_Update(&context, (void*)(data_to_hash + i), length - i);
        else
            RIPEMD160_Update(&context, (void*)(data_to_hash + i), DATA_LENGTH);

        i += DATA_LENGTH;
    }

    RIPEMD160_Final(hash, &context);

    bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
    
    for (i = 0; i < RIPEMD160_DIGEST_LENGTH; i++)
        BIO_printf(bio_out, "%02x", (unsigned char*)hash[i]);

    return 0;

}
Ejemplo n.º 10
0
unsigned char *RIPEMD160(const unsigned char *d, unsigned long n,
	     unsigned char *md)
	{
	RIPEMD160_CTX c;
	static unsigned char m[RIPEMD160_DIGEST_LENGTH];

	if (md == NULL) md=m;
	RIPEMD160_Init(&c);
	RIPEMD160_Update(&c,d,n);
	RIPEMD160_Final(md,&c);
	memset(&c,0,sizeof(c)); /* security consideration */
	return(md);
	}
Ejemplo n.º 11
0
unsigned char *RIPEMD160(const unsigned char *d, size_t n, unsigned char *md)
{
    RIPEMD160_CTX c;
    static unsigned char m[RIPEMD160_DIGEST_LENGTH];

    if (md == NULL)
        md = m;
    if (!RIPEMD160_Init(&c))
        return NULL;
    RIPEMD160_Update(&c, d, n);
    RIPEMD160_Final(md, &c);
    OPENSSL_cleanse(&c, sizeof(c)); /* security consideration */
    return (md);
}
ikptr
ikrt_openssl_ripemd160_update (ikptr s_ctx, ikptr s_input, ikptr s_input_len, ikpcb * pcb)
{
#ifdef HAVE_RIPEMD160_UPDATE
  RIPEMD160_CTX *	ctx	= IK_RIPEMD160_CTX(s_ctx);
  const void *	in	= IK_GENERALISED_C_STRING(s_input);
  size_t	in_len	= ik_generalised_c_buffer_len(s_input, s_input_len);
  int		rv;
  rv = RIPEMD160_Update(ctx, in, (unsigned long)in_len);
  return IK_BOOLEAN_FROM_INT(rv);
#else
  feature_failure(__func__);
#endif
}
Ejemplo n.º 13
0
		void genkey(const Memblock &string, uint8_t *key, uint32_t length) const
		{
			RIPEMD160_CTX ctx;
			uint32_t numHashes = (length + RIPEMD160_DIGEST_LENGTH - 1) / RIPEMD160_DIGEST_LENGTH;

			// TODO: This is not very efficient with multiple hashes
			for (uint32_t i = 0; i < numHashes; i++) {
				RIPEMD160_Init(&ctx);
				for (uint32_t j = 0; j < i; j++) {
					RIPEMD160_Update(&ctx, "\0", 1);
				}

				// Find multiplicator
				int32_t tl = string.length + 8;
				int32_t mul = 1;
				while (mul < tl && ((64 * mul) % tl)) {
					++mul;
				}

				// Try to feed the hash function with 64-byte blocks
				const int32_t bs = mul * 64;
				assert(bs <= KEYBUFFER_LENGTH);
				uint8_t *bptr = m_keybuf + tl;
				int32_t n = bs / tl;
				memcpy(m_keybuf + 8, string.data, string.length);
				while (n-- > 1) {
					memcpy(bptr, m_keybuf, tl);
					bptr += tl;
				}
				n = m_count / bs;
				while (n-- > 0) {
					RIPEMD160_Update(&ctx, m_keybuf, bs);
				}
				RIPEMD160_Update(&ctx, m_keybuf, m_count % bs);
				RIPEMD160_Final(key + (i * RIPEMD160_DIGEST_LENGTH), &ctx);
			}
		}
Ejemplo n.º 14
0
short_hash generate_ripemd_hash(const data_chunk& chunk)
{
    hash_digest sha_hash;
    SHA256_CTX sha_ctx;
    SHA256_Init(&sha_ctx);
    SHA256_Update(&sha_ctx, &chunk[0], chunk.size());
    SHA256_Final(sha_hash.data(), &sha_ctx);

    short_hash ripemd_hash;
    RIPEMD160_CTX ripemd_ctx;
    RIPEMD160_Init(&ripemd_ctx);
    RIPEMD160_Update(&ripemd_ctx, sha_hash.data(), SHA256_DIGEST_LENGTH);
    RIPEMD160_Final(ripemd_hash.data(), &ripemd_ctx);

    return ripemd_hash;
}
Ejemplo n.º 15
0
void do_fp(FILE *f)
	{
	RIPEMD160_CTX c;
	unsigned char md[RIPEMD160_DIGEST_LENGTH];
	int fd;
	int i;
	static unsigned char buf[BUFSIZE];

	fd=fileno(f);
	RIPEMD160_Init(&c);
	for (;;)
		{
		i=read(fd,buf,BUFSIZE);
		if (i <= 0) break;
		RIPEMD160_Update(&c,buf,(unsigned long)i);
		}
	RIPEMD160_Final(&(md[0]),&c);
	pt(md);
	}
Ejemplo n.º 16
0
ERL_NIF_TERM ucrypto_ripemd160_update_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    RIPEMD160_CTX *new_context;
    ErlNifBinary context_binary, data_binary;
    ERL_NIF_TERM result;

    if (! enif_inspect_binary(env, argv[0], &context_binary) || context_binary.size != sizeof(RIPEMD160_CTX))
        return enif_make_badarg(env);

    if (! enif_inspect_iolist_as_binary(env, argv[1], &data_binary))
        return enif_make_badarg(env);

    new_context = (RIPEMD160_CTX *)enif_make_new_binary(env, sizeof(RIPEMD160_CTX), &result);
    memcpy(new_context, context_binary.data, sizeof(RIPEMD160_CTX));

    RIPEMD160_Update(new_context, data_binary.data, data_binary.size);

    return result;
}
static void RIPEMD160_File(FILE *file, unsigned char **output, int *outlength)
{
	*output = new unsigned char[RIPEMD160_DIGEST_LENGTH];
	*outlength = RIPEMD160_DIGEST_LENGTH;

	RIPEMD160_CTX c;
	int i;
	unsigned char buf[RIPEMD160_FILE_BUFFER_SIZE];
	
	RIPEMD160_Init(&c);
	for (;;)
	{
		i = fread(buf,1,RIPEMD160_FILE_BUFFER_SIZE,file);
		if(i <= 0)
			break;
		RIPEMD160_Update(&c,buf,(unsigned long)i);
	}
	RIPEMD160_Final(*output, &c);
}
Ejemplo n.º 18
0
static void
digest_update(DIGEST_CTX *c, const unsigned char *data, size_t len)
{

	switch (digesttype) {
	case DIGEST_NONE:
		break;
	case DIGEST_MD5:
		MD5Update(&(c->MD5), data, len);
		break;
	case DIGEST_RIPEMD160:
		RIPEMD160_Update(&(c->RIPEMD160), data, len);
		break;
	case DIGEST_SHA1:
		SHA1_Update(&(c->SHA1), data, len);
		break;
	case DIGEST_SHA256:
		SHA256_Update(&(c->SHA256), data, len);
		break;
	case DIGEST_SHA512:
		SHA512_Update(&(c->SHA512), data, len);
		break;
	}
}
Ejemplo n.º 19
0
static int update(EVP_MD_CTX *ctx,const void *data,size_t count)
{
    return RIPEMD160_Update(ctx->md_data,data,count);
}
Ejemplo n.º 20
0
void CGUL::RIPEMD::UpdateData(const Byte* data, int len)
{
    RIPEMD160_Update(ctx, data, len);
}