06f5c81e65d3e48771d8d7a8562efdab349f56ae
[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 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
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 #if USE_INTERNAL_PBKDF2
70 /* internal PBKDF2 implementation */
71 int pkcs5_pbkdf2(const char *hash,
72                  const char *P, size_t Plen,
73                  const char *S, size_t Slen,
74                  unsigned int c,
75                  unsigned int dkLen,char *DK);
76 #endif
77
78 /* CRC32 */
79 uint32_t crypt_crc32(uint32_t seed, const unsigned char *buf, size_t len);
80
81 /* ciphers */
82 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
83                     const char *mode, const void *buffer, size_t length);
84 int crypt_cipher_destroy(struct crypt_cipher *ctx);
85 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
86                          const char *in, char *out, size_t length,
87                          const char *iv, size_t iv_length);
88 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
89                          const char *in, char *out, size_t length,
90                          const char *iv, size_t iv_length);
91
92 #endif /* _CRYPTO_BACKEND_H */