Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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) 2012, Intel Corporation. |
| 28 | */ |
| 29 | |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 30 | #include <crypto/hash.h> |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
Greg Kroah-Hartman | 9fdaf8c | 2014-07-11 20:51:16 -0700 | [diff] [blame] | 32 | #include "../../../include/linux/libcfs/libcfs.h" |
Andreas Dilger | 997e518 | 2016-03-26 15:40:45 -0400 | [diff] [blame] | 33 | #include "../../../include/linux/libcfs/libcfs_crypto.h" |
Greg Kroah-Hartman | 13636be | 2014-07-11 23:12:04 -0700 | [diff] [blame] | 34 | #include "linux-crypto.h" |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 35 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 36 | /** |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 37 | * Array of hash algorithm speed in MByte per second |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 38 | */ |
| 39 | static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX]; |
| 40 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 41 | /** |
| 42 | * Initialize the state descriptor for the specified hash algorithm. |
| 43 | * |
| 44 | * An internal routine to allocate the hash-specific state in \a hdesc for |
| 45 | * use with cfs_crypto_hash_digest() to compute the hash of a single message, |
| 46 | * though possibly in multiple chunks. The descriptor internal state should |
| 47 | * be freed with cfs_crypto_hash_final(). |
| 48 | * |
| 49 | * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*) |
| 50 | * \param[out] type pointer to the hash description in hash_types[] |
| 51 | * array |
| 52 | * \param[in,out] hdesc hash state descriptor to be initialized |
| 53 | * \param[in] key initial hash value/state, NULL to use default |
| 54 | * value |
| 55 | * \param[in] key_len length of \a key |
| 56 | * |
| 57 | * \retval 0 on success |
| 58 | * \retval negative errno on failure |
| 59 | */ |
Andreas Dilger | 244cd87 | 2016-03-26 15:40:50 -0400 | [diff] [blame] | 60 | static int cfs_crypto_hash_alloc(enum cfs_crypto_hash_alg hash_alg, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 61 | const struct cfs_crypto_hash_type **type, |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 62 | struct ahash_request **req, |
| 63 | unsigned char *key, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 64 | unsigned int key_len) |
| 65 | { |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 66 | struct crypto_ahash *tfm; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 67 | int err = 0; |
| 68 | |
Andreas Dilger | 4d60ffa | 2016-03-26 15:40:47 -0400 | [diff] [blame] | 69 | *type = cfs_crypto_hash_type(hash_alg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 70 | |
Oleg Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 71 | if (!*type) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 72 | CWARN("Unsupported hash algorithm id = %d, max id is %d\n", |
Andreas Dilger | 4d60ffa | 2016-03-26 15:40:47 -0400 | [diff] [blame] | 73 | hash_alg, CFS_HASH_ALG_MAX); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 74 | return -EINVAL; |
| 75 | } |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 76 | tfm = crypto_alloc_ahash((*type)->cht_name, 0, CRYPTO_ALG_ASYNC); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 77 | |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 78 | if (IS_ERR(tfm)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 79 | CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n", |
| 80 | (*type)->cht_name); |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 81 | return PTR_ERR(tfm); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 84 | *req = ahash_request_alloc(tfm, GFP_KERNEL); |
| 85 | if (!*req) { |
| 86 | CDEBUG(D_INFO, "Failed to alloc ahash_request for %s\n", |
| 87 | (*type)->cht_name); |
| 88 | crypto_free_ahash(tfm); |
| 89 | return -ENOMEM; |
| 90 | } |
| 91 | |
| 92 | ahash_request_set_callback(*req, 0, NULL, NULL); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 93 | |
Oleg Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 94 | if (key) |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 95 | err = crypto_ahash_setkey(tfm, key, key_len); |
Shraddha Barke | bd2909f | 2015-09-05 18:58:21 +0530 | [diff] [blame] | 96 | else if ((*type)->cht_key != 0) |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 97 | err = crypto_ahash_setkey(tfm, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 98 | (unsigned char *)&((*type)->cht_key), |
| 99 | (*type)->cht_size); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 100 | |
| 101 | if (err != 0) { |
James Simmons | cd779f2 | 2016-05-09 10:53:47 -0400 | [diff] [blame] | 102 | ahash_request_free(*req); |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 103 | crypto_free_ahash(tfm); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 104 | return err; |
| 105 | } |
| 106 | |
| 107 | CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n", |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 108 | crypto_ahash_alg_name(tfm), crypto_ahash_driver_name(tfm), |
Andreas Dilger | 4d60ffa | 2016-03-26 15:40:47 -0400 | [diff] [blame] | 109 | cfs_crypto_hash_speeds[hash_alg]); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 110 | |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 111 | err = crypto_ahash_init(*req); |
| 112 | if (err) { |
| 113 | ahash_request_free(*req); |
| 114 | crypto_free_ahash(tfm); |
| 115 | } |
| 116 | return err; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 117 | } |
| 118 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 119 | /** |
| 120 | * Calculate hash digest for the passed buffer. |
| 121 | * |
| 122 | * This should be used when computing the hash on a single contiguous buffer. |
| 123 | * It combines the hash initialization, computation, and cleanup. |
| 124 | * |
| 125 | * \param[in] hash_alg id of hash algorithm (CFS_HASH_ALG_*) |
| 126 | * \param[in] buf data buffer on which to compute hash |
| 127 | * \param[in] buf_len length of \a buf in bytes |
| 128 | * \param[in] key initial value/state for algorithm, |
| 129 | * if \a key = NULL use default initial value |
| 130 | * \param[in] key_len length of \a key in bytes |
| 131 | * \param[out] hash pointer to computed hash value, |
| 132 | * if \a hash = NULL then \a hash_len is to digest |
| 133 | * size in bytes, retval -ENOSPC |
| 134 | * \param[in,out] hash_len size of \a hash buffer |
| 135 | * |
| 136 | * \retval -EINVAL \a buf, \a buf_len, \a hash_len, |
Andreas Dilger | 4d60ffa | 2016-03-26 15:40:47 -0400 | [diff] [blame] | 137 | * \a hash_alg invalid |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 138 | * \retval -ENOENT \a hash_alg is unsupported |
| 139 | * \retval -ENOSPC \a hash is NULL, or \a hash_len less than |
| 140 | * digest size |
| 141 | * \retval 0 for success |
| 142 | * \retval negative errno for other errors from lower |
| 143 | * layers. |
| 144 | */ |
Andreas Dilger | 244cd87 | 2016-03-26 15:40:50 -0400 | [diff] [blame] | 145 | int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 146 | const void *buf, unsigned int buf_len, |
| 147 | unsigned char *key, unsigned int key_len, |
| 148 | unsigned char *hash, unsigned int *hash_len) |
| 149 | { |
| 150 | struct scatterlist sl; |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 151 | struct ahash_request *req; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 152 | int err; |
| 153 | const struct cfs_crypto_hash_type *type; |
| 154 | |
Oleg Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 155 | if (!buf || buf_len == 0 || !hash_len) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 156 | return -EINVAL; |
| 157 | |
Andreas Dilger | 4d60ffa | 2016-03-26 15:40:47 -0400 | [diff] [blame] | 158 | err = cfs_crypto_hash_alloc(hash_alg, &type, &req, key, key_len); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 159 | if (err != 0) |
| 160 | return err; |
| 161 | |
Oleg Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 162 | if (!hash || *hash_len < type->cht_size) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 163 | *hash_len = type->cht_size; |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 164 | crypto_free_ahash(crypto_ahash_reqtfm(req)); |
| 165 | ahash_request_free(req); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 166 | return -ENOSPC; |
| 167 | } |
Shraddha Barke | 0d74951 | 2015-07-27 22:20:35 +0530 | [diff] [blame] | 168 | sg_init_one(&sl, buf, buf_len); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 169 | |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 170 | ahash_request_set_crypt(req, &sl, hash, sl.length); |
| 171 | err = crypto_ahash_digest(req); |
| 172 | crypto_free_ahash(crypto_ahash_reqtfm(req)); |
| 173 | ahash_request_free(req); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 174 | |
| 175 | return err; |
| 176 | } |
| 177 | EXPORT_SYMBOL(cfs_crypto_hash_digest); |
| 178 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 179 | /** |
| 180 | * Allocate and initialize desriptor for hash algorithm. |
| 181 | * |
| 182 | * This should be used to initialize a hash descriptor for multiple calls |
| 183 | * to a single hash function when computing the hash across multiple |
| 184 | * separate buffers or pages using cfs_crypto_hash_update{,_page}(). |
| 185 | * |
| 186 | * The hash descriptor should be freed with cfs_crypto_hash_final(). |
| 187 | * |
| 188 | * \param[in] hash_alg algorithm id (CFS_HASH_ALG_*) |
| 189 | * \param[in] key initial value/state for algorithm, if \a key = NULL |
| 190 | * use default initial value |
| 191 | * \param[in] key_len length of \a key in bytes |
| 192 | * |
| 193 | * \retval pointer to descriptor of hash instance |
| 194 | * \retval ERR_PTR(errno) in case of error |
| 195 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 196 | struct cfs_crypto_hash_desc * |
Andreas Dilger | 244cd87 | 2016-03-26 15:40:50 -0400 | [diff] [blame] | 197 | cfs_crypto_hash_init(enum cfs_crypto_hash_alg hash_alg, |
Andreas Dilger | 56ebc2e | 2016-03-26 15:40:49 -0400 | [diff] [blame] | 198 | unsigned char *key, unsigned int key_len) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 199 | { |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 200 | struct ahash_request *req; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 201 | int err; |
| 202 | const struct cfs_crypto_hash_type *type; |
| 203 | |
Andreas Dilger | 4d60ffa | 2016-03-26 15:40:47 -0400 | [diff] [blame] | 204 | err = cfs_crypto_hash_alloc(hash_alg, &type, &req, key, key_len); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 205 | |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 206 | if (err) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 207 | return ERR_PTR(err); |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 208 | return (struct cfs_crypto_hash_desc *)req; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 209 | } |
| 210 | EXPORT_SYMBOL(cfs_crypto_hash_init); |
| 211 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 212 | /** |
| 213 | * Update hash digest computed on data within the given \a page |
| 214 | * |
| 215 | * \param[in] hdesc hash state descriptor |
| 216 | * \param[in] page data page on which to compute the hash |
| 217 | * \param[in] offset offset within \a page at which to start hash |
| 218 | * \param[in] len length of data on which to compute hash |
| 219 | * |
| 220 | * \retval 0 for success |
| 221 | * \retval negative errno on failure |
| 222 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 223 | int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc, |
| 224 | struct page *page, unsigned int offset, |
| 225 | unsigned int len) |
| 226 | { |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 227 | struct ahash_request *req = (void *)hdesc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 228 | struct scatterlist sl; |
| 229 | |
| 230 | sg_init_table(&sl, 1); |
Oleg Drokin | 616387e | 2016-03-30 19:48:23 -0400 | [diff] [blame] | 231 | sg_set_page(&sl, page, len, offset & ~PAGE_MASK); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 232 | |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 233 | ahash_request_set_crypt(req, &sl, NULL, sl.length); |
| 234 | return crypto_ahash_update(req); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 235 | } |
| 236 | EXPORT_SYMBOL(cfs_crypto_hash_update_page); |
| 237 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 238 | /** |
| 239 | * Update hash digest computed on the specified data |
| 240 | * |
| 241 | * \param[in] hdesc hash state descriptor |
| 242 | * \param[in] buf data buffer on which to compute the hash |
| 243 | * \param[in] buf_len length of \buf on which to compute hash |
| 244 | * |
| 245 | * \retval 0 for success |
| 246 | * \retval negative errno on failure |
| 247 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 248 | int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc, |
| 249 | const void *buf, unsigned int buf_len) |
| 250 | { |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 251 | struct ahash_request *req = (void *)hdesc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 252 | struct scatterlist sl; |
| 253 | |
Shraddha Barke | 0d74951 | 2015-07-27 22:20:35 +0530 | [diff] [blame] | 254 | sg_init_one(&sl, buf, buf_len); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 255 | |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 256 | ahash_request_set_crypt(req, &sl, NULL, sl.length); |
| 257 | return crypto_ahash_update(req); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 258 | } |
| 259 | EXPORT_SYMBOL(cfs_crypto_hash_update); |
| 260 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 261 | /** |
| 262 | * Finish hash calculation, copy hash digest to buffer, clean up hash descriptor |
| 263 | * |
| 264 | * \param[in] hdesc hash descriptor |
| 265 | * \param[out] hash pointer to hash buffer to store hash digest |
| 266 | * \param[in,out] hash_len pointer to hash buffer size, if \a hdesc = NULL |
| 267 | * only free \a hdesc instead of computing the hash |
| 268 | * |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 269 | * \retval 0 for success |
Andreas Dilger | c11e27a | 2016-03-26 15:40:51 -0400 | [diff] [blame] | 270 | * \retval -EOVERFLOW if hash_len is too small for the hash digest |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 271 | * \retval negative errno for other errors from lower layers |
| 272 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 273 | int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc, |
| 274 | unsigned char *hash, unsigned int *hash_len) |
| 275 | { |
| 276 | int err; |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 277 | struct ahash_request *req = (void *)hdesc; |
| 278 | int size = crypto_ahash_digestsize(crypto_ahash_reqtfm(req)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 279 | |
Andreas Dilger | c11e27a | 2016-03-26 15:40:51 -0400 | [diff] [blame] | 280 | if (!hash || !hash_len) { |
| 281 | err = 0; |
| 282 | goto free_ahash; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 283 | } |
Andreas Dilger | c11e27a | 2016-03-26 15:40:51 -0400 | [diff] [blame] | 284 | if (*hash_len < size) { |
| 285 | err = -EOVERFLOW; |
| 286 | goto free_ahash; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 287 | } |
Andreas Dilger | c11e27a | 2016-03-26 15:40:51 -0400 | [diff] [blame] | 288 | |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 289 | ahash_request_set_crypt(req, NULL, hash, 0); |
| 290 | err = crypto_ahash_final(req); |
Andreas Dilger | c11e27a | 2016-03-26 15:40:51 -0400 | [diff] [blame] | 291 | if (!err) |
| 292 | *hash_len = size; |
| 293 | free_ahash: |
Herbert Xu | 6dae100 | 2016-02-01 21:36:53 +0800 | [diff] [blame] | 294 | crypto_free_ahash(crypto_ahash_reqtfm(req)); |
| 295 | ahash_request_free(req); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 296 | return err; |
| 297 | } |
| 298 | EXPORT_SYMBOL(cfs_crypto_hash_final); |
| 299 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 300 | /** |
| 301 | * Compute the speed of specified hash function |
| 302 | * |
| 303 | * Run a speed test on the given hash algorithm on buffer of the given size. |
| 304 | * The speed is stored internally in the cfs_crypto_hash_speeds[] array, and |
| 305 | * is available through the cfs_crypto_hash_speed() function. |
| 306 | * |
| 307 | * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*) |
| 308 | * \param[in] buf data buffer on which to compute the hash |
| 309 | * \param[in] buf_len length of \buf on which to compute hash |
| 310 | */ |
Andreas Dilger | 6ba3f37 | 2016-03-26 15:40:53 -0400 | [diff] [blame] | 311 | static void cfs_crypto_performance_test(enum cfs_crypto_hash_alg hash_alg) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 312 | { |
Andreas Dilger | 6ba3f37 | 2016-03-26 15:40:53 -0400 | [diff] [blame] | 313 | int buf_len = max(PAGE_SIZE, 1048576UL); |
| 314 | void *buf; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 315 | unsigned long start, end; |
| 316 | int bcount, err = 0; |
Andreas Dilger | 6ba3f37 | 2016-03-26 15:40:53 -0400 | [diff] [blame] | 317 | struct page *page; |
Andreas Dilger | 24a4e1e | 2016-03-26 15:40:48 -0400 | [diff] [blame] | 318 | unsigned char hash[CFS_CRYPTO_HASH_DIGESTSIZE_MAX]; |
| 319 | unsigned int hash_len = sizeof(hash); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 320 | |
Andreas Dilger | 6ba3f37 | 2016-03-26 15:40:53 -0400 | [diff] [blame] | 321 | page = alloc_page(GFP_KERNEL); |
| 322 | if (!page) { |
| 323 | err = -ENOMEM; |
| 324 | goto out_err; |
| 325 | } |
| 326 | |
| 327 | buf = kmap(page); |
| 328 | memset(buf, 0xAD, PAGE_SIZE); |
| 329 | kunmap(page); |
| 330 | |
Jian Yu | 304d13f | 2016-03-27 20:26:27 -0400 | [diff] [blame] | 331 | for (start = jiffies, end = start + msecs_to_jiffies(MSEC_PER_SEC), |
| 332 | bcount = 0; time_before(jiffies, end); bcount++) { |
Andreas Dilger | 1bc55f7 | 2016-03-26 15:40:56 -0400 | [diff] [blame] | 333 | struct cfs_crypto_hash_desc *hdesc; |
| 334 | int i; |
| 335 | |
| 336 | hdesc = cfs_crypto_hash_init(hash_alg, NULL, 0); |
| 337 | if (IS_ERR(hdesc)) { |
| 338 | err = PTR_ERR(hdesc); |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | for (i = 0; i < buf_len / PAGE_SIZE; i++) { |
| 343 | err = cfs_crypto_hash_update_page(hdesc, page, 0, |
| 344 | PAGE_SIZE); |
| 345 | if (err) |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | err = cfs_crypto_hash_final(hdesc, hash, &hash_len); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 350 | if (err) |
| 351 | break; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 352 | } |
| 353 | end = jiffies; |
Andreas Dilger | 6ba3f37 | 2016-03-26 15:40:53 -0400 | [diff] [blame] | 354 | __free_page(page); |
| 355 | out_err: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 356 | if (err) { |
Andreas Dilger | 78675cb | 2016-03-26 15:40:55 -0400 | [diff] [blame] | 357 | cfs_crypto_hash_speeds[hash_alg] = err; |
| 358 | CDEBUG(D_INFO, "Crypto hash algorithm %s test error: rc = %d\n", |
Andreas Dilger | 4d60ffa | 2016-03-26 15:40:47 -0400 | [diff] [blame] | 359 | cfs_crypto_hash_name(hash_alg), err); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 360 | } else { |
| 361 | unsigned long tmp; |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 362 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 363 | tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) * |
| 364 | 1000) / (1024 * 1024); |
Andreas Dilger | 4d60ffa | 2016-03-26 15:40:47 -0400 | [diff] [blame] | 365 | cfs_crypto_hash_speeds[hash_alg] = (int)tmp; |
Andreas Dilger | 8260d4e | 2016-03-26 15:40:54 -0400 | [diff] [blame] | 366 | CDEBUG(D_CONFIG, "Crypto hash algorithm %s speed = %d MB/s\n", |
| 367 | cfs_crypto_hash_name(hash_alg), |
| 368 | cfs_crypto_hash_speeds[hash_alg]); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 369 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 370 | } |
| 371 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 372 | /** |
| 373 | * hash speed in Mbytes per second for valid hash algorithm |
| 374 | * |
| 375 | * Return the performance of the specified \a hash_alg that was previously |
| 376 | * computed using cfs_crypto_performance_test(). |
| 377 | * |
| 378 | * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*) |
| 379 | * |
| 380 | * \retval positive speed of the hash function in MB/s |
| 381 | * \retval -ENOENT if \a hash_alg is unsupported |
| 382 | * \retval negative errno if \a hash_alg speed is unavailable |
| 383 | */ |
Andreas Dilger | 244cd87 | 2016-03-26 15:40:50 -0400 | [diff] [blame] | 384 | int cfs_crypto_hash_speed(enum cfs_crypto_hash_alg hash_alg) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 385 | { |
| 386 | if (hash_alg < CFS_HASH_ALG_MAX) |
| 387 | return cfs_crypto_hash_speeds[hash_alg]; |
Andreas Dilger | e43c658 | 2016-03-26 15:40:52 -0400 | [diff] [blame] | 388 | return -ENOENT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 389 | } |
| 390 | EXPORT_SYMBOL(cfs_crypto_hash_speed); |
| 391 | |
| 392 | /** |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 393 | * Run the performance test for all hash algorithms. |
| 394 | * |
| 395 | * Run the cfs_crypto_performance_test() benchmark for all of the available |
| 396 | * hash functions using a 1MB buffer size. This is a reasonable buffer size |
| 397 | * for Lustre RPCs, even if the actual RPC size is larger or smaller. |
| 398 | * |
| 399 | * Since the setup cost and computation speed of various hash algorithms is |
| 400 | * a function of the buffer size (and possibly internal contention of offload |
| 401 | * engines), this speed only represents an estimate of the actual speed under |
| 402 | * actual usage, but is reasonable for comparing available algorithms. |
| 403 | * |
| 404 | * The actual speeds are available via cfs_crypto_hash_speed() for later |
| 405 | * comparison. |
| 406 | * |
| 407 | * \retval 0 on success |
| 408 | * \retval -ENOMEM if no memory is available for test buffer |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 409 | */ |
| 410 | static int cfs_crypto_test_hashes(void) |
| 411 | { |
Andreas Dilger | 6ba3f37 | 2016-03-26 15:40:53 -0400 | [diff] [blame] | 412 | enum cfs_crypto_hash_alg hash_alg; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 413 | |
Andreas Dilger | 6ba3f37 | 2016-03-26 15:40:53 -0400 | [diff] [blame] | 414 | for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++) |
| 415 | cfs_crypto_performance_test(hash_alg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 416 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
Peng Tao | 9c782da | 2013-06-06 22:59:13 +0800 | [diff] [blame] | 420 | static int adler32; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 421 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 422 | /** |
| 423 | * Register available hash functions |
| 424 | * |
| 425 | * \retval 0 |
| 426 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 427 | int cfs_crypto_register(void) |
| 428 | { |
Alexander.Boyko | 7045b38 | 2013-07-23 00:06:49 +0800 | [diff] [blame] | 429 | request_module("crc32c"); |
| 430 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 431 | adler32 = cfs_crypto_adler32_register(); |
| 432 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 433 | /* check all algorithms and do performance test */ |
| 434 | cfs_crypto_test_hashes(); |
| 435 | return 0; |
| 436 | } |
Mike Rapoport | c9f6bb9 | 2015-10-13 16:03:42 +0300 | [diff] [blame] | 437 | |
Andreas Dilger | 020b021 | 2016-03-26 15:40:46 -0400 | [diff] [blame] | 438 | /** |
| 439 | * Unregister previously registered hash functions |
| 440 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 441 | void cfs_crypto_unregister(void) |
| 442 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 443 | if (adler32 == 0) |
| 444 | cfs_crypto_adler32_unregister(); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 445 | } |