Thomas Gleixner | 64d85cc | 2019-05-29 07:18:13 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 2 | /** |
| 3 | * AES GCM routines supporting the Power 7+ Nest Accelerators driver |
| 4 | * |
| 5 | * Copyright (C) 2012 International Business Machines Inc. |
| 6 | * |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 7 | * Author: Kent Yoder <yoder1@us.ibm.com> |
| 8 | */ |
| 9 | |
| 10 | #include <crypto/internal/aead.h> |
| 11 | #include <crypto/aes.h> |
David Gstir | cb8affb | 2015-11-15 17:14:41 +0100 | [diff] [blame] | 12 | #include <crypto/algapi.h> |
Corentin LABBE | fdd0f3d | 2017-08-22 10:08:11 +0200 | [diff] [blame] | 13 | #include <crypto/gcm.h> |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 14 | #include <crypto/scatterwalk.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 17 | #include <asm/vio.h> |
| 18 | |
| 19 | #include "nx_csbcpb.h" |
| 20 | #include "nx.h" |
| 21 | |
| 22 | |
| 23 | static int gcm_aes_nx_set_key(struct crypto_aead *tfm, |
| 24 | const u8 *in_key, |
| 25 | unsigned int key_len) |
| 26 | { |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 27 | struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 28 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 29 | struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead; |
| 30 | |
| 31 | nx_ctx_init(nx_ctx, HCOP_FC_AES); |
| 32 | |
| 33 | switch (key_len) { |
| 34 | case AES_KEYSIZE_128: |
| 35 | NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128); |
| 36 | NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_128); |
| 37 | nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128]; |
| 38 | break; |
| 39 | case AES_KEYSIZE_192: |
| 40 | NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192); |
| 41 | NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_192); |
| 42 | nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192]; |
| 43 | break; |
| 44 | case AES_KEYSIZE_256: |
| 45 | NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256); |
| 46 | NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_256); |
| 47 | nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256]; |
| 48 | break; |
| 49 | default: |
| 50 | return -EINVAL; |
| 51 | } |
| 52 | |
| 53 | csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM; |
| 54 | memcpy(csbcpb->cpb.aes_gcm.key, in_key, key_len); |
| 55 | |
| 56 | csbcpb_aead->cpb.hdr.mode = NX_MODE_AES_GCA; |
| 57 | memcpy(csbcpb_aead->cpb.aes_gca.key, in_key, key_len); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int gcm4106_aes_nx_set_key(struct crypto_aead *tfm, |
| 63 | const u8 *in_key, |
| 64 | unsigned int key_len) |
| 65 | { |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 66 | struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 67 | char *nonce = nx_ctx->priv.gcm.nonce; |
| 68 | int rc; |
| 69 | |
| 70 | if (key_len < 4) |
| 71 | return -EINVAL; |
| 72 | |
| 73 | key_len -= 4; |
| 74 | |
| 75 | rc = gcm_aes_nx_set_key(tfm, in_key, key_len); |
| 76 | if (rc) |
| 77 | goto out; |
| 78 | |
| 79 | memcpy(nonce, in_key + key_len, 4); |
| 80 | out: |
| 81 | return rc; |
| 82 | } |
| 83 | |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 84 | static int gcm4106_aes_nx_setauthsize(struct crypto_aead *tfm, |
| 85 | unsigned int authsize) |
| 86 | { |
| 87 | switch (authsize) { |
| 88 | case 8: |
| 89 | case 12: |
| 90 | case 16: |
| 91 | break; |
| 92 | default: |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int nx_gca(struct nx_crypto_ctx *nx_ctx, |
| 100 | struct aead_request *req, |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 101 | u8 *out, |
| 102 | unsigned int assoclen) |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 103 | { |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 104 | int rc; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 105 | struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 106 | struct scatter_walk walk; |
| 107 | struct nx_sg *nx_sg = nx_ctx->in_sg; |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 108 | unsigned int nbytes = assoclen; |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 109 | unsigned int processed = 0, to_process; |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 110 | unsigned int max_sg_len; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 111 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 112 | if (nbytes <= AES_BLOCK_SIZE) { |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 113 | scatterwalk_start(&walk, req->src); |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 114 | scatterwalk_copychunks(out, &walk, nbytes, SCATTERWALK_FROM_SG); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 115 | scatterwalk_done(&walk, SCATTERWALK_FROM_SG, 0); |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 116 | return 0; |
| 117 | } |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 118 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 119 | NX_CPB_FDM(csbcpb_aead) &= ~NX_FDM_CONTINUATION; |
| 120 | |
| 121 | /* page_limit: number of sg entries that fit on one page */ |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 122 | max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg), |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 123 | nx_ctx->ap->sglen); |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 124 | max_sg_len = min_t(u64, max_sg_len, |
| 125 | nx_ctx->ap->databytelen/NX_PAGE_SIZE); |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 126 | |
| 127 | do { |
| 128 | /* |
| 129 | * to_process: the data chunk to process in this update. |
| 130 | * This value is bound by sg list limits. |
| 131 | */ |
| 132 | to_process = min_t(u64, nbytes - processed, |
| 133 | nx_ctx->ap->databytelen); |
| 134 | to_process = min_t(u64, to_process, |
| 135 | NX_PAGE_SIZE * (max_sg_len - 1)); |
| 136 | |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 137 | nx_sg = nx_walk_and_build(nx_ctx->in_sg, max_sg_len, |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 138 | req->src, processed, &to_process); |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 139 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 140 | if ((to_process + processed) < nbytes) |
| 141 | NX_CPB_FDM(csbcpb_aead) |= NX_FDM_INTERMEDIATE; |
| 142 | else |
| 143 | NX_CPB_FDM(csbcpb_aead) &= ~NX_FDM_INTERMEDIATE; |
| 144 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 145 | nx_ctx->op_aead.inlen = (nx_ctx->in_sg - nx_sg) |
| 146 | * sizeof(struct nx_sg); |
| 147 | |
| 148 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op_aead, |
| 149 | req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 150 | if (rc) |
| 151 | return rc; |
| 152 | |
| 153 | memcpy(csbcpb_aead->cpb.aes_gca.in_pat, |
| 154 | csbcpb_aead->cpb.aes_gca.out_pat, |
| 155 | AES_BLOCK_SIZE); |
| 156 | NX_CPB_FDM(csbcpb_aead) |= NX_FDM_CONTINUATION; |
| 157 | |
| 158 | atomic_inc(&(nx_ctx->stats->aes_ops)); |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 159 | atomic64_add(assoclen, &(nx_ctx->stats->aes_bytes)); |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 160 | |
| 161 | processed += to_process; |
| 162 | } while (processed < nbytes); |
| 163 | |
| 164 | memcpy(out, csbcpb_aead->cpb.aes_gca.out_pat, AES_BLOCK_SIZE); |
| 165 | |
| 166 | return rc; |
| 167 | } |
| 168 | |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 169 | static int gmac(struct aead_request *req, struct blkcipher_desc *desc, |
| 170 | unsigned int assoclen) |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 171 | { |
| 172 | int rc; |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 173 | struct nx_crypto_ctx *nx_ctx = |
| 174 | crypto_aead_ctx(crypto_aead_reqtfm(req)); |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 175 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 176 | struct nx_sg *nx_sg; |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 177 | unsigned int nbytes = assoclen; |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 178 | unsigned int processed = 0, to_process; |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 179 | unsigned int max_sg_len; |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 180 | |
| 181 | /* Set GMAC mode */ |
| 182 | csbcpb->cpb.hdr.mode = NX_MODE_AES_GMAC; |
| 183 | |
| 184 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION; |
| 185 | |
| 186 | /* page_limit: number of sg entries that fit on one page */ |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 187 | max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg), |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 188 | nx_ctx->ap->sglen); |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 189 | max_sg_len = min_t(u64, max_sg_len, |
| 190 | nx_ctx->ap->databytelen/NX_PAGE_SIZE); |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 191 | |
| 192 | /* Copy IV */ |
| 193 | memcpy(csbcpb->cpb.aes_gcm.iv_or_cnt, desc->info, AES_BLOCK_SIZE); |
| 194 | |
| 195 | do { |
| 196 | /* |
| 197 | * to_process: the data chunk to process in this update. |
| 198 | * This value is bound by sg list limits. |
| 199 | */ |
| 200 | to_process = min_t(u64, nbytes - processed, |
| 201 | nx_ctx->ap->databytelen); |
| 202 | to_process = min_t(u64, to_process, |
| 203 | NX_PAGE_SIZE * (max_sg_len - 1)); |
| 204 | |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 205 | nx_sg = nx_walk_and_build(nx_ctx->in_sg, max_sg_len, |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 206 | req->src, processed, &to_process); |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 207 | |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 208 | if ((to_process + processed) < nbytes) |
| 209 | NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE; |
| 210 | else |
| 211 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; |
| 212 | |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 213 | nx_ctx->op.inlen = (nx_ctx->in_sg - nx_sg) |
| 214 | * sizeof(struct nx_sg); |
| 215 | |
| 216 | csbcpb->cpb.aes_gcm.bit_length_data = 0; |
| 217 | csbcpb->cpb.aes_gcm.bit_length_aad = 8 * nbytes; |
| 218 | |
| 219 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 220 | req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 221 | if (rc) |
| 222 | goto out; |
| 223 | |
| 224 | memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad, |
| 225 | csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE); |
| 226 | memcpy(csbcpb->cpb.aes_gcm.in_s0, |
| 227 | csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE); |
| 228 | |
| 229 | NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; |
| 230 | |
| 231 | atomic_inc(&(nx_ctx->stats->aes_ops)); |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 232 | atomic64_add(assoclen, &(nx_ctx->stats->aes_bytes)); |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 233 | |
| 234 | processed += to_process; |
| 235 | } while (processed < nbytes); |
| 236 | |
| 237 | out: |
| 238 | /* Restore GCM mode */ |
| 239 | csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM; |
| 240 | return rc; |
| 241 | } |
| 242 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 243 | static int gcm_empty(struct aead_request *req, struct blkcipher_desc *desc, |
| 244 | int enc) |
| 245 | { |
| 246 | int rc; |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 247 | struct nx_crypto_ctx *nx_ctx = |
| 248 | crypto_aead_ctx(crypto_aead_reqtfm(req)); |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 249 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 250 | char out[AES_BLOCK_SIZE]; |
| 251 | struct nx_sg *in_sg, *out_sg; |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 252 | int len; |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 253 | |
| 254 | /* For scenarios where the input message is zero length, AES CTR mode |
| 255 | * may be used. Set the source data to be a single block (16B) of all |
| 256 | * zeros, and set the input IV value to be the same as the GMAC IV |
| 257 | * value. - nx_wb 4.8.1.3 */ |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 258 | |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 259 | /* Change to ECB mode */ |
| 260 | csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB; |
| 261 | memcpy(csbcpb->cpb.aes_ecb.key, csbcpb->cpb.aes_gcm.key, |
| 262 | sizeof(csbcpb->cpb.aes_ecb.key)); |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 263 | if (enc) |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 264 | NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT; |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 265 | else |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 266 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 267 | |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 268 | len = AES_BLOCK_SIZE; |
| 269 | |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 270 | /* Encrypt the counter/IV */ |
| 271 | in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) desc->info, |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 272 | &len, nx_ctx->ap->sglen); |
| 273 | |
| 274 | if (len != AES_BLOCK_SIZE) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | len = sizeof(out); |
| 278 | out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *) out, &len, |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 279 | nx_ctx->ap->sglen); |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 280 | |
| 281 | if (len != sizeof(out)) |
| 282 | return -EINVAL; |
| 283 | |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 284 | nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg); |
| 285 | nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg); |
| 286 | |
| 287 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 288 | desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 289 | if (rc) |
| 290 | goto out; |
| 291 | atomic_inc(&(nx_ctx->stats->aes_ops)); |
| 292 | |
| 293 | /* Copy out the auth tag */ |
| 294 | memcpy(csbcpb->cpb.aes_gcm.out_pat_or_mac, out, |
| 295 | crypto_aead_authsize(crypto_aead_reqtfm(req))); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 296 | out: |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 297 | /* Restore XCBC mode */ |
| 298 | csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM; |
| 299 | |
| 300 | /* |
| 301 | * ECB key uses the same region that GCM AAD and counter, so it's safe |
| 302 | * to just fill it with zeroes. |
| 303 | */ |
| 304 | memset(csbcpb->cpb.aes_ecb.key, 0, sizeof(csbcpb->cpb.aes_ecb.key)); |
| 305 | |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 306 | return rc; |
| 307 | } |
| 308 | |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 309 | static int gcm_aes_nx_crypt(struct aead_request *req, int enc, |
| 310 | unsigned int assoclen) |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 311 | { |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 312 | struct nx_crypto_ctx *nx_ctx = |
| 313 | crypto_aead_ctx(crypto_aead_reqtfm(req)); |
Herbert Xu | 030f4e9 | 2015-07-07 17:30:25 +0800 | [diff] [blame] | 314 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 315 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 316 | struct blkcipher_desc desc; |
| 317 | unsigned int nbytes = req->cryptlen; |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 318 | unsigned int processed = 0, to_process; |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 319 | unsigned long irq_flags; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 320 | int rc = -EINVAL; |
| 321 | |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 322 | spin_lock_irqsave(&nx_ctx->lock, irq_flags); |
| 323 | |
Herbert Xu | 030f4e9 | 2015-07-07 17:30:25 +0800 | [diff] [blame] | 324 | desc.info = rctx->iv; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 325 | /* initialize the counter */ |
| 326 | *(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1; |
| 327 | |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 328 | if (nbytes == 0) { |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 329 | if (assoclen == 0) |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 330 | rc = gcm_empty(req, &desc, enc); |
| 331 | else |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 332 | rc = gmac(req, &desc, assoclen); |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 333 | if (rc) |
| 334 | goto out; |
| 335 | else |
| 336 | goto mac; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 339 | /* Process associated data */ |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 340 | csbcpb->cpb.aes_gcm.bit_length_aad = assoclen * 8; |
| 341 | if (assoclen) { |
| 342 | rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad, |
| 343 | assoclen); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 344 | if (rc) |
| 345 | goto out; |
| 346 | } |
| 347 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 348 | /* Set flags for encryption */ |
| 349 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION; |
| 350 | if (enc) { |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 351 | NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT; |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 352 | } else { |
| 353 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT; |
Kent Yoder | 1ad936e | 2013-04-12 17:13:59 +0000 | [diff] [blame] | 354 | nbytes -= crypto_aead_authsize(crypto_aead_reqtfm(req)); |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 355 | } |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 356 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 357 | do { |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 358 | to_process = nbytes - processed; |
| 359 | |
| 360 | csbcpb->cpb.aes_gcm.bit_length_data = nbytes * 8; |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 361 | rc = nx_build_sg_lists(nx_ctx, &desc, req->dst, |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 362 | req->src, &to_process, |
| 363 | processed + req->assoclen, |
Leonidas S. Barbosa | e13a79a | 2014-10-28 15:47:48 -0200 | [diff] [blame] | 364 | csbcpb->cpb.aes_gcm.iv_or_cnt); |
| 365 | |
| 366 | if (rc) |
| 367 | goto out; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 368 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 369 | if ((to_process + processed) < nbytes) |
| 370 | NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE; |
| 371 | else |
| 372 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 373 | |
Marcelo Cerri | 7998043 | 2013-08-29 11:36:35 -0300 | [diff] [blame] | 374 | |
| 375 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 376 | req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 377 | if (rc) |
| 378 | goto out; |
| 379 | |
| 380 | memcpy(desc.info, csbcpb->cpb.aes_gcm.out_cnt, AES_BLOCK_SIZE); |
| 381 | memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad, |
| 382 | csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE); |
| 383 | memcpy(csbcpb->cpb.aes_gcm.in_s0, |
| 384 | csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE); |
| 385 | |
| 386 | NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; |
| 387 | |
| 388 | atomic_inc(&(nx_ctx->stats->aes_ops)); |
| 389 | atomic64_add(csbcpb->csb.processed_byte_count, |
| 390 | &(nx_ctx->stats->aes_bytes)); |
| 391 | |
| 392 | processed += to_process; |
| 393 | } while (processed < nbytes); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 394 | |
Marcelo Cerri | dec0ed6 | 2013-08-29 11:36:39 -0300 | [diff] [blame] | 395 | mac: |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 396 | if (enc) { |
| 397 | /* copy out the auth tag */ |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 398 | scatterwalk_map_and_copy( |
| 399 | csbcpb->cpb.aes_gcm.out_pat_or_mac, |
| 400 | req->dst, req->assoclen + nbytes, |
| 401 | crypto_aead_authsize(crypto_aead_reqtfm(req)), |
| 402 | SCATTERWALK_TO_SG); |
jmlatten@linux.vnet.ibm.com | b4eba0c | 2013-08-14 17:17:57 -0500 | [diff] [blame] | 403 | } else { |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 404 | u8 *itag = nx_ctx->priv.gcm.iauth_tag; |
| 405 | u8 *otag = csbcpb->cpb.aes_gcm.out_pat_or_mac; |
| 406 | |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 407 | scatterwalk_map_and_copy( |
| 408 | itag, req->src, req->assoclen + nbytes, |
| 409 | crypto_aead_authsize(crypto_aead_reqtfm(req)), |
| 410 | SCATTERWALK_FROM_SG); |
David Gstir | cb8affb | 2015-11-15 17:14:41 +0100 | [diff] [blame] | 411 | rc = crypto_memneq(itag, otag, |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 412 | crypto_aead_authsize(crypto_aead_reqtfm(req))) ? |
| 413 | -EBADMSG : 0; |
| 414 | } |
| 415 | out: |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 416 | spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 417 | return rc; |
| 418 | } |
| 419 | |
| 420 | static int gcm_aes_nx_encrypt(struct aead_request *req) |
| 421 | { |
Herbert Xu | 030f4e9 | 2015-07-07 17:30:25 +0800 | [diff] [blame] | 422 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); |
| 423 | char *iv = rctx->iv; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 424 | |
Corentin LABBE | fdd0f3d | 2017-08-22 10:08:11 +0200 | [diff] [blame] | 425 | memcpy(iv, req->iv, GCM_AES_IV_SIZE); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 426 | |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 427 | return gcm_aes_nx_crypt(req, 1, req->assoclen); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static int gcm_aes_nx_decrypt(struct aead_request *req) |
| 431 | { |
Herbert Xu | 030f4e9 | 2015-07-07 17:30:25 +0800 | [diff] [blame] | 432 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); |
| 433 | char *iv = rctx->iv; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 434 | |
Corentin LABBE | fdd0f3d | 2017-08-22 10:08:11 +0200 | [diff] [blame] | 435 | memcpy(iv, req->iv, GCM_AES_IV_SIZE); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 436 | |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 437 | return gcm_aes_nx_crypt(req, 0, req->assoclen); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static int gcm4106_aes_nx_encrypt(struct aead_request *req) |
| 441 | { |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 442 | struct nx_crypto_ctx *nx_ctx = |
| 443 | crypto_aead_ctx(crypto_aead_reqtfm(req)); |
Herbert Xu | 030f4e9 | 2015-07-07 17:30:25 +0800 | [diff] [blame] | 444 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); |
| 445 | char *iv = rctx->iv; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 446 | char *nonce = nx_ctx->priv.gcm.nonce; |
| 447 | |
| 448 | memcpy(iv, nonce, NX_GCM4106_NONCE_LEN); |
| 449 | memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8); |
| 450 | |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 451 | if (req->assoclen < 8) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | return gcm_aes_nx_crypt(req, 1, req->assoclen - 8); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static int gcm4106_aes_nx_decrypt(struct aead_request *req) |
| 458 | { |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 459 | struct nx_crypto_ctx *nx_ctx = |
| 460 | crypto_aead_ctx(crypto_aead_reqtfm(req)); |
Herbert Xu | 030f4e9 | 2015-07-07 17:30:25 +0800 | [diff] [blame] | 461 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); |
| 462 | char *iv = rctx->iv; |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 463 | char *nonce = nx_ctx->priv.gcm.nonce; |
| 464 | |
| 465 | memcpy(iv, nonce, NX_GCM4106_NONCE_LEN); |
| 466 | memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8); |
| 467 | |
Herbert Xu | c3d2194 | 2015-07-09 07:17:31 +0800 | [diff] [blame] | 468 | if (req->assoclen < 8) |
| 469 | return -EINVAL; |
| 470 | |
| 471 | return gcm_aes_nx_crypt(req, 0, req->assoclen - 8); |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* tell the block cipher walk routines that this is a stream cipher by |
| 475 | * setting cra_blocksize to 1. Even using blkcipher_walk_virt_block |
| 476 | * during encrypt/decrypt doesn't solve this problem, because it calls |
| 477 | * blkcipher_walk_done under the covers, which doesn't use walk->blocksize, |
| 478 | * but instead uses this tfm->blocksize. */ |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 479 | struct aead_alg nx_gcm_aes_alg = { |
| 480 | .base = { |
| 481 | .cra_name = "gcm(aes)", |
| 482 | .cra_driver_name = "gcm-aes-nx", |
| 483 | .cra_priority = 300, |
| 484 | .cra_blocksize = 1, |
| 485 | .cra_ctxsize = sizeof(struct nx_crypto_ctx), |
| 486 | .cra_module = THIS_MODULE, |
| 487 | }, |
| 488 | .init = nx_crypto_ctx_aes_gcm_init, |
| 489 | .exit = nx_crypto_ctx_aead_exit, |
Corentin LABBE | fdd0f3d | 2017-08-22 10:08:11 +0200 | [diff] [blame] | 490 | .ivsize = GCM_AES_IV_SIZE, |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 491 | .maxauthsize = AES_BLOCK_SIZE, |
| 492 | .setkey = gcm_aes_nx_set_key, |
| 493 | .encrypt = gcm_aes_nx_encrypt, |
| 494 | .decrypt = gcm_aes_nx_decrypt, |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 495 | }; |
| 496 | |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 497 | struct aead_alg nx_gcm4106_aes_alg = { |
| 498 | .base = { |
| 499 | .cra_name = "rfc4106(gcm(aes))", |
| 500 | .cra_driver_name = "rfc4106-gcm-aes-nx", |
| 501 | .cra_priority = 300, |
| 502 | .cra_blocksize = 1, |
| 503 | .cra_ctxsize = sizeof(struct nx_crypto_ctx), |
| 504 | .cra_module = THIS_MODULE, |
| 505 | }, |
| 506 | .init = nx_crypto_ctx_aes_gcm_init, |
| 507 | .exit = nx_crypto_ctx_aead_exit, |
Corentin LABBE | fdd0f3d | 2017-08-22 10:08:11 +0200 | [diff] [blame] | 508 | .ivsize = GCM_RFC4106_IV_SIZE, |
Herbert Xu | 201f28f | 2015-06-16 13:54:21 +0800 | [diff] [blame] | 509 | .maxauthsize = AES_BLOCK_SIZE, |
| 510 | .setkey = gcm4106_aes_nx_set_key, |
| 511 | .setauthsize = gcm4106_aes_nx_setauthsize, |
| 512 | .encrypt = gcm4106_aes_nx_encrypt, |
| 513 | .decrypt = gcm4106_aes_nx_decrypt, |
Kent Yoder | f2a15f1 | 2012-05-14 11:05:59 +0000 | [diff] [blame] | 514 | }; |