1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/param.h>
30#ifdef _KERNEL
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#else
34#include <sys/resource.h>
35#include <stdint.h>
36#include <strings.h>
37#endif
38
39#include <geom/eli/g_eli.h>
40#include <geom/eli/pkcs5v2.h>
41
42static __inline void
43xor(uint8_t *dst, const uint8_t *src, size_t size)
44{
45
46	for (; size > 0; size--)
47		*dst++ ^= *src++;
48}
49
50void
51pkcs5v2_genkey(uint8_t *key, unsigned keylen, const uint8_t *salt,
52    size_t saltsize, const char *passphrase, u_int iterations)
53{
54	uint8_t md[SHA512_MDLEN], saltcount[saltsize + sizeof(uint32_t)];
55	uint8_t *counter, *keyp;
56	u_int i, bsize, passlen;
57	uint32_t count;
58	struct hmac_ctx startpoint, ctx;
59
60	passlen = strlen(passphrase);
61	bzero(key, keylen);
62	bcopy(salt, saltcount, saltsize);
63	counter = saltcount + saltsize;
64
65	keyp = key;
66	for (count = 1; keylen > 0; count++, keylen -= bsize, keyp += bsize) {
67		bsize = MIN(keylen, sizeof(md));
68
69		be32enc(counter, count);
70
71		g_eli_crypto_hmac_init(&startpoint, passphrase, passlen);
72		ctx = startpoint;
73		g_eli_crypto_hmac_update(&ctx, saltcount, sizeof(saltcount));
74		g_eli_crypto_hmac_final(&ctx, md, sizeof(md));
75		xor(keyp, md, bsize);
76
77		for(i = 1; i < iterations; i++) {
78			ctx = startpoint;
79			g_eli_crypto_hmac_update(&ctx, md, sizeof(md));
80			g_eli_crypto_hmac_final(&ctx, md, sizeof(md));
81			xor(keyp, md, bsize);
82		}
83	}
84	explicit_bzero(&startpoint, sizeof(startpoint));
85	explicit_bzero(&ctx, sizeof(ctx));
86}
87
88#ifndef _KERNEL
89#ifndef _STANDALONE
90/*
91 * Return the number of microseconds needed for 'interations' iterations.
92 */
93static int
94pkcs5v2_probe(int iterations)
95{
96	uint8_t	key[G_ELI_USERKEYLEN], salt[G_ELI_SALTLEN];
97	const char passphrase[] = "passphrase";
98	struct rusage start, end;
99	int usecs;
100
101	getrusage(RUSAGE_SELF, &start);
102	pkcs5v2_genkey(key, sizeof(key), salt, sizeof(salt), passphrase,
103	    iterations);
104	getrusage(RUSAGE_SELF, &end);
105
106	usecs = end.ru_utime.tv_sec - start.ru_utime.tv_sec;
107	usecs *= 1000000;
108	usecs += end.ru_utime.tv_usec - start.ru_utime.tv_usec;
109	return (usecs);
110}
111
112/*
113 * Return the number of iterations which takes 'usecs' microseconds.
114 */
115int
116pkcs5v2_calculate(int usecs)
117{
118	int iterations, v;
119
120	for (iterations = 1; ; iterations <<= 1) {
121		v = pkcs5v2_probe(iterations);
122		if (v > 2000000)
123			break;
124	}
125	return (((intmax_t)iterations * (intmax_t)usecs) / v);
126}
127#endif	/* !_STANDALONE */
128#endif	/* !_KERNEL */
129