1148456Spjd/*-
2148456Spjd * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3148456Spjd * All rights reserved.
4148456Spjd *
5148456Spjd * Redistribution and use in source and binary forms, with or without
6148456Spjd * modification, are permitted provided that the following conditions
7148456Spjd * are met:
8148456Spjd * 1. Redistributions of source code must retain the above copyright
9148456Spjd *    notice, this list of conditions and the following disclaimer.
10148456Spjd * 2. Redistributions in binary form must reproduce the above copyright
11148456Spjd *    notice, this list of conditions and the following disclaimer in the
12148456Spjd *    documentation and/or other materials provided with the distribution.
13155174Spjd *
14148456Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15148456Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16148456Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17148456Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18148456Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19148456Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20148456Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21148456Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22148456Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23148456Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24148456Spjd * SUCH DAMAGE.
25148456Spjd */
26148456Spjd
27148456Spjd#include <sys/cdefs.h>
28148456Spjd__FBSDID("$FreeBSD: stable/11/sys/geom/eli/pkcs5v2.c 329175 2018-02-12 17:44:35Z kevans $");
29148456Spjd
30148456Spjd#include <sys/param.h>
31148456Spjd#ifdef _KERNEL
32148456Spjd#include <sys/systm.h>
33148456Spjd#include <sys/kernel.h>
34148456Spjd#else
35148456Spjd#include <sys/resource.h>
36148456Spjd#include <stdint.h>
37148456Spjd#include <strings.h>
38148456Spjd#endif
39148456Spjd
40148456Spjd#include <geom/eli/g_eli.h>
41148456Spjd#include <geom/eli/pkcs5v2.h>
42148456Spjd
43148456Spjdstatic __inline void
44148456Spjdxor(uint8_t *dst, const uint8_t *src, size_t size)
45148456Spjd{
46148456Spjd
47148456Spjd	for (; size > 0; size--)
48148456Spjd		*dst++ ^= *src++;
49148456Spjd}
50148456Spjd
51148456Spjdvoid
52148456Spjdpkcs5v2_genkey(uint8_t *key, unsigned keylen, const uint8_t *salt,
53148456Spjd    size_t saltsize, const char *passphrase, u_int iterations)
54148456Spjd{
55148456Spjd	uint8_t md[SHA512_MDLEN], saltcount[saltsize + sizeof(uint32_t)];
56148456Spjd	uint8_t *counter, *keyp;
57148456Spjd	u_int i, bsize, passlen;
58148456Spjd	uint32_t count;
59327856Sasomers	struct hmac_ctx startpoint, ctx;
60148456Spjd
61148456Spjd	passlen = strlen(passphrase);
62148456Spjd	bzero(key, keylen);
63148456Spjd	bcopy(salt, saltcount, saltsize);
64148456Spjd	counter = saltcount + saltsize;
65148456Spjd
66148456Spjd	keyp = key;
67148456Spjd	for (count = 1; keylen > 0; count++, keylen -= bsize, keyp += bsize) {
68148456Spjd		bsize = MIN(keylen, sizeof(md));
69148456Spjd
70327856Sasomers		be32enc(counter, count);
71327856Sasomers
72327856Sasomers		g_eli_crypto_hmac_init(&startpoint, passphrase, passlen);
73327856Sasomers		ctx = startpoint;
74327856Sasomers		g_eli_crypto_hmac_update(&ctx, saltcount, sizeof(saltcount));
75327856Sasomers		g_eli_crypto_hmac_final(&ctx, md, sizeof(md));
76148456Spjd		xor(keyp, md, bsize);
77148456Spjd
78148456Spjd		for(i = 1; i < iterations; i++) {
79327856Sasomers			ctx = startpoint;
80327856Sasomers			g_eli_crypto_hmac_update(&ctx, md, sizeof(md));
81327856Sasomers			g_eli_crypto_hmac_final(&ctx, md, sizeof(md));
82148456Spjd			xor(keyp, md, bsize);
83148456Spjd		}
84148456Spjd	}
85327856Sasomers	explicit_bzero(&startpoint, sizeof(startpoint));
86327856Sasomers	explicit_bzero(&ctx, sizeof(ctx));
87148456Spjd}
88148456Spjd
89148456Spjd#ifndef _KERNEL
90329175Skevans#ifndef _STANDALONE
91148456Spjd/*
92148456Spjd * Return the number of microseconds needed for 'interations' iterations.
93148456Spjd */
94148456Spjdstatic int
95148456Spjdpkcs5v2_probe(int iterations)
96148456Spjd{
97148456Spjd	uint8_t	key[G_ELI_USERKEYLEN], salt[G_ELI_SALTLEN];
98148456Spjd	uint8_t passphrase[] = "passphrase";
99148456Spjd	struct rusage start, end;
100148456Spjd	int usecs;
101148456Spjd
102148456Spjd	getrusage(RUSAGE_SELF, &start);
103148456Spjd	pkcs5v2_genkey(key, sizeof(key), salt, sizeof(salt), passphrase,
104148456Spjd	    iterations);
105148456Spjd	getrusage(RUSAGE_SELF, &end);
106148456Spjd
107148456Spjd	usecs = end.ru_utime.tv_sec - start.ru_utime.tv_sec;
108148456Spjd	usecs *= 1000000;
109148456Spjd	usecs += end.ru_utime.tv_usec - start.ru_utime.tv_usec;
110148456Spjd	return (usecs);
111148456Spjd}
112148456Spjd
113148456Spjd/*
114148456Spjd * Return the number of iterations which takes 'usecs' microseconds.
115148456Spjd */
116148456Spjdint
117148456Spjdpkcs5v2_calculate(int usecs)
118148456Spjd{
119148456Spjd	int iterations, v;
120148456Spjd
121148456Spjd	for (iterations = 1; ; iterations <<= 1) {
122148456Spjd		v = pkcs5v2_probe(iterations);
123148456Spjd		if (v > 2000000)
124148456Spjd			break;
125148456Spjd	}
126148456Spjd	return (((intmax_t)iterations * (intmax_t)usecs) / v);
127148456Spjd}
128329175Skevans#endif	/* !_STANDALONE */
129148456Spjd#endif	/* !_KERNEL */
130