Linux Audio

Check our new training course

Loading...
v5.9
  1/* GPL HEADER START
  2 *
  3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4 *
  5 * This program is free software; you can redistribute it and/or modify
  6 * it under the terms of the GNU General Public License version 2 only,
  7 * as published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope that it will be useful, but
 10 * WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12 * General Public License version 2 for more details (a copy is included
 13 * in the LICENSE file that accompanied this code).
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * version 2 along with this program; If not, see http://www.gnu.org/licenses
 17 *
 18 * Please  visit http://www.xyratex.com/contact if you need additional
 19 * information or have any questions.
 20 *
 21 * GPL HEADER END
 22 */
 23
 24/*
 25 * Copyright 2012 Xyratex Technology Limited
 26 */
 27
 28/*
 29 * This is crypto api shash wrappers to crc32_le.
 30 */
 31
 32#include <asm/unaligned.h>
 33#include <linux/crc32.h>
 34#include <crypto/internal/hash.h>
 35#include <linux/init.h>
 36#include <linux/module.h>
 37#include <linux/string.h>
 38#include <linux/kernel.h>
 39
 40#define CHKSUM_BLOCK_SIZE	1
 41#define CHKSUM_DIGEST_SIZE	4
 42
 43/** No default init with ~0 */
 44static int crc32_cra_init(struct crypto_tfm *tfm)
 45{
 46	u32 *key = crypto_tfm_ctx(tfm);
 47
 48	*key = 0;
 49
 50	return 0;
 51}
 52
 53/*
 54 * Setting the seed allows arbitrary accumulators and flexible XOR policy
 55 * If your algorithm starts with ~0, then XOR with ~0 before you set
 56 * the seed.
 57 */
 58static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
 59			unsigned int keylen)
 60{
 61	u32 *mctx = crypto_shash_ctx(hash);
 62
 63	if (keylen != sizeof(u32))
 64		return -EINVAL;
 65	*mctx = get_unaligned_le32(key);
 66	return 0;
 67}
 68
 69static int crc32_init(struct shash_desc *desc)
 70{
 71	u32 *mctx = crypto_shash_ctx(desc->tfm);
 72	u32 *crcp = shash_desc_ctx(desc);
 73
 74	*crcp = *mctx;
 75
 76	return 0;
 77}
 78
 79static int crc32_update(struct shash_desc *desc, const u8 *data,
 80			unsigned int len)
 81{
 82	u32 *crcp = shash_desc_ctx(desc);
 83
 
 
 
 
 
 
 
 
 
 84	*crcp = crc32_le(*crcp, data, len);
 85	return 0;
 86}
 87
 88/* No final XOR 0xFFFFFFFF, like crc32_le */
 89static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
 90			 u8 *out)
 91{
 
 
 
 
 
 
 
 92	put_unaligned_le32(crc32_le(*crcp, data, len), out);
 93	return 0;
 94}
 95
 96static int crc32_finup(struct shash_desc *desc, const u8 *data,
 97		       unsigned int len, u8 *out)
 98{
 99	return __crc32_finup(shash_desc_ctx(desc), data, len, out);
100}
101
 
 
 
 
 
 
102static int crc32_final(struct shash_desc *desc, u8 *out)
103{
104	u32 *crcp = shash_desc_ctx(desc);
105
106	put_unaligned_le32(*crcp, out);
107	return 0;
108}
109
110static int crc32_digest(struct shash_desc *desc, const u8 *data,
111			unsigned int len, u8 *out)
112{
113	return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
114			     out);
115}
116static struct shash_alg alg = {
117	.setkey		= crc32_setkey,
118	.init		= crc32_init,
119	.update		= crc32_update,
120	.final		= crc32_final,
121	.finup		= crc32_finup,
122	.digest		= crc32_digest,
123	.descsize	= sizeof(u32),
124	.digestsize	= CHKSUM_DIGEST_SIZE,
125	.base		= {
126		.cra_name		= "crc32",
127		.cra_driver_name	= "crc32-generic",
128		.cra_priority		= 100,
129		.cra_flags		= CRYPTO_ALG_OPTIONAL_KEY,
130		.cra_blocksize		= CHKSUM_BLOCK_SIZE,
131		.cra_ctxsize		= sizeof(u32),
132		.cra_module		= THIS_MODULE,
133		.cra_init		= crc32_cra_init,
134	}
135};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
137static int __init crc32_mod_init(void)
138{
139	return crypto_register_shash(&alg);
 
140}
141
142static void __exit crc32_mod_fini(void)
143{
144	crypto_unregister_shash(&alg);
145}
146
147subsys_initcall(crc32_mod_init);
148module_exit(crc32_mod_fini);
149
150MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
151MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
152MODULE_LICENSE("GPL");
153MODULE_ALIAS_CRYPTO("crc32");
154MODULE_ALIAS_CRYPTO("crc32-generic");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  2/*
  3 * Copyright 2012 Xyratex Technology Limited
  4 */
  5
  6/*
  7 * This is crypto api shash wrappers to crc32_le.
  8 */
  9
 10#include <linux/unaligned.h>
 11#include <linux/crc32.h>
 12#include <crypto/internal/hash.h>
 13#include <linux/init.h>
 14#include <linux/module.h>
 15#include <linux/string.h>
 16#include <linux/kernel.h>
 17
 18#define CHKSUM_BLOCK_SIZE	1
 19#define CHKSUM_DIGEST_SIZE	4
 20
 21/** No default init with ~0 */
 22static int crc32_cra_init(struct crypto_tfm *tfm)
 23{
 24	u32 *key = crypto_tfm_ctx(tfm);
 25
 26	*key = 0;
 27
 28	return 0;
 29}
 30
 31/*
 32 * Setting the seed allows arbitrary accumulators and flexible XOR policy
 33 * If your algorithm starts with ~0, then XOR with ~0 before you set
 34 * the seed.
 35 */
 36static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
 37			unsigned int keylen)
 38{
 39	u32 *mctx = crypto_shash_ctx(hash);
 40
 41	if (keylen != sizeof(u32))
 42		return -EINVAL;
 43	*mctx = get_unaligned_le32(key);
 44	return 0;
 45}
 46
 47static int crc32_init(struct shash_desc *desc)
 48{
 49	u32 *mctx = crypto_shash_ctx(desc->tfm);
 50	u32 *crcp = shash_desc_ctx(desc);
 51
 52	*crcp = *mctx;
 53
 54	return 0;
 55}
 56
 57static int crc32_update(struct shash_desc *desc, const u8 *data,
 58			unsigned int len)
 59{
 60	u32 *crcp = shash_desc_ctx(desc);
 61
 62	*crcp = crc32_le_base(*crcp, data, len);
 63	return 0;
 64}
 65
 66static int crc32_update_arch(struct shash_desc *desc, const u8 *data,
 67			     unsigned int len)
 68{
 69	u32 *crcp = shash_desc_ctx(desc);
 70
 71	*crcp = crc32_le(*crcp, data, len);
 72	return 0;
 73}
 74
 75/* No final XOR 0xFFFFFFFF, like crc32_le */
 76static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
 77			 u8 *out)
 78{
 79	put_unaligned_le32(crc32_le_base(*crcp, data, len), out);
 80	return 0;
 81}
 82
 83static int __crc32_finup_arch(u32 *crcp, const u8 *data, unsigned int len,
 84			      u8 *out)
 85{
 86	put_unaligned_le32(crc32_le(*crcp, data, len), out);
 87	return 0;
 88}
 89
 90static int crc32_finup(struct shash_desc *desc, const u8 *data,
 91		       unsigned int len, u8 *out)
 92{
 93	return __crc32_finup(shash_desc_ctx(desc), data, len, out);
 94}
 95
 96static int crc32_finup_arch(struct shash_desc *desc, const u8 *data,
 97		       unsigned int len, u8 *out)
 98{
 99	return __crc32_finup_arch(shash_desc_ctx(desc), data, len, out);
100}
101
102static int crc32_final(struct shash_desc *desc, u8 *out)
103{
104	u32 *crcp = shash_desc_ctx(desc);
105
106	put_unaligned_le32(*crcp, out);
107	return 0;
108}
109
110static int crc32_digest(struct shash_desc *desc, const u8 *data,
111			unsigned int len, u8 *out)
112{
113	return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len, out);
 
