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
138 * Return hash algorithm information for the specified algorithm identifier
140 * Hash information includes algorithm name, initial seed, hash size.
142 * \retval cfs_crypto_hash_type for valid ID (CFS_HASH_ALG_*)
143 * \retval NULL for unknown algorithm identifier
145 static inline const struct
146 cfs_crypto_hash_type *cfs_crypto_hash_type(enum cfs_crypto_hash_alg hash_alg)
148 struct cfs_crypto_hash_type *ht;
150 if (hash_alg < CFS_HASH_ALG_MAX) {
151 ht = &hash_types[hash_alg];
152 if (ht->cht_name != NULL)
159 * Return hash name for hash algorithm identifier
161 * \param[in] hash_alg hash alrgorithm id (CFS_HASH_ALG_*)
163 * \retval string name of known hash algorithm
164 * \retval "unknown" if hash algorithm is unknown
167 char *cfs_crypto_hash_name(enum cfs_crypto_hash_alg hash_alg)
169 const struct cfs_crypto_hash_type *ht;
171 ht = cfs_crypto_hash_type(hash_alg);
179 * Return digest size for hash algorithm type
181 * \param[in] hash_alg hash alrgorithm id (CFS_HASH_ALG_*)
183 * \retval hash algorithm digest size in bytes
184 * \retval 0 if hash algorithm type is unknown
187 unsigned int cfs_crypto_hash_digestsize(enum cfs_crypto_hash_alg hash_alg)
189 const struct cfs_crypto_hash_type *ht;
191 ht = cfs_crypto_hash_type(hash_alg);
199 * Find hash algorithm ID for the specified algorithm name
201 * \retval hash algorithm ID for valid ID (CFS_HASH_ALG_*)
202 * \retval CFS_HASH_ALG_UNKNOWN for unknown algorithm name
204 static inline unsigned char cfs_crypto_hash_alg(const char *algname)
206 enum cfs_crypto_hash_alg hash_alg;
208 for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++)
209 if (strcmp(hash_types[hash_alg].cht_name, algname) == 0)
212 return CFS_HASH_ALG_UNKNOWN;
216 * Return crypt algorithm information for the specified algorithm identifier
218 * Crypt information includes algorithm name, key size.
220 * \retval cfs_crypto_crupt_type for valid ID (CFS_CRYPT_ALG_*)
221 * \retval NULL for unknown algorithm identifier
223 static inline const struct
224 cfs_crypto_crypt_type *cfs_crypto_crypt_type(
225 enum cfs_crypto_crypt_alg crypt_alg)
227 struct cfs_crypto_crypt_type *ct;
229 if (crypt_alg < CFS_CRYPT_ALG_MAX) {
230 ct = &crypt_types[crypt_alg];
231 if (ct->cct_name != NULL)
238 * Return crypt name for crypt algorithm identifier
240 * \param[in] crypt_alg crypt alrgorithm id (CFS_CRYPT_ALG_*)
242 * \retval string name of known crypt algorithm
243 * \retval "unknown" if hash algorithm is unknown
246 char *cfs_crypto_crypt_name(enum cfs_crypto_crypt_alg crypt_alg)
248 const struct cfs_crypto_crypt_type *ct;
250 ct = cfs_crypto_crypt_type(crypt_alg);
259 * Return key size for crypto algorithm type
261 * \param[in] crypt_alg crypt alrgorithm id (CFS_CRYPT_ALG_*)
263 * \retval crypt algorithm key size in bytes
264 * \retval 0 if crypt algorithm type is unknown
267 unsigned int cfs_crypto_crypt_keysize(enum cfs_crypto_crypt_alg crypt_alg)
269 const struct cfs_crypto_crypt_type *ct;
271 ct = cfs_crypto_crypt_type(crypt_alg);
279 * Find crypto algorithm ID for the specified algorithm name
281 * \retval crypto algorithm ID for valid ID (CFS_CRYPT_ALG_*)
282 * \retval CFS_CRYPT_ALG_UNKNOWN for unknown algorithm name
284 static inline unsigned char cfs_crypto_crypt_alg(const char *algname)
286 enum cfs_crypto_crypt_alg crypt_alg;
288 for (crypt_alg = 0; crypt_alg < CFS_CRYPT_ALG_MAX; crypt_alg++)
289 if (strcmp(crypt_types[crypt_alg].cct_name, algname) == 0)
292 return CFS_CRYPT_ALG_UNKNOWN;
295 int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg,
296 const void *buf, unsigned int buf_len,
297 unsigned char *key, unsigned int key_len,
298 unsigned char *hash, unsigned int *hash_len);
300 /* cfs crypto hash descriptor */
303 struct ahash_request *
304 cfs_crypto_hash_init(enum cfs_crypto_hash_alg hash_alg,
305 unsigned char *key, unsigned int key_len);
306 int cfs_crypto_hash_update_page(struct ahash_request *req,
307 struct page *page, unsigned int offset,
309 int cfs_crypto_hash_update(struct ahash_request *req, const void *buf,
310 unsigned int buf_len);
311 int cfs_crypto_hash_final(struct ahash_request *req,
312 unsigned char *hash, unsigned int *hash_len);
313 int cfs_crypto_register(void);
314 void cfs_crypto_unregister(void);
315 int cfs_crypto_hash_speed(enum cfs_crypto_hash_alg hash_alg);