Exemple #1
0
/**
 * proto_kvlds_response_params(Q, ID, kmax, vmax):
 * Send a PARAMS response with ID ${ID} specifying that the maximum key
 * length is ${kmax} bytes and the maximum value length is ${vmax} bytes
 * to the write queue ${Q}.
 */
int
proto_kvlds_response_params(struct netbuf_write * Q, uint64_t ID,
    uint32_t kmax, uint32_t vmax)
{
	uint8_t * wbuf;

	/* Get a packet data buffer. */
	if ((wbuf = wire_writepacket_getbuf(Q, ID, 8)) == NULL)
		goto err0;

	/* Write the packet data. */
	be32enc(&wbuf[0], kmax);
	be32enc(&wbuf[4], vmax);

	/* Finish the packet. */
	if (wire_writepacket_done(Q, wbuf, 8))
		goto err0;

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}
/**
 * proto_dynamodb_kv_response_data(Q, ID, status, len, buf):
 * Send a response with ID ${ID} to the write queue ${Q} indicating that
 * the DynamoDB request completed successfully (${status} = 0) with the
 * provided data, failed (${status} = 1), or returned no data (${status} = 2).
 */
int
proto_dynamodb_kv_response_data(struct netbuf_write * Q, uint64_t ID,
    int status, uint32_t len, const uint8_t * buf)
{
	uint8_t * wbuf;
	size_t rlen;

	/* Compute the response length. */
	rlen = 4 + ((status == 0) ? len + 4 : 0);

	/* Get a packet data buffer. */
	if ((wbuf = wire_writepacket_getbuf(Q, ID, rlen)) == NULL)
		goto err0;

	/* Write the packet data. */
	be32enc(&wbuf[0], status);
	if (status == 0) {
		be32enc(&wbuf[4], len);
		memcpy(&wbuf[8], buf, len);
	}

	/* Finish the packet. */
	if (wire_writepacket_done(Q, wbuf, rlen))
		goto err0;

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}
Exemple #3
0
int scanhash_luffa(int thr_id, struct work *work,
	uint32_t max_nonce, uint64_t *hashes_done)
{
        uint32_t *pdata = work->data;
        uint32_t *ptarget = work->target;

	uint32_t _ALIGN(64) hash64[8];
	uint32_t _ALIGN(64) endiandata[20];

	const uint32_t Htarg = ptarget[7];
	const uint32_t first_nonce = pdata[19];

	uint32_t n = first_nonce;

        for (int i=0; i < 19; i++) 
                be32enc(&endiandata[i], pdata[i]);

	do {
		be32enc(&endiandata[19], n);
		luffahash(hash64, endiandata);
		if (hash64[7] < Htarg && fulltest(hash64, ptarget)) {
			*hashes_done = n - first_nonce + 1;
			pdata[19] = n;
			return true;
		}
		n++;

	} while (n < max_nonce && !work_restart[thr_id].restart);

	*hashes_done = n - first_nonce + 1;
	pdata[19] = n;

	return 0;
}
Exemple #4
0
void write_kdfp(uint8_t *addr, kdfp *kdfp) {
    uint64_t *N  = (uint64_t *) (addr + SALT_LEN);
    uint32_t *r  = (uint32_t *) (N + 1);
    uint32_t *p  = (uint32_t *) (r + 1);
    memcpy(addr, kdfp->salt, SALT_LEN);
    be64enc(N, kdfp->N);
    be32enc(r, kdfp->r);
    be32enc(p, kdfp->p);
}
Exemple #5
0
int scanhash_x12_4way( int thr_id, struct work *work, uint32_t max_nonce,
                       uint64_t *hashes_done )
{
     uint32_t hash[4*8] __attribute__ ((aligned (64)));
     uint32_t vdata[24*4] __attribute__ ((aligned (64)));
     uint32_t endiandata[20] __attribute__((aligned(64)));
     uint32_t *pdata = work->data;
     uint32_t *ptarget = work->target;
     uint32_t n = pdata[19];
     const uint32_t first_nonce = pdata[19];
     uint32_t *nonces = work->nonces;
     int num_found = 0;
     uint32_t *noncep = vdata + 73;   // 9*8 + 1
     const uint32_t Htarg = ptarget[7];
     uint64_t htmax[] = {          0,        0xF,       0xFF,
                               0xFFF,     0xFFFF, 0x10000000  };
     uint32_t masks[] = { 0xFFFFFFFF, 0xFFFFFFF0, 0xFFFFFF00,
                          0xFFFFF000, 0xFFFF0000,          0  };

     // big endian encode 0..18 uint32_t, 64 bits at a time
     swab32_array( endiandata, pdata, 20 );

     uint64_t *edata = (uint64_t*)endiandata;
     mm256_interleave_4x64( (uint64_t*)vdata, edata, edata, edata, edata, 640 );

     for ( int m=0; m < 6; m++ )
       if ( Htarg <= htmax[m] )
       {
         uint32_t mask = masks[m];
         do
         {
            be32enc( noncep,   n   );
            be32enc( noncep+2, n+1 );
            be32enc( noncep+4, n+2 );
            be32enc( noncep+6, n+3 );

            x12_4way_hash( hash, vdata );
            pdata[19] = n;

            for ( int i = 0; i < 4; i++ )
            if ( ( ( (hash+(i<<3))[7] & mask ) == 0 )
                 && fulltest( hash+(i<<3), ptarget ) )
            {
               pdata[19] = n+i;
               nonces[ num_found++ ] = n+i;
               work_set_target_ratio( work, hash+(i<<3) );
            }
            n += 4;
         } while ( ( num_found == 0 ) && ( n < max_nonce )
                   && !work_restart[thr_id].restart );
         break;
       }

     *hashes_done = n - first_nonce + 1;
     return num_found;
}
int
fifolog_write_record(struct fifolog_writer *f, uint32_t id, time_t now,
    const void *ptr, ssize_t len)
{
	const unsigned char *p;
	uint8_t buf[9];
	ssize_t bufl;

	fifolog_write_assert(f);
	assert(!(id & (FIFOLOG_TIMESTAMP|FIFOLOG_LENGTH)));
	assert(ptr != NULL);

	p = ptr;
	if (len == 0) {
		len = strlen(ptr);
		len++;
	} else {
		assert(len <= 255);
		id |= FIFOLOG_LENGTH;
	}
	assert (len > 0);

	/* Do a timestamp, if needed */
	if (now == 0)
		time(&now);

	if (now != f->last)
		id |= FIFOLOG_TIMESTAMP;

	/* Emit instance+flag */
	be32enc(buf, id);
	bufl = 4;

	if (id & FIFOLOG_TIMESTAMP) {
		be32enc(buf + bufl, (uint32_t)now);
		bufl += 4;
	}
	if (id & FIFOLOG_LENGTH)
		buf[bufl++] = (u_char)len;

	if (bufl + len + f->ibufptr > f->ibufsize)
		return (0);

	memcpy(f->ibuf + f->ibufptr, buf, bufl);
	f->ibufptr += bufl;
	memcpy(f->ibuf + f->ibufptr, p, len);
	f->ibufptr += len;
	f->cnt[FIFOLOG_PT_BYTES_PRE] += bufl + len;

	if (id & FIFOLOG_TIMESTAMP)
		f->last = now;
	return (1);
}
Exemple #7
0
int scanhash_blakecoin(int thr_id, struct work *work, uint32_t max_nonce,
                          uint64_t *hashes_done)
{
        uint32_t *pdata = work->data;
        uint32_t *ptarget = work->target;
	const uint32_t first_nonce = pdata[19];
	uint32_t HTarget = ptarget[7];

	uint32_t _ALIGN(32) hash64[8];
	uint32_t _ALIGN(32) endiandata[20];

	uint32_t n = first_nonce;

	ctx_midstate_done = false;

	if (opt_benchmark)
		HTarget = 0x7f;

	// we need big endian data...
	for (int kk=0; kk < 19; kk++) {
		be32enc(&endiandata[kk], ((uint32_t*)pdata)[kk]);
	};

#ifdef DEBUG_ALGO
	applog(LOG_DEBUG,"[%d] Target=%08x %08x", thr_id, ptarget[6], ptarget[7]);
#endif

	do {
		be32enc(&endiandata[19], n);
		blakecoinhash(hash64, endiandata);
#ifndef DEBUG_ALGO
		if (hash64[7] <= HTarget && fulltest(hash64, ptarget)) {
			*hashes_done = n - first_nonce + 1;
			return true;
		}
#else
		if (!(n % 0x1000) && !thr_id) printf(".");
		if (hash64[7] == 0) {
			printf("[%d]",thr_id);
			if (fulltest(hash64, ptarget)) {
				*hashes_done = n - first_nonce + 1;
				return true;
			}
		}
#endif
		n++; pdata[19] = n;

	} while (n < max_nonce && !work_restart[thr_id].restart);

	*hashes_done = n - first_nonce + 1;
	pdata[19] = n;
	return 0;
}
Exemple #8
0
static int
scryptenc_setup(uint8_t header[96], uint8_t dk[64],
    const uint8_t * passwd, size_t passwdlen,
    size_t maxmem, double maxmemfrac, double maxtime)
{
	uint8_t salt[32];
	uint8_t hbuf[32];
	int logN;
	uint64_t N;
	uint32_t r;
	uint32_t p;
	SHA256_CTX ctx;
	uint8_t * key_hmac = &dk[32];
	HMAC_SHA256_CTX hctx;
	int rc;

	/* Pick values for N, r, p. */
	if ((rc = pickparams(maxmem, maxmemfrac, maxtime,
	    &logN, &r, &p)) != 0)
		return (rc);
	N = (uint64_t)(1) << logN;

	/* Get some salt. */
	if (crypto_entropy_read(salt, 32))
		return (4);

	/* Generate the derived keys. */
	if (crypto_scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64))
		return (3);

	/* Construct the file header. */
	memcpy(header, "scrypt", 6);
	header[6] = 0;
	header[7] = logN;
	be32enc(&header[8], r);
	be32enc(&header[12], p);
	memcpy(&header[16], salt, 32);

	/* Add header checksum. */
	SHA256_Init(&ctx);
	SHA256_Update(&ctx, header, 48);
	SHA256_Final(hbuf, &ctx);
	memcpy(&header[48], hbuf, 16);

	/* Add header signature (used for verifying password). */
	HMAC_SHA256_Init(&hctx, key_hmac, 32);
	HMAC_SHA256_Update(&hctx, header, 64);
	HMAC_SHA256_Final(hbuf, &hctx);
	memcpy(&header[64], hbuf, 32);

	/* Success! */
	return (0);
}
Exemple #9
0
static int test_be32enc() {
  uint8_t buf[4];

  be32enc(buf, 0x01020304U);
  if(buf[0] != 1 || buf[1] != 2 || buf[2] != 3 || buf[3] != 4)
    return -1;

  be32enc(buf, 0xffefdfcfU);
  if(buf[0] != 0xff || buf[1] != 0xef || buf[2] != 0xdf || buf[3] != 0xcf)
    return -1;

  return 0;
}
Exemple #10
0
/**
 * proto_kvlds_response_range(Q, ID, nkeys, next, keys, values):
 * Send a RANGE response with ID ${ID}, next key ${next} and ${nkeys}
 * key-value pairs with the keys in ${keys} and values in ${values} to the
 * write queue ${Q}.
 */
