Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / utils_crypt.c
1 /*
2  * utils_crypt - cipher utilities for cryptsetup
3  *
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
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include <errno.h>
30
31 #include "libcryptsetup.h"
32 #include "utils_crypt.h"
33
34 #define MAX_CAPI_LEN_STR "143" /* for sscanf of crypto API string + 16  + \0 */
35
36 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
37                               char *cipher_mode)
38 {
39         if (!s || !cipher || !cipher_mode)
40                 return -EINVAL;
41
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");
46                 if (key_nums) {
47                         char *tmp = strchr(cipher, ':');
48                         *key_nums = tmp ? atoi(++tmp) : 1;
49                         if (!*key_nums)
50                                 return -EINVAL;
51                 }
52
53                 return 0;
54         }
55
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");
60                 if (key_nums)
61                         *key_nums = 0;
62                 return 0;
63         }
64
65         if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
66                 strcpy(cipher_mode, "cbc-plain");
67                 if (key_nums)
68                         *key_nums = 1;
69                 return 0;
70         }
71
72         return -EINVAL;
73 }
74
75 int crypt_parse_hash_integrity_mode(const char *s, char *integrity)
76 {
77         char mode[MAX_CIPHER_LEN], hash[MAX_CIPHER_LEN];
78         int r;
79
80         if (!s || !integrity || strchr(s, '(') || strchr(s, ')'))
81                 return -EINVAL;
82
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);
86         else if (r == 2)
87                 r = snprintf(integrity, MAX_CIPHER_LEN, "%s-%s", mode, hash);
88         else if (r == 1)
89                 r = snprintf(integrity, MAX_CIPHER_LEN, "%s", mode);
90         else
91                 return -EINVAL;
92
93         if (r < 0 || r >= MAX_CIPHER_LEN)
94                 return -EINVAL;
95
96         return 0;
97 }
98
99 int crypt_parse_integrity_mode(const char *s, char *integrity,
100                                int *integrity_key_size)
101 {
102         int ks = 0, r = 0;
103
104         if (!s || !integrity)
105                 return -EINVAL;
106
107         /* AEAD modes */
108         if (!strcmp(s, "aead") ||
109             !strcmp(s, "poly1305") ||
110             !strcmp(s, "none")) {
111                 strncpy(integrity, s, MAX_CIPHER_LEN);
112                 ks = 0;
113         } else if (!strcmp(s, "hmac-sha1")) {
114                 strncpy(integrity, "hmac(sha1)", MAX_CIPHER_LEN);
115                 ks = 20;
116         } else if (!strcmp(s, "hmac-sha256")) {
117                 strncpy(integrity, "hmac(sha256)", MAX_CIPHER_LEN);
118                 ks = 32;
119         } else if (!strcmp(s, "hmac-sha512")) {
120                 ks = 64;
121                 strncpy(integrity, "hmac(sha512)", MAX_CIPHER_LEN);
122         } else if (!strcmp(s, "cmac-aes")) {
123                 ks = 16;
124                 strncpy(integrity, "cmac(aes)", MAX_CIPHER_LEN);
125         } else
126                 r = -EINVAL;
127
128         if (integrity_key_size)
129                 *integrity_key_size = ks;
130
131         return r;
132 }
133
134 int crypt_parse_pbkdf(const char *s, const char **pbkdf)
135 {
136         const char *tmp = NULL;
137
138         if (!s)
139                 return -EINVAL;
140
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;
147
148         if (!tmp)
149                 return -EINVAL;
150
151         if (pbkdf)
152                 *pbkdf = tmp;
153
154         return 0;
155 }
156
157 /*
158  * Thanks Mikulas Patocka for these two char converting functions.
159  *
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.
162  *
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
167  * '0' ... '9'
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
179  */
180 static int hex_to_bin(unsigned char ch)
181 {
182         unsigned char cu = ch & 0xdf;
183         return -1 +
184                 ((ch - '0' +  1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) +
185                 ((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8);
186 }
187
188 static char hex2asc(unsigned char c)
189 {
190         return c + '0' + ((unsigned)(9 - c) >> 4 & 0x27);
191 }
192
193 ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc)
194 {
195         char *bytes;
196         size_t i, len;
197         int bl, bh;
198
199         if (!hex || !result)
200                 return -EINVAL;
201
202         len = strlen(hex);
203         if (len % 2)
204                 return -EINVAL;
205         len /= 2;
206
207         bytes = safe_alloc ? crypt_safe_alloc(len) : malloc(len);
208         if (!bytes)
209                 return -ENOMEM;
210
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);
216                         return -EINVAL;
217                 }
218                 bytes[i] = (bh << 4) | bl;
219         }
220         *result = bytes;
221         return i;
222 }
223
224 char *crypt_bytes_to_hex(size_t size, const char *bytes)
225 {
226         unsigned i;
227         char *hex;
228
229         if (size && !bytes)
230                 return NULL;
231
232         /* Alloc adds trailing \0 */
233         if (size == 0)
234                 hex = crypt_safe_alloc(2);
235         else
236                 hex = crypt_safe_alloc(size * 2 + 1);
237         if (!hex)
238                 return NULL;
239
240         if (size == 0)
241                 hex[0] = '-';
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);
245         }
246
247         return hex;
248 }
249
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)
253 {
254         unsigned i;
255
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);
262         }
263 }
264
265 bool crypt_is_cipher_null(const char *cipher_spec)
266 {
267         if (!cipher_spec)
268                 return false;
269         return (strstr(cipher_spec, "cipher_null") || !strcmp(cipher_spec, "null"));
270 }
271
272 int crypt_capi_to_cipher(char **org_c, char **org_i, const char *c_dm, const char *i_dm)
273 {
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];
277         size_t len;
278         int i;
279
280         if (!c_dm)
281                 return -EINVAL;
282
283         /* legacy mode */
284         if (strncmp(c_dm, "capi:", 4)) {
285                 if (!(*org_c = strdup(c_dm)))
286                         return -ENOMEM;
287                 if (i_dm) {
288                         if (!(*org_i = strdup(i_dm))) {
289                                 free(*org_c);
290                                 *org_c = NULL;
291                                 return -ENOMEM;
292                         }
293                 } else
294                         *org_i = NULL;
295                 return 0;
296         }
297
298         /* modes with capi: prefix */
299         i = sscanf(c_dm, "capi:%" MAX_CAPI_LEN_STR "[^-]-%" MAX_CAPI_ONE_LEN_STR "s", tmp, iv);
300         if (i != 2)
301                 return -EINVAL;
302
303         len = strlen(tmp);
304         if (len < 2)
305                 return -EINVAL;
306
307         if (tmp[len-1] == ')')
308                 tmp[len-1] = '\0';
309
310         if (sscanf(tmp, "rfc4309(%" MAX_CAPI_LEN_STR "s", capi) == 1) {
311                 if (!(*org_i = strdup("aead")))
312                         return -ENOMEM;
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)))
315                         return -ENOMEM;
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)))
318                         return -ENOMEM;
319         } else {
320                 if (i_dm) {
321                         if (!(*org_i = strdup(i_dm)))
322                                 return -ENOMEM;
323                 } else
324                         *org_i = NULL;
325                 memset(capi, 0, sizeof(capi));
326                 strncpy(capi, tmp, sizeof(capi)-1);
327         }
328
329         i = sscanf(capi, "%" MAX_CAPI_ONE_LEN_STR "[^(](%" MAX_CAPI_ONE_LEN_STR "[^)])", mode, cipher);
330         if (i == 2)
331                 i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s-%s", cipher, mode, iv);
332         else
333                 i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s", capi, iv);
334         if (i < 0 || (size_t)i >= sizeof(dmcrypt_tmp)) {
335                 free(*org_i);
336                 *org_i = NULL;
337                 return -EINVAL;
338         }
339
340         if (!(*org_c = strdup(dmcrypt_tmp))) {
341                 free(*org_i);
342                 *org_i = NULL;
343                 return -ENOMEM;
344         }
345
346         return 0;
347 }