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>
10 #define CHKSUM_BLOCK_SIZE 1
11 #define CHKSUM_DIGEST_SIZE 4
14 #define VMX_ALIGN_MASK (VMX_ALIGN-1)
16 #define VECTOR_BREAKPOINT 512
18 u32 __crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len);
20 static u32 crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len)
22 unsigned int prealign;
25 if (len < (VECTOR_BREAKPOINT + VMX_ALIGN) || in_interrupt())
26 return __crc32c_le(crc, p, len);
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);
35 if (len & ~VMX_ALIGN_MASK) {
37 enable_kernel_altivec();
38 crc = __crc32c_vpmsum(crc, p, len & ~VMX_ALIGN_MASK);
42 tail = len & VMX_ALIGN_MASK;
44 p += len & ~VMX_ALIGN_MASK;
45 crc = __crc32c_le(crc, p, tail);
51 static int crc32c_vpmsum_cra_init(struct crypto_tfm *tfm)
53 u32 *key = crypto_tfm_ctx(tfm);
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
65 static int crc32c_vpmsum_setkey(struct crypto_shash *hash, const u8 *key,
68 u32 *mctx = crypto_shash_ctx(hash);
70 if (keylen != sizeof(u32)) {
71 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
74 *mctx = le32_to_cpup((__le32 *)key);
78 static int crc32c_vpmsum_init(struct shash_desc *desc)
80 u32 *mctx = crypto_shash_ctx(desc->tfm);
81 u32 *crcp = shash_desc_ctx(desc);
88 static int crc32c_vpmsum_update(struct shash_desc *desc, const u8 *data,
91 u32 *crcp = shash_desc_ctx(desc);
93 *crcp = crc32c_vpmsum(*crcp, data, len);
98 static int __crc32c_vpmsum_finup(u32 *crcp, const u8 *data, unsigned int len,
101 *(__le32 *)out = ~cpu_to_le32(crc32c_vpmsum(*crcp, data, len));
106 static int crc32c_vpmsum_finup(struct shash_desc *desc, const u8 *data,
107 unsigned int len, u8 *out)
109 return __crc32c_vpmsum_finup(shash_desc_ctx(desc), data, len, out);
112 static int crc32c_vpmsum_final(struct shash_desc *desc, u8 *out)
114 u32 *crcp = shash_desc_ctx(desc);
116 *(__le32 *)out = ~cpu_to_le32p(crcp);
121 static int crc32c_vpmsum_digest(struct shash_desc *desc, const u8 *data,
122 unsigned int len, u8 *out)
124 return __crc32c_vpmsum_finup(crypto_shash_ctx(desc->tfm), data, len,
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,
138 .cra_name = "crc32c",
139 .cra_driver_name = "crc32c-vpmsum",
141 .cra_blocksize = CHKSUM_BLOCK_SIZE,
142 .cra_ctxsize = sizeof(u32),
143 .cra_module = THIS_MODULE,
144 .cra_init = crc32c_vpmsum_cra_init,
148 static int __init crc32c_vpmsum_mod_init(void)
150 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
153 return crypto_register_shash(&alg);
156 static void __exit crc32c_vpmsum_mod_fini(void)
158 crypto_unregister_shash(&alg);
161 module_cpu_feature_match(PPC_MODULE_FEATURE_VEC_CRYPTO, crc32c_vpmsum_mod_init);
162 module_exit(crc32c_vpmsum_mod_fini);
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");