static inline void HMAC_SHA256_80_init(const uint32_t *key, uint32_t *tstate, uint32_t *ostate) { uint32_t ihash[8]; uint32_t pad[16]; int i; /* tstate is assumed to contain the midstate of key */ memcpy(pad, key + 16, 16); memcpy(pad + 4, keypad, 48); sha256_transform(tstate, pad, 0); memcpy(ihash, tstate, 32); sha256_init(ostate); for (i = 0; i < 8; i++) pad[i] = ihash[i] ^ 0x5c5c5c5c; for (; i < 16; i++) pad[i] = 0x5c5c5c5c; sha256_transform(ostate, pad, 0); sha256_init(tstate); for (i = 0; i < 8; i++) pad[i] = ihash[i] ^ 0x36363636; for (; i < 16; i++) pad[i] = 0x36363636; sha256_transform(tstate, pad, 0); }
void testHash() { unsigned char Digest[32]; sha256_context ctx; sha256_init( &ctx ); sha256_starts( &ctx, 0 ); sha256_update( &ctx, sha256_test_buf[0], sha256_test_buflen[0] ); sha256_finish( &ctx, Digest ); print_buffer(Digest, sizeof(Digest)); memset(Digest, 0, sizeof(Digest)); uint8 buffer[1]; sha256_init( &ctx ); sha256_starts( &ctx, 0 ); buffer[0] = 'a'; sha256_update( &ctx, buffer, 1 ); buffer[0] = 'b'; sha256_update( &ctx, buffer, 1 ); buffer[0] = 'c'; sha256_update( &ctx, buffer, 1 ); buffer[0] = '\0'; sha256_update( &ctx, buffer, 1 ); sha256_finish( &ctx, Digest ); print_buffer(Digest, sizeof(Digest)); }
void yarrow256_init(struct yarrow256_ctx *ctx, unsigned n, struct yarrow_source *s) { unsigned i; sha256_init(&ctx->pools[0]); sha256_init(&ctx->pools[1]); ctx->seeded = 0; /* Not strictly necessary, but it makes it easier to see if the * values are sane. */ memset(ctx->counter, 0, sizeof(ctx->counter)); ctx->nsources = n; ctx->sources = s; for (i = 0; i<n; i++) { ctx->sources[i].estimate[YARROW_FAST] = 0; ctx->sources[i].estimate[YARROW_SLOW] = 0; ctx->sources[i].next = YARROW_FAST; } }
int main() { unsigned char text1[]={"abc"}, text2[]={"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}, text3[]={"aaaaaaaaaa"}, hash[32]; int idx; SHA256_CTX ctx; // Hash one sha256_init(&ctx); sha256_update(&ctx,text1,strlen(text1)); sha256_final(&ctx,hash); print_hash(hash); // Hash two sha256_init(&ctx); sha256_update(&ctx,text2,strlen(text2)); sha256_final(&ctx,hash); print_hash(hash); // Hash three sha256_init(&ctx); for (idx=0; idx < 100000; ++idx) sha256_update(&ctx,text3,strlen(text3)); sha256_final(&ctx,hash); print_hash(hash); getchar(); return 0; }
void hmac_sha256_init(hmac_sha256_ctx_t *s, const void* key, uint16_t keylength_b){ uint8_t buffer[HMAC_SHA256_BLOCK_BYTES]; uint8_t i; memset(buffer, 0, HMAC_SHA256_BLOCK_BYTES); if (keylength_b > HMAC_SHA256_BLOCK_BITS){ sha256((void*)buffer, key, keylength_b); } else { memcpy(buffer, key, (keylength_b+7)/8); } for (i=0; i<HMAC_SHA256_BLOCK_BYTES; ++i){ buffer[i] ^= IPAD; } sha256_init(&(s->a)); sha256_nextBlock(&(s->a), buffer); for (i=0; i<HMAC_SHA256_BLOCK_BYTES; ++i){ buffer[i] ^= IPAD^OPAD; } sha256_init(&(s->b)); sha256_nextBlock(&(s->b), buffer); #if defined SECURE_WIPE_BUFFER memset(buffer, 0, SHA256_BLOCK_BYTES); #endif }
void hmac(char* key,char* m,char len,char* buf){ SHA256_CTX ctx; BYTE concat[130]; BYTE o_key_pad[65]; BYTE i_key_pad[65]; memset(o_key_pad,0,sizeof o_key_pad); memset(i_key_pad,0,sizeof i_key_pad); memcpy(o_key_pad,key,SHA256_BLOCK_SIZE); memcpy(i_key_pad,key,SHA256_BLOCK_SIZE); for(char i=0;i<64;i++){ o_key_pad[i]^=0x5c; i_key_pad[i]^=0x36; } memset(concat,0,sizeof concat); memcpy(concat,i_key_pad,64); memcpy(concat+64,m,len); sha256_init(&ctx); sha256_update(&ctx, concat, 64+len); sha256_final(&ctx, buf); memset(concat,0,sizeof concat); memcpy(concat,o_key_pad,64); memcpy(concat+64,buf,SHA256_BLOCK_SIZE); sha256_init(&ctx); sha256_update(&ctx, concat, 64+SHA256_BLOCK_SIZE); sha256_final(&ctx, buf); }
void hmac_sha256_init(hmac_sha256_ctx *ctx, const void *key, size_t keylen) { uint8_t keybuf[SHA256_BLOCK_LEN], pad[SHA256_BLOCK_LEN]; /* prepare key */ memset(keybuf, 0, sizeof keybuf); if (keylen > sizeof keybuf) sha256_complete(key, keylen, keybuf); else memcpy(keybuf, key, keylen); /* input pad */ for (unsigned int i = 0; i < sizeof pad; ++i) pad[i] = 0x36 ^ keybuf[i]; sha256_init(&ctx->ictx); sha256_update(&ctx->ictx, pad, sizeof pad); /* output pad */ for (unsigned int i = 0; i < sizeof pad; ++i) pad[i] = 0x5c ^ keybuf[i]; sha256_init(&ctx->octx); sha256_update(&ctx->octx, pad, sizeof pad); /* hide the evidence */ memset(keybuf, 0, sizeof keybuf); memset(pad, 0, sizeof pad); }
/*********************** FUNCTION DEFINITIONS ***********************/ void sha256_test() { BYTE text1[] = {"abc"}; BYTE text2[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; BYTE text3[] = {"aaaaaaaaaa"}; BYTE hash1[SHA256_BLOCK_SIZE] = {0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23, 0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad}; BYTE hash2[SHA256_BLOCK_SIZE] = {0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39, 0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1}; BYTE hash3[SHA256_BLOCK_SIZE] = {0xcd,0xc7,0x6e,0x5c,0x99,0x14,0xfb,0x92,0x81,0xa1,0xc7,0xe2,0x84,0xd7,0x3e,0x67, 0xf1,0x80,0x9a,0x48,0xa4,0x97,0x20,0x0e,0x04,0x6d,0x39,0xcc,0xc7,0x11,0x2c,0xd0}; BYTE buf[SHA256_BLOCK_SIZE]; SHA256_CTX ctx; int idx; sha256_init(&ctx); sha256_update(&ctx, text1, strlen((char *)text1)); sha256_final(&ctx, buf); ok (!memcmp(hash1, buf, SHA256_BLOCK_SIZE), "text1 OK"); sha256_init(&ctx); sha256_update(&ctx, text2, strlen((char *)text2)); sha256_final(&ctx, buf); ok (!memcmp(hash2, buf, SHA256_BLOCK_SIZE), "text2 OK"); sha256_init(&ctx); for (idx = 0; idx < 100000; ++idx) sha256_update(&ctx, text3, strlen((char *)text3)); sha256_final(&ctx, buf); ok (!memcmp(hash3, buf, SHA256_BLOCK_SIZE), "text3 OK"); }
/* * keylength in bits! * message length in bits! */ void hmac_sha256(void* dest, void* key, uint16_t keylength_b, void* msg, uint64_t msglength_b){ /* a one-shot*/ sha256_ctx_t s; uint8_t i; uint8_t buffer[SHA256_HASH_BYTES]; memset(buffer, 0, SHA256_HASH_BYTES); /* if key is larger than a block we have to hash it*/ if (keylength_b > SHA256_BLOCK_BITS){ sha256((void*)buffer, key, keylength_b); } else { memcpy(buffer, key, (keylength_b+7)/8); } for (i=0; i<SHA256_HASH_BYTES; ++i){ buffer[i] ^= IPAD; } sha256_init(&s); sha256_nextBlock(&s, buffer); while (msglength_b >= SHA256_BLOCK_BITS){ sha256_nextBlock(&s, msg); msg = (uint8_t*)msg + SHA256_HASH_BYTES; msglength_b -= SHA256_BLOCK_BITS; } sha256_lastBlock(&s, msg, msglength_b); /* since buffer still contains key xor ipad we can do ... */ for (i=0; i<SHA256_HASH_BYTES; ++i){ buffer[i] ^= IPAD ^ OPAD; } sha256_ctx2hash(dest, &s); /* save inner hash temporary to dest */ sha256_init(&s); sha256_nextBlock(&s, buffer); sha256_lastBlock(&s, dest, SHA256_HASH_BITS); sha256_ctx2hash(dest, &s); }
void protoshares_process_8(minerProtosharesBlock_t* block) { // generate mid hash using sha256 (header hash) uint8 midHash[32]; sha256_ctx c256; sha256_init(&c256); sha256_update(&c256, (unsigned char*)block, 80); sha256_final(&c256, midHash); sha256_init(&c256); sha256_update(&c256, (unsigned char*)midHash, 32); sha256_final(&c256, midHash); // init collision map if( __collisionMap == NULL ) __collisionMap = (uint32*)malloc(sizeof(uint32)*COLLISION_TABLE_SIZE); uint32* collisionIndices = __collisionMap; memset(collisionIndices, 0x00, sizeof(uint32)*COLLISION_TABLE_SIZE); // start search // uint8 midHash[64]; uint8 tempHash[32+4]; sha512_ctx c512; uint64 resultHashStorage[8*CACHED_HASHES]; memcpy(tempHash+4, midHash, 32); for(uint32 n=0; n<MAX_MOMENTUM_NONCE; n += BIRTHDAYS_PER_HASH * CACHED_HASHES) { if( block->height != monitorCurrentBlockHeight ) break; for(uint32 m=0; m<CACHED_HASHES; m++) { sha512_init(&c512); *(uint32*)tempHash = n+m*8; sha512_update_final(&c512, tempHash, 32+4, (unsigned char*)(resultHashStorage+8*m)); } for(uint32 m=0; m<CACHED_HASHES; m++) { uint64* resultHash = resultHashStorage + 8*m; uint32 i = n + m*8; for(uint32 f=0; f<8; f++) { uint64 birthday = resultHash[f] >> (64ULL-SEARCH_SPACE_BITS); uint32 collisionKey = (uint32)((birthday>>18) & COLLISION_KEY_MASK); birthday %= COLLISION_TABLE_SIZE; if( collisionIndices[birthday] ) { if( ((collisionIndices[birthday]&COLLISION_KEY_MASK) != collisionKey) || protoshares_revalidateCollision(block, midHash, collisionIndices[birthday]&~COLLISION_KEY_MASK, i+f) == false ) { // invalid collision -> ignore } } collisionIndices[birthday] = i+f | collisionKey; // we have 6 bits available for validation } } } }
/* Direct mapping from MD5 example in RFC2104 */ void hmac_sha256(struct hmac_sha256 *hmac, const void *key, size_t key_len, const void *text, size_t text_len) { struct sha256_ctx context; unsigned char k_ipad[65]; /* inner padding - * key XORd with ipad */ unsigned char k_opad[65]; /* outer padding - * key XORd with opad *//* start out by storing key in pads */ unsigned char tk[32]; int i; /* if key is longer than 64 bytes reset it to key=MD5(key) */ if (key_len > 64) { struct sha256_ctx tctx; sha256_init(&tctx); sha256_update(&tctx, key, key_len); sha256_done(&tctx, tk); key = tk; key_len = 32; } bzero( k_ipad, sizeof k_ipad); bzero( k_opad, sizeof k_opad); bcopy( key, k_ipad, key_len); bcopy( key, k_opad, key_len); /* XOR key with ipad and opad values */ for (i=0; i<64; i++) { k_ipad[i] ^= 0x36; k_opad[i] ^= 0x5c; } /* * perform inner MD5 */ sha256_init(&context); /* init context for 1st * pass */ sha256_update(&context, k_ipad, 64); /* start with inner pad */ sha256_update(&context, text, text_len); /* then text of datagram */ sha256_done(&context, &hmac->sha); /* finish up 1st pass */ /* * perform outer MD5 */ sha256_init(&context); /* init context for 2nd * pass */ sha256_update(&context, k_opad, 64); /* start with outer pad */ sha256_update(&context, &hmac->sha, 32); /* then results of 1st * hash */ sha256_done(&context, &hmac->sha); /* finish up 2nd pass */ }
void xdag_hash(void *data, size_t size, xdag_hash_t hash) { SHA256REF_CTX ctx; sha256_init(&ctx); sha256_update(&ctx, data, size); sha256_final(&ctx, (uint8_t*)hash); sha256_init(&ctx); sha256_update(&ctx, (uint8_t*)hash, sizeof(xdag_hash_t)); sha256_final(&ctx, (uint8_t*)hash); }
void sha256d_80_swap(uint32_t *hash, const uint32_t *data) { uint32_t S[16]; int i; sha256_init(S); sha256_transform(S, data, 0); sha256_transform(S, data + 16, 0); memcpy(S + 8, sha256d_hash1 + 8, 32); sha256_init(hash); sha256_transform(hash, S, 0); for (i = 0; i < 8; i++) hash[i] = swab32(hash[i]); }
void sha256_done(sha256_context *ctx, byte *Digest) { ctx->Data = ctx->Buffer; uint64 BitLength = ctx->Count * 8; uint BufPos = (uint)ctx->Count & 0x3f; ctx->Buffer[BufPos++] = 0x80; // Padding the message with "1" bit. while (BufPos != 56) // We need 56 bytes block followed by 8 byte length. { BufPos &= 0x3f; if (BufPos == 0) sha256_transform(ctx); ctx->Buffer[BufPos++] = 0; } for (uint i = 0; i < 8; i++) // Store bit length of entire message. { ctx->Buffer[BufPos++] = (byte)(BitLength >> 56); BitLength <<= 8; } sha256_transform(ctx); for (uint i = 0; i < 32; i++) Digest[i] = byte(ctx->H[i / 4] >> ((3 - i % 4) * 8)); sha256_init(ctx); sha256_transform(NULL); cleandata(ctx->Buffer, sizeof(ctx->Buffer)); }
/* * \brief Computes the master secret. * * \param ps The premaster secret. * \param client_random The random value from the client hello. * \param server_random The random value from the server hello. * \param master_secret A pointer to the final value of the master secret. * Write the end result here. */ void compute_master_secret(int ps, int client_random, int server_random, char *master_secret) { SHA256_CTX ctx; sha256_init(&ctx); unsigned char data[sizeof(int)]; unsigned char data1[sizeof(int)]; unsigned char data2[sizeof(int)]; unsigned char data3[sizeof(int)]; char master_secret0[16], master_secret1[16], master_secret2[16], master_secret3[16]; memcpy(data, &ps, sizeof(ps)); sha256_update(&ctx, data, (int) sizeof(data)); memcpy(data1, &client_random, sizeof(client_random)); sha256_update(&ctx, data1, (int) sizeof(data1)); memcpy(data2, &server_random, sizeof(server_random)); sha256_update(&ctx, data2, (int) sizeof(data2)); memcpy(data3, &ps, sizeof(ps)); sha256_update(&ctx, data3, (int) sizeof(data3)); sha256_final(&ctx, (unsigned char*) master_secret); }
void mgf1_sha256(uint8_t *seed, size_t seedLen, size_t maskLen, uint8_t *out) { SHA256_CTX base, ctrhash; sha256_init(&base); sha256_update(&base, seed, seedLen); uint8_t ctrbuf[4]; uint8_t hashbuf[32]; uint64_t ctr; for(ctr = 0; ctr < maskLen; ctr+=32) { memcpy(&ctrhash, &base, sizeof(SHA256_CTX)); encbe32(ctr / 32, ctrbuf); sha256_update(&ctrhash, ctrbuf, 4); sha256_final(&ctrhash, hashbuf); if(maskLen - ctr < 32) { memcpy(&out[ctr], hashbuf, maskLen - ctr); } else { memcpy(&out[ctr], hashbuf, 32); } } memsets(&base, 0, sizeof(SHA256_CTX)); memsets(&ctrhash, 0, sizeof(SHA256_CTX)); memsets(ctrbuf, 0, 4); memsets(hashbuf, 0, 32); }
static void _sol_message_digest_sha256_init(struct sol_message_digest *handle) { sha256_context_t *ctx = sol_message_digest_common_get_context(handle); sha256_init(ctx); }
void sha256(unsigned char* in, size_t in_size, unsigned char* out) { hash_state h; sha256_init(&h); sha256_process(&h, in, (unsigned long)in_size); sha256_done(&h, out); }
int create_passphrase_digest(const unsigned char* passphrase, unsigned char* digest) { int was_successful = 0; do{ if(!passphrase) break; SHA256_CTX ctx; size_t passphrase_len = strlen((char*)passphrase); sha256_init(&ctx); sha256_update(&ctx, passphrase, passphrase_len); sha256_final(&ctx, digest); was_successful = 1; }while(0); if(!was_successful) { } return was_successful; }
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]); }
static uint32_t ota_package_verify(struct ota *ota) { uint32_t err_code; uint32_t offset; uint8_t buffer[32]; sha256_context_t c __attribute__((aligned(4))); unsigned char md[SHA256_DIGEST_LENGTH] __attribute__((aligned(4))); size_t len; sha256_init(&c); len = sizeof(buffer); for (offset = 0; offset < ota->sig.size; offset += sizeof(buffer)) { if (offset + len >= ota->sig.size) { len = ota->sig.size - offset; } err_code = ota_read_bytes(ota_get_cache_offset(ota) * ERASE_PAGE_SIZE + OTA_HEADER_OFFSET + offset, buffer, len); if (err_code) return err_code; sha256_update(&c, buffer, len); } sha256_final(md, &c); uint8_t *buf; buf = (uint8_t *) (ota_get_cache_offset(ota) * ERASE_PAGE_SIZE + OTA_HEADER_OFFSET); ota->payload = buf + ota->header.hdr_length; return !secure_update(& (ota->sig), md); }
/* * Generate a cookie (aka SPI) * First argument is true if we're to create an Initiator cookie. * Length SHOULD be a multiple of sizeof(u_int32_t). * * As responder, we use a hashing method to get a pseudo random * value instead of using our own random pool. It will prevent * an attacker from gaining raw data from our random pool and * it will prevent an attacker from depleting our random pool * or entropy. */ void get_cookie(bool initiator, u_int8_t cookie[COOKIE_SIZE], const ip_address *addr) { do { if (initiator) { get_rnd_bytes(cookie, COOKIE_SIZE); } else { static u_int32_t counter = 0; /* STATIC */ unsigned char addr_buff[ sizeof(union { struct in_addr A; struct in6_addr B; })]; u_char buffer[SHA2_256_DIGEST_SIZE]; sha256_context ctx; size_t addr_length = addrbytesof(addr, addr_buff, sizeof(addr_buff)); sha256_init(&ctx); sha256_write(&ctx, addr_buff, addr_length); sha256_write(&ctx, secret_of_the_day, sizeof(secret_of_the_day)); counter++; sha256_write(&ctx, (const void *) &counter, sizeof(counter)); sha256_final(buffer, &ctx); /* cookie size is smaller than any hash output sizes */ memcpy(cookie, buffer, COOKIE_SIZE); } } while (is_zero_cookie(cookie)); /* probably never loops */ }
ssh_mac_ctx ssh_mac_ctx_init(enum ssh_mac_e type){ ssh_mac_ctx ctx = malloc(sizeof(struct ssh_mac_ctx_struct)); if (ctx == NULL) { return NULL; } ctx->mac_type=type; switch(type){ case SSH_MAC_SHA1: ctx->ctx.sha1_ctx = sha1_init(); return ctx; case SSH_MAC_SHA256: ctx->ctx.sha256_ctx = sha256_init(); return ctx; case SSH_MAC_SHA384: ctx->ctx.sha384_ctx = sha384_init(); return ctx; case SSH_MAC_SHA512: ctx->ctx.sha512_ctx = sha512_init(); return ctx; default: SAFE_FREE(ctx); return NULL; } }
SHA256_DIGEST sha256(const void *message, size_t message_len) { SHA256_CTX ctxt; sha256_init(&ctxt); sha256_update(&ctxt, message, message_len); return sha256_finish(&ctxt); }
/* Only does SIGHASH_ALL */ static void sha256_tx_one_input(struct bitcoin_tx *tx, size_t input_num, const u8 *script, size_t script_len, struct sha256_double *hash) { struct sha256_ctx ctx = SHA256_INIT; size_t i; assert(input_num < tx->input_count); /* You must have all inputs zeroed to start. */ for (i = 0; i < tx->input_count; i++) assert(tx->input[i].script_length == 0); tx->input[input_num].script_length = script_len; tx->input[input_num].script = cast_const(u8 *, script); sha256_init(&ctx); sha256_tx_for_sig(&ctx, tx, input_num); sha256_le32(&ctx, SIGHASH_ALL); sha256_double_done(&ctx, hash); /* Reset it for next time. */ tx->input[input_num].script_length = 0; tx->input[input_num].script = NULL; }
static void yarrow_iterate(uint8_t *digest) { uint8_t v0[SHA256_DIGEST_SIZE]; unsigned i; memcpy(v0, digest, SHA256_DIGEST_SIZE); /* When hashed inside the loop, i should run from 1 to * YARROW_RESEED_ITERATIONS */ for (i = 0; ++i < YARROW_RESEED_ITERATIONS; ) { uint8_t count[4]; struct sha256_ctx hash; sha256_init(&hash); /* Hash v_i | v_0 | i */ WRITE_UINT32(count, i); sha256_update(&hash, SHA256_DIGEST_SIZE, digest); sha256_update(&hash, sizeof(v0), v0); sha256_update(&hash, sizeof(count), count); sha256_digest(&hash, SHA256_DIGEST_SIZE, digest); } }
String FileAccess::get_sha256(const String &p_file) { FileAccess *f = FileAccess::open(p_file, READ); if (!f) return String(); sha256_context sha256; sha256_init(&sha256); unsigned char step[32768]; while (true) { int br = f->get_buffer(step, 32768); if (br > 0) { sha256_hash(&sha256, step, br); } if (br < 4096) break; } unsigned char hash[32]; sha256_done(&sha256, hash); memdelete(f); return String::hex_encode_buffer(hash, 32); }
PRIVATE void sha256_final(sha256_t *p, uint8_t *digest) { uint64_t len = (p->count << 3); uint32_t pos = (uint32_t)p->count & 0x3F; unsigned i; p->buffer[pos++] = 0x80; while (pos != (64 - 8)) { pos &= 0x3F; if (pos == 0) sha256_write_byte_block(p); p->buffer[pos++] = 0; } for (i = 0; i < 8; i++) { p->buffer[pos++] = (uint8_t)(len >> 56); len <<= 8; } sha256_write_byte_block(p); for (i = 0; i < 8; i++) { *digest++ = (uint8_t)(p->state[i] >> 24); *digest++ = (uint8_t)(p->state[i] >> 16); *digest++ = (uint8_t)(p->state[i] >> 8); *digest++ = (uint8_t)(p->state[i]); } sha256_init(p); }
void hmac_sha256_final(hmac_sha256_ctx_t *s, void* key, uint16_t keylength_b){ uint8_t buffer[SHA256_HASH_BYTES]; uint8_t i; sha256_ctx_t a; memset(buffer, 0, SHA256_HASH_BYTES); if (keylength_b > SHA256_BLOCK_BITS){ sha256((void*)buffer, key, keylength_b); } else { memcpy(buffer, key, (keylength_b+7)/8); } for (i=0; i<SHA256_HASH_BYTES; ++i){ buffer[i] ^= OPAD; } sha256_init(&a); sha256_nextBlock(&a, buffer); /* hash key ^ opad */ sha256_ctx2hash((void*)buffer, s); /* copy hash(key ^ ipad, msg) to buffer */ sha256_lastBlock(&a, buffer, SHA256_HASH_BITS); memcpy(s, &a, sizeof(sha256_ctx_t)); #if defined SECURE_WIPE_BUFFER memset(buffer, 0, SHA256_HASH_BYTES); memset(a.h, 0, 8*4); #endif }
void do_gpu_sha(){ int i; uint32_t digest[8*STRIDE]; uint32_t block[20*STRIDE] = {0}; block[(72/4)*STRIDE] = 0x9FEF014; block[(76/4)*STRIDE] = 0x2a400100; sha256cl_context ctx; sha256_init(0,&ctx); sha256_starts(0,&ctx); sha256_update(0,&ctx,block,80); sha256_finish(0,&ctx,digest); for(i=0; i < 8; i++) printf("%8.8X",D_REF(digest,7-i,0)); printf("\n"); sha256_starts(0,&ctx); sha256_update(0,&ctx,digest,32); sha256_finish(0,&ctx,digest); for(i=0; i < 8; i++) printf("%8.8X",D_REF(digest,7-i,0)); printf("\n"); }