3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
18 * Please visit http://www.xyratex.com/contact if you need additional
19 * information or have any questions.
25 * Copyright 2012 Xyratex Technology Limited
27 * Copyright (c) 2014, Intel Corporation.
30 #ifndef _LIBCFS_CRYPTO_H
31 #define _LIBCFS_CRYPTO_H
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 */
41 struct cfs_crypto_crypt_type {
42 char *cct_name; /**< crypto algorithm name, equal to
43 * format name for crypto api */
44 unsigned int cct_size; /**< crypto key size */
47 enum cfs_crypto_hash_alg {
48 CFS_HASH_ALG_NULL = 0,
52 /* hashes before here will be speed-tested at module load */
59 CFS_HASH_ALG_SPEED_MAX = CFS_HASH_ALG_MD5,
60 CFS_HASH_ALG_UNKNOWN = 0xff
63 enum cfs_crypto_crypt_alg {
64 CFS_CRYPT_ALG_NULL = 0,
65 CFS_CRYPT_ALG_AES256_CTR,
67 CFS_CRYPT_ALG_UNKNOWN = 0xff
70 static struct cfs_crypto_hash_type hash_types[] = {
71 [CFS_HASH_ALG_NULL] = {
76 [CFS_HASH_ALG_ADLER32] = {
77 .cht_name = "adler32",
81 [CFS_HASH_ALG_CRC32] = {
86 [CFS_HASH_ALG_CRC32C] = {
91 [CFS_HASH_ALG_MD5] = {
96 [CFS_HASH_ALG_SHA1] = {
101 [CFS_HASH_ALG_SHA256] = {
102 .cht_name = "sha256",
106 [CFS_HASH_ALG_SHA384] = {
107 .cht_name = "sha384",
111 [CFS_HASH_ALG_SHA512] = {
112 .cht_name = "sha512",
116 [CFS_HASH_ALG_MAX] = {
123 static struct cfs_crypto_crypt_type crypt_types[] = {
124 [CFS_CRYPT_ALG_NULL] = {
128 [CFS_CRYPT_ALG_AES256_CTR] = {
129 .cct_name = "ctr(aes)",
134 /* Maximum size of hash_types[].cht_size */
135 #define CFS_CRYPTO_HASH_DIGESTSIZE_MAX 64
137 /* Array of hash algorithm speed in MByte per second */
138 extern int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
141 * Return hash algorithm information for the specified algorithm identifier
143 * Hash information includes algorithm name, initial seed, hash size.
145 * \retval cfs_crypto_hash_type for valid ID (CFS_HASH_ALG_*)
146 * \retval NULL for unknown algorithm identifier
148 static inline const struct
149 cfs_crypto_hash_type *cfs_crypto_hash_type(enum cfs_crypto_hash_alg hash_alg)
151 struct cfs_crypto_hash_type *ht;
153 if (hash_alg < CFS_HASH_ALG_MAX) {
154 ht = &hash_types[hash_alg];
155 if (ht->cht_name != NULL)
162 * Return hash name for hash algorithm identifier
164 * \param[in] hash_alg hash alrgorithm id (CFS_HASH_ALG_*)
166 * \retval string name of known hash algorithm
167 * \retval "unknown" if hash algorithm is unknown
170 char *cfs_crypto_hash_name(enum cfs_crypto_hash_alg hash_alg)
172 const struct cfs_crypto_hash_type *ht;
174 ht = cfs_crypto_hash_type(hash_alg);
182 * Return digest size for hash algorithm type
184 * \param[in] hash_alg hash alrgorithm id (CFS_HASH_ALG_*)
186 * \retval hash algorithm digest size in bytes
187 * \retval 0 if hash algorithm type is unknown
190 unsigned int cfs_crypto_hash_digestsize(enum cfs_crypto_hash_alg hash_alg)
192 const struct cfs_crypto_hash_type *ht;
194 ht = cfs_crypto_hash_type(hash_alg);
202 * Find hash algorithm ID for the specified algorithm name
204 * \retval hash algorithm ID for valid ID (CFS_HASH_ALG_*)
205 * \retval CFS_HASH_ALG_UNKNOWN for unknown algorithm name
207 static inline unsigned char cfs_crypto_hash_alg(const char *algname)
209 enum cfs_crypto_hash_alg hash_alg;
211 for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++)
212 if (strcmp(hash_types[hash_alg].cht_name, algname) == 0)
215 return CFS_HASH_ALG_UNKNOWN;
219 * Return crypt algorithm information for the specified algorithm identifier
221 * Crypt information includes algorithm name, key size.
223 * \retval cfs_crypto_crupt_type for valid ID (CFS_CRYPT_ALG_*)
224 * \retval NULL for unknown algorithm identifier
226 static inline const struct
227 cfs_crypto_crypt_type *cfs_crypto_crypt_type(
228 enum cfs_crypto_crypt_alg crypt_alg)
230 struct cfs_crypto_crypt_type *ct;
232 if (crypt_alg < CFS_CRYPT_ALG_MAX) {
233 ct = &crypt_types[crypt_alg];
234 if (ct->cct_name != NULL)
241 * Return crypt name for crypt algorithm identifier
243 * \param[in] crypt_alg crypt alrgorithm id (CFS_CRYPT_ALG_*)
245 * \retval string name of known crypt algorithm
246 * \retval "unknown" if hash algorithm is unknown
249 char *cfs_crypto_crypt_name(enum cfs_crypto_crypt_alg crypt_alg)
251 const struct cfs_crypto_crypt_type *ct;
253 ct = cfs_crypto_crypt_type(crypt_alg);
262 * Return key size for crypto algorithm type
264 * \param[in] crypt_alg crypt alrgorithm id (CFS_CRYPT_ALG_*)
266 * \retval crypt algorithm key size in bytes
267 * \retval 0 if crypt algorithm type is unknown
270 unsigned int cfs_crypto_crypt_keysize(enum cfs_crypto_crypt_alg crypt_alg)
272 const struct cfs_crypto_crypt_type *ct;
274 ct = cfs_crypto_crypt_type(crypt_alg);
282 * Find crypto algorithm ID for the specified algorithm name
284 * \retval crypto algorithm ID for valid ID (CFS_CRYPT_ALG_*)
285 * \retval CFS_CRYPT_ALG_UNKNOWN for unknown algorithm name
287 static inline unsigned char cfs_crypto_crypt_alg(const char *algname)
289 enum cfs_crypto_crypt_alg crypt_alg;
291 for (crypt_alg = 0; crypt_alg < CFS_CRYPT_ALG_MAX; crypt_alg++)
292 if (strcmp(crypt_types[crypt_alg].cct_name, algname) == 0)
295 return CFS_CRYPT_ALG_UNKNOWN;
298 int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg,
299 const void *buf, unsigned int buf_len,
300 unsigned char *key, unsigned int key_len,
301 unsigned char *hash, unsigned int *hash_len);
303 /* cfs crypto hash descriptor */
306 struct ahash_request *
307 cfs_crypto_hash_init(enum cfs_crypto_hash_alg hash_alg,
308 unsigned char *key, unsigned int key_len);
309 int cfs_crypto_hash_update_page(struct ahash_request *req,
310 struct page *page, unsigned int offset,
312 int cfs_crypto_hash_update(struct ahash_request *req, const void *buf,
313 unsigned int buf_len);
314 int cfs_crypto_hash_final(struct ahash_request *req,
315 unsigned char *hash, unsigned int *hash_len);
316 int cfs_crypto_register(void);
317 void cfs_crypto_unregister(void);
318 int cfs_crypto_hash_speed(enum cfs_crypto_hash_alg hash_alg);