2 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/resource.h>
40 #include <geom/eli/g_eli.h>
41 #include <geom/eli/pkcs5v2.h>
44 xor(uint8_t *dst, const uint8_t *src, size_t size)
47 for (; size > 0; size--)
52 pkcs5v2_genkey(uint8_t *key, unsigned keylen, const uint8_t *salt,
53 size_t saltsize, const char *passphrase, u_int iterations)
55 uint8_t md[SHA512_MDLEN], saltcount[saltsize + sizeof(uint32_t)];
56 uint8_t *counter, *keyp;
57 u_int i, bsize, passlen;
59 struct hmac_ctx startpoint, ctx;
61 passlen = strlen(passphrase);
63 bcopy(salt, saltcount, saltsize);
64 counter = saltcount + saltsize;
67 for (count = 1; keylen > 0; count++, keylen -= bsize, keyp += bsize) {
68 bsize = MIN(keylen, sizeof(md));
70 be32enc(counter, count);
72 g_eli_crypto_hmac_init(&startpoint, passphrase, passlen);
74 g_eli_crypto_hmac_update(&ctx, saltcount, sizeof(saltcount));
75 g_eli_crypto_hmac_final(&ctx, md, sizeof(md));
78 for(i = 1; i < iterations; i++) {
80 g_eli_crypto_hmac_update(&ctx, md, sizeof(md));
81 g_eli_crypto_hmac_final(&ctx, md, sizeof(md));
85 explicit_bzero(&startpoint, sizeof(startpoint));
86 explicit_bzero(&ctx, sizeof(ctx));
92 * Return the number of microseconds needed for 'interations' iterations.
95 pkcs5v2_probe(int iterations)
97 uint8_t key[G_ELI_USERKEYLEN], salt[G_ELI_SALTLEN];
98 uint8_t passphrase[] = "passphrase";
99 struct rusage start, end;
102 getrusage(RUSAGE_SELF, &start);
103 pkcs5v2_genkey(key, sizeof(key), salt, sizeof(salt), passphrase,
105 getrusage(RUSAGE_SELF, &end);
107 usecs = end.ru_utime.tv_sec - start.ru_utime.tv_sec;
109 usecs += end.ru_utime.tv_usec - start.ru_utime.tv_usec;
114 * Return the number of iterations which takes 'usecs' microseconds.
117 pkcs5v2_calculate(int usecs)
121 for (iterations = 1; ; iterations <<= 1) {
122 v = pkcs5v2_probe(iterations);
126 return (((intmax_t)iterations * (intmax_t)usecs) / v);
129 #endif /* !_KERNEL */