18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Cryptographic API.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * T10 Data Integrity Field CRC16 Crypto Transform using PCLMULQDQ Instructions
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2013 Intel Corporation
78c2ecf20Sopenharmony_ci * Author: Tim Chen <tim.c.chen@linux.intel.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it
108c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License as published by the Free
118c2ecf20Sopenharmony_ci * Software Foundation; either version 2 of the License, or (at your option)
128c2ecf20Sopenharmony_ci * any later version.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
158c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
168c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
178c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
188c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
198c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
208c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
218c2ecf20Sopenharmony_ci * SOFTWARE.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/types.h>
268c2ecf20Sopenharmony_ci#include <linux/module.h>
278c2ecf20Sopenharmony_ci#include <linux/crc-t10dif.h>
288c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
298c2ecf20Sopenharmony_ci#include <crypto/internal/simd.h>
308c2ecf20Sopenharmony_ci#include <linux/init.h>
318c2ecf20Sopenharmony_ci#include <linux/string.h>
328c2ecf20Sopenharmony_ci#include <linux/kernel.h>
338c2ecf20Sopenharmony_ci#include <asm/cpufeatures.h>
348c2ecf20Sopenharmony_ci#include <asm/cpu_device_id.h>
358c2ecf20Sopenharmony_ci#include <asm/simd.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciasmlinkage u16 crc_t10dif_pcl(u16 init_crc, const u8 *buf, size_t len);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct chksum_desc_ctx {
408c2ecf20Sopenharmony_ci	__u16 crc;
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int chksum_init(struct shash_desc *desc)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	ctx->crc = 0;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	return 0;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int chksum_update(struct shash_desc *desc, const u8 *data,
538c2ecf20Sopenharmony_ci			 unsigned int length)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (length >= 16 && crypto_simd_usable()) {
588c2ecf20Sopenharmony_ci		kernel_fpu_begin();
598c2ecf20Sopenharmony_ci		ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);
608c2ecf20Sopenharmony_ci		kernel_fpu_end();
618c2ecf20Sopenharmony_ci	} else
628c2ecf20Sopenharmony_ci		ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
638c2ecf20Sopenharmony_ci	return 0;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int chksum_final(struct shash_desc *desc, u8 *out)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	*(__u16 *)out = ctx->crc;
718c2ecf20Sopenharmony_ci	return 0;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	if (len >= 16 && crypto_simd_usable()) {
778c2ecf20Sopenharmony_ci		kernel_fpu_begin();
788c2ecf20Sopenharmony_ci		*(__u16 *)out = crc_t10dif_pcl(crc, data, len);
798c2ecf20Sopenharmony_ci		kernel_fpu_end();
808c2ecf20Sopenharmony_ci	} else
818c2ecf20Sopenharmony_ci		*(__u16 *)out = crc_t10dif_generic(crc, data, len);
828c2ecf20Sopenharmony_ci	return 0;
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic int chksum_finup(struct shash_desc *desc, const u8 *data,
868c2ecf20Sopenharmony_ci			unsigned int len, u8 *out)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return __chksum_finup(ctx->crc, data, len, out);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int chksum_digest(struct shash_desc *desc, const u8 *data,
948c2ecf20Sopenharmony_ci			 unsigned int length, u8 *out)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	return __chksum_finup(0, data, length, out);
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic struct shash_alg alg = {
1008c2ecf20Sopenharmony_ci	.digestsize		=	CRC_T10DIF_DIGEST_SIZE,
1018c2ecf20Sopenharmony_ci	.init		=	chksum_init,
1028c2ecf20Sopenharmony_ci	.update		=	chksum_update,
1038c2ecf20Sopenharmony_ci	.final		=	chksum_final,
1048c2ecf20Sopenharmony_ci	.finup		=	chksum_finup,
1058c2ecf20Sopenharmony_ci	.digest		=	chksum_digest,
1068c2ecf20Sopenharmony_ci	.descsize		=	sizeof(struct chksum_desc_ctx),
1078c2ecf20Sopenharmony_ci	.base			=	{
1088c2ecf20Sopenharmony_ci		.cra_name		=	"crct10dif",
1098c2ecf20Sopenharmony_ci		.cra_driver_name	=	"crct10dif-pclmul",
1108c2ecf20Sopenharmony_ci		.cra_priority		=	200,
1118c2ecf20Sopenharmony_ci		.cra_blocksize		=	CRC_T10DIF_BLOCK_SIZE,
1128c2ecf20Sopenharmony_ci		.cra_module		=	THIS_MODULE,
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic const struct x86_cpu_id crct10dif_cpu_id[] = {
1178c2ecf20Sopenharmony_ci	X86_MATCH_FEATURE(X86_FEATURE_PCLMULQDQ, NULL),
1188c2ecf20Sopenharmony_ci	{}
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(x86cpu, crct10dif_cpu_id);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic int __init crct10dif_intel_mod_init(void)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	if (!x86_match_cpu(crct10dif_cpu_id))
1258c2ecf20Sopenharmony_ci		return -ENODEV;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	return crypto_register_shash(&alg);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic void __exit crct10dif_intel_mod_fini(void)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	crypto_unregister_shash(&alg);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cimodule_init(crct10dif_intel_mod_init);
1368c2ecf20Sopenharmony_cimodule_exit(crct10dif_intel_mod_fini);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
1398c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("T10 DIF CRC calculation accelerated with PCLMULQDQ.");
1408c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crct10dif");
1438c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crct10dif-pclmul");
144