Martin K. Petersen | f11f594 | 2008-06-25 11:22:42 -0400 | [diff] [blame] | 1 | /* |
| 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> |
Tim Chen | 2d31e51 | 2013-05-01 12:52:48 -0700 | [diff] [blame^] | 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <crypto/hash.h> |
Martin K. Petersen | f11f594 | 2008-06-25 11:22:42 -0400 | [diff] [blame] | 17 | |
Tim Chen | 2d31e51 | 2013-05-01 12:52:48 -0700 | [diff] [blame^] | 18 | static struct crypto_shash *crct10dif_tfm; |
Martin K. Petersen | f11f594 | 2008-06-25 11:22:42 -0400 | [diff] [blame] | 19 | |
| 20 | __u16 crc_t10dif(const unsigned char *buffer, size_t len) |
| 21 | { |
Tim Chen | 2d31e51 | 2013-05-01 12:52:48 -0700 | [diff] [blame^] | 22 | struct { |
| 23 | struct shash_desc shash; |
| 24 | char ctx[2]; |
| 25 | } desc; |
| 26 | int err; |
Martin K. Petersen | f11f594 | 2008-06-25 11:22:42 -0400 | [diff] [blame] | 27 | |
Tim Chen | 2d31e51 | 2013-05-01 12:52:48 -0700 | [diff] [blame^] | 28 | desc.shash.tfm = crct10dif_tfm; |
| 29 | desc.shash.flags = 0; |
| 30 | *(__u16 *)desc.ctx = 0; |
Martin K. Petersen | f11f594 | 2008-06-25 11:22:42 -0400 | [diff] [blame] | 31 | |
Tim Chen | 2d31e51 | 2013-05-01 12:52:48 -0700 | [diff] [blame^] | 32 | err = crypto_shash_update(&desc.shash, buffer, len); |
| 33 | BUG_ON(err); |
| 34 | |
| 35 | return *(__u16 *)desc.ctx; |
Martin K. Petersen | f11f594 | 2008-06-25 11:22:42 -0400 | [diff] [blame] | 36 | } |
| 37 | EXPORT_SYMBOL(crc_t10dif); |
| 38 | |
Tim Chen | 2d31e51 | 2013-05-01 12:52:48 -0700 | [diff] [blame^] | 39 | static int __init crc_t10dif_mod_init(void) |
| 40 | { |
| 41 | crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0); |
| 42 | if (IS_ERR(crct10dif_tfm)) |
| 43 | return PTR_ERR(crct10dif_tfm); |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static void __exit crc_t10dif_mod_fini(void) |
| 48 | { |
| 49 | crypto_free_shash(crct10dif_tfm); |
| 50 | } |
| 51 | |
| 52 | module_init(crc_t10dif_mod_init); |
| 53 | module_exit(crc_t10dif_mod_fini); |
| 54 | |
Martin K. Petersen | f11f594 | 2008-06-25 11:22:42 -0400 | [diff] [blame] | 55 | MODULE_DESCRIPTION("T10 DIF CRC calculation"); |
| 56 | MODULE_LICENSE("GPL"); |