hmac_link.c

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

C
1,669
字号
		}	}	dst__privstruct_free(&priv, mctx);	memset(&priv, 0, sizeof(priv));	return (result);}static dst_func_t hmacsha1_functions = {	hmacsha1_createctx,	hmacsha1_destroyctx,	hmacsha1_adddata,	hmacsha1_sign,	hmacsha1_verify,	NULL, /* computesecret */	hmacsha1_compare,	NULL, /* paramcompare */	hmacsha1_generate,	hmacsha1_isprivate,	hmacsha1_destroy,	hmacsha1_todns,	hmacsha1_fromdns,	hmacsha1_tofile,	hmacsha1_parse,	NULL, /* cleanup */};isc_result_tdst__hmacsha1_init(dst_func_t **funcp) {	REQUIRE(funcp != NULL);	if (*funcp == NULL)		*funcp = &hmacsha1_functions;	return (ISC_R_SUCCESS);}static isc_result_t hmacsha224_fromdns(dst_key_t *key, isc_buffer_t *data);typedef struct {	unsigned char key[ISC_SHA224_DIGESTLENGTH];} HMACSHA224_Key;static isc_result_thmacsha224_createctx(dst_key_t *key, dst_context_t *dctx) {	isc_hmacsha224_t *hmacsha224ctx;	HMACSHA224_Key *hkey = key->opaque;	hmacsha224ctx = isc_mem_get(dctx->mctx, sizeof(isc_hmacsha224_t));	if (hmacsha224ctx == NULL)		return (ISC_R_NOMEMORY);	isc_hmacsha224_init(hmacsha224ctx, hkey->key, ISC_SHA224_DIGESTLENGTH);	dctx->opaque = hmacsha224ctx;	return (ISC_R_SUCCESS);}static voidhmacsha224_destroyctx(dst_context_t *dctx) {	isc_hmacsha224_t *hmacsha224ctx = dctx->opaque;	if (hmacsha224ctx != NULL) {		isc_hmacsha224_invalidate(hmacsha224ctx);		isc_mem_put(dctx->mctx, hmacsha224ctx, sizeof(isc_hmacsha224_t));		dctx->opaque = NULL;	}}static isc_result_thmacsha224_adddata(dst_context_t *dctx, const isc_region_t *data) {	isc_hmacsha224_t *hmacsha224ctx = dctx->opaque;	isc_hmacsha224_update(hmacsha224ctx, data->base, data->length);	return (ISC_R_SUCCESS);}static isc_result_thmacsha224_sign(dst_context_t *dctx, isc_buffer_t *sig) {	isc_hmacsha224_t *hmacsha224ctx = dctx->opaque;	unsigned char *digest;	if (isc_buffer_availablelength(sig) < ISC_SHA224_DIGESTLENGTH)		return (ISC_R_NOSPACE);	digest = isc_buffer_used(sig);	isc_hmacsha224_sign(hmacsha224ctx, digest, ISC_SHA224_DIGESTLENGTH);	isc_buffer_add(sig, ISC_SHA224_DIGESTLENGTH);	return (ISC_R_SUCCESS);}static isc_result_thmacsha224_verify(dst_context_t *dctx, const isc_region_t *sig) {	isc_hmacsha224_t *hmacsha224ctx = dctx->opaque;	if (sig->length > ISC_SHA224_DIGESTLENGTH || sig->length == 0)		return (DST_R_VERIFYFAILURE);	if (isc_hmacsha224_verify(hmacsha224ctx, sig->base, sig->length))		return (ISC_R_SUCCESS);	else		return (DST_R_VERIFYFAILURE);}static isc_boolean_thmacsha224_compare(const dst_key_t *key1, const dst_key_t *key2) {	HMACSHA224_Key *hkey1, *hkey2;	hkey1 = (HMACSHA224_Key *)key1->opaque;	hkey2 = (HMACSHA224_Key *)key2->opaque;	if (hkey1 == NULL && hkey2 == NULL)		return (ISC_TRUE);	else if (hkey1 == NULL || hkey2 == NULL)		return (ISC_FALSE);	if (memcmp(hkey1->key, hkey2->key, ISC_SHA224_DIGESTLENGTH) == 0)		return (ISC_TRUE);	else		return (ISC_FALSE);}static isc_result_thmacsha224_generate(dst_key_t *key, int pseudorandom_ok) {	isc_buffer_t b;	isc_result_t ret;	int bytes;	unsigned char data[HMAC_LEN];	bytes = (key->key_size + 7) / 8;	if (bytes > HMAC_LEN) {		bytes = HMAC_LEN;		key->key_size = HMAC_LEN * 8;	}	memset(data, 0, HMAC_LEN);	ret = dst__entropy_getdata(data, bytes, ISC_TF(pseudorandom_ok != 0));	if (ret != ISC_R_SUCCESS)		return (ret);	isc_buffer_init(&b, data, bytes);	isc_buffer_add(&b, bytes);	ret = hmacsha224_fromdns(key, &b);	memset(data, 0, ISC_SHA224_DIGESTLENGTH);	return (ret);}static isc_boolean_thmacsha224_isprivate(const dst_key_t *key) {	UNUSED(key);	return (ISC_TRUE);}static voidhmacsha224_destroy(dst_key_t *key) {	HMACSHA224_Key *hkey = key->opaque;	memset(hkey, 0, sizeof(HMACSHA224_Key));	isc_mem_put(key->mctx, hkey, sizeof(HMACSHA224_Key));	key->opaque = NULL;}static isc_result_thmacsha224_todns(const dst_key_t *key, isc_buffer_t *data) {	HMACSHA224_Key *hkey;	unsigned int bytes;	REQUIRE(key->opaque != NULL);	hkey = (HMACSHA224_Key *) key->opaque;	bytes = (key->key_size + 7) / 8;	if (isc_buffer_availablelength(data) < bytes)		return (ISC_R_NOSPACE);	isc_buffer_putmem(data, hkey->key, bytes);	return (ISC_R_SUCCESS);}static isc_result_thmacsha224_fromdns(dst_key_t *key, isc_buffer_t *data) {	HMACSHA224_Key *hkey;	int keylen;	isc_region_t r;	isc_sha224_t sha224ctx;	isc_buffer_remainingregion(data, &r);	if (r.length == 0)		return (ISC_R_SUCCESS);	hkey = (HMACSHA224_Key *) isc_mem_get(key->mctx, sizeof(HMACSHA224_Key));	if (hkey == NULL)		return (ISC_R_NOMEMORY);	memset(hkey->key, 0, sizeof(hkey->key));	if (r.length > ISC_SHA224_DIGESTLENGTH) {		isc_sha224_init(&sha224ctx);		isc_sha224_update(&sha224ctx, r.base, r.length);		isc_sha224_final(hkey->key, &sha224ctx);		keylen = ISC_SHA224_DIGESTLENGTH;	}	else {		memcpy(hkey->key, r.base, r.length);		keylen = r.length;	}	key->key_size = keylen * 8;	key->opaque = hkey;	return (ISC_R_SUCCESS);}static isc_result_thmacsha224_tofile(const dst_key_t *key, const char *directory) {	int cnt = 0;	HMACSHA224_Key *hkey;	dst_private_t priv;	int bytes = (key->key_size + 7) / 8;	unsigned char buf[2];	if (key->opaque == NULL)		return (DST_R_NULLKEY);	hkey = (HMACSHA224_Key *) key->opaque;	priv.elements[cnt].tag = TAG_HMACSHA224_KEY;	priv.elements[cnt].length = bytes;	priv.elements[cnt++].data = hkey->key;	buf[0] = (key->key_bits >> 8) & 0xffU;	buf[1] = key->key_bits & 0xffU;	priv.elements[cnt].tag = TAG_HMACSHA224_BITS;	priv.elements[cnt].data = buf;	priv.elements[cnt++].length = 2;	priv.nelements = cnt;	return (dst__privstruct_writefile(key, &priv, directory));}static isc_result_thmacsha224_parse(dst_key_t *key, isc_lex_t *lexer) {	dst_private_t priv;	isc_result_t result, tresult;	isc_buffer_t b;	isc_mem_t *mctx = key->mctx;	unsigned int i;	/* read private key file */	result = dst__privstruct_parse(key, DST_ALG_HMACSHA224, lexer, mctx,				       &priv);	if (result != ISC_R_SUCCESS)		return (result);	key->key_bits = 0;	for (i = 0; i < priv.nelements; i++) {		switch (priv.elements[i].tag) {		case TAG_HMACSHA224_KEY:			isc_buffer_init(&b, priv.elements[i].data,				        priv.elements[i].length);			isc_buffer_add(&b, priv.elements[i].length);			tresult = hmacsha224_fromdns(key, &b);			if (tresult != ISC_R_SUCCESS)				result = tresult;			break;		case TAG_HMACSHA224_BITS:			tresult = getkeybits(key, &priv.elements[i]);			if (tresult != ISC_R_SUCCESS)				result = tresult;			break;		default:			result = DST_R_INVALIDPRIVATEKEY;			break;		}	}	dst__privstruct_free(&priv, mctx);	memset(&priv, 0, sizeof(priv));	return (result);}static dst_func_t hmacsha224_functions = {	hmacsha224_createctx,	hmacsha224_destroyctx,	hmacsha224_adddata,	hmacsha224_sign,	hmacsha224_verify,	NULL, /* computesecret */	hmacsha224_compare,	NULL, /* paramcompare */	hmacsha224_generate,	hmacsha224_isprivate,	hmacsha224_destroy,	hmacsha224_todns,	hmacsha224_fromdns,	hmacsha224_tofile,	hmacsha224_parse,	NULL, /* cleanup */};isc_result_tdst__hmacsha224_init(dst_func_t **funcp) {	REQUIRE(funcp != NULL);	if (*funcp == NULL)		*funcp = &hmacsha224_functions;	return (ISC_R_SUCCESS);}static isc_result_t hmacsha256_fromdns(dst_key_t *key, isc_buffer_t *data);typedef struct {	unsigned char key[ISC_SHA256_DIGESTLENGTH];} HMACSHA256_Key;static isc_result_thmacsha256_createctx(dst_key_t *key, dst_context_t *dctx) {	isc_hmacsha256_t *hmacsha256ctx;	HMACSHA256_Key *hkey = key->opaque;	hmacsha256ctx = isc_mem_get(dctx->mctx, sizeof(isc_hmacsha256_t));	if (hmacsha256ctx == NULL)		return (ISC_R_NOMEMORY);	isc_hmacsha256_init(hmacsha256ctx, hkey->key, ISC_SHA256_DIGESTLENGTH);	dctx->opaque = hmacsha256ctx;	return (ISC_R_SUCCESS);}static voidhmacsha256_destroyctx(dst_context_t *dctx) {	isc_hmacsha256_t *hmacsha256ctx = dctx->opaque;	if (hmacsha256ctx != NULL) {		isc_hmacsha256_invalidate(hmacsha256ctx);		isc_mem_put(dctx->mctx, hmacsha256ctx, sizeof(isc_hmacsha256_t));		dctx->opaque = NULL;	}}static isc_result_thmacsha256_adddata(dst_context_t *dctx, const isc_region_t *data) {	isc_hmacsha256_t *hmacsha256ctx = dctx->opaque;	isc_hmacsha256_update(hmacsha256ctx, data->base, data->length);	return (ISC_R_SUCCESS);}static isc_result_thmacsha256_sign(dst_context_t *dctx, isc_buffer_t *sig) {	isc_hmacsha256_t *hmacsha256ctx = dctx->opaque;	unsigned char *digest;	if (isc_buffer_availablelength(sig) < ISC_SHA256_DIGESTLENGTH)		return (ISC_R_NOSPACE);	digest = isc_buffer_used(sig);	isc_hmacsha256_sign(hmacsha256ctx, digest, ISC_SHA256_DIGESTLENGTH);	isc_buffer_add(sig, ISC_SHA256_DIGESTLENGTH);	return (ISC_R_SUCCESS);}static isc_result_thmacsha256_verify(dst_context_t *dctx, const isc_region_t *sig) {	isc_hmacsha256_t *hmacsha256ctx = dctx->opaque;	if (sig->length > ISC_SHA256_DIGESTLENGTH || sig->length == 0)		return (DST_R_VERIFYFAILURE);	if (isc_hmacsha256_verify(hmacsha256ctx, sig->base, sig->length))		return (ISC_R_SUCCESS);	else		return (DST_R_VERIFYFAILURE);}static isc_boolean_thmacsha256_compare(const dst_key_t *key1, const dst_key_t *key2) {	HMACSHA256_Key *hkey1, *hkey2;	hkey1 = (HMACSHA256_Key *)key1->opaque;	hkey2 = (HMACSHA256_Key *)key2->opaque;	if (hkey1 == NULL && hkey2 == NULL)		return (ISC_TRUE);	else if (hkey1 == NULL || hkey2 == NULL)		return (ISC_FALSE);	if (memcmp(hkey1->key, hkey2->key, ISC_SHA256_DIGESTLENGTH) == 0)		return (ISC_TRUE);	else		return (ISC_FALSE);}static isc_result_thmacsha256_generate(dst_key_t *key, int pseudorandom_ok) {	isc_buffer_t b;	isc_result_t ret;	int bytes;	unsigned char data[HMAC_LEN];	bytes = (key->key_size + 7) / 8;	if (bytes > HMAC_LEN) {		bytes = HMAC_LEN;		key->key_size = HMAC_LEN * 8;	}	memset(data, 0, HMAC_LEN);	ret = dst__entropy_getdata(data, bytes, ISC_TF(pseudorandom_ok != 0));	if (ret != ISC_R_SUCCESS)		return (ret);	isc_buffer_init(&b, data, bytes);	isc_buffer_add(&b, bytes);	ret = hmacsha256_fromdns(key, &b);	memset(data, 0, ISC_SHA256_DIGESTLENGTH);	return (ret);}static isc_boolean_thmacsha256_isprivate(const dst_key_t *key) {	UNUSED(key);	return (ISC_TRUE);}static voidhmacsha256_destroy(dst_key_t *key) {	HMACSHA256_Key *hkey = key->opaque;	memset(hkey, 0, sizeof(HMACSHA256_Key));	isc_mem_put(key->mctx, hkey, sizeof(HMACSHA256_Key));	key->opaque = NULL;}static isc_result_thmacsha256_todns(const dst_key_t *key, isc_buffer_t *data) {	HMACSHA256_Key *hkey;	unsigned int bytes;	REQUIRE(key->opaque != NULL);	hkey = (HMACSHA256_Key *) key->opaque;	bytes = (key->key_size + 7) / 8;	if (isc_buffer_availablelength(data) < bytes)		return (ISC_R_NOSPACE);	isc_buffer_putmem(data, hkey->key, bytes);	return (ISC_R_SUCCESS);}static isc_result_thmacsha256_fromdns(dst_key_t *key, isc_buffer_t *data) {	HMACSHA256_Key *hkey;	int keylen;	isc_region_t r;	isc_sha256_t sha256ctx;	isc_buffer_remainingregion(data, &r);	if (r.length == 0)		return (ISC_R_SUCCESS);	hkey = (HMACSHA256_Key *) isc_mem_get(key->mctx, sizeof(HMACSHA256_Key));	if (hkey == NULL)		return (ISC_R_NOMEMORY);	memset(hkey->key, 0, sizeof(hkey->key));	if (r.length > ISC_SHA256_DIGESTLENGTH) {		isc_sha256_init(&sha256ctx);		isc_sha256_update(&sha256ctx, r.base, r.length);		isc_sha256_final(hkey->key, &sha256ctx);		keylen = ISC_SHA256_DIGESTLENGTH;	}	else {		memcpy(hkey->key, r.base, r.length);		keylen = r.length;	}	key->key_size = keylen * 8;	key->opaque = hkey;	return (ISC_R_SUCCESS);}static isc_result_thmacsha256_tofile(const dst_key_t *key, const char *directory) {	int cnt = 0;	HMACSHA256_Key *hkey;	dst_private_t priv;	int bytes = (key->key_size + 7) / 8;	unsigned char buf[2];	if (key->opaque == NULL)		return (DST_R_NULLKEY);	hkey = (HMACSHA256_Key *) key->opaque;	priv.elements[cnt].tag = TAG_HMACSHA256_KEY;	priv.elements[cnt].length = bytes;	priv.elements[cnt++].data = hkey->key;	buf[0] = (key->key_bits >> 8) & 0xffU;	buf[1] = key->key_bits & 0xffU;	priv.elements[cnt].tag = TAG_HMACSHA256_BITS;	priv.elements[cnt].data = buf;	priv.elements[cnt++].length = 2;	priv.nelements = cnt;	return (dst__privstruct_writefile(key, &priv, directory));}static isc_result_thmacsha256_parse(dst_key_t *key, isc_lex_t *lexer) {	dst_private_t priv;	isc_result_t result, tresult;	isc_buffer_t b;	isc_mem_t *mctx = key->mctx;	unsigned int i;	/* read private key file */	result = dst__privstruct_parse(key, DST_ALG_HMACSHA256, lexer, mctx,				       &priv);	if (result != ISC_R_SUCCESS)		return (result);	key->key_bits = 0;	for (i = 0; i < priv.nelements; i++) {		switch (priv.elements[i].tag) {		case TAG_HMACSHA256_KEY:			isc_buffer_init(&b, priv.elements[i].data,				        priv.elements[i].length);			isc_buffer_add(&b, priv.elements[i].length);			tresult = hmacsha256_fromdns(key, &b);			if (tresult != ISC_R_SUCCESS)				result = tresult;			break;		case TAG_HMACSHA256_BITS:			tresult = getkeybits(key, &priv.elements[i]);			if (tresult != ISC_R_SUCCESS)				result = tresult;			break;		default:			result = DST_R_INVALIDPRIVATEKEY;			break;		}	}	dst__privstruct_free(&priv, mctx);	memset(&priv, 0, sizeof(priv));	return (result);}static dst_func_t hmacsha256_functions = {	hmacsha256_createctx,	hmacsha256_destroyctx,	hmacsha256_adddata,	hmacsha256_sign,	hmacsha256_verify,	NULL, /* computesecret */	hmacsha256_compare,	NULL, /* paramcompare */	hmacsha256_generate,	hmacsha256_isprivate,	hmacsha256_destroy,

⌨️ 快捷键说明

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