0aab38c987dc981a000d223b34e5ede988183c17
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_backend.h
1 /*
2  * crypto backend implementation
3  *
4  * Copyright (C) 2010-2012, Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2010-2014, 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 <string.h>
26
27 struct crypt_device;
28 struct crypt_hash;
29 struct crypt_hmac;
30 struct crypt_cipher;
31 struct crypt_storage;
32
33 int crypt_backend_init(struct crypt_device *ctx);
34
35 #define CRYPT_BACKEND_KERNEL (1 << 0)   /* Crypto uses kernel part, for benchmark */
36
37 uint32_t crypt_backend_flags(void);
38 const char *crypt_backend_version(void);
39
40 /* HASH */
41 int crypt_hash_size(const char *name);
42 int crypt_hash_init(struct crypt_hash **ctx, const char *name);
43 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length);
44 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length);
45 int crypt_hash_destroy(struct crypt_hash *ctx);
46
47 /* HMAC */
48 int crypt_hmac_size(const char *name);
49 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
50                     const void *buffer, size_t length);
51 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length);
52 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length);
53 int crypt_hmac_destroy(struct crypt_hmac *ctx);
54
55 /* RNG (if fips paramater set, must provide FIPS compliance) */
56 enum { CRYPT_RND_NORMAL = 0, CRYPT_RND_KEY = 1, CRYPT_RND_SALT = 2 };
57 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips);
58
59 /* PBKDF*/
60 int crypt_pbkdf_check(const char *kdf, const char *hash,
61                       const char *password, size_t password_size,
62                       const char *salt, size_t salt_size,
63                       uint64_t *iter_secs);
64 int crypt_pbkdf(const char *kdf, const char *hash,
65                 const char *password, size_t password_length,
66                 const char *salt, size_t salt_length,
67                 char *key, size_t key_length,
68                 unsigned int iterations);
69
70 #if USE_INTERNAL_PBKDF2
71 /* internal PBKDF2 implementation */
72 int pkcs5_pbkdf2(const char *hash,
73                  const char *P, size_t Plen,
74                  const char *S, size_t Slen,
75                  unsigned int c,
76                  unsigned int dkLen, char *DK,
77                  unsigned int hash_block_size);
78 #endif
79
80 /* CRC32 */
81 uint32_t crypt_crc32(uint32_t seed, const unsigned char *buf, size_t len);
82
83 /* ciphers */
84 int crypt_cipher_blocksize(const char *name);
85 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
86                     const char *mode, const void *buffer, size_t length);
87 int crypt_cipher_destroy(struct crypt_cipher *ctx);
88 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
89                          const char *in, char *out, size_t length,
90                          const char *iv, size_t iv_length);
91 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
92                          const char *in, char *out, size_t length,
93                          const char *iv, size_t iv_length);
94
95 /* storage encryption wrappers */
96 int crypt_storage_init(struct crypt_storage **ctx, uint64_t sector_start,
97                        const char *cipher, const char *cipher_mode,
98                        char *key, size_t key_length);
99 int crypt_storage_destroy(struct crypt_storage *ctx);
100 int crypt_storage_decrypt(struct crypt_storage *ctx, uint64_t sector,
101                           size_t count, char *buffer);
102 int crypt_storage_encrypt(struct crypt_storage *ctx, uint64_t sector,
103                           size_t count, char *buffer);
104
105 /* Memzero helper (memset on stack can be optimized out) */
106 static inline void crypt_backend_memzero(void *s, size_t n)
107 {
108         volatile uint8_t *p = (volatile uint8_t *)s;
109         while(n--) *p++ = 0;
110 }
111
112 #endif /* _CRYPTO_BACKEND_H */