^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * crc32-mips.c - CRC32 and CRC32C using optional MIPSr6 instructions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Module based on arm64/crypto/crc32-arm.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2014 Linaro Ltd <yazen.ghannam@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2018 MIPS Tech, LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/unaligned/access_ok.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/mipsregs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) enum crc_op_size {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) b, h, w, d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) enum crc_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) crc32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) crc32c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #ifndef TOOLCHAIN_SUPPORTS_CRC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define _ASM_MACRO_CRC32(OP, SZ, TYPE) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) _ASM_MACRO_3R(OP, rt, rs, rt2, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ".ifnc \\rt, \\rt2\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ".error \"invalid operands \\\"" #OP " \\rt,\\rs,\\rt2\\\"\"\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ".endif\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) _ASM_INSN_IF_MIPS(0x7c00000f | (__rt << 16) | (__rs << 21) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ((SZ) << 6) | ((TYPE) << 8)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) _ASM_INSN32_IF_MM(0x00000030 | (__rs << 16) | (__rt << 21) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) ((SZ) << 14) | ((TYPE) << 3)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) _ASM_MACRO_CRC32(crc32b, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) _ASM_MACRO_CRC32(crc32h, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) _ASM_MACRO_CRC32(crc32w, 2, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) _ASM_MACRO_CRC32(crc32d, 3, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) _ASM_MACRO_CRC32(crc32cb, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) _ASM_MACRO_CRC32(crc32ch, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) _ASM_MACRO_CRC32(crc32cw, 2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) _ASM_MACRO_CRC32(crc32cd, 3, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define _ASM_SET_CRC ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #else /* !TOOLCHAIN_SUPPORTS_CRC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define _ASM_SET_CRC ".set\tcrc\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define _CRC32(crc, value, size, type) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) __asm__ __volatile__( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ".set push\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) _ASM_SET_CRC \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #type #size " %0, %1, %0\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ".set pop" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) : "+r" (crc) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) : "r" (value)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define CRC32(crc, value, size) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) _CRC32(crc, value, size, crc32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define CRC32C(crc, value, size) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) _CRC32(crc, value, size, crc32c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static u32 crc32_mips_le_hw(u32 crc_, const u8 *p, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u32 crc = crc_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) while (len >= sizeof(u64)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u64 value = get_unaligned_le64(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) CRC32(crc, value, d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) p += sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) len -= sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (len & sizeof(u32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #else /* !CONFIG_64BIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) while (len >= sizeof(u32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u32 value = get_unaligned_le32(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) CRC32(crc, value, w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) p += sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) len -= sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (len & sizeof(u16)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) u16 value = get_unaligned_le16(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) CRC32(crc, value, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) p += sizeof(u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (len & sizeof(u8)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) u8 value = *p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) CRC32(crc, value, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static u32 crc32c_mips_le_hw(u32 crc_, const u8 *p, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) u32 crc = crc_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) while (len >= sizeof(u64)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) u64 value = get_unaligned_le64(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) CRC32C(crc, value, d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) p += sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) len -= sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (len & sizeof(u32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #else /* !CONFIG_64BIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) while (len >= sizeof(u32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) u32 value = get_unaligned_le32(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) CRC32C(crc, value, w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) p += sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) len -= sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (len & sizeof(u16)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) u16 value = get_unaligned_le16(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) CRC32C(crc, value, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) p += sizeof(u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (len & sizeof(u8)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) u8 value = *p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) CRC32C(crc, value, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define CHKSUM_BLOCK_SIZE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #define CHKSUM_DIGEST_SIZE 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct chksum_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) u32 key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct chksum_desc_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) u32 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int chksum_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ctx->crc = mctx->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * Setting the seed allows arbitrary accumulators and flexible XOR policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * If your algorithm starts with ~0, then XOR with ~0 before you set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * the seed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (keylen != sizeof(mctx->key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) mctx->key = get_unaligned_le32(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int chksum_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ctx->crc = crc32_mips_le_hw(ctx->crc, data, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static int chksumc_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ctx->crc = crc32c_mips_le_hw(ctx->crc, data, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int chksum_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) put_unaligned_le32(ctx->crc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static int chksumc_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) put_unaligned_le32(~ctx->crc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static int __chksum_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) put_unaligned_le32(crc32_mips_le_hw(crc, data, len), out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static int __chksumc_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) put_unaligned_le32(~crc32c_mips_le_hw(crc, data, len), out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int chksum_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return __chksum_finup(ctx->crc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static int chksumc_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return __chksumc_finup(ctx->crc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static int chksum_digest(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) unsigned int length, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return __chksum_finup(mctx->key, data, length, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static int chksumc_digest(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) unsigned int length, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return __chksumc_finup(mctx->key, data, length, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int chksum_cra_init(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) mctx->key = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static struct shash_alg crc32_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) .digestsize = CHKSUM_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) .setkey = chksum_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) .init = chksum_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) .update = chksum_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) .final = chksum_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) .finup = chksum_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) .digest = chksum_digest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) .descsize = sizeof(struct chksum_desc_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) .cra_name = "crc32",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) .cra_driver_name = "crc32-mips-hw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .cra_priority = 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .cra_blocksize = CHKSUM_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .cra_alignmask = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) .cra_ctxsize = sizeof(struct chksum_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) .cra_init = chksum_cra_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static struct shash_alg crc32c_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) .digestsize = CHKSUM_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) .setkey = chksum_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) .init = chksum_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) .update = chksumc_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) .final = chksumc_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) .finup = chksumc_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) .digest = chksumc_digest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) .descsize = sizeof(struct chksum_desc_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) .cra_name = "crc32c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) .cra_driver_name = "crc32c-mips-hw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) .cra_priority = 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .cra_blocksize = CHKSUM_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) .cra_alignmask = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) .cra_ctxsize = sizeof(struct chksum_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) .cra_init = chksum_cra_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static int __init crc32_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) err = crypto_register_shash(&crc32_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) err = crypto_register_shash(&crc32c_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) crypto_unregister_shash(&crc32_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static void __exit crc32_mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) crypto_unregister_shash(&crc32_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) crypto_unregister_shash(&crc32c_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) MODULE_AUTHOR("Marcin Nowakowski <marcin.nowakowski@mips.com");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) MODULE_DESCRIPTION("CRC32 and CRC32C using optional MIPS instructions");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) module_cpu_feature_match(MIPS_CRC32, crc32_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) module_exit(crc32_mod_exit);