Back to home page

LXR

 
 

    


0001 /*
0002  * Cryptographic API for the 842 software compression algorithm.
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * Copyright (C) IBM Corporation, 2011-2015
0015  *
0016  * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
0017  *                   Seth Jennings <sjenning@linux.vnet.ibm.com>
0018  *
0019  * Rewrite: Dan Streetman <ddstreet@ieee.org>
0020  *
0021  * This is the software implementation of compression and decompression using
0022  * the 842 format.  This uses the software 842 library at lib/842/ which is
0023  * only a reference implementation, and is very, very slow as compared to other
0024  * software compressors.  You probably do not want to use this software
0025  * compression.  If you have access to the PowerPC 842 compression hardware, you
0026  * want to use the 842 hardware compression interface, which is at:
0027  * drivers/crypto/nx/nx-842-crypto.c
0028  */
0029 
0030 #include <linux/init.h>
0031 #include <linux/module.h>
0032 #include <linux/crypto.h>
0033 #include <linux/sw842.h>
0034 #include <crypto/internal/scompress.h>
0035 
0036 struct crypto842_ctx {
0037     void *wmem; /* working memory for compress */
0038 };
0039 
0040 static void *crypto842_alloc_ctx(struct crypto_scomp *tfm)
0041 {
0042     void *ctx;
0043 
0044     ctx = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL);
0045     if (!ctx)
0046         return ERR_PTR(-ENOMEM);
0047 
0048     return ctx;
0049 }
0050 
0051 static int crypto842_init(struct crypto_tfm *tfm)
0052 {
0053     struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
0054 
0055     ctx->wmem = crypto842_alloc_ctx(NULL);
0056     if (IS_ERR(ctx->wmem))
0057         return -ENOMEM;
0058 
0059     return 0;
0060 }
0061 
0062 static void crypto842_free_ctx(struct crypto_scomp *tfm, void *ctx)
0063 {
0064     kfree(ctx);
0065 }
0066 
0067 static void crypto842_exit(struct crypto_tfm *tfm)
0068 {
0069     struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
0070 
0071     crypto842_free_ctx(NULL, ctx->wmem);
0072 }
0073 
0074 static int crypto842_compress(struct crypto_tfm *tfm,
0075                   const u8 *src, unsigned int slen,
0076                   u8 *dst, unsigned int *dlen)
0077 {
0078     struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
0079 
0080     return sw842_compress(src, slen, dst, dlen, ctx->wmem);
0081 }
0082 
0083 static int crypto842_scompress(struct crypto_scomp *tfm,
0084                    const u8 *src, unsigned int slen,
0085                    u8 *dst, unsigned int *dlen, void *ctx)
0086 {
0087     return sw842_compress(src, slen, dst, dlen, ctx);
0088 }
0089 
0090 static int crypto842_decompress(struct crypto_tfm *tfm,
0091                 const u8 *src, unsigned int slen,
0092                 u8 *dst, unsigned int *dlen)
0093 {
0094     return sw842_decompress(src, slen, dst, dlen);
0095 }
0096 
0097 static int crypto842_sdecompress(struct crypto_scomp *tfm,
0098                  const u8 *src, unsigned int slen,
0099                  u8 *dst, unsigned int *dlen, void *ctx)
0100 {
0101     return sw842_decompress(src, slen, dst, dlen);
0102 }
0103 
0104 static struct crypto_alg alg = {
0105     .cra_name       = "842",
0106     .cra_driver_name    = "842-generic",
0107     .cra_priority       = 100,
0108     .cra_flags      = CRYPTO_ALG_TYPE_COMPRESS,
0109     .cra_ctxsize        = sizeof(struct crypto842_ctx),
0110     .cra_module     = THIS_MODULE,
0111     .cra_init       = crypto842_init,
0112     .cra_exit       = crypto842_exit,
0113     .cra_u          = { .compress = {
0114     .coa_compress       = crypto842_compress,
0115     .coa_decompress     = crypto842_decompress } }
0116 };
0117 
0118 static struct scomp_alg scomp = {
0119     .alloc_ctx      = crypto842_alloc_ctx,
0120     .free_ctx       = crypto842_free_ctx,
0121     .compress       = crypto842_scompress,
0122     .decompress     = crypto842_sdecompress,
0123     .base           = {
0124         .cra_name   = "842",
0125         .cra_driver_name = "842-scomp",
0126         .cra_priority    = 100,
0127         .cra_module  = THIS_MODULE,
0128     }
0129 };
0130 
0131 static int __init crypto842_mod_init(void)
0132 {
0133     int ret;
0134 
0135     ret = crypto_register_alg(&alg);
0136     if (ret)
0137         return ret;
0138 
0139     ret = crypto_register_scomp(&scomp);
0140     if (ret) {
0141         crypto_unregister_alg(&alg);
0142         return ret;
0143     }
0144 
0145     return ret;
0146 }
0147 module_init(crypto842_mod_init);
0148 
0149 static void __exit crypto842_mod_exit(void)
0150 {
0151     crypto_unregister_alg(&alg);
0152     crypto_unregister_scomp(&scomp);
0153 }
0154 module_exit(crypto842_mod_exit);
0155 
0156 MODULE_LICENSE("GPL");
0157 MODULE_DESCRIPTION("842 Software Compression Algorithm");
0158 MODULE_ALIAS_CRYPTO("842");
0159 MODULE_ALIAS_CRYPTO("842-generic");
0160 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");