2 * LUKS - Linux Unified Key Setup v2, PBKDF2 digest handler (LUKS1 compatible)
4 * Copyright (C) 2015-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2015-2020 Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "luks2_internal.h"
24 #define LUKS_DIGESTSIZE 20 // since SHA1
25 #define LUKS_SALTSIZE 32
26 #define LUKS_MKD_ITERATIONS_MS 125
28 static int PBKDF2_digest_verify(struct crypt_device *cd,
30 const char *volume_key,
31 size_t volume_key_len)
33 char checkHashBuf[64];
34 json_object *jobj_digest, *jobj1;
36 char *mkDigest = NULL, mkDigestSalt[LUKS_SALTSIZE];
37 unsigned int mkDigestIterations;
41 /* This can be done only for internally linked digests */
42 jobj_digest = LUKS2_get_digest_jobj(crypt_get_hdr(cd, CRYPT_LUKS2), digest);
46 if (!json_object_object_get_ex(jobj_digest, "hash", &jobj1))
48 hashSpec = json_object_get_string(jobj1);
50 if (!json_object_object_get_ex(jobj_digest, "iterations", &jobj1))
52 mkDigestIterations = json_object_get_int64(jobj1);
54 if (!json_object_object_get_ex(jobj_digest, "salt", &jobj1))
56 len = sizeof(mkDigestSalt);
57 if (!base64_decode(json_object_get_string(jobj1),
58 json_object_get_string_len(jobj1), mkDigestSalt, &len))
60 if (len != LUKS_SALTSIZE)
63 if (!json_object_object_get_ex(jobj_digest, "digest", &jobj1))
66 if (!base64_decode_alloc(json_object_get_string(jobj1),
67 json_object_get_string_len(jobj1), &mkDigest, &len))
69 if (len < LUKS_DIGESTSIZE ||
70 len > sizeof(checkHashBuf) ||
71 (len != LUKS_DIGESTSIZE && len != (size_t)crypt_hash_size(hashSpec))) {
77 if (crypt_pbkdf(CRYPT_KDF_PBKDF2, hashSpec, volume_key, volume_key_len,
78 mkDigestSalt, LUKS_SALTSIZE,
80 mkDigestIterations, 0, 0) < 0) {
83 if (memcmp(checkHashBuf, mkDigest, len) == 0)
91 static int PBKDF2_digest_store(struct crypt_device *cd,
93 const char *volume_key,
94 size_t volume_key_len)
96 json_object *jobj_digest, *jobj_digests;
97 char salt[LUKS_SALTSIZE], digest_raw[128];
100 struct luks2_hdr *hdr;
101 struct crypt_pbkdf_limits pbkdf_limits;
102 const struct crypt_pbkdf_type *pbkdf_cd;
103 struct crypt_pbkdf_type pbkdf = {
104 .type = CRYPT_KDF_PBKDF2,
105 .time_ms = LUKS_MKD_ITERATIONS_MS,
108 /* Inherit hash from PBKDF setting */
109 pbkdf_cd = crypt_get_pbkdf_type(cd);
111 pbkdf.hash = pbkdf_cd->hash;
113 pbkdf.hash = DEFAULT_LUKS1_HASH;
115 log_dbg(cd, "Setting PBKDF2 type key digest %d.", digest);
117 r = crypt_random_get(cd, salt, LUKS_SALTSIZE, CRYPT_RND_SALT);
121 r = crypt_pbkdf_get_limits(CRYPT_KDF_PBKDF2, &pbkdf_limits);
125 if (crypt_get_pbkdf(cd)->flags & CRYPT_PBKDF_NO_BENCHMARK)
126 pbkdf.iterations = pbkdf_limits.min_iterations;
128 r = crypt_benchmark_pbkdf_internal(cd, &pbkdf, volume_key_len);
133 hmac_size = crypt_hmac_size(pbkdf.hash);
134 if (hmac_size < 0 || hmac_size > (int)sizeof(digest_raw))
137 r = crypt_pbkdf(CRYPT_KDF_PBKDF2, pbkdf.hash, volume_key, volume_key_len,
138 salt, LUKS_SALTSIZE, digest_raw, hmac_size,
139 pbkdf.iterations, 0, 0);
143 jobj_digest = LUKS2_get_digest_jobj(crypt_get_hdr(cd, CRYPT_LUKS2), digest);
146 hdr = crypt_get_hdr(cd, CRYPT_LUKS2);
147 jobj_digest = json_object_new_object();
148 json_object_object_get_ex(hdr->jobj, "digests", &jobj_digests);
151 json_object_object_add(jobj_digest, "type", json_object_new_string("pbkdf2"));
152 json_object_object_add(jobj_digest, "keyslots", json_object_new_array());
153 json_object_object_add(jobj_digest, "segments", json_object_new_array());
154 json_object_object_add(jobj_digest, "hash", json_object_new_string(pbkdf.hash));
155 json_object_object_add(jobj_digest, "iterations", json_object_new_int(pbkdf.iterations));
157 base64_encode_alloc(salt, LUKS_SALTSIZE, &base64_str);
159 json_object_put(jobj_digest);
162 json_object_object_add(jobj_digest, "salt", json_object_new_string(base64_str));
165 base64_encode_alloc(digest_raw, hmac_size, &base64_str);
167 json_object_put(jobj_digest);
170 json_object_object_add(jobj_digest, "digest", json_object_new_string(base64_str));
174 json_object_object_add_by_uint(jobj_digests, digest, jobj_digest);
176 JSON_DBG(cd, jobj_digest, "Digest JSON:");
180 static int PBKDF2_digest_dump(struct crypt_device *cd, int digest)
182 json_object *jobj_digest, *jobj1;
184 /* This can be done only for internally linked digests */
185 jobj_digest = LUKS2_get_digest_jobj(crypt_get_hdr(cd, CRYPT_LUKS2), digest);
189 json_object_object_get_ex(jobj_digest, "hash", &jobj1);
190 log_std(cd, "\tHash: %s\n", json_object_get_string(jobj1));
192 json_object_object_get_ex(jobj_digest, "iterations", &jobj1);
193 log_std(cd, "\tIterations: %" PRIu64 "\n", json_object_get_int64(jobj1));
195 json_object_object_get_ex(jobj_digest, "salt", &jobj1);
196 log_std(cd, "\tSalt: ");
197 hexprint_base64(cd, jobj1, " ", " ");
199 json_object_object_get_ex(jobj_digest, "digest", &jobj1);
200 log_std(cd, "\tDigest: ");
201 hexprint_base64(cd, jobj1, " ", " ");
206 const digest_handler PBKDF2_digest = {
208 .verify = PBKDF2_digest_verify,
209 .store = PBKDF2_digest_store,
210 .dump = PBKDF2_digest_dump,