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