int
proto_kvlds_response_range(struct netbuf_write * Q, uint64_t ID,
    size_t nkeys, const struct kvldskey * next,
    struct kvldskey ** keys, struct kvldskey ** values)
{
	uint8_t * wbuf;
	size_t len;
	size_t i;
	size_t bufpos;

	/* Sanity check: We can't return more than 2^32-1 keys. */
	assert(nkeys <= UINT32_MAX);

	/* Figure out how long the packet will be. */
	len = 8;
	len += kvldskey_serial_size(next);
	for (i = 0; i < nkeys; i++) {
		len += kvldskey_serial_size(keys[i]);
		len += kvldskey_serial_size(values[i]);
	}

	/* Get a packet data buffer. */
	if ((wbuf = wire_writepacket_getbuf(Q, ID, len)) == NULL)
		goto err0;

	/* Write the packet data. */
	be32enc(&wbuf[0], 0);
	be32enc(&wbuf[4], nkeys);
	bufpos = 8;
	kvldskey_serialize(next, &wbuf[bufpos]);
	bufpos += kvldskey_serial_size(next);
	for (i = 0; i < nkeys; i++) {
		kvldskey_serialize(keys[i], &wbuf[bufpos]);
		bufpos += kvldskey_serial_size(keys[i]);
		kvldskey_serialize(values[i], &wbuf[bufpos]);
		bufpos += kvldskey_serial_size(values[i]);
	}

