123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907 |
- /*
- * Cryptographic API.
- *
- * Support for ATMEL SHA1/SHA256 HW acceleration.
- *
- * Copyright (c) 2012 Eukréa Electromatique - ATMEL
- * Author: Nicolas Royer <nicolas@eukrea.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- *
- * Some ideas are from omap-sham.c drivers.
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/slab.h>
- #include <linux/err.h>
- #include <linux/clk.h>
- #include <linux/io.h>
- #include <linux/hw_random.h>
- #include <linux/platform_device.h>
- #include <linux/device.h>
- #include <linux/init.h>
- #include <linux/errno.h>
- #include <linux/interrupt.h>
- #include <linux/irq.h>
- #include <linux/scatterlist.h>
- #include <linux/dma-mapping.h>
- #include <linux/of_device.h>
- #include <linux/delay.h>
- #include <linux/crypto.h>
- #include <linux/cryptohash.h>
- #include <crypto/scatterwalk.h>
- #include <crypto/algapi.h>
- #include <crypto/sha.h>
- #include <crypto/hash.h>
- #include <crypto/internal/hash.h>
- #include <linux/platform_data/crypto-atmel.h>
- #include "atmel-sha-regs.h"
- #include "atmel-authenc.h"
- /* SHA flags */
- #define SHA_FLAGS_BUSY BIT(0)
- #define SHA_FLAGS_FINAL BIT(1)
- #define SHA_FLAGS_DMA_ACTIVE BIT(2)
- #define SHA_FLAGS_OUTPUT_READY BIT(3)
- #define SHA_FLAGS_INIT BIT(4)
- #define SHA_FLAGS_CPU BIT(5)
- #define SHA_FLAGS_DMA_READY BIT(6)
- #define SHA_FLAGS_DUMP_REG BIT(7)
- /* bits[11:8] are reserved. */
- #define SHA_FLAGS_FINUP BIT(16)
- #define SHA_FLAGS_SG BIT(17)
- #define SHA_FLAGS_ERROR BIT(23)
- #define SHA_FLAGS_PAD BIT(24)
- #define SHA_FLAGS_RESTORE BIT(25)
- #define SHA_FLAGS_IDATAR0 BIT(26)
- #define SHA_FLAGS_WAIT_DATARDY BIT(27)
- #define SHA_OP_INIT 0
- #define SHA_OP_UPDATE 1
- #define SHA_OP_FINAL 2
- #define SHA_OP_DIGEST 3
- #define SHA_BUFFER_LEN (PAGE_SIZE / 16)
- #define ATMEL_SHA_DMA_THRESHOLD 56
- struct atmel_sha_caps {
- bool has_dma;
- bool has_dualbuff;
- bool has_sha224;
- bool has_sha_384_512;
- bool has_uihv;
- bool has_hmac;
- };
- struct atmel_sha_dev;
- /*
- * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
- * tested by the ahash_prepare_alg() function.
- */
- struct atmel_sha_reqctx {
- struct atmel_sha_dev *dd;
- unsigned long flags;
- unsigned long op;
- u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
- u64 digcnt[2];
- size_t bufcnt;
- size_t buflen;
- dma_addr_t dma_addr;
- /* walk state */
- struct scatterlist *sg;
- unsigned int offset; /* offset in current sg */
- unsigned int total; /* total request */
- size_t block_size;
- size_t hash_size;
- u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
- };
- typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *);
- struct atmel_sha_ctx {
- struct atmel_sha_dev *dd;
- atmel_sha_fn_t start;
- unsigned long flags;
- };
- #define ATMEL_SHA_QUEUE_LENGTH 50
- struct atmel_sha_dma {
- struct dma_chan *chan;
- struct dma_slave_config dma_conf;
- struct scatterlist *sg;
- int nents;
- unsigned int last_sg_length;
- };
- struct atmel_sha_dev {
- struct list_head list;
- unsigned long phys_base;
- struct device *dev;
- struct clk *iclk;
- int irq;
- void __iomem *io_base;
- spinlock_t lock;
- int err;
- struct tasklet_struct done_task;
- struct tasklet_struct queue_task;
- unsigned long flags;
- struct crypto_queue queue;
- struct ahash_request *req;
- bool is_async;
- bool force_complete;
- atmel_sha_fn_t resume;
- atmel_sha_fn_t cpu_transfer_complete;
- struct atmel_sha_dma dma_lch_in;
- struct atmel_sha_caps caps;
- struct scatterlist tmp;
- u32 hw_version;
- };
- struct atmel_sha_drv {
- struct list_head dev_list;
- spinlock_t lock;
- };
- static struct atmel_sha_drv atmel_sha = {
- .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
- .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
- };
- #ifdef VERBOSE_DEBUG
- static const char *atmel_sha_reg_name(u32 offset, char *tmp, size_t sz, bool wr)
- {
- switch (offset) {
- case SHA_CR:
- return "CR";
- case SHA_MR:
- return "MR";
- case SHA_IER:
- return "IER";
- case SHA_IDR:
- return "IDR";
- case SHA_IMR:
- return "IMR";
- case SHA_ISR:
- return "ISR";
- case SHA_MSR:
- return "MSR";
- case SHA_BCR:
- return "BCR";
- case SHA_REG_DIN(0):
- case SHA_REG_DIN(1):
- case SHA_REG_DIN(2):
- case SHA_REG_DIN(3):
- case SHA_REG_DIN(4):
- case SHA_REG_DIN(5):
- case SHA_REG_DIN(6):
- case SHA_REG_DIN(7):
- case SHA_REG_DIN(8):
- case SHA_REG_DIN(9):
- case SHA_REG_DIN(10):
- case SHA_REG_DIN(11):
- case SHA_REG_DIN(12):
- case SHA_REG_DIN(13):
- case SHA_REG_DIN(14):
- case SHA_REG_DIN(15):
- snprintf(tmp, sz, "IDATAR[%u]", (offset - SHA_REG_DIN(0)) >> 2);
- break;
- case SHA_REG_DIGEST(0):
- case SHA_REG_DIGEST(1):
- case SHA_REG_DIGEST(2):
- case SHA_REG_DIGEST(3):
- case SHA_REG_DIGEST(4):
- case SHA_REG_DIGEST(5):
- case SHA_REG_DIGEST(6):
- case SHA_REG_DIGEST(7):
- case SHA_REG_DIGEST(8):
- case SHA_REG_DIGEST(9):
- case SHA_REG_DIGEST(10):
- case SHA_REG_DIGEST(11):
- case SHA_REG_DIGEST(12):
- case SHA_REG_DIGEST(13):
- case SHA_REG_DIGEST(14):
- case SHA_REG_DIGEST(15):
- if (wr)
- snprintf(tmp, sz, "IDATAR[%u]",
- 16u + ((offset - SHA_REG_DIGEST(0)) >> 2));
- else
- snprintf(tmp, sz, "ODATAR[%u]",
- (offset - SHA_REG_DIGEST(0)) >> 2);
- break;
- case SHA_HW_VERSION:
- return "HWVER";
- default:
- snprintf(tmp, sz, "0x%02x", offset);
- break;
- }
- return tmp;
- }
- #endif /* VERBOSE_DEBUG */
- static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
- {
- u32 value = readl_relaxed(dd->io_base + offset);
- #ifdef VERBOSE_DEBUG
- if (dd->flags & SHA_FLAGS_DUMP_REG) {
- char tmp[16];
- dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
- atmel_sha_reg_name(offset, tmp, sizeof(tmp), false));
- }
- #endif /* VERBOSE_DEBUG */
- return value;
- }
- static inline void atmel_sha_write(struct atmel_sha_dev *dd,
- u32 offset, u32 value)
- {
- #ifdef VERBOSE_DEBUG
- if (dd->flags & SHA_FLAGS_DUMP_REG) {
- char tmp[16];
- dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
- atmel_sha_reg_name(offset, tmp, sizeof(tmp), true));
- }
- #endif /* VERBOSE_DEBUG */
- writel_relaxed(value, dd->io_base + offset);
- }
- static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err)
- {
- struct ahash_request *req = dd->req;
- dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
- SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY |
- SHA_FLAGS_DUMP_REG);
- clk_disable(dd->iclk);
- if ((dd->is_async || dd->force_complete) && req->base.complete)
- req->base.complete(&req->base, err);
- /* handle new request */
- tasklet_schedule(&dd->queue_task);
- return err;
- }
- static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
- {
- size_t count;
- while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
- count = min(ctx->sg->length - ctx->offset, ctx->total);
- count = min(count, ctx->buflen - ctx->bufcnt);
- if (count <= 0) {
- /*
- * Check if count <= 0 because the buffer is full or
- * because the sg length is 0. In the latest case,
- * check if there is another sg in the list, a 0 length
- * sg doesn't necessarily mean the end of the sg list.
- */
- if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
- ctx->sg = sg_next(ctx->sg);
- continue;
- } else {
- break;
- }
- }
- scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
- ctx->offset, count, 0);
- ctx->bufcnt += count;
- ctx->offset += count;
- ctx->total -= count;
- if (ctx->offset == ctx->sg->length) {
- ctx->sg = sg_next(ctx->sg);
- if (ctx->sg)
- ctx->offset = 0;
- else
- ctx->total = 0;
- }
- }
- return 0;
- }
- /*
- * The purpose of this padding is to ensure that the padded message is a
- * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
- * The bit "1" is appended at the end of the message followed by
- * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
- * 128 bits block (SHA384/SHA512) equals to the message length in bits
- * is appended.
- *
- * For SHA1/SHA224/SHA256, padlen is calculated as followed:
- * - if message length < 56 bytes then padlen = 56 - message length
- * - else padlen = 64 + 56 - message length
- *
- * For SHA384/SHA512, padlen is calculated as followed:
- * - if message length < 112 bytes then padlen = 112 - message length
- * - else padlen = 128 + 112 - message length
- */
- static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
- {
- unsigned int index, padlen;
- u64 bits[2];
- u64 size[2];
- size[0] = ctx->digcnt[0];
- size[1] = ctx->digcnt[1];
- size[0] += ctx->bufcnt;
- if (size[0] < ctx->bufcnt)
- size[1]++;
- size[0] += length;
- if (size[0] < length)
- size[1]++;
- bits[1] = cpu_to_be64(size[0] << 3);
- bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
- switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
- case SHA_FLAGS_SHA384:
- case SHA_FLAGS_SHA512:
- index = ctx->bufcnt & 0x7f;
- padlen = (index < 112) ? (112 - index) : ((128+112) - index);
- *(ctx->buffer + ctx->bufcnt) = 0x80;
- memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
- memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
- ctx->bufcnt += padlen + 16;
- ctx->flags |= SHA_FLAGS_PAD;
- break;
- default:
- index = ctx->bufcnt & 0x3f;
- padlen = (index < 56) ? (56 - index) : ((64+56) - index);
- *(ctx->buffer + ctx->bufcnt) = 0x80;
- memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
- memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
- ctx->bufcnt += padlen + 8;
- ctx->flags |= SHA_FLAGS_PAD;
- break;
- }
- }
- static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx)
- {
- struct atmel_sha_dev *dd = NULL;
- struct atmel_sha_dev *tmp;
- spin_lock_bh(&atmel_sha.lock);
- if (!tctx->dd) {
- list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
- dd = tmp;
- break;
- }
- tctx->dd = dd;
- } else {
- dd = tctx->dd;
- }
- spin_unlock_bh(&atmel_sha.lock);
- return dd;
- }
- static int atmel_sha_init(struct ahash_request *req)
- {
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx);
- ctx->dd = dd;
- ctx->flags = 0;
- dev_dbg(dd->dev, "init: digest size: %d\n",
- crypto_ahash_digestsize(tfm));
- switch (crypto_ahash_digestsize(tfm)) {
- case SHA1_DIGEST_SIZE:
- ctx->flags |= SHA_FLAGS_SHA1;
- ctx->block_size = SHA1_BLOCK_SIZE;
- break;
- case SHA224_DIGEST_SIZE:
- ctx->flags |= SHA_FLAGS_SHA224;
- ctx->block_size = SHA224_BLOCK_SIZE;
- break;
- case SHA256_DIGEST_SIZE:
- ctx->flags |= SHA_FLAGS_SHA256;
- ctx->block_size = SHA256_BLOCK_SIZE;
- break;
- case SHA384_DIGEST_SIZE:
- ctx->flags |= SHA_FLAGS_SHA384;
- ctx->block_size = SHA384_BLOCK_SIZE;
- break;
- case SHA512_DIGEST_SIZE:
- ctx->flags |= SHA_FLAGS_SHA512;
- ctx->block_size = SHA512_BLOCK_SIZE;
- break;
- default:
- return -EINVAL;
- break;
- }
- ctx->bufcnt = 0;
- ctx->digcnt[0] = 0;
- ctx->digcnt[1] = 0;
- ctx->buflen = SHA_BUFFER_LEN;
- return 0;
- }
- static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
- u32 valmr = SHA_MR_MODE_AUTO;
- unsigned int i, hashsize = 0;
- if (likely(dma)) {
- if (!dd->caps.has_dma)
- atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
- valmr = SHA_MR_MODE_PDC;
- if (dd->caps.has_dualbuff)
- valmr |= SHA_MR_DUALBUFF;
- } else {
- atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
- }
- switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
- case SHA_FLAGS_SHA1:
- valmr |= SHA_MR_ALGO_SHA1;
- hashsize = SHA1_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA224:
- valmr |= SHA_MR_ALGO_SHA224;
- hashsize = SHA256_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA256:
- valmr |= SHA_MR_ALGO_SHA256;
- hashsize = SHA256_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA384:
- valmr |= SHA_MR_ALGO_SHA384;
- hashsize = SHA512_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA512:
- valmr |= SHA_MR_ALGO_SHA512;
- hashsize = SHA512_DIGEST_SIZE;
- break;
- default:
- break;
- }
- /* Setting CR_FIRST only for the first iteration */
- if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
- atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
- } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
- const u32 *hash = (const u32 *)ctx->digest;
- /*
- * Restore the hardware context: update the User Initialize
- * Hash Value (UIHV) with the value saved when the latest
- * 'update' operation completed on this very same crypto
- * request.
- */
- ctx->flags &= ~SHA_FLAGS_RESTORE;
- atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
- for (i = 0; i < hashsize / sizeof(u32); ++i)
- atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
- atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
- valmr |= SHA_MR_UIHV;
- }
- /*
- * WARNING: If the UIHV feature is not available, the hardware CANNOT
- * process concurrent requests: the internal registers used to store
- * the hash/digest are still set to the partial digest output values
- * computed during the latest round.
- */
- atmel_sha_write(dd, SHA_MR, valmr);
- }
- static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
- atmel_sha_fn_t resume)
- {
- u32 isr = atmel_sha_read(dd, SHA_ISR);
- if (unlikely(isr & SHA_INT_DATARDY))
- return resume(dd);
- dd->resume = resume;
- atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
- return -EINPROGRESS;
- }
- static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
- size_t length, int final)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
- int count, len32;
- const u32 *buffer = (const u32 *)buf;
- dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
- ctx->digcnt[1], ctx->digcnt[0], length, final);
- atmel_sha_write_ctrl(dd, 0);
- /* should be non-zero before next lines to disable clocks later */
- ctx->digcnt[0] += length;
- if (ctx->digcnt[0] < length)
- ctx->digcnt[1]++;
- if (final)
- dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
- len32 = DIV_ROUND_UP(length, sizeof(u32));
- dd->flags |= SHA_FLAGS_CPU;
- for (count = 0; count < len32; count++)
- atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
- return -EINPROGRESS;
- }
- static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
- size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
- int len32;
- dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
- ctx->digcnt[1], ctx->digcnt[0], length1, final);
- len32 = DIV_ROUND_UP(length1, sizeof(u32));
- atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
- atmel_sha_write(dd, SHA_TPR, dma_addr1);
- atmel_sha_write(dd, SHA_TCR, len32);
- len32 = DIV_ROUND_UP(length2, sizeof(u32));
- atmel_sha_write(dd, SHA_TNPR, dma_addr2);
- atmel_sha_write(dd, SHA_TNCR, len32);
- atmel_sha_write_ctrl(dd, 1);
- /* should be non-zero before next lines to disable clocks later */
- ctx->digcnt[0] += length1;
- if (ctx->digcnt[0] < length1)
- ctx->digcnt[1]++;
- if (final)
- dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
- dd->flags |= SHA_FLAGS_DMA_ACTIVE;
- /* Start DMA transfer */
- atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
- return -EINPROGRESS;
- }
- static void atmel_sha_dma_callback(void *data)
- {
- struct atmel_sha_dev *dd = data;
- dd->is_async = true;
- /* dma_lch_in - completed - wait DATRDY */
- atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
- }
- static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
- size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
- struct dma_async_tx_descriptor *in_desc;
- struct scatterlist sg[2];
- dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
- ctx->digcnt[1], ctx->digcnt[0], length1, final);
- dd->dma_lch_in.dma_conf.src_maxburst = 16;
- dd->dma_lch_in.dma_conf.dst_maxburst = 16;
- dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
- if (length2) {
- sg_init_table(sg, 2);
- sg_dma_address(&sg[0]) = dma_addr1;
- sg_dma_len(&sg[0]) = length1;
- sg_dma_address(&sg[1]) = dma_addr2;
- sg_dma_len(&sg[1]) = length2;
- in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
- DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- } else {
- sg_init_table(sg, 1);
- sg_dma_address(&sg[0]) = dma_addr1;
- sg_dma_len(&sg[0]) = length1;
- in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
- DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- }
- if (!in_desc)
- return atmel_sha_complete(dd, -EINVAL);
- in_desc->callback = atmel_sha_dma_callback;
- in_desc->callback_param = dd;
- atmel_sha_write_ctrl(dd, 1);
- /* should be non-zero before next lines to disable clocks later */
- ctx->digcnt[0] += length1;
- if (ctx->digcnt[0] < length1)
- ctx->digcnt[1]++;
- if (final)
- dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
- dd->flags |= SHA_FLAGS_DMA_ACTIVE;
- /* Start DMA transfer */
- dmaengine_submit(in_desc);
- dma_async_issue_pending(dd->dma_lch_in.chan);
- return -EINPROGRESS;
- }
- static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
- size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
- {
- if (dd->caps.has_dma)
- return atmel_sha_xmit_dma(dd, dma_addr1, length1,
- dma_addr2, length2, final);
- else
- return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
- dma_addr2, length2, final);
- }
- static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
- int bufcnt;
- atmel_sha_append_sg(ctx);
- atmel_sha_fill_padding(ctx, 0);
- bufcnt = ctx->bufcnt;
- ctx->bufcnt = 0;
- return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
- }
- static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
- struct atmel_sha_reqctx *ctx,
- size_t length, int final)
- {
- ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
- ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
- if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
- dev_err(dd->dev, "dma %zu bytes error\n", ctx->buflen +
- ctx->block_size);
- return atmel_sha_complete(dd, -EINVAL);
- }
- ctx->flags &= ~SHA_FLAGS_SG;
- /* next call does not fail... so no unmap in the case of error */
- return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
- }
- static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
- unsigned int final;
- size_t count;
- atmel_sha_append_sg(ctx);
- final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
- dev_dbg(dd->dev, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d\n",
- ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
- if (final)
- atmel_sha_fill_padding(ctx, 0);
- if (final || (ctx->bufcnt == ctx->buflen)) {
- count = ctx->bufcnt;
- ctx->bufcnt = 0;
- return atmel_sha_xmit_dma_map(dd, ctx, count, final);
- }
- return 0;
- }
- static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
- unsigned int length, final, tail;
- struct scatterlist *sg;
- unsigned int count;
- if (!ctx->total)
- return 0;
- if (ctx->bufcnt || ctx->offset)
- return atmel_sha_update_dma_slow(dd);
- dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u\n",
- ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
- sg = ctx->sg;
- if (!IS_ALIGNED(sg->offset, sizeof(u32)))
- return atmel_sha_update_dma_slow(dd);
- if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
- /* size is not ctx->block_size aligned */
- return atmel_sha_update_dma_slow(dd);
- length = min(ctx->total, sg->length);
- if (sg_is_last(sg)) {
- if (!(ctx->flags & SHA_FLAGS_FINUP)) {
- /* not last sg must be ctx->block_size aligned */
- tail = length & (ctx->block_size - 1);
- length -= tail;
- }
- }
- ctx->total -= length;
- ctx->offset = length; /* offset where to start slow */
- final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
- /* Add padding */
- if (final) {
- tail = length & (ctx->block_size - 1);
- length -= tail;
- ctx->total += tail;
- ctx->offset = length; /* offset where to start slow */
- sg = ctx->sg;
- atmel_sha_append_sg(ctx);
- atmel_sha_fill_padding(ctx, length);
- ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
- ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
- if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
- dev_err(dd->dev, "dma %zu bytes error\n",
- ctx->buflen + ctx->block_size);
- return atmel_sha_complete(dd, -EINVAL);
- }
- if (length == 0) {
- ctx->flags &= ~SHA_FLAGS_SG;
- count = ctx->bufcnt;
- ctx->bufcnt = 0;
- return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
- 0, final);
- } else {
- ctx->sg = sg;
- if (!dma_map_sg(dd->dev, ctx->sg, 1,
- DMA_TO_DEVICE)) {
- dev_err(dd->dev, "dma_map_sg error\n");
- return atmel_sha_complete(dd, -EINVAL);
- }
- ctx->flags |= SHA_FLAGS_SG;
- count = ctx->bufcnt;
- ctx->bufcnt = 0;
- return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
- length, ctx->dma_addr, count, final);
- }
- }
- if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
- dev_err(dd->dev, "dma_map_sg error\n");
- return atmel_sha_complete(dd, -EINVAL);
- }
- ctx->flags |= SHA_FLAGS_SG;
- /* next call does not fail... so no unmap in the case of error */
- return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
- 0, final);
- }
- static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
- if (ctx->flags & SHA_FLAGS_SG) {
- dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
- if (ctx->sg->length == ctx->offset) {
- ctx->sg = sg_next(ctx->sg);
- if (ctx->sg)
- ctx->offset = 0;
- }
- if (ctx->flags & SHA_FLAGS_PAD) {
- dma_unmap_single(dd->dev, ctx->dma_addr,
- ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
- }
- } else {
- dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
- ctx->block_size, DMA_TO_DEVICE);
- }
- return 0;
- }
- static int atmel_sha_update_req(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- int err;
- dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
- ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
- if (ctx->flags & SHA_FLAGS_CPU)
- err = atmel_sha_update_cpu(dd);
- else
- err = atmel_sha_update_dma_start(dd);
- /* wait for dma completion before can take more data */
- dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
- err, ctx->digcnt[1], ctx->digcnt[0]);
- return err;
- }
- static int atmel_sha_final_req(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- int err = 0;
- int count;
- if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
- atmel_sha_fill_padding(ctx, 0);
- count = ctx->bufcnt;
- ctx->bufcnt = 0;
- err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
- }
- /* faster to handle last block with cpu */
- else {
- atmel_sha_fill_padding(ctx, 0);
- count = ctx->bufcnt;
- ctx->bufcnt = 0;
- err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
- }
- dev_dbg(dd->dev, "final_req: err: %d\n", err);
- return err;
- }
- static void atmel_sha_copy_hash(struct ahash_request *req)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- u32 *hash = (u32 *)ctx->digest;
- unsigned int i, hashsize;
- switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
- case SHA_FLAGS_SHA1:
- hashsize = SHA1_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA224:
- case SHA_FLAGS_SHA256:
- hashsize = SHA256_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA384:
- case SHA_FLAGS_SHA512:
- hashsize = SHA512_DIGEST_SIZE;
- break;
- default:
- /* Should not happen... */
- return;
- }
- for (i = 0; i < hashsize / sizeof(u32); ++i)
- hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
- ctx->flags |= SHA_FLAGS_RESTORE;
- }
- static void atmel_sha_copy_ready_hash(struct ahash_request *req)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- if (!req->result)
- return;
- switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
- default:
- case SHA_FLAGS_SHA1:
- memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
- break;
- case SHA_FLAGS_SHA224:
- memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
- break;
- case SHA_FLAGS_SHA256:
- memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
- break;
- case SHA_FLAGS_SHA384:
- memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
- break;
- case SHA_FLAGS_SHA512:
- memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
- break;
- }
- }
- static int atmel_sha_finish(struct ahash_request *req)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- struct atmel_sha_dev *dd = ctx->dd;
- if (ctx->digcnt[0] || ctx->digcnt[1])
- atmel_sha_copy_ready_hash(req);
- dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx->digcnt[1],
- ctx->digcnt[0], ctx->bufcnt);
- return 0;
- }
- static void atmel_sha_finish_req(struct ahash_request *req, int err)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- struct atmel_sha_dev *dd = ctx->dd;
- if (!err) {
- atmel_sha_copy_hash(req);
- if (SHA_FLAGS_FINAL & dd->flags)
- err = atmel_sha_finish(req);
- } else {
- ctx->flags |= SHA_FLAGS_ERROR;
- }
- /* atomic operation is not needed here */
- (void)atmel_sha_complete(dd, err);
- }
- static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
- {
- int err;
- err = clk_enable(dd->iclk);
- if (err)
- return err;
- if (!(SHA_FLAGS_INIT & dd->flags)) {
- atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
- dd->flags |= SHA_FLAGS_INIT;
- dd->err = 0;
- }
- return 0;
- }
- static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
- {
- return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
- }
- static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
- {
- atmel_sha_hw_init(dd);
- dd->hw_version = atmel_sha_get_version(dd);
- dev_info(dd->dev,
- "version: 0x%x\n", dd->hw_version);
- clk_disable(dd->iclk);
- }
- static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
- struct ahash_request *req)
- {
- struct crypto_async_request *async_req, *backlog;
- struct atmel_sha_ctx *ctx;
- unsigned long flags;
- bool start_async;
- int err = 0, ret = 0;
- spin_lock_irqsave(&dd->lock, flags);
- if (req)
- ret = ahash_enqueue_request(&dd->queue, req);
- if (SHA_FLAGS_BUSY & dd->flags) {
- spin_unlock_irqrestore(&dd->lock, flags);
- return ret;
- }
- backlog = crypto_get_backlog(&dd->queue);
- async_req = crypto_dequeue_request(&dd->queue);
- if (async_req)
- dd->flags |= SHA_FLAGS_BUSY;
- spin_unlock_irqrestore(&dd->lock, flags);
- if (!async_req)
- return ret;
- if (backlog)
- backlog->complete(backlog, -EINPROGRESS);
- ctx = crypto_tfm_ctx(async_req->tfm);
- dd->req = ahash_request_cast(async_req);
- start_async = (dd->req != req);
- dd->is_async = start_async;
- dd->force_complete = false;
- /* WARNING: ctx->start() MAY change dd->is_async. */
- err = ctx->start(dd);
- return (start_async) ? ret : err;
- }
- static int atmel_sha_done(struct atmel_sha_dev *dd);
- static int atmel_sha_start(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- int err;
- dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
- ctx->op, req->nbytes);
- err = atmel_sha_hw_init(dd);
- if (err)
- return atmel_sha_complete(dd, err);
- /*
- * atmel_sha_update_req() and atmel_sha_final_req() can return either:
- * -EINPROGRESS: the hardware is busy and the SHA driver will resume
- * its job later in the done_task.
- * This is the main path.
- *
- * 0: the SHA driver can continue its job then release the hardware
- * later, if needed, with atmel_sha_finish_req().
- * This is the alternate path.
- *
- * < 0: an error has occurred so atmel_sha_complete(dd, err) has already
- * been called, hence the hardware has been released.
- * The SHA driver must stop its job without calling
- * atmel_sha_finish_req(), otherwise atmel_sha_complete() would be
- * called a second time.
- *
- * Please note that currently, atmel_sha_final_req() never returns 0.
- */
- dd->resume = atmel_sha_done;
- if (ctx->op == SHA_OP_UPDATE) {
- err = atmel_sha_update_req(dd);
- if (!err && (ctx->flags & SHA_FLAGS_FINUP))
- /* no final() after finup() */
- err = atmel_sha_final_req(dd);
- } else if (ctx->op == SHA_OP_FINAL) {
- err = atmel_sha_final_req(dd);
- }
- if (!err)
- /* done_task will not finish it, so do it here */
- atmel_sha_finish_req(req, err);
- dev_dbg(dd->dev, "exit, err: %d\n", err);
- return err;
- }
- static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
- struct atmel_sha_dev *dd = tctx->dd;
- ctx->op = op;
- return atmel_sha_handle_queue(dd, req);
- }
- static int atmel_sha_update(struct ahash_request *req)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- if (!req->nbytes)
- return 0;
- ctx->total = req->nbytes;
- ctx->sg = req->src;
- ctx->offset = 0;
- if (ctx->flags & SHA_FLAGS_FINUP) {
- if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
- /* faster to use CPU for short transfers */
- ctx->flags |= SHA_FLAGS_CPU;
- } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
- atmel_sha_append_sg(ctx);
- return 0;
- }
- return atmel_sha_enqueue(req, SHA_OP_UPDATE);
- }
- static int atmel_sha_final(struct ahash_request *req)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- ctx->flags |= SHA_FLAGS_FINUP;
- if (ctx->flags & SHA_FLAGS_ERROR)
- return 0; /* uncompleted hash is not needed */
- if (ctx->flags & SHA_FLAGS_PAD)
- /* copy ready hash (+ finalize hmac) */
- return atmel_sha_finish(req);
- return atmel_sha_enqueue(req, SHA_OP_FINAL);
- }
- static int atmel_sha_finup(struct ahash_request *req)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- int err1, err2;
- ctx->flags |= SHA_FLAGS_FINUP;
- err1 = atmel_sha_update(req);
- if (err1 == -EINPROGRESS ||
- (err1 == -EBUSY && (ahash_request_flags(req) &
- CRYPTO_TFM_REQ_MAY_BACKLOG)))
- return err1;
- /*
- * final() has to be always called to cleanup resources
- * even if udpate() failed, except EINPROGRESS
- */
- err2 = atmel_sha_final(req);
- return err1 ?: err2;
- }
- static int atmel_sha_digest(struct ahash_request *req)
- {
- return atmel_sha_init(req) ?: atmel_sha_finup(req);
- }
- static int atmel_sha_export(struct ahash_request *req, void *out)
- {
- const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- memcpy(out, ctx, sizeof(*ctx));
- return 0;
- }
- static int atmel_sha_import(struct ahash_request *req, const void *in)
- {
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- memcpy(ctx, in, sizeof(*ctx));
- return 0;
- }
- static int atmel_sha_cra_init(struct crypto_tfm *tfm)
- {
- struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
- crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
- sizeof(struct atmel_sha_reqctx));
- ctx->start = atmel_sha_start;
- return 0;
- }
- static struct ahash_alg sha_1_256_algs[] = {
- {
- .init = atmel_sha_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .finup = atmel_sha_finup,
- .digest = atmel_sha_digest,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA1_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "sha1",
- .cra_driver_name = "atmel-sha1",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA1_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_ctx),
- .cra_alignmask = 0,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_cra_init,
- }
- }
- },
- {
- .init = atmel_sha_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .finup = atmel_sha_finup,
- .digest = atmel_sha_digest,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA256_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "sha256",
- .cra_driver_name = "atmel-sha256",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA256_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_ctx),
- .cra_alignmask = 0,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_cra_init,
- }
- }
- },
- };
- static struct ahash_alg sha_224_alg = {
- .init = atmel_sha_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .finup = atmel_sha_finup,
- .digest = atmel_sha_digest,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA224_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "sha224",
- .cra_driver_name = "atmel-sha224",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA224_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_ctx),
- .cra_alignmask = 0,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_cra_init,
- }
- }
- };
- static struct ahash_alg sha_384_512_algs[] = {
- {
- .init = atmel_sha_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .finup = atmel_sha_finup,
- .digest = atmel_sha_digest,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA384_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "sha384",
- .cra_driver_name = "atmel-sha384",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA384_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_ctx),
- .cra_alignmask = 0x3,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_cra_init,
- }
- }
- },
- {
- .init = atmel_sha_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .finup = atmel_sha_finup,
- .digest = atmel_sha_digest,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA512_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "sha512",
- .cra_driver_name = "atmel-sha512",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA512_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_ctx),
- .cra_alignmask = 0x3,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_cra_init,
- }
- }
- },
- };
- static void atmel_sha_queue_task(unsigned long data)
- {
- struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
- atmel_sha_handle_queue(dd, NULL);
- }
- static int atmel_sha_done(struct atmel_sha_dev *dd)
- {
- int err = 0;
- if (SHA_FLAGS_CPU & dd->flags) {
- if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
- dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
- goto finish;
- }
- } else if (SHA_FLAGS_DMA_READY & dd->flags) {
- if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
- dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
- atmel_sha_update_dma_stop(dd);
- if (dd->err) {
- err = dd->err;
- goto finish;
- }
- }
- if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
- /* hash or semi-hash ready */
- dd->flags &= ~(SHA_FLAGS_DMA_READY |
- SHA_FLAGS_OUTPUT_READY);
- err = atmel_sha_update_dma_start(dd);
- if (err != -EINPROGRESS)
- goto finish;
- }
- }
- return err;
- finish:
- /* finish curent request */
- atmel_sha_finish_req(dd->req, err);
- return err;
- }
- static void atmel_sha_done_task(unsigned long data)
- {
- struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
- dd->is_async = true;
- (void)dd->resume(dd);
- }
- static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
- {
- struct atmel_sha_dev *sha_dd = dev_id;
- u32 reg;
- reg = atmel_sha_read(sha_dd, SHA_ISR);
- if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
- atmel_sha_write(sha_dd, SHA_IDR, reg);
- if (SHA_FLAGS_BUSY & sha_dd->flags) {
- sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
- if (!(SHA_FLAGS_CPU & sha_dd->flags))
- sha_dd->flags |= SHA_FLAGS_DMA_READY;
- tasklet_schedule(&sha_dd->done_task);
- } else {
- dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
- }
- return IRQ_HANDLED;
- }
- return IRQ_NONE;
- }
- /* DMA transfer functions */
- static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
- struct scatterlist *sg,
- size_t len)
- {
- struct atmel_sha_dma *dma = &dd->dma_lch_in;
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- size_t bs = ctx->block_size;
- int nents;
- for (nents = 0; sg; sg = sg_next(sg), ++nents) {
- if (!IS_ALIGNED(sg->offset, sizeof(u32)))
- return false;
- /*
- * This is the last sg, the only one that is allowed to
- * have an unaligned length.
- */
- if (len <= sg->length) {
- dma->nents = nents + 1;
- dma->last_sg_length = sg->length;
- sg->length = ALIGN(len, sizeof(u32));
- return true;
- }
- /* All other sg lengths MUST be aligned to the block size. */
- if (!IS_ALIGNED(sg->length, bs))
- return false;
- len -= sg->length;
- }
- return false;
- }
- static void atmel_sha_dma_callback2(void *data)
- {
- struct atmel_sha_dev *dd = data;
- struct atmel_sha_dma *dma = &dd->dma_lch_in;
- struct scatterlist *sg;
- int nents;
- dmaengine_terminate_all(dma->chan);
- dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
- sg = dma->sg;
- for (nents = 0; nents < dma->nents - 1; ++nents)
- sg = sg_next(sg);
- sg->length = dma->last_sg_length;
- dd->is_async = true;
- (void)atmel_sha_wait_for_data_ready(dd, dd->resume);
- }
- static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
- struct scatterlist *src,
- size_t len,
- atmel_sha_fn_t resume)
- {
- struct atmel_sha_dma *dma = &dd->dma_lch_in;
- struct dma_slave_config *config = &dma->dma_conf;
- struct dma_chan *chan = dma->chan;
- struct dma_async_tx_descriptor *desc;
- dma_cookie_t cookie;
- unsigned int sg_len;
- int err;
- dd->resume = resume;
- /*
- * dma->nents has already been initialized by
- * atmel_sha_dma_check_aligned().
- */
- dma->sg = src;
- sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
- if (!sg_len) {
- err = -ENOMEM;
- goto exit;
- }
- config->src_maxburst = 16;
- config->dst_maxburst = 16;
- err = dmaengine_slave_config(chan, config);
- if (err)
- goto unmap_sg;
- desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!desc) {
- err = -ENOMEM;
- goto unmap_sg;
- }
- desc->callback = atmel_sha_dma_callback2;
- desc->callback_param = dd;
- cookie = dmaengine_submit(desc);
- err = dma_submit_error(cookie);
- if (err)
- goto unmap_sg;
- dma_async_issue_pending(chan);
- return -EINPROGRESS;
- unmap_sg:
- dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
- exit:
- return atmel_sha_complete(dd, err);
- }
- /* CPU transfer functions */
- static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- const u32 *words = (const u32 *)ctx->buffer;
- size_t i, num_words;
- u32 isr, din, din_inc;
- din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
- for (;;) {
- /* Write data into the Input Data Registers. */
- num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
- for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
- atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
- ctx->offset += ctx->bufcnt;
- ctx->total -= ctx->bufcnt;
- if (!ctx->total)
- break;
- /*
- * Prepare next block:
- * Fill ctx->buffer now with the next data to be written into
- * IDATARx: it gives time for the SHA hardware to process
- * the current data so the SHA_INT_DATARDY flag might be set
- * in SHA_ISR when polling this register at the beginning of
- * the next loop.
- */
- ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
- scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
- ctx->offset, ctx->bufcnt, 0);
- /* Wait for hardware to be ready again. */
- isr = atmel_sha_read(dd, SHA_ISR);
- if (!(isr & SHA_INT_DATARDY)) {
- /* Not ready yet. */
- dd->resume = atmel_sha_cpu_transfer;
- atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
- return -EINPROGRESS;
- }
- }
- if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
- return dd->cpu_transfer_complete(dd);
- return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
- }
- static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
- struct scatterlist *sg,
- unsigned int len,
- bool idatar0_only,
- bool wait_data_ready,
- atmel_sha_fn_t resume)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- if (!len)
- return resume(dd);
- ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
- if (idatar0_only)
- ctx->flags |= SHA_FLAGS_IDATAR0;
- if (wait_data_ready)
- ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
- ctx->sg = sg;
- ctx->total = len;
- ctx->offset = 0;
- /* Prepare the first block to be written. */
- ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
- scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
- ctx->offset, ctx->bufcnt, 0);
- dd->cpu_transfer_complete = resume;
- return atmel_sha_cpu_transfer(dd);
- }
- static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd,
- const void *data, unsigned int datalen,
- bool auto_padding,
- atmel_sha_fn_t resume)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- u32 msglen = (auto_padding) ? datalen : 0;
- u32 mr = SHA_MR_MODE_AUTO;
- if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding))
- return atmel_sha_complete(dd, -EINVAL);
- mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
- atmel_sha_write(dd, SHA_MR, mr);
- atmel_sha_write(dd, SHA_MSR, msglen);
- atmel_sha_write(dd, SHA_BCR, msglen);
- atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
- sg_init_one(&dd->tmp, data, datalen);
- return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume);
- }
- /* hmac functions */
- struct atmel_sha_hmac_key {
- bool valid;
- unsigned int keylen;
- u8 buffer[SHA512_BLOCK_SIZE];
- u8 *keydup;
- };
- static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey)
- {
- memset(hkey, 0, sizeof(*hkey));
- }
- static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey)
- {
- kfree(hkey->keydup);
- memset(hkey, 0, sizeof(*hkey));
- }
- static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey,
- const u8 *key,
- unsigned int keylen)
- {
- atmel_sha_hmac_key_release(hkey);
- if (keylen > sizeof(hkey->buffer)) {
- hkey->keydup = kmemdup(key, keylen, GFP_KERNEL);
- if (!hkey->keydup)
- return -ENOMEM;
- } else {
- memcpy(hkey->buffer, key, keylen);
- }
- hkey->valid = true;
- hkey->keylen = keylen;
- return 0;
- }
- static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey,
- const u8 **key,
- unsigned int *keylen)
- {
- if (!hkey->valid)
- return false;
- *keylen = hkey->keylen;
- *key = (hkey->keydup) ? hkey->keydup : hkey->buffer;
- return true;
- }
- struct atmel_sha_hmac_ctx {
- struct atmel_sha_ctx base;
- struct atmel_sha_hmac_key hkey;
- u32 ipad[SHA512_BLOCK_SIZE / sizeof(u32)];
- u32 opad[SHA512_BLOCK_SIZE / sizeof(u32)];
- atmel_sha_fn_t resume;
- };
- static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
- atmel_sha_fn_t resume);
- static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
- const u8 *key, unsigned int keylen);
- static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd);
- static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd);
- static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd);
- static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd);
- static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd);
- static int atmel_sha_hmac_final(struct atmel_sha_dev *dd);
- static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd);
- static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd);
- static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
- atmel_sha_fn_t resume)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- unsigned int keylen;
- const u8 *key;
- size_t bs;
- hmac->resume = resume;
- switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
- case SHA_FLAGS_SHA1:
- ctx->block_size = SHA1_BLOCK_SIZE;
- ctx->hash_size = SHA1_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA224:
- ctx->block_size = SHA224_BLOCK_SIZE;
- ctx->hash_size = SHA256_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA256:
- ctx->block_size = SHA256_BLOCK_SIZE;
- ctx->hash_size = SHA256_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA384:
- ctx->block_size = SHA384_BLOCK_SIZE;
- ctx->hash_size = SHA512_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA512:
- ctx->block_size = SHA512_BLOCK_SIZE;
- ctx->hash_size = SHA512_DIGEST_SIZE;
- break;
- default:
- return atmel_sha_complete(dd, -EINVAL);
- }
- bs = ctx->block_size;
- if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen)))
- return resume(dd);
- /* Compute K' from K. */
- if (unlikely(keylen > bs))
- return atmel_sha_hmac_prehash_key(dd, key, keylen);
- /* Prepare ipad. */
- memcpy((u8 *)hmac->ipad, key, keylen);
- memset((u8 *)hmac->ipad + keylen, 0, bs - keylen);
- return atmel_sha_hmac_compute_ipad_hash(dd);
- }
- static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
- const u8 *key, unsigned int keylen)
- {
- return atmel_sha_cpu_hash(dd, key, keylen, true,
- atmel_sha_hmac_prehash_key_done);
- }
- static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- size_t ds = crypto_ahash_digestsize(tfm);
- size_t bs = ctx->block_size;
- size_t i, num_words = ds / sizeof(u32);
- /* Prepare ipad. */
- for (i = 0; i < num_words; ++i)
- hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
- memset((u8 *)hmac->ipad + ds, 0, bs - ds);
- return atmel_sha_hmac_compute_ipad_hash(dd);
- }
- static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- size_t bs = ctx->block_size;
- size_t i, num_words = bs / sizeof(u32);
- memcpy(hmac->opad, hmac->ipad, bs);
- for (i = 0; i < num_words; ++i) {
- hmac->ipad[i] ^= 0x36363636;
- hmac->opad[i] ^= 0x5c5c5c5c;
- }
- return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false,
- atmel_sha_hmac_compute_opad_hash);
- }
- static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- size_t bs = ctx->block_size;
- size_t hs = ctx->hash_size;
- size_t i, num_words = hs / sizeof(u32);
- for (i = 0; i < num_words; ++i)
- hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
- return atmel_sha_cpu_hash(dd, hmac->opad, bs, false,
- atmel_sha_hmac_setup_done);
- }
- static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- size_t hs = ctx->hash_size;
- size_t i, num_words = hs / sizeof(u32);
- for (i = 0; i < num_words; ++i)
- hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
- atmel_sha_hmac_key_release(&hmac->hkey);
- return hmac->resume(dd);
- }
- static int atmel_sha_hmac_start(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- int err;
- err = atmel_sha_hw_init(dd);
- if (err)
- return atmel_sha_complete(dd, err);
- switch (ctx->op) {
- case SHA_OP_INIT:
- err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done);
- break;
- case SHA_OP_UPDATE:
- dd->resume = atmel_sha_done;
- err = atmel_sha_update_req(dd);
- break;
- case SHA_OP_FINAL:
- dd->resume = atmel_sha_hmac_final;
- err = atmel_sha_final_req(dd);
- break;
- case SHA_OP_DIGEST:
- err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2);
- break;
- default:
- return atmel_sha_complete(dd, -EINVAL);
- }
- return err;
- }
- static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
- unsigned int keylen)
- {
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- return atmel_sha_hmac_key_set(&hmac->hkey, key, keylen);
- }
- static int atmel_sha_hmac_init(struct ahash_request *req)
- {
- int err;
- err = atmel_sha_init(req);
- if (err)
- return err;
- return atmel_sha_enqueue(req, SHA_OP_INIT);
- }
- static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- size_t bs = ctx->block_size;
- size_t hs = ctx->hash_size;
- ctx->bufcnt = 0;
- ctx->digcnt[0] = bs;
- ctx->digcnt[1] = 0;
- ctx->flags |= SHA_FLAGS_RESTORE;
- memcpy(ctx->digest, hmac->ipad, hs);
- return atmel_sha_complete(dd, 0);
- }
- static int atmel_sha_hmac_final(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- u32 *digest = (u32 *)ctx->digest;
- size_t ds = crypto_ahash_digestsize(tfm);
- size_t bs = ctx->block_size;
- size_t hs = ctx->hash_size;
- size_t i, num_words;
- u32 mr;
- /* Save d = SHA((K' + ipad) | msg). */
- num_words = ds / sizeof(u32);
- for (i = 0; i < num_words; ++i)
- digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
- /* Restore context to finish computing SHA((K' + opad) | d). */
- atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
- num_words = hs / sizeof(u32);
- for (i = 0; i < num_words; ++i)
- atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
- mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV;
- mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
- atmel_sha_write(dd, SHA_MR, mr);
- atmel_sha_write(dd, SHA_MSR, bs + ds);
- atmel_sha_write(dd, SHA_BCR, ds);
- atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
- sg_init_one(&dd->tmp, digest, ds);
- return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true,
- atmel_sha_hmac_final_done);
- }
- static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd)
- {
- /*
- * req->result might not be sizeof(u32) aligned, so copy the
- * digest into ctx->digest[] before memcpy() the data into
- * req->result.
- */
- atmel_sha_copy_hash(dd->req);
- atmel_sha_copy_ready_hash(dd->req);
- return atmel_sha_complete(dd, 0);
- }
- static int atmel_sha_hmac_digest(struct ahash_request *req)
- {
- int err;
- err = atmel_sha_init(req);
- if (err)
- return err;
- return atmel_sha_enqueue(req, SHA_OP_DIGEST);
- }
- static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- size_t hs = ctx->hash_size;
- size_t i, num_words = hs / sizeof(u32);
- bool use_dma = false;
- u32 mr;
- /* Special case for empty message. */
- if (!req->nbytes)
- return atmel_sha_complete(dd, -EINVAL); // TODO:
- /* Check DMA threshold and alignment. */
- if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
- atmel_sha_dma_check_aligned(dd, req->src, req->nbytes))
- use_dma = true;
- /* Write both initial hash values to compute a HMAC. */
- atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
- for (i = 0; i < num_words; ++i)
- atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
- atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
- for (i = 0; i < num_words; ++i)
- atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
- /* Write the Mode, Message Size, Bytes Count then Control Registers. */
- mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF);
- mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
- if (use_dma)
- mr |= SHA_MR_MODE_IDATAR0;
- else
- mr |= SHA_MR_MODE_AUTO;
- atmel_sha_write(dd, SHA_MR, mr);
- atmel_sha_write(dd, SHA_MSR, req->nbytes);
- atmel_sha_write(dd, SHA_BCR, req->nbytes);
- atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
- /* Process data. */
- if (use_dma)
- return atmel_sha_dma_start(dd, req->src, req->nbytes,
- atmel_sha_hmac_final_done);
- return atmel_sha_cpu_start(dd, req->src, req->nbytes, false, true,
- atmel_sha_hmac_final_done);
- }
- static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm)
- {
- struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
- crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
- sizeof(struct atmel_sha_reqctx));
- hmac->base.start = atmel_sha_hmac_start;
- atmel_sha_hmac_key_init(&hmac->hkey);
- return 0;
- }
- static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm)
- {
- struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
- atmel_sha_hmac_key_release(&hmac->hkey);
- }
- static struct ahash_alg sha_hmac_algs[] = {
- {
- .init = atmel_sha_hmac_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .digest = atmel_sha_hmac_digest,
- .setkey = atmel_sha_hmac_setkey,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA1_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "hmac(sha1)",
- .cra_driver_name = "atmel-hmac-sha1",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA1_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
- .cra_alignmask = 0,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_hmac_cra_init,
- .cra_exit = atmel_sha_hmac_cra_exit,
- }
- }
- },
- {
- .init = atmel_sha_hmac_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .digest = atmel_sha_hmac_digest,
- .setkey = atmel_sha_hmac_setkey,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA224_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "hmac(sha224)",
- .cra_driver_name = "atmel-hmac-sha224",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA224_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
- .cra_alignmask = 0,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_hmac_cra_init,
- .cra_exit = atmel_sha_hmac_cra_exit,
- }
- }
- },
- {
- .init = atmel_sha_hmac_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .digest = atmel_sha_hmac_digest,
- .setkey = atmel_sha_hmac_setkey,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA256_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "hmac(sha256)",
- .cra_driver_name = "atmel-hmac-sha256",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA256_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
- .cra_alignmask = 0,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_hmac_cra_init,
- .cra_exit = atmel_sha_hmac_cra_exit,
- }
- }
- },
- {
- .init = atmel_sha_hmac_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .digest = atmel_sha_hmac_digest,
- .setkey = atmel_sha_hmac_setkey,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA384_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "hmac(sha384)",
- .cra_driver_name = "atmel-hmac-sha384",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA384_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
- .cra_alignmask = 0,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_hmac_cra_init,
- .cra_exit = atmel_sha_hmac_cra_exit,
- }
- }
- },
- {
- .init = atmel_sha_hmac_init,
- .update = atmel_sha_update,
- .final = atmel_sha_final,
- .digest = atmel_sha_hmac_digest,
- .setkey = atmel_sha_hmac_setkey,
- .export = atmel_sha_export,
- .import = atmel_sha_import,
- .halg = {
- .digestsize = SHA512_DIGEST_SIZE,
- .statesize = sizeof(struct atmel_sha_reqctx),
- .base = {
- .cra_name = "hmac(sha512)",
- .cra_driver_name = "atmel-hmac-sha512",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_ASYNC,
- .cra_blocksize = SHA512_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
- .cra_alignmask = 0,
- .cra_module = THIS_MODULE,
- .cra_init = atmel_sha_hmac_cra_init,
- .cra_exit = atmel_sha_hmac_cra_exit,
- }
- }
- },
- };
- #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
- /* authenc functions */
- static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd);
- static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd);
- static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd);
- struct atmel_sha_authenc_ctx {
- struct crypto_ahash *tfm;
- };
- struct atmel_sha_authenc_reqctx {
- struct atmel_sha_reqctx base;
- atmel_aes_authenc_fn_t cb;
- struct atmel_aes_dev *aes_dev;
- /* _init() parameters. */
- struct scatterlist *assoc;
- u32 assoclen;
- u32 textlen;
- /* _final() parameters. */
- u32 *digest;
- unsigned int digestlen;
- };
- static void atmel_sha_authenc_complete(struct crypto_async_request *areq,
- int err)
- {
- struct ahash_request *req = areq->data;
- struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
- authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async);
- }
- static int atmel_sha_authenc_start(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
- int err;
- /*
- * Force atmel_sha_complete() to call req->base.complete(), ie
- * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
- */
- dd->force_complete = true;
- err = atmel_sha_hw_init(dd);
- return authctx->cb(authctx->aes_dev, err, dd->is_async);
- }
- bool atmel_sha_authenc_is_ready(void)
- {
- struct atmel_sha_ctx dummy;
- dummy.dd = NULL;
- return (atmel_sha_find_dev(&dummy) != NULL);
- }
- EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready);
- unsigned int atmel_sha_authenc_get_reqsize(void)
- {
- return sizeof(struct atmel_sha_authenc_reqctx);
- }
- EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize);
- struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode)
- {
- struct atmel_sha_authenc_ctx *auth;
- struct crypto_ahash *tfm;
- struct atmel_sha_ctx *tctx;
- const char *name;
- int err = -EINVAL;
- switch (mode & SHA_FLAGS_MODE_MASK) {
- case SHA_FLAGS_HMAC_SHA1:
- name = "atmel-hmac-sha1";
- break;
- case SHA_FLAGS_HMAC_SHA224:
- name = "atmel-hmac-sha224";
- break;
- case SHA_FLAGS_HMAC_SHA256:
- name = "atmel-hmac-sha256";
- break;
- case SHA_FLAGS_HMAC_SHA384:
- name = "atmel-hmac-sha384";
- break;
- case SHA_FLAGS_HMAC_SHA512:
- name = "atmel-hmac-sha512";
- break;
- default:
- goto error;
- }
- tfm = crypto_alloc_ahash(name, 0, 0);
- if (IS_ERR(tfm)) {
- err = PTR_ERR(tfm);
- goto error;
- }
- tctx = crypto_ahash_ctx(tfm);
- tctx->start = atmel_sha_authenc_start;
- tctx->flags = mode;
- auth = kzalloc(sizeof(*auth), GFP_KERNEL);
- if (!auth) {
- err = -ENOMEM;
- goto err_free_ahash;
- }
- auth->tfm = tfm;
- return auth;
- err_free_ahash:
- crypto_free_ahash(tfm);
- error:
- return ERR_PTR(err);
- }
- EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
- void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
- {
- if (auth)
- crypto_free_ahash(auth->tfm);
- kfree(auth);
- }
- EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
- int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
- const u8 *key, unsigned int keylen,
- u32 *flags)
- {
- struct crypto_ahash *tfm = auth->tfm;
- int err;
- crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
- crypto_ahash_set_flags(tfm, *flags & CRYPTO_TFM_REQ_MASK);
- err = crypto_ahash_setkey(tfm, key, keylen);
- *flags = crypto_ahash_get_flags(tfm);
- return err;
- }
- EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
- int atmel_sha_authenc_schedule(struct ahash_request *req,
- struct atmel_sha_authenc_ctx *auth,
- atmel_aes_authenc_fn_t cb,
- struct atmel_aes_dev *aes_dev)
- {
- struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
- struct atmel_sha_reqctx *ctx = &authctx->base;
- struct crypto_ahash *tfm = auth->tfm;
- struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
- struct atmel_sha_dev *dd;
- /* Reset request context (MUST be done first). */
- memset(authctx, 0, sizeof(*authctx));
- /* Get SHA device. */
- dd = atmel_sha_find_dev(tctx);
- if (!dd)
- return cb(aes_dev, -ENODEV, false);
- /* Init request context. */
- ctx->dd = dd;
- ctx->buflen = SHA_BUFFER_LEN;
- authctx->cb = cb;
- authctx->aes_dev = aes_dev;
- ahash_request_set_tfm(req, tfm);
- ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
- return atmel_sha_handle_queue(dd, req);
- }
- EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
- int atmel_sha_authenc_init(struct ahash_request *req,
- struct scatterlist *assoc, unsigned int assoclen,
- unsigned int textlen,
- atmel_aes_authenc_fn_t cb,
- struct atmel_aes_dev *aes_dev)
- {
- struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
- struct atmel_sha_reqctx *ctx = &authctx->base;
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- struct atmel_sha_dev *dd = ctx->dd;
- if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
- return atmel_sha_complete(dd, -EINVAL);
- authctx->cb = cb;
- authctx->aes_dev = aes_dev;
- authctx->assoc = assoc;
- authctx->assoclen = assoclen;
- authctx->textlen = textlen;
- ctx->flags = hmac->base.flags;
- return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
- }
- EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
- static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
- struct atmel_sha_reqctx *ctx = &authctx->base;
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
- size_t hs = ctx->hash_size;
- size_t i, num_words = hs / sizeof(u32);
- u32 mr, msg_size;
- atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
- for (i = 0; i < num_words; ++i)
- atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
- atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
- for (i = 0; i < num_words; ++i)
- atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
- mr = (SHA_MR_MODE_IDATAR0 |
- SHA_MR_HMAC |
- SHA_MR_DUALBUFF);
- mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
- atmel_sha_write(dd, SHA_MR, mr);
- msg_size = authctx->assoclen + authctx->textlen;
- atmel_sha_write(dd, SHA_MSR, msg_size);
- atmel_sha_write(dd, SHA_BCR, msg_size);
- atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
- /* Process assoc data. */
- return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
- true, false,
- atmel_sha_authenc_init_done);
- }
- static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
- return authctx->cb(authctx->aes_dev, 0, dd->is_async);
- }
- int atmel_sha_authenc_final(struct ahash_request *req,
- u32 *digest, unsigned int digestlen,
- atmel_aes_authenc_fn_t cb,
- struct atmel_aes_dev *aes_dev)
- {
- struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
- struct atmel_sha_reqctx *ctx = &authctx->base;
- struct atmel_sha_dev *dd = ctx->dd;
- switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
- case SHA_FLAGS_SHA1:
- authctx->digestlen = SHA1_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA224:
- authctx->digestlen = SHA224_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA256:
- authctx->digestlen = SHA256_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA384:
- authctx->digestlen = SHA384_DIGEST_SIZE;
- break;
- case SHA_FLAGS_SHA512:
- authctx->digestlen = SHA512_DIGEST_SIZE;
- break;
- default:
- return atmel_sha_complete(dd, -EINVAL);
- }
- if (authctx->digestlen > digestlen)
- authctx->digestlen = digestlen;
- authctx->cb = cb;
- authctx->aes_dev = aes_dev;
- authctx->digest = digest;
- return atmel_sha_wait_for_data_ready(dd,
- atmel_sha_authenc_final_done);
- }
- EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
- static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
- {
- struct ahash_request *req = dd->req;
- struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
- size_t i, num_words = authctx->digestlen / sizeof(u32);
- for (i = 0; i < num_words; ++i)
- authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
- return atmel_sha_complete(dd, 0);
- }
- void atmel_sha_authenc_abort(struct ahash_request *req)
- {
- struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
- struct atmel_sha_reqctx *ctx = &authctx->base;
- struct atmel_sha_dev *dd = ctx->dd;
- /* Prevent atmel_sha_complete() from calling req->base.complete(). */
- dd->is_async = false;
- dd->force_complete = false;
- (void)atmel_sha_complete(dd, 0);
- }
- EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
- #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
- static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
- {
- int i;
- if (dd->caps.has_hmac)
- for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
- crypto_unregister_ahash(&sha_hmac_algs[i]);
- for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
- crypto_unregister_ahash(&sha_1_256_algs[i]);
- if (dd->caps.has_sha224)
- crypto_unregister_ahash(&sha_224_alg);
- if (dd->caps.has_sha_384_512) {
- for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
- crypto_unregister_ahash(&sha_384_512_algs[i]);
- }
- }
- static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
- {
- int err, i, j;
- for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
- err = crypto_register_ahash(&sha_1_256_algs[i]);
- if (err)
- goto err_sha_1_256_algs;
- }
- if (dd->caps.has_sha224) {
- err = crypto_register_ahash(&sha_224_alg);
- if (err)
- goto err_sha_224_algs;
- }
- if (dd->caps.has_sha_384_512) {
- for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
- err = crypto_register_ahash(&sha_384_512_algs[i]);
- if (err)
- goto err_sha_384_512_algs;
- }
- }
- if (dd->caps.has_hmac) {
- for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
- err = crypto_register_ahash(&sha_hmac_algs[i]);
- if (err)
- goto err_sha_hmac_algs;
- }
- }
- return 0;
- /*i = ARRAY_SIZE(sha_hmac_algs);*/
- err_sha_hmac_algs:
- for (j = 0; j < i; j++)
- crypto_unregister_ahash(&sha_hmac_algs[j]);
- i = ARRAY_SIZE(sha_384_512_algs);
- err_sha_384_512_algs:
- for (j = 0; j < i; j++)
- crypto_unregister_ahash(&sha_384_512_algs[j]);
- crypto_unregister_ahash(&sha_224_alg);
- err_sha_224_algs:
- i = ARRAY_SIZE(sha_1_256_algs);
- err_sha_1_256_algs:
- for (j = 0; j < i; j++)
- crypto_unregister_ahash(&sha_1_256_algs[j]);
- return err;
- }
- static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
- {
- struct at_dma_slave *sl = slave;
- if (sl && sl->dma_dev == chan->device->dev) {
- chan->private = sl;
- return true;
- } else {
- return false;
- }
- }
- static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
- struct crypto_platform_data *pdata)
- {
- dma_cap_mask_t mask_in;
- /* Try to grab DMA channel */
- dma_cap_zero(mask_in);
- dma_cap_set(DMA_SLAVE, mask_in);
- dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
- atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
- if (!dd->dma_lch_in.chan) {
- dev_warn(dd->dev, "no DMA channel available\n");
- return -ENODEV;
- }
- dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
- dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
- SHA_REG_DIN(0);
- dd->dma_lch_in.dma_conf.src_maxburst = 1;
- dd->dma_lch_in.dma_conf.src_addr_width =
- DMA_SLAVE_BUSWIDTH_4_BYTES;
- dd->dma_lch_in.dma_conf.dst_maxburst = 1;
- dd->dma_lch_in.dma_conf.dst_addr_width =
- DMA_SLAVE_BUSWIDTH_4_BYTES;
- dd->dma_lch_in.dma_conf.device_fc = false;
- return 0;
- }
- static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
- {
- dma_release_channel(dd->dma_lch_in.chan);
- }
- static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
- {
- dd->caps.has_dma = 0;
- dd->caps.has_dualbuff = 0;
- dd->caps.has_sha224 = 0;
- dd->caps.has_sha_384_512 = 0;
- dd->caps.has_uihv = 0;
- dd->caps.has_hmac = 0;
- /* keep only major version number */
- switch (dd->hw_version & 0xff0) {
- case 0x510:
- dd->caps.has_dma = 1;
- dd->caps.has_dualbuff = 1;
- dd->caps.has_sha224 = 1;
- dd->caps.has_sha_384_512 = 1;
- dd->caps.has_uihv = 1;
- dd->caps.has_hmac = 1;
- break;
- case 0x420:
- dd->caps.has_dma = 1;
- dd->caps.has_dualbuff = 1;
- dd->caps.has_sha224 = 1;
- dd->caps.has_sha_384_512 = 1;
- dd->caps.has_uihv = 1;
- break;
- case 0x410:
- dd->caps.has_dma = 1;
- dd->caps.has_dualbuff = 1;
- dd->caps.has_sha224 = 1;
- dd->caps.has_sha_384_512 = 1;
- break;
- case 0x400:
- dd->caps.has_dma = 1;
- dd->caps.has_dualbuff = 1;
- dd->caps.has_sha224 = 1;
- break;
- case 0x320:
- break;
- default:
- dev_warn(dd->dev,
- "Unmanaged sha version, set minimum capabilities\n");
- break;
- }
- }
- #if defined(CONFIG_OF)
- static const struct of_device_id atmel_sha_dt_ids[] = {
- { .compatible = "atmel,at91sam9g46-sha" },
- { /* sentinel */ }
- };
- MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
- static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
- {
- struct device_node *np = pdev->dev.of_node;
- struct crypto_platform_data *pdata;
- if (!np) {
- dev_err(&pdev->dev, "device node not found\n");
- return ERR_PTR(-EINVAL);
- }
- pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
- if (!pdata)
- return ERR_PTR(-ENOMEM);
- pdata->dma_slave = devm_kzalloc(&pdev->dev,
- sizeof(*(pdata->dma_slave)),
- GFP_KERNEL);
- if (!pdata->dma_slave)
- return ERR_PTR(-ENOMEM);
- return pdata;
- }
- #else /* CONFIG_OF */
- static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
- {
- return ERR_PTR(-EINVAL);
- }
- #endif
- static int atmel_sha_probe(struct platform_device *pdev)
- {
- struct atmel_sha_dev *sha_dd;
- struct crypto_platform_data *pdata;
- struct device *dev = &pdev->dev;
- struct resource *sha_res;
- int err;
- sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
- if (sha_dd == NULL) {
- err = -ENOMEM;
- goto sha_dd_err;
- }
- sha_dd->dev = dev;
- platform_set_drvdata(pdev, sha_dd);
- INIT_LIST_HEAD(&sha_dd->list);
- spin_lock_init(&sha_dd->lock);
- tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
- (unsigned long)sha_dd);
- tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
- (unsigned long)sha_dd);
- crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
- /* Get the base address */
- sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (!sha_res) {
- dev_err(dev, "no MEM resource info\n");
- err = -ENODEV;
- goto res_err;
- }
- sha_dd->phys_base = sha_res->start;
- /* Get the IRQ */
- sha_dd->irq = platform_get_irq(pdev, 0);
- if (sha_dd->irq < 0) {
- dev_err(dev, "no IRQ resource info\n");
- err = sha_dd->irq;
- goto res_err;
- }
- err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
- IRQF_SHARED, "atmel-sha", sha_dd);
- if (err) {
- dev_err(dev, "unable to request sha irq.\n");
- goto res_err;
- }
- /* Initializing the clock */
- sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
- if (IS_ERR(sha_dd->iclk)) {
- dev_err(dev, "clock initialization failed.\n");
- err = PTR_ERR(sha_dd->iclk);
- goto res_err;
- }
- sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
- if (IS_ERR(sha_dd->io_base)) {
- dev_err(dev, "can't ioremap\n");
- err = PTR_ERR(sha_dd->io_base);
- goto res_err;
- }
- err = clk_prepare(sha_dd->iclk);
- if (err)
- goto res_err;
- atmel_sha_hw_version_init(sha_dd);
- atmel_sha_get_cap(sha_dd);
- if (sha_dd->caps.has_dma) {
- pdata = pdev->dev.platform_data;
- if (!pdata) {
- pdata = atmel_sha_of_init(pdev);
- if (IS_ERR(pdata)) {
- dev_err(&pdev->dev, "platform data not available\n");
- err = PTR_ERR(pdata);
- goto iclk_unprepare;
- }
- }
- if (!pdata->dma_slave) {
- err = -ENXIO;
- goto iclk_unprepare;
- }
- err = atmel_sha_dma_init(sha_dd, pdata);
- if (err)
- goto err_sha_dma;
- dev_info(dev, "using %s for DMA transfers\n",
- dma_chan_name(sha_dd->dma_lch_in.chan));
- }
- spin_lock(&atmel_sha.lock);
- list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
- spin_unlock(&atmel_sha.lock);
- err = atmel_sha_register_algs(sha_dd);
- if (err)
- goto err_algs;
- dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
- sha_dd->caps.has_sha224 ? "/SHA224" : "",
- sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
- return 0;
- err_algs:
- spin_lock(&atmel_sha.lock);
- list_del(&sha_dd->list);
- spin_unlock(&atmel_sha.lock);
- if (sha_dd->caps.has_dma)
- atmel_sha_dma_cleanup(sha_dd);
- err_sha_dma:
- iclk_unprepare:
- clk_unprepare(sha_dd->iclk);
- res_err:
- tasklet_kill(&sha_dd->queue_task);
- tasklet_kill(&sha_dd->done_task);
- sha_dd_err:
- dev_err(dev, "initialization failed.\n");
- return err;
- }
- static int atmel_sha_remove(struct platform_device *pdev)
- {
- struct atmel_sha_dev *sha_dd;
- sha_dd = platform_get_drvdata(pdev);
- if (!sha_dd)
- return -ENODEV;
- spin_lock(&atmel_sha.lock);
- list_del(&sha_dd->list);
- spin_unlock(&atmel_sha.lock);
- atmel_sha_unregister_algs(sha_dd);
- tasklet_kill(&sha_dd->queue_task);
- tasklet_kill(&sha_dd->done_task);
- if (sha_dd->caps.has_dma)
- atmel_sha_dma_cleanup(sha_dd);
- clk_unprepare(sha_dd->iclk);
- return 0;
- }
- static struct platform_driver atmel_sha_driver = {
- .probe = atmel_sha_probe,
- .remove = atmel_sha_remove,
- .driver = {
- .name = "atmel_sha",
- .of_match_table = of_match_ptr(atmel_sha_dt_ids),
- },
- };
- module_platform_driver(atmel_sha_driver);
- MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
- MODULE_LICENSE("GPL v2");
- MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");
|