18c2ecf20Sopenharmony_ci/* GPL HEADER START
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
68c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License version 2 only,
78c2ecf20Sopenharmony_ci * as published by the Free Software Foundation.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
108c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
118c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
128c2ecf20Sopenharmony_ci * General Public License version 2 for more details (a copy is included
138c2ecf20Sopenharmony_ci * in the LICENSE file that accompanied this code).
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License
168c2ecf20Sopenharmony_ci * version 2 along with this program; If not, see http://www.gnu.org/licenses
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Please  visit http://www.xyratex.com/contact if you need additional
198c2ecf20Sopenharmony_ci * information or have any questions.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * GPL HEADER END
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * Copyright 2012 Xyratex Technology Limited
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * This is crypto api shash wrappers to crc32_le.
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
338c2ecf20Sopenharmony_ci#include <linux/crc32.h>
348c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
358c2ecf20Sopenharmony_ci#include <linux/init.h>
368c2ecf20Sopenharmony_ci#include <linux/module.h>
378c2ecf20Sopenharmony_ci#include <linux/string.h>
388c2ecf20Sopenharmony_ci#include <linux/kernel.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define CHKSUM_BLOCK_SIZE	1
418c2ecf20Sopenharmony_ci#define CHKSUM_DIGEST_SIZE	4
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/** No default init with ~0 */
448c2ecf20Sopenharmony_cistatic int crc32_cra_init(struct crypto_tfm *tfm)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	u32 *key = crypto_tfm_ctx(tfm);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	*key = 0;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	return 0;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/*
548c2ecf20Sopenharmony_ci * Setting the seed allows arbitrary accumulators and flexible XOR policy
558c2ecf20Sopenharmony_ci * If your algorithm starts with ~0, then XOR with ~0 before you set
568c2ecf20Sopenharmony_ci * the seed.
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_cistatic int crc32_setkey(struct crypto_shash *hash, const u8 *key,
598c2ecf20Sopenharmony_ci			unsigned int keylen)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	u32 *mctx = crypto_shash_ctx(hash);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	if (keylen != sizeof(u32))
648c2ecf20Sopenharmony_ci		return -EINVAL;
658c2ecf20Sopenharmony_ci	*mctx = get_unaligned_le32(key);
668c2ecf20Sopenharmony_ci	return 0;
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic int crc32_init(struct shash_desc *desc)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	u32 *mctx = crypto_shash_ctx(desc->tfm);
728c2ecf20Sopenharmony_ci	u32 *crcp = shash_desc_ctx(desc);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	*crcp = *mctx;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic int crc32_update(struct shash_desc *desc, const u8 *data,
808c2ecf20Sopenharmony_ci			unsigned int len)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	u32 *crcp = shash_desc_ctx(desc);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	*crcp = crc32_le(*crcp, data, len);
858c2ecf20Sopenharmony_ci	return 0;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* No final XOR 0xFFFFFFFF, like crc32_le */
898c2ecf20Sopenharmony_cistatic int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
908c2ecf20Sopenharmony_ci			 u8 *out)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	put_unaligned_le32(crc32_le(*crcp, data, len), out);
938c2ecf20Sopenharmony_ci	return 0;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic int crc32_finup(struct shash_desc *desc, const u8 *data,
978c2ecf20Sopenharmony_ci		       unsigned int len, u8 *out)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	return __crc32_finup(shash_desc_ctx(desc), data, len, out);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int crc32_final(struct shash_desc *desc, u8 *out)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	u32 *crcp = shash_desc_ctx(desc);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	put_unaligned_le32(*crcp, out);
1078c2ecf20Sopenharmony_ci	return 0;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic int crc32_digest(struct shash_desc *desc, const u8 *data,
1118c2ecf20Sopenharmony_ci			unsigned int len, u8 *out)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
1148c2ecf20Sopenharmony_ci			     out);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_cistatic struct shash_alg alg = {
1178c2ecf20Sopenharmony_ci	.setkey		= crc32_setkey,
1188c2ecf20Sopenharmony_ci	.init		= crc32_init,
1198c2ecf20Sopenharmony_ci	.update		= crc32_update,
1208c2ecf20Sopenharmony_ci	.final		= crc32_final,
1218c2ecf20Sopenharmony_ci	.finup		= crc32_finup,
1228c2ecf20Sopenharmony_ci	.digest		= crc32_digest,
1238c2ecf20Sopenharmony_ci	.descsize	= sizeof(u32),
1248c2ecf20Sopenharmony_ci	.digestsize	= CHKSUM_DIGEST_SIZE,
1258c2ecf20Sopenharmony_ci	.base		= {
1268c2ecf20Sopenharmony_ci		.cra_name		= "crc32",
1278c2ecf20Sopenharmony_ci		.cra_driver_name	= "crc32-generic",
1288c2ecf20Sopenharmony_ci		.cra_priority		= 100,
1298c2ecf20Sopenharmony_ci		.cra_flags		= CRYPTO_ALG_OPTIONAL_KEY,
1308c2ecf20Sopenharmony_ci		.cra_blocksize		= CHKSUM_BLOCK_SIZE,
1318c2ecf20Sopenharmony_ci		.cra_ctxsize		= sizeof(u32),
1328c2ecf20Sopenharmony_ci		.cra_module		= THIS_MODULE,
1338c2ecf20Sopenharmony_ci		.cra_init		= crc32_cra_init,
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic int __init crc32_mod_init(void)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	return crypto_register_shash(&alg);
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic void __exit crc32_mod_fini(void)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	crypto_unregister_shash(&alg);
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cisubsys_initcall(crc32_mod_init);
1488c2ecf20Sopenharmony_cimodule_exit(crc32_mod_fini);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
1518c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
1528c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1538c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crc32");
1548c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crc32-generic");
155