Browse Source

Use explicit host to BE functions in scrypt code instead of hard coding byteswap everywhere.

nfactor-troky
Con Kolivas 12 years ago
parent
commit
04c7a21ddd
  1. 12
      scrypt.c

12
scrypt.c

@ -34,8 +34,6 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#define byteswap(x) ((((x) << 24) & 0xff000000u) | (((x) << 8) & 0x00ff0000u) | (((x) >> 8) & 0x0000ff00u) | (((x) >> 24) & 0x000000ffu))
typedef struct SHA256Context { typedef struct SHA256Context {
uint32_t state[8]; uint32_t state[8];
uint32_t buf[16]; uint32_t buf[16];
@ -51,7 +49,7 @@ be32enc_vect(uint32_t *dst, const uint32_t *src, uint32_t len)
uint32_t i; uint32_t i;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
dst[i] = byteswap(src[i]); dst[i] = htobe32(src[i]);
} }
/* Elementary functions used by SHA256 */ /* Elementary functions used by SHA256 */
@ -94,7 +92,7 @@ SHA256_Transform(uint32_t * state, const uint32_t block[16], int swap)
/* 1. Prepare message schedule W. */ /* 1. Prepare message schedule W. */
if(swap) if(swap)
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
W[i] = byteswap(block[i]); W[i] = htobe32(block[i]);
else else
memcpy(W, block, 64); memcpy(W, block, 64);
for (i = 16; i < 64; i += 2) { for (i = 16; i < 64; i += 2) {
@ -295,7 +293,7 @@ PBKDF2_SHA256_80_128_32(const uint32_t * passwd, const uint32_t * salt)
/* Feed the inner hash to the outer SHA256 operation. */ /* Feed the inner hash to the outer SHA256 operation. */
SHA256_Transform(ostate, pad, 0); SHA256_Transform(ostate, pad, 0);
/* Finish the outer SHA256 operation. */ /* Finish the outer SHA256 operation. */
return byteswap(ostate[7]); return be32toh(ostate[7]);
} }
@ -415,7 +413,7 @@ bool scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t no
uint32_t data[20]; uint32_t data[20];
be32enc_vect(data, (const uint32_t *)pdata, 19); be32enc_vect(data, (const uint32_t *)pdata, 19);
data[19] = byteswap(nonce); data[19] = htobe32(nonce);
scratchbuf = alloca(131584); scratchbuf = alloca(131584);
tmp_hash7 = scrypt_1024_1_1_256_sp(data, scratchbuf); tmp_hash7 = scrypt_1024_1_1_256_sp(data, scratchbuf);
@ -448,7 +446,7 @@ bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *p
tmp_hash7 = scrypt_1024_1_1_256_sp(data, scratchbuf); tmp_hash7 = scrypt_1024_1_1_256_sp(data, scratchbuf);
if (unlikely(tmp_hash7 <= Htarg)) { if (unlikely(tmp_hash7 <= Htarg)) {
((uint32_t *)pdata)[19] = byteswap(n); ((uint32_t *)pdata)[19] = htobe32(n);
*last_nonce = n; *last_nonce = n;
ret = true; ret = true;
break; break;

Loading…
Cancel
Save