xref: /openbsd-src/usr.sbin/nsd/iterated_hash.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*
2  * iterated_hash.c -- nsec3 hash calculation.
3  *
4  * Copyright (c) 2001-2011, NLnet Labs. All rights reserved.
5  *
6  * See LICENSE for the license.
7  *
8  * With thanks to Ben Laurie.
9  */
10 #include <config.h>
11 #ifdef NSEC3
12 #include <openssl/sha.h>
13 #include <stdio.h>
14 #include <assert.h>
15 
16 #include "iterated_hash.h"
17 
18 int
19 iterated_hash(unsigned char out[SHA_DIGEST_LENGTH],
20 	const unsigned char *salt, int saltlength,
21 	const unsigned char *in, int inlength, int iterations)
22 {
23 #if defined(NSEC3) && defined(HAVE_SSL)
24 	SHA_CTX ctx;
25 	int n;
26 	assert(in && inlength > 0 && iterations >= 0);
27 	for(n=0 ; n <= iterations ; ++n)
28 	{
29 		SHA1_Init(&ctx);
30 		SHA1_Update(&ctx, in, inlength);
31 		if(saltlength > 0)
32 			SHA1_Update(&ctx, salt, saltlength);
33 		SHA1_Final(out, &ctx);
34 		in=out;
35 		inlength=SHA_DIGEST_LENGTH;
36 	}
37 	return SHA_DIGEST_LENGTH;
38 #else
39 	return 0;
40 #endif
41 }
42 
43 #endif /* NSEC3 */
44