sha2.c

来自「非常好的dns解析软件」· C语言 代码 · 共 1,235 行 · 第 1/3 页

C
1,235
字号
	memset(context->buffer, 0, ISC_SHA256_BLOCK_LENGTH);	context->bitcount = 0;}void isc_sha224_update(isc_sha224_t *context, const isc_uint8_t* data, size_t len) {	isc_sha256_update((isc_sha256_t *)context, data, len);}void isc_sha224_final(isc_uint8_t digest[], isc_sha224_t *context) {	isc_uint8_t sha256_digest[ISC_SHA256_DIGESTLENGTH];	isc_sha256_final(sha256_digest, (isc_sha256_t *)context);	memcpy(digest, sha256_digest, ISC_SHA224_DIGESTLENGTH);	memset(sha256_digest, 0, ISC_SHA256_DIGESTLENGTH);}char *isc_sha224_end(isc_sha224_t *context, char buffer[]) {	isc_uint8_t	digest[ISC_SHA224_DIGESTLENGTH], *d = digest;	unsigned int	i;	/* Sanity check: */	REQUIRE(context != (isc_sha224_t *)0);	if (buffer != (char*)0) {		isc_sha224_final(digest, context);		for (i = 0; i < ISC_SHA224_DIGESTLENGTH; i++) {			*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];			*buffer++ = sha2_hex_digits[*d & 0x0f];			d++;		}		*buffer = (char)0;	} else {		memset(context, 0, sizeof(context));	}	memset(digest, 0, ISC_SHA224_DIGESTLENGTH);	return buffer;}char*isc_sha224_data(const isc_uint8_t *data, size_t len,	        char digest[ISC_SHA224_DIGESTSTRINGLENGTH]){	isc_sha224_t context;	isc_sha224_init(&context);	isc_sha224_update(&context, data, len);	return (isc_sha224_end(&context, digest));}/*** SHA-256: *********************************************************/voidisc_sha256_init(isc_sha256_t *context) {	if (context == (isc_sha256_t *)0) {		return;	}	memcpy(context->state, sha256_initial_hash_value,	       ISC_SHA256_DIGESTLENGTH);	memset(context->buffer, 0, ISC_SHA256_BLOCK_LENGTH);	context->bitcount = 0;}#ifdef ISC_SHA2_UNROLL_TRANSFORM/* Unrolled SHA-256 round macros: */#if BYTE_ORDER == LITTLE_ENDIAN#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)	\	REVERSE32(*data++, W256[j]); \	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \             K256[j] + W256[j]; \	(d) += T1; \	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \	j++#else /* BYTE_ORDER == LITTLE_ENDIAN */#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)	\	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \	     K256[j] + (W256[j] = *data++); \	(d) += T1; \	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \	j++#endif /* BYTE_ORDER == LITTLE_ENDIAN */#define ROUND256(a,b,c,d,e,f,g,h)	\	s0 = W256[(j+1)&0x0f]; \	s0 = sigma0_256(s0); \	s1 = W256[(j+14)&0x0f]; \	s1 = sigma1_256(s1); \	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \	     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \	(d) += T1; \	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \	j++void isc_sha256_transform(isc_sha256_t *context, const isc_uint32_t* data) {	isc_uint32_t	a, b, c, d, e, f, g, h, s0, s1;	isc_uint32_t	T1, *W256;	int		j;	W256 = (isc_uint32_t*)context->buffer;	/* Initialize registers with the prev. intermediate value */	a = context->state[0];	b = context->state[1];	c = context->state[2];	d = context->state[3];	e = context->state[4];	f = context->state[5];	g = context->state[6];	h = context->state[7];	j = 0;	do {		/* Rounds 0 to 15 (unrolled): */		ROUND256_0_TO_15(a,b,c,d,e,f,g,h);		ROUND256_0_TO_15(h,a,b,c,d,e,f,g);		ROUND256_0_TO_15(g,h,a,b,c,d,e,f);		ROUND256_0_TO_15(f,g,h,a,b,c,d,e);		ROUND256_0_TO_15(e,f,g,h,a,b,c,d);		ROUND256_0_TO_15(d,e,f,g,h,a,b,c);		ROUND256_0_TO_15(c,d,e,f,g,h,a,b);		ROUND256_0_TO_15(b,c,d,e,f,g,h,a);	} while (j < 16);	/* Now for the remaining rounds to 64: */	do {		ROUND256(a,b,c,d,e,f,g,h);		ROUND256(h,a,b,c,d,e,f,g);		ROUND256(g,h,a,b,c,d,e,f);		ROUND256(f,g,h,a,b,c,d,e);		ROUND256(e,f,g,h,a,b,c,d);		ROUND256(d,e,f,g,h,a,b,c);		ROUND256(c,d,e,f,g,h,a,b);		ROUND256(b,c,d,e,f,g,h,a);	} while (j < 64);	/* Compute the current intermediate hash value */	context->state[0] += a;	context->state[1] += b;	context->state[2] += c;	context->state[3] += d;	context->state[4] += e;	context->state[5] += f;	context->state[6] += g;	context->state[7] += h;	/* Clean up */	a = b = c = d = e = f = g = h = T1 = 0;}#else /* ISC_SHA2_UNROLL_TRANSFORM */voidisc_sha256_transform(isc_sha256_t *context, const isc_uint32_t* data) {	isc_uint32_t	a, b, c, d, e, f, g, h, s0, s1;	isc_uint32_t	T1, T2, *W256;	int		j;	W256 = (isc_uint32_t*)context->buffer;	/* Initialize registers with the prev. intermediate value */	a = context->state[0];	b = context->state[1];	c = context->state[2];	d = context->state[3];	e = context->state[4];	f = context->state[5];	g = context->state[6];	h = context->state[7];	j = 0;	do {#if BYTE_ORDER == LITTLE_ENDIAN		/* Copy data while converting to host byte order */		REVERSE32(*data++,W256[j]);		/* Apply the SHA-256 compression function to update a..h */		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];#else /* BYTE_ORDER == LITTLE_ENDIAN */		/* Apply the SHA-256 compression function to update a..h with copy */		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);#endif /* BYTE_ORDER == LITTLE_ENDIAN */		T2 = Sigma0_256(a) + Maj(a, b, c);		h = g;		g = f;		f = e;		e = d + T1;		d = c;		c = b;		b = a;		a = T1 + T2;		j++;	} while (j < 16);	do {		/* Part of the message block expansion: */		s0 = W256[(j+1)&0x0f];		s0 = sigma0_256(s0);		s1 = W256[(j+14)&0x0f];			s1 = sigma1_256(s1);		/* Apply the SHA-256 compression function to update a..h */		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + 		     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);		T2 = Sigma0_256(a) + Maj(a, b, c);		h = g;		g = f;		f = e;		e = d + T1;		d = c;		c = b;		b = a;		a = T1 + T2;		j++;	} while (j < 64);	/* Compute the current intermediate hash value */	context->state[0] += a;	context->state[1] += b;	context->state[2] += c;	context->state[3] += d;	context->state[4] += e;	context->state[5] += f;	context->state[6] += g;	context->state[7] += h;	/* Clean up */	a = b = c = d = e = f = g = h = T1 = T2 = 0;}#endif /* ISC_SHA2_UNROLL_TRANSFORM */voidisc_sha256_update(isc_sha256_t *context, const isc_uint8_t *data, size_t len) {	unsigned int	freespace, usedspace;	if (len == 0U) {		/* Calling with no data is valid - we do nothing */		return;	}	/* Sanity check: */	REQUIRE(context != (isc_sha256_t *)0 && data != (isc_uint8_t*)0);	usedspace = (unsigned int)((context->bitcount >> 3) %				   ISC_SHA256_BLOCK_LENGTH);	if (usedspace > 0) {		/* Calculate how much free space is available in the buffer */		freespace = ISC_SHA256_BLOCK_LENGTH - usedspace;		if (len >= freespace) {			/* Fill the buffer completely and process it */			memcpy(&context->buffer[usedspace], data, freespace);			context->bitcount += freespace << 3;			len -= freespace;			data += freespace;			isc_sha256_transform(context,					     (isc_uint32_t*)context->buffer);		} else {			/* The buffer is not yet full */			memcpy(&context->buffer[usedspace], data, len);			context->bitcount += len << 3;			/* Clean up: */			usedspace = freespace = 0;			return;		}	}	while (len >= ISC_SHA256_BLOCK_LENGTH) {		/* Process as many complete blocks as we can */		memcpy(context->buffer, data, ISC_SHA256_BLOCK_LENGTH);		isc_sha256_transform(context, (isc_uint32_t*)context->buffer);		context->bitcount += ISC_SHA256_BLOCK_LENGTH << 3;		len -= ISC_SHA256_BLOCK_LENGTH;		data += ISC_SHA256_BLOCK_LENGTH;	}	if (len > 0U) {		/* There's left-overs, so save 'em */		memcpy(context->buffer, data, len);		context->bitcount += len << 3;	}	/* Clean up: */	usedspace = freespace = 0;}voidisc_sha256_final(isc_uint8_t digest[], isc_sha256_t *context) {	isc_uint32_t	*d = (isc_uint32_t*)digest;	unsigned int	usedspace;	/* Sanity check: */	REQUIRE(context != (isc_sha256_t *)0);	/* If no digest buffer is passed, we don't bother doing this: */	if (digest != (isc_uint8_t*)0) {		usedspace = (unsigned int)((context->bitcount >> 3) %					   ISC_SHA256_BLOCK_LENGTH);#if BYTE_ORDER == 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 <= ISC_SHA256_SHORT_BLOCK_LENGTH) {				/* Set-up for the last transform: */				memset(&context->buffer[usedspace], 0,				       ISC_SHA256_SHORT_BLOCK_LENGTH - usedspace);			} else {				if (usedspace < ISC_SHA256_BLOCK_LENGTH) {					memset(&context->buffer[usedspace], 0,					       ISC_SHA256_BLOCK_LENGTH -					       usedspace);				}				/* Do second-to-last transform: */				isc_sha256_transform(context,					       (isc_uint32_t*)context->buffer);				/* And set-up for the last transform: */				memset(context->buffer, 0,				       ISC_SHA256_SHORT_BLOCK_LENGTH);			}		} else {			/* Set-up for the last transform: */			memset(context->buffer, 0, ISC_SHA256_SHORT_BLOCK_LENGTH);			/* Begin padding with a 1 bit: */			*context->buffer = 0x80;		}		/* Set the bit count: */		*(isc_uint64_t*)&context->buffer[ISC_SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;		/* Final transform: */		isc_sha256_transform(context, (isc_uint32_t*)context->buffer);#if BYTE_ORDER == 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(d, context->state, ISC_SHA256_DIGESTLENGTH);#endif	}	/* Clean up state data: */	memset(context, 0, sizeof(context));	usedspace = 0;}char *isc_sha256_end(isc_sha256_t *context, char buffer[]) {	isc_uint8_t	digest[ISC_SHA256_DIGESTLENGTH], *d = digest;	unsigned int	i;	/* Sanity check: */	REQUIRE(context != (isc_sha256_t *)0);	if (buffer != (char*)0) {		isc_sha256_final(digest, context);		for (i = 0; i < ISC_SHA256_DIGESTLENGTH; i++) {			*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];			*buffer++ = sha2_hex_digits[*d & 0x0f];			d++;		}		*buffer = (char)0;	} else {		memset(context, 0, sizeof(context));	}	memset(digest, 0, ISC_SHA256_DIGESTLENGTH);	return buffer;}char *isc_sha256_data(const isc_uint8_t* data, size_t len,		char digest[ISC_SHA256_DIGESTSTRINGLENGTH]){	isc_sha256_t context;	isc_sha256_init(&context);	isc_sha256_update(&context, data, len);	return (isc_sha256_end(&context, digest));}/*** SHA-512: *********************************************************/voidisc_sha512_init(isc_sha512_t *context) {	if (context == (isc_sha512_t *)0) {		return;	}	memcpy(context->state, sha512_initial_hash_value,	       ISC_SHA512_DIGESTLENGTH);	memset(context->buffer, 0, ISC_SHA512_BLOCK_LENGTH);	context->bitcount[0] = context->bitcount[1] =  0;}#ifdef ISC_SHA2_UNROLL_TRANSFORM

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?