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 <asm/switch_to.h>
9 #define CHKSUM_BLOCK_SIZE 1
10 #define CHKSUM_DIGEST_SIZE 4
13 #define VMX_ALIGN_MASK (VMX_ALIGN-1)
15 #define VECTOR_BREAKPOINT 512
17 u32 __crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len);
19 static u32 crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len)
21 unsigned int prealign;
24 if (len < (VECTOR_BREAKPOINT + VMX_ALIGN) || in_interrupt())
25 return __crc32c_le(crc, p, len);
27 if ((unsigned long)p & VMX_ALIGN_MASK) {
28 prealign = VMX_ALIGN - ((unsigned long)p & VMX_ALIGN_MASK);
29 crc = __crc32c_le(crc, p, prealign);
34 if (len & ~VMX_ALIGN_MASK) {
36 enable_kernel_altivec();
37 crc = __crc32c_vpmsum(crc, p, len & ~VMX_ALIGN_MASK);
41 tail = len & VMX_ALIGN_MASK;
43 p += len & ~VMX_ALIGN_MASK;
44 crc = __crc32c_le(crc, p, tail);
50 static int crc32c_vpmsum_cra_init(struct crypto_tfm *tfm)
52 u32 *key = crypto_tfm_ctx(tfm);
60 * Setting the seed allows arbitrary accumulators and flexible XOR policy
61 * If your algorithm starts with ~0, then XOR with ~0 before you set
64 static int crc32c_vpmsum_setkey(struct crypto_shash *hash, const u8 *key,
67 u32 *mctx = crypto_shash_ctx(hash);
69 if (keylen != sizeof(u32)) {
70 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
73 *mctx = le32_to_cpup((__le32 *)key);
77 static int crc32c_vpmsum_init(struct shash_desc *desc)
79 u32 *mctx = crypto_shash_ctx(desc->tfm);
80 u32 *crcp = shash_desc_ctx(desc);
87 static int crc32c_vpmsum_update(struct shash_desc *desc, const u8 *data,
90 u32 *crcp = shash_desc_ctx(desc);
92 *crcp = crc32c_vpmsum(*crcp, data, len);
97 static int __crc32c_vpmsum_finup(u32 *crcp, const u8 *data, unsigned int len,
100 *(__le32 *)out = ~cpu_to_le32(crc32c_vpmsum(*crcp, data, len));
105 static int crc32c_vpmsum_finup(struct shash_desc *desc, const u8 *data,
106 unsigned int len, u8 *out)
108 return __crc32c_vpmsum_finup(shash_desc_ctx(desc), data, len, out);
111 static int crc32c_vpmsum_final(struct shash_desc *desc, u8 *out)
113 u32 *crcp = shash_desc_ctx(desc);
115 *(__le32 *)out = ~cpu_to_le32p(crcp);
120 static int crc32c_vpmsum_digest(struct shash_desc *desc, const u8 *data,
121 unsigned int len, u8 *out)
123 return __crc32c_vpmsum_finup(crypto_shash_ctx(desc->tfm), data, len,
127 static struct shash_alg alg = {
128 .setkey = crc32c_vpmsum_setkey,
129 .init = crc32c_vpmsum_init,
130 .update = crc32c_vpmsum_update,
131 .final = crc32c_vpmsum_final,
132 .finup = crc32c_vpmsum_finup,
133 .digest = crc32c_vpmsum_digest,
134 .descsize = sizeof(u32),
135 .digestsize = CHKSUM_DIGEST_SIZE,
137 .cra_name = "crc32c",
138 .cra_driver_name = "crc32c-vpmsum",
140 .cra_blocksize = CHKSUM_BLOCK_SIZE,
141 .cra_ctxsize = sizeof(u32),
142 .cra_module = THIS_MODULE,
143 .cra_init = crc32c_vpmsum_cra_init,
147 static int __init crc32c_vpmsum_mod_init(void)
149 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
152 return crypto_register_shash(&alg);
155 static void __exit crc32c_vpmsum_mod_fini(void)
157 crypto_unregister_shash(&alg);
160 module_init(crc32c_vpmsum_mod_init);
161 module_exit(crc32c_vpmsum_mod_fini);
163 MODULE_AUTHOR("Anton Blanchard <anton@samba.org>");
164 MODULE_DESCRIPTION("CRC32C using vector polynomial multiply-sum instructions");
165 MODULE_LICENSE("GPL");
166 MODULE_ALIAS_CRYPTO("crc32c");
167 MODULE_ALIAS_CRYPTO("crc32c-vpmsum");