xref: /netbsd-src/external/mpl/dhcp/bind/dist/lib/isc/iterated_hash.c (revision 4afad4b7fa6d4a0d3dedf41d1587a7250710ae54)
1 /*	$NetBSD: iterated_hash.c,v 1.1 2024/02/18 20:57:49 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 #include <stdio.h>
17 
18 #include <openssl/opensslv.h>
19 #include <openssl/sha.h>
20 
21 #include <isc/iterated_hash.h>
22 #include <isc/thread.h>
23 #include <isc/util.h>
24 
25 int
isc_iterated_hash(unsigned char * out,const unsigned int hashalg,const int iterations,const unsigned char * salt,const int saltlength,const unsigned char * in,const int inlength)26 isc_iterated_hash(unsigned char *out, const unsigned int hashalg,
27 		  const int iterations, const unsigned char *salt,
28 		  const int saltlength, const unsigned char *in,
29 		  const int inlength) {
30 	REQUIRE(out != NULL);
31 
32 	int n = 0;
33 	size_t len;
34 	const unsigned char *buf;
35 	SHA_CTX ctx;
36 
37 	if (hashalg != 1) {
38 		return (0);
39 	}
40 
41 	buf = in;
42 	len = inlength;
43 
44 	do {
45 		if (SHA1_Init(&ctx) != 1) {
46 			return (0);
47 		}
48 
49 		if (SHA1_Update(&ctx, buf, len) != 1) {
50 			return (0);
51 		}
52 
53 		if (SHA1_Update(&ctx, salt, saltlength) != 1) {
54 			return (0);
55 		}
56 
57 		if (SHA1_Final(out, &ctx) != 1) {
58 			return (0);
59 		}
60 
61 		buf = out;
62 		len = SHA_DIGEST_LENGTH;
63 	} while (n++ < iterations);
64 
65 	return (SHA_DIGEST_LENGTH);
66 }
67