예제 #1
0
파일: hmac.c 프로젝트: aido/picocoin
void hmac_sha512(const void *key_p, const uint32_t keylen, const void *msg_p, const uint32_t msglen, uint8_t *hmac)
{
	const uint8_t *key = key_p;
	const uint8_t *msg = msg_p;
	int i;
	uint8_t buf[SHA512_BLOCK_LENGTH], o_key_pad[SHA512_BLOCK_LENGTH], i_key_pad[SHA512_BLOCK_LENGTH];
	SHA512_CTX ctx;

	memset(buf, 0, SHA512_BLOCK_LENGTH);
	if (keylen > SHA512_BLOCK_LENGTH) {
		sha512_Raw(key, keylen, buf);
	} else {
		memcpy(buf, key, keylen);
	}

	for (i = 0; i < SHA512_BLOCK_LENGTH; i++) {
		o_key_pad[i] = buf[i] ^ 0x5c;
		i_key_pad[i] = buf[i] ^ 0x36;
	}

	sha512_Init(&ctx);
	sha512_Update(&ctx, i_key_pad, SHA512_BLOCK_LENGTH);
	sha512_Update(&ctx, msg, msglen);
	sha512_Final(buf, &ctx);

	sha512_Init(&ctx);
	sha512_Update(&ctx, o_key_pad, SHA512_BLOCK_LENGTH);
	sha512_Update(&ctx, buf, SHA512_DIGEST_LENGTH);
	sha512_Final(hmac, &ctx);

	MEMSET_BZERO(buf, sizeof(buf));
	MEMSET_BZERO(o_key_pad, sizeof(o_key_pad));
	MEMSET_BZERO(i_key_pad, sizeof(i_key_pad));
}
예제 #2
0
void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
	sha2_word32	*d = (sha2_word32*)digest;
	unsigned int	usedspace;

	/* Sanity check: */
	assert(context != (SHA256_CTX*)0);

	/* If no digest buffer is passed, we don't bother doing this: */
	if (digest != (sha2_byte*)0) {
		usedspace = (unsigned int)((context->bitcount >> 3) % SHA256_BLOCK_LENGTH);
#if SIRIKATA_BYTE_ORDER == SIRIKATA_LITTLE_ENDIAN
		/* Convert FROM host byte order */
		REVERSE64(context->bitcount,context->bitcount);
#endif
		if (usedspace > 0) {
			/* Begin padding with a 1 bit: */
			context->buffer[usedspace++] = 0x80;

			if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
				/* Set-up for the last transform: */
				MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
			} else {
				if (usedspace < SHA256_BLOCK_LENGTH) {
					MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
				}
				/* Do second-to-last transform: */
				SHA256_Transform(context, (sha2_word32*)context->buffer);

				/* And set-up for the last transform: */
				MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
			}
		} else {
			/* Set-up for the last transform: */
			MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);

			/* Begin padding with a 1 bit: */
			*context->buffer = 0x80;
		}
		/* Set the bit count: */
		*(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;

		/* Final transform: */
		SHA256_Transform(context, (sha2_word32*)context->buffer);

#if SIRIKATA_BYTE_ORDER == SIRIKATA_LITTLE_ENDIAN
		{
			/* Convert TO host byte order */
			int	j;
			for (j = 0; j < 8; j++) {
				REVERSE32(context->state[j],context->state[j]);
				*d++ = context->state[j];
			}
		}
#else
		MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH);
#endif
	}
예제 #3
0
void unabto_sha256_init(sha256_ctx* context) {
#if UNABTO_PLATFORM_PIC18
  memcpypgm2ram(context->state, sha256_initial_hash_value, sizeof(sha256_initial_hash_value));
#else
    memcpy(context->state, sha256_initial_hash_value, sizeof(sha256_initial_hash_value));
#endif
	MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);

	context->byteCount = 0;
}
예제 #4
0
파일: base58.c 프로젝트: axic/trezor-crypto
int base58_encode_check(const uint8_t *data, int datalen, char *str, int strsize)
{
	if (datalen > 128) {
		return 0;
	}
	uint8_t buf[datalen + 32];
	uint8_t *hash = buf + datalen;
	memcpy(buf, data, datalen);
	sha256_Raw(data, datalen, hash);
	sha256_Raw(hash, 32, hash);
	size_t res = strsize;
	bool success = b58enc(str, &res, buf, datalen + 4);
	MEMSET_BZERO(buf, sizeof(buf));
	return success ? res : 0;
}
예제 #5
0
void unabto_sha256_final(sha256_ctx* context,sha2_byte digest[]) {
	sha2_word32	*d = (sha2_word32*)digest;
	unsigned int	usedspace;

	/* Sanity check: */
//	assert(context != (sha256_ctx*)0);

	/* If no digest buffer is passed, we don't bother doing this: */
	if (digest != (sha2_byte*)0) {
		usedspace = context->byteCount % SHA256_BLOCK_LENGTH;

		if (usedspace > 0) {
			/* Begin padding with a 1 bit: */
			context->buffer[usedspace++] = 0x80;

			if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
				/* Set-up for the last transform: */
				MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
			} else {
				if (usedspace < SHA256_BLOCK_LENGTH) {
					MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
				}
				/* Do second-to-last transform: */
				SHA256_Transform(context, (sha2_word32*)context->buffer);

				/* And set-up for the last transform: */
				MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
			}
		} else {
			/* Set-up for the last transform: */
			MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);

			/* Begin padding with a 1 bit: */
			*context->buffer = 0x80;
		}

		/* Set the bit count: */
		/* Convert FROM host byte order */
    {
      uint16_t tmp16;
      uint16_t tmp16_ = context->byteCount;
      tmp16_ <<= 3;
      READ_U16(tmp16, &tmp16_);
      memset(&context->buffer[SHA256_BLOCK_LENGTH - 8], 0, 6);
      *(uint16_t*)&context->buffer[SHA256_BLOCK_LENGTH - 2] = tmp16;
    }
        //print_sha256_ctx(context);
		/* Final transform: */
		SHA256_Transform(context, (sha2_word32*)context->buffer);
        //print_sha256_ctx(context);

		{
			/* Convert TO host byte order */
			uint8_t	j;
			for (j = 0; j < 8; j++) {
        WRITE_U32(d, context->state[j]);
        d++;
			}
		}
	}

	/* Clean up state data: */
	MEMSET_BZERO(context, sizeof(sha256_ctx));
	usedspace = 0;
}