Back to home page

LXR

 
 

    


0001 /*
0002  * Cryptographic API.
0003  *
0004  * T10 Data Integrity Field CRC16 Crypto Transform
0005  *
0006  * Copyright (c) 2007 Oracle Corporation.  All rights reserved.
0007  * Written by Martin K. Petersen <martin.petersen@oracle.com>
0008  * Copyright (C) 2013 Intel Corporation
0009  * Author: Tim Chen <tim.c.chen@linux.intel.com>
0010  *
0011  * This program is free software; you can redistribute it and/or modify it
0012  * under the terms of the GNU General Public License as published by the Free
0013  * Software Foundation; either version 2 of the License, or (at your option)
0014  * any later version.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0020  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0021  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0022  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023  * SOFTWARE.
0024  *
0025  */
0026 
0027 #include <linux/module.h>
0028 #include <linux/crc-t10dif.h>
0029 #include <crypto/internal/hash.h>
0030 #include <linux/init.h>
0031 #include <linux/kernel.h>
0032 
0033 struct chksum_desc_ctx {
0034     __u16 crc;
0035 };
0036 
0037 /*
0038  * Steps through buffer one byte at at time, calculates reflected
0039  * crc using table.
0040  */
0041 
0042 static int chksum_init(struct shash_desc *desc)
0043 {
0044     struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0045 
0046     ctx->crc = 0;
0047 
0048     return 0;
0049 }
0050 
0051 static int chksum_update(struct shash_desc *desc, const u8 *data,
0052              unsigned int length)
0053 {
0054     struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0055 
0056     ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
0057     return 0;
0058 }
0059 
0060 static int chksum_final(struct shash_desc *desc, u8 *out)
0061 {
0062     struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0063 
0064     *(__u16 *)out = ctx->crc;
0065     return 0;
0066 }
0067 
0068 static int __chksum_finup(__u16 *crcp, const u8 *data, unsigned int len,
0069             u8 *out)
0070 {
0071     *(__u16 *)out = crc_t10dif_generic(*crcp, data, len);
0072     return 0;
0073 }
0074 
0075 static int chksum_finup(struct shash_desc *desc, const u8 *data,
0076             unsigned int len, u8 *out)
0077 {
0078     struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0079 
0080     return __chksum_finup(&ctx->crc, data, len, out);
0081 }
0082 
0083 static int chksum_digest(struct shash_desc *desc, const u8 *data,
0084              unsigned int length, u8 *out)
0085 {
0086     struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0087 
0088     return __chksum_finup(&ctx->crc, data, length, out);
0089 }
0090 
0091 static struct shash_alg alg = {
0092     .digestsize     =   CRC_T10DIF_DIGEST_SIZE,
0093     .init       =   chksum_init,
0094     .update     =   chksum_update,
0095     .final      =   chksum_final,
0096     .finup      =   chksum_finup,
0097     .digest     =   chksum_digest,
0098     .descsize       =   sizeof(struct chksum_desc_ctx),
0099     .base           =   {
0100         .cra_name       =   "crct10dif",
0101         .cra_driver_name    =   "crct10dif-generic",
0102         .cra_priority       =   100,
0103         .cra_blocksize      =   CRC_T10DIF_BLOCK_SIZE,
0104         .cra_module     =   THIS_MODULE,
0105     }
0106 };
0107 
0108 static int __init crct10dif_mod_init(void)
0109 {
0110     return crypto_register_shash(&alg);
0111 }
0112 
0113 static void __exit crct10dif_mod_fini(void)
0114 {
0115     crypto_unregister_shash(&alg);
0116 }
0117 
0118 module_init(crct10dif_mod_init);
0119 module_exit(crct10dif_mod_fini);
0120 
0121 MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
0122 MODULE_DESCRIPTION("T10 DIF CRC calculation.");
0123 MODULE_LICENSE("GPL");
0124 MODULE_ALIAS_CRYPTO("crct10dif");
0125 MODULE_ALIAS_CRYPTO("crct10dif-generic");