blob: 43bc5b071f9698847e66353e867c7b4771628475 [file] [log] [blame]
Martin K. Petersenf11f5942008-06-25 11:22:42 -04001/*
2 * T10 Data Integrity Field CRC16 calculation
3 *
4 * Copyright (c) 2007 Oracle Corporation. All rights reserved.
5 * Written by Martin K. Petersen <martin.petersen@oracle.com>
6 *
7 * This source code is licensed under the GNU General Public License,
8 * Version 2. See the file COPYING for more details.
9 */
10
11#include <linux/types.h>
12#include <linux/module.h>
13#include <linux/crc-t10dif.h>
Herbert Xu684115212013-09-07 12:56:26 +100014#include <linux/err.h>
15#include <linux/init.h>
16#include <crypto/hash.h>
Martin K. Petersenf11f5942008-06-25 11:22:42 -040017
Herbert Xu684115212013-09-07 12:56:26 +100018static struct crypto_shash *crct10dif_tfm;
Martin K. Petersenf11f5942008-06-25 11:22:42 -040019
20__u16 crc_t10dif(const unsigned char *buffer, size_t len)
21{
Herbert Xu684115212013-09-07 12:56:26 +100022 struct {
23 struct shash_desc shash;
24 char ctx[2];
25 } desc;
26 int err;
Martin K. Petersenf11f5942008-06-25 11:22:42 -040027
Herbert Xu684115212013-09-07 12:56:26 +100028 desc.shash.tfm = crct10dif_tfm;
29 desc.shash.flags = 0;
30 *(__u16 *)desc.ctx = 0;
Martin K. Petersenf11f5942008-06-25 11:22:42 -040031
Herbert Xu684115212013-09-07 12:56:26 +100032 err = crypto_shash_update(&desc.shash, buffer, len);
33 BUG_ON(err);
34
35 return *(__u16 *)desc.ctx;
Martin K. Petersenf11f5942008-06-25 11:22:42 -040036}
37EXPORT_SYMBOL(crc_t10dif);
38
Herbert Xu684115212013-09-07 12:56:26 +100039static int __init crc_t10dif_mod_init(void)
40{
41 crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
42 return PTR_RET(crct10dif_tfm);
43}
44
45static void __exit crc_t10dif_mod_fini(void)
46{
47 crypto_free_shash(crct10dif_tfm);
48}
49
50module_init(crc_t10dif_mod_init);
51module_exit(crc_t10dif_mod_fini);
52
Martin K. Petersenf11f5942008-06-25 11:22:42 -040053MODULE_DESCRIPTION("T10 DIF CRC calculation");
54MODULE_LICENSE("GPL");
Herbert Xu684115212013-09-07 12:56:26 +100055MODULE_SOFTDEP("pre: crct10dif");