1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#ifdef _KERNEL
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#else
37#include <sys/resource.h>
38#include <stdint.h>
39#include <strings.h>
40#endif
41
42#include <geom/eli/g_eli.h>
43#include <geom/eli/pkcs5v2.h>
44
45static __inline void
46xor(uint8_t *dst, const uint8_t *src, size_t size)
47{
48
49	for (; size > 0; size--)
50		*dst++ ^= *src++;
51}
52
53void
54pkcs5v2_genkey(uint8_t *key, unsigned keylen, const uint8_t *salt,
55    size_t saltsize, const char *passphrase, u_int iterations)
56{
57	uint8_t md[SHA512_MDLEN], saltcount[saltsize + sizeof(uint32_t)];
58	uint8_t *counter, *keyp;
59	u_int i, bsize, passlen;
60	uint32_t count;
61	struct hmac_ctx startpoint, ctx;
62
63	passlen = strlen(passphrase);
64	bzero(key, keylen);
65	bcopy(salt, saltcount, saltsize);
66	counter = saltcount + saltsize;
67
68	keyp = key;
69	for (count = 1; keylen > 0; count++, keylen -= bsize, keyp += bsize) {
70		bsize = MIN(keylen, sizeof(md));
71
72		be32enc(counter, count);
73
74		g_eli_crypto_hmac_init(&startpoint, passphrase, passlen);
75		ctx = startpoint;
76		g_eli_crypto_hmac_update(&ctx, saltcount, sizeof(saltcount));
77		g_eli_crypto_hmac_final(&ctx, md, sizeof(md));
78		xor(keyp, md, bsize);
79
80		for(i = 1; i < iterations; i++) {
81			ctx = startpoint;
82			g_eli_crypto_hmac_update(&ctx, md, sizeof(md));
83			g_eli_crypto_hmac_final(&ctx, md, sizeof(md));
84			xor(keyp, md, bsize);
85		}
86	}
87	explicit_bzero(&startpoint, sizeof(startpoint));
88	explicit_bzero(&ctx, sizeof(ctx));
89}
90
91#ifndef _KERNEL
92#ifndef _STANDALONE
93/*
94 * Return the number of microseconds needed for 'interations' iterations.
95 */
96static int
97pkcs5v2_probe(int iterations)
98{
99	uint8_t	key[G_ELI_USERKEYLEN], salt[G_ELI_SALTLEN];
100	const char passphrase[] = "passphrase";
101	struct rusage start, end;
102	int usecs;
103
104	getrusage(RUSAGE_SELF, &start);
105	pkcs5v2_genkey(key, sizeof(key), salt, sizeof(salt), passphrase,
106	    iterations);
107	getrusage(RUSAGE_SELF, &end);
108
109	usecs = end.ru_utime.tv_sec - start.ru_utime.tv_sec;
110	usecs *= 1000000;
111	usecs += end.ru_utime.tv_usec - start.ru_utime.tv_usec;
112	return (usecs);
113}
114
115/*
116 * Return the number of iterations which takes 'usecs' microseconds.
117 */
118int
119pkcs5v2_calculate(int usecs)
120{
121	int iterations, v;
122
123	for (iterations = 1; ; iterations <<= 1) {
124		v = pkcs5v2_probe(iterations);
125		if (v > 2000000)
126			break;
127	}
128	return (((intmax_t)iterations * (intmax_t)usecs) / v);
129}
130#endif	/* !_STANDALONE */
131#endif	/* !_KERNEL */
132