Back to home page

LXR

 
 

    


0001 /*
0002  * Shared async block cipher helpers
0003  *
0004  * Copyright (c) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
0005  *
0006  * Based on aesni-intel_glue.c by:
0007  *  Copyright (C) 2008, Intel Corp.
0008  *    Author: Huang Ying <ying.huang@intel.com>
0009  *
0010  * This program is free software; you can redistribute it and/or modify
0011  * it under the terms of the GNU General Public License as published by
0012  * the Free Software Foundation; either version 2 of the License, or
0013  * (at your option) any later version.
0014  *
0015  * This program is distributed in the hope that it will be useful,
0016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  * GNU General Public License for more details.
0019  *
0020  * You should have received a copy of the GNU General Public License
0021  * along with this program; if not, write to the Free Software
0022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
0023  * USA
0024  *
0025  */
0026 
0027 #include <linux/kernel.h>
0028 #include <linux/crypto.h>
0029 #include <linux/init.h>
0030 #include <linux/module.h>
0031 #include <linux/hardirq.h>
0032 #include <crypto/algapi.h>
0033 #include <crypto/cryptd.h>
0034 #include <crypto/ablk_helper.h>
0035 #include <asm/simd.h>
0036 
0037 int ablk_set_key(struct crypto_ablkcipher *tfm, const u8 *key,
0038          unsigned int key_len)
0039 {
0040     struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
0041     struct crypto_ablkcipher *child = &ctx->cryptd_tfm->base;
0042     int err;
0043 
0044     crypto_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
0045     crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(tfm)
0046                     & CRYPTO_TFM_REQ_MASK);
0047     err = crypto_ablkcipher_setkey(child, key, key_len);
0048     crypto_ablkcipher_set_flags(tfm, crypto_ablkcipher_get_flags(child)
0049                     & CRYPTO_TFM_RES_MASK);
0050     return err;
0051 }
0052 EXPORT_SYMBOL_GPL(ablk_set_key);
0053 
0054 int __ablk_encrypt(struct ablkcipher_request *req)
0055 {
0056     struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
0057     struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
0058     struct blkcipher_desc desc;
0059 
0060     desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm);
0061     desc.info = req->info;
0062     desc.flags = 0;
0063 
0064     return crypto_blkcipher_crt(desc.tfm)->encrypt(
0065         &desc, req->dst, req->src, req->nbytes);
0066 }
0067 EXPORT_SYMBOL_GPL(__ablk_encrypt);
0068 
0069 int ablk_encrypt(struct ablkcipher_request *req)
0070 {
0071     struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
0072     struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
0073 
0074     if (!may_use_simd() ||
0075         (in_atomic() && cryptd_ablkcipher_queued(ctx->cryptd_tfm))) {
0076         struct ablkcipher_request *cryptd_req =
0077             ablkcipher_request_ctx(req);
0078 
0079         *cryptd_req = *req;
0080         ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
0081 
0082         return crypto_ablkcipher_encrypt(cryptd_req);
0083     } else {
0084         return __ablk_encrypt(req);
0085     }
0086 }
0087 EXPORT_SYMBOL_GPL(ablk_encrypt);
0088 
0089 int ablk_decrypt(struct ablkcipher_request *req)
0090 {
0091     struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
0092     struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
0093 
0094     if (!may_use_simd() ||
0095         (in_atomic() && cryptd_ablkcipher_queued(ctx->cryptd_tfm))) {
0096         struct ablkcipher_request *cryptd_req =
0097             ablkcipher_request_ctx(req);
0098 
0099         *cryptd_req = *req;
0100         ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
0101 
0102         return crypto_ablkcipher_decrypt(cryptd_req);
0103     } else {
0104         struct blkcipher_desc desc;
0105 
0106         desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm);
0107         desc.info = req->info;
0108         desc.flags = 0;
0109 
0110         return crypto_blkcipher_crt(desc.tfm)->decrypt(
0111             &desc, req->dst, req->src, req->nbytes);
0112     }
0113 }
0114 EXPORT_SYMBOL_GPL(ablk_decrypt);
0115 
0116 void ablk_exit(struct crypto_tfm *tfm)
0117 {
0118     struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm);
0119 
0120     cryptd_free_ablkcipher(ctx->cryptd_tfm);
0121 }
0122 EXPORT_SYMBOL_GPL(ablk_exit);
0123 
0124 int ablk_init_common(struct crypto_tfm *tfm, const char *drv_name)
0125 {
0126     struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm);
0127     struct cryptd_ablkcipher *cryptd_tfm;
0128 
0129     cryptd_tfm = cryptd_alloc_ablkcipher(drv_name, CRYPTO_ALG_INTERNAL,
0130                          CRYPTO_ALG_INTERNAL);
0131     if (IS_ERR(cryptd_tfm))
0132         return PTR_ERR(cryptd_tfm);
0133 
0134     ctx->cryptd_tfm = cryptd_tfm;
0135     tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request) +
0136         crypto_ablkcipher_reqsize(&cryptd_tfm->base);
0137 
0138     return 0;
0139 }
0140 EXPORT_SYMBOL_GPL(ablk_init_common);
0141 
0142 int ablk_init(struct crypto_tfm *tfm)
0143 {
0144     char drv_name[CRYPTO_MAX_ALG_NAME];
0145 
0146     snprintf(drv_name, sizeof(drv_name), "__driver-%s",
0147                     crypto_tfm_alg_driver_name(tfm));
0148 
0149     return ablk_init_common(tfm, drv_name);
0150 }
0151 EXPORT_SYMBOL_GPL(ablk_init);
0152 
0153 MODULE_LICENSE("GPL");