2
* Implementation of Password-Based Cryptography as per PKCS#5
3
* Copyright (C) 2002,2003 Simon Josefsson
4
* Copyright (C) 2004 Free Software Foundation
6
* cryptsetup related changes
7
* Copyright (C) 2012, Red Hat, Inc. All rights reserved.
9
* This file is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Lesser General Public
11
* License as published by the Free Software Foundation; either
12
* version 2.1 of the License, or (at your option) any later version.
14
* This file is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this file; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
#include "crypto_backend.h"
32
* PBKDF2 applies a pseudorandom function (see Appendix B.1 for an
33
* example) to derive keys. The length of the derived key is essentially
34
* unbounded. (However, the maximum effective search space for the
35
* derived key may be limited by the structure of the underlying
36
* pseudorandom function. See Appendix B.1 for further discussion.)
37
* PBKDF2 is recommended for new applications.
39
* PBKDF2 (P, S, c, dkLen)
41
* Options: PRF underlying pseudorandom function (hLen
42
* denotes the length in octets of the
43
* pseudorandom function output)
45
* Input: P password, an octet string (ASCII or UTF-8)
46
* S salt, an octet string
47
* c iteration count, a positive integer
48
* dkLen intended length in octets of the derived
49
* key, a positive integer, at most
52
* Output: DK derived key, a dkLen-octet string
55
#define MAX_PRF_BLOCK_LEN 80
57
int pkcs5_pbkdf2(const char *hash,
58
const char *P, size_t Plen,
59
const char *S, size_t Slen,
60
unsigned int c, unsigned int dkLen,
63
struct crypt_hmac *hmac;
64
char U[MAX_PRF_BLOCK_LEN];
65
char T[MAX_PRF_BLOCK_LEN];
66
int i, k, rc = -EINVAL;
67
unsigned int u, hLen, l, r;
68
size_t tmplen = Slen + 4;
75
hLen = crypt_hmac_size(hash);
76
if (hLen == 0 || hLen > MAX_PRF_BLOCK_LEN)
89
* 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
93
if (dkLen > 4294967295U)
97
* 2. Let l be the number of hLen-octet blocks in the derived key,
98
* rounding up, and let r be the number of octets in the last
101
* l = CEIL (dkLen / hLen) ,
102
* r = dkLen - (l - 1) * hLen .
104
* Here, CEIL (x) is the "ceiling" function, i.e. the smallest
105
* integer greater than, or equal to, x.
111
r = dkLen - (l - 1) * hLen;
114
* 3. For each block of the derived key apply the function F defined
115
* below to the password P, the salt S, the iteration count c, and
116
* the block index to compute the block:
118
* T_1 = F (P, S, c, 1) ,
119
* T_2 = F (P, S, c, 2) ,
121
* T_l = F (P, S, c, l) ,
123
* where the function F is defined as the exclusive-or sum of the
124
* first c iterates of the underlying pseudorandom function PRF
125
* applied to the password P and the concatenation of the salt S
126
* and the block index i:
128
* F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
132
* U_1 = PRF (P, S || INT (i)) ,
133
* U_2 = PRF (P, U_1) ,
135
* U_c = PRF (P, U_{c-1}) .
137
* Here, INT (i) is a four-octet encoding of the integer i, most
138
* significant octet first.
140
* 4. Concatenate the blocks and extract the first dkLen octets to
141
* produce a derived key DK:
143
* DK = T_1 || T_2 || ... || T_l<0..r-1>
145
* 5. Output the derived key DK.
147
* Note. The construction of the function F follows a "belt-and-
148
* suspenders" approach. The iterates U_i are computed recursively to
149
* remove a degree of parallelism from an opponent; they are exclusive-
150
* ored together to reduce concerns about the recursion degenerating
151
* into a small set of values.
155
if (crypt_hmac_init(&hmac, hash, P, Plen))
158
for (i = 1; (unsigned int) i <= l; i++) {
161
for (u = 1; u <= c ; u++) {
163
memcpy(tmp, S, Slen);
164
tmp[Slen + 0] = (i & 0xff000000) >> 24;
165
tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
166
tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
167
tmp[Slen + 3] = (i & 0x000000ff) >> 0;
169
if (crypt_hmac_write(hmac, tmp, tmplen))
172
if (crypt_hmac_write(hmac, U, hLen))
176
if (crypt_hmac_final(hmac, U, hLen))
179
for (k = 0; (unsigned int) k < hLen; k++)
183
memcpy(DK + (i - 1) * hLen, T, (unsigned int) i == l ? r : hLen);
187
crypt_hmac_destroy(hmac);