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