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> 8 9 #define CHKSUM_BLOCK_SIZE 1 10 #define CHKSUM_DIGEST_SIZE 4 11 12 #define VMX_ALIGN 16 13 #define VMX_ALIGN_MASK (VMX_ALIGN-1) 14 15 #define VECTOR_BREAKPOINT 512 16 17 u32 __crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len); 18 19 static u32 crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len) 20 { 21 unsigned int prealign; 22 unsigned int tail; 23 24 if (len < (VECTOR_BREAKPOINT + VMX_ALIGN) || in_interrupt()) 25 return __crc32c_le(crc, p, len); 26 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); 30 len -= prealign; 31 p += prealign; 32 } 33 34 if (len & ~VMX_ALIGN_MASK) { 35 pagefault_disable(); 36 enable_kernel_altivec(); 37 crc = __crc32c_vpmsum(crc, p, len & ~VMX_ALIGN_MASK); 38 pagefault_enable(); 39 } 40 41 tail = len & VMX_ALIGN_MASK; 42 if (tail) { 43 p += len & ~VMX_ALIGN_MASK; 44 crc = __crc32c_le(crc, p, tail); 45 } 46 47 return crc; 48 } 49 50 static int crc32c_vpmsum_cra_init(struct crypto_tfm *tfm) 51 { 52 u32 *key = crypto_tfm_ctx(tfm); 53 54 *key = 0; 55 56 return 0; 57 } 58 59 /* 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 62 * the seed. 63 */ 64 static int crc32c_vpmsum_setkey(struct crypto_shash *hash, const u8 *key, 65 unsigned int keylen) 66 { 67 u32 *mctx = crypto_shash_ctx(hash); 68 69 if (keylen != sizeof(u32)) { 70 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN); 71 return -EINVAL; 72 } 73 *mctx = le32_to_cpup((__le32 *)key); 74 return 0; 75 } 76 77 static int crc32c_vpmsum_init(struct shash_desc *desc) 78 { 79 u32 *mctx = crypto_shash_ctx(desc->tfm); 80 u32 *crcp = shash_desc_ctx(desc); 81 82 *crcp = *mctx; 83 84 return 0; 85 } 86 87 static int crc32c_vpmsum_update(struct shash_desc *desc, const u8 *data, 88 unsigned int len) 89 { 90 u32 *crcp = shash_desc_ctx(desc); 91 92 *crcp = crc32c_vpmsum(*crcp, data, len); 93 94 return 0; 95 } 96 97 static int __crc32c_vpmsum_finup(u32 *crcp, const u8 *data, unsigned int len, 98 u8 *out) 99 { 100 *(__le32 *)out = ~cpu_to_le32(crc32c_vpmsum(*crcp, data, len)); 101 102 return 0; 103 } 104 105 static int crc32c_vpmsum_finup(struct shash_desc *desc, const u8 *data, 106 unsigned int len, u8 *out) 107 { 108 return __crc32c_vpmsum_finup(shash_desc_ctx(desc), data, len, out); 109 } 110 111 static int crc32c_vpmsum_final(struct shash_desc *desc, u8 *out) 112 { 113 u32 *crcp = shash_desc_ctx(desc); 114 115 *(__le32 *)out = ~cpu_to_le32p(crcp); 116 117 return 0; 118 } 119 120 static int crc32c_vpmsum_digest(struct shash_desc *desc, const u8 *data, 121 unsigned int len, u8 *out) 122 { 123 return __crc32c_vpmsum_finup(crypto_shash_ctx(desc->tfm), data, len, 124 out); 125 } 126 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, 136 .base = { 137 .cra_name = "crc32c", 138 .cra_driver_name = "crc32c-vpmsum", 139 .cra_priority = 200, 140 .cra_blocksize = CHKSUM_BLOCK_SIZE, 141 .cra_ctxsize = sizeof(u32), 142 .cra_module = THIS_MODULE, 143 .cra_init = crc32c_vpmsum_cra_init, 144 } 145 }; 146 147 static int __init crc32c_vpmsum_mod_init(void) 148 { 149 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) 150 return -ENODEV; 151 152 return crypto_register_shash(&alg); 153 } 154 155 static void __exit crc32c_vpmsum_mod_fini(void) 156 { 157 crypto_unregister_shash(&alg); 158 } 159 160 module_init(crc32c_vpmsum_mod_init); 161 module_exit(crc32c_vpmsum_mod_fini); 162 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"); 168