Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 1 | /** |
| 2 | * AES XCBC 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/internal/hash.h> |
| 23 | #include <crypto/aes.h> |
| 24 | #include <crypto/algapi.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/crypto.h> |
| 28 | #include <asm/vio.h> |
| 29 | |
| 30 | #include "nx_csbcpb.h" |
| 31 | #include "nx.h" |
| 32 | |
| 33 | |
| 34 | struct xcbc_state { |
| 35 | u8 state[AES_BLOCK_SIZE]; |
| 36 | unsigned int count; |
| 37 | u8 buffer[AES_BLOCK_SIZE]; |
| 38 | }; |
| 39 | |
| 40 | static int nx_xcbc_set_key(struct crypto_shash *desc, |
| 41 | const u8 *in_key, |
| 42 | unsigned int key_len) |
| 43 | { |
| 44 | struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(desc); |
| 45 | |
| 46 | switch (key_len) { |
| 47 | case AES_KEYSIZE_128: |
| 48 | nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128]; |
| 49 | break; |
| 50 | default: |
| 51 | return -EINVAL; |
| 52 | } |
| 53 | |
| 54 | memcpy(nx_ctx->priv.xcbc.key, in_key, key_len); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 59 | /* |
| 60 | * Based on RFC 3566, for a zero-length message: |
| 61 | * |
| 62 | * n = 1 |
| 63 | * K1 = E(K, 0x01010101010101010101010101010101) |
| 64 | * K3 = E(K, 0x03030303030303030303030303030303) |
| 65 | * E[0] = 0x00000000000000000000000000000000 |
| 66 | * M[1] = 0x80000000000000000000000000000000 (0 length message with padding) |
| 67 | * E[1] = (K1, M[1] ^ E[0] ^ K3) |
| 68 | * Tag = M[1] |
| 69 | */ |
| 70 | static int nx_xcbc_empty(struct shash_desc *desc, u8 *out) |
| 71 | { |
| 72 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); |
| 73 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 74 | struct nx_sg *in_sg, *out_sg; |
| 75 | u8 keys[2][AES_BLOCK_SIZE]; |
| 76 | u8 key[32]; |
| 77 | int rc = 0; |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 78 | int len; |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 79 | |
| 80 | /* Change to ECB mode */ |
| 81 | csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB; |
| 82 | memcpy(key, csbcpb->cpb.aes_xcbc.key, AES_BLOCK_SIZE); |
| 83 | memcpy(csbcpb->cpb.aes_ecb.key, key, AES_BLOCK_SIZE); |
| 84 | NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT; |
| 85 | |
| 86 | /* K1 and K3 base patterns */ |
| 87 | memset(keys[0], 0x01, sizeof(keys[0])); |
| 88 | memset(keys[1], 0x03, sizeof(keys[1])); |
| 89 | |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 90 | len = sizeof(keys); |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 91 | /* Generate K1 and K3 encrypting the patterns */ |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 92 | in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) keys, &len, |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 93 | nx_ctx->ap->sglen); |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 94 | |
| 95 | if (len != sizeof(keys)) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *) keys, &len, |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 99 | nx_ctx->ap->sglen); |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 100 | |
| 101 | if (len != sizeof(keys)) |
| 102 | return -EINVAL; |
| 103 | |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 104 | nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg); |
| 105 | nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg); |
| 106 | |
| 107 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 108 | desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 109 | if (rc) |
| 110 | goto out; |
| 111 | atomic_inc(&(nx_ctx->stats->aes_ops)); |
| 112 | |
| 113 | /* XOr K3 with the padding for a 0 length message */ |
| 114 | keys[1][0] ^= 0x80; |
| 115 | |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 116 | len = sizeof(keys[1]); |
| 117 | |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 118 | /* Encrypt the final result */ |
| 119 | memcpy(csbcpb->cpb.aes_ecb.key, keys[0], AES_BLOCK_SIZE); |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 120 | in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) keys[1], &len, |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 121 | nx_ctx->ap->sglen); |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 122 | |
| 123 | if (len != sizeof(keys[1])) |
| 124 | return -EINVAL; |
| 125 | |
| 126 | len = AES_BLOCK_SIZE; |
| 127 | out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 128 | nx_ctx->ap->sglen); |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 129 | |
| 130 | if (len != AES_BLOCK_SIZE) |
| 131 | return -EINVAL; |
| 132 | |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 133 | nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg); |
| 134 | nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg); |
| 135 | |
| 136 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 137 | desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 138 | if (rc) |
| 139 | goto out; |
| 140 | atomic_inc(&(nx_ctx->stats->aes_ops)); |
| 141 | |
| 142 | out: |
| 143 | /* Restore XCBC mode */ |
| 144 | csbcpb->cpb.hdr.mode = NX_MODE_AES_XCBC_MAC; |
| 145 | memcpy(csbcpb->cpb.aes_xcbc.key, key, AES_BLOCK_SIZE); |
| 146 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT; |
| 147 | |
| 148 | return rc; |
| 149 | } |
| 150 | |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 151 | static int nx_xcbc_init(struct shash_desc *desc) |
| 152 | { |
| 153 | struct xcbc_state *sctx = shash_desc_ctx(desc); |
| 154 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); |
| 155 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 156 | struct nx_sg *out_sg; |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 157 | int len; |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 158 | |
| 159 | nx_ctx_init(nx_ctx, HCOP_FC_AES); |
| 160 | |
| 161 | memset(sctx, 0, sizeof *sctx); |
| 162 | |
| 163 | NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128); |
| 164 | csbcpb->cpb.hdr.mode = NX_MODE_AES_XCBC_MAC; |
| 165 | |
| 166 | memcpy(csbcpb->cpb.aes_xcbc.key, nx_ctx->priv.xcbc.key, AES_BLOCK_SIZE); |
| 167 | memset(nx_ctx->priv.xcbc.key, 0, sizeof *nx_ctx->priv.xcbc.key); |
| 168 | |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 169 | len = AES_BLOCK_SIZE; |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 170 | out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state, |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 171 | &len, nx_ctx->ap->sglen); |
| 172 | |
| 173 | if (len != AES_BLOCK_SIZE) |
| 174 | return -EINVAL; |
| 175 | |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 176 | nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int nx_xcbc_update(struct shash_desc *desc, |
| 182 | const u8 *data, |
| 183 | unsigned int len) |
| 184 | { |
| 185 | struct xcbc_state *sctx = shash_desc_ctx(desc); |
| 186 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); |
| 187 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 188 | struct nx_sg *in_sg; |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 189 | u32 to_process = 0, leftover, total; |
| 190 | unsigned int max_sg_len; |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 191 | unsigned long irq_flags; |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 192 | int rc = 0; |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 193 | int data_len; |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 194 | |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 195 | spin_lock_irqsave(&nx_ctx->lock, irq_flags); |
| 196 | |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 197 | |
| 198 | total = sctx->count + len; |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 199 | |
| 200 | /* 2 cases for total data len: |
| 201 | * 1: <= AES_BLOCK_SIZE: copy into state, return 0 |
| 202 | * 2: > AES_BLOCK_SIZE: process X blocks, copy in leftover |
| 203 | */ |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 204 | if (total <= AES_BLOCK_SIZE) { |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 205 | memcpy(sctx->buffer + sctx->count, data, len); |
| 206 | sctx->count += len; |
| 207 | goto out; |
| 208 | } |
| 209 | |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 210 | in_sg = nx_ctx->in_sg; |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 211 | max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg), |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 212 | nx_ctx->ap->sglen); |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 213 | max_sg_len = min_t(u64, max_sg_len, |
| 214 | nx_ctx->ap->databytelen/NX_PAGE_SIZE); |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 215 | |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 216 | do { |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 217 | to_process = total - to_process; |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 218 | to_process = to_process & ~(AES_BLOCK_SIZE - 1); |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 219 | |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 220 | leftover = total - to_process; |
| 221 | |
| 222 | /* the hardware will not accept a 0 byte operation for this |
| 223 | * algorithm and the operation MUST be finalized to be correct. |
| 224 | * So if we happen to get an update that falls on a block sized |
| 225 | * boundary, we must save off the last block to finalize with |
| 226 | * later. */ |
| 227 | if (!leftover) { |
| 228 | to_process -= AES_BLOCK_SIZE; |
| 229 | leftover = AES_BLOCK_SIZE; |
| 230 | } |
| 231 | |
| 232 | if (sctx->count) { |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 233 | data_len = sctx->count; |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 234 | in_sg = nx_build_sg_list(nx_ctx->in_sg, |
| 235 | (u8 *) sctx->buffer, |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 236 | &data_len, |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 237 | max_sg_len); |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 238 | if (data_len != sctx->count) |
| 239 | return -EINVAL; |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 240 | } |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 241 | |
| 242 | data_len = to_process - sctx->count; |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 243 | in_sg = nx_build_sg_list(in_sg, |
| 244 | (u8 *) data, |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 245 | &data_len, |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 246 | max_sg_len); |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 247 | |
| 248 | if (data_len != to_process - sctx->count) |
| 249 | return -EINVAL; |
| 250 | |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 251 | nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * |
| 252 | sizeof(struct nx_sg); |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 253 | |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 254 | /* we've hit the nx chip previously and we're updating again, |
| 255 | * so copy over the partial digest */ |
| 256 | if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) { |
| 257 | memcpy(csbcpb->cpb.aes_xcbc.cv, |
| 258 | csbcpb->cpb.aes_xcbc.out_cv_mac, |
| 259 | AES_BLOCK_SIZE); |
| 260 | } |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 261 | |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 262 | NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE; |
| 263 | if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) { |
| 264 | rc = -EINVAL; |
| 265 | goto out; |
| 266 | } |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 267 | |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 268 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 269 | desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 270 | if (rc) |
| 271 | goto out; |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 272 | |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 273 | atomic_inc(&(nx_ctx->stats->aes_ops)); |
| 274 | |
| 275 | /* everything after the first update is continuation */ |
| 276 | NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; |
| 277 | |
| 278 | total -= to_process; |
| 279 | data += to_process - sctx->count; |
| 280 | sctx->count = 0; |
| 281 | in_sg = nx_ctx->in_sg; |
| 282 | } while (leftover > AES_BLOCK_SIZE); |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 283 | |
| 284 | /* copy the leftover back into the state struct */ |
Fionnuala Gunter | 9d6f1a8 | 2013-08-29 11:36:36 -0300 | [diff] [blame] | 285 | memcpy(sctx->buffer, data, leftover); |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 286 | sctx->count = leftover; |
| 287 | |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 288 | out: |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 289 | spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 290 | return rc; |
| 291 | } |
| 292 | |
| 293 | static int nx_xcbc_final(struct shash_desc *desc, u8 *out) |
| 294 | { |
| 295 | struct xcbc_state *sctx = shash_desc_ctx(desc); |
| 296 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); |
| 297 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 298 | struct nx_sg *in_sg, *out_sg; |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 299 | unsigned long irq_flags; |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 300 | int rc = 0; |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 301 | int len; |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 302 | |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 303 | spin_lock_irqsave(&nx_ctx->lock, irq_flags); |
| 304 | |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 305 | if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) { |
| 306 | /* we've hit the nx chip previously, now we're finalizing, |
| 307 | * so copy over the partial digest */ |
| 308 | memcpy(csbcpb->cpb.aes_xcbc.cv, |
| 309 | csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE); |
| 310 | } else if (sctx->count == 0) { |
Marcelo Cerri | 41e3173 | 2013-08-29 11:36:38 -0300 | [diff] [blame] | 311 | /* |
| 312 | * we've never seen an update, so this is a 0 byte op. The |
| 313 | * hardware cannot handle a 0 byte op, so just ECB to |
| 314 | * generate the hash. |
| 315 | */ |
| 316 | rc = nx_xcbc_empty(desc, out); |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 317 | goto out; |
| 318 | } |
| 319 | |
| 320 | /* final is represented by continuing the operation and indicating that |
| 321 | * this is not an intermediate operation */ |
| 322 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; |
| 323 | |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 324 | len = sctx->count; |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 325 | in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buffer, |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 326 | &len, nx_ctx->ap->sglen); |
| 327 | |
| 328 | if (len != sctx->count) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | len = AES_BLOCK_SIZE; |
| 332 | out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 333 | nx_ctx->ap->sglen); |
| 334 | |
Leonidas S. Barbosa | 5313231 | 2014-10-28 15:48:47 -0200 | [diff] [blame] | 335 | if (len != AES_BLOCK_SIZE) |
| 336 | return -EINVAL; |
| 337 | |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 338 | nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg); |
| 339 | nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg); |
| 340 | |
| 341 | if (!nx_ctx->op.outlen) { |
| 342 | rc = -EINVAL; |
| 343 | goto out; |
| 344 | } |
| 345 | |
| 346 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 347 | desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 348 | if (rc) |
| 349 | goto out; |
| 350 | |
| 351 | atomic_inc(&(nx_ctx->stats->aes_ops)); |
| 352 | |
| 353 | memcpy(out, csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE); |
| 354 | out: |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 355 | spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); |
Kent Yoder | 6148c1a | 2012-05-14 11:06:09 +0000 | [diff] [blame] | 356 | return rc; |
| 357 | } |
| 358 | |
| 359 | struct shash_alg nx_shash_aes_xcbc_alg = { |
| 360 | .digestsize = AES_BLOCK_SIZE, |
| 361 | .init = nx_xcbc_init, |
| 362 | .update = nx_xcbc_update, |
| 363 | .final = nx_xcbc_final, |
| 364 | .setkey = nx_xcbc_set_key, |
| 365 | .descsize = sizeof(struct xcbc_state), |
| 366 | .statesize = sizeof(struct xcbc_state), |
| 367 | .base = { |
| 368 | .cra_name = "xcbc(aes)", |
| 369 | .cra_driver_name = "xcbc-aes-nx", |
| 370 | .cra_priority = 300, |
| 371 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 372 | .cra_blocksize = AES_BLOCK_SIZE, |
| 373 | .cra_module = THIS_MODULE, |
| 374 | .cra_ctxsize = sizeof(struct nx_crypto_ctx), |
| 375 | .cra_init = nx_crypto_ctx_aes_xcbc_init, |
| 376 | .cra_exit = nx_crypto_ctx_exit, |
| 377 | } |
| 378 | }; |