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-2020 Red Hat, Inc. All rights reserved.
8 * Copyright (C) 2012-2020 Milan Broz
10 * This file is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This file is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this file; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "crypto_backend_internal.h"
30 static int hash_buf(const char *src, size_t src_len,
31 char *dst, size_t dst_len,
32 const char *hash_name)
34 struct crypt_hash *hd = NULL;
37 if (crypt_hash_init(&hd, hash_name))
40 r = crypt_hash_write(hd, src, src_len);
43 r = crypt_hash_final(hd, dst, dst_len);
45 crypt_hash_destroy(hd);
52 * PBKDF2 applies a pseudorandom function (see Appendix B.1 for an
53 * example) to derive keys. The length of the derived key is essentially
54 * unbounded. (However, the maximum effective search space for the
55 * derived key may be limited by the structure of the underlying
56 * pseudorandom function. See Appendix B.1 for further discussion.)
57 * PBKDF2 is recommended for new applications.
59 * PBKDF2 (P, S, c, dkLen)
61 * Options: PRF underlying pseudorandom function (hLen
62 * denotes the length in octets of the
63 * pseudorandom function output)
65 * Input: P password, an octet string (ASCII or UTF-8)
66 * S salt, an octet string
67 * c iteration count, a positive integer
68 * dkLen intended length in octets of the derived
69 * key, a positive integer, at most
72 * Output: DK derived key, a dkLen-octet string
76 * if hash_block_size is not zero, the HMAC key is pre-hashed
77 * inside this function.
78 * This prevents situation when crypto backend doesn't support
79 * long HMAC keys or it tries hash long key in every iteration
80 * (because of crypt_final() cannot do simple key reset.
83 #define MAX_PRF_BLOCK_LEN 80
85 int pkcs5_pbkdf2(const char *hash,
86 const char *P, size_t Plen,
87 const char *S, size_t Slen,
88 unsigned int c, unsigned int dkLen,
89 char *DK, unsigned int hash_block_size)
91 struct crypt_hmac *hmac;
92 char U[MAX_PRF_BLOCK_LEN];
93 char T[MAX_PRF_BLOCK_LEN];
94 char P_hash[MAX_PRF_BLOCK_LEN];
95 int i, k, rc = -EINVAL;
96 unsigned int u, hLen, l, r;
97 size_t tmplen = Slen + 4;
100 tmp = alloca(tmplen);
104 hLen = crypt_hmac_size(hash);
105 if (hLen == 0 || hLen > MAX_PRF_BLOCK_LEN)
118 * 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
122 if (dkLen > 4294967295U)
126 * 2. Let l be the number of hLen-octet blocks in the derived key,
127 * rounding up, and let r be the number of octets in the last
130 * l = CEIL (dkLen / hLen) ,
131 * r = dkLen - (l - 1) * hLen .
133 * Here, CEIL (x) is the "ceiling" function, i.e. the smallest
134 * integer greater than, or equal to, x.
140 r = dkLen - (l - 1) * hLen;
143 * 3. For each block of the derived key apply the function F defined
144 * below to the password P, the salt S, the iteration count c, and
145 * the block index to compute the block:
147 * T_1 = F (P, S, c, 1) ,
148 * T_2 = F (P, S, c, 2) ,
150 * T_l = F (P, S, c, l) ,
152 * where the function F is defined as the exclusive-or sum of the
153 * first c iterates of the underlying pseudorandom function PRF
154 * applied to the password P and the concatenation of the salt S
155 * and the block index i:
157 * F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
161 * U_1 = PRF (P, S || INT (i)) ,
162 * U_2 = PRF (P, U_1) ,
164 * U_c = PRF (P, U_{c-1}) .
166 * Here, INT (i) is a four-octet encoding of the integer i, most
167 * significant octet first.
169 * 4. Concatenate the blocks and extract the first dkLen octets to
170 * produce a derived key DK:
172 * DK = T_1 || T_2 || ... || T_l<0..r-1>
174 * 5. Output the derived key DK.
176 * Note. The construction of the function F follows a "belt-and-
177 * suspenders" approach. The iterates U_i are computed recursively to
178 * remove a degree of parallelism from an opponent; they are exclusive-
179 * ored together to reduce concerns about the recursion degenerating
180 * into a small set of values.
184 /* If hash_block_size is provided, hash password in advance. */
185 if (hash_block_size > 0 && Plen > hash_block_size) {
186 if (hash_buf(P, Plen, P_hash, hLen, hash))
189 if (crypt_hmac_init(&hmac, hash, P_hash, hLen))
191 crypt_backend_memzero(P_hash, sizeof(P_hash));
193 if (crypt_hmac_init(&hmac, hash, P, Plen))
197 for (i = 1; (unsigned int) i <= l; i++) {
200 for (u = 1; u <= c ; u++) {
202 memcpy(tmp, S, Slen);
203 tmp[Slen + 0] = (i & 0xff000000) >> 24;
204 tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
205 tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
206 tmp[Slen + 3] = (i & 0x000000ff) >> 0;
208 if (crypt_hmac_write(hmac, tmp, tmplen))
211 if (crypt_hmac_write(hmac, U, hLen))
215 if (crypt_hmac_final(hmac, U, hLen))
218 for (k = 0; (unsigned int) k < hLen; k++)
222 memcpy(DK + (i - 1) * hLen, T, (unsigned int) i == l ? r : hLen);
226 crypt_hmac_destroy(hmac);
227 crypt_backend_memzero(U, sizeof(U));
228 crypt_backend_memzero(T, sizeof(T));
229 crypt_backend_memzero(tmp, tmplen);