Whamcloud - gitweb
LU-10937 mgc: restore mgc binding for sptlrpc
[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_CRC32C,
46         /* hashes before here will be speed-tested at module load */
47         CFS_HASH_ALG_MD5,
48         CFS_HASH_ALG_SHA1,
49         CFS_HASH_ALG_SHA256,
50         CFS_HASH_ALG_SHA384,
51         CFS_HASH_ALG_SHA512,
52         CFS_HASH_ALG_MAX,
53         CFS_HASH_ALG_SPEED_MAX = CFS_HASH_ALG_MD5,
54         CFS_HASH_ALG_UNKNOWN    = 0xff
55 };
56
57 static struct cfs_crypto_hash_type hash_types[] = {
58         [CFS_HASH_ALG_NULL] = {
59                 .cht_name       = "null",
60                 .cht_key        = 0,
61                 .cht_size       = 0
62         },
63         [CFS_HASH_ALG_ADLER32] = {
64                 .cht_name       = "adler32",
65                 .cht_key        = 1,
66                 .cht_size       = 4
67         },
68         [CFS_HASH_ALG_CRC32] = {
69                 .cht_name       = "crc32",
70                 .cht_key        = ~0,
71                 .cht_size       = 4
72         },
73         [CFS_HASH_ALG_CRC32C] = {
74                 .cht_name       = "crc32c",
75                 .cht_key        = ~0,
76                 .cht_size       = 4
77         },
78         [CFS_HASH_ALG_MD5] = {
79                 .cht_name       = "md5",
80                 .cht_key        = 0,
81                 .cht_size       = 16
82         },
83         [CFS_HASH_ALG_SHA1] = {
84                 .cht_name       = "sha1",
85                 .cht_key        = 0,
86                 .cht_size       = 20
87         },
88         [CFS_HASH_ALG_SHA256] = {
89                 .cht_name       = "sha256",
90                 .cht_key        = 0,
91                 .cht_size       = 32
92         },
93         [CFS_HASH_ALG_SHA384] = {
94                 .cht_name       = "sha384",
95                 .cht_key        = 0,
96                 .cht_size       = 48
97         },
98         [CFS_HASH_ALG_SHA512] = {
99                 .cht_name       = "sha512",
100                 .cht_key        = 0,
101                 .cht_size       = 64
102         },
103         [CFS_HASH_ALG_MAX] = {
104                 .cht_name       = NULL,
105                 .cht_key        = 0,
106                 .cht_size       = 64
107         }
108 };
109
110 /* Maximum size of hash_types[].cht_size */
111 #define CFS_CRYPTO_HASH_DIGESTSIZE_MAX 64
112
113 /**
114  * Return hash algorithm information for the specified algorithm identifier
115  *
116  * Hash information includes algorithm name, initial seed, hash size.
117  *
118  * \retval              cfs_crypto_hash_type for valid ID (CFS_HASH_ALG_*)
119  * \retval              NULL for unknown algorithm identifier
120  */
121 static inline const struct
122 cfs_crypto_hash_type *cfs_crypto_hash_type(enum cfs_crypto_hash_alg hash_alg)
123 {
124         struct cfs_crypto_hash_type *ht;
125
126         if (hash_alg < CFS_HASH_ALG_MAX) {
127                 ht = &hash_types[hash_alg];
128                 if (ht->cht_name != NULL)
129                         return ht;
130         }
131         return NULL;
132 }
133
134 /**
135  * Return hash name for hash algorithm identifier
136  *
137  * \param[in] hash_alg  hash alrgorithm id (CFS_HASH_ALG_*)
138  *
139  * \retval              string name of known hash algorithm
140  * \retval              "unknown" if hash algorithm is unknown
141  */
142 static inline const
143 char *cfs_crypto_hash_name(enum cfs_crypto_hash_alg hash_alg)
144 {
145         const struct cfs_crypto_hash_type *ht;
146
147         ht = cfs_crypto_hash_type(hash_alg);
148         if (ht)
149                 return ht->cht_name;
150
151         return "unknown";
152 }
153
154 /**
155  * Return digest size for hash algorithm type
156  *
157  * \param[in] hash_alg  hash alrgorithm id (CFS_HASH_ALG_*)
158  *
159  * \retval              hash algorithm digest size in bytes
160  * \retval              0 if hash algorithm type is unknown
161  */
162 static inline
163 unsigned int cfs_crypto_hash_digestsize(enum cfs_crypto_hash_alg hash_alg)
164 {
165         const struct cfs_crypto_hash_type *ht;
166
167         ht = cfs_crypto_hash_type(hash_alg);
168         if (ht != NULL)
169                 return ht->cht_size;
170
171         return 0;
172 }
173
174 /**
175  * Find hash algorithm ID for the specified algorithm name
176  *
177  * \retval              hash algorithm ID for valid ID (CFS_HASH_ALG_*)
178  * \retval              CFS_HASH_ALG_UNKNOWN for unknown algorithm name
179  */
180 static inline unsigned char cfs_crypto_hash_alg(const char *algname)
181 {
182         enum cfs_crypto_hash_alg hash_alg;
183
184         for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++)
185                 if (strcmp(hash_types[hash_alg].cht_name, algname) == 0)
186                         return hash_alg;
187
188         return CFS_HASH_ALG_UNKNOWN;
189 }
190
191 int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg,
192                            const void *buf, unsigned int buf_len,
193                            unsigned char *key, unsigned int key_len,
194                            unsigned char *hash, unsigned int *hash_len);
195
196 /* cfs crypto hash descriptor */
197 struct cfs_crypto_hash_desc;
198 struct page;
199
200 struct cfs_crypto_hash_desc *
201         cfs_crypto_hash_init(enum cfs_crypto_hash_alg hash_alg,
202                              unsigned char *key, unsigned int key_len);
203 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *desc,
204                                 struct page *page, unsigned int offset,
205                                 unsigned int len);
206 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *desc, const void *buf,
207                            unsigned int buf_len);
208 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *desc,
209                           unsigned char *hash, unsigned int *hash_len);
210 int cfs_crypto_register(void);
211 void cfs_crypto_unregister(void);
212 int cfs_crypto_hash_speed(enum cfs_crypto_hash_alg hash_alg);
213 #endif