blob: 0e103fb5dd777c625d554114fe32a574674dae42 [file] [log] [blame]
Alexander Boyko78c37d12013-01-10 18:54:59 +04001/* 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
Eric Biggersfffe7d92018-05-19 22:07:37 -070032#include <asm/unaligned.h>
Alexander Boyko78c37d12013-01-10 18:54:59 +040033#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
Alexander Boyko78c37d12013-01-10 18:54:59 +040043/** 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
Alexander Boyko78c37d12013-01-10 18:54:59 +040053/*
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
Eric Biggers674f3682019-12-30 21:19:36 -060063 if (keylen != sizeof(u32))
Alexander Boyko78c37d12013-01-10 18:54:59 +040064 return -EINVAL;
Eric Biggersfffe7d92018-05-19 22:07:37 -070065 *mctx = get_unaligned_le32(key);
Alexander Boyko78c37d12013-01-10 18:54:59 +040066 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
Eric Biggers69435462018-05-19 22:07:39 -070084 *crcp = crc32_le(*crcp, data, len);
Alexander Boyko78c37d12013-01-10 18:54:59 +040085 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{
Eric Biggers69435462018-05-19 22:07:39 -070092 put_unaligned_le32(crc32_le(*crcp, data, len), out);
Alexander Boyko78c37d12013-01-10 18:54:59 +040093 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
Eric Biggersfffe7d92018-05-19 22:07:37 -0700106 put_unaligned_le32(*crcp, out);
Alexander Boyko78c37d12013-01-10 18:54:59 +0400107 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",
Herbert Xua7c58ac2016-01-29 18:20:17 +0800127 .cra_driver_name = "crc32-generic",
Alexander Boyko78c37d12013-01-10 18:54:59 +0400128 .cra_priority = 100,
Eric Biggersa208fa82018-01-03 11:16:26 -0800129 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
Alexander Boyko78c37d12013-01-10 18:54:59 +0400130 .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
Eric Biggersc4741b22019-04-11 21:57:42 -0700147subsys_initcall(crc32_mod_init);
Alexander Boyko78c37d12013-01-10 18:54:59 +0400148module_exit(crc32_mod_fini);
149
150MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
151MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
152MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -0800153MODULE_ALIAS_CRYPTO("crc32");
Herbert Xua7c58ac2016-01-29 18:20:17 +0800154MODULE_ALIAS_CRYPTO("crc32-generic");