Path: blob/master/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
26292 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* sun4i-ss-cipher.c - hardware cryptographic accelerator for Allwinner A20 SoC3*4* Copyright (C) 2013-2015 Corentin LABBE <[email protected]>5*6* This file add support for AES cipher with 128,192,256 bits7* keysize in CBC and ECB mode.8* Add support also for DES and 3DES in CBC and ECB mode.9*10* You could find the datasheet in Documentation/arch/arm/sunxi.rst11*/12#include "sun4i-ss.h"1314static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)15{16struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);17struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);18struct sun4i_ss_ctx *ss = op->ss;19unsigned int ivsize = crypto_skcipher_ivsize(tfm);20struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);21u32 mode = ctx->mode;22/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */23u32 rx_cnt = SS_RX_DEFAULT;24u32 tx_cnt = 0;25u32 spaces;26u32 v;27int err = 0;28unsigned int i;29unsigned int ileft = areq->cryptlen;30unsigned int oleft = areq->cryptlen;31unsigned int todo;32unsigned long pi = 0, po = 0; /* progress for in and out */33bool miter_err;34struct sg_mapping_iter mi, mo;35unsigned int oi, oo; /* offset for in and out */36unsigned long flags;37struct skcipher_alg *alg = crypto_skcipher_alg(tfm);38struct sun4i_ss_alg_template *algt;3940if (!areq->cryptlen)41return 0;4243if (!areq->src || !areq->dst) {44dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");45return -EINVAL;46}4748if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {49scatterwalk_map_and_copy(ctx->backup_iv, areq->src,50areq->cryptlen - ivsize, ivsize, 0);51}5253if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {54algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);55algt->stat_opti++;56algt->stat_bytes += areq->cryptlen;57}5859spin_lock_irqsave(&ss->slock, flags);6061for (i = 0; i < op->keylen / 4; i++)62writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);6364if (areq->iv) {65for (i = 0; i < 4 && i < ivsize / 4; i++) {66v = *(u32 *)(areq->iv + i * 4);67writesl(ss->base + SS_IV0 + i * 4, &v, 1);68}69}70writel(mode, ss->base + SS_CTL);717273ileft = areq->cryptlen / 4;74oleft = areq->cryptlen / 4;75oi = 0;76oo = 0;77do {78if (ileft) {79sg_miter_start(&mi, areq->src, sg_nents(areq->src),80SG_MITER_FROM_SG | SG_MITER_ATOMIC);81if (pi)82sg_miter_skip(&mi, pi);83miter_err = sg_miter_next(&mi);84if (!miter_err || !mi.addr) {85dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");86err = -EINVAL;87goto release_ss;88}89todo = min(rx_cnt, ileft);90todo = min_t(size_t, todo, (mi.length - oi) / 4);91if (todo) {92ileft -= todo;93writesl(ss->base + SS_RXFIFO, mi.addr + oi, todo);94oi += todo * 4;95}96if (oi == mi.length) {97pi += mi.length;98oi = 0;99}100sg_miter_stop(&mi);101}102103spaces = readl(ss->base + SS_FCSR);104rx_cnt = SS_RXFIFO_SPACES(spaces);105tx_cnt = SS_TXFIFO_SPACES(spaces);106107sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),108SG_MITER_TO_SG | SG_MITER_ATOMIC);109if (po)110sg_miter_skip(&mo, po);111miter_err = sg_miter_next(&mo);112if (!miter_err || !mo.addr) {113dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");114err = -EINVAL;115goto release_ss;116}117todo = min(tx_cnt, oleft);118todo = min_t(size_t, todo, (mo.length - oo) / 4);119if (todo) {120oleft -= todo;121readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);122oo += todo * 4;123}124if (oo == mo.length) {125oo = 0;126po += mo.length;127}128sg_miter_stop(&mo);129} while (oleft);130131if (areq->iv) {132if (mode & SS_DECRYPTION) {133memcpy(areq->iv, ctx->backup_iv, ivsize);134memzero_explicit(ctx->backup_iv, ivsize);135} else {136scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,137ivsize, 0);138}139}140141release_ss:142writel(0, ss->base + SS_CTL);143spin_unlock_irqrestore(&ss->slock, flags);144return err;145}146147static int noinline_for_stack sun4i_ss_cipher_poll_fallback(struct skcipher_request *areq)148{149struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);150struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);151struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);152int err;153struct skcipher_alg *alg = crypto_skcipher_alg(tfm);154struct sun4i_ss_alg_template *algt;155156if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {157algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);158algt->stat_fb++;159}160161skcipher_request_set_tfm(&ctx->fallback_req, op->fallback_tfm);162skcipher_request_set_callback(&ctx->fallback_req, areq->base.flags,163areq->base.complete, areq->base.data);164skcipher_request_set_crypt(&ctx->fallback_req, areq->src, areq->dst,165areq->cryptlen, areq->iv);166if (ctx->mode & SS_DECRYPTION)167err = crypto_skcipher_decrypt(&ctx->fallback_req);168else169err = crypto_skcipher_encrypt(&ctx->fallback_req);170171return err;172}173174/* Generic function that support SG with size not multiple of 4 */175static int sun4i_ss_cipher_poll(struct skcipher_request *areq)176{177struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);178struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);179struct sun4i_ss_ctx *ss = op->ss;180int no_chunk = 1;181struct scatterlist *in_sg = areq->src;182struct scatterlist *out_sg = areq->dst;183unsigned int ivsize = crypto_skcipher_ivsize(tfm);184struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);185struct skcipher_alg *alg = crypto_skcipher_alg(tfm);186struct sun4i_ss_alg_template *algt;187u32 mode = ctx->mode;188/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */189u32 rx_cnt = SS_RX_DEFAULT;190u32 tx_cnt = 0;191u32 v;192u32 spaces;193int err = 0;194unsigned int i;195unsigned int ileft = areq->cryptlen;196unsigned int oleft = areq->cryptlen;197unsigned int todo;198struct sg_mapping_iter mi, mo;199unsigned long pi = 0, po = 0; /* progress for in and out */200bool miter_err;201unsigned int oi, oo; /* offset for in and out */202unsigned int ob = 0; /* offset in buf */203unsigned int obo = 0; /* offset in bufo*/204unsigned int obl = 0; /* length of data in bufo */205unsigned long flags;206bool need_fallback = false;207208if (!areq->cryptlen)209return 0;210211if (!areq->src || !areq->dst) {212dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");213return -EINVAL;214}215216algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);217if (areq->cryptlen % algt->alg.crypto.base.cra_blocksize)218need_fallback = true;219220/*221* if we have only SGs with size multiple of 4,222* we can use the SS optimized function223*/224while (in_sg && no_chunk == 1) {225if ((in_sg->length | in_sg->offset) & 3u)226no_chunk = 0;227in_sg = sg_next(in_sg);228}229while (out_sg && no_chunk == 1) {230if ((out_sg->length | out_sg->offset) & 3u)231no_chunk = 0;232out_sg = sg_next(out_sg);233}234235if (no_chunk == 1 && !need_fallback)236return sun4i_ss_opti_poll(areq);237238if (need_fallback)239return sun4i_ss_cipher_poll_fallback(areq);240241if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {242scatterwalk_map_and_copy(ctx->backup_iv, areq->src,243areq->cryptlen - ivsize, ivsize, 0);244}245246if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {247algt->stat_req++;248algt->stat_bytes += areq->cryptlen;249}250251spin_lock_irqsave(&ss->slock, flags);252253for (i = 0; i < op->keylen / 4; i++)254writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);255256if (areq->iv) {257for (i = 0; i < 4 && i < ivsize / 4; i++) {258v = *(u32 *)(areq->iv + i * 4);259writesl(ss->base + SS_IV0 + i * 4, &v, 1);260}261}262writel(mode, ss->base + SS_CTL);263264ileft = areq->cryptlen;265oleft = areq->cryptlen;266oi = 0;267oo = 0;268269while (oleft) {270if (ileft) {271sg_miter_start(&mi, areq->src, sg_nents(areq->src),272SG_MITER_FROM_SG | SG_MITER_ATOMIC);273if (pi)274sg_miter_skip(&mi, pi);275miter_err = sg_miter_next(&mi);276if (!miter_err || !mi.addr) {277dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");278err = -EINVAL;279goto release_ss;280}281/*282* todo is the number of consecutive 4byte word that we283* can read from current SG284*/285todo = min(rx_cnt, ileft / 4);286todo = min_t(size_t, todo, (mi.length - oi) / 4);287if (todo && !ob) {288writesl(ss->base + SS_RXFIFO, mi.addr + oi,289todo);290ileft -= todo * 4;291oi += todo * 4;292} else {293/*294* not enough consecutive bytes, so we need to295* linearize in buf. todo is in bytes296* After that copy, if we have a multiple of 4297* we need to be able to write all buf in one298* pass, so it is why we min() with rx_cnt299*/300todo = min(rx_cnt * 4 - ob, ileft);301todo = min_t(size_t, todo, mi.length - oi);302memcpy(ss->buf + ob, mi.addr + oi, todo);303ileft -= todo;304oi += todo;305ob += todo;306if (!(ob % 4)) {307writesl(ss->base + SS_RXFIFO, ss->buf,308ob / 4);309ob = 0;310}311}312if (oi == mi.length) {313pi += mi.length;314oi = 0;315}316sg_miter_stop(&mi);317}318319spaces = readl(ss->base + SS_FCSR);320rx_cnt = SS_RXFIFO_SPACES(spaces);321tx_cnt = SS_TXFIFO_SPACES(spaces);322323if (!tx_cnt)324continue;325sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),326SG_MITER_TO_SG | SG_MITER_ATOMIC);327if (po)328sg_miter_skip(&mo, po);329miter_err = sg_miter_next(&mo);330if (!miter_err || !mo.addr) {331dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");332err = -EINVAL;333goto release_ss;334}335/* todo in 4bytes word */336todo = min(tx_cnt, oleft / 4);337todo = min_t(size_t, todo, (mo.length - oo) / 4);338339if (todo) {340readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);341oleft -= todo * 4;342oo += todo * 4;343if (oo == mo.length) {344po += mo.length;345oo = 0;346}347} else {348/*349* read obl bytes in bufo, we read at maximum for350* emptying the device351*/352readsl(ss->base + SS_TXFIFO, ss->bufo, tx_cnt);353obl = tx_cnt * 4;354obo = 0;355do {356/*357* how many bytes we can copy ?358* no more than remaining SG size359* no more than remaining buffer360* no need to test against oleft361*/362todo = min_t(size_t,363mo.length - oo, obl - obo);364memcpy(mo.addr + oo, ss->bufo + obo, todo);365oleft -= todo;366obo += todo;367oo += todo;368if (oo == mo.length) {369po += mo.length;370sg_miter_next(&mo);371oo = 0;372}373} while (obo < obl);374/* bufo must be fully used here */375}376sg_miter_stop(&mo);377}378if (areq->iv) {379if (mode & SS_DECRYPTION) {380memcpy(areq->iv, ctx->backup_iv, ivsize);381memzero_explicit(ctx->backup_iv, ivsize);382} else {383scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,384ivsize, 0);385}386}387388release_ss:389writel(0, ss->base + SS_CTL);390spin_unlock_irqrestore(&ss->slock, flags);391392return err;393}394395/* CBC AES */396int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq)397{398struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);399struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);400struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);401402rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |403op->keymode;404return sun4i_ss_cipher_poll(areq);405}406407int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq)408{409struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);410struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);411struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);412413rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_DECRYPTION |414op->keymode;415return sun4i_ss_cipher_poll(areq);416}417418/* ECB AES */419int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq)420{421struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);422struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);423struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);424425rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |426op->keymode;427return sun4i_ss_cipher_poll(areq);428}429430int sun4i_ss_ecb_aes_decrypt(struct skcipher_request *areq)431{432struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);433struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);434struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);435436rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_DECRYPTION |437op->keymode;438return sun4i_ss_cipher_poll(areq);439}440441/* CBC DES */442int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq)443{444struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);445struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);446struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);447448rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |449op->keymode;450return sun4i_ss_cipher_poll(areq);451}452453int sun4i_ss_cbc_des_decrypt(struct skcipher_request *areq)454{455struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);456struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);457struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);458459rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |460op->keymode;461return sun4i_ss_cipher_poll(areq);462}463464/* ECB DES */465int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq)466{467struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);468struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);469struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);470471rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |472op->keymode;473return sun4i_ss_cipher_poll(areq);474}475476int sun4i_ss_ecb_des_decrypt(struct skcipher_request *areq)477{478struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);479struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);480struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);481482rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |483op->keymode;484return sun4i_ss_cipher_poll(areq);485}486487/* CBC 3DES */488int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq)489{490struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);491struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);492struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);493494rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |495op->keymode;496return sun4i_ss_cipher_poll(areq);497}498499int sun4i_ss_cbc_des3_decrypt(struct skcipher_request *areq)500{501struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);502struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);503struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);504505rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |506op->keymode;507return sun4i_ss_cipher_poll(areq);508}509510/* ECB 3DES */511int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq)512{513struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);514struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);515struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);516517rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |518op->keymode;519return sun4i_ss_cipher_poll(areq);520}521522int sun4i_ss_ecb_des3_decrypt(struct skcipher_request *areq)523{524struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);525struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);526struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);527528rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |529op->keymode;530return sun4i_ss_cipher_poll(areq);531}532533int sun4i_ss_cipher_init(struct crypto_tfm *tfm)534{535struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);536struct sun4i_ss_alg_template *algt;537const char *name = crypto_tfm_alg_name(tfm);538int err;539540memset(op, 0, sizeof(struct sun4i_tfm_ctx));541542algt = container_of(tfm->__crt_alg, struct sun4i_ss_alg_template,543alg.crypto.base);544op->ss = algt->ss;545546op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);547if (IS_ERR(op->fallback_tfm)) {548dev_err(op->ss->dev, "ERROR: Cannot allocate fallback for %s %ld\n",549name, PTR_ERR(op->fallback_tfm));550return PTR_ERR(op->fallback_tfm);551}552553crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),554sizeof(struct sun4i_cipher_req_ctx) +555crypto_skcipher_reqsize(op->fallback_tfm));556557err = pm_runtime_resume_and_get(op->ss->dev);558if (err < 0)559goto error_pm;560561return 0;562error_pm:563crypto_free_skcipher(op->fallback_tfm);564return err;565}566567void sun4i_ss_cipher_exit(struct crypto_tfm *tfm)568{569struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);570571crypto_free_skcipher(op->fallback_tfm);572pm_runtime_put(op->ss->dev);573}574575/* check and set the AES key, prepare the mode to be used */576int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,577unsigned int keylen)578{579struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);580struct sun4i_ss_ctx *ss = op->ss;581582switch (keylen) {583case 128 / 8:584op->keymode = SS_AES_128BITS;585break;586case 192 / 8:587op->keymode = SS_AES_192BITS;588break;589case 256 / 8:590op->keymode = SS_AES_256BITS;591break;592default:593dev_dbg(ss->dev, "ERROR: Invalid keylen %u\n", keylen);594return -EINVAL;595}596op->keylen = keylen;597memcpy(op->key, key, keylen);598599crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);600crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);601602return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);603}604605/* check and set the DES key, prepare the mode to be used */606int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,607unsigned int keylen)608{609struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);610int err;611612err = verify_skcipher_des_key(tfm, key);613if (err)614return err;615616op->keylen = keylen;617memcpy(op->key, key, keylen);618619crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);620crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);621622return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);623}624625/* check and set the 3DES key, prepare the mode to be used */626int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,627unsigned int keylen)628{629struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);630int err;631632err = verify_skcipher_des3_key(tfm, key);633if (err)634return err;635636op->keylen = keylen;637memcpy(op->key, key, keylen);638639crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);640crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);641642return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);643}644645646