	/* Finish the packet. */
	if (wire_writepacket_done(Q, wbuf, len))
		goto err0;

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}
/*
 * Encode the relevant fields into a sun disklabel, compute new checksum.
 */
void
sunlabel_enc(void *pp, struct sun_disklabel *sl)
{
	uint8_t *p;
	size_t i;
	u_int u;

	p = pp;
	for (i = 0; i < SL_TEXT_SIZEOF; i++)
		p[SL_TEXT + i] = sl->sl_text[i];
	be16enc(p + SL_RPM, sl->sl_rpm);
	be16enc(p + SL_PCYLINDERS, sl->sl_pcylinders);
	be16enc(p + SL_SPARESPERCYL, sl->sl_sparespercyl);
	be16enc(p + SL_INTERLEAVE, sl->sl_interleave);
	be16enc(p + SL_NCYLINDERS, sl->sl_ncylinders);
	be16enc(p + SL_ACYLINDERS, sl->sl_acylinders);
	be16enc(p + SL_NTRACKS, sl->sl_ntracks);
	be16enc(p + SL_NSECTORS, sl->sl_nsectors);
	for (i = 0; i < SUN_NPART; i++) {
		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_CYLOFFSET,
		    sl->sl_part[i].sdkp_cyloffset);
		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_NSECTORS,
		    sl->sl_part[i].sdkp_nsectors);
	}
	be16enc(p + SL_MAGIC, sl->sl_magic);
	if (sl->sl_vtoc_sane == SUN_VTOC_SANE
	    && sl->sl_vtoc_nparts == SUN_NPART) {
		/*
		 * Write SVR4-compatible VTOC elements.
		 */
		be32enc(p + SL_VTOC_VERS, sl->sl_vtoc_vers);
		be32enc(p + SL_VTOC_SANITY, SUN_VTOC_SANE);
		memcpy(p + SL_VTOC_VOLNAME, sl->sl_vtoc_volname,
		    SUN_VOLNAME_LEN);
		be16enc(p + SL_VTOC_NPART, SUN_NPART);
		for (i = 0; i < SUN_NPART; i++) {
			be16enc(p + SL_VTOC_MAP + (i * SVTOC_SIZEOF)
				+ SVTOC_TAG,
				sl->sl_vtoc_map[i].svtoc_tag);
			be16enc(p + SL_VTOC_MAP + (i * SVTOC_SIZEOF)
				+ SVTOC_FLAG,
				sl->sl_vtoc_map[i].svtoc_flag);
		}
	}
	for (i = u = 0; i < SUN_SIZE; i += 2)
		u ^= be16dec(p + i);
	be16enc(p + SL_CKSUM, u);
}
Exemple #12
0
static void
prf_iterate(u_int8_t *r, const u_int8_t *P, size_t Plen,
	    const u_int8_t *S, size_t Slen, size_t c, size_t ind)
{
	int		 first_time = 1;
	size_t		 i;
	size_t		 datalen;
	ssize_t		 tmplen;
	u_int8_t	*data;
	u_int8_t	 tmp[128];

	data = emalloc(Slen + 4);
	(void)memcpy(data, S, Slen);
	be32enc(data + Slen, ind);
	datalen = Slen + 4;

	for (i=0; i < c; i++) {
		tmplen = hmac("sha1", P, Plen, data, datalen, tmp, sizeof(tmp));

		assert(tmplen == PRF_BLOCKLEN);

		if (first_time) {
			(void)memcpy(r, tmp, PRF_BLOCKLEN);
			first_time = 0;
		} else
			memxor(r, tmp, PRF_BLOCKLEN);
		(void)memcpy(data, tmp, PRF_BLOCKLEN);
		datalen = PRF_BLOCKLEN;
	}
	free(data);
}
Exemple #13
0
/**
 * proto_kvlds_response_get(Q, ID, status, value):
 * Send a GET response with ID ${ID}, status ${status}, and value ${value}
 * (if ${status} == 0) to the write queue ${Q} indicating that the provided
 * key is associated with the specified data (or not).
 */
