1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * sl3516-ce-cipher.c - hardware cryptographic offloader for Storlink SL3516 SoC 4 * 5 * Copyright (C) 2021 Corentin LABBE <clabbe@baylibre.com> 6 * 7 * This file adds support for AES cipher with 128,192,256 bits keysize in 8 * ECB mode. 9 */ 10 11 #include <crypto/engine.h> 12 #include <crypto/internal/skcipher.h> 13 #include <crypto/scatterwalk.h> 14 #include <linux/dma-mapping.h> 15 #include <linux/delay.h> 16 #include <linux/err.h> 17 #include <linux/io.h> 18 #include <linux/kernel.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/slab.h> 21 #include <linux/string.h> 22 #include "sl3516-ce.h" 23 24 /* sl3516_ce_need_fallback - check if a request can be handled by the CE */ 25 static bool sl3516_ce_need_fallback(struct skcipher_request *areq) 26 { 27 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 28 struct sl3516_ce_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 29 struct sl3516_ce_dev *ce = op->ce; 30 struct scatterlist *in_sg; 31 struct scatterlist *out_sg; 32 struct scatterlist *sg; 33 34 if (areq->cryptlen == 0 || areq->cryptlen % 16) { 35 ce->fallback_mod16++; 36 return true; 37 } 38 39 /* 40 * check if we have enough descriptors for TX 41 * Note: TX need one control desc for each SG 42 */ 43 if (sg_nents(areq->src) > MAXDESC / 2) { 44 ce->fallback_sg_count_tx++; 45 return true; 46 } 47 /* check if we have enough descriptors for RX */ 48 if (sg_nents(areq->dst) > MAXDESC) { 49 ce->fallback_sg_count_rx++; 50 return true; 51 } 52 53 sg = areq->src; 54 while (sg) { 55 if ((sg->length % 16) != 0) { 56 ce->fallback_mod16++; 57 return true; 58 } 59 if (!IS_ALIGNED(sg->offset, 16)) { 60 ce->fallback_align16++; 61 return true; 62 } 63 sg = sg_next(sg); 64 } 65 sg = areq->dst; 66 while (sg) { 67 if ((sg->length % 16) != 0) { 68 ce->fallback_mod16++; 69 return true; 70 } 71 if (!IS_ALIGNED(sg->offset, 16)) { 72 ce->fallback_align16++; 73 return true; 74 } 75 sg = sg_next(sg); 76 } 77 78 /* need same numbers of SG (with same length) for source and destination */ 79 in_sg = areq->src; 80 out_sg = areq->dst; 81 while (in_sg && out_sg) { 82 if (in_sg->length != out_sg->length) { 83 ce->fallback_not_same_len++; 84 return true; 85 } 86 in_sg = sg_next(in_sg); 87 out_sg = sg_next(out_sg); 88 } 89 if (in_sg || out_sg) 90 return true; 91 92 return false; 93 } 94 95 static int sl3516_ce_cipher_fallback(struct skcipher_request *areq) 96 { 97 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 98 struct sl3516_ce_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 99 struct sl3516_ce_cipher_req_ctx *rctx = skcipher_request_ctx(areq); 100 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 101 struct sl3516_ce_alg_template *algt; 102 int err; 103 104 algt = container_of(alg, struct sl3516_ce_alg_template, alg.skcipher.base); 105 algt->stat_fb++; 106 107 skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm); 108 skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags, 109 areq->base.complete, areq->base.data); 110 skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst, 111 areq->cryptlen, areq->iv); 112 if (rctx->op_dir == CE_DECRYPTION) 113 err = crypto_skcipher_decrypt(&rctx->fallback_req); 114 else 115 err = crypto_skcipher_encrypt(&rctx->fallback_req); 116 return err; 117 } 118 119 static int sl3516_ce_cipher(struct skcipher_request *areq) 120 { 121 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 122 struct sl3516_ce_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 123 struct sl3516_ce_dev *ce = op->ce; 124 struct sl3516_ce_cipher_req_ctx *rctx = skcipher_request_ctx(areq); 125 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 126 struct sl3516_ce_alg_template *algt; 127 struct scatterlist *sg; 128 unsigned int todo, len; 129 struct pkt_control_ecb *ecb; 130 int nr_sgs = 0; 131 int nr_sgd = 0; 132 int err = 0; 133 int i; 134 135 algt = container_of(alg, struct sl3516_ce_alg_template, alg.skcipher.base); 136 137 dev_dbg(ce->dev, "%s %s %u %x IV(%p %u) key=%u\n", __func__, 138 crypto_tfm_alg_name(areq->base.tfm), 139 areq->cryptlen, 140 rctx->op_dir, areq->iv, crypto_skcipher_ivsize(tfm), 141 op->keylen); 142 143 algt->stat_req++; 144 145 if (areq->src == areq->dst) { 146 nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src), 147 DMA_BIDIRECTIONAL); 148 if (nr_sgs <= 0 || nr_sgs > MAXDESC / 2) { 149 dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs); 150 err = -EINVAL; 151 goto theend; 152 } 153 nr_sgd = nr_sgs; 154 } else { 155 nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src), 156 DMA_TO_DEVICE); 157 if (nr_sgs <= 0 || nr_sgs > MAXDESC / 2) { 158 dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs); 159 err = -EINVAL; 160 goto theend; 161 } 162 nr_sgd = dma_map_sg(ce->dev, areq->dst, sg_nents(areq->dst), 163 DMA_FROM_DEVICE); 164 if (nr_sgd <= 0 || nr_sgd > MAXDESC) { 165 dev_err(ce->dev, "Invalid sg number %d\n", nr_sgd); 166 err = -EINVAL; 167 goto theend_sgs; 168 } 169 } 170 171 len = areq->cryptlen; 172 i = 0; 173 sg = areq->src; 174 while (i < nr_sgs && sg && len) { 175 if (sg_dma_len(sg) == 0) 176 goto sgs_next; 177 rctx->t_src[i].addr = sg_dma_address(sg); 178 todo = min(len, sg_dma_len(sg)); 179 rctx->t_src[i].len = todo; 180 dev_dbg(ce->dev, "%s total=%u SGS(%d %u off=%d) todo=%u\n", __func__, 181 areq->cryptlen, i, rctx->t_src[i].len, sg->offset, todo); 182 len -= todo; 183 i++; 184 sgs_next: 185 sg = sg_next(sg); 186 } 187 if (len > 0) { 188 dev_err(ce->dev, "remaining len %d/%u nr_sgs=%d\n", len, areq->cryptlen, nr_sgs); 189 err = -EINVAL; 190 goto theend_sgs; 191 } 192 193 len = areq->cryptlen; 194 i = 0; 195 sg = areq->dst; 196 while (i < nr_sgd && sg && len) { 197 if (sg_dma_len(sg) == 0) 198 goto sgd_next; 199 rctx->t_dst[i].addr = sg_dma_address(sg); 200 todo = min(len, sg_dma_len(sg)); 201 rctx->t_dst[i].len = todo; 202 dev_dbg(ce->dev, "%s total=%u SGD(%d %u off=%d) todo=%u\n", __func__, 203 areq->cryptlen, i, rctx->t_dst[i].len, sg->offset, todo); 204 len -= todo; 205 i++; 206 207 sgd_next: 208 sg = sg_next(sg); 209 } 210 if (len > 0) { 211 dev_err(ce->dev, "remaining len %d\n", len); 212 err = -EINVAL; 213 goto theend_sgs; 214 } 215 216 switch (algt->mode) { 217 case ECB_AES: 218 rctx->pctrllen = sizeof(struct pkt_control_ecb); 219 ecb = (struct pkt_control_ecb *)ce->pctrl; 220 221 rctx->tqflag = TQ0_TYPE_CTRL; 222 rctx->tqflag |= TQ1_CIPHER; 223 ecb->control.op_mode = rctx->op_dir; 224 ecb->control.cipher_algorithm = ECB_AES; 225 ecb->cipher.header_len = 0; 226 ecb->cipher.algorithm_len = areq->cryptlen; 227 cpu_to_be32_array((__be32 *)ecb->key, (u32 *)op->key, op->keylen / 4); 228 rctx->h = &ecb->cipher; 229 230 rctx->tqflag |= TQ4_KEY0; 231 rctx->tqflag |= TQ5_KEY4; 232 rctx->tqflag |= TQ6_KEY6; 233 ecb->control.aesnk = op->keylen / 4; 234 break; 235 } 236 237 rctx->nr_sgs = nr_sgs; 238 rctx->nr_sgd = nr_sgd; 239 err = sl3516_ce_run_task(ce, rctx, crypto_tfm_alg_name(areq->base.tfm)); 240 241 theend_sgs: 242 if (areq->src == areq->dst) { 243 dma_unmap_sg(ce->dev, areq->src, sg_nents(areq->src), 244 DMA_BIDIRECTIONAL); 245 } else { 246 dma_unmap_sg(ce->dev, areq->src, sg_nents(areq->src), 247 DMA_TO_DEVICE); 248 dma_unmap_sg(ce->dev, areq->dst, sg_nents(areq->dst), 249 DMA_FROM_DEVICE); 250 } 251 252 theend: 253 254 return err; 255 } 256 257 int sl3516_ce_handle_cipher_request(struct crypto_engine *engine, void *areq) 258 { 259 int err; 260 struct skcipher_request *breq = container_of(areq, struct skcipher_request, base); 261 262 err = sl3516_ce_cipher(breq); 263 local_bh_disable(); 264 crypto_finalize_skcipher_request(engine, breq, err); 265 local_bh_enable(); 266 267 return 0; 268 } 269 270 int sl3516_ce_skdecrypt(struct skcipher_request *areq) 271 { 272 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 273 struct sl3516_ce_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 274 struct sl3516_ce_cipher_req_ctx *rctx = skcipher_request_ctx(areq); 275 struct crypto_engine *engine; 276 277 memset(rctx, 0, sizeof(struct sl3516_ce_cipher_req_ctx)); 278 rctx->op_dir = CE_DECRYPTION; 279 280 if (sl3516_ce_need_fallback(areq)) 281 return sl3516_ce_cipher_fallback(areq); 282 283 engine = op->ce->engine; 284 285 return crypto_transfer_skcipher_request_to_engine(engine, areq); 286 } 287 288 int sl3516_ce_skencrypt(struct skcipher_request *areq) 289 { 290 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 291 struct sl3516_ce_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 292 struct sl3516_ce_cipher_req_ctx *rctx = skcipher_request_ctx(areq); 293 struct crypto_engine *engine; 294 295 memset(rctx, 0, sizeof(struct sl3516_ce_cipher_req_ctx)); 296 rctx->op_dir = CE_ENCRYPTION; 297 298 if (sl3516_ce_need_fallback(areq)) 299 return sl3516_ce_cipher_fallback(areq); 300 301 engine = op->ce->engine; 302 303 return crypto_transfer_skcipher_request_to_engine(engine, areq); 304 } 305 306 int sl3516_ce_cipher_init(struct crypto_tfm *tfm) 307 { 308 struct sl3516_ce_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm); 309 struct sl3516_ce_alg_template *algt; 310 const char *name = crypto_tfm_alg_name(tfm); 311 struct crypto_skcipher *sktfm = __crypto_skcipher_cast(tfm); 312 struct skcipher_alg *alg = crypto_skcipher_alg(sktfm); 313 int err; 314 315 memset(op, 0, sizeof(struct sl3516_ce_cipher_tfm_ctx)); 316 317 algt = container_of(alg, struct sl3516_ce_alg_template, alg.skcipher.base); 318 op->ce = algt->ce; 319 320 op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK); 321 if (IS_ERR(op->fallback_tfm)) { 322 dev_err(op->ce->dev, "ERROR: Cannot allocate fallback for %s %ld\n", 323 name, PTR_ERR(op->fallback_tfm)); 324 return PTR_ERR(op->fallback_tfm); 325 } 326 327 crypto_skcipher_set_reqsize(sktfm, sizeof(struct sl3516_ce_cipher_req_ctx) + 328 crypto_skcipher_reqsize(op->fallback_tfm)); 329 330 dev_info(op->ce->dev, "Fallback for %s is %s\n", 331 crypto_tfm_alg_driver_name(&sktfm->base), 332 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(op->fallback_tfm))); 333 334 err = pm_runtime_get_sync(op->ce->dev); 335 if (err < 0) 336 goto error_pm; 337 338 return 0; 339 error_pm: 340 pm_runtime_put_noidle(op->ce->dev); 341 crypto_free_skcipher(op->fallback_tfm); 342 return err; 343 } 344 345 void sl3516_ce_cipher_exit(struct crypto_tfm *tfm) 346 { 347 struct sl3516_ce_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm); 348 349 kfree_sensitive(op->key); 350 crypto_free_skcipher(op->fallback_tfm); 351 pm_runtime_put_sync_suspend(op->ce->dev); 352 } 353 354 int sl3516_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, 355 unsigned int keylen) 356 { 357 struct sl3516_ce_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 358 struct sl3516_ce_dev *ce = op->ce; 359 360 switch (keylen) { 361 case 128 / 8: 362 break; 363 case 192 / 8: 364 break; 365 case 256 / 8: 366 break; 367 default: 368 dev_dbg(ce->dev, "ERROR: Invalid keylen %u\n", keylen); 369 return -EINVAL; 370 } 371 kfree_sensitive(op->key); 372 op->keylen = keylen; 373 op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA); 374 if (!op->key) 375 return -ENOMEM; 376 377 crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK); 378 crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK); 379 380 return crypto_skcipher_setkey(op->fallback_tfm, key, keylen); 381 } 382