Back to home page

OSCL-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 a 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 crc, const u8 *data, unsigned int len, u8 *out)
0069 {
0070     *(__u16 *)out = crc_t10dif_generic(crc, data, len);
0071     return 0;
0072 }
0073 
0074 static int chksum_finup(struct shash_desc *desc, const u8 *data,
0075             unsigned int len, u8 *out)
0076 {
0077     struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0078 
0079     return __chksum_finup(ctx->crc, data, len, out);
0080 }
0081 
0082 static int chksum_digest(struct shash_desc *desc, const u8 *data,
0083              unsigned int length, u8 *out)
0084 {
0085     return __chksum_finup(0, data, length, out);
0086 }
0087 
0088 static struct shash_alg alg = {
0089     .digestsize     =   CRC_T10DIF_DIGEST_SIZE,
0090     .init       =   chksum_init,
0091     .update     =   chksum_update,
0092     .final      =   chksum_final,
0093     .finup      =   chksum_finup,
0094     .digest     =   chksum_digest,
0095     .descsize       =   sizeof(struct chksum_desc_ctx),
0096     .base           =   {
0097         .cra_name       =   "crct10dif",
0098         .cra_driver_name    =   "crct10dif-generic",
0099         .cra_priority       =   100,
0100         .cra_blocksize      =   CRC_T10DIF_BLOCK_SIZE,
0101         .cra_module     =   THIS_MODULE,
0102     }
0103 };
0104 
0105 static int __init crct10dif_mod_init(void)
0106 {
0107     return crypto_register_shash(&alg);
0108 }
0109 
0110 static void __exit crct10dif_mod_fini(void)
0111 {
0112     crypto_unregister_shash(&alg);
0113 }
0114 
0115 subsys_initcall(crct10dif_mod_init);
0116 module_exit(crct10dif_mod_fini);
0117 
0118 MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
0119 MODULE_DESCRIPTION("T10 DIF CRC calculation.");
0120 MODULE_LICENSE("GPL");
0121 MODULE_ALIAS_CRYPTO("crct10dif");
0122 MODULE_ALIAS_CRYPTO("crct10dif-generic");