13fd47c1222abc29192a24601754faa10f0f283e
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_backend.h
1 /*
2  * crypto backend implementation
3  *
4  * Copyright (C) 2010-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2010-2020 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 <stdint.h>
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <string.h>
28
29 struct crypt_hash;
30 struct crypt_hmac;
31 struct crypt_cipher;
32 struct crypt_storage;
33
34 int crypt_backend_init(void);
35 void crypt_backend_destroy(void);
36
37 #define CRYPT_BACKEND_KERNEL (1 << 0)   /* Crypto uses kernel part, for benchmark */
38
39 uint32_t crypt_backend_flags(void);
40 const char *crypt_backend_version(void);
41
42 /* HASH */
43 int crypt_hash_size(const char *name);
44 int crypt_hash_init(struct crypt_hash **ctx, const char *name);
45 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length);
46 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length);
47 void crypt_hash_destroy(struct crypt_hash *ctx);
48
49 /* HMAC */
50 int crypt_hmac_size(const char *name);
51 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
52                     const void *key, size_t key_length);
53 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length);
54 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length);
55 void crypt_hmac_destroy(struct crypt_hmac *ctx);
56
57 /* RNG (if fips parameter set, must provide FIPS compliance) */
58 enum { CRYPT_RND_NORMAL = 0, CRYPT_RND_KEY = 1, CRYPT_RND_SALT = 2 };
59 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips);
60
61
62 /* PBKDF*/
63 struct crypt_pbkdf_limits {
64         uint32_t min_iterations, max_iterations;
65         uint32_t min_memory, max_memory;
66         uint32_t min_parallel, max_parallel;
67 };
68
69 int crypt_pbkdf_get_limits(const char *kdf, struct crypt_pbkdf_limits *l);
70 int crypt_pbkdf(const char *kdf, const char *hash,
71                 const char *password, size_t password_length,
72                 const char *salt, size_t salt_length,
73                 char *key, size_t key_length,
74                 uint32_t iterations, uint32_t memory, uint32_t parallel);
75 int crypt_pbkdf_perf(const char *kdf, const char *hash,
76                 const char *password, size_t password_size,
77                 const char *salt, size_t salt_size,
78                 size_t volume_key_size, uint32_t time_ms,
79                 uint32_t max_memory_kb, uint32_t parallel_threads,
80                 uint32_t *iterations_out, uint32_t *memory_out,
81                 int (*progress)(uint32_t time_ms, void *usrptr), void *usrptr);
82
83 /* CRC32 */
84 uint32_t crypt_crc32(uint32_t seed, const unsigned char *buf, size_t len);
85
86 /* Block ciphers */
87 int crypt_cipher_ivsize(const char *name, const char *mode);
88 int crypt_cipher_wrapped_key(const char *name, const char *mode);
89 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
90                     const char *mode, const void *key, size_t key_length);
91 void crypt_cipher_destroy(struct crypt_cipher *ctx);
92 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
93                          const char *in, char *out, size_t length,
94                          const char *iv, size_t iv_length);
95 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
96                          const char *in, char *out, size_t length,
97                          const char *iv, size_t iv_length);
98 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx);
99
100 /* Benchmark of kernel cipher performance */
101 int crypt_cipher_perf_kernel(const char *name, const char *mode, char *buffer, size_t buffer_size,
102                              const char *key, size_t key_size, const char *iv, size_t iv_size,
103                              double *encryption_mbs, double *decryption_mbs);
104
105 /* Check availability of a cipher (in kernel only) */
106 int crypt_cipher_check_kernel(const char *name, const char *mode,
107                               const char *integrity, size_t key_length);
108
109 /* Storage encryption wrappers */
110 int crypt_storage_init(struct crypt_storage **ctx, size_t sector_size,
111                        const char *cipher, const char *cipher_mode,
112                        const void *key, size_t key_length);
113 void crypt_storage_destroy(struct crypt_storage *ctx);
114 int crypt_storage_decrypt(struct crypt_storage *ctx, uint64_t iv_offset,
115                           uint64_t length, char *buffer);
116 int crypt_storage_encrypt(struct crypt_storage *ctx, uint64_t iv_offset,
117                           uint64_t length, char *buffer);
118
119 bool crypt_storage_kernel_only(struct crypt_storage *ctx);
120
121 /* Temporary Bitlk helper */
122 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
123                             const char *in, char *out, size_t length,
124                             const char *iv, size_t iv_length,
125                             const char *tag, size_t tag_length);
126
127 /* Memzero helper (memset on stack can be optimized out) */
128 static inline void crypt_backend_memzero(void *s, size_t n)
129 {
130 #ifdef HAVE_EXPLICIT_BZERO
131         explicit_bzero(s, n);
132 #else
133         volatile uint8_t *p = (volatile uint8_t *)s;
134         while(n--) *p++ = 0;
135 #endif
136 }
137
138 #endif /* _CRYPTO_BACKEND_H */