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,
52 CFS_HASH_ALG_UNKNOWN = 0xff
55 static struct cfs_crypto_hash_type hash_types[] = {
56 [CFS_HASH_ALG_NULL] = {
61 [CFS_HASH_ALG_ADLER32] = {
62 .cht_name = "adler32",
66 [CFS_HASH_ALG_CRC32] = {
71 [CFS_HASH_ALG_CRC32C] = {
76 [CFS_HASH_ALG_MD5] = {
81 [CFS_HASH_ALG_SHA1] = {
86 [CFS_HASH_ALG_SHA256] = {
91 [CFS_HASH_ALG_SHA384] = {
96 [CFS_HASH_ALG_SHA512] = {
101 [CFS_HASH_ALG_MAX] = {
108 /* Maximum size of hash_types[].cht_size */
109 #define CFS_CRYPTO_HASH_DIGESTSIZE_MAX 64
112 * Return hash algorithm information for the specified algorithm identifier
114 * Hash information includes algorithm name, initial seed, hash size.
116 * \retval cfs_crypto_hash_type for valid ID (CFS_HASH_ALG_*)
117 * \retval NULL for unknown algorithm identifier
119 static inline const struct
120 cfs_crypto_hash_type *cfs_crypto_hash_type(enum cfs_crypto_hash_alg hash_alg)
122 struct cfs_crypto_hash_type *ht;
124 if (hash_alg < CFS_HASH_ALG_MAX) {
125 ht = &hash_types[hash_alg];
126 if (ht->cht_name != NULL)
133 * Return hash name for hash algorithm identifier
135 * \param[in] hash_alg hash alrgorithm id (CFS_HASH_ALG_*)
137 * \retval string name of known hash algorithm
138 * \retval "unknown" if hash algorithm is unknown
141 char *cfs_crypto_hash_name(enum cfs_crypto_hash_alg hash_alg)
143 const struct cfs_crypto_hash_type *ht;
145 ht = cfs_crypto_hash_type(hash_alg);
153 * Return digest size for hash algorithm type
155 * \param[in] hash_alg hash alrgorithm id (CFS_HASH_ALG_*)
157 * \retval hash algorithm digest size in bytes
158 * \retval 0 if hash algorithm type is unknown
161 unsigned int cfs_crypto_hash_digestsize(enum cfs_crypto_hash_alg hash_alg)
163 const struct cfs_crypto_hash_type *ht;
165 ht = cfs_crypto_hash_type(hash_alg);
173 * Find hash algorithm ID for the specified algorithm name
175 * \retval hash algorithm ID for valid ID (CFS_HASH_ALG_*)
176 * \retval CFS_HASH_ALG_UNKNOWN for unknown algorithm name
178 static inline unsigned char cfs_crypto_hash_alg(const char *algname)
180 enum cfs_crypto_hash_alg hash_alg;
182 for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++)
183 if (strcmp(hash_types[hash_alg].cht_name, algname) == 0)
186 return CFS_HASH_ALG_UNKNOWN;
189 int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg,
190 const void *buf, unsigned int buf_len,
191 unsigned char *key, unsigned int key_len,
192 unsigned char *hash, unsigned int *hash_len);
194 /* cfs crypto hash descriptor */
195 struct cfs_crypto_hash_desc;
197 struct cfs_crypto_hash_desc *
198 cfs_crypto_hash_init(enum cfs_crypto_hash_alg hash_alg,
199 unsigned char *key, unsigned int key_len);
200 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *desc,
201 struct page *page, unsigned int offset,
203 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *desc, const void *buf,
204 unsigned int buf_len);
205 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *desc,
206 unsigned char *hash, unsigned int *hash_len);
207 int cfs_crypto_register(void);
208 void cfs_crypto_unregister(void);
209 int cfs_crypto_hash_speed(enum cfs_crypto_hash_alg hash_alg);