1*06f751b6SCorentin Labbe // SPDX-License-Identifier: GPL-2.0 2*06f751b6SCorentin Labbe /* 3*06f751b6SCorentin Labbe * sun8i-ce-cipher.c - hardware cryptographic offloader for 4*06f751b6SCorentin Labbe * Allwinner H3/A64/H5/H2+/H6/R40 SoC 5*06f751b6SCorentin Labbe * 6*06f751b6SCorentin Labbe * Copyright (C) 2016-2019 Corentin LABBE <clabbe.montjoie@gmail.com> 7*06f751b6SCorentin Labbe * 8*06f751b6SCorentin Labbe * This file add support for AES cipher with 128,192,256 bits keysize in 9*06f751b6SCorentin Labbe * CBC and ECB mode. 10*06f751b6SCorentin Labbe * 11*06f751b6SCorentin Labbe * You could find a link for the datasheet in Documentation/arm/sunxi/README 12*06f751b6SCorentin Labbe */ 13*06f751b6SCorentin Labbe 14*06f751b6SCorentin Labbe #include <linux/crypto.h> 15*06f751b6SCorentin Labbe #include <linux/dma-mapping.h> 16*06f751b6SCorentin Labbe #include <linux/io.h> 17*06f751b6SCorentin Labbe #include <linux/pm_runtime.h> 18*06f751b6SCorentin Labbe #include <crypto/scatterwalk.h> 19*06f751b6SCorentin Labbe #include <crypto/internal/des.h> 20*06f751b6SCorentin Labbe #include <crypto/internal/skcipher.h> 21*06f751b6SCorentin Labbe #include "sun8i-ce.h" 22*06f751b6SCorentin Labbe 23*06f751b6SCorentin Labbe static int sun8i_ce_cipher_need_fallback(struct skcipher_request *areq) 24*06f751b6SCorentin Labbe { 25*06f751b6SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 26*06f751b6SCorentin Labbe struct scatterlist *sg; 27*06f751b6SCorentin Labbe 28*06f751b6SCorentin Labbe if (sg_nents(areq->src) > MAX_SG || sg_nents(areq->dst) > MAX_SG) 29*06f751b6SCorentin Labbe return true; 30*06f751b6SCorentin Labbe 31*06f751b6SCorentin Labbe if (areq->cryptlen < crypto_skcipher_ivsize(tfm)) 32*06f751b6SCorentin Labbe return true; 33*06f751b6SCorentin Labbe 34*06f751b6SCorentin Labbe if (areq->cryptlen == 0 || areq->cryptlen % 16) 35*06f751b6SCorentin Labbe return true; 36*06f751b6SCorentin Labbe 37*06f751b6SCorentin Labbe sg = areq->src; 38*06f751b6SCorentin Labbe while (sg) { 39*06f751b6SCorentin Labbe if (sg->length % 4 || !IS_ALIGNED(sg->offset, sizeof(u32))) 40*06f751b6SCorentin Labbe return true; 41*06f751b6SCorentin Labbe sg = sg_next(sg); 42*06f751b6SCorentin Labbe } 43*06f751b6SCorentin Labbe sg = areq->dst; 44*06f751b6SCorentin Labbe while (sg) { 45*06f751b6SCorentin Labbe if (sg->length % 4 || !IS_ALIGNED(sg->offset, sizeof(u32))) 46*06f751b6SCorentin Labbe return true; 47*06f751b6SCorentin Labbe sg = sg_next(sg); 48*06f751b6SCorentin Labbe } 49*06f751b6SCorentin Labbe return false; 50*06f751b6SCorentin Labbe } 51*06f751b6SCorentin Labbe 52*06f751b6SCorentin Labbe static int sun8i_ce_cipher_fallback(struct skcipher_request *areq) 53*06f751b6SCorentin Labbe { 54*06f751b6SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 55*06f751b6SCorentin Labbe struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 56*06f751b6SCorentin Labbe struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq); 57*06f751b6SCorentin Labbe int err; 58*06f751b6SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG 59*06f751b6SCorentin Labbe struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 60*06f751b6SCorentin Labbe struct sun8i_ce_alg_template *algt; 61*06f751b6SCorentin Labbe #endif 62*06f751b6SCorentin Labbe SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, op->fallback_tfm); 63*06f751b6SCorentin Labbe 64*06f751b6SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG 65*06f751b6SCorentin Labbe algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher); 66*06f751b6SCorentin Labbe algt->stat_fb++; 67*06f751b6SCorentin Labbe #endif 68*06f751b6SCorentin Labbe 69*06f751b6SCorentin Labbe skcipher_request_set_sync_tfm(subreq, op->fallback_tfm); 70*06f751b6SCorentin Labbe skcipher_request_set_callback(subreq, areq->base.flags, NULL, NULL); 71*06f751b6SCorentin Labbe skcipher_request_set_crypt(subreq, areq->src, areq->dst, 72*06f751b6SCorentin Labbe areq->cryptlen, areq->iv); 73*06f751b6SCorentin Labbe if (rctx->op_dir & CE_DECRYPTION) 74*06f751b6SCorentin Labbe err = crypto_skcipher_decrypt(subreq); 75*06f751b6SCorentin Labbe else 76*06f751b6SCorentin Labbe err = crypto_skcipher_encrypt(subreq); 77*06f751b6SCorentin Labbe skcipher_request_zero(subreq); 78*06f751b6SCorentin Labbe return err; 79*06f751b6SCorentin Labbe } 80*06f751b6SCorentin Labbe 81*06f751b6SCorentin Labbe static int sun8i_ce_cipher(struct skcipher_request *areq) 82*06f751b6SCorentin Labbe { 83*06f751b6SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 84*06f751b6SCorentin Labbe struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 85*06f751b6SCorentin Labbe struct sun8i_ce_dev *ce = op->ce; 86*06f751b6SCorentin Labbe struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq); 87*06f751b6SCorentin Labbe struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 88*06f751b6SCorentin Labbe struct sun8i_ce_alg_template *algt; 89*06f751b6SCorentin Labbe struct sun8i_ce_flow *chan; 90*06f751b6SCorentin Labbe struct ce_task *cet; 91*06f751b6SCorentin Labbe struct scatterlist *sg; 92*06f751b6SCorentin Labbe unsigned int todo, len, offset, ivsize; 93*06f751b6SCorentin Labbe void *backup_iv = NULL; 94*06f751b6SCorentin Labbe int flow, i; 95*06f751b6SCorentin Labbe int nr_sgs = 0; 96*06f751b6SCorentin Labbe int nr_sgd = 0; 97*06f751b6SCorentin Labbe int err = 0; 98*06f751b6SCorentin Labbe 99*06f751b6SCorentin Labbe algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher); 100*06f751b6SCorentin Labbe 101*06f751b6SCorentin Labbe dev_dbg(ce->dev, "%s %s %u %x IV(%p %u) key=%u\n", __func__, 102*06f751b6SCorentin Labbe crypto_tfm_alg_name(areq->base.tfm), 103*06f751b6SCorentin Labbe areq->cryptlen, 104*06f751b6SCorentin Labbe rctx->op_dir, areq->iv, crypto_skcipher_ivsize(tfm), 105*06f751b6SCorentin Labbe op->keylen); 106*06f751b6SCorentin Labbe 107*06f751b6SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG 108*06f751b6SCorentin Labbe algt->stat_req++; 109*06f751b6SCorentin Labbe #endif 110*06f751b6SCorentin Labbe 111*06f751b6SCorentin Labbe flow = rctx->flow; 112*06f751b6SCorentin Labbe 113*06f751b6SCorentin Labbe chan = &ce->chanlist[flow]; 114*06f751b6SCorentin Labbe 115*06f751b6SCorentin Labbe cet = chan->tl; 116*06f751b6SCorentin Labbe memset(cet, 0, sizeof(struct ce_task)); 117*06f751b6SCorentin Labbe 118*06f751b6SCorentin Labbe cet->t_id = flow; 119*06f751b6SCorentin Labbe cet->t_common_ctl = ce->variant->alg_cipher[algt->ce_algo_id]; 120*06f751b6SCorentin Labbe cet->t_common_ctl |= rctx->op_dir | CE_COMM_INT; 121*06f751b6SCorentin Labbe cet->t_dlen = areq->cryptlen / 4; 122*06f751b6SCorentin Labbe /* CTS and recent CE (H6) need length in bytes, in word otherwise */ 123*06f751b6SCorentin Labbe if (ce->variant->has_t_dlen_in_bytes) 124*06f751b6SCorentin Labbe cet->t_dlen = areq->cryptlen; 125*06f751b6SCorentin Labbe 126*06f751b6SCorentin Labbe cet->t_sym_ctl = ce->variant->op_mode[algt->ce_blockmode]; 127*06f751b6SCorentin Labbe len = op->keylen; 128*06f751b6SCorentin Labbe switch (len) { 129*06f751b6SCorentin Labbe case 128 / 8: 130*06f751b6SCorentin Labbe cet->t_sym_ctl |= CE_AES_128BITS; 131*06f751b6SCorentin Labbe break; 132*06f751b6SCorentin Labbe case 192 / 8: 133*06f751b6SCorentin Labbe cet->t_sym_ctl |= CE_AES_192BITS; 134*06f751b6SCorentin Labbe break; 135*06f751b6SCorentin Labbe case 256 / 8: 136*06f751b6SCorentin Labbe cet->t_sym_ctl |= CE_AES_256BITS; 137*06f751b6SCorentin Labbe break; 138*06f751b6SCorentin Labbe } 139*06f751b6SCorentin Labbe 140*06f751b6SCorentin Labbe cet->t_asym_ctl = 0; 141*06f751b6SCorentin Labbe 142*06f751b6SCorentin Labbe chan->op_mode = ce->variant->op_mode[algt->ce_blockmode]; 143*06f751b6SCorentin Labbe chan->op_dir = rctx->op_dir; 144*06f751b6SCorentin Labbe chan->method = ce->variant->alg_cipher[algt->ce_algo_id]; 145*06f751b6SCorentin Labbe chan->keylen = op->keylen; 146*06f751b6SCorentin Labbe 147*06f751b6SCorentin Labbe cet->t_key = dma_map_single(ce->dev, op->key, op->keylen, 148*06f751b6SCorentin Labbe DMA_TO_DEVICE); 149*06f751b6SCorentin Labbe if (dma_mapping_error(ce->dev, cet->t_key)) { 150*06f751b6SCorentin Labbe dev_err(ce->dev, "Cannot DMA MAP KEY\n"); 151*06f751b6SCorentin Labbe err = -EFAULT; 152*06f751b6SCorentin Labbe goto theend; 153*06f751b6SCorentin Labbe } 154*06f751b6SCorentin Labbe 155*06f751b6SCorentin Labbe ivsize = crypto_skcipher_ivsize(tfm); 156*06f751b6SCorentin Labbe if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) { 157*06f751b6SCorentin Labbe chan->ivlen = ivsize; 158*06f751b6SCorentin Labbe chan->bounce_iv = kzalloc(ivsize, GFP_KERNEL | GFP_DMA); 159*06f751b6SCorentin Labbe if (!chan->bounce_iv) { 160*06f751b6SCorentin Labbe err = -ENOMEM; 161*06f751b6SCorentin Labbe goto theend_key; 162*06f751b6SCorentin Labbe } 163*06f751b6SCorentin Labbe if (rctx->op_dir & CE_DECRYPTION) { 164*06f751b6SCorentin Labbe backup_iv = kzalloc(ivsize, GFP_KERNEL); 165*06f751b6SCorentin Labbe if (!backup_iv) { 166*06f751b6SCorentin Labbe err = -ENOMEM; 167*06f751b6SCorentin Labbe goto theend_key; 168*06f751b6SCorentin Labbe } 169*06f751b6SCorentin Labbe offset = areq->cryptlen - ivsize; 170*06f751b6SCorentin Labbe scatterwalk_map_and_copy(backup_iv, areq->src, offset, 171*06f751b6SCorentin Labbe ivsize, 0); 172*06f751b6SCorentin Labbe } 173*06f751b6SCorentin Labbe memcpy(chan->bounce_iv, areq->iv, ivsize); 174*06f751b6SCorentin Labbe cet->t_iv = dma_map_single(ce->dev, chan->bounce_iv, 175*06f751b6SCorentin Labbe chan->ivlen, DMA_TO_DEVICE); 176*06f751b6SCorentin Labbe if (dma_mapping_error(ce->dev, cet->t_iv)) { 177*06f751b6SCorentin Labbe dev_err(ce->dev, "Cannot DMA MAP IV\n"); 178*06f751b6SCorentin Labbe err = -ENOMEM; 179*06f751b6SCorentin Labbe goto theend_iv; 180*06f751b6SCorentin Labbe } 181*06f751b6SCorentin Labbe } 182*06f751b6SCorentin Labbe 183*06f751b6SCorentin Labbe if (areq->src == areq->dst) { 184*06f751b6SCorentin Labbe nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src), 185*06f751b6SCorentin Labbe DMA_BIDIRECTIONAL); 186*06f751b6SCorentin Labbe if (nr_sgs <= 0 || nr_sgs > MAX_SG) { 187*06f751b6SCorentin Labbe dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs); 188*06f751b6SCorentin Labbe err = -EINVAL; 189*06f751b6SCorentin Labbe goto theend_iv; 190*06f751b6SCorentin Labbe } 191*06f751b6SCorentin Labbe nr_sgd = nr_sgs; 192*06f751b6SCorentin Labbe } else { 193*06f751b6SCorentin Labbe nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src), 194*06f751b6SCorentin Labbe DMA_TO_DEVICE); 195*06f751b6SCorentin Labbe if (nr_sgs <= 0 || nr_sgs > MAX_SG) { 196*06f751b6SCorentin Labbe dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs); 197*06f751b6SCorentin Labbe err = -EINVAL; 198*06f751b6SCorentin Labbe goto theend_iv; 199*06f751b6SCorentin Labbe } 200*06f751b6SCorentin Labbe nr_sgd = dma_map_sg(ce->dev, areq->dst, sg_nents(areq->dst), 201*06f751b6SCorentin Labbe DMA_FROM_DEVICE); 202*06f751b6SCorentin Labbe if (nr_sgd <= 0 || nr_sgd > MAX_SG) { 203*06f751b6SCorentin Labbe dev_err(ce->dev, "Invalid sg number %d\n", nr_sgd); 204*06f751b6SCorentin Labbe err = -EINVAL; 205*06f751b6SCorentin Labbe goto theend_sgs; 206*06f751b6SCorentin Labbe } 207*06f751b6SCorentin Labbe } 208*06f751b6SCorentin Labbe 209*06f751b6SCorentin Labbe len = areq->cryptlen; 210*06f751b6SCorentin Labbe for_each_sg(areq->src, sg, nr_sgs, i) { 211*06f751b6SCorentin Labbe cet->t_src[i].addr = sg_dma_address(sg); 212*06f751b6SCorentin Labbe todo = min(len, sg_dma_len(sg)); 213*06f751b6SCorentin Labbe cet->t_src[i].len = todo / 4; 214*06f751b6SCorentin Labbe dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__, 215*06f751b6SCorentin Labbe areq->cryptlen, i, cet->t_src[i].len, sg->offset, todo); 216*06f751b6SCorentin Labbe len -= todo; 217*06f751b6SCorentin Labbe } 218*06f751b6SCorentin Labbe if (len > 0) { 219*06f751b6SCorentin Labbe dev_err(ce->dev, "remaining len %d\n", len); 220*06f751b6SCorentin Labbe err = -EINVAL; 221*06f751b6SCorentin Labbe goto theend_sgs; 222*06f751b6SCorentin Labbe } 223*06f751b6SCorentin Labbe 224*06f751b6SCorentin Labbe len = areq->cryptlen; 225*06f751b6SCorentin Labbe for_each_sg(areq->dst, sg, nr_sgd, i) { 226*06f751b6SCorentin Labbe cet->t_dst[i].addr = sg_dma_address(sg); 227*06f751b6SCorentin Labbe todo = min(len, sg_dma_len(sg)); 228*06f751b6SCorentin Labbe cet->t_dst[i].len = todo / 4; 229*06f751b6SCorentin Labbe dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__, 230*06f751b6SCorentin Labbe areq->cryptlen, i, cet->t_dst[i].len, sg->offset, todo); 231*06f751b6SCorentin Labbe len -= todo; 232*06f751b6SCorentin Labbe } 233*06f751b6SCorentin Labbe if (len > 0) { 234*06f751b6SCorentin Labbe dev_err(ce->dev, "remaining len %d\n", len); 235*06f751b6SCorentin Labbe err = -EINVAL; 236*06f751b6SCorentin Labbe goto theend_sgs; 237*06f751b6SCorentin Labbe } 238*06f751b6SCorentin Labbe 239*06f751b6SCorentin Labbe chan->timeout = areq->cryptlen; 240*06f751b6SCorentin Labbe err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm)); 241*06f751b6SCorentin Labbe 242*06f751b6SCorentin Labbe theend_sgs: 243*06f751b6SCorentin Labbe if (areq->src == areq->dst) { 244*06f751b6SCorentin Labbe dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_BIDIRECTIONAL); 245*06f751b6SCorentin Labbe } else { 246*06f751b6SCorentin Labbe if (nr_sgs > 0) 247*06f751b6SCorentin Labbe dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_TO_DEVICE); 248*06f751b6SCorentin Labbe dma_unmap_sg(ce->dev, areq->dst, nr_sgd, DMA_FROM_DEVICE); 249*06f751b6SCorentin Labbe } 250*06f751b6SCorentin Labbe 251*06f751b6SCorentin Labbe theend_iv: 252*06f751b6SCorentin Labbe if (areq->iv && ivsize > 0) { 253*06f751b6SCorentin Labbe if (cet->t_iv) 254*06f751b6SCorentin Labbe dma_unmap_single(ce->dev, cet->t_iv, chan->ivlen, 255*06f751b6SCorentin Labbe DMA_TO_DEVICE); 256*06f751b6SCorentin Labbe offset = areq->cryptlen - ivsize; 257*06f751b6SCorentin Labbe if (rctx->op_dir & CE_DECRYPTION) { 258*06f751b6SCorentin Labbe memcpy(areq->iv, backup_iv, ivsize); 259*06f751b6SCorentin Labbe kzfree(backup_iv); 260*06f751b6SCorentin Labbe } else { 261*06f751b6SCorentin Labbe scatterwalk_map_and_copy(areq->iv, areq->dst, offset, 262*06f751b6SCorentin Labbe ivsize, 0); 263*06f751b6SCorentin Labbe } 264*06f751b6SCorentin Labbe kfree(chan->bounce_iv); 265*06f751b6SCorentin Labbe } 266*06f751b6SCorentin Labbe 267*06f751b6SCorentin Labbe theend_key: 268*06f751b6SCorentin Labbe dma_unmap_single(ce->dev, cet->t_key, op->keylen, DMA_TO_DEVICE); 269*06f751b6SCorentin Labbe 270*06f751b6SCorentin Labbe theend: 271*06f751b6SCorentin Labbe return err; 272*06f751b6SCorentin Labbe } 273*06f751b6SCorentin Labbe 274*06f751b6SCorentin Labbe static int sun8i_ce_handle_cipher_request(struct crypto_engine *engine, void *areq) 275*06f751b6SCorentin Labbe { 276*06f751b6SCorentin Labbe int err; 277*06f751b6SCorentin Labbe struct skcipher_request *breq = container_of(areq, struct skcipher_request, base); 278*06f751b6SCorentin Labbe 279*06f751b6SCorentin Labbe err = sun8i_ce_cipher(breq); 280*06f751b6SCorentin Labbe crypto_finalize_skcipher_request(engine, breq, err); 281*06f751b6SCorentin Labbe 282*06f751b6SCorentin Labbe return 0; 283*06f751b6SCorentin Labbe } 284*06f751b6SCorentin Labbe 285*06f751b6SCorentin Labbe int sun8i_ce_skdecrypt(struct skcipher_request *areq) 286*06f751b6SCorentin Labbe { 287*06f751b6SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 288*06f751b6SCorentin Labbe struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 289*06f751b6SCorentin Labbe struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq); 290*06f751b6SCorentin Labbe struct crypto_engine *engine; 291*06f751b6SCorentin Labbe int e; 292*06f751b6SCorentin Labbe 293*06f751b6SCorentin Labbe rctx->op_dir = CE_DECRYPTION; 294*06f751b6SCorentin Labbe if (sun8i_ce_cipher_need_fallback(areq)) 295*06f751b6SCorentin Labbe return sun8i_ce_cipher_fallback(areq); 296*06f751b6SCorentin Labbe 297*06f751b6SCorentin Labbe e = sun8i_ce_get_engine_number(op->ce); 298*06f751b6SCorentin Labbe rctx->flow = e; 299*06f751b6SCorentin Labbe engine = op->ce->chanlist[e].engine; 300*06f751b6SCorentin Labbe 301*06f751b6SCorentin Labbe return crypto_transfer_skcipher_request_to_engine(engine, areq); 302*06f751b6SCorentin Labbe } 303*06f751b6SCorentin Labbe 304*06f751b6SCorentin Labbe int sun8i_ce_skencrypt(struct skcipher_request *areq) 305*06f751b6SCorentin Labbe { 306*06f751b6SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); 307*06f751b6SCorentin Labbe struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 308*06f751b6SCorentin Labbe struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq); 309*06f751b6SCorentin Labbe struct crypto_engine *engine; 310*06f751b6SCorentin Labbe int e; 311*06f751b6SCorentin Labbe 312*06f751b6SCorentin Labbe rctx->op_dir = CE_ENCRYPTION; 313*06f751b6SCorentin Labbe if (sun8i_ce_cipher_need_fallback(areq)) 314*06f751b6SCorentin Labbe return sun8i_ce_cipher_fallback(areq); 315*06f751b6SCorentin Labbe 316*06f751b6SCorentin Labbe e = sun8i_ce_get_engine_number(op->ce); 317*06f751b6SCorentin Labbe rctx->flow = e; 318*06f751b6SCorentin Labbe engine = op->ce->chanlist[e].engine; 319*06f751b6SCorentin Labbe 320*06f751b6SCorentin Labbe return crypto_transfer_skcipher_request_to_engine(engine, areq); 321*06f751b6SCorentin Labbe } 322*06f751b6SCorentin Labbe 323*06f751b6SCorentin Labbe int sun8i_ce_cipher_init(struct crypto_tfm *tfm) 324*06f751b6SCorentin Labbe { 325*06f751b6SCorentin Labbe struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm); 326*06f751b6SCorentin Labbe struct sun8i_ce_alg_template *algt; 327*06f751b6SCorentin Labbe const char *name = crypto_tfm_alg_name(tfm); 328*06f751b6SCorentin Labbe struct crypto_skcipher *sktfm = __crypto_skcipher_cast(tfm); 329*06f751b6SCorentin Labbe struct skcipher_alg *alg = crypto_skcipher_alg(sktfm); 330*06f751b6SCorentin Labbe int err; 331*06f751b6SCorentin Labbe 332*06f751b6SCorentin Labbe memset(op, 0, sizeof(struct sun8i_cipher_tfm_ctx)); 333*06f751b6SCorentin Labbe 334*06f751b6SCorentin Labbe algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher); 335*06f751b6SCorentin Labbe op->ce = algt->ce; 336*06f751b6SCorentin Labbe 337*06f751b6SCorentin Labbe sktfm->reqsize = sizeof(struct sun8i_cipher_req_ctx); 338*06f751b6SCorentin Labbe 339*06f751b6SCorentin Labbe op->fallback_tfm = crypto_alloc_sync_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK); 340*06f751b6SCorentin Labbe if (IS_ERR(op->fallback_tfm)) { 341*06f751b6SCorentin Labbe dev_err(op->ce->dev, "ERROR: Cannot allocate fallback for %s %ld\n", 342*06f751b6SCorentin Labbe name, PTR_ERR(op->fallback_tfm)); 343*06f751b6SCorentin Labbe return PTR_ERR(op->fallback_tfm); 344*06f751b6SCorentin Labbe } 345*06f751b6SCorentin Labbe 346*06f751b6SCorentin Labbe dev_info(op->ce->dev, "Fallback for %s is %s\n", 347*06f751b6SCorentin Labbe crypto_tfm_alg_driver_name(&sktfm->base), 348*06f751b6SCorentin Labbe crypto_tfm_alg_driver_name(crypto_skcipher_tfm(&op->fallback_tfm->base))); 349*06f751b6SCorentin Labbe 350*06f751b6SCorentin Labbe op->enginectx.op.do_one_request = sun8i_ce_handle_cipher_request; 351*06f751b6SCorentin Labbe op->enginectx.op.prepare_request = NULL; 352*06f751b6SCorentin Labbe op->enginectx.op.unprepare_request = NULL; 353*06f751b6SCorentin Labbe 354*06f751b6SCorentin Labbe err = pm_runtime_get_sync(op->ce->dev); 355*06f751b6SCorentin Labbe if (err < 0) 356*06f751b6SCorentin Labbe goto error_pm; 357*06f751b6SCorentin Labbe 358*06f751b6SCorentin Labbe return 0; 359*06f751b6SCorentin Labbe error_pm: 360*06f751b6SCorentin Labbe crypto_free_sync_skcipher(op->fallback_tfm); 361*06f751b6SCorentin Labbe return err; 362*06f751b6SCorentin Labbe } 363*06f751b6SCorentin Labbe 364*06f751b6SCorentin Labbe void sun8i_ce_cipher_exit(struct crypto_tfm *tfm) 365*06f751b6SCorentin Labbe { 366*06f751b6SCorentin Labbe struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm); 367*06f751b6SCorentin Labbe 368*06f751b6SCorentin Labbe if (op->key) { 369*06f751b6SCorentin Labbe memzero_explicit(op->key, op->keylen); 370*06f751b6SCorentin Labbe kfree(op->key); 371*06f751b6SCorentin Labbe } 372*06f751b6SCorentin Labbe crypto_free_sync_skcipher(op->fallback_tfm); 373*06f751b6SCorentin Labbe pm_runtime_put_sync_suspend(op->ce->dev); 374*06f751b6SCorentin Labbe } 375*06f751b6SCorentin Labbe 376*06f751b6SCorentin Labbe int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, 377*06f751b6SCorentin Labbe unsigned int keylen) 378*06f751b6SCorentin Labbe { 379*06f751b6SCorentin Labbe struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 380*06f751b6SCorentin Labbe struct sun8i_ce_dev *ce = op->ce; 381*06f751b6SCorentin Labbe 382*06f751b6SCorentin Labbe switch (keylen) { 383*06f751b6SCorentin Labbe case 128 / 8: 384*06f751b6SCorentin Labbe break; 385*06f751b6SCorentin Labbe case 192 / 8: 386*06f751b6SCorentin Labbe break; 387*06f751b6SCorentin Labbe case 256 / 8: 388*06f751b6SCorentin Labbe break; 389*06f751b6SCorentin Labbe default: 390*06f751b6SCorentin Labbe dev_dbg(ce->dev, "ERROR: Invalid keylen %u\n", keylen); 391*06f751b6SCorentin Labbe crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); 392*06f751b6SCorentin Labbe return -EINVAL; 393*06f751b6SCorentin Labbe } 394*06f751b6SCorentin Labbe if (op->key) { 395*06f751b6SCorentin Labbe memzero_explicit(op->key, op->keylen); 396*06f751b6SCorentin Labbe kfree(op->key); 397*06f751b6SCorentin Labbe } 398*06f751b6SCorentin Labbe op->keylen = keylen; 399*06f751b6SCorentin Labbe op->key = kmalloc(keylen, GFP_KERNEL | GFP_DMA); 400*06f751b6SCorentin Labbe if (!op->key) 401*06f751b6SCorentin Labbe return -ENOMEM; 402*06f751b6SCorentin Labbe memcpy(op->key, key, keylen); 403*06f751b6SCorentin Labbe 404*06f751b6SCorentin Labbe crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK); 405*06f751b6SCorentin Labbe crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK); 406*06f751b6SCorentin Labbe 407*06f751b6SCorentin Labbe return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen); 408*06f751b6SCorentin Labbe } 409*06f751b6SCorentin Labbe 410*06f751b6SCorentin Labbe int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key, 411*06f751b6SCorentin Labbe unsigned int keylen) 412*06f751b6SCorentin Labbe { 413*06f751b6SCorentin Labbe struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); 414*06f751b6SCorentin Labbe int err; 415*06f751b6SCorentin Labbe 416*06f751b6SCorentin Labbe err = verify_skcipher_des3_key(tfm, key); 417*06f751b6SCorentin Labbe if (err) 418*06f751b6SCorentin Labbe return err; 419*06f751b6SCorentin Labbe 420*06f751b6SCorentin Labbe if (op->key) { 421*06f751b6SCorentin Labbe memzero_explicit(op->key, op->keylen); 422*06f751b6SCorentin Labbe kfree(op->key); 423*06f751b6SCorentin Labbe } 424*06f751b6SCorentin Labbe op->keylen = keylen; 425*06f751b6SCorentin Labbe op->key = kmalloc(keylen, GFP_KERNEL | GFP_DMA); 426*06f751b6SCorentin Labbe if (!op->key) 427*06f751b6SCorentin Labbe return -ENOMEM; 428*06f751b6SCorentin Labbe memcpy(op->key, key, keylen); 429*06f751b6SCorentin Labbe 430*06f751b6SCorentin Labbe crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK); 431*06f751b6SCorentin Labbe crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK); 432*06f751b6SCorentin Labbe 433*06f751b6SCorentin Labbe return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen); 434*06f751b6SCorentin Labbe } 435