0c71d9d6bc35cacb3d1f3593c13358ed5f87a118
[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-2012, Milan Broz
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 #ifndef _CRYPTO_BACKEND_H
21 #define _CRYPTO_BACKEND_H
22
23 #include <stdint.h>
24 #include <string.h>
25 #include "config.h"
26
27 struct crypt_device;
28 struct crypt_hash;
29 struct crypt_hmac;
30 struct crypt_cipher;
31
32 int crypt_backend_init(struct crypt_device *ctx);
33
34 #define CRYPT_BACKEND_KERNEL (1 << 0)   /* Crypto uses kernel part, for benchmark */
35
36 uint32_t crypt_backend_flags(void);
37 const char *crypt_backend_version(void);
38
39 /* HASH */
40 int crypt_hash_size(const char *name);
41 int crypt_hash_init(struct crypt_hash **ctx, const char *name);
42 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length);
43 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length);
44 int crypt_hash_destroy(struct crypt_hash *ctx);
45
46 /* HMAC */
47 int crypt_hmac_size(const char *name);
48 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
49                     const void *buffer, size_t length);
50 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length);
51 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length);
52 int crypt_hmac_destroy(struct crypt_hmac *ctx);
53
54 /* RNG (if fips paramater set, must provide FIPS compliance) */
55 enum { CRYPT_RND_NORMAL = 0, CRYPT_RND_KEY = 1, CRYPT_RND_SALT = 2 };
56 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips);
57
58 /* PBKDF*/
59 int crypt_pbkdf_check(const char *kdf, const char *hash,
60                       const char *password, size_t password_size,
61                       const char *salt, size_t salt_size,
62                       uint64_t *iter_secs);
63 int crypt_pbkdf(const char *kdf, const char *hash,
64                 const char *password, size_t password_length,
65                 const char *salt, size_t salt_length,
66                 char *key, size_t key_length,
67                 unsigned int iterations);
68
69 /* internal PBKDF2 implementation */
70 int pkcs5_pbkdf2(const char *hash,
71                  const char *P, size_t Plen,
72                  const char *S, size_t Slen,
73                  unsigned int c,
74                  unsigned int dkLen,char *DK);
75
76 /* CRC32 */
77 uint32_t crypt_crc32(uint32_t seed, const unsigned char *buf, size_t len);
78
79 /* ciphers */
80 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
81                     const char *mode, const void *buffer, size_t length);
82 int crypt_cipher_destroy(struct crypt_cipher *ctx);
83 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
84                          const char *in, char *out, size_t length,
85                          const char *iv, size_t iv_length);
86 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
87                          const char *in, char *out, size_t length,
88                          const char *iv, size_t iv_length);
89
90 #endif /* _CRYPTO_BACKEND_H */