Browse Source

Test

etomic
jl777 8 years ago
parent
commit
57cb61f984
  1. 54
      crypto777/hmac/sha256.c
  2. 34
      crypto777/scrypt.c

54
crypto777/hmac/sha256.c

@ -28,9 +28,9 @@ const struct ltc_hash_descriptor sha256_desc =
{ 2, 16, 840, 1, 101, 3, 4, 2, 1, }, { 2, 16, 840, 1, 101, 3, 4, 2, 1, },
9, 9,
&sha256_init, &sha256i_init,
&sha256_process, &sha256i_process,
&sha256_done, &sha256i_done,
&sha256_test, &sha256_test,
NULL NULL
}; };
@ -227,7 +227,7 @@ int sha256_init(hash_state * md)
@param inlen The length of the data (octets) @param inlen The length of the data (octets)
@return CRYPT_OK if successful @return CRYPT_OK if successful
*/ */
HASH_PROCESS(sha256_process, sha256_compress, sha256, 64) HASH_PROCESS(sha256i_process, sha256_compress, sha256, 64)
/** /**
Terminate the hash to get the digest Terminate the hash to get the digest
@ -235,7 +235,7 @@ HASH_PROCESS(sha256_process, sha256_compress, sha256, 64)
@param out [out] The destination of the hash (32 bytes) @param out [out] The destination of the hash (32 bytes)
@return CRYPT_OK if successful @return CRYPT_OK if successful
*/ */
int sha256_done(hash_state * md, unsigned char *out) int sha256i_done(hash_state * md, unsigned char *out)
{ {
int i; int i;
@ -287,9 +287,9 @@ int sha256_done(hash_state * md, unsigned char *out)
void calc_sha256(char hashstr[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len) void calc_sha256(char hashstr[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len)
{ {
hash_state md; hash_state md;
sha256_init(&md); sha256i_init(&md);
sha256_process(&md,src,len); sha256i_process(&md,src,len);
sha256_done(&md,hash); sha256i_done(&md,hash);
if ( hashstr != 0 ) if ( hashstr != 0 )
{ {
int32_t init_hexbytes_noT(char *hexbytes,uint8_t *message,long len); int32_t init_hexbytes_noT(char *hexbytes,uint8_t *message,long len);
@ -300,11 +300,11 @@ void calc_sha256(char hashstr[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t
void calc_sha256cat(uint8_t hash[256 >> 3],uint8_t *src,int32_t len,uint8_t *src2,int32_t len2) void calc_sha256cat(uint8_t hash[256 >> 3],uint8_t *src,int32_t len,uint8_t *src2,int32_t len2)
{ {
hash_state md; hash_state md;
sha256_init(&md); sha256i_init(&md);
sha256_process(&md,src,len); sha256i_process(&md,src,len);
if ( src2 != 0 ) if ( src2 != 0 )
sha256_process(&md,src2,len2); sha256i_process(&md,src2,len2);
sha256_done(&md,hash); sha256i_done(&md,hash);
} }
void update_sha256(uint8_t hash[256 >> 3],struct sha256_state *state,uint8_t *src,int32_t len) void update_sha256(uint8_t hash[256 >> 3],struct sha256_state *state,uint8_t *src,int32_t len)
@ -312,14 +312,14 @@ void update_sha256(uint8_t hash[256 >> 3],struct sha256_state *state,uint8_t *sr
hash_state md; hash_state md;
memset(&md,0,sizeof(md)); memset(&md,0,sizeof(md));
if ( src == 0 ) if ( src == 0 )
sha256_init(&md); sha256i_init(&md);
else else
{ {
md.sha256 = *state; md.sha256 = *state;
sha256_process(&md,src,len); sha256i_process(&md,src,len);
} }
*state = md.sha256; *state = md.sha256;
sha256_done(&md,hash); sha256i_done(&md,hash);
} }
/*void calc_OP_HASH160(char hexstr[41],uint8_t hash160[20],char *pubkey) /*void calc_OP_HASH160(char hexstr[41],uint8_t hash160[20],char *pubkey)
@ -334,9 +334,9 @@ void update_sha256(uint8_t hash[256 >> 3],struct sha256_state *state,uint8_t *sr
return; return;
} }
decode_hex(buf,len,pubkey); decode_hex(buf,len,pubkey);
sha256_init(&md); sha256i_init(&md);
sha256_process(&md,buf,len); sha256i_process(&md,buf,len);
sha256_done(&md,sha256); sha256i_done(&md,sha256);
rmd160_init(&md); rmd160_init(&md);
rmd160_process(&md,sha256,256 >> 3); rmd160_process(&md,sha256,256 >> 3);
@ -389,9 +389,9 @@ int sha256_test(void)
char *str; char *str;
for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
sha256_init(&md); sha256i_init(&md);
sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); sha256i_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
sha256_done(&md, tmp); sha256i_done(&md, tmp);
if (XMEMCMP(tmp, tests[i].hash, 32) != 0) { if (XMEMCMP(tmp, tests[i].hash, 32) != 0) {
for (j=0; j<32; j++) for (j=0; j<32; j++)
printf("%02x",tmp[j]); printf("%02x",tmp[j]);
@ -400,16 +400,16 @@ int sha256_test(void)
strcpy(str,(char*)tests[i].msg); strcpy(str,(char*)tests[i].msg);
reverse_hexstr(str); reverse_hexstr(str);
printf("reversed.(%s)\n",str); printf("reversed.(%s)\n",str);
sha256_init(&md); sha256i_init(&md);
sha256_process(&md, (unsigned char*)str, (unsigned long)strlen(str)); sha256i_process(&md, (unsigned char*)str, (unsigned long)strlen(str));
sha256_done(&md, tmp); sha256i_done(&md, tmp);
for (j=0; j<32; j++) for (j=0; j<32; j++)
printf("%02x",tmp[j]); printf("%02x",tmp[j]);
printf(" <- sha256(%s)\n",str); printf(" <- sha256(%s)\n",str);
decode_hex(buf,(int)strlen(tests[i].msg),tests[i].msg); decode_hex(buf,(int)strlen(tests[i].msg),tests[i].msg);
sha256_init(&md); sha256i_init(&md);
sha256_process(&md, (unsigned char*)buf, (unsigned long)strlen(tests[i].msg)/2); sha256i_process(&md, (unsigned char*)buf, (unsigned long)strlen(tests[i].msg)/2);
sha256_done(&md, tmp); sha256i_done(&md, tmp);
for (j=0; j<32; j++) for (j=0; j<32; j++)
printf("%02x",tmp[j]); printf("%02x",tmp[j]);
printf(" <- sha256(binary %s)\n",tests[i].msg); printf(" <- sha256(binary %s)\n",tests[i].msg);

34
crypto777/scrypt.c

@ -69,7 +69,7 @@ static const uint32_t sha256_k[64] = {
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
}; };
static inline void sha256_init(uint32_t *state) static inline void scrypt_sha256_init(uint32_t *state)
{ {
memcpy(state, sha256_h, 32); memcpy(state, sha256_h, 32);
} }
@ -102,7 +102,7 @@ W[i] + sha256_k[i])
#define swab32(x) ((((x) << 24) & 0xff000000u) | (((x) << 8) & 0x00ff0000u) | (((x) >> 8) & 0x0000ff00u) | (((x) >> 24) & 0x000000ffu)) #define swab32(x) ((((x) << 24) & 0xff000000u) | (((x) << 8) & 0x00ff0000u) | (((x) >> 8) & 0x0000ff00u) | (((x) >> 24) & 0x000000ffu))
static inline void sha256_transform(uint32_t *state, const uint32_t *block, int swap) static inline void scrypt_sha256_transform(uint32_t *state, const uint32_t *block, int swap)
{ {
uint32_t W[64]; uint32_t W[64];
uint32_t S[8]; uint32_t S[8];
@ -203,29 +203,29 @@ static inline void HMAC_SHA256_80_init(const uint32_t *key,uint32_t *tstate, uin
/* tstate is assumed to contain the midstate of key */ /* tstate is assumed to contain the midstate of key */
memcpy(pad, key + 16, 16); memcpy(pad, key + 16, 16);
memcpy(pad + 4, keypad, 48); memcpy(pad + 4, keypad, 48);
sha256_transform(tstate, pad, 0); scrypt_sha256_transform(tstate, pad, 0);
memcpy(ihash, tstate, 32); memcpy(ihash, tstate, 32);
sha256_init(ostate); scrypt_sha256_init(ostate);
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
pad[i] = ihash[i] ^ 0x5c5c5c5c; pad[i] = ihash[i] ^ 0x5c5c5c5c;
for (; i < 16; i++) for (; i < 16; i++)
pad[i] = 0x5c5c5c5c; pad[i] = 0x5c5c5c5c;
sha256_transform(ostate, pad, 0); scrypt_sha256_transform(ostate, pad, 0);
sha256_init(tstate); scrypt_sha256_init(tstate);
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
pad[i] = ihash[i] ^ 0x36363636; pad[i] = ihash[i] ^ 0x36363636;
for (; i < 16; i++) for (; i < 16; i++)
pad[i] = 0x36363636; pad[i] = 0x36363636;
sha256_transform(tstate, pad, 0); scrypt_sha256_transform(tstate, pad, 0);
} }
static inline void PBKDF2_SHA256_80_128(const uint32_t *tstate,const uint32_t *ostate, const uint32_t *salt, uint32_t *output) static inline void PBKDF2_SHA256_80_128(const uint32_t *tstate,const uint32_t *ostate, const uint32_t *salt, uint32_t *output)
{ {
uint32_t istate[8], ostate2[8],ibuf[16], obuf[16]; int i, j; uint32_t istate[8], ostate2[8],ibuf[16], obuf[16]; int i, j;
memcpy(istate, tstate, 32); memcpy(istate, tstate, 32);
sha256_transform(istate, salt, 0); scrypt_sha256_transform(istate, salt, 0);
memcpy(ibuf, salt + 16, 16); memcpy(ibuf, salt + 16, 16);
memcpy(ibuf + 5, innerpad, 44); memcpy(ibuf + 5, innerpad, 44);
memcpy(obuf + 8, outerpad, 32); memcpy(obuf + 8, outerpad, 32);
@ -233,9 +233,9 @@ static inline void PBKDF2_SHA256_80_128(const uint32_t *tstate,const uint32_t *o
{ {
memcpy(obuf, istate, 32); memcpy(obuf, istate, 32);
ibuf[4] = i + 1; ibuf[4] = i + 1;
sha256_transform(obuf, ibuf, 0); scrypt_sha256_transform(obuf, ibuf, 0);
memcpy(ostate2, ostate, 32); memcpy(ostate2, ostate, 32);
sha256_transform(ostate2, obuf, 0); scrypt_sha256_transform(ostate2, obuf, 0);
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
output[8 * i + j] = swab32(ostate2[j]); output[8 * i + j] = swab32(ostate2[j]);
} }
@ -244,12 +244,12 @@ static inline void PBKDF2_SHA256_80_128(const uint32_t *tstate,const uint32_t *o
static inline void PBKDF2_SHA256_128_32(uint32_t *tstate, uint32_t *ostate,const uint32_t *salt, uint32_t *output) static inline void PBKDF2_SHA256_128_32(uint32_t *tstate, uint32_t *ostate,const uint32_t *salt, uint32_t *output)
{ {
uint32_t buf[16]; int i; uint32_t buf[16]; int i;
sha256_transform(tstate, salt, 1); scrypt_sha256_transform(tstate, salt, 1);
sha256_transform(tstate, salt + 16, 1); scrypt_sha256_transform(tstate, salt + 16, 1);
sha256_transform(tstate, finalblk, 0); scrypt_sha256_transform(tstate, finalblk, 0);
memcpy(buf, tstate, 32); memcpy(buf, tstate, 32);
memcpy(buf + 8, outerpad, 32); memcpy(buf + 8, outerpad, 32);
sha256_transform(ostate, buf, 0); scrypt_sha256_transform(ostate, buf, 0);
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
output[i] = swab32(ostate[i]); output[i] = swab32(ostate[i]);
} }
@ -363,11 +363,11 @@ void calc_scrypthash(uint32_t *hash,void *data)
uint8_t *scratchbuf; uint32_t midstate[8]; uint8_t *scratchbuf; uint32_t midstate[8];
memset(midstate,0,sizeof(midstate)); memset(midstate,0,sizeof(midstate));
memset(hash,0,32); memset(hash,0,32);
sha256_init(midstate); scrypt_sha256_init(midstate);
sha256_transform(midstate,(void *)data,0); scrypt_sha256_transform(midstate,(void *)data,0);
scratchbuf = malloc(1024 * 128 + 64); scratchbuf = malloc(1024 * 128 + 64);
scrypt_1024_1_1_256((void *)data,hash,midstate,scratchbuf,1024); scrypt_1024_1_1_256((void *)data,hash,midstate,scratchbuf,1024);
free(scratchbuf); free(scratchbuf);
} }
//010000000000000000000000000000000000000000000000000000000000000000000000d9ced4ed1130f7b7faad9be25323ffafa33232a17c3edf6cfd97bee6bafbdd97b9aa8e4ef0ff0f1ecd513f7c //010000000000000000000000000000000000000000000000000000000000000000000000d9ced4ed1130f7b7faad9be25323ffafa33232a17c3edf6cfd97bee6bafbdd97b9aa8e4ef0ff0f1ecd513f7c
//010000000000000000000000000000000000000000000000000000000000000000000000d9ced4ed1130f7b7faad9be25323ffafa33232a17c3edf6cfd97bee6bafbdd97b9aa8e4ef0ff0f1ecd513f7c00 //010000000000000000000000000000000000000000000000000000000000000000000000d9ced4ed1130f7b7faad9be25323ffafa33232a17c3edf6cfd97bee6bafbdd97b9aa8e4ef0ff0f1ecd513f7c00

Loading…
Cancel
Save