18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Cryptographic API. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * T10 Data Integrity Field CRC16 Crypto Transform 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (c) 2007 Oracle Corporation. All rights reserved. 78c2ecf20Sopenharmony_ci * Written by Martin K. Petersen <martin.petersen@oracle.com> 88c2ecf20Sopenharmony_ci * Copyright (C) 2013 Intel Corporation 98c2ecf20Sopenharmony_ci * Author: Tim Chen <tim.c.chen@linux.intel.com> 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it 128c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License as published by the Free 138c2ecf20Sopenharmony_ci * Software Foundation; either version 2 of the License, or (at your option) 148c2ecf20Sopenharmony_ci * any later version. 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 178c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 188c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 198c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 208c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 218c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 228c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 238c2ecf20Sopenharmony_ci * SOFTWARE. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#include <linux/module.h> 288c2ecf20Sopenharmony_ci#include <linux/crc-t10dif.h> 298c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h> 308c2ecf20Sopenharmony_ci#include <linux/init.h> 318c2ecf20Sopenharmony_ci#include <linux/kernel.h> 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistruct chksum_desc_ctx { 348c2ecf20Sopenharmony_ci __u16 crc; 358c2ecf20Sopenharmony_ci}; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/* 388c2ecf20Sopenharmony_ci * Steps through buffer one byte at a time, calculates reflected 398c2ecf20Sopenharmony_ci * crc using table. 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic int chksum_init(struct shash_desc *desc) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci ctx->crc = 0; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci return 0; 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic int chksum_update(struct shash_desc *desc, const u8 *data, 528c2ecf20Sopenharmony_ci unsigned int length) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci ctx->crc = crc_t10dif_generic(ctx->crc, data, length); 578c2ecf20Sopenharmony_ci return 0; 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic int chksum_final(struct shash_desc *desc, u8 *out) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci *(__u16 *)out = ctx->crc; 658c2ecf20Sopenharmony_ci return 0; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci *(__u16 *)out = crc_t10dif_generic(crc, data, len); 718c2ecf20Sopenharmony_ci return 0; 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic int chksum_finup(struct shash_desc *desc, const u8 *data, 758c2ecf20Sopenharmony_ci unsigned int len, u8 *out) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci return __chksum_finup(ctx->crc, data, len, out); 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic int chksum_digest(struct shash_desc *desc, const u8 *data, 838c2ecf20Sopenharmony_ci unsigned int length, u8 *out) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci return __chksum_finup(0, data, length, out); 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic struct shash_alg alg = { 898c2ecf20Sopenharmony_ci .digestsize = CRC_T10DIF_DIGEST_SIZE, 908c2ecf20Sopenharmony_ci .init = chksum_init, 918c2ecf20Sopenharmony_ci .update = chksum_update, 928c2ecf20Sopenharmony_ci .final = chksum_final, 938c2ecf20Sopenharmony_ci .finup = chksum_finup, 948c2ecf20Sopenharmony_ci .digest = chksum_digest, 958c2ecf20Sopenharmony_ci .descsize = sizeof(struct chksum_desc_ctx), 968c2ecf20Sopenharmony_ci .base = { 978c2ecf20Sopenharmony_ci .cra_name = "crct10dif", 988c2ecf20Sopenharmony_ci .cra_driver_name = "crct10dif-generic", 998c2ecf20Sopenharmony_ci .cra_priority = 100, 1008c2ecf20Sopenharmony_ci .cra_blocksize = CRC_T10DIF_BLOCK_SIZE, 1018c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci}; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistatic int __init crct10dif_mod_init(void) 1068c2ecf20Sopenharmony_ci{ 1078c2ecf20Sopenharmony_ci return crypto_register_shash(&alg); 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistatic void __exit crct10dif_mod_fini(void) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci crypto_unregister_shash(&alg); 1138c2ecf20Sopenharmony_ci} 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_cisubsys_initcall(crct10dif_mod_init); 1168c2ecf20Sopenharmony_cimodule_exit(crct10dif_mod_fini); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>"); 1198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("T10 DIF CRC calculation."); 1208c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1218c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crct10dif"); 1228c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crct10dif-generic"); 123