Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_backend_internal.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_INTERNAL_H
22 #define _CRYPTO_BACKEND_INTERNAL_H
23
24 #include "crypto_backend.h"
25
26 /* internal PBKDF2 implementation */
27 int pkcs5_pbkdf2(const char *hash,
28                  const char *P, size_t Plen,
29                  const char *S, size_t Slen,
30                  unsigned int c,
31                  unsigned int dkLen, char *DK,
32                  unsigned int hash_block_size);
33
34 /* Argon2 implementation wrapper */
35 int argon2(const char *type, const char *password, size_t password_length,
36            const char *salt, size_t salt_length,
37            char *key, size_t key_length,
38            uint32_t iterations, uint32_t memory, uint32_t parallel);
39
40 /* Block ciphers: fallback to kernel crypto API */
41
42 struct crypt_cipher_kernel {
43         int tfmfd;
44         int opfd;
45 };
46
47 int crypt_cipher_init_kernel(struct crypt_cipher_kernel *ctx, const char *name,
48                              const char *mode, const void *key, size_t key_length);
49 int crypt_cipher_encrypt_kernel(struct crypt_cipher_kernel *ctx,
50                                 const char *in, char *out, size_t length,
51                                 const char *iv, size_t iv_length);
52 int crypt_cipher_decrypt_kernel(struct crypt_cipher_kernel *ctx,
53                                 const char *in, char *out, size_t length,
54                                 const char *iv, size_t iv_length);
55 void crypt_cipher_destroy_kernel(struct crypt_cipher_kernel *ctx);
56 int crypt_bitlk_decrypt_key_kernel(const void *key, size_t key_length,
57                                    const char *in, char *out, size_t length,
58                                    const char *iv, size_t iv_length,
59                                    const char *tag, size_t tag_length);
60
61 /* Internal implementation for constant time memory comparison */
62 static inline int crypt_internal_memeq(const void *m1, const void *m2, size_t n)
63 {
64         const unsigned char *_m1 = (const unsigned char *) m1;
65         const unsigned char *_m2 = (const unsigned char *) m2;
66         unsigned char result = 0;
67         size_t i;
68
69         for (i = 0; i < n; i++)
70                 result |= _m1[i] ^ _m2[i];
71
72         return result;
73 }
74
75 #endif /* _CRYPTO_BACKEND_INTERNAL_H */