Fixed the build error for riscv64 arch using gcc 13
[platform/upstream/cryptsetup.git] / lib / utils_crypt.c
index 907c535..f2519c1 100644 (file)
@@ -1,12 +1,14 @@
 /*
- * util_crypt - cipher utilities for cryptsetup
+ * utils_crypt - cipher utilities for cryptsetup
  *
- * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
- * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
+ * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2021 Milan Broz
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <stdlib.h>
-#include <stddef.h>
 #include <stdio.h>
 #include <string.h>
+#include <ctype.h>
 #include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <termios.h>
 
 #include "libcryptsetup.h"
-#include "nls.h"
 #include "utils_crypt.h"
 
-struct safe_allocation {
-       size_t  size;
-       char    data[0];
-};
-
 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
                              char *cipher_mode)
 {
+       if (!s || !cipher || !cipher_mode)
+               return -EINVAL;
+
        if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
                   cipher, cipher_mode) == 2) {
                if (!strcmp(cipher_mode, "plain"))
-                       strncpy(cipher_mode, "cbc-plain", 10);
+                       strcpy(cipher_mode, "cbc-plain");
                if (key_nums) {
                        char *tmp = strchr(cipher, ':');
                        *key_nums = tmp ? atoi(++tmp) : 1;
@@ -55,8 +49,17 @@ int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
                return 0;
        }
 
+       /* Short version for "empty" cipher */
+       if (!strcmp(s, "null") || !strcmp(s, "cipher_null")) {
+               strcpy(cipher, "cipher_null");
+               strcpy(cipher_mode, "ecb");
+               if (key_nums)
+                       *key_nums = 0;
+               return 0;
+       }
+
        if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
-               strncpy(cipher_mode, "cbc-plain", 10);
+               strcpy(cipher_mode, "cbc-plain");
                if (key_nums)
                        *key_nums = 1;
                return 0;
@@ -65,261 +68,119 @@ int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
        return -EINVAL;
 }
 
-/* safe allocations */
-void *crypt_safe_alloc(size_t size)
+int crypt_parse_hash_integrity_mode(const char *s, char *integrity)
 {
-       struct safe_allocation *alloc;
+       char mode[MAX_CIPHER_LEN], hash[MAX_CIPHER_LEN];
+       int r;
 
-       if (!size)
-               return NULL;
+       if (!s || !integrity || strchr(s, '(') || strchr(s, ')'))
+               return -EINVAL;
 
-       alloc = malloc(size + offsetof(struct safe_allocation, data));
-       if (!alloc)
-               return NULL;
+       r = sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", mode, hash);
+       if (r == 2 && !isdigit(hash[0]))
+               r = snprintf(integrity, MAX_CIPHER_LEN, "%s(%s)", mode, hash);
+       else if (r == 2)
+               r = snprintf(integrity, MAX_CIPHER_LEN, "%s-%s", mode, hash);
+       else if (r == 1)
+               r = snprintf(integrity, MAX_CIPHER_LEN, "%s", mode);
+       else
+               return -EINVAL;
 
-       alloc->size = size;
+       if (r < 0 || r >= MAX_CIPHER_LEN)
+               return -EINVAL;
 
-       return &alloc->data;
+       return 0;
 }
 
-void crypt_safe_free(void *data)
+int crypt_parse_integrity_mode(const char *s, char *integrity,
+                              int *integrity_key_size)
 {
-       struct safe_allocation *alloc;
-
-       if (!data)
-               return;
-
-       alloc = data - offsetof(struct safe_allocation, data);
-
-       memset(data, 0, alloc->size);
-
-       alloc->size = 0x55aa55aa;
-       free(alloc);
+       int ks = 0, r = 0;
+
+       if (!s || !integrity)
+               return -EINVAL;
+
+       // FIXME: do not hardcode it here
+
+       /* AEAD modes */
+       if (!strcmp(s, "aead") ||
+           !strcmp(s, "poly1305") ||
+           !strcmp(s, "none")) {
+               strncpy(integrity, s, MAX_CIPHER_LEN);
+               ks = 0;
+       } else if (!strcmp(s, "hmac-sha1")) {
+               strncpy(integrity, "hmac(sha1)", MAX_CIPHER_LEN);
+               ks = 20;
+       } else if (!strcmp(s, "hmac-sha256")) {
+               strncpy(integrity, "hmac(sha256)", MAX_CIPHER_LEN);
+               ks = 32;
+       } else if (!strcmp(s, "hmac-sha512")) {
+               ks = 64;
+               strncpy(integrity, "hmac(sha512)", MAX_CIPHER_LEN);
+       } else if (!strcmp(s, "cmac-aes")) {
+               ks = 16;
+               strncpy(integrity, "cmac(aes)", MAX_CIPHER_LEN);
+       } else
+               r = -EINVAL;
+
+       if (integrity_key_size)
+               *integrity_key_size = ks;
+
+       return r;
 }
 
