Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_backend.h
1 /*
2  * crypto backend implementation
3  *
4  * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2010-2023 Milan Broz
6  *
7  * This file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this file; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #ifndef _CRYPTO_BACKEND_H
22 #define _CRYPTO_BACKEND_H
23
24 #include <assert.h>
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <string.h>
29 #ifdef HAVE_UCHAR_H
30 #include <uchar.h>
31 #else
32 #define char32_t uint32_t
33 #define char16_t uint16_t
34 #endif
35
36 struct crypt_hash;
37 struct crypt_hmac;
38 struct crypt_cipher;
39 struct crypt_storage;
40
41 int crypt_backend_init(bool fips);
42 void crypt_backend_destroy(void);
43
44 #define CRYPT_BACKEND_KERNEL     (1 << 0) /* Crypto uses kernel part, for benchmark */
45 #define CRYPT_BACKEND_PBKDF2_INT (1 << 1) /* Iteration in PBKDF2 is signed int and can overflow */
46
47 uint32_t crypt_backend_flags(void);
48 const char *crypt_backend_version(void);
49
50 /* HASH */
51 int crypt_hash_size(const char *name);
52 int crypt_hash_init(struct crypt_hash **ctx, const char *name);
53 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length);
54 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length);
55 void crypt_hash_destroy(struct crypt_hash *ctx);
56
57 /* HMAC */
58 int crypt_hmac_size(const char *name);
59 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
60                     const void *key, size_t key_length);
61 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length);
62 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length);
63 void crypt_hmac_destroy(struct crypt_hmac *ctx);
64
65 /* RNG (if fips parameter set, must provide FIPS compliance) */
66 enum { CRYPT_RND_NORMAL = 0, CRYPT_RND_KEY = 1, CRYPT_RND_SALT = 2 };
67 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips);
68
69
70 /* PBKDF*/
71 struct crypt_pbkdf_limits {
72         uint32_t min_iterations, max_iterations;
73         uint32_t min_memory, max_memory, min_bench_memory;
74         uint32_t min_parallel, max_parallel;
75 };
76
77 int crypt_pbkdf_get_limits(const char *kdf, struct crypt_pbkdf_limits *l);
78 int crypt_pbkdf(const char *kdf, const char *hash,
79                 const char *password, size_t password_length,
80                 const char *salt, size_t salt_length,
81                 char *key, size_t key_length,
82                 uint32_t iterations, uint32_t memory, uint32_t parallel);
83 int crypt_pbkdf_perf(const char *kdf, const char *hash,
84                 const char *password, size_t password_size,
85                 const char *salt, size_t salt_size,
86                 size_t volume_key_size, uint32_t time_ms,
87                 uint32_t max_memory_kb, uint32_t parallel_threads,
88                 uint32_t *iterations_out, uint32_t *memory_out,
89                 int (*progress)(uint32_t time_ms, void *usrptr), void *usrptr);
90
91 /* CRC32 */
92 uint32_t crypt_crc32(uint32_t seed, const unsigned char *buf, size_t len);
93 uint32_t crypt_crc32c(uint32_t seed, const unsigned char *buf, size_t len);
94
95 /* Base64 */
96 int crypt_base64_encode(char **out, size_t *out_length, const char *in, size_t in_length);
97 int crypt_base64_decode(char **out, size_t *out_length, const char *in, size_t in_length);
98
99 /* UTF8/16 */
100 int crypt_utf16_to_utf8(char **out, const char16_t *s, size_t length /* bytes! */);
101 int crypt_utf8_to_utf16(char16_t **out, const char *s, size_t length);
102
103 /* Block ciphers */
104 int crypt_cipher_ivsize(const char *name, const char *mode);
105 int crypt_cipher_wrapped_key(const char *name, const char *mode);
106 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
107                     const char *mode, const void *key, size_t key_length);
108 void crypt_cipher_destroy(struct crypt_cipher *ctx);
109 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
110                          const char *in, char *out, size_t length,
111                          const char *iv, size_t iv_length);
112 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
113                          const char *in, char *out, size_t length,
114                          const char *iv, size_t iv_length);
115 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx);
116
117 /* Benchmark of kernel cipher performance */
118 int crypt_cipher_perf_kernel(const char *name, const char *mode, char *buffer, size_t buffer_size,
119                              const char *key, size_t key_size, const char *iv, size_t iv_size,
120                              double *encryption_mbs, double *decryption_mbs);
121
122 /* Check availability of a cipher (in kernel only) */
123 int crypt_cipher_check_kernel(const char *name, const char *mode,
124                               const char *integrity, size_t key_length);
125
126 /* Storage encryption wrappers */
127 int crypt_storage_init(struct crypt_storage **ctx, size_t sector_size,
128                        const char *cipher, const char *cipher_mode,
129                        const void *key, size_t key_length, bool large_iv);
130 void crypt_storage_destroy(struct crypt_storage *ctx);
131 int crypt_storage_decrypt(struct crypt_storage *ctx, uint64_t iv_offset,
132                           uint64_t length, char *buffer);
133 int crypt_storage_encrypt(struct crypt_storage *ctx, uint64_t iv_offset,
134                           uint64_t length, char *buffer);
135
136 bool crypt_storage_kernel_only(struct crypt_storage *ctx);
137
138 /* Temporary Bitlk helper */
139 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
140                             const char *in, char *out, size_t length,
141                             const char *iv, size_t iv_length,
142                             const char *tag, size_t tag_length);
143
144 /* Memzero helper (memset on stack can be optimized out) */
145 static inline void crypt_backend_memzero(void *s, size_t n)
146 {
147 #ifdef HAVE_EXPLICIT_BZERO
148         explicit_bzero(s, n);
149 #else
150         volatile uint8_t *p = (volatile uint8_t *)s;
151         while(n--) *p++ = 0;
152 #endif
153 }
154
155 /* Memcmp helper (memcmp in constant time) */
156 int crypt_backend_memeq(const void *m1, const void *m2, size_t n);
157
158 /* crypto backend running in FIPS mode */
159 bool crypt_fips_mode(void);
160
161 #endif /* _CRYPTO_BACKEND_H */