/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-2025 Red Hat, Inc. All rights reserved. |
7 | | * Copyright (C) 2009-2025 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 | | #define MAX_CAPI_LEN_STR "143" /* for sscanf of crypto API string + 16 + \0 */ |
22 | | |
23 | | int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums, |
24 | | char *cipher_mode) |
25 | 0 | { |
26 | 0 | if (!s || !cipher || !cipher_mode) |
27 | 0 | return -EINVAL; |
28 | | |
29 | 0 | if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", |
30 | 0 | cipher, cipher_mode) == 2) { |
31 | 0 | if (!strncmp(cipher, "capi:", 5)) { |
32 | | /* CAPI must not use internal cipher driver names with dash */ |
33 | 0 | if (strchr(cipher_mode, ')')) |
34 | 0 | return -EINVAL; |
35 | 0 | if (key_nums) |
36 | 0 | *key_nums = 1; |
37 | 0 | return 0; |
38 | 0 | } |
39 | 0 | if (!strcmp(cipher_mode, "plain")) |
40 | 0 | strcpy(cipher_mode, "cbc-plain"); |
41 | 0 | if (key_nums) { |
42 | 0 | char *tmp = strchr(cipher, ':'); |
43 | 0 | *key_nums = tmp ? atoi(++tmp) : 1; |
44 | 0 | if (!*key_nums) |
45 | 0 | return -EINVAL; |
46 | 0 | } |
47 | | |
48 | 0 | return 0; |
49 | 0 | } |
50 | | |
51 | | /* Short version for "empty" cipher */ |
52 | 0 | if (!strcmp(s, "null") || !strcmp(s, "cipher_null")) { |
53 | 0 | strcpy(cipher, "cipher_null"); |
54 | 0 | strcpy(cipher_mode, "ecb"); |
55 | 0 | if (key_nums) |
56 | 0 | *key_nums = 0; |
57 | 0 | return 0; |
58 | 0 | } |
59 | | |
60 | 0 | if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) { |
61 | 0 | if (!strncmp(cipher, "capi:", 5)) |
62 | 0 | strcpy(cipher_mode, ""); |
63 | 0 | else |
64 | 0 | strcpy(cipher_mode, "cbc-plain"); |
65 | 0 | if (key_nums) |
66 | 0 | *key_nums = 1; |
67 | 0 | return 0; |
68 | 0 | } |
69 | | |
70 | 0 | return -EINVAL; |
71 | 0 | } |
72 | | |
73 | | int crypt_parse_hash_integrity_mode(const char *s, char *integrity) |
74 | 0 | { |
75 | 0 | char mode[MAX_CIPHER_LEN], hash[MAX_CIPHER_LEN]; |
76 | 0 | int r; |
77 | |
|
78 | 0 | if (!s || !integrity || strchr(s, '(') || strchr(s, ')')) |
79 | 0 | return -EINVAL; |
80 | | |
81 | 0 | r = sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", mode, hash); |
82 | 0 | if (r == 2 && !isdigit(hash[0])) |
83 | 0 | r = snprintf(integrity, MAX_CIPHER_LEN, "%s(%s)", mode, hash); |
84 | 0 | else if (r == 2) |
85 | 0 | r = snprintf(integrity, MAX_CIPHER_LEN, "%s-%s", mode, hash); |
86 | 0 | else if (r == 1) |
87 | 0 | r = snprintf(integrity, MAX_CIPHER_LEN, "%s", mode); |
88 | 0 | else |
89 | 0 | return -EINVAL; |
90 | | |
91 | 0 | if (r < 0 || r >= MAX_CIPHER_LEN) |
92 | 0 | return -EINVAL; |
93 | | |
94 | 0 | return 0; |
95 | 0 | } |
96 | | |
97 | | int crypt_parse_integrity_mode(const char *s, char *integrity, |
98 | | int *integrity_key_size, int required_key_size) |
99 | 0 | { |
100 | 0 | int ks = 0, r = 0; |
101 | |
|
102 | 0 | if (!s || !integrity) |
103 | 0 | return -EINVAL; |
104 | | |
105 | | /* AEAD modes */ |
106 | 0 | if (!strcmp(s, "aead") || |
107 | 0 | !strcmp(s, "poly1305") || |
108 | 0 | !strcmp(s, "none")) { |
109 | 0 | strncpy(integrity, s, MAX_CIPHER_LEN); |
110 | 0 | ks = 0; |
111 | 0 | if (required_key_size != ks) |
112 | 0 | r = -EINVAL; |
113 | 0 | } else if (!strcmp(s, "hmac-sha1")) { |
114 | 0 | strncpy(integrity, "hmac(sha1)", MAX_CIPHER_LEN); |
115 | 0 | ks = required_key_size ?: 20; |
116 | 0 | } else if (!strcmp(s, "hmac-sha256")) { |
117 | 0 | strncpy(integrity, "hmac(sha256)", MAX_CIPHER_LEN); |
118 | 0 | ks = required_key_size ?: 32; |
119 | 0 | } else if (!strcmp(s, "hmac-sha512")) { |
120 | 0 | strncpy(integrity, "hmac(sha512)", MAX_CIPHER_LEN); |
121 | 0 | ks = required_key_size ?: 64; |
122 | 0 | } else if (!strcmp(s, "phmac-sha1")) { |
123 | 0 | strncpy(integrity, "phmac(sha1)", MAX_CIPHER_LEN); |
124 | 0 | ks = required_key_size; |
125 | 0 | if (!required_key_size) |
126 | 0 | r = -EINVAL; |
127 | 0 | } else if (!strcmp(s, "phmac-sha256")) { |
128 | 0 | strncpy(integrity, "phmac(sha256)", MAX_CIPHER_LEN); |
129 | 0 | ks = required_key_size; |
130 | 0 | if (!required_key_size) |
131 | 0 | r = -EINVAL; |
132 | 0 | } else if (!strcmp(s, "phmac-sha512")) { |
133 | 0 | strncpy(integrity, "phmac(sha512)", MAX_CIPHER_LEN); |
134 | 0 | ks = required_key_size; |
135 | 0 | if (!required_key_size) |
136 | 0 | r = -EINVAL; |
137 | 0 | } else if (!strcmp(s, "cmac-aes")) { |
138 | 0 | strncpy(integrity, "cmac(aes)", MAX_CIPHER_LEN); |
139 | 0 | ks = 16; |
140 | 0 | if (required_key_size && required_key_size != ks) |
141 | 0 | r = -EINVAL; |
142 | 0 | } else |
143 | 0 | r = -EINVAL; |
144 | |
|
145 | 0 | if (integrity_key_size) |
146 | 0 | *integrity_key_size = ks; |
147 | |
|
148 | 0 | return r; |
149 | 0 | } |
150 | | |
151 | | int crypt_parse_pbkdf(const char *s, const char **pbkdf) |
152 | 2.19k | { |
153 | 2.19k | const char *tmp = NULL; |
154 | | |
155 | 2.19k | if (!s) |
156 | 0 | return -EINVAL; |
157 | | |
158 | 2.19k | if (!strcasecmp(s, CRYPT_KDF_PBKDF2)) |
159 | 0 | tmp = CRYPT_KDF_PBKDF2; |
160 | 2.19k | else if (!strcasecmp(s, CRYPT_KDF_ARGON2I)) |
161 | 0 | tmp = CRYPT_KDF_ARGON2I; |
162 | 2.19k | else if (!strcasecmp(s, CRYPT_KDF_ARGON2ID)) |
163 | 2.19k | tmp = CRYPT_KDF_ARGON2ID; |
164 | | |
165 | 2.19k | if (!tmp) |
166 | 0 | return -EINVAL; |
167 | | |
168 | 2.19k | if (pbkdf) |
169 | 2.19k | *pbkdf = tmp; |
170 | | |
171 | 2.19k | return 0; |
172 | 2.19k | } |
173 | | |
174 | | /* |
175 | | * Thanks Mikulas Patocka for these two char converting functions. |
176 | | * |
177 | | * This function is used to load cryptographic keys, so it is coded in such a |
178 | | * way that there are no conditions or memory accesses that depend on data. |
179 | | * |
180 | | * Explanation of the logic: |
181 | | * (ch - '9' - 1) is negative if ch <= '9' |
182 | | * ('0' - 1 - ch) is negative if ch >= '0' |
183 | | * we "and" these two values, so the result is negative if ch is in the range |
184 | | * '0' ... '9' |
185 | | * we are only interested in the sign, so we do a shift ">> 8"; note that right |
186 | | * shift of a negative value is implementation-defined, so we cast the |
187 | | * value to (unsigned) before the shift --- we have 0xffffff if ch is in |
188 | | * the range '0' ... '9', 0 otherwise |
189 | | * we "and" this value with (ch - '0' + 1) --- we have a value 1 ... 10 if ch is |
190 | | * in the range '0' ... '9', 0 otherwise |
191 | | * we add this value to -1 --- we have a value 0 ... 9 if ch is in the range '0' |
192 | | * ... '9', -1 otherwise |
193 | | * the next line is similar to the previous one, but we need to decode both |
194 | | * uppercase and lowercase letters, so we use (ch & 0xdf), which converts |
195 | | * lowercase to uppercase |
196 | | */ |
197 | | static int hex_to_bin(unsigned char ch) |
198 | 0 | { |
199 | 0 | unsigned char cu = ch & 0xdf; |
200 | 0 | return -1 + |
201 | 0 | ((ch - '0' + 1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) + |
202 | 0 | ((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8); |
203 | 0 | } |
204 | | |
205 | | static char hex2asc(unsigned char c) |
206 | 0 | { |
207 | 0 | return c + '0' + ((unsigned)(9 - c) >> 4 & 0x27); |
208 | 0 | } |
209 | | |
210 | | ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc) |
211 | 0 | { |
212 | 0 | char *bytes; |
213 | 0 | size_t i, len; |
214 | 0 | int bl, bh; |
215 | |
|
216 | 0 | if (!hex || !result) |
217 | 0 | return -EINVAL; |
218 | | |
219 | 0 | len = strlen(hex); |
220 | 0 | if (len % 2) |
221 | 0 | return -EINVAL; |
222 | 0 | len /= 2; |
223 | |
|
224 | 0 | bytes = safe_alloc ? crypt_safe_alloc(len) : malloc(len); |
225 | 0 | if (!bytes) |
226 | 0 | return -ENOMEM; |
227 | | |
228 | 0 | for (i = 0; i < len; i++) { |
229 | 0 | bh = hex_to_bin(hex[i * 2]); |
230 | 0 | bl = hex_to_bin(hex[i * 2 + 1]); |
231 | 0 | if (bh == -1 || bl == -1) { |
232 | 0 | safe_alloc ? crypt_safe_free(bytes) : free(bytes); |
233 | 0 | return -EINVAL; |
234 | 0 | } |
235 | 0 | bytes[i] = (bh << 4) | bl; |
236 | 0 | } |
237 | 0 | *result = bytes; |
238 | 0 | return i; |
239 | 0 | } |
240 | | |
241 | | char *crypt_bytes_to_hex(size_t size, const char *bytes) |
242 | 0 | { |
243 | 0 | unsigned i; |
244 | 0 | char *hex; |
245 | |
|
246 | 0 | if (size && !bytes) |
247 | 0 | return NULL; |
248 | | |
249 | | /* Alloc adds trailing \0 */ |
250 | 0 | if (size == 0) |
251 | 0 | hex = crypt_safe_alloc(2); |
252 | 0 | else |
253 | 0 | hex = crypt_safe_alloc(size * 2 + 1); |
254 | 0 | if (!hex) |
255 | 0 | return NULL; |
256 | | |
257 | 0 | if (size == 0) |
258 | 0 | hex[0] = '-'; |
259 | 0 | else for (i = 0; i < size; i++) { |
260 | 0 | hex[i * 2] = hex2asc((const unsigned char)bytes[i] >> 4); |
261 | 0 | hex[i * 2 + 1] = hex2asc((const unsigned char)bytes[i] & 0xf); |
262 | 0 | } |
263 | |
|
264 | 0 | return hex; |
265 | 0 | } |
266 | | |
267 | | void crypt_log_hex(struct crypt_device *cd, |
268 | | const char *bytes, size_t size, |
269 | | const char *sep, int numwrap, const char *wrapsep) |
270 | 0 | { |
271 | 0 | unsigned i; |
272 | |
|
273 | 0 | for (i = 0; i < size; i++) { |
274 | 0 | if (wrapsep && numwrap && i && !(i % numwrap)) |
275 | 0 | crypt_logf(cd, CRYPT_LOG_NORMAL, wrapsep); |
276 | 0 | crypt_logf(cd, CRYPT_LOG_NORMAL, "%c%c%s", |
277 | 0 | hex2asc((const unsigned char)bytes[i] >> 4), |
278 | 0 | hex2asc((const unsigned char)bytes[i] & 0xf), sep); |
279 | 0 | } |
280 | 0 | } |
281 | | |
282 | | bool crypt_is_cipher_null(const char *cipher_spec) |
283 | 0 | { |
284 | 0 | if (!cipher_spec) |
285 | 0 | return false; |
286 | 0 | return (strstr(cipher_spec, "cipher_null") || !strcmp(cipher_spec, "null")); |
287 | 0 | } |
288 | | |
289 | | int crypt_capi_to_cipher(char **org_c, char **org_i, const char *c_dm, const char *i_dm) |
290 | 0 | { |
291 | 0 | char cipher[MAX_CAPI_ONE_LEN], mode[MAX_CAPI_ONE_LEN], iv[MAX_CAPI_ONE_LEN], |
292 | 0 | auth[MAX_CAPI_ONE_LEN], tmp[MAX_CAPI_LEN], dmcrypt_tmp[MAX_CAPI_LEN*2], |
293 | 0 | capi[MAX_CAPI_LEN+1]; |
294 | 0 | size_t len; |
295 | 0 | int i; |
296 | |
|
297 | 0 | if (!c_dm) |
298 | 0 | return -EINVAL; |
299 | | |
300 | | /* legacy mode */ |
301 | 0 | if (strncmp(c_dm, "capi:", 4)) { |
302 | 0 | if (!(*org_c = strdup(c_dm))) |
303 | 0 | return -ENOMEM; |
304 | 0 | if (i_dm) { |
305 | 0 | if (!(*org_i = strdup(i_dm))) { |
306 | 0 | free(*org_c); |
307 | 0 | *org_c = NULL; |
308 | 0 | return -ENOMEM; |
309 | 0 | } |
310 | 0 | } else |
311 | 0 | *org_i = NULL; |
312 | 0 | return 0; |
313 | 0 | } |
314 | | |
315 | | /* modes with capi: prefix */ |
316 | 0 | i = sscanf(c_dm, "capi:%" MAX_CAPI_LEN_STR "[^-]-%" MAX_CAPI_ONE_LEN_STR "s", tmp, iv); |
317 | 0 | if (i != 2) |
318 | 0 | return -EINVAL; |
319 | | |
320 | | /* non-cryptsetup compatible mode (generic driver with dash?) */ |
321 | 0 | if (strrchr(iv, ')')) { |
322 | 0 | if (i_dm) |
323 | 0 | return -EINVAL; |
324 | 0 | if (!(*org_c = strdup(c_dm))) |
325 | 0 | return -ENOMEM; |
326 | 0 | return 0; |
327 | 0 | } |
328 | | |
329 | 0 | len = strlen(tmp); |
330 | 0 | if (len < 2) |
331 | 0 | return -EINVAL; |
332 | | |
333 | 0 | if (tmp[len-1] == ')') |
334 | 0 | tmp[len-1] = '\0'; |
335 | |
|
336 | 0 | if (sscanf(tmp, "rfc4309(%" MAX_CAPI_LEN_STR "s", capi) == 1) { |
337 | 0 | if (!(*org_i = strdup("aead"))) |
338 | 0 | return -ENOMEM; |
339 | 0 | } else if (sscanf(tmp, "rfc7539(%" MAX_CAPI_LEN_STR "[^,],%" MAX_CAPI_ONE_LEN_STR "s", capi, auth) == 2) { |
340 | 0 | if (!(*org_i = strdup(auth))) |
341 | 0 | return -ENOMEM; |
342 | 0 | } else if (sscanf(tmp, "authenc(%" MAX_CAPI_ONE_LEN_STR "[^,],%" MAX_CAPI_LEN_STR "s", auth, capi) == 2) { |
343 | 0 | if (!(*org_i = strdup(auth))) |
344 | 0 | return -ENOMEM; |
345 | 0 | } else { |
346 | 0 | if (i_dm) { |
347 | 0 | if (!(*org_i = strdup(i_dm))) |
348 | 0 | return -ENOMEM; |
349 | 0 | } else |
350 | 0 | *org_i = NULL; |
351 | 0 | memset(capi, 0, sizeof(capi)); |
352 | 0 | strncpy(capi, tmp, sizeof(capi)-1); |
353 | 0 | } |
354 | | |
355 | 0 | i = sscanf(capi, "%" MAX_CAPI_ONE_LEN_STR "[^(](%" MAX_CAPI_ONE_LEN_STR "[^)])", mode, cipher); |
356 | 0 | if (i == 2) |
357 | 0 | i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s-%s", cipher, mode, iv); |
358 | 0 | else |
359 | 0 | i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s", capi, iv); |
360 | 0 | if (i < 0 || (size_t)i >= sizeof(dmcrypt_tmp)) { |
361 | 0 | free(*org_i); |
362 | 0 | *org_i = NULL; |
363 | 0 | return -EINVAL; |
364 | 0 | } |
365 | | |
366 | 0 | if (!(*org_c = strdup(dmcrypt_tmp))) { |
367 | 0 | free(*org_i); |
368 | 0 | *org_i = NULL; |
369 | 0 | return -ENOMEM; |
370 | 0 | } |
371 | | |
372 | 0 | return 0; |
373 | 0 | } |