-void *crypt_safe_realloc(void *data, size_t size)
+int crypt_parse_pbkdf(const char *s, const char **pbkdf)
 {
-       void *new_data;
-
-       new_data = crypt_safe_alloc(size);
+       const char *tmp = NULL;
 
-       if (new_data && data) {
-               struct safe_allocation *alloc;
+       if (!s)
+               return -EINVAL;
 
-               alloc = data - offsetof(struct safe_allocation, data);
+       if (!strcasecmp(s, CRYPT_KDF_PBKDF2))
+               tmp = CRYPT_KDF_PBKDF2;
+       else if (!strcasecmp(s, CRYPT_KDF_ARGON2I))
+               tmp = CRYPT_KDF_ARGON2I;
+       else if (!strcasecmp(s, CRYPT_KDF_ARGON2ID))
+               tmp = CRYPT_KDF_ARGON2ID;
 
-               if (size > alloc->size)
-                       size = alloc->size;
+       if (!tmp)
+               return -EINVAL;
 
-               memcpy(new_data, data, size);
-       }
+       if (pbkdf)
+               *pbkdf = tmp;
 
-       crypt_safe_free(data);
-       return new_data;
+       return 0;
 }
 
-/* Password reading helpers */
-static int untimed_read(int fd, char *pass, size_t maxlen)
+ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc)
 {
-       ssize_t i;
-
-       i = read(fd, pass, maxlen);
-       if (i > 0) {
-               pass[i-1] = '\0';
-               i = 0;
-       } else if (i == 0) { /* EOF */
-               *pass = 0;
-               i = -1;
+       char buf[3] = "xx\0", *endp, *bytes;
+       size_t i, len;
+
+       len = strlen(hex);
+       if (len % 2)
+               return -EINVAL;
+       len /= 2;
+
+       bytes = safe_alloc ? crypt_safe_alloc(len) : malloc(len);
+       if (!bytes)
+               return -ENOMEM;
+
+       for (i = 0; i < len; i++) {
+               memcpy(buf, &hex[i * 2], 2);
+               bytes[i] = strtoul(buf, &endp, 16);
+               if (endp != &buf[2]) {
+                       safe_alloc ? crypt_safe_free(bytes) : free(bytes);
+                       return -EINVAL;
+               }
        }
+       *result = bytes;
        return i;
 }
 
-static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
-{
-       struct timeval t;
-       fd_set fds;
-       int failed = -1;
-
-       FD_ZERO(&fds);
-       FD_SET(fd, &fds);
-       t.tv_sec = timeout;
-       t.tv_usec = 0;
-
-       if (select(fd+1, &fds, NULL, NULL, &t) > 0)
-               failed = untimed_read(fd, pass, maxlen);
-
-       return failed;
-}
-
-static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
-               long timeout)
-{
-       struct termios orig, tmp;
-       int failed = -1;
-       int infd = STDIN_FILENO, outfd;
-
-       if (maxlen < 1)
-               goto out_err;
-
-       /* Read and write to /dev/tty if available */
-       if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
-               infd = STDIN_FILENO;
-               outfd = STDERR_FILENO;
-       }
-
-       if (tcgetattr(infd, &orig))
-               goto out_err;
-
-       memcpy(&tmp, &orig, sizeof(tmp));
-       tmp.c_lflag &= ~ECHO;
-
-       if (prompt && write(outfd, prompt, strlen(prompt)) < 0)
-               goto out_err;
-
-       tcsetattr(infd, TCSAFLUSH, &tmp);
-       if (timeout)
-               failed = timed_read(infd, pass, maxlen, timeout);
-       else
-               failed = untimed_read(infd, pass, maxlen);
-       tcsetattr(infd, TCSAFLUSH, &orig);
-
-out_err:
-       if (!failed && write(outfd, "\n", 1)) {};
-
-       if (infd != STDIN_FILENO)
-               close(infd);
-       return failed;
-}
-
-/*
- * Password reading behaviour matrix of get_key
- * FIXME: rewrite this from scratch.
- *                    p   v   n   h
- * -----------------+---+---+---+---
- * interactive      | Y | Y | Y | Inf
- * from fd          | N | N | Y | Inf
- * from binary file | N | N | N | Inf or options->key_size
- *
- * Legend: p..prompt, v..can verify, n..newline-stop, h..read horizon
- *
- * Note: --key-file=- is interpreted as a read from a binary file (stdin)
- */
-
-int crypt_get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
-                 const char *key_file, int timeout, int verify,
-                 struct crypt_device *cd)
+bool crypt_is_cipher_null(const char *cipher_spec)
 {
-       int fd = -1;
-       char *pass = NULL;
-       int read_horizon;
-       int regular_file = 0;
-       int read_stdin;
-       int r;
-       struct stat st;
-
-       /* Passphrase read from stdin? */
-       read_stdin = (!key_file || !strcmp(key_file, "-")) ? 1 : 0;
-
-       /* read_horizon applies only for real keyfile, not stdin or terminal */
-       read_horizon = (key_file && !read_stdin) ? key_size : 0 /* until EOF */;
-
-       /* Setup file descriptior */
-       fd = read_stdin ? STDIN_FILENO : open(key_file, O_RDONLY);
-       if (fd < 0) {
-               crypt_log(cd, CRYPT_LOG_ERROR,
-                         _("Failed to open key file.\n"));
-               goto out_err;
-       }
-
-       /* Interactive case */
-       if(isatty(fd)) {
-               int i;
-
-               pass = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
-               if (!pass || interactive_pass(prompt, pass, MAX_TTY_PASSWORD_LEN, timeout)) {
-                       crypt_log(cd, CRYPT_LOG_ERROR,
-                                 _("Error reading passphrase from terminal.\n"));
-                       goto out_err;
-               }
-               if (verify) {
-                       char pass_verify[MAX_TTY_PASSWORD_LEN];
-                       i = interactive_pass(_("Verify passphrase: "), pass_verify, sizeof(pass_verify), timeout);
-                       if (i || strcmp(pass, pass_verify) != 0) {
-                               crypt_log(cd, CRYPT_LOG_ERROR,
-                                _("Passphrases do not match.\n"));
-                               goto out_err;
-                       }
-                       memset(pass_verify, 0, sizeof(pass_verify));
-               }
-               *passLen = strlen(pass);
-               *key = pass;
-       } else {
-               /*
-                * This is either a fd-input or a file, in neither case we can verify the input,
-                * however we don't stop on new lines if it's a binary file.
-                */
-               int buflen, i;
-
-               /* The following for control loop does an exhausting
-                * read on the key material file, if requested with
-                * key_size == 0, as it's done by LUKS. However, we
-                * should warn the user, if it's a non-regular file,
-                * such as /dev/random, because in this case, the loop
-                * will read forever.
-                */
-               if(!read_stdin && read_horizon == 0) {
-                       if(stat(key_file, &st) < 0) {
-                               crypt_log(cd, CRYPT_LOG_ERROR,
-                                       _("Failed to stat key file.\n"));
-                               goto out_err;
-                       }
-                       if(!S_ISREG(st.st_mode))
-                               crypt_log(cd, CRYPT_LOG_NORMAL,
-                                         _("Warning: exhausting read requested, but key file"
-                                           " is not a regular file, function might never return.\n"));
-                       else
-                               regular_file = 1;
-               }
-               buflen = 0;
-               for(i = 0; read_horizon == 0 || i < read_horizon; i++) {
-                       if(i >= buflen - 1) {
-                               buflen += 128;
-                               pass = crypt_safe_realloc(pass, buflen);
-                               if (!pass) {
-                                       crypt_log(cd, CRYPT_LOG_ERROR,
-                                                 _("Out of memory while reading passphrase.\n"));
-                                       goto out_err;
-                               }
-                       }
-
-                       r = read(fd, pass + i, 1);
-                       if (r < 0) {
-                               crypt_log(cd, CRYPT_LOG_ERROR,
-                                         _("Error reading passphrase.\n"));
-                               goto out_err;
-                       }
-
-                       /* Stop on newline only if not requested read from keyfile */
-                       if(r == 0 || (!key_file && pass[i] == '\n'))
-                               break;
-               }
-               /* Fail if piped input dies reading nothing */
-               if(!i && !regular_file)
-                       goto out_err;
-               pass[i] = 0;
-               *key = pass;
-               *passLen = i;
-       }
-       if(fd != STDIN_FILENO)
-               close(fd);
-       return 0;
-
-out_err:
-       if(fd >= 0 && fd != STDIN_FILENO)
-               close(fd);
-       if(pass)
-               crypt_safe_free(pass);
-       *key = NULL;
-       *passLen = 0;
-       return -EINVAL;
+       if (!cipher_spec)
+               return false;
+       return (strstr(cipher_spec, "cipher_null") || !strcmp(cipher_spec, "null"));
 }