1 #include <linux/crc32.h>
2 #include <crypto/internal/hash.h>
3 #include <crypto/internal/simd.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/string.h>
7 #include <linux/kernel.h>
8 #include <linux/cpufeature.h>
9 #include <asm/simd.h>
10 #include <asm/switch_to.h>
11 
12 #define CHKSUM_BLOCK_SIZE	1
13 #define CHKSUM_DIGEST_SIZE	4
14 
15 #define VMX_ALIGN		16
16 #define VMX_ALIGN_MASK		(VMX_ALIGN-1)
17 
18 #define VECTOR_BREAKPOINT	512
19 
20 u32 __crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len);
21 
22 static u32 crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len)
23 {
24 	unsigned int prealign;
25 	unsigned int tail;
26 
27 	if (len < (VECTOR_BREAKPOINT + VMX_ALIGN) || !crypto_simd_usable())
28 		return __crc32c_le(crc, p, len);
29 
30 	if ((unsigned long)p & VMX_ALIGN_MASK) {
31 		prealign = VMX_ALIGN - ((unsigned long)p & VMX_ALIGN_MASK);
32 		crc = __crc32c_le(crc, p, prealign);
33 		len -= prealign;
34 		p += prealign;
35 	}
36 
37 	if (len & ~VMX_ALIGN_MASK) {
38 		preempt_disable();
39 		pagefault_disable();
40 		enable_kernel_altivec();
41 		crc = __crc32c_vpmsum(crc, p, len & ~VMX_ALIGN_MASK);
42 		disable_kernel_altivec();
43 		pagefault_enable();
44 		preempt_enable();
45 	}
46 
47 	tail = len & VMX_ALIGN_MASK;
48 	if (tail) {
49 		p += len & ~VMX_ALIGN_MASK;
50 		crc = __crc32c_le(crc, p, tail);
51 	}
52 
53 	return crc;
54 }
55 
56 static int crc32c_vpmsum_cra_init(struct crypto_tfm *tfm)
57 {
58 	u32 *key = crypto_tfm_ctx(tfm);
59 
60 	*key = ~0;
61 
62 	return 0;
63 }
64 
65 /*
66  * Setting the seed allows arbitrary accumulators and flexible XOR policy
67  * If your algorithm starts with ~0, then XOR with ~0 before you set
68  * the seed.
69  */
70 static int crc32c_vpmsum_setkey(struct crypto_shash *hash, const u8 *key,
71 			       unsigned int keylen)
72 {
73 	u32 *mctx = crypto_shash_ctx(hash);
74 
75 	if (keylen != sizeof(u32)) {
76 		crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
77 		return -EINVAL;
78 	}
79 	*mctx = le32_to_cpup((__le32 *)key);
80 	return 0;
81 }
82 
83 static int crc32c_vpmsum_init(struct shash_desc *desc)
84 {
85 	u32 *mctx = crypto_shash_ctx(desc->tfm);
86 	u32 *crcp = shash_desc_ctx(desc);
87 
88 	*crcp = *mctx;
89 
90 	return 0;
91 }
92 
93 static int crc32c_vpmsum_update(struct shash_desc *desc, const u8 *data,
94 			       unsigned int len)
95 {
96 	u32 *crcp = shash_desc_ctx(desc);
97 
98 	*crcp = crc32c_vpmsum(*crcp, data, len);
99 
100 	return 0;
101 }
102 
103 static int __crc32c_vpmsum_finup(u32 *crcp, const u8 *data, unsigned int len,
104 				u8 *out)
105 {
106 	*(__le32 *)out = ~cpu_to_le32(crc32c_vpmsum(*crcp, data, len));
107 
108 	return 0;
109 }
110 
111 static int crc32c_vpmsum_finup(struct shash_desc *desc, const u8 *data,
112 			      unsigned int len, u8 *out)
113 {
114 	return __crc32c_vpmsum_finup(shash_desc_ctx(desc), data, len, out);
115 }
116 
117 static int crc32c_vpmsum_final(struct shash_desc *desc, u8 *out)
118 {
119 	u32 *crcp = shash_desc_ctx(desc);
120 
121 	*(__le32 *)out = ~cpu_to_le32p(crcp);
122 
123 	return 0;
124 }
125 
126 static int crc32c_vpmsum_digest(struct shash_desc *desc, const u8 *data,
127 			       unsigned int len, u8 *out)
128 {
129 	return __crc32c_vpmsum_finup(crypto_shash_ctx(desc->tfm), data, len,
130 				     out);
131 }
132 
133 static struct shash_alg alg = {
134 	.setkey		= crc32c_vpmsum_setkey,
135 	.init		= crc32c_vpmsum_init,
136 	.update		= crc32c_vpmsum_update,
137 	.final		= crc32c_vpmsum_final,
138 	.finup		= crc32c_vpmsum_finup,
139 	.digest		= crc32c_vpmsum_digest,
140 	.descsize	= sizeof(u32),
141 	.digestsize	= CHKSUM_DIGEST_SIZE,
142 	.base		= {
143 		.cra_name		= "crc32c",
144 		.cra_driver_name	= "crc32c-vpmsum",
145 		.cra_priority		= 200,
146 		.cra_flags		= CRYPTO_ALG_OPTIONAL_KEY,
147 		.cra_blocksize		= CHKSUM_BLOCK_SIZE,
148 		.cra_ctxsize		= sizeof(u32),
149 		.cra_module		= THIS_MODULE,
150 		.cra_init		= crc32c_vpmsum_cra_init,
151 	}
152 };
153 
154 static int __init crc32c_vpmsum_mod_init(void)
155 {
156 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
157 		return -ENODEV;
158 
159 	return crypto_register_shash(&alg);
160 }
161 
162 static void __exit crc32c_vpmsum_mod_fini(void)
163 {
164 	crypto_unregister_shash(&alg);
165 }
166 
167 module_cpu_feature_match(PPC_MODULE_FEATURE_VEC_CRYPTO, crc32c_vpmsum_mod_init);
168 module_exit(crc32c_vpmsum_mod_fini);
169 
170 MODULE_AUTHOR("Anton Blanchard <anton@samba.org>");
171 MODULE_DESCRIPTION("CRC32C using vector polynomial multiply-sum instructions");
172 MODULE_LICENSE("GPL");
173 MODULE_ALIAS_CRYPTO("crc32c");
174 MODULE_ALIAS_CRYPTO("crc32c-vpmsum");
175