2 * utils_crypt - cipher utilities for cryptsetup
4 * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2009-2021 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.
29 #include "libcryptsetup.h"
30 #include "utils_crypt.h"
32 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
35 if (!s || !cipher || !cipher_mode)
38 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
39 cipher, cipher_mode) == 2) {
40 if (!strcmp(cipher_mode, "plain"))
41 strcpy(cipher_mode, "cbc-plain");
43 char *tmp = strchr(cipher, ':');
44 *key_nums = tmp ? atoi(++tmp) : 1;
52 /* Short version for "empty" cipher */
53 if (!strcmp(s, "null") || !strcmp(s, "cipher_null")) {
54 strcpy(cipher, "cipher_null");
55 strcpy(cipher_mode, "ecb");
61 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
62 strcpy(cipher_mode, "cbc-plain");
71 int crypt_parse_hash_integrity_mode(const char *s, char *integrity)
73 char mode[MAX_CIPHER_LEN], hash[MAX_CIPHER_LEN];
76 if (!s || !integrity || strchr(s, '(') || strchr(s, ')'))
79 r = sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", mode, hash);
80 if (r == 2 && !isdigit(hash[0]))
81 r = snprintf(integrity, MAX_CIPHER_LEN, "%s(%s)", mode, hash);
83 r = snprintf(integrity, MAX_CIPHER_LEN, "%s-%s", mode, hash);
85 r = snprintf(integrity, MAX_CIPHER_LEN, "%s", mode);
89 if (r < 0 || r >= MAX_CIPHER_LEN)
95 int crypt_parse_integrity_mode(const char *s, char *integrity,
96 int *integrity_key_size)
100 if (!s || !integrity)
103 // FIXME: do not hardcode it here
106 if (!strcmp(s, "aead") ||
107 !strcmp(s, "poly1305") ||
108 !strcmp(s, "none")) {
109 strncpy(integrity, s, MAX_CIPHER_LEN);
111 } else if (!strcmp(s, "hmac-sha1")) {
112 strncpy(integrity, "hmac(sha1)", MAX_CIPHER_LEN);
114 } else if (!strcmp(s, "hmac-sha256")) {
115 strncpy(integrity, "hmac(sha256)", MAX_CIPHER_LEN);
117 } else if (!strcmp(s, "hmac-sha512")) {
119 strncpy(integrity, "hmac(sha512)", MAX_CIPHER_LEN);
120 } else if (!strcmp(s, "cmac-aes")) {
122 strncpy(integrity, "cmac(aes)", MAX_CIPHER_LEN);
126 if (integrity_key_size)
127 *integrity_key_size = ks;
132 int crypt_parse_pbkdf(const char *s, const char **pbkdf)
134 const char *tmp = NULL;
139 if (!strcasecmp(s, CRYPT_KDF_PBKDF2))
140 tmp = CRYPT_KDF_PBKDF2;
141 else if (!strcasecmp(s, CRYPT_KDF_ARGON2I))
142 tmp = CRYPT_KDF_ARGON2I;
143 else if (!strcasecmp(s, CRYPT_KDF_ARGON2ID))
144 tmp = CRYPT_KDF_ARGON2ID;
155 ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc)
157 char buf[3] = "xx\0", *endp, *bytes;
165 bytes = safe_alloc ? crypt_safe_alloc(len) : malloc(len);
169 for (i = 0; i < len; i++) {
170 memcpy(buf, &hex[i * 2], 2);
171 bytes[i] = strtoul(buf, &endp, 16);
172 if (endp != &buf[2]) {
173 safe_alloc ? crypt_safe_free(bytes) : free(bytes);
181 bool crypt_is_cipher_null(const char *cipher_spec)
185 return (strstr(cipher_spec, "cipher_null") || !strcmp(cipher_spec, "null"));