Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 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 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 14 | #include <crypto/internal/skcipher.h> |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Ingo Molnar | df6b35f | 2015-04-24 02:46:00 +0200 | [diff] [blame] | 20 | #include <asm/fpu/api.h> |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 21 | |
| 22 | struct crypto_fpu_ctx { |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 23 | struct crypto_skcipher *child; |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 24 | }; |
| 25 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 26 | static int crypto_fpu_setkey(struct crypto_skcipher *parent, const u8 *key, |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 27 | unsigned int keylen) |
| 28 | { |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 29 | struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(parent); |
| 30 | struct crypto_skcipher *child = ctx->child; |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 31 | int err; |
| 32 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 33 | crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 34 | crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) & |
| 35 | CRYPTO_TFM_REQ_MASK); |
| 36 | err = crypto_skcipher_setkey(child, key, keylen); |
| 37 | crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) & |
| 38 | CRYPTO_TFM_RES_MASK); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 39 | return err; |
| 40 | } |
| 41 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 42 | static int crypto_fpu_encrypt(struct skcipher_request *req) |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 43 | { |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 44 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 45 | struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 46 | struct crypto_skcipher *child = ctx->child; |
| 47 | SKCIPHER_REQUEST_ON_STACK(subreq, child); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 48 | int err; |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 49 | |
| 50 | skcipher_request_set_tfm(subreq, child); |
| 51 | skcipher_request_set_callback(subreq, 0, NULL, NULL); |
| 52 | skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, |
| 53 | req->iv); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 54 | |
| 55 | kernel_fpu_begin(); |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 56 | err = crypto_skcipher_encrypt(subreq); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 57 | kernel_fpu_end(); |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 58 | |
| 59 | skcipher_request_zero(subreq); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 60 | return err; |
| 61 | } |
| 62 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 63 | static int crypto_fpu_decrypt(struct skcipher_request *req) |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 64 | { |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 65 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 66 | struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 67 | struct crypto_skcipher *child = ctx->child; |
| 68 | SKCIPHER_REQUEST_ON_STACK(subreq, child); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 69 | int err; |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 70 | |
| 71 | skcipher_request_set_tfm(subreq, child); |
| 72 | skcipher_request_set_callback(subreq, 0, NULL, NULL); |
| 73 | skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, |
| 74 | req->iv); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 75 | |
| 76 | kernel_fpu_begin(); |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 77 | err = crypto_skcipher_decrypt(subreq); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 78 | kernel_fpu_end(); |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 79 | |
| 80 | skcipher_request_zero(subreq); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 81 | return err; |
| 82 | } |
| 83 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 84 | static int crypto_fpu_init_tfm(struct crypto_skcipher *tfm) |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 85 | { |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 86 | struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
| 87 | struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 88 | struct crypto_skcipher_spawn *spawn; |
| 89 | struct crypto_skcipher *cipher; |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 90 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 91 | spawn = skcipher_instance_ctx(inst); |
| 92 | cipher = crypto_spawn_skcipher(spawn); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 93 | if (IS_ERR(cipher)) |
| 94 | return PTR_ERR(cipher); |
| 95 | |
| 96 | ctx->child = cipher; |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 97 | |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 101 | static void crypto_fpu_exit_tfm(struct crypto_skcipher *tfm) |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 102 | { |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 103 | struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 104 | |
| 105 | crypto_free_skcipher(ctx->child); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 106 | } |
| 107 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 108 | static void crypto_fpu_free(struct skcipher_instance *inst) |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 109 | { |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 110 | crypto_drop_skcipher(skcipher_instance_ctx(inst)); |
| 111 | kfree(inst); |
| 112 | } |
| 113 | |
| 114 | static int crypto_fpu_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 115 | { |
| 116 | struct crypto_skcipher_spawn *spawn; |
| 117 | struct skcipher_instance *inst; |
| 118 | struct crypto_attr_type *algt; |
| 119 | struct skcipher_alg *alg; |
| 120 | const char *cipher_name; |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 121 | int err; |
| 122 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 123 | algt = crypto_get_attr_type(tb); |
| 124 | if (IS_ERR(algt)) |
| 125 | return PTR_ERR(algt); |
| 126 | |
| 127 | if ((algt->type ^ (CRYPTO_ALG_INTERNAL | CRYPTO_ALG_TYPE_SKCIPHER)) & |
| 128 | algt->mask) |
| 129 | return -EINVAL; |
| 130 | |
| 131 | if (!(algt->mask & CRYPTO_ALG_INTERNAL)) |
| 132 | return -EINVAL; |
| 133 | |
| 134 | cipher_name = crypto_attr_alg_name(tb[1]); |
| 135 | if (IS_ERR(cipher_name)) |
| 136 | return PTR_ERR(cipher_name); |
| 137 | |
| 138 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 139 | if (!inst) |
| 140 | return -ENOMEM; |
| 141 | |
| 142 | spawn = skcipher_instance_ctx(inst); |
| 143 | |
| 144 | crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst)); |
| 145 | err = crypto_grab_skcipher(spawn, cipher_name, CRYPTO_ALG_INTERNAL, |
| 146 | CRYPTO_ALG_INTERNAL | CRYPTO_ALG_ASYNC); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 147 | if (err) |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 148 | goto out_free_inst; |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 149 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 150 | alg = crypto_skcipher_spawn_alg(spawn); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 151 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 152 | err = crypto_inst_setname(skcipher_crypto_instance(inst), "fpu", |
| 153 | &alg->base); |
| 154 | if (err) |
| 155 | goto out_drop_skcipher; |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 156 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 157 | inst->alg.base.cra_flags = CRYPTO_ALG_INTERNAL; |
| 158 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 159 | inst->alg.base.cra_blocksize = alg->base.cra_blocksize; |
| 160 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 161 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 162 | inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg); |
| 163 | inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg); |
| 164 | inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg); |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 165 | |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 166 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_fpu_ctx); |
| 167 | |
| 168 | inst->alg.init = crypto_fpu_init_tfm; |
| 169 | inst->alg.exit = crypto_fpu_exit_tfm; |
| 170 | |
| 171 | inst->alg.setkey = crypto_fpu_setkey; |
| 172 | inst->alg.encrypt = crypto_fpu_encrypt; |
| 173 | inst->alg.decrypt = crypto_fpu_decrypt; |
| 174 | |
| 175 | inst->free = crypto_fpu_free; |
| 176 | |
| 177 | err = skcipher_register_instance(tmpl, inst); |
| 178 | if (err) |
| 179 | goto out_drop_skcipher; |
| 180 | |
| 181 | out: |
| 182 | return err; |
| 183 | |
| 184 | out_drop_skcipher: |
| 185 | crypto_drop_skcipher(spawn); |
| 186 | out_free_inst: |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 187 | kfree(inst); |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 188 | goto out; |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static struct crypto_template crypto_fpu_tmpl = { |
| 192 | .name = "fpu", |
Herbert Xu | 8567186 | 2016-11-22 20:08:33 +0800 | [diff] [blame] | 193 | .create = crypto_fpu_create, |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 194 | .module = THIS_MODULE, |
| 195 | }; |
| 196 | |
Andy Lutomirski | b23b645 | 2011-05-16 15:12:47 +1000 | [diff] [blame] | 197 | int __init crypto_fpu_init(void) |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 198 | { |
| 199 | return crypto_register_template(&crypto_fpu_tmpl); |
| 200 | } |
| 201 | |
Jeremiah Mahler | de1e008 | 2015-06-12 21:56:40 -0700 | [diff] [blame] | 202 | void crypto_fpu_exit(void) |
Huang Ying | 150c7e8 | 2009-03-29 15:39:02 +0800 | [diff] [blame] | 203 | { |
| 204 | crypto_unregister_template(&crypto_fpu_tmpl); |
| 205 | } |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 206 | |
| 207 | MODULE_ALIAS_CRYPTO("fpu"); |