12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069 |
- // SPDX-License-Identifier: GPL-2.0+
- /*
- * caam - Freescale FSL CAAM support for ahash functions of crypto API
- *
- * Copyright 2011 Freescale Semiconductor, Inc.
- * Copyright 2018-2019 NXP
- *
- * Based on caamalg.c crypto API driver.
- *
- * relationship of digest job descriptor or first job descriptor after init to
- * shared descriptors:
- *
- * --------------- ---------------
- * | JobDesc #1 |-------------------->| ShareDesc |
- * | *(packet 1) | | (hashKey) |
- * --------------- | (operation) |
- * ---------------
- *
- * relationship of subsequent job descriptors to shared descriptors:
- *
- * --------------- ---------------
- * | JobDesc #2 |-------------------->| ShareDesc |
- * | *(packet 2) | |------------->| (hashKey) |
- * --------------- | |-------->| (operation) |
- * . | | | (load ctx2) |
- * . | | ---------------
- * --------------- | |
- * | JobDesc #3 |------| |
- * | *(packet 3) | |
- * --------------- |
- * . |
- * . |
- * --------------- |
- * | JobDesc #4 |------------
- * | *(packet 4) |
- * ---------------
- *
- * The SharedDesc never changes for a connection unless rekeyed, but
- * each packet will likely be in a different place. So all we need
- * to know to process the packet is where the input is, where the
- * output goes, and what context we want to process with. Context is
- * in the SharedDesc, packet references in the JobDesc.
- *
- * So, a job desc looks like:
- *
- * ---------------------
- * | Header |
- * | ShareDesc Pointer |
- * | SEQ_OUT_PTR |
- * | (output buffer) |
- * | (output length) |
- * | SEQ_IN_PTR |
- * | (input buffer) |
- * | (input length) |
- * ---------------------
- */
- #include "compat.h"
- #include "regs.h"
- #include "intern.h"
- #include "desc_constr.h"
- #include "jr.h"
- #include "error.h"
- #include "sg_sw_sec4.h"
- #include "key_gen.h"
- #include "caamhash_desc.h"
- #define CAAM_CRA_PRIORITY 3000
- /* max hash key is max split key size */
- #define CAAM_MAX_HASH_KEY_SIZE (SHA512_DIGEST_SIZE * 2)
- #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE
- #define CAAM_MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE
- #define DESC_HASH_MAX_USED_BYTES (DESC_AHASH_FINAL_LEN + \
- CAAM_MAX_HASH_KEY_SIZE)
- #define DESC_HASH_MAX_USED_LEN (DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)
- /* caam context sizes for hashes: running digest + 8 */
- #define HASH_MSG_LEN 8
- #define MAX_CTX_LEN (HASH_MSG_LEN + SHA512_DIGEST_SIZE)
- static struct list_head hash_list;
- /* ahash per-session context */
- struct caam_hash_ctx {
- u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
- u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
- u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
- u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
- u8 key[CAAM_MAX_HASH_KEY_SIZE] ____cacheline_aligned;
- dma_addr_t sh_desc_update_dma ____cacheline_aligned;
- dma_addr_t sh_desc_update_first_dma;
- dma_addr_t sh_desc_fin_dma;
- dma_addr_t sh_desc_digest_dma;
- enum dma_data_direction dir;
- enum dma_data_direction key_dir;
- struct device *jrdev;
- int ctx_len;
- struct alginfo adata;
- };
- /* ahash state */
- struct caam_hash_state {
- dma_addr_t buf_dma;
- dma_addr_t ctx_dma;
- int ctx_dma_len;
- u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
- int buflen_0;
- u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
- int buflen_1;
- u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
- int (*update)(struct ahash_request *req);
- int (*final)(struct ahash_request *req);
- int (*finup)(struct ahash_request *req);
- int current_buf;
- };
- struct caam_export_state {
- u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
- u8 caam_ctx[MAX_CTX_LEN];
- int buflen;
- int (*update)(struct ahash_request *req);
- int (*final)(struct ahash_request *req);
- int (*finup)(struct ahash_request *req);
- };
- static inline void switch_buf(struct caam_hash_state *state)
- {
- state->current_buf ^= 1;
- }
- static inline u8 *current_buf(struct caam_hash_state *state)
- {
- return state->current_buf ? state->buf_1 : state->buf_0;
- }
- static inline u8 *alt_buf(struct caam_hash_state *state)
- {
- return state->current_buf ? state->buf_0 : state->buf_1;
- }
- static inline int *current_buflen(struct caam_hash_state *state)
- {
- return state->current_buf ? &state->buflen_1 : &state->buflen_0;
- }
- static inline int *alt_buflen(struct caam_hash_state *state)
- {
- return state->current_buf ? &state->buflen_0 : &state->buflen_1;
- }
- static inline bool is_cmac_aes(u32 algtype)
- {
- return (algtype & (OP_ALG_ALGSEL_MASK | OP_ALG_AAI_MASK)) ==
- (OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC);
- }
- /* Common job descriptor seq in/out ptr routines */
- /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
- static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
- struct caam_hash_state *state,
- int ctx_len)
- {
- state->ctx_dma_len = ctx_len;
- state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
- ctx_len, DMA_FROM_DEVICE);
- if (dma_mapping_error(jrdev, state->ctx_dma)) {
- dev_err(jrdev, "unable to map ctx\n");
- state->ctx_dma = 0;
- return -ENOMEM;
- }
- append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
- return 0;
- }
- /* Map current buffer in state (if length > 0) and put it in link table */
- static inline int buf_map_to_sec4_sg(struct device *jrdev,
- struct sec4_sg_entry *sec4_sg,
- struct caam_hash_state *state)
- {
- int buflen = *current_buflen(state);
- if (!buflen)
- return 0;
- state->buf_dma = dma_map_single(jrdev, current_buf(state), buflen,
- DMA_TO_DEVICE);
- if (dma_mapping_error(jrdev, state->buf_dma)) {
- dev_err(jrdev, "unable to map buf\n");
- state->buf_dma = 0;
- return -ENOMEM;
- }
- dma_to_sec4_sg_one(sec4_sg, state->buf_dma, buflen, 0);
- return 0;
- }
- /* Map state->caam_ctx, and add it to link table */
- static inline int ctx_map_to_sec4_sg(struct device *jrdev,
- struct caam_hash_state *state, int ctx_len,
- struct sec4_sg_entry *sec4_sg, u32 flag)
- {
- state->ctx_dma_len = ctx_len;
- state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
- if (dma_mapping_error(jrdev, state->ctx_dma)) {
- dev_err(jrdev, "unable to map ctx\n");
- state->ctx_dma = 0;
- return -ENOMEM;
- }
- dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
- return 0;
- }
- static int ahash_set_sh_desc(struct crypto_ahash *ahash)
- {
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int digestsize = crypto_ahash_digestsize(ahash);
- struct device *jrdev = ctx->jrdev;
- struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
- u32 *desc;
- ctx->adata.key_virt = ctx->key;
- /* ahash_update shared descriptor */
- desc = ctx->sh_desc_update;
- cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len,
- ctx->ctx_len, true, ctrlpriv->era);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("ahash update shdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- /* ahash_update_first shared descriptor */
- desc = ctx->sh_desc_update_first;
- cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
- ctx->ctx_len, false, ctrlpriv->era);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("ahash update first shdesc@"__stringify(__LINE__)
- ": ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- /* ahash_final shared descriptor */
- desc = ctx->sh_desc_fin;
- cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize,
- ctx->ctx_len, true, ctrlpriv->era);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("ahash final shdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- /* ahash_digest shared descriptor */
- desc = ctx->sh_desc_digest;
- cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize,
- ctx->ctx_len, false, ctrlpriv->era);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("ahash digest shdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- return 0;
- }
- static int axcbc_set_sh_desc(struct crypto_ahash *ahash)
- {
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int digestsize = crypto_ahash_digestsize(ahash);
- struct device *jrdev = ctx->jrdev;
- u32 *desc;
- /* shared descriptor for ahash_update */
- desc = ctx->sh_desc_update;
- cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
- ctx->ctx_len, ctx->ctx_len);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("axcbc update shdesc@" __stringify(__LINE__)" : ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- /* shared descriptor for ahash_{final,finup} */
- desc = ctx->sh_desc_fin;
- cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
- digestsize, ctx->ctx_len);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("axcbc finup shdesc@" __stringify(__LINE__)" : ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- /* key is immediate data for INIT and INITFINAL states */
- ctx->adata.key_virt = ctx->key;
- /* shared descriptor for first invocation of ahash_update */
- desc = ctx->sh_desc_update_first;
- cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
- ctx->ctx_len);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("axcbc update first shdesc@" __stringify(__LINE__)
- " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- /* shared descriptor for ahash_digest */
- desc = ctx->sh_desc_digest;
- cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
- digestsize, ctx->ctx_len);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("axcbc digest shdesc@" __stringify(__LINE__)" : ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- return 0;
- }
- static int acmac_set_sh_desc(struct crypto_ahash *ahash)
- {
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int digestsize = crypto_ahash_digestsize(ahash);
- struct device *jrdev = ctx->jrdev;
- u32 *desc;
- /* shared descriptor for ahash_update */
- desc = ctx->sh_desc_update;
- cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
- ctx->ctx_len, ctx->ctx_len);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("acmac update shdesc@" __stringify(__LINE__)" : ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- /* shared descriptor for ahash_{final,finup} */
- desc = ctx->sh_desc_fin;
- cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
- digestsize, ctx->ctx_len);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("acmac finup shdesc@" __stringify(__LINE__)" : ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- /* shared descriptor for first invocation of ahash_update */
- desc = ctx->sh_desc_update_first;
- cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
- ctx->ctx_len);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("acmac update first shdesc@" __stringify(__LINE__)
- " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- /* shared descriptor for ahash_digest */
- desc = ctx->sh_desc_digest;
- cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
- digestsize, ctx->ctx_len);
- dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
- desc_bytes(desc), ctx->dir);
- print_hex_dump_debug("acmac digest shdesc@" __stringify(__LINE__)" : ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- return 0;
- }
- /* Digest hash size if it is too large */
- static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key,
- u32 digestsize)
- {
- struct device *jrdev = ctx->jrdev;
- u32 *desc;
- struct split_key_result result;
- dma_addr_t key_dma;
- int ret;
- desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
- if (!desc) {
- dev_err(jrdev, "unable to allocate key input memory\n");
- return -ENOMEM;
- }
- init_job_desc(desc, 0);
- key_dma = dma_map_single(jrdev, key, *keylen, DMA_BIDIRECTIONAL);
- if (dma_mapping_error(jrdev, key_dma)) {
- dev_err(jrdev, "unable to map key memory\n");
- kfree(desc);
- return -ENOMEM;
- }
- /* Job descriptor to perform unkeyed hash on key_in */
- append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
- OP_ALG_AS_INITFINAL);
- append_seq_in_ptr(desc, key_dma, *keylen, 0);
- append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
- FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
- append_seq_out_ptr(desc, key_dma, digestsize, 0);
- append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
- LDST_SRCDST_BYTE_CONTEXT);
- print_hex_dump_debug("key_in@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1);
- print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- result.err = 0;
- init_completion(&result.completion);
- ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
- if (!ret) {
- /* in progress */
- wait_for_completion(&result.completion);
- ret = result.err;
- print_hex_dump_debug("digested key@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, key,
- digestsize, 1);
- }
- dma_unmap_single(jrdev, key_dma, *keylen, DMA_BIDIRECTIONAL);
- *keylen = digestsize;
- kfree(desc);
- return ret;
- }
- static int ahash_setkey(struct crypto_ahash *ahash,
- const u8 *key, unsigned int keylen)
- {
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct device *jrdev = ctx->jrdev;
- int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
- int digestsize = crypto_ahash_digestsize(ahash);
- struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
- int ret;
- u8 *hashed_key = NULL;
- dev_dbg(jrdev, "keylen %d\n", keylen);
- if (keylen > blocksize) {
- hashed_key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
- if (!hashed_key)
- return -ENOMEM;
- ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
- if (ret)
- goto bad_free_key;
- key = hashed_key;
- }
- /*
- * If DKP is supported, use it in the shared descriptor to generate
- * the split key.
- */
- if (ctrlpriv->era >= 6) {
- ctx->adata.key_inline = true;
- ctx->adata.keylen = keylen;
- ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
- OP_ALG_ALGSEL_MASK);
- if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
- goto bad_free_key;
- memcpy(ctx->key, key, keylen);
- /*
- * In case |user key| > |derived key|, using DKP<imm,imm>
- * would result in invalid opcodes (last bytes of user key) in
- * the resulting descriptor. Use DKP<ptr,imm> instead => both
- * virtual and dma key addresses are needed.
- */
- if (keylen > ctx->adata.keylen_pad)
- dma_sync_single_for_device(ctx->jrdev,
- ctx->adata.key_dma,
- ctx->adata.keylen_pad,
- DMA_TO_DEVICE);
- } else {
- ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key,
- keylen, CAAM_MAX_HASH_KEY_SIZE);
- if (ret)
- goto bad_free_key;
- }
- kfree(hashed_key);
- return ahash_set_sh_desc(ahash);
- bad_free_key:
- kfree(hashed_key);
- crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
- return -EINVAL;
- }
- static int axcbc_setkey(struct crypto_ahash *ahash, const u8 *key,
- unsigned int keylen)
- {
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct device *jrdev = ctx->jrdev;
- if (keylen != AES_KEYSIZE_128) {
- crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
- return -EINVAL;
- }
- memcpy(ctx->key, key, keylen);
- dma_sync_single_for_device(jrdev, ctx->adata.key_dma, keylen,
- DMA_TO_DEVICE);
- ctx->adata.keylen = keylen;
- print_hex_dump_debug("axcbc ctx.key@" __stringify(__LINE__)" : ",
- DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, keylen, 1);
- return axcbc_set_sh_desc(ahash);
- }
- static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key,
- unsigned int keylen)
- {
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int err;
- err = aes_check_keylen(keylen);
- if (err) {
- crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
- return err;
- }
- /* key is immediate data for all cmac shared descriptors */
- ctx->adata.key_virt = key;
- ctx->adata.keylen = keylen;
- print_hex_dump_debug("acmac ctx.key@" __stringify(__LINE__)" : ",
- DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
- return acmac_set_sh_desc(ahash);
- }
- /*
- * ahash_edesc - s/w-extended ahash descriptor
- * @sec4_sg_dma: physical mapped address of h/w link table
- * @src_nents: number of segments in input scatterlist
- * @sec4_sg_bytes: length of dma mapped sec4_sg space
- * @hw_desc: the h/w job descriptor followed by any referenced link tables
- * @sec4_sg: h/w link table
- */
- struct ahash_edesc {
- dma_addr_t sec4_sg_dma;
- int src_nents;
- int sec4_sg_bytes;
- u32 hw_desc[DESC_JOB_IO_LEN_MAX / sizeof(u32)] ____cacheline_aligned;
- struct sec4_sg_entry sec4_sg[0];
- };
- static inline void ahash_unmap(struct device *dev,
- struct ahash_edesc *edesc,
- struct ahash_request *req, int dst_len)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- if (edesc->src_nents)
- dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
- if (edesc->sec4_sg_bytes)
- dma_unmap_single(dev, edesc->sec4_sg_dma,
- edesc->sec4_sg_bytes, DMA_TO_DEVICE);
- if (state->buf_dma) {
- dma_unmap_single(dev, state->buf_dma, *current_buflen(state),
- DMA_TO_DEVICE);
- state->buf_dma = 0;
- }
- }
- static inline void ahash_unmap_ctx(struct device *dev,
- struct ahash_edesc *edesc,
- struct ahash_request *req, int dst_len, u32 flag)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- if (state->ctx_dma) {
- dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
- state->ctx_dma = 0;
- }
- ahash_unmap(dev, edesc, req, dst_len);
- }
- static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
- void *context)
- {
- struct ahash_request *req = context;
- struct ahash_edesc *edesc;
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- int digestsize = crypto_ahash_digestsize(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int ecode = 0;
- dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
- edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
- if (err)
- ecode = caam_jr_strstatus(jrdev, err);
- ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
- memcpy(req->result, state->caam_ctx, digestsize);
- kfree(edesc);
- print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
- ctx->ctx_len, 1);
- req->base.complete(&req->base, ecode);
- }
- static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
- void *context)
- {
- struct ahash_request *req = context;
- struct ahash_edesc *edesc;
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- int digestsize = crypto_ahash_digestsize(ahash);
- int ecode = 0;
- dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
- edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
- if (err)
- ecode = caam_jr_strstatus(jrdev, err);
- ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
- switch_buf(state);
- kfree(edesc);
- print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
- ctx->ctx_len, 1);
- if (req->result)
- print_hex_dump_debug("result@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, req->result,
- digestsize, 1);
- req->base.complete(&req->base, ecode);
- }
- static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
- void *context)
- {
- struct ahash_request *req = context;
- struct ahash_edesc *edesc;
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- int digestsize = crypto_ahash_digestsize(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int ecode = 0;
- dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
- edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
- if (err)
- ecode = caam_jr_strstatus(jrdev, err);
- ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
- memcpy(req->result, state->caam_ctx, digestsize);
- kfree(edesc);
- print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
- ctx->ctx_len, 1);
- req->base.complete(&req->base, ecode);
- }
- static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
- void *context)
- {
- struct ahash_request *req = context;
- struct ahash_edesc *edesc;
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- int digestsize = crypto_ahash_digestsize(ahash);
- int ecode = 0;
- dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
- edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
- if (err)
- ecode = caam_jr_strstatus(jrdev, err);
- ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_FROM_DEVICE);
- switch_buf(state);
- kfree(edesc);
- print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
- ctx->ctx_len, 1);
- if (req->result)
- print_hex_dump_debug("result@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, req->result,
- digestsize, 1);
- req->base.complete(&req->base, ecode);
- }
- /*
- * Allocate an enhanced descriptor, which contains the hardware descriptor
- * and space for hardware scatter table containing sg_num entries.
- */
- static struct ahash_edesc *ahash_edesc_alloc(struct caam_hash_ctx *ctx,
- int sg_num, u32 *sh_desc,
- dma_addr_t sh_desc_dma,
- gfp_t flags)
- {
- struct ahash_edesc *edesc;
- unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
- edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
- if (!edesc) {
- dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
- return NULL;
- }
- init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
- HDR_SHARE_DEFER | HDR_REVERSE);
- return edesc;
- }
- static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
- struct ahash_edesc *edesc,
- struct ahash_request *req, int nents,
- unsigned int first_sg,
- unsigned int first_bytes, size_t to_hash)
- {
- dma_addr_t src_dma;
- u32 options;
- if (nents > 1 || first_sg) {
- struct sec4_sg_entry *sg = edesc->sec4_sg;
- unsigned int sgsize = sizeof(*sg) *
- pad_sg_nents(first_sg + nents);
- sg_to_sec4_sg_last(req->src, to_hash, sg + first_sg, 0);
- src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->jrdev, src_dma)) {
- dev_err(ctx->jrdev, "unable to map S/G table\n");
- return -ENOMEM;
- }
- edesc->sec4_sg_bytes = sgsize;
- edesc->sec4_sg_dma = src_dma;
- options = LDST_SGF;
- } else {
- src_dma = sg_dma_address(req->src);
- options = 0;
- }
- append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash,
- options);
- return 0;
- }
- /* submit update job descriptor */
- static int ahash_update_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct device *jrdev = ctx->jrdev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- u8 *buf = current_buf(state);
- int *buflen = current_buflen(state);
- u8 *next_buf = alt_buf(state);
- int blocksize = crypto_ahash_blocksize(ahash);
- int *next_buflen = alt_buflen(state), last_buflen;
- int in_len = *buflen + req->nbytes, to_hash;
- u32 *desc;
- int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
- struct ahash_edesc *edesc;
- int ret = 0;
- last_buflen = *next_buflen;
- *next_buflen = in_len & (blocksize - 1);
- to_hash = in_len - *next_buflen;
- /*
- * For XCBC and CMAC, if to_hash is multiple of block size,
- * keep last block in internal buffer
- */
- if ((is_xcbc_aes(ctx->adata.algtype) ||
- is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
- (*next_buflen == 0)) {
- *next_buflen = blocksize;
- to_hash -= blocksize;
- }
- if (to_hash) {
- int pad_nents;
- int src_len = req->nbytes - *next_buflen;
- src_nents = sg_nents_for_len(req->src, src_len);
- if (src_nents < 0) {
- dev_err(jrdev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(jrdev, "unable to DMA map source\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
- pad_nents = pad_sg_nents(sec4_sg_src_index + mapped_nents);
- sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
- /*
- * allocate space for base edesc and hw desc commands,
- * link tables
- */
- edesc = ahash_edesc_alloc(ctx, pad_nents, ctx->sh_desc_update,
- ctx->sh_desc_update_dma, flags);
- if (!edesc) {
- dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
- return -ENOMEM;
- }
- edesc->src_nents = src_nents;
- edesc->sec4_sg_bytes = sec4_sg_bytes;
- ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
- edesc->sec4_sg, DMA_BIDIRECTIONAL);
- if (ret)
- goto unmap_ctx;
- ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
- if (ret)
- goto unmap_ctx;
- if (mapped_nents)
- sg_to_sec4_sg_last(req->src, src_len,
- edesc->sec4_sg + sec4_sg_src_index,
- 0);
- else
- sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index -
- 1);
- if (*next_buflen)
- scatterwalk_map_and_copy(next_buf, req->src,
- to_hash - *buflen,
- *next_buflen, 0);
- desc = edesc->hw_desc;
- edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
- sec4_sg_bytes,
- DMA_TO_DEVICE);
- if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
- dev_err(jrdev, "unable to map S/G table\n");
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
- to_hash, LDST_SGF);
- append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
- print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req);
- if (ret)
- goto unmap_ctx;
- ret = -EINPROGRESS;
- } else if (*next_buflen) {
- scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
- req->nbytes, 0);
- *buflen = *next_buflen;
- *next_buflen = last_buflen;
- }
- print_hex_dump_debug("buf@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
- print_hex_dump_debug("next buf@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
- *next_buflen, 1);
- return ret;
- unmap_ctx:
- ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
- kfree(edesc);
- return ret;
- }
- static int ahash_final_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct device *jrdev = ctx->jrdev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- int buflen = *current_buflen(state);
- u32 *desc;
- int sec4_sg_bytes;
- int digestsize = crypto_ahash_digestsize(ahash);
- struct ahash_edesc *edesc;
- int ret;
- sec4_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) *
- sizeof(struct sec4_sg_entry);
- /* allocate space for base edesc and hw desc commands, link tables */
- edesc = ahash_edesc_alloc(ctx, 4, ctx->sh_desc_fin,
- ctx->sh_desc_fin_dma, flags);
- if (!edesc)
- return -ENOMEM;
- desc = edesc->hw_desc;
- edesc->sec4_sg_bytes = sec4_sg_bytes;
- ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
- edesc->sec4_sg, DMA_BIDIRECTIONAL);
- if (ret)
- goto unmap_ctx;
- ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
- if (ret)
- goto unmap_ctx;
- sg_to_sec4_set_last(edesc->sec4_sg + (buflen ? 1 : 0));
- edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
- sec4_sg_bytes, DMA_TO_DEVICE);
- if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
- dev_err(jrdev, "unable to map S/G table\n");
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
- LDST_SGF);
- append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
- print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
- if (ret)
- goto unmap_ctx;
- return -EINPROGRESS;
- unmap_ctx:
- ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
- kfree(edesc);
- return ret;
- }
- static int ahash_finup_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct device *jrdev = ctx->jrdev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- int buflen = *current_buflen(state);
- u32 *desc;
- int sec4_sg_src_index;
- int src_nents, mapped_nents;
- int digestsize = crypto_ahash_digestsize(ahash);
- struct ahash_edesc *edesc;
- int ret;
- src_nents = sg_nents_for_len(req->src, req->nbytes);
- if (src_nents < 0) {
- dev_err(jrdev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(jrdev, "unable to DMA map source\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- sec4_sg_src_index = 1 + (buflen ? 1 : 0);
- /* allocate space for base edesc and hw desc commands, link tables */
- edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
- ctx->sh_desc_fin, ctx->sh_desc_fin_dma,
- flags);
- if (!edesc) {
- dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
- return -ENOMEM;
- }
- desc = edesc->hw_desc;
- edesc->src_nents = src_nents;
- ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
- edesc->sec4_sg, DMA_BIDIRECTIONAL);
- if (ret)
- goto unmap_ctx;
- ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
- if (ret)
- goto unmap_ctx;
- ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents,
- sec4_sg_src_index, ctx->ctx_len + buflen,
- req->nbytes);
- if (ret)
- goto unmap_ctx;
- append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
- print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
- if (ret)
- goto unmap_ctx;
- return -EINPROGRESS;
- unmap_ctx:
- ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
- kfree(edesc);
- return ret;
- }
- static int ahash_digest(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct device *jrdev = ctx->jrdev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- u32 *desc;
- int digestsize = crypto_ahash_digestsize(ahash);
- int src_nents, mapped_nents;
- struct ahash_edesc *edesc;
- int ret;
- state->buf_dma = 0;
- src_nents = sg_nents_for_len(req->src, req->nbytes);
- if (src_nents < 0) {
- dev_err(jrdev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(jrdev, "unable to map source for DMA\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- /* allocate space for base edesc and hw desc commands, link tables */
- edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ? mapped_nents : 0,
- ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
- flags);
- if (!edesc) {
- dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
- return -ENOMEM;
- }
- edesc->src_nents = src_nents;
- ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
- req->nbytes);
- if (ret) {
- ahash_unmap(jrdev, edesc, req, digestsize);
- kfree(edesc);
- return ret;
- }
- desc = edesc->hw_desc;
- ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
- if (ret) {
- ahash_unmap(jrdev, edesc, req, digestsize);
- kfree(edesc);
- return -ENOMEM;
- }
- print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
- if (!ret) {
- ret = -EINPROGRESS;
- } else {
- ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
- kfree(edesc);
- }
- return ret;
- }
- /* submit ahash final if it the first job descriptor */
- static int ahash_final_no_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct device *jrdev = ctx->jrdev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- u8 *buf = current_buf(state);
- int buflen = *current_buflen(state);
- u32 *desc;
- int digestsize = crypto_ahash_digestsize(ahash);
- struct ahash_edesc *edesc;
- int ret;
- /* allocate space for base edesc and hw desc commands, link tables */
- edesc = ahash_edesc_alloc(ctx, 0, ctx->sh_desc_digest,
- ctx->sh_desc_digest_dma, flags);
- if (!edesc)
- return -ENOMEM;
- desc = edesc->hw_desc;
- if (buflen) {
- state->buf_dma = dma_map_single(jrdev, buf, buflen,
- DMA_TO_DEVICE);
- if (dma_mapping_error(jrdev, state->buf_dma)) {
- dev_err(jrdev, "unable to map src\n");
- goto unmap;
- }
- append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
- }
- ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
- if (ret)
- goto unmap;
- print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
- if (!ret) {
- ret = -EINPROGRESS;
- } else {
- ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
- kfree(edesc);
- }
- return ret;
- unmap:
- ahash_unmap(jrdev, edesc, req, digestsize);
- kfree(edesc);
- return -ENOMEM;
- }
- /* submit ahash update if it the first job descriptor after update */
- static int ahash_update_no_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct device *jrdev = ctx->jrdev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- u8 *buf = current_buf(state);
- int *buflen = current_buflen(state);
- int blocksize = crypto_ahash_blocksize(ahash);
- u8 *next_buf = alt_buf(state);
- int *next_buflen = alt_buflen(state);
- int in_len = *buflen + req->nbytes, to_hash;
- int sec4_sg_bytes, src_nents, mapped_nents;
- struct ahash_edesc *edesc;
- u32 *desc;
- int ret = 0;
- *next_buflen = in_len & (blocksize - 1);
- to_hash = in_len - *next_buflen;
- /*
- * For XCBC and CMAC, if to_hash is multiple of block size,
- * keep last block in internal buffer
- */
- if ((is_xcbc_aes(ctx->adata.algtype) ||
- is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
- (*next_buflen == 0)) {
- *next_buflen = blocksize;
- to_hash -= blocksize;
- }
- if (to_hash) {
- int pad_nents;
- int src_len = req->nbytes - *next_buflen;
- src_nents = sg_nents_for_len(req->src, src_len);
- if (src_nents < 0) {
- dev_err(jrdev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(jrdev, "unable to DMA map source\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- pad_nents = pad_sg_nents(1 + mapped_nents);
- sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
- /*
- * allocate space for base edesc and hw desc commands,
- * link tables
- */
- edesc = ahash_edesc_alloc(ctx, pad_nents,
- ctx->sh_desc_update_first,
- ctx->sh_desc_update_first_dma,
- flags);
- if (!edesc) {
- dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
- return -ENOMEM;
- }
- edesc->src_nents = src_nents;
- edesc->sec4_sg_bytes = sec4_sg_bytes;
- ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
- if (ret)
- goto unmap_ctx;
- sg_to_sec4_sg_last(req->src, src_len, edesc->sec4_sg + 1, 0);
- if (*next_buflen) {
- scatterwalk_map_and_copy(next_buf, req->src,
- to_hash - *buflen,
- *next_buflen, 0);
- }
- desc = edesc->hw_desc;
- edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
- sec4_sg_bytes,
- DMA_TO_DEVICE);
- if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
- dev_err(jrdev, "unable to map S/G table\n");
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
- ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
- if (ret)
- goto unmap_ctx;
- print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
- if (ret)
- goto unmap_ctx;
- ret = -EINPROGRESS;
- state->update = ahash_update_ctx;
- state->finup = ahash_finup_ctx;
- state->final = ahash_final_ctx;
- } else if (*next_buflen) {
- scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
- req->nbytes, 0);
- *buflen = *next_buflen;
- *next_buflen = 0;
- }
- print_hex_dump_debug("buf@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
- print_hex_dump_debug("next buf@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
- 1);
- return ret;
- unmap_ctx:
- ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
- kfree(edesc);
- return ret;
- }
- /* submit ahash finup if it the first job descriptor after update */
- static int ahash_finup_no_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct device *jrdev = ctx->jrdev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- int buflen = *current_buflen(state);
- u32 *desc;
- int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
- int digestsize = crypto_ahash_digestsize(ahash);
- struct ahash_edesc *edesc;
- int ret;
- src_nents = sg_nents_for_len(req->src, req->nbytes);
- if (src_nents < 0) {
- dev_err(jrdev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(jrdev, "unable to DMA map source\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- sec4_sg_src_index = 2;
- sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
- sizeof(struct sec4_sg_entry);
- /* allocate space for base edesc and hw desc commands, link tables */
- edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
- ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
- flags);
- if (!edesc) {
- dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
- return -ENOMEM;
- }
- desc = edesc->hw_desc;
- edesc->src_nents = src_nents;
- edesc->sec4_sg_bytes = sec4_sg_bytes;
- ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
- if (ret)
- goto unmap;
- ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen,
- req->nbytes);
- if (ret) {
- dev_err(jrdev, "unable to map S/G table\n");
- goto unmap;
- }
- ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
- if (ret)
- goto unmap;
- print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
- if (!ret) {
- ret = -EINPROGRESS;
- } else {
- ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
- kfree(edesc);
- }
- return ret;
- unmap:
- ahash_unmap(jrdev, edesc, req, digestsize);
- kfree(edesc);
- return -ENOMEM;
- }
- /* submit first update job descriptor after init */
- static int ahash_update_first(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct device *jrdev = ctx->jrdev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- u8 *next_buf = alt_buf(state);
- int *next_buflen = alt_buflen(state);
- int to_hash;
- int blocksize = crypto_ahash_blocksize(ahash);
- u32 *desc;
- int src_nents, mapped_nents;
- struct ahash_edesc *edesc;
- int ret = 0;
- *next_buflen = req->nbytes & (blocksize - 1);
- to_hash = req->nbytes - *next_buflen;
- /*
- * For XCBC and CMAC, if to_hash is multiple of block size,
- * keep last block in internal buffer
- */
- if ((is_xcbc_aes(ctx->adata.algtype) ||
- is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
- (*next_buflen == 0)) {
- *next_buflen = blocksize;
- to_hash -= blocksize;
- }
- if (to_hash) {
- src_nents = sg_nents_for_len(req->src,
- req->nbytes - *next_buflen);
- if (src_nents < 0) {
- dev_err(jrdev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(jrdev, "unable to map source for DMA\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- /*
- * allocate space for base edesc and hw desc commands,
- * link tables
- */
- edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ?
- mapped_nents : 0,
- ctx->sh_desc_update_first,
- ctx->sh_desc_update_first_dma,
- flags);
- if (!edesc) {
- dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
- return -ENOMEM;
- }
- edesc->src_nents = src_nents;
- ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
- to_hash);
- if (ret)
- goto unmap_ctx;
- if (*next_buflen)
- scatterwalk_map_and_copy(next_buf, req->src, to_hash,
- *next_buflen, 0);
- desc = edesc->hw_desc;
- ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
- if (ret)
- goto unmap_ctx;
- print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc,
- desc_bytes(desc), 1);
- ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
- if (ret)
- goto unmap_ctx;
- ret = -EINPROGRESS;
- state->update = ahash_update_ctx;
- state->finup = ahash_finup_ctx;
- state->final = ahash_final_ctx;
- } else if (*next_buflen) {
- state->update = ahash_update_no_ctx;
- state->finup = ahash_finup_no_ctx;
- state->final = ahash_final_no_ctx;
- scatterwalk_map_and_copy(next_buf, req->src, 0,
- req->nbytes, 0);
- switch_buf(state);
- }
- print_hex_dump_debug("next buf@"__stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
- 1);
- return ret;
- unmap_ctx:
- ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
- kfree(edesc);
- return ret;
- }
- static int ahash_finup_first(struct ahash_request *req)
- {
- return ahash_digest(req);
- }
- static int ahash_init(struct ahash_request *req)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- state->update = ahash_update_first;
- state->finup = ahash_finup_first;
- state->final = ahash_final_no_ctx;
- state->ctx_dma = 0;
- state->ctx_dma_len = 0;
- state->current_buf = 0;
- state->buf_dma = 0;
- state->buflen_0 = 0;
- state->buflen_1 = 0;
- return 0;
- }
- static int ahash_update(struct ahash_request *req)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- return state->update(req);
- }
- static int ahash_finup(struct ahash_request *req)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- return state->finup(req);
- }
- static int ahash_final(struct ahash_request *req)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- return state->final(req);
- }
- static int ahash_export(struct ahash_request *req, void *out)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_export_state *export = out;
- int len;
- u8 *buf;
- if (state->current_buf) {
- buf = state->buf_1;
- len = state->buflen_1;
- } else {
- buf = state->buf_0;
- len = state->buflen_0;
- }
- memcpy(export->buf, buf, len);
- memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
- export->buflen = len;
- export->update = state->update;
- export->final = state->final;
- export->finup = state->finup;
- return 0;
- }
- static int ahash_import(struct ahash_request *req, const void *in)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- const struct caam_export_state *export = in;
- memset(state, 0, sizeof(*state));
- memcpy(state->buf_0, export->buf, export->buflen);
- memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
- state->buflen_0 = export->buflen;
- state->update = export->update;
- state->final = export->final;
- state->finup = export->finup;
- return 0;
- }
- struct caam_hash_template {
- char name[CRYPTO_MAX_ALG_NAME];
- char driver_name[CRYPTO_MAX_ALG_NAME];
- char hmac_name[CRYPTO_MAX_ALG_NAME];
- char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
- unsigned int blocksize;
- struct ahash_alg template_ahash;
- u32 alg_type;
- };
- /* ahash descriptors */
- static struct caam_hash_template driver_hash[] = {
- {
- .name = "sha1",
- .driver_name = "sha1-caam",
- .hmac_name = "hmac(sha1)",
- .hmac_driver_name = "hmac-sha1-caam",
- .blocksize = SHA1_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA1_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA1,
- }, {
- .name = "sha224",
- .driver_name = "sha224-caam",
- .hmac_name = "hmac(sha224)",
- .hmac_driver_name = "hmac-sha224-caam",
- .blocksize = SHA224_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA224_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA224,
- }, {
- .name = "sha256",
- .driver_name = "sha256-caam",
- .hmac_name = "hmac(sha256)",
- .hmac_driver_name = "hmac-sha256-caam",
- .blocksize = SHA256_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA256_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA256,
- }, {
- .name = "sha384",
- .driver_name = "sha384-caam",
- .hmac_name = "hmac(sha384)",
- .hmac_driver_name = "hmac-sha384-caam",
- .blocksize = SHA384_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA384_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA384,
- }, {
- .name = "sha512",
- .driver_name = "sha512-caam",
- .hmac_name = "hmac(sha512)",
- .hmac_driver_name = "hmac-sha512-caam",
- .blocksize = SHA512_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA512_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA512,
- }, {
- .name = "md5",
- .driver_name = "md5-caam",
- .hmac_name = "hmac(md5)",
- .hmac_driver_name = "hmac-md5-caam",
- .blocksize = MD5_BLOCK_WORDS * 4,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = MD5_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_MD5,
- }, {
- .hmac_name = "xcbc(aes)",
- .hmac_driver_name = "xcbc-aes-caam",
- .blocksize = AES_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = axcbc_setkey,
- .halg = {
- .digestsize = AES_BLOCK_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XCBC_MAC,
- }, {
- .hmac_name = "cmac(aes)",
- .hmac_driver_name = "cmac-aes-caam",
- .blocksize = AES_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = acmac_setkey,
- .halg = {
- .digestsize = AES_BLOCK_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC,
- },
- };
- struct caam_hash_alg {
- struct list_head entry;
- int alg_type;
- struct ahash_alg ahash_alg;
- };
- static int caam_hash_cra_init(struct crypto_tfm *tfm)
- {
- struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
- struct crypto_alg *base = tfm->__crt_alg;
- struct hash_alg_common *halg =
- container_of(base, struct hash_alg_common, base);
- struct ahash_alg *alg =
- container_of(halg, struct ahash_alg, halg);
- struct caam_hash_alg *caam_hash =
- container_of(alg, struct caam_hash_alg, ahash_alg);
- struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
- /* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
- static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
- HASH_MSG_LEN + SHA1_DIGEST_SIZE,
- HASH_MSG_LEN + 32,
- HASH_MSG_LEN + SHA256_DIGEST_SIZE,
- HASH_MSG_LEN + 64,
- HASH_MSG_LEN + SHA512_DIGEST_SIZE };
- dma_addr_t dma_addr;
- struct caam_drv_private *priv;
- /*
- * Get a Job ring from Job Ring driver to ensure in-order
- * crypto request processing per tfm
- */
- ctx->jrdev = caam_jr_alloc();
- if (IS_ERR(ctx->jrdev)) {
- pr_err("Job Ring Device allocation for transform failed\n");
- return PTR_ERR(ctx->jrdev);
- }
- priv = dev_get_drvdata(ctx->jrdev->parent);
- if (is_xcbc_aes(caam_hash->alg_type)) {
- ctx->dir = DMA_TO_DEVICE;
- ctx->key_dir = DMA_BIDIRECTIONAL;
- ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
- ctx->ctx_len = 48;
- } else if (is_cmac_aes(caam_hash->alg_type)) {
- ctx->dir = DMA_TO_DEVICE;
- ctx->key_dir = DMA_NONE;
- ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
- ctx->ctx_len = 32;
- } else {
- if (priv->era >= 6) {
- ctx->dir = DMA_BIDIRECTIONAL;
- ctx->key_dir = alg->setkey ? DMA_TO_DEVICE : DMA_NONE;
- } else {
- ctx->dir = DMA_TO_DEVICE;
- ctx->key_dir = DMA_NONE;
- }
- ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
- ctx->ctx_len = runninglen[(ctx->adata.algtype &
- OP_ALG_ALGSEL_SUBMASK) >>
- OP_ALG_ALGSEL_SHIFT];
- }
- if (ctx->key_dir != DMA_NONE) {
- ctx->adata.key_dma = dma_map_single_attrs(ctx->jrdev, ctx->key,
- ARRAY_SIZE(ctx->key),
- ctx->key_dir,
- DMA_ATTR_SKIP_CPU_SYNC);
- if (dma_mapping_error(ctx->jrdev, ctx->adata.key_dma)) {
- dev_err(ctx->jrdev, "unable to map key\n");
- caam_jr_free(ctx->jrdev);
- return -ENOMEM;
- }
- }
- dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_update,
- offsetof(struct caam_hash_ctx, key),
- ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
- if (dma_mapping_error(ctx->jrdev, dma_addr)) {
- dev_err(ctx->jrdev, "unable to map shared descriptors\n");
- if (ctx->key_dir != DMA_NONE)
- dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
- ARRAY_SIZE(ctx->key),
- ctx->key_dir,
- DMA_ATTR_SKIP_CPU_SYNC);
- caam_jr_free(ctx->jrdev);
- return -ENOMEM;
- }
- ctx->sh_desc_update_dma = dma_addr;
- ctx->sh_desc_update_first_dma = dma_addr +
- offsetof(struct caam_hash_ctx,
- sh_desc_update_first);
- ctx->sh_desc_fin_dma = dma_addr + offsetof(struct caam_hash_ctx,
- sh_desc_fin);
- ctx->sh_desc_digest_dma = dma_addr + offsetof(struct caam_hash_ctx,
- sh_desc_digest);
- crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
- sizeof(struct caam_hash_state));
- /*
- * For keyed hash algorithms shared descriptors
- * will be created later in setkey() callback
- */
- return alg->setkey ? 0 : ahash_set_sh_desc(ahash);
- }
- static void caam_hash_cra_exit(struct crypto_tfm *tfm)
- {
- struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
- dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_update_dma,
- offsetof(struct caam_hash_ctx, key),
- ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
- if (ctx->key_dir != DMA_NONE)
- dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
- ARRAY_SIZE(ctx->key), ctx->key_dir,
- DMA_ATTR_SKIP_CPU_SYNC);
- caam_jr_free(ctx->jrdev);
- }
- void caam_algapi_hash_exit(void)
- {
- struct caam_hash_alg *t_alg, *n;
- if (!hash_list.next)
- return;
- list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
- crypto_unregister_ahash(&t_alg->ahash_alg);
- list_del(&t_alg->entry);
- kfree(t_alg);
- }
- }
- static struct caam_hash_alg *
- caam_hash_alloc(struct caam_hash_template *template,
- bool keyed)
- {
- struct caam_hash_alg *t_alg;
- struct ahash_alg *halg;
- struct crypto_alg *alg;
- t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
- if (!t_alg) {
- pr_err("failed to allocate t_alg\n");
- return ERR_PTR(-ENOMEM);
- }
- t_alg->ahash_alg = template->template_ahash;
- halg = &t_alg->ahash_alg;
- alg = &halg->halg.base;
- if (keyed) {
- snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
- template->hmac_name);
- snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
- template->hmac_driver_name);
- } else {
- snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
- template->name);
- snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
- template->driver_name);
- t_alg->ahash_alg.setkey = NULL;
- }
- alg->cra_module = THIS_MODULE;
- alg->cra_init = caam_hash_cra_init;
- alg->cra_exit = caam_hash_cra_exit;
- alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
- alg->cra_priority = CAAM_CRA_PRIORITY;
- alg->cra_blocksize = template->blocksize;
- alg->cra_alignmask = 0;
- alg->cra_flags = CRYPTO_ALG_ASYNC;
- t_alg->alg_type = template->alg_type;
- return t_alg;
- }
- int caam_algapi_hash_init(struct device *ctrldev)
- {
- int i = 0, err = 0;
- struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
- unsigned int md_limit = SHA512_DIGEST_SIZE;
- u32 md_inst, md_vid;
- /*
- * Register crypto algorithms the device supports. First, identify
- * presence and attributes of MD block.
- */
- if (priv->era < 10) {
- md_vid = (rd_reg32(&priv->ctrl->perfmon.cha_id_ls) &
- CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
- md_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
- CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
- } else {
- u32 mdha = rd_reg32(&priv->ctrl->vreg.mdha);
- md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
- md_inst = mdha & CHA_VER_NUM_MASK;
- }
- /*
- * Skip registration of any hashing algorithms if MD block
- * is not present.
- */
- if (!md_inst)
- return 0;
- /* Limit digest size based on LP256 */
- if (md_vid == CHA_VER_VID_MD_LP256)
- md_limit = SHA256_DIGEST_SIZE;
- INIT_LIST_HEAD(&hash_list);
- /* register crypto algorithms the device supports */
- for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
- struct caam_hash_alg *t_alg;
- struct caam_hash_template *alg = driver_hash + i;
- /* If MD size is not supported by device, skip registration */
- if (is_mdha(alg->alg_type) &&
- alg->template_ahash.halg.digestsize > md_limit)
- continue;
- /* register hmac version */
- t_alg = caam_hash_alloc(alg, true);
- if (IS_ERR(t_alg)) {
- err = PTR_ERR(t_alg);
- pr_warn("%s alg allocation failed\n",
- alg->hmac_driver_name);
- continue;
- }
- err = crypto_register_ahash(&t_alg->ahash_alg);
- if (err) {
- pr_warn("%s alg registration failed: %d\n",
- t_alg->ahash_alg.halg.base.cra_driver_name,
- err);
- kfree(t_alg);
- } else
- list_add_tail(&t_alg->entry, &hash_list);
- if ((alg->alg_type & OP_ALG_ALGSEL_MASK) == OP_ALG_ALGSEL_AES)
- continue;
- /* register unkeyed version */
- t_alg = caam_hash_alloc(alg, false);
- if (IS_ERR(t_alg)) {
- err = PTR_ERR(t_alg);
- pr_warn("%s alg allocation failed\n", alg->driver_name);
- continue;
- }
- err = crypto_register_ahash(&t_alg->ahash_alg);
- if (err) {
- pr_warn("%s alg registration failed: %d\n",
- t_alg->ahash_alg.halg.base.cra_driver_name,
- err);
- kfree(t_alg);
- } else
- list_add_tail(&t_alg->entry, &hash_list);
- }
- return err;
- }
|