[BACK]Return to iterated_hash.c CVS log [TXT][DIR] Up to [local] / src / usr.sbin / nsd

File: [local] / src / usr.sbin / nsd / iterated_hash.c (download)

Revision 1.2, Sun Oct 24 12:14:19 2021 UTC (2 years, 7 months ago) by florian
Branch: MAIN
CVS Tags: OPENBSD_7_5_BASE, OPENBSD_7_5, OPENBSD_7_4_BASE, OPENBSD_7_4, OPENBSD_7_3_BASE, OPENBSD_7_3, OPENBSD_7_2_BASE, OPENBSD_7_2, OPENBSD_7_1_BASE, OPENBSD_7_1, HEAD
Changes since 1.1: +38 -1 lines

Update to 4.3.8.
OK sthen

/*
 * iterated_hash.c -- nsec3 hash calculation.
 *
 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
 *
 * See LICENSE for the license.
 *
 * With thanks to Ben Laurie.
 */
#include "config.h"
#ifdef NSEC3
#if defined(HAVE_SHA1_INIT) && !defined(DEPRECATED_SHA1_INIT)
#include <openssl/sha.h>
#else
#include <openssl/evp.h>
#endif
#include <stdio.h>
#include <assert.h>

#include "iterated_hash.h"
#include "util.h"

int
iterated_hash(unsigned char out[SHA_DIGEST_LENGTH],
	const unsigned char *salt, int saltlength,
	const unsigned char *in, int inlength, int iterations)
{
#if defined(NSEC3) && defined(HAVE_SSL)
#if defined(HAVE_SHA1_INIT) && !defined(DEPRECATED_SHA1_INIT)
	SHA_CTX ctx;
#else
	EVP_MD_CTX* ctx;
#endif
	int n;
#if defined(HAVE_SHA1_INIT) && !defined(DEPRECATED_SHA1_INIT)
#else
	ctx = EVP_MD_CTX_create();
	if(!ctx) {
		log_msg(LOG_ERR, "out of memory in iterated_hash");
		return 0;
	}
#endif
	assert(in && inlength > 0 && iterations >= 0);
	for(n=0 ; n <= iterations ; ++n)
	{
#if defined(HAVE_SHA1_INIT) && !defined(DEPRECATED_SHA1_INIT)
		SHA1_Init(&ctx);
		SHA1_Update(&ctx, in, inlength);
		if(saltlength > 0)
			SHA1_Update(&ctx, salt, saltlength);
		SHA1_Final(out, &ctx);
#else
		if(!EVP_DigestInit(ctx, EVP_sha1()))
			log_msg(LOG_ERR, "iterated_hash could not EVP_DigestInit");

		if(!EVP_DigestUpdate(ctx, in, inlength))
			log_msg(LOG_ERR, "iterated_hash could not EVP_DigestUpdate");
		if(saltlength > 0) {
			if(!EVP_DigestUpdate(ctx, salt, saltlength))
				log_msg(LOG_ERR, "iterated_hash could not EVP_DigestUpdate salt");
		}
		if(!EVP_DigestFinal_ex(ctx, out, NULL))
			log_msg(LOG_ERR, "iterated_hash could not EVP_DigestFinal_ex");
#endif
		in=out;
		inlength=SHA_DIGEST_LENGTH;
	}
#if defined(HAVE_SHA1_INIT) && !defined(DEPRECATED_SHA1_INIT)
#else
	EVP_MD_CTX_destroy(ctx);
#endif
	return SHA_DIGEST_LENGTH;
#else
	(void)out; (void)salt; (void)saltlength;
	(void)in; (void)inlength; (void)iterations;
	return 0;
#endif
}

#endif /* NSEC3 */