^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Cryptographic API for the 842 software compression algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) IBM Corporation, 2011-2015
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Seth Jennings <sjenning@linux.vnet.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Rewrite: Dan Streetman <ddstreet@ieee.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This is the software implementation of compression and decompression using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * the 842 format. This uses the software 842 library at lib/842/ which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * only a reference implementation, and is very, very slow as compared to other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * software compressors. You probably do not want to use this software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * compression. If you have access to the PowerPC 842 compression hardware, you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * want to use the 842 hardware compression interface, which is at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * drivers/crypto/nx/nx-842-crypto.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/sw842.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <crypto/internal/scompress.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct crypto842_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) void *wmem; /* working memory for compress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void *crypto842_alloc_ctx(struct crypto_scomp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ctx = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int crypto842_init(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ctx->wmem = crypto842_alloc_ctx(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (IS_ERR(ctx->wmem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void crypto842_free_ctx(struct crypto_scomp *tfm, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) kfree(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static void crypto842_exit(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) crypto842_free_ctx(NULL, ctx->wmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int crypto842_compress(struct crypto_tfm *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) const u8 *src, unsigned int slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) u8 *dst, unsigned int *dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return sw842_compress(src, slen, dst, dlen, ctx->wmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static int crypto842_scompress(struct crypto_scomp *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) const u8 *src, unsigned int slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u8 *dst, unsigned int *dlen, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return sw842_compress(src, slen, dst, dlen, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int crypto842_decompress(struct crypto_tfm *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) const u8 *src, unsigned int slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) u8 *dst, unsigned int *dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return sw842_decompress(src, slen, dst, dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int crypto842_sdecompress(struct crypto_scomp *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) const u8 *src, unsigned int slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) u8 *dst, unsigned int *dlen, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return sw842_decompress(src, slen, dst, dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static struct crypto_alg alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .cra_name = "842",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .cra_driver_name = "842-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .cra_ctxsize = sizeof(struct crypto842_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .cra_init = crypto842_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .cra_exit = crypto842_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .cra_u = { .compress = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .coa_compress = crypto842_compress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .coa_decompress = crypto842_decompress } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static struct scomp_alg scomp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .alloc_ctx = crypto842_alloc_ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .free_ctx = crypto842_free_ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .compress = crypto842_scompress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .decompress = crypto842_sdecompress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .cra_name = "842",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .cra_driver_name = "842-scomp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int __init crypto842_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = crypto_register_alg(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ret = crypto_register_scomp(&scomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) crypto_unregister_alg(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) subsys_initcall(crypto842_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static void __exit crypto842_mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) crypto_unregister_alg(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) crypto_unregister_scomp(&scomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) module_exit(crypto842_mod_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) MODULE_DESCRIPTION("842 Software Compression Algorithm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) MODULE_ALIAS_CRYPTO("842");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) MODULE_ALIAS_CRYPTO("842-generic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");