Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 1 | /** |
| 2 | * AES ECB routines supporting the Power 7+ Nest Accelerators driver |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 International Business Machines Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 only. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | * |
| 19 | * Author: Kent Yoder <yoder1@us.ibm.com> |
| 20 | */ |
| 21 | |
| 22 | #include <crypto/aes.h> |
| 23 | #include <crypto/algapi.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/crypto.h> |
| 27 | #include <asm/vio.h> |
| 28 | |
| 29 | #include "nx_csbcpb.h" |
| 30 | #include "nx.h" |
| 31 | |
| 32 | |
| 33 | static int ecb_aes_nx_set_key(struct crypto_tfm *tfm, |
| 34 | const u8 *in_key, |
| 35 | unsigned int key_len) |
| 36 | { |
| 37 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm); |
| 38 | struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb; |
| 39 | |
| 40 | nx_ctx_init(nx_ctx, HCOP_FC_AES); |
| 41 | |
| 42 | switch (key_len) { |
| 43 | case AES_KEYSIZE_128: |
| 44 | NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128); |
| 45 | nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128]; |
| 46 | break; |
| 47 | case AES_KEYSIZE_192: |
| 48 | NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192); |
| 49 | nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192]; |
| 50 | break; |
| 51 | case AES_KEYSIZE_256: |
| 52 | NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256); |
| 53 | nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256]; |
| 54 | break; |
| 55 | default: |
| 56 | return -EINVAL; |
| 57 | } |
| 58 | |
| 59 | csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB; |
| 60 | memcpy(csbcpb->cpb.aes_ecb.key, in_key, key_len); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int ecb_aes_nx_crypt(struct blkcipher_desc *desc, |
| 66 | struct scatterlist *dst, |
| 67 | struct scatterlist *src, |
| 68 | unsigned int nbytes, |
| 69 | int enc) |
| 70 | { |
| 71 | struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm); |
| 72 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 73 | unsigned long irq_flags; |
Marcelo Cerri | ab74175 | 2013-08-29 11:36:32 -0300 | [diff] [blame] | 74 | unsigned int processed = 0, to_process; |
| 75 | u32 max_sg_len; |
Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 76 | int rc; |
| 77 | |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 78 | spin_lock_irqsave(&nx_ctx->lock, irq_flags); |
| 79 | |
Marcelo Cerri | ab74175 | 2013-08-29 11:36:32 -0300 | [diff] [blame] | 80 | max_sg_len = min_t(u32, nx_driver.of.max_sg_len/sizeof(struct nx_sg), |
| 81 | nx_ctx->ap->sglen); |
Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 82 | |
| 83 | if (enc) |
| 84 | NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT; |
| 85 | else |
| 86 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT; |
| 87 | |
Marcelo Cerri | ab74175 | 2013-08-29 11:36:32 -0300 | [diff] [blame] | 88 | do { |
| 89 | to_process = min_t(u64, nbytes - processed, |
| 90 | nx_ctx->ap->databytelen); |
| 91 | to_process = min_t(u64, to_process, |
| 92 | NX_PAGE_SIZE * (max_sg_len - 1)); |
| 93 | to_process = to_process & ~(AES_BLOCK_SIZE - 1); |
Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 94 | |
Marcelo Cerri | ab74175 | 2013-08-29 11:36:32 -0300 | [diff] [blame] | 95 | rc = nx_build_sg_lists(nx_ctx, desc, dst, src, to_process, |
| 96 | processed, NULL); |
| 97 | if (rc) |
| 98 | goto out; |
Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 99 | |
Marcelo Cerri | ab74175 | 2013-08-29 11:36:32 -0300 | [diff] [blame] | 100 | if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) { |
| 101 | rc = -EINVAL; |
| 102 | goto out; |
| 103 | } |
Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 104 | |
Marcelo Cerri | ab74175 | 2013-08-29 11:36:32 -0300 | [diff] [blame] | 105 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 106 | desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 107 | if (rc) |
| 108 | goto out; |
| 109 | |
| 110 | atomic_inc(&(nx_ctx->stats->aes_ops)); |
| 111 | atomic64_add(csbcpb->csb.processed_byte_count, |
| 112 | &(nx_ctx->stats->aes_bytes)); |
| 113 | |
| 114 | processed += to_process; |
| 115 | } while (processed < nbytes); |
| 116 | |
Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 117 | out: |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 118 | spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); |
Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 119 | return rc; |
| 120 | } |
| 121 | |
| 122 | static int ecb_aes_nx_encrypt(struct blkcipher_desc *desc, |
| 123 | struct scatterlist *dst, |
| 124 | struct scatterlist *src, |
| 125 | unsigned int nbytes) |
| 126 | { |
| 127 | return ecb_aes_nx_crypt(desc, dst, src, nbytes, 1); |
| 128 | } |
| 129 | |
| 130 | static int ecb_aes_nx_decrypt(struct blkcipher_desc *desc, |
| 131 | struct scatterlist *dst, |
| 132 | struct scatterlist *src, |
| 133 | unsigned int nbytes) |
| 134 | { |
| 135 | return ecb_aes_nx_crypt(desc, dst, src, nbytes, 0); |
| 136 | } |
| 137 | |
| 138 | struct crypto_alg nx_ecb_aes_alg = { |
| 139 | .cra_name = "ecb(aes)", |
| 140 | .cra_driver_name = "ecb-aes-nx", |
| 141 | .cra_priority = 300, |
| 142 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 143 | .cra_blocksize = AES_BLOCK_SIZE, |
Kent Yoder | 1ad936e | 2013-04-12 17:13:59 +0000 | [diff] [blame] | 144 | .cra_alignmask = 0xf, |
Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 145 | .cra_ctxsize = sizeof(struct nx_crypto_ctx), |
| 146 | .cra_type = &crypto_blkcipher_type, |
| 147 | .cra_module = THIS_MODULE, |
Kent Yoder | cc62508 | 2012-05-14 11:05:42 +0000 | [diff] [blame] | 148 | .cra_init = nx_crypto_ctx_aes_ecb_init, |
| 149 | .cra_exit = nx_crypto_ctx_exit, |
| 150 | .cra_blkcipher = { |
| 151 | .min_keysize = AES_MIN_KEY_SIZE, |
| 152 | .max_keysize = AES_MAX_KEY_SIZE, |
| 153 | .setkey = ecb_aes_nx_set_key, |
| 154 | .encrypt = ecb_aes_nx_encrypt, |
| 155 | .decrypt = ecb_aes_nx_decrypt, |
| 156 | } |
| 157 | }; |