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 enum cfs_crypto_hash_alg {
42 CFS_HASH_ALG_NULL = 0,
46 /* hashes before here will be speed-tested at module load */
53 CFS_HASH_ALG_SPEED_MAX = CFS_HASH_ALG_MD5,
54 CFS_HASH_ALG_UNKNOWN = 0xff
57 static struct cfs_crypto_hash_type hash_types[] = {
58 [CFS_HASH_ALG_NULL] = {
63 [CFS_HASH_ALG_ADLER32] = {
64 .cht_name = "adler32",
68 [CFS_HASH_ALG_CRC32] = {
73 [CFS_HASH_ALG_CRC32C] = {
78 [CFS_HASH_ALG_MD5] = {
83 [CFS_HASH_ALG_SHA1] = {
88 [CFS_HASH_ALG_SHA256] = {
93 [CFS_HASH_ALG_SHA384] = {
98 [CFS_HASH_ALG_SHA512] = {
103 [CFS_HASH_ALG_MAX] = {
110 /* Maximum size of hash_types[].cht_size */
111 #define CFS_CRYPTO_HASH_DIGESTSIZE_MAX 64
114 * Return hash algorithm information for the specified algorithm identifier
116 * Hash information includes algorithm name, initial seed, hash size.
118 * \retval cfs_crypto_hash_type for valid ID (CFS_HASH_ALG_*)
119 * \retval NULL for unknown algorithm identifier
121 static inline const struct
122 cfs_crypto_hash_type *cfs_crypto_hash_type(enum cfs_crypto_hash_alg hash_alg)
124 struct cfs_crypto_hash_type *ht;
126 if (hash_alg < CFS_HASH_ALG_MAX) {
127 ht = &hash_types[hash_alg];
128 if (ht->cht_name != NULL)
135 * Return hash name for hash algorithm identifier
137 * \param[in] hash_alg hash alrgorithm id (CFS_HASH_ALG_*)
139 * \retval string name of known hash algorithm
140 * \retval "unknown" if hash algorithm is unknown
143 char *cfs_crypto_hash_name(enum cfs_crypto_hash_alg hash_alg)
145 const struct cfs_crypto_hash_type *ht;
147 ht = cfs_crypto_hash_type(hash_alg);
155 * Return digest size for hash algorithm type
157 * \param[in] hash_alg hash alrgorithm id (CFS_HASH_ALG_*)
159 * \retval hash algorithm digest size in bytes
160 * \retval 0 if hash algorithm type is unknown
163 unsigned int cfs_crypto_hash_digestsize(enum cfs_crypto_hash_alg hash_alg)
165 const struct cfs_crypto_hash_type *ht;
167 ht = cfs_crypto_hash_type(hash_alg);
175 * Find hash algorithm ID for the specified algorithm name
177 * \retval hash algorithm ID for valid ID (CFS_HASH_ALG_*)
178 * \retval CFS_HASH_ALG_UNKNOWN for unknown algorithm name
180 static inline unsigned char cfs_crypto_hash_alg(const char *algname)
182 enum cfs_crypto_hash_alg hash_alg;
184 for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++)
185 if (strcmp(hash_types[hash_alg].cht_name, algname) == 0)
188 return CFS_HASH_ALG_UNKNOWN;
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);
196 /* cfs crypto hash descriptor */
197 struct cfs_crypto_hash_desc;
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,
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);