114}
115
116static int crc32_digest_arch(struct shash_desc *desc, const u8 *data,
117			     unsigned int len, u8 *out)
118{
119	return __crc32_finup_arch(crypto_shash_ctx(desc->tfm), data, len, out);
120}
121
122static struct shash_alg algs[] = {{
123	.setkey			= crc32_setkey,
124	.init			= crc32_init,
125	.update			= crc32_update,
126	.final			= crc32_final,
127	.finup			= crc32_finup,
128	.digest			= crc32_digest,
129	.descsize		= sizeof(u32),
130	.digestsize		= CHKSUM_DIGEST_SIZE,
131
132	.base.cra_name		= "crc32",
133	.base.cra_driver_name	= "crc32-generic",
134	.base.cra_priority	= 100,
135	.base.cra_flags		= CRYPTO_ALG_OPTIONAL_KEY,
136	.base.cra_blocksize	= CHKSUM_BLOCK_SIZE,
137	.base.cra_ctxsize	= sizeof(u32),
138	.base.cra_module	= THIS_MODULE,
139	.base.cra_init		= crc32_cra_init,
140}, {
141	.setkey			= crc32_setkey,
142	.init			= crc32_init,
143	.update			= crc32_update_arch,
144	.final			= crc32_final,
145	.finup			= crc32_finup_arch,
146	.digest			= crc32_digest_arch,
147	.descsize		= sizeof(u32),
148	.digestsize		= CHKSUM_DIGEST_SIZE,
149
150	.base.cra_name		= "crc32",
151	.base.cra_driver_name	= "crc32-" __stringify(ARCH),
152	.base.cra_priority	= 150,
153	.base.cra_flags		= CRYPTO_ALG_OPTIONAL_KEY,
154	.base.cra_blocksize	= CHKSUM_BLOCK_SIZE,
155	.base.cra_ctxsize	= sizeof(u32),
156	.base.cra_module	= THIS_MODULE,
157	.base.cra_init		= crc32_cra_init,
158}};
159
160static int __init crc32_mod_init(void)
161{
162	/* register the arch flavor only if it differs from the generic one */
163	return crypto_register_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
164}
165
166static void __exit crc32_mod_fini(void)
167{
168	crypto_unregister_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
169}
170
171subsys_initcall(crc32_mod_init);
172module_exit(crc32_mod_fini);
173
174MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
175MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
176MODULE_LICENSE("GPL");
177MODULE_ALIAS_CRYPTO("crc32");
178MODULE_ALIAS_CRYPTO("crc32-generic");