Deleted Added
full compact
1/*-
2 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/eli/pkcs5v2.c 155174 2006-02-01 12:06:01Z pjd $");
28__FBSDID("$FreeBSD: head/sys/geom/eli/pkcs5v2.c 293306 2016-01-07 05:47:34Z allanjude $");
29
30#include <sys/param.h>
31#ifdef _KERNEL
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#else
35#include <sys/resource.h>
36#include <stdint.h>
37#include <strings.h>
38#endif
39
40#include <geom/eli/g_eli.h>
41#include <geom/eli/pkcs5v2.h>
42
43static __inline void
44xor(uint8_t *dst, const uint8_t *src, size_t size)
45{
46
47 for (; size > 0; size--)
48 *dst++ ^= *src++;
49}
50
51void
52pkcs5v2_genkey(uint8_t *key, unsigned keylen, const uint8_t *salt,
53 size_t saltsize, const char *passphrase, u_int iterations)
54{
55 uint8_t md[SHA512_MDLEN], saltcount[saltsize + sizeof(uint32_t)];
56 uint8_t *counter, *keyp;
57 u_int i, bsize, passlen;
58 uint32_t count;
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 counter[0] = (count >> 24) & 0xff;
70 counter[1] = (count >> 16) & 0xff;
71 counter[2] = (count >> 8) & 0xff;
72 counter[3] = count & 0xff;
73 g_eli_crypto_hmac(passphrase, passlen, saltcount,
74 sizeof(saltcount), md, 0);
75 xor(keyp, md, bsize);
76
77 for(i = 1; i < iterations; i++) {
78 g_eli_crypto_hmac(passphrase, passlen, md, sizeof(md),
79 md, 0);
80 xor(keyp, md, bsize);
81 }
82 }
83}
84
85#ifndef _KERNEL
86#ifndef _STAND
87/*
88 * Return the number of microseconds needed for 'interations' iterations.
89 */
90static int
91pkcs5v2_probe(int iterations)
92{
93 uint8_t key[G_ELI_USERKEYLEN], salt[G_ELI_SALTLEN];
94 uint8_t passphrase[] = "passphrase";
95 struct rusage start, end;
96 int usecs;
97
98 getrusage(RUSAGE_SELF, &start);
99 pkcs5v2_genkey(key, sizeof(key), salt, sizeof(salt), passphrase,
100 iterations);
101 getrusage(RUSAGE_SELF, &end);
102
103 usecs = end.ru_utime.tv_sec - start.ru_utime.tv_sec;
104 usecs *= 1000000;
105 usecs += end.ru_utime.tv_usec - start.ru_utime.tv_usec;
106 return (usecs);
107}
108
109/*
110 * Return the number of iterations which takes 'usecs' microseconds.
111 */
112int
113pkcs5v2_calculate(int usecs)
114{
115 int iterations, v;
116
117 for (iterations = 1; ; iterations <<= 1) {
118 v = pkcs5v2_probe(iterations);
119 if (v > 2000000)
120 break;
121 }
122 return (((intmax_t)iterations * (intmax_t)usecs) / v);
123}
124#endif /* !_STAND */
125#endif /* !_KERNEL */