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$");
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;
59148456Spjd
60148456Spjd	passlen = strlen(passphrase);
61148456Spjd	bzero(key, keylen);
62148456Spjd	bcopy(salt, saltcount, saltsize);
63148456Spjd	counter = saltcount + saltsize;
64148456Spjd
65148456Spjd	keyp = key;
66148456Spjd	for (count = 1; keylen > 0; count++, keylen -= bsize, keyp += bsize) {
67148456Spjd		bsize = MIN(keylen, sizeof(md));
68148456Spjd
69148456Spjd		counter[0] = (count >> 24) & 0xff;
70148456Spjd		counter[1] = (count >> 16) & 0xff;
71148456Spjd		counter[2] = (count >> 8) & 0xff;
72148456Spjd		counter[3] = count & 0xff;
73148456Spjd		g_eli_crypto_hmac(passphrase, passlen, saltcount,
74148456Spjd		    sizeof(saltcount), md, 0);
75148456Spjd		xor(keyp, md, bsize);
76148456Spjd
77148456Spjd		for(i = 1; i < iterations; i++) {
78148456Spjd			g_eli_crypto_hmac(passphrase, passlen, md, sizeof(md),
79148456Spjd			    md, 0);
80148456Spjd			xor(keyp, md, bsize);
81148456Spjd		}
82148456Spjd	}
83148456Spjd}
84148456Spjd
85148456Spjd#ifndef _KERNEL
86148456Spjd/*
87148456Spjd * Return the number of microseconds needed for 'interations' iterations.
88148456Spjd */
89148456Spjdstatic int
90148456Spjdpkcs5v2_probe(int iterations)
91148456Spjd{
92148456Spjd	uint8_t	key[G_ELI_USERKEYLEN], salt[G_ELI_SALTLEN];
93148456Spjd	uint8_t passphrase[] = "passphrase";
94148456Spjd	struct rusage start, end;
95148456Spjd	int usecs;
96148456Spjd
97148456Spjd	getrusage(RUSAGE_SELF, &start);
98148456Spjd	pkcs5v2_genkey(key, sizeof(key), salt, sizeof(salt), passphrase,
99148456Spjd	    iterations);
100148456Spjd	getrusage(RUSAGE_SELF, &end);
101148456Spjd
102148456Spjd	usecs = end.ru_utime.tv_sec - start.ru_utime.tv_sec;
103148456Spjd	usecs *= 1000000;
104148456Spjd	usecs += end.ru_utime.tv_usec - start.ru_utime.tv_usec;
105148456Spjd	return (usecs);
106148456Spjd}
107148456Spjd
108148456Spjd/*
109148456Spjd * Return the number of iterations which takes 'usecs' microseconds.
110148456Spjd */
111148456Spjdint
112148456Spjdpkcs5v2_calculate(int usecs)
113148456Spjd{
114148456Spjd	int iterations, v;
115148456Spjd
116148456Spjd	for (iterations = 1; ; iterations <<= 1) {
117148456Spjd		v = pkcs5v2_probe(iterations);
118148456Spjd		if (v > 2000000)
119148456Spjd			break;
120148456Spjd	}
121148456Spjd	return (((intmax_t)iterations * (intmax_t)usecs) / v);
122148456Spjd}
123148456Spjd#endif	/* !_KERNEL */
124