2 * utils_crypt - cipher utilities for cryptsetup
4 * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2009-2023 Milan Broz
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include "libcryptsetup.h"
32 #include "utils_crypt.h"
34 #define MAX_CAPI_LEN_STR "143" /* for sscanf of crypto API string + 16 + \0 */
36 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
39 if (!s || !cipher || !cipher_mode)
42 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
43 cipher, cipher_mode) == 2) {
44 if (!strcmp(cipher_mode, "plain"))
45 strcpy(cipher_mode, "cbc-plain");
47 char *tmp = strchr(cipher, ':');
48 *key_nums = tmp ? atoi(++tmp) : 1;
56 /* Short version for "empty" cipher */
57 if (!strcmp(s, "null") || !strcmp(s, "cipher_null")) {
58 strcpy(cipher, "cipher_null");
59 strcpy(cipher_mode, "ecb");
65 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
66 strcpy(cipher_mode, "cbc-plain");
75 int crypt_parse_hash_integrity_mode(const char *s, char *integrity)
77 char mode[MAX_CIPHER_LEN], hash[MAX_CIPHER_LEN];
80 if (!s || !integrity || strchr(s, '(') || strchr(s, ')'))
83 r = sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", mode, hash);
84 if (r == 2 && !isdigit(hash[0]))
85 r = snprintf(integrity, MAX_CIPHER_LEN, "%s(%s)", mode, hash);
87 r = snprintf(integrity, MAX_CIPHER_LEN, "%s-%s", mode, hash);
89 r = snprintf(integrity, MAX_CIPHER_LEN, "%s", mode);
93 if (r < 0 || r >= MAX_CIPHER_LEN)
99 int crypt_parse_integrity_mode(const char *s, char *integrity,
100 int *integrity_key_size)
104 if (!s || !integrity)
108 if (!strcmp(s, "aead") ||
109 !strcmp(s, "poly1305") ||
110 !strcmp(s, "none")) {
111 strncpy(integrity, s, MAX_CIPHER_LEN);
113 } else if (!strcmp(s, "hmac-sha1")) {
114 strncpy(integrity, "hmac(sha1)", MAX_CIPHER_LEN);
116 } else if (!strcmp(s, "hmac-sha256")) {
117 strncpy(integrity, "hmac(sha256)", MAX_CIPHER_LEN);
119 } else if (!strcmp(s, "hmac-sha512")) {
121 strncpy(integrity, "hmac(sha512)", MAX_CIPHER_LEN);
122 } else if (!strcmp(s, "cmac-aes")) {
124 strncpy(integrity, "cmac(aes)", MAX_CIPHER_LEN);
128 if (integrity_key_size)
129 *integrity_key_size = ks;
134 int crypt_parse_pbkdf(const char *s, const char **pbkdf)
136 const char *tmp = NULL;
141 if (!strcasecmp(s, CRYPT_KDF_PBKDF2))
142 tmp = CRYPT_KDF_PBKDF2;
143 else if (!strcasecmp(s, CRYPT_KDF_ARGON2I))
144 tmp = CRYPT_KDF_ARGON2I;
145 else if (!strcasecmp(s, CRYPT_KDF_ARGON2ID))
146 tmp = CRYPT_KDF_ARGON2ID;
158 * Thanks Mikulas Patocka for these two char converting functions.
160 * This function is used to load cryptographic keys, so it is coded in such a
161 * way that there are no conditions or memory accesses that depend on data.
163 * Explanation of the logic:
164 * (ch - '9' - 1) is negative if ch <= '9'
165 * ('0' - 1 - ch) is negative if ch >= '0'
166 * we "and" these two values, so the result is negative if ch is in the range
168 * we are only interested in the sign, so we do a shift ">> 8"; note that right
169 * shift of a negative value is implementation-defined, so we cast the
170 * value to (unsigned) before the shift --- we have 0xffffff if ch is in
171 * the range '0' ... '9', 0 otherwise
172 * we "and" this value with (ch - '0' + 1) --- we have a value 1 ... 10 if ch is
173 * in the range '0' ... '9', 0 otherwise
174 * we add this value to -1 --- we have a value 0 ... 9 if ch is in the range '0'
175 * ... '9', -1 otherwise
176 * the next line is similar to the previous one, but we need to decode both
177 * uppercase and lowercase letters, so we use (ch & 0xdf), which converts
178 * lowercase to uppercase
180 static int hex_to_bin(unsigned char ch)
182 unsigned char cu = ch & 0xdf;
184 ((ch - '0' + 1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) +
185 ((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8);
188 static char hex2asc(unsigned char c)
190 return c + '0' + ((unsigned)(9 - c) >> 4 & 0x27);
193 ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc)
207 bytes = safe_alloc ? crypt_safe_alloc(len) : malloc(len);
211 for (i = 0; i < len; i++) {
212 bh = hex_to_bin(hex[i * 2]);
213 bl = hex_to_bin(hex[i * 2 + 1]);
214 if (bh == -1 || bl == -1) {
215 safe_alloc ? crypt_safe_free(bytes) : free(bytes);
218 bytes[i] = (bh << 4) | bl;
224 char *crypt_bytes_to_hex(size_t size, const char *bytes)
232 /* Alloc adds trailing \0 */
234 hex = crypt_safe_alloc(2);
236 hex = crypt_safe_alloc(size * 2 + 1);
242 else for (i = 0; i < size; i++) {
243 hex[i * 2] = hex2asc((const unsigned char)bytes[i] >> 4);
244 hex[i * 2 + 1] = hex2asc((const unsigned char)bytes[i] & 0xf);
250 void crypt_log_hex(struct crypt_device *cd,
251 const char *bytes, size_t size,
252 const char *sep, int numwrap, const char *wrapsep)
256 for (i = 0; i < size; i++) {
257 if (wrapsep && numwrap && i && !(i % numwrap))
258 crypt_logf(cd, CRYPT_LOG_NORMAL, wrapsep);
259 crypt_logf(cd, CRYPT_LOG_NORMAL, "%c%c%s",
260 hex2asc((const unsigned char)bytes[i] >> 4),
261 hex2asc((const unsigned char)bytes[i] & 0xf), sep);
265 bool crypt_is_cipher_null(const char *cipher_spec)
269 return (strstr(cipher_spec, "cipher_null") || !strcmp(cipher_spec, "null"));
272 int crypt_capi_to_cipher(char **org_c, char **org_i, const char *c_dm, const char *i_dm)
274 char cipher[MAX_CAPI_ONE_LEN], mode[MAX_CAPI_ONE_LEN], iv[MAX_CAPI_ONE_LEN],
275 auth[MAX_CAPI_ONE_LEN], tmp[MAX_CAPI_LEN], dmcrypt_tmp[MAX_CAPI_LEN*2],
276 capi[MAX_CAPI_LEN+1];
284 if (strncmp(c_dm, "capi:", 4)) {
285 if (!(*org_c = strdup(c_dm)))
288 if (!(*org_i = strdup(i_dm))) {
298 /* modes with capi: prefix */
299 i = sscanf(c_dm, "capi:%" MAX_CAPI_LEN_STR "[^-]-%" MAX_CAPI_ONE_LEN_STR "s", tmp, iv);
307 if (tmp[len-1] == ')')
310 if (sscanf(tmp, "rfc4309(%" MAX_CAPI_LEN_STR "s", capi) == 1) {
311 if (!(*org_i = strdup("aead")))
313 } else if (sscanf(tmp, "rfc7539(%" MAX_CAPI_LEN_STR "[^,],%" MAX_CAPI_ONE_LEN_STR "s", capi, auth) == 2) {
314 if (!(*org_i = strdup(auth)))
316 } else if (sscanf(tmp, "authenc(%" MAX_CAPI_ONE_LEN_STR "[^,],%" MAX_CAPI_LEN_STR "s", auth, capi) == 2) {
317 if (!(*org_i = strdup(auth)))
321 if (!(*org_i = strdup(i_dm)))
325 memset(capi, 0, sizeof(capi));
326 strncpy(capi, tmp, sizeof(capi)-1);
329 i = sscanf(capi, "%" MAX_CAPI_ONE_LEN_STR "[^(](%" MAX_CAPI_ONE_LEN_STR "[^)])", mode, cipher);
331 i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s-%s", cipher, mode, iv);
333 i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s", capi, iv);
334 if (i < 0 || (size_t)i >= sizeof(dmcrypt_tmp)) {
340 if (!(*org_c = strdup(dmcrypt_tmp))) {