/src/cryptsetup/lib/utils_crypt.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * utils_crypt - cipher utilities for cryptsetup |
4 | | * |
5 | | * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org> |
6 | | * Copyright (C) 2009-2026 Red Hat, Inc. All rights reserved. |
7 | | * Copyright (C) 2009-2026 Milan Broz |
8 | | */ |
9 | | |
10 | | #include <stdlib.h> |
11 | | #include <stdio.h> |
12 | | #include <string.h> |
13 | | #include <strings.h> |
14 | | #include <unistd.h> |
15 | | #include <ctype.h> |
16 | | #include <errno.h> |
17 | | |
18 | | #include "libcryptsetup.h" |
19 | | #include "utils_crypt.h" |
20 | | |
21 | | int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums, |
22 | | char *cipher_mode) |
23 | 0 | { |
24 | 0 | if (!s || !cipher || !cipher_mode) |
25 | 0 | return -EINVAL; |
26 | | |
27 | 0 | if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", |
28 | 0 | cipher, cipher_mode) == 2) { |
29 | 0 | if (!strncmp(cipher, "capi:", 5)) { |
30 | | /* CAPI must not use internal cipher driver names with dash */ |
31 | 0 | if (strchr(cipher_mode, ')')) |
32 | 0 | return -EINVAL; |
33 | 0 | if (key_nums) |
34 | 0 | *key_nums = 1; |
35 | 0 | return 0; |
36 | 0 | } |
37 | 0 | if (!strcmp(cipher_mode, "plain")) |
38 | 0 | strcpy(cipher_mode, "cbc-plain"); |
39 | 0 | if (key_nums) { |
40 | 0 | char *tmp = strchr(cipher, ':'); |
41 | 0 | *key_nums = tmp ? atoi(++tmp) : 1; |
42 | 0 | if (!*key_nums) |
43 | 0 | return -EINVAL; |
44 | 0 | } |
45 | | |
46 | 0 | return 0; |
47 | 0 | } |
48 | | |
49 | | /* Short version for "empty" cipher */ |
50 | 0 | if (!strcmp(s, "null") || !strcmp(s, "cipher_null")) { |
51 | 0 | strcpy(cipher, "cipher_null"); |
52 | 0 | strcpy(cipher_mode, "ecb"); |
53 | 0 | if (key_nums) |
54 | 0 | *key_nums = 0; |
55 | 0 | return 0; |
56 | 0 | } |
57 | | |
58 | 0 | if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) { |
59 | 0 | if (!strncmp(cipher, "capi:", 5)) |
60 | 0 | strcpy(cipher_mode, ""); |
61 | 0 | else |
62 | 0 | strcpy(cipher_mode, "cbc-plain"); |
63 | 0 | if (key_nums) |
64 | 0 | *key_nums = 1; |
65 | 0 | return 0; |
66 | 0 | } |
67 | | |
68 | 0 | return -EINVAL; |
69 | 0 | } |
70 | | |
71 | | int crypt_parse_hash_integrity_mode(const char *s, char *integrity) |
72 | 0 | { |
73 | 0 | char mode[MAX_CIPHER_LEN], hash[MAX_CIPHER_LEN]; |
74 | 0 | int r; |
75 | |
|
76 | 0 | if (!s || !integrity || strchr(s, '(') || strchr(s, ')')) |
77 | 0 | return -EINVAL; |
78 | | |
79 | 0 | r = sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", mode, hash); |
80 | 0 | if (r == 2 && !isdigit(hash[0])) |
81 | 0 | r = snprintf(integrity, MAX_CIPHER_LEN, "%s(%s)", mode, hash); |
82 | 0 | else if (r == 2) |
83 | 0 | r = snprintf(integrity, MAX_CIPHER_LEN, "%s-%s", mode, hash); |
84 | 0 | else if (r == 1) |
85 | 0 | r = snprintf(integrity, MAX_CIPHER_LEN, "%s", mode); |
86 | 0 | else |
87 | 0 | return -EINVAL; |
88 | | |
89 | 0 | if (r < 0 || r >= MAX_CIPHER_LEN) |
90 | 0 | return -EINVAL; |
91 | | |
92 | 0 | return 0; |
93 | 0 | } |
94 | | |
95 | | int crypt_parse_integrity_mode(const char *s, char *integrity, |
96 | | int *integrity_key_size, int required_key_size) |
97 | 0 | { |
98 | 0 | int ks = 0, r = 0; |
99 | |
|
100 | 0 | if (!s || !integrity) |
101 | 0 | return -EINVAL; |
102 | | |
103 | | /* AEAD modes */ |
104 | 0 | if (!strcmp(s, "aead") || |
105 | 0 | !strcmp(s, "poly1305") || |
106 | 0 | !strcmp(s, "none")) { |
107 | 0 | strncpy(integrity, s, MAX_CIPHER_LEN); |
108 | 0 | ks = 0; |
109 | 0 | if (required_key_size != ks) |
110 | 0 | r = -EINVAL; |
111 | 0 | } else if (!strcmp(s, "hmac-sha1")) { |
112 | 0 | strncpy(integrity, "hmac(sha1)", MAX_CIPHER_LEN); |
113 | 0 | ks = required_key_size ?: 20; |
114 | 0 | } else if (!strcmp(s, "hmac-sha256")) { |
115 | 0 | strncpy(integrity, "hmac(sha256)", MAX_CIPHER_LEN); |
116 | 0 | ks = required_key_size ?: 32; |
117 | 0 | } else if (!strcmp(s, "hmac-sha512")) { |
118 | 0 | strncpy(integrity, "hmac(sha512)", MAX_CIPHER_LEN); |
119 | 0 | ks = required_key_size ?: 64; |
120 | 0 | } else if (!strcmp(s, "phmac-sha1")) { |
121 | 0 | strncpy(integrity, "phmac(sha1)", MAX_CIPHER_LEN); |
122 | 0 | ks = required_key_size; |
123 | 0 | if (!required_key_size) |
124 | 0 | r = -EINVAL; |
125 | 0 | } else if (!strcmp(s, "phmac-sha256")) { |
126 | 0 | strncpy(integrity, "phmac(sha256)", MAX_CIPHER_LEN); |
127 | 0 | ks = required_key_size; |
128 | 0 | if (!required_key_size) |
129 | 0 | r = -EINVAL; |
130 | 0 | } else if (!strcmp(s, "phmac-sha512")) { |
131 | 0 | strncpy(integrity, "phmac(sha512)", MAX_CIPHER_LEN); |
132 | 0 | ks = required_key_size; |
133 | 0 | if (!required_key_size) |
134 | 0 | r = -EINVAL; |
135 | 0 | } else if (!strcmp(s, "cmac-aes")) { |
136 | 0 | strncpy(integrity, "cmac(aes)", MAX_CIPHER_LEN); |
137 | 0 | ks = 16; |
138 | 0 | if (required_key_size && required_key_size != ks) |
139 | 0 | r = -EINVAL; |
140 | 0 | } else |
141 | 0 | r = -EINVAL; |
142 | |
|
143 | 0 | if (integrity_key_size) |
144 | 0 | *integrity_key_size = ks; |
145 | |
|
146 | 0 | return r; |
147 | 0 | } |
148 | | |
149 | | int crypt_parse_pbkdf(const char *s, const char **pbkdf) |
150 | 3.75k | { |
151 | 3.75k | const char *tmp = NULL; |
152 | | |
153 | 3.75k | if (!s) |
154 | 0 | return -EINVAL; |
155 | | |
156 | 3.75k | if (!strcasecmp(s, CRYPT_KDF_PBKDF2)) |
157 | 1.72k | tmp = CRYPT_KDF_PBKDF2; |
158 | 2.03k | else if (!strcasecmp(s, CRYPT_KDF_ARGON2I)) |
159 | 0 | tmp = CRYPT_KDF_ARGON2I; |
160 | 2.03k | else if (!strcasecmp(s, CRYPT_KDF_ARGON2ID)) |
161 | 2.03k | tmp = CRYPT_KDF_ARGON2ID; |
162 | | |
163 | 3.75k | if (!tmp) |
164 | 0 | return -EINVAL; |
165 | | |
166 | 3.75k | if (pbkdf) |
167 | 3.75k | *pbkdf = tmp; |
168 | | |
169 | 3.75k | return 0; |
170 | 3.75k | } |
171 | | |
172 | | /* |
173 | | * Thanks Mikulas Patocka for these two char converting functions. |
174 | | * |
175 | | * This function is used to load cryptographic keys, so it is coded in such a |
176 | | * way that there are no conditions or memory accesses that depend on data. |
177 | | * |
178 | | * Explanation of the logic: |
179 | | * (ch - '9' - 1) is negative if ch <= '9' |
180 | | * ('0' - 1 - ch) is negative if ch >= '0' |
181 | | * we "and" these two values, so the result is negative if ch is in the range |
182 | | * '0' ... '9' |
183 | | * we are only interested in the sign, so we do a shift ">> 8"; note that right |
184 | | * shift of a negative value is implementation-defined, so we cast the |
185 | | * value to (unsigned) before the shift --- we have 0xffffff if ch is in |
186 | | * the range '0' ... '9', 0 otherwise |
187 | | * we "and" this value with (ch - '0' + 1) --- we have a value 1 ... 10 if ch is |
188 | | * in the range '0' ... '9', 0 otherwise |
189 | | * we add this value to -1 --- we have a value 0 ... 9 if ch is in the range '0' |
190 | | * ... '9', -1 otherwise |
191 | | * the next line is similar to the previous one, but we need to decode both |
192 | | * uppercase and lowercase letters, so we use (ch & 0xdf), which converts |
193 | | * lowercase to uppercase |
194 | | */ |
195 | | static int hex_to_bin(unsigned char ch) |
196 | 0 | { |
197 | 0 | unsigned char cu = ch & 0xdf; |
198 | 0 | return -1 + |
199 | 0 | ((ch - '0' + 1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) + |
200 | 0 | ((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8); |
201 | 0 | } |
202 | | |
203 | | static char hex2asc(unsigned char c) |
204 | 0 | { |
205 | 0 | return c + '0' + ((unsigned)(9 - c) >> 4 & 0x27); |
206 | 0 | } |
207 | | |
208 | | ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc) |
209 | 0 | { |
210 | 0 | char *bytes; |
211 | 0 | size_t i, len; |
212 | 0 | int bl, bh; |
213 | |
|
214 | 0 | if (!hex || !result) |
215 | 0 | return -EINVAL; |
216 | | |
217 | 0 | len = strlen(hex); |
218 | 0 | if (len % 2) |
219 | 0 | return -EINVAL; |
220 | 0 | len /= 2; |
221 | |
|
222 | 0 | bytes = safe_alloc ? crypt_safe_alloc(len) : malloc(len); |
223 | 0 | if (!bytes) |
224 | 0 | return -ENOMEM; |
225 | | |
226 | 0 | for (i = 0; i < len; i++) { |
227 | 0 | bh = hex_to_bin(hex[i * 2]); |
228 | 0 | bl = hex_to_bin(hex[i * 2 + 1]); |
229 | 0 | if (bh == -1 || bl == -1) { |
230 | 0 | safe_alloc ? crypt_safe_free(bytes) : free(bytes); |
231 | 0 | return -EINVAL; |
232 | 0 | } |
233 | 0 | bytes[i] = (bh << 4) | bl; |
234 | 0 | } |
235 | 0 | *result = bytes; |
236 | 0 | return i; |
237 | 0 | } |
238 | | |
239 | | char *crypt_bytes_to_hex(size_t size, const char *bytes) |
240 | 0 | { |
241 | 0 | unsigned i; |
242 | 0 | char *hex; |
243 | |
|
244 | 0 | if (size && !bytes) |
245 | 0 | return NULL; |
246 | | |
247 | | /* Alloc adds trailing \0 */ |
248 | 0 | if (size == 0) |
249 | 0 | hex = crypt_safe_alloc(2); |
250 | 0 | else |
251 | 0 | hex = crypt_safe_alloc(size * 2 + 1); |
252 | 0 | if (!hex) |
253 | 0 | return NULL; |
254 | | |
255 | 0 | if (size == 0) |
256 | 0 | hex[0] = '-'; |
257 | 0 | else for (i = 0; i < size; i++) { |
258 | 0 | hex[i * 2] = hex2asc((const unsigned char)bytes[i] >> 4); |
259 | 0 | hex[i * 2 + 1] = hex2asc((const unsigned char)bytes[i] & 0xf); |
260 | 0 | } |
261 | |
|
262 | 0 | return hex; |
263 | 0 | } |
264 | | |
265 | | void crypt_log_hex(struct crypt_device *cd, |
266 | | const char *bytes, size_t size, |
267 | | const char *sep, int numwrap, const char *wrapsep) |
268 | 0 | { |
269 | 0 | unsigned i; |
270 | |
|
271 | 0 | for (i = 0; i < size; i++) { |
272 | 0 | if (wrapsep && numwrap && i && !(i % numwrap)) |
273 | 0 | crypt_logf(cd, CRYPT_LOG_NORMAL, wrapsep); |
274 | 0 | crypt_logf(cd, CRYPT_LOG_NORMAL, "%c%c%s", |
275 | 0 | hex2asc((const unsigned char)bytes[i] >> 4), |
276 | 0 | hex2asc((const unsigned char)bytes[i] & 0xf), sep); |
277 | 0 | } |
278 | 0 | } |
279 | | |
280 | | bool crypt_is_cipher_null(const char *cipher_spec) |
281 | 0 | { |
282 | 0 | if (!cipher_spec) |
283 | 0 | return false; |
284 | 0 | return (strstr(cipher_spec, "cipher_null") || !strcmp(cipher_spec, "null")); |
285 | 0 | } |