linux/arch/x86/crypto/fpu.c
<<
>>
Prefs
   1/*
   2 * FPU: Wrapper for blkcipher touching fpu
   3 *
   4 * Copyright (c) Intel Corp.
   5 *   Author: Huang Ying <ying.huang@intel.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms of the GNU General Public License as published by the Free
   9 * Software Foundation; either version 2 of the License, or (at your option)
  10 * any later version.
  11 *
  12 */
  13
  14#include <crypto/algapi.h>
  15#include <linux/err.h>
  16#include <linux/init.h>
  17#include <linux/kernel.h>
  18#include <linux/module.h>
  19#include <asm/i387.h>
  20
  21struct crypto_fpu_ctx {
  22        struct crypto_blkcipher *child;
  23};
  24
  25static int crypto_fpu_setkey(struct crypto_tfm *parent, const u8 *key,
  26                             unsigned int keylen)
  27{
  28        struct crypto_fpu_ctx *ctx = crypto_tfm_ctx(parent);
  29        struct crypto_blkcipher *child = ctx->child;
  30        int err;
  31
  32        crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  33        crypto_blkcipher_set_flags(child, crypto_tfm_get_flags(parent) &
  34                                   CRYPTO_TFM_REQ_MASK);
  35        err = crypto_blkcipher_setkey(child, key, keylen);
  36        crypto_tfm_set_flags(parent, crypto_blkcipher_get_flags(child) &
  37                                     CRYPTO_TFM_RES_MASK);
  38        return err;
  39}
  40
  41static int crypto_fpu_encrypt(struct blkcipher_desc *desc_in,
  42                              struct scatterlist *dst, struct scatterlist *src,
  43                              unsigned int nbytes)
  44{
  45        int err;
  46        struct crypto_fpu_ctx *ctx = crypto_blkcipher_ctx(desc_in->tfm);
  47        struct crypto_blkcipher *child = ctx->child;
  48        struct blkcipher_desc desc = {
  49                .tfm = child,
  50                .info = desc_in->info,
  51                .flags = desc_in->flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  52        };
  53
  54        kernel_fpu_begin();
  55        err = crypto_blkcipher_crt(desc.tfm)->encrypt(&desc, dst, src, nbytes);
  56        kernel_fpu_end();
  57        return err;
  58}
  59
  60static int crypto_fpu_decrypt(struct blkcipher_desc *desc_in,
  61                              struct scatterlist *dst, struct scatterlist *src,
  62                              unsigned int nbytes)
  63{
  64        int err;
  65        struct crypto_fpu_ctx *ctx = crypto_blkcipher_ctx(desc_in->tfm);
  66        struct crypto_blkcipher *child = ctx->child;
  67        struct blkcipher_desc desc = {
  68                .tfm = child,
  69                .info = desc_in->info,
  70                .flags = desc_in->flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  71        };
  72
  73        kernel_fpu_begin();
  74        err = crypto_blkcipher_crt(desc.tfm)->decrypt(&desc, dst, src, nbytes);
  75        kernel_fpu_end();
  76        return err;
  77}
  78
  79static int crypto_fpu_init_tfm(struct crypto_tfm *tfm)
  80{
  81        struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  82        struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  83        struct crypto_fpu_ctx *ctx = crypto_tfm_ctx(tfm);
  84        struct crypto_blkcipher *cipher;
  85
  86        cipher = crypto_spawn_blkcipher(spawn);
  87        if (IS_ERR(cipher))
  88                return PTR_ERR(cipher);
  89
  90        ctx->child = cipher;
  91        return 0;
  92}
  93
  94static void crypto_fpu_exit_tfm(struct crypto_tfm *tfm)
  95{
  96        struct crypto_fpu_ctx *ctx = crypto_tfm_ctx(tfm);
  97        crypto_free_blkcipher(ctx->child);
  98}
  99
 100static struct crypto_instance *crypto_fpu_alloc(struct rtattr **tb)
 101{
 102        struct crypto_instance *inst;
 103        struct crypto_alg *alg;
 104        int err;
 105
 106        err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
 107        if (err)
 108                return ERR_PTR(err);
 109
 110        alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
 111                                  CRYPTO_ALG_TYPE_MASK);
 112        if (IS_ERR(alg))
 113                return ERR_CAST(alg);
 114
 115        inst = crypto_alloc_instance("fpu", alg);
 116        if (IS_ERR(inst))
 117                goto out_put_alg;
 118
 119        inst->alg.cra_flags = alg->cra_flags;
 120        inst->alg.cra_priority = alg->cra_priority;
 121        inst->alg.cra_blocksize = alg->cra_blocksize;
 122        inst->alg.cra_alignmask = alg->cra_alignmask;
 123        inst->alg.cra_type = alg->cra_type;
 124        inst->alg.cra_blkcipher.ivsize = alg->cra_blkcipher.ivsize;
 125        inst->alg.cra_blkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
 126        inst->alg.cra_blkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
 127        inst->alg.cra_ctxsize = sizeof(struct crypto_fpu_ctx);
 128        inst->alg.cra_init = crypto_fpu_init_tfm;
 129        inst->alg.cra_exit = crypto_fpu_exit_tfm;
 130        inst->alg.cra_blkcipher.setkey = crypto_fpu_setkey;
 131        inst->alg.cra_blkcipher.encrypt = crypto_fpu_encrypt;
 132        inst->alg.cra_blkcipher.decrypt = crypto_fpu_decrypt;
 133
 134out_put_alg:
 135        crypto_mod_put(alg);
 136        return inst;
 137}
 138
 139static void crypto_fpu_free(struct crypto_instance *inst)
 140{
 141        crypto_drop_spawn(crypto_instance_ctx(inst));
 142        kfree(inst);
 143}
 144
 145static struct crypto_template crypto_fpu_tmpl = {
 146        .name = "fpu",
 147        .alloc = crypto_fpu_alloc,
 148        .free = crypto_fpu_free,
 149        .module = THIS_MODULE,
 150};
 151
 152static int __init crypto_fpu_module_init(void)
 153{
 154        return crypto_register_template(&crypto_fpu_tmpl);
 155}
 156
 157static void __exit crypto_fpu_module_exit(void)
 158{
 159        crypto_unregister_template(&crypto_fpu_tmpl);
 160}
 161
 162module_init(crypto_fpu_module_init);
 163module_exit(crypto_fpu_module_exit);
 164
 165MODULE_LICENSE("GPL");
 166MODULE_DESCRIPTION("FPU block cipher wrapper");
 167