Merge branch 'upstream' into tizen
[platform/upstream/cryptsetup.git] / src / cryptsetup.h
1 /*
2  * cryptsetup - setup cryptographic volumes for dm-crypt
3  *
4  * Copyright (C) 2004 Jana Saout <jana@saout.de>
5  * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2021 Milan Broz
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #ifndef CRYPTSETUP_H
25 #define CRYPTSETUP_H
26
27 #include <stdbool.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <inttypes.h>
36 #include <limits.h>
37 #include <ctype.h>
38 #include <fcntl.h>
39 #include <popt.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42
43 #include "lib/nls.h"
44 #include "lib/utils_crypt.h"
45 #include "lib/utils_loop.h"
46 #include "lib/utils_fips.h"
47 #include "lib/utils_io.h"
48 #include "lib/utils_blkid.h"
49
50 #include "libcryptsetup.h"
51
52 #define CONST_CAST(x) (x)(uintptr_t)
53 #define DEFAULT_CIPHER(type)    (DEFAULT_##type##_CIPHER "-" DEFAULT_##type##_MODE)
54 #define SECTOR_SIZE 512
55 #define MAX_SECTOR_SIZE 4096
56 #define ROUND_SECTOR(x) (((x) + SECTOR_SIZE - 1) / SECTOR_SIZE)
57
58 #define DEFAULT_WIPE_BLOCK      1048576 /* 1 MiB */
59
60 extern int opt_debug;
61 extern int opt_debug_json;
62 extern int opt_verbose;
63 extern int opt_batch_mode;
64 extern int opt_force_password;
65 extern int opt_progress_frequency;
66
67 /* Common tools */
68 void clogger(struct crypt_device *cd, int level, const char *file, int line,
69              const char *format, ...)  __attribute__ ((format (printf, 5, 6)));
70 void tool_log(int level, const char *msg, void *usrptr __attribute__((unused)));
71 void quiet_log(int level, const char *msg, void *usrptr);
72
73 int yesDialog(const char *msg, void *usrptr);
74 int noDialog(const char *msg, void *usrptr);
75 void show_status(int errcode);
76 const char *uuid_or_device(const char *spec);
77 __attribute__ ((noreturn)) \
78 void usage(poptContext popt_context, int exitcode, const char *error, const char *more);
79 void dbg_version_and_cmd(int argc, const char **argv);
80 int translate_errno(int r);
81
82 typedef enum { CREATED, UNLOCKED, REMOVED  } crypt_object_op;
83 void tools_keyslot_msg(int keyslot, crypt_object_op op);
84 void tools_token_msg(int token, crypt_object_op op);
85
86 extern volatile int quit;
87 void set_int_block(int block);
88 void set_int_handler(int block);
89 void check_signal(int *r);
90 int tools_signals_blocked(void);
91
92 int tools_get_key(const char *prompt,
93                   char **key, size_t *key_size,
94                   uint64_t keyfile_offset, size_t keyfile_size_max,
95                   const char *key_file,
96                   int timeout, int verify, int pwquality,
97                   struct crypt_device *cd);
98 void tools_passphrase_msg(int r);
99 int tools_is_stdin(const char *key_file);
100 int tools_string_to_size(struct crypt_device *cd, const char *s, uint64_t *size);
101
102 void tools_clear_line(void);
103
104 int tools_wipe_progress(uint64_t size, uint64_t offset, void *usrptr);
105 int tools_reencrypt_progress(uint64_t size, uint64_t offset, void *usrptr);
106
107 int tools_read_mk(const char *file, char **key, int keysize);
108 int tools_write_mk(const char *file, const char *key, int keysize);
109
110 int tools_read_json_file(struct crypt_device *cd, const char *file, char **json, size_t *json_size);
111 int tools_write_json_file(struct crypt_device *cd, const char *file, const char *json);
112
113 int tools_detect_signatures(const char *device, int ignore_luks, size_t *count);
114 int tools_wipe_all_signatures(const char *path);
115
116 int tools_lookup_crypt_device(struct crypt_device *cd, const char *type,
117                 const char *data_device_path, char *name, size_t name_length);
118
119 /* each utility is required to implement it */
120 void tools_cleanup(void);
121
122 #define FREE_AND_NULL(x) do { free(x); x = NULL; } while (0)
123
124 /* Log */
125 #define log_dbg(x...) clogger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x)
126 #define log_std(x...) clogger(NULL, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x)
127 #define log_verbose(x...) clogger(NULL, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x)
128 #define log_err(x...) clogger(NULL, CRYPT_LOG_ERROR, __FILE__, __LINE__, x)
129
130 #endif /* CRYPTSETUP_H */