int
proto_kvlds_response_get(struct netbuf_write * Q, uint64_t ID,
    uint32_t status, const struct kvldskey * value)
{
	uint8_t * wbuf;
	size_t len;

	/* Compute the response length. */
	len = 4;
	if (status == 0)
		len += kvldskey_serial_size(value);

	/* Get a packet data buffer. */
	if ((wbuf = wire_writepacket_getbuf(Q, ID, len)) == NULL)
		goto err0;

	/* Write the packet data. */
	be32enc(&wbuf[0], status);
	if (status == 0)
		kvldskey_serialize(value, &wbuf[4]);

	/* Finish the packet. */
	if (wire_writepacket_done(Q, wbuf, len))
		goto err0;

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}
Exemple #14
0
/**
 * proto_lbs_response_append(Q, ID, status, blkno):
 * Send an APPEND response with ID ${ID} to the write queue ${Q} with status
 * code ${status} and next block number ${blkno} if ${status} is zero.
 */
int
proto_lbs_response_append(struct netbuf_write * Q, uint64_t ID,
    uint32_t status, uint64_t blkno)
{
	uint8_t * wbuf;
	size_t len;

	/* Compute the response length. */
	len = (status == 0) ? 12 : 4;

	/* Get a packet data buffer. */
	if ((wbuf = wire_writepacket_getbuf(Q, ID, len)) == NULL)
		goto err0;

	/* Write the packet data. */
	be32enc(&wbuf[0], status);
	if (status == 0)
		be64enc(&wbuf[4], blkno);

	/* Finish the packet. */
	if (wire_writepacket_done(Q, wbuf, len))
		goto err0;

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}
Exemple #15
0
/**
 * proto_lbs_response_params2(Q, ID, blklen, blkno, lastblk):
 * Send a PARAMS2 response with ID ${ID} to the write queue ${Q} indicating
 * that the block size is ${blklen} bytes, the next available block # is
 * ${blkno}, and the last block written was ${lastblk}.
 */
int
proto_lbs_response_params2(struct netbuf_write * Q, uint64_t ID,
    uint32_t blklen, uint64_t blkno, uint64_t lastblk)
{
	uint8_t * wbuf;

	/* Get a packet data buffer. */
	if ((wbuf = wire_writepacket_getbuf(Q, ID, 20)) == NULL)
		goto err0;

	/* Write the packet data. */
	be32enc(&wbuf[0], blklen);
	be64enc(&wbuf[4], blkno);
	be64enc(&wbuf[12], lastblk);

	/* Finish the packet. */
	if (wire_writepacket_done(Q, wbuf, 20))
		goto err0;

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}
Exemple #16
0
/**
 * proto_crypt_enc(ibuf, len, obuf, k):
 * Encrypt ${len} bytes from ${ibuf} into PCRYPT_ESZ bytes using the keys in
 * ${k}, and write the result into ${obuf}.
 */
void
proto_crypt_enc(uint8_t * ibuf, size_t len, uint8_t obuf[PCRYPT_ESZ],
    struct proto_keys * k)
{
	HMAC_SHA256_CTX ctx;
	uint8_t pnum_exp[8];

	/* Sanity-check the length. */
	assert(len <= PCRYPT_MAXDSZ);

	/* Copy the decrypted data into the encrypted buffer. */
	memcpy(obuf, ibuf, len);

	/* Pad up to PCRYPT_MAXDSZ with zeroes. */
	memset(&obuf[len], 0, PCRYPT_MAXDSZ - len);

	/* Add the length. */
	be32enc(&obuf[PCRYPT_MAXDSZ], len);

	/* Encrypt the buffer in-place. */
	crypto_aesctr_buf(k->k_aes, k->pnum, obuf, obuf, PCRYPT_MAXDSZ + 4);

	/* Append an HMAC. */
	be64enc(pnum_exp, k->pnum);
	HMAC_SHA256_Init(&ctx, k->k_hmac, 32);
	HMAC_SHA256_Update(&ctx, obuf, PCRYPT_MAXDSZ + 4);
	HMAC_SHA256_Update(&ctx, pnum_exp, 8);
	HMAC_SHA256_Final(&obuf[PCRYPT_MAXDSZ + 4], &ctx);

	/* Increment packet number. */
	k->pnum += 1;
}
Exemple #17
0
/**
 * crypto_MGF1(seed, seedlen, buf, buflen):
 * The MGF1 mask generation function, as specified in RFC 3447.
 */
void
crypto_MGF1(uint8_t * seed, size_t seedlen, uint8_t * buf, size_t buflen)
{
	uint8_t hbuf[32];
	size_t pos;
	uint32_t i;
	uint8_t C[4];

	/* Sanity check for I2OSP function. */
	assert(((buflen - 1) / 32) <= UINT32_MAX);

	/* Iterate through the buffer. */
	for (pos = 0; pos < buflen; pos += 32) {
		/* The ith block starts at position i * 32. */
		i = (uint32_t)(pos / 32);

		/* Convert counter to big-endian format. */
		be32enc(C, i);

		/* Compute the hash of (seed || C). */
		if (crypto_hash_data_2(CRYPTO_KEY_HMAC_SHA256, seed, seedlen,
		    C, 4, hbuf)) {
			warn0("Programmer error: "
			    "SHA256 should never fail");
			abort();
		}

		/* Copy as much data as needed. */
		if (buflen - pos > 32)
			memcpy(buf + pos, hbuf, 32);
		else
			memcpy(buf + pos, hbuf, buflen - pos);
	}
}
Exemple #18
0
static void
prf_iterate(u_int8_t *r, const u_int8_t *P, size_t Plen,
	    const u_int8_t *S, size_t Slen, size_t c, size_t ind)
{
	int		 first_time = 1;
	size_t		 i;
	size_t		 datalen;
	unsigned int	 tmplen;
	u_int8_t	*data;
	u_int8_t	 tmp[EVP_MAX_MD_SIZE];

	data = emalloc(Slen + 4);
	(void)memcpy(data, S, Slen);
	be32enc(data + Slen, ind);
	datalen = Slen + 4;

	for (i=0; i < c; i++) {
		(void)HMAC(EVP_sha1(), P, Plen, data, datalen, tmp, &tmplen);

		assert(tmplen == PRF_BLOCKLEN);

		if (first_time) {
			(void)memcpy(r, tmp, PRF_BLOCKLEN);
			first_time = 0;
		} else 
			memxor(r, tmp, PRF_BLOCKLEN);
		(void)memcpy(data, tmp, PRF_BLOCKLEN);
		datalen = PRF_BLOCKLEN;
	}
	free(data);
}
Exemple #19
0
int scanhash_lyra2re(int thr_id, struct work *work,
	uint32_t max_nonce,	uint64_t *hashes_done)
{
        uint32_t *pdata = work->data;
        uint32_t *ptarget = work->target;
	uint32_t _ALIGN(64) endiandata[20];
        uint32_t hash[8] __attribute__((aligned(32)));
	const uint32_t first_nonce = pdata[19];
	uint32_t nonce = first_nonce;
        const uint32_t Htarg = ptarget[7];

        swab32_array( endiandata, pdata, 20 );

	do {
		be32enc(&endiandata[19], nonce);
		lyra2re_hash(hash, endiandata);
		if (hash[7] <= Htarg )
                {
                   if ( fulltest(hash, ptarget) )
                   {
			pdata[19] = nonce;
			*hashes_done = pdata[19] - first_nonce;
			return 1;
                   }
		}
		nonce++;

	} while (nonce < max_nonce && !work_restart[thr_id].restart);

	pdata[19] = nonce;
	*hashes_done = pdata[19] - first_nonce + 1;
	return 0;
}
Exemple #20
0
void sha256func(unsigned char *hash, const unsigned char *data, int len)
{
    uint32_t S[16], T[16];
    int i, r;

    sha256_init(S);
    for (r = len; r > -9; r -= 64) {
        if (r < 64)
            memset(T, 0, 64);
        memcpy(T, data + len - r, r > 64 ? 64 : (r < 0 ? 0 : r));
        if (r >= 0 && r < 64)
            ((unsigned char *)T)[r] = 0x80;
        for (i = 0; i < 16; i++)
            T[i] = be32dec(T + i);
        if (r < 56)
            T[15] = 8 * len;
        sha256_transform(S, T, 0);
    }
    /*
    memcpy(S + 8, sha256d_hash1 + 8, 32);
    sha256_init(T);
    sha256_transform(T, S, 0);
    */
    for (i = 0; i < 8; i++)
        be32enc((uint32_t *)hash + i, T[i]);
}
Exemple #21
0
char* hodl_malloc_txs_request( struct work *work )
{
  char* req;
  json_t *val;
  char data_str[2 * sizeof(work->data) + 1];
  int i;

  for ( i = 0; i < ARRAY_SIZE(work->data); i++ )
    be32enc( work->data + i, work->data[i] );

  bin2hex( data_str, (unsigned char *)work->data, 88 );
  if ( work->workid )
  {
    char *params;
    val = json_object();
    json_object_set_new( val, "workid", json_string( work->workid ) );
    params = json_dumps( val, 0 );
    json_decref( val );
    req = malloc( 128 + 2*88 + strlen( work->txs ) + strlen( params ) );
    sprintf( req,
     "{\"method\": \"submitblock\", \"params\": [\"%s%s\", %s], \"id\":1}\r\n",
      data_str, work->txs, params);
    free( params );
  }
  else
  {
    req = malloc( 128 + 2*88 + strlen(work->txs));
    sprintf( req,
       "{\"method\": \"submitblock\", \"params\": [\"%s%s\"], \"id\":1}\r\n",
        data_str, work->txs);
  }
  return req;
}
Exemple #22
0
static const char *tencode(int t, uint64_t val)
{
	static char res[64];
	uint8_t buf[16];
	bool be = t > 0;
	int i;

	if (t < 0) t = -t;

	memset(buf, 0xFC, sizeof(buf));

	if (be) {
		switch (t) {
		case 2: be16enc(buf, val); break;
		case 4: be32enc(buf, val); break;
		case 8: be64enc(buf, val); break;
		}
	} else {
		switch (t) {
		case 2: le16enc(buf, val); break;
		case 4: le32enc(buf, val); break;
		case 8: le64enc(buf, val); break;
		}
	}

	for (i = t; i < (int)sizeof(buf); i++) {
		if (buf[i] != 0xFC)
			return "OVER";
	}

	snprintf(res, sizeof(res), "%02X %02X %02X %02X %02X %02X %02X %02X ",
		 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
	res[t*3 - 1] = 0;
	return res;
}
Exemple #23
0
int scanhash_veltor_4way( int thr_id, struct work *work, uint32_t max_nonce,
                          uint64_t *hashes_done )
{
     uint32_t hash[4*8] __attribute__ ((aligned (64)));
     uint32_t vdata[24*4] __attribute__ ((aligned (64)));
     uint32_t endiandata[20] __attribute__((aligned(64)));
     uint32_t *pdata = work->data;
     uint32_t *ptarget = work->target;
     const uint32_t Htarg = ptarget[7];
     const uint32_t first_nonce = pdata[19];
     uint32_t n = first_nonce;
     uint32_t *nonces = work->nonces;
     int num_found = 0;
     uint32_t *noncep = vdata + 73;   // 9*8 + 1
     volatile uint8_t *restart = &(work_restart[thr_id].restart);

     if ( opt_benchmark )
        ptarget[7] = 0x0cff;
     for ( int i=0; i < 19; i++ )
     {
        be32enc( &endiandata[i], pdata[i] );
     }

     uint64_t *edata = (uint64_t*)endiandata;
     mm256_interleave_4x64( (uint64_t*)vdata, edata, edata, edata, edata, 640 );
     do
     {
         be32enc( noncep,   n   );
         be32enc( noncep+2, n+1 );
         be32enc( noncep+4, n+2 );
         be32enc( noncep+6, n+3 );

         veltor_4way_hash( hash, vdata );
         pdata[19] = n;

         for ( int i = 0; i < 4; i++ )
         if ( (hash+(i<<3))[7] <= Htarg && fulltest( hash+(i<<3), ptarget ) )
         {
            pdata[19] = n+i;
            nonces[ num_found++ ] = n+i;
            work_set_target_ratio( work, hash+(i<<3) );
         }
         n += 4;
     } while ( ( num_found == 0 ) && ( n < max_nonce ) && !(*restart) );
     *hashes_done = n - first_nonce + 1;
     return num_found;
}
Exemple #24
0
extern "C" int scanhash_groestlcoin(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
    uint32_t max_nonce, unsigned long *hashes_done)
{
    uint32_t start_nonce = pdata[19]++;
    uint32_t throughput = device_intensity(thr_id, __func__, 1 << 19); // 256*256*8
    throughput = min(throughput, max_nonce - start_nonce);

    uint32_t *outputHash = (uint32_t*)malloc(throughput * 16 * sizeof(uint32_t));

    if (opt_benchmark)
        ((uint32_t*)ptarget)[7] = 0x000000ff;

    // init
    if(!init[thr_id])
    {
        groestlcoin_cpu_init(thr_id, throughput);
        init[thr_id] = true;
    }

    // Endian Drehung ist notwendig
    uint32_t endiandata[32];
    for (int kk=0; kk < 32; kk++)
        be32enc(&endiandata[kk], pdata[kk]);

    // Context mit dem Endian gedrehten Blockheader vorbereiten (Nonce wird später ersetzt)
    groestlcoin_cpu_setBlock(thr_id, endiandata, (void*)ptarget);

    do {
        // GPU
        uint32_t foundNounce = 0xFFFFFFFF;
        const uint32_t Htarg = ptarget[7];

        groestlcoin_cpu_hash(thr_id, throughput, pdata[19], outputHash, &foundNounce);

        if(foundNounce < 0xffffffff)
        {
            uint32_t tmpHash[8];
            endiandata[19] = SWAP32(foundNounce);
            groestlhash(tmpHash, endiandata);

            if (tmpHash[7] <= Htarg && fulltest(tmpHash, ptarget)) {
                pdata[19] = foundNounce;
                *hashes_done = foundNounce - start_nonce + 1;
                free(outputHash);
                return true;
            } else {
                applog(LOG_INFO, "GPU #%d: result for nonce $%08X does not validate on CPU!", thr_id, foundNounce);
            }

            foundNounce = 0xffffffff;
        }

		pdata[19] += throughput;
	} while (!work_restart[thr_id].restart && ((uint64_t)max_nonce > ((uint64_t)(pdata[19]) + (uint64_t)throughput)));

    *hashes_done = pdata[19] - start_nonce + 1;
    free(outputHash);
    return 0;
}
Exemple #25
0
void
isns_req_add_32(struct isns_req *req, uint32_t tag, uint32_t value)
{
	uint32_t beval;

	be32enc(&beval, value);
	isns_req_add(req, tag, sizeof(value), &beval);
}
Exemple #26
0
void
isns_req_add(struct isns_req *req, uint32_t tag, uint32_t len,
    const void *value)
{
	struct isns_tlv *tlv;
	uint32_t vlen;

	vlen = len + ((len & 3) ? (4 - (len & 3)) : 0);
	isns_req_getspace(req, sizeof(*tlv) + vlen);
	tlv = (struct isns_tlv *)&req->ir_buf[req->ir_usedlen];
	be32enc(tlv->it_tag, tag);
	be32enc(tlv->it_length, vlen);
	memcpy(tlv->it_value, value, len);
	if (vlen != len)
		memset(&tlv->it_value[len], 0, vlen - len);
	req->ir_usedlen += sizeof(*tlv) + vlen;
}
Exemple #27
0
extern "C" int scanhash_fugue256(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
	uint32_t max_nonce, unsigned long *hashes_done)
{
	uint32_t start_nonce = pdata[19]++;
	unsigned int intensity = (device_sm[device_map[thr_id]] > 500) ? 22 : 19;
	uint32_t throughput = device_intensity(device_map[thr_id], __func__, 1 << intensity); // 256*256*8
	throughput = min(throughput, max_nonce - start_nonce);

	if (opt_benchmark)
		((uint32_t*)ptarget)[7] = 0xf;

	// init
	if(!init[thr_id])
	{
		fugue256_cpu_init(thr_id, throughput);
		init[thr_id] = true;
	}

	// Endian Drehung ist notwendig
	uint32_t endiandata[20];
	for (int kk=0; kk < 20; kk++)
		be32enc(&endiandata[kk], pdata[kk]);

	// Context mit dem Endian gedrehten Blockheader vorbereiten (Nonce wird später ersetzt)
	fugue256_cpu_setBlock(thr_id, endiandata, (void*)ptarget);

	do {
		// GPU
		uint32_t foundNounce = 0xFFFFFFFF;
		fugue256_cpu_hash(thr_id, throughput, pdata[19], NULL, &foundNounce);

		if(foundNounce < 0xffffffff)
		{
			uint32_t hash[8];
			const uint32_t Htarg = ptarget[7];

			endiandata[19] = SWAP32(foundNounce);
			sph_fugue256_context ctx_fugue;
			sph_fugue256_init(&ctx_fugue);
			sph_fugue256 (&ctx_fugue, endiandata, 80);
			sph_fugue256_close(&ctx_fugue, &hash);

			if (hash[7] <= Htarg && fulltest(hash, ptarget))
			{
				pdata[19] = foundNounce;
				*hashes_done = foundNounce - start_nonce + 1;
				return 1;
			} else {
				applog(LOG_INFO, "GPU #%d: result for nonce $%08X does not validate on CPU!", thr_id, foundNounce);
			}
		}

		pdata[19] += throughput;
	} while (!work_restart[thr_id].restart && ((uint64_t)max_nonce > ((uint64_t)(pdata[19]) + (uint64_t)throughput)));

	*hashes_done = pdata[19] - start_nonce + 1;
	return 0;
}
Exemple #28
0
void decred_be_build_stratum_request( char *req, struct work *work,
                                      struct stratum_ctx *sctx )
{
   unsigned char *xnonce2str;
   uint32_t ntime, nonce;
   char ntimestr[9], noncestr[9];

   be32enc( &ntime, work->data[ DECRED_NTIME_INDEX ] );
   be32enc( &nonce, work->data[ DECRED_NONCE_INDEX ] );
   bin2hex( ntimestr, (char*)(&ntime), sizeof(uint32_t) );
   bin2hex( noncestr, (char*)(&nonce), sizeof(uint32_t) );
   xnonce2str = abin2hex( (char*)( &work->data[ DECRED_XNONCE_INDEX ] ),
                                     sctx->xnonce1_size );
   snprintf( req, JSON_BUF_LEN,
        "{\"method\": \"mining.submit\", \"params\": [\"%s\", \"%s\", \"%s\", \"%s\", \"%s\"], \"id\":4}",
         rpc_user, work->job_id, xnonce2str, ntimestr, noncestr );
   free(xnonce2str);
}
Exemple #29
0
int scanhash_lyra2z(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
{

	size_t size = (int64_t) ((int64_t) 16 * 16 * 96);
    uint64_t *wholeMatrix = _mm_malloc(size, 64);

	uint32_t _ALIGN(128) hash[8];
	uint32_t _ALIGN(128) endiandata[20];
	uint32_t *pdata = work->data;
	uint32_t *ptarget = work->target;

	const uint32_t Htarg = ptarget[7];
	const uint32_t first_nonce = pdata[19];
	uint32_t nonce = first_nonce;

	if (opt_benchmark)
		ptarget[7] = 0x0000ff;

	for (int i=0; i < 19; i++) {
		be32enc(&endiandata[i], pdata[i]);
	}

	do {
		be32enc(&endiandata[19], nonce);
		lyra2z_hash(wholeMatrix, hash, endiandata);
//		lyra2z_hash(0, hash, endiandata);

		if (hash[7] <= Htarg && fulltest(hash, ptarget)) {
			work_set_target_ratio(work, hash);
			pdata[19] = nonce;
			*hashes_done = pdata[19] - first_nonce;
			_mm_free(wholeMatrix);
			return 1;
		}
		nonce++;

	} while (nonce < max_nonce && !work_restart[thr_id].restart);

	pdata[19] = nonce;
	*hashes_done = pdata[19] - first_nonce + 1;
	_mm_free(wholeMatrix);
	return 0;
}
Exemple #30
0
int scanhash_x16r(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
{
	uint32_t _ALIGN(128) hash32[8];
	uint32_t _ALIGN(128) endiandata[20];
	uint32_t *pdata = work->data;
	uint32_t *ptarget = work->target;
	const uint32_t Htarg = ptarget[7];
	const uint32_t first_nonce = pdata[19];
	uint32_t nonce = first_nonce;
	volatile uint8_t *restart = &(work_restart[thr_id].restart);

	for (int k=0; k < 19; k++)
		be32enc(&endiandata[k], pdata[k]);

	if (s_ntime != pdata[17]) {
		uint32_t ntime = swab32(pdata[17]);
		getAlgoString((const char*) (&endiandata[1]), hashOrder);
		s_ntime = ntime;
		if (opt_debug && !thr_id) applog(LOG_DEBUG, "hash order %s (%08x)", hashOrder, ntime);
	}

	if (opt_benchmark)
		ptarget[7] = 0x0cff;

	do {
		be32enc(&endiandata[19], nonce);
		x16r_hash(hash32, endiandata);

		if (hash32[7] <= Htarg && fulltest(hash32, ptarget)) {
			work_set_target_ratio(work, hash32);
			pdata[19] = nonce;
			*hashes_done = pdata[19] - first_nonce;
			return 1;
		}
		nonce++;

	} while (nonce < max_nonce && !(*restart));

	pdata[19] = nonce;
	*hashes_done = pdata[19] - first_nonce + 1;
	return 0;
}