Whamcloud - gitweb
2d6897cec37f169b05b701760cd7a058c383ed73
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_crypto.h
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 only,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License version 2 for more details (a copy is included
13  * in the LICENSE file that accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 2 along with this program; If not, see http://www.gnu.org/licenses
17  *
18  * Please  visit http://www.xyratex.com/contact if you need additional
19  * information or have any questions.
20  *
21  * GPL HEADER END
22  */
23
24 /*
25  * Copyright 2012 Xyratex Technology Limited
26  *
27  * Copyright (c) 2014, Intel Corporation.
28  */
29
30 #ifndef _LIBCFS_CRYPTO_H
31 #define _LIBCFS_CRYPTO_H
32
33 struct cfs_crypto_hash_type {
34         char            *cht_name;      /**< hash algorithm name, equal to
35                                          * format name for crypto api */
36         unsigned int    cht_key;        /**< init key by default (vaild for
37                                          * 4 bytes context like crc32, adler */
38         unsigned int    cht_size;       /**< hash digest size */
39 };
40
41 enum cfs_crypto_hash_alg {
42         CFS_HASH_ALG_NULL       = 0,
43         CFS_HASH_ALG_ADLER32,
44         CFS_HASH_ALG_CRC32,
45         CFS_HASH_ALG_MD5,
46         CFS_HASH_ALG_SHA1,
47         CFS_HASH_ALG_SHA256,
48         CFS_HASH_ALG_SHA384,
49         CFS_HASH_ALG_SHA512,
50         CFS_HASH_ALG_CRC32C,
51         CFS_HASH_ALG_MAX,
52         CFS_HASH_ALG_UNKNOWN    = 0xff
53 };
54
55 static struct cfs_crypto_hash_type hash_types[] = {
56         [CFS_HASH_ALG_NULL]     = { "null",      0,      0 },
57         [CFS_HASH_ALG_ADLER32]  = { "adler32",   1,      4 },
58         [CFS_HASH_ALG_CRC32]    = { "crc32",    ~0,      4 },
59         [CFS_HASH_ALG_CRC32C]   = { "crc32c",   ~0,      4 },
60         [CFS_HASH_ALG_MD5]      = { "md5",       0,     16 },
61         [CFS_HASH_ALG_SHA1]     = { "sha1",      0,     20 },
62         [CFS_HASH_ALG_SHA256]   = { "sha256",    0,     32 },
63         [CFS_HASH_ALG_SHA384]   = { "sha384",    0,     48 },
64         [CFS_HASH_ALG_SHA512]   = { "sha512",    0,     64 },
65         [CFS_HASH_ALG_MAX]      = { NULL,        0,     64 },
66 };
67
68 /* Maximum size of hash_types[].cht_size */
69 #define CFS_CRYPTO_HASH_DIGESTSIZE_MAX 64
70
71 /**
72  * Return hash algorithm information for the specified algorithm identifier
73  *
74  * Hash information includes algorithm name, initial seed, hash size.
75  *
76  * \retval              cfs_crypto_hash_type for valid ID (CFS_HASH_ALG_*)
77  * \retval              NULL for unknown algorithm identifier
78  */
79 static inline const struct
80 cfs_crypto_hash_type *cfs_crypto_hash_type(enum cfs_crypto_hash_alg hash_alg)
81 {
82         struct cfs_crypto_hash_type *ht;
83
84         if (hash_alg < CFS_HASH_ALG_MAX) {
85                 ht = &hash_types[hash_alg];
86                 if (ht->cht_name != NULL)
87                         return ht;
88         }
89         return NULL;
90 }
91
92 /**
93  * Return hash name for hash algorithm identifier
94  *
95  * \param[in] hash_alg  hash alrgorithm id (CFS_HASH_ALG_*)
96  *
97  * \retval              string name of known hash algorithm
98  * \retval              "unknown" if hash algorithm is unknown
99  */
100 static inline const
101 char *cfs_crypto_hash_name(enum cfs_crypto_hash_alg hash_alg)
102 {
103         const struct cfs_crypto_hash_type *ht;
104
105         ht = cfs_crypto_hash_type(hash_alg);
106         if (ht)
107                 return ht->cht_name;
108
109         return "unknown";
110 }
111
112 /**
113  * Return digest size for hash algorithm type
114  *
115  * \param[in] hash_alg  hash alrgorithm id (CFS_HASH_ALG_*)
116  *
117  * \retval              hash algorithm digest size in bytes
118  * \retval              0 if hash algorithm type is unknown
119  */
120 static inline
121 unsigned int cfs_crypto_hash_digestsize(enum cfs_crypto_hash_alg hash_alg)
122 {
123         const struct cfs_crypto_hash_type *ht;
124
125         ht = cfs_crypto_hash_type(hash_alg);
126         if (ht != NULL)
127                 return ht->cht_size;
128
129         return 0;
130 }
131
132 /**
133  * Find hash algorithm ID for the specified algorithm name
134  *
135  * \retval              hash algorithm ID for valid ID (CFS_HASH_ALG_*)
136  * \retval              CFS_HASH_ALG_UNKNOWN for unknown algorithm name
137  */
138 static inline unsigned char cfs_crypto_hash_alg(const char *algname)
139 {
140         enum cfs_crypto_hash_alg hash_alg;
141
142         for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++)
143                 if (strcmp(hash_types[hash_alg].cht_name, algname) == 0)
144                         return hash_alg;
145
146         return CFS_HASH_ALG_UNKNOWN;
147 }
148
149 int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg,
150                            const void *buf, unsigned int buf_len,
151                            unsigned char *key, unsigned int key_len,
152                            unsigned char *hash, unsigned int *hash_len);
153
154 /* cfs crypto hash descriptor */
155 struct cfs_crypto_hash_desc;
156
157 struct cfs_crypto_hash_desc *
158         cfs_crypto_hash_init(enum cfs_crypto_hash_alg hash_alg,
159                              unsigned char *key, unsigned int key_len);
160 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *desc,
161                                 struct page *page, unsigned int offset,
162                                 unsigned int len);
163 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *desc, const void *buf,
164                            unsigned int buf_len);
165 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *desc,
166                           unsigned char *hash, unsigned int *hash_len);
167 int cfs_crypto_register(void);
168 void cfs_crypto_unregister(void);
169 int cfs_crypto_hash_speed(enum cfs_crypto_hash_alg hash_alg);
170 #endif