17dc6d8e243328151211cc65a7c21fd3310bc20a
[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-2020 Red Hat, Inc. All rights reserved.
6  * Copyright (C) 2009-2020 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 <errno.h>
27
28 #include "libcryptsetup.h"
29 #include "utils_crypt.h"
30
31 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
32                               char *cipher_mode)
33 {
34         if (!s || !cipher || !cipher_mode)
35                 return -EINVAL;
36
37         if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
38                    cipher, cipher_mode) == 2) {
39                 if (!strcmp(cipher_mode, "plain"))
40                         strcpy(cipher_mode, "cbc-plain");
41                 if (key_nums) {
42                         char *tmp = strchr(cipher, ':');
43                         *key_nums = tmp ? atoi(++tmp) : 1;
44                         if (!*key_nums)
45                                 return -EINVAL;
46                 }
47
48                 return 0;
49         }
50
51         /* Short version for "empty" cipher */
52         if (!strcmp(s, "null") || !strcmp(s, "cipher_null")) {
53                 strcpy(cipher, "cipher_null");
54                 strcpy(cipher_mode, "ecb");
55                 if (key_nums)
56                         *key_nums = 0;
57                 return 0;
58         }
59
60         if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
61                 strcpy(cipher_mode, "cbc-plain");
62                 if (key_nums)
63                         *key_nums = 1;
64                 return 0;
65         }
66
67         return -EINVAL;
68 }
69
70 int crypt_parse_hash_integrity_mode(const char *s, char *integrity)
71 {
72         char mode[MAX_CIPHER_LEN], hash[MAX_CIPHER_LEN];
73         int r;
74
75         if (!s || !integrity || strchr(s, '(') || strchr(s, ')'))
76                 return -EINVAL;
77
78         r = sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", mode, hash);
79         if (r == 2)
80                 r = snprintf(integrity, MAX_CIPHER_LEN, "%s(%s)", mode, hash);
81         else if (r == 1)
82                 r = snprintf(integrity, MAX_CIPHER_LEN, "%s", mode);
83         else
84                 return -EINVAL;
85
86         if (r < 0 || r == MAX_CIPHER_LEN)
87                 return -EINVAL;
88
89         return 0;
90 }
91
92 int crypt_parse_integrity_mode(const char *s, char *integrity,
93                                int *integrity_key_size)
94 {
95         int ks = 0, r = 0;
96
97         if (!s || !integrity)
98                 return -EINVAL;
99
100         // FIXME: do not hardcode it here
101
102         /* AEAD modes */
103         if (!strcmp(s, "aead") ||
104             !strcmp(s, "poly1305") ||
105             !strcmp(s, "none")) {
106                 strncpy(integrity, s, MAX_CIPHER_LEN);
107                 ks = 0;
108         } else if (!strcmp(s, "hmac-sha1")) {
109                 strncpy(integrity, "hmac(sha1)", MAX_CIPHER_LEN);
110                 ks = 20;
111         } else if (!strcmp(s, "hmac-sha256")) {
112                 strncpy(integrity, "hmac(sha256)", MAX_CIPHER_LEN);
113                 ks = 32;
114         } else if (!strcmp(s, "hmac-sha512")) {
115                 ks = 64;
116                 strncpy(integrity, "hmac(sha512)", MAX_CIPHER_LEN);
117         } else if (!strcmp(s, "cmac-aes")) {
118                 ks = 16;
119                 strncpy(integrity, "cmac(aes)", MAX_CIPHER_LEN);
120         } else
121                 r = -EINVAL;
122
123         if (integrity_key_size)
124                 *integrity_key_size = ks;
125
126         return r;
127 }
128
129 int crypt_parse_pbkdf(const char *s, const char **pbkdf)
130 {
131         const char *tmp = NULL;
132
133         if (!s)
134                 return -EINVAL;
135
136         if (!strcasecmp(s, CRYPT_KDF_PBKDF2))
137                 tmp = CRYPT_KDF_PBKDF2;
138         else if (!strcasecmp(s, CRYPT_KDF_ARGON2I))
139                 tmp = CRYPT_KDF_ARGON2I;
140         else if (!strcasecmp(s, CRYPT_KDF_ARGON2ID))
141                 tmp = CRYPT_KDF_ARGON2ID;
142
143         if (!tmp)
144                 return -EINVAL;
145
146         if (pbkdf)
147                 *pbkdf = tmp;
148
149         return 0;
150 }
151
152 ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc)
153 {
154         char buf[3] = "xx\0", *endp, *bytes;
155         size_t i, len;
156
157         len = strlen(hex);
158         if (len % 2)
159                 return -EINVAL;
160         len /= 2;
161
162         bytes = safe_alloc ? crypt_safe_alloc(len) : malloc(len);
163         if (!bytes)
164                 return -ENOMEM;
165
166         for (i = 0; i < len; i++) {
167                 memcpy(buf, &hex[i * 2], 2);
168                 bytes[i] = strtoul(buf, &endp, 16);
169                 if (endp != &buf[2]) {
170                         safe_alloc ? crypt_safe_free(bytes) : free(bytes);
171                         return -EINVAL;
172                 }
173         }
174         *result = bytes;
175         return i;
176 }