Path: blob/master/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-hash.c
26292 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* sun4i-ss-hash.c - hardware cryptographic accelerator for Allwinner A20 SoC3*4* Copyright (C) 2013-2015 Corentin LABBE <[email protected]>5*6* This file add support for MD5 and SHA1.7*8* You could find the datasheet in Documentation/arch/arm/sunxi.rst9*/10#include "sun4i-ss.h"11#include <linux/unaligned.h>12#include <linux/scatterlist.h>1314/* This is a totally arbitrary value */15#define SS_TIMEOUT 1001617int sun4i_hash_crainit(struct crypto_tfm *tfm)18{19struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);20struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);21struct sun4i_ss_alg_template *algt;22int err;2324memset(op, 0, sizeof(struct sun4i_tfm_ctx));2526algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);27op->ss = algt->ss;2829err = pm_runtime_resume_and_get(op->ss->dev);30if (err < 0)31return err;3233crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),34sizeof(struct sun4i_req_ctx));35return 0;36}3738void sun4i_hash_craexit(struct crypto_tfm *tfm)39{40struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);4142pm_runtime_put(op->ss->dev);43}4445/* sun4i_hash_init: initialize request context */46int sun4i_hash_init(struct ahash_request *areq)47{48struct sun4i_req_ctx *op = ahash_request_ctx(areq);49struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);50struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);51struct sun4i_ss_alg_template *algt;5253memset(op, 0, sizeof(struct sun4i_req_ctx));5455algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);56op->mode = algt->mode;5758return 0;59}6061int sun4i_hash_export_md5(struct ahash_request *areq, void *out)62{63struct sun4i_req_ctx *op = ahash_request_ctx(areq);64struct md5_state *octx = out;65int i;6667octx->byte_count = op->byte_count + op->len;6869memcpy(octx->block, op->buf, op->len);7071if (op->byte_count) {72for (i = 0; i < 4; i++)73octx->hash[i] = op->hash[i];74} else {75octx->hash[0] = SHA1_H0;76octx->hash[1] = SHA1_H1;77octx->hash[2] = SHA1_H2;78octx->hash[3] = SHA1_H3;79}8081return 0;82}8384int sun4i_hash_import_md5(struct ahash_request *areq, const void *in)85{86struct sun4i_req_ctx *op = ahash_request_ctx(areq);87const struct md5_state *ictx = in;88int i;8990sun4i_hash_init(areq);9192op->byte_count = ictx->byte_count & ~0x3F;93op->len = ictx->byte_count & 0x3F;9495memcpy(op->buf, ictx->block, op->len);9697for (i = 0; i < 4; i++)98op->hash[i] = ictx->hash[i];99100return 0;101}102103int sun4i_hash_export_sha1(struct ahash_request *areq, void *out)104{105struct sun4i_req_ctx *op = ahash_request_ctx(areq);106struct sha1_state *octx = out;107int i;108109octx->count = op->byte_count + op->len;110111memcpy(octx->buffer, op->buf, op->len);112113if (op->byte_count) {114for (i = 0; i < 5; i++)115octx->state[i] = op->hash[i];116} else {117octx->state[0] = SHA1_H0;118octx->state[1] = SHA1_H1;119octx->state[2] = SHA1_H2;120octx->state[3] = SHA1_H3;121octx->state[4] = SHA1_H4;122}123124return 0;125}126127int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in)128{129struct sun4i_req_ctx *op = ahash_request_ctx(areq);130const struct sha1_state *ictx = in;131int i;132133sun4i_hash_init(areq);134135op->byte_count = ictx->count & ~0x3F;136op->len = ictx->count & 0x3F;137138memcpy(op->buf, ictx->buffer, op->len);139140for (i = 0; i < 5; i++)141op->hash[i] = ictx->state[i];142143return 0;144}145146#define SS_HASH_UPDATE 1147#define SS_HASH_FINAL 2148149/*150* sun4i_hash_update: update hash engine151*152* Could be used for both SHA1 and MD5153* Write data by step of 32bits and put then in the SS.154*155* Since we cannot leave partial data and hash state in the engine,156* we need to get the hash state at the end of this function.157* We can get the hash state every 64 bytes158*159* So the first work is to get the number of bytes to write to SS modulo 64160* The extra bytes will go to a temporary buffer op->buf storing op->len bytes161*162* So at the begin of update()163* if op->len + areq->nbytes < 64164* => all data will be written to wait buffer (op->buf) and end=0165* if not, write all data from op->buf to the device and position end to166* complete to 64bytes167*168* example 1:169* update1 60o => op->len=60170* update2 60o => need one more word to have 64 bytes171* end=4172* so write all data from op->buf and one word of SGs173* write remaining data in op->buf174* final state op->len=56175*/176static int sun4i_hash(struct ahash_request *areq)177{178/*179* i is the total bytes read from SGs, to be compared to areq->nbytes180* i is important because we cannot rely on SG length since the sum of181* SG->length could be greater than areq->nbytes182*183* end is the position when we need to stop writing to the device,184* to be compared to i185*186* in_i: advancement in the current SG187*/188unsigned int i = 0, end, fill, min_fill, nwait, nbw = 0, j = 0, todo;189unsigned int in_i = 0;190u32 spaces, rx_cnt = SS_RX_DEFAULT, bf[32] = {0}, v, ivmode = 0;191struct sun4i_req_ctx *op = ahash_request_ctx(areq);192struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);193struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);194struct sun4i_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);195struct sun4i_ss_ctx *ss = tfmctx->ss;196struct sun4i_ss_alg_template *algt;197struct scatterlist *in_sg = areq->src;198struct sg_mapping_iter mi;199int in_r, err = 0;200size_t copied = 0;201u32 wb = 0;202203dev_dbg(ss->dev, "%s %s bc=%llu len=%u mode=%x wl=%u h0=%0x",204__func__, crypto_tfm_alg_name(areq->base.tfm),205op->byte_count, areq->nbytes, op->mode,206op->len, op->hash[0]);207208if (unlikely(!areq->nbytes) && !(op->flags & SS_HASH_FINAL))209return 0;210211/* protect against overflow */212if (unlikely(areq->nbytes > UINT_MAX - op->len)) {213dev_err(ss->dev, "Cannot process too large request\n");214return -EINVAL;215}216217if (op->len + areq->nbytes < 64 && !(op->flags & SS_HASH_FINAL)) {218/* linearize data to op->buf */219copied = sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),220op->buf + op->len, areq->nbytes, 0);221op->len += copied;222return 0;223}224225spin_lock_bh(&ss->slock);226227/*228* if some data have been processed before,229* we need to restore the partial hash state230*/231if (op->byte_count) {232ivmode = SS_IV_ARBITRARY;233for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)234writel(op->hash[i], ss->base + SS_IV0 + i * 4);235}236/* Enable the device */237writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);238239if (!(op->flags & SS_HASH_UPDATE))240goto hash_final;241242/* start of handling data */243if (!(op->flags & SS_HASH_FINAL)) {244end = ((areq->nbytes + op->len) / 64) * 64 - op->len;245246if (end > areq->nbytes || areq->nbytes - end > 63) {247dev_err(ss->dev, "ERROR: Bound error %u %u\n",248end, areq->nbytes);249err = -EINVAL;250goto release_ss;251}252} else {253/* Since we have the flag final, we can go up to modulo 4 */254if (areq->nbytes < 4)255end = 0;256else257end = ((areq->nbytes + op->len) / 4) * 4 - op->len;258}259260/* TODO if SGlen % 4 and !op->len then DMA */261i = 1;262while (in_sg && i == 1) {263if (in_sg->length % 4)264i = 0;265in_sg = sg_next(in_sg);266}267if (i == 1 && !op->len && areq->nbytes)268dev_dbg(ss->dev, "We can DMA\n");269270i = 0;271sg_miter_start(&mi, areq->src, sg_nents(areq->src),272SG_MITER_FROM_SG | SG_MITER_ATOMIC);273sg_miter_next(&mi);274in_i = 0;275276do {277/*278* we need to linearize in two case:279* - the buffer is already used280* - the SG does not have enough byte remaining ( < 4)281*/282if (op->len || (mi.length - in_i) < 4) {283/*284* if we have entered here we have two reason to stop285* - the buffer is full286* - reach the end287*/288while (op->len < 64 && i < end) {289/* how many bytes we can read from current SG */290in_r = min(end - i, 64 - op->len);291in_r = min_t(size_t, mi.length - in_i, in_r);292memcpy(op->buf + op->len, mi.addr + in_i, in_r);293op->len += in_r;294i += in_r;295in_i += in_r;296if (in_i == mi.length) {297sg_miter_next(&mi);298in_i = 0;299}300}301if (op->len > 3 && !(op->len % 4)) {302/* write buf to the device */303writesl(ss->base + SS_RXFIFO, op->buf,304op->len / 4);305op->byte_count += op->len;306op->len = 0;307}308}309if (mi.length - in_i > 3 && i < end) {310/* how many bytes we can read from current SG */311in_r = min_t(size_t, mi.length - in_i, areq->nbytes - i);312in_r = min_t(size_t, ((mi.length - in_i) / 4) * 4, in_r);313/* how many bytes we can write in the device*/314todo = min3((u32)(end - i) / 4, rx_cnt, (u32)in_r / 4);315writesl(ss->base + SS_RXFIFO, mi.addr + in_i, todo);316op->byte_count += todo * 4;317i += todo * 4;318in_i += todo * 4;319rx_cnt -= todo;320if (!rx_cnt) {321spaces = readl(ss->base + SS_FCSR);322rx_cnt = SS_RXFIFO_SPACES(spaces);323}324if (in_i == mi.length) {325sg_miter_next(&mi);326in_i = 0;327}328}329} while (i < end);330331/*332* Now we have written to the device all that we can,333* store the remaining bytes in op->buf334*/335if ((areq->nbytes - i) < 64) {336while (i < areq->nbytes && in_i < mi.length && op->len < 64) {337/* how many bytes we can read from current SG */338in_r = min(areq->nbytes - i, 64 - op->len);339in_r = min_t(size_t, mi.length - in_i, in_r);340memcpy(op->buf + op->len, mi.addr + in_i, in_r);341op->len += in_r;342i += in_r;343in_i += in_r;344if (in_i == mi.length) {345sg_miter_next(&mi);346in_i = 0;347}348}349}350351sg_miter_stop(&mi);352353/*354* End of data process355* Now if we have the flag final go to finalize part356* If not, store the partial hash357*/358if (op->flags & SS_HASH_FINAL)359goto hash_final;360361writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);362i = 0;363do {364v = readl(ss->base + SS_CTL);365i++;366} while (i < SS_TIMEOUT && (v & SS_DATA_END));367if (unlikely(i >= SS_TIMEOUT)) {368dev_err_ratelimited(ss->dev,369"ERROR: hash end timeout %d>%d ctl=%x len=%u\n",370i, SS_TIMEOUT, v, areq->nbytes);371err = -EIO;372goto release_ss;373}374375/*376* The datasheet isn't very clear about when to retrieve the digest. The377* bit SS_DATA_END is cleared when the engine has processed the data and378* when the digest is computed *but* it doesn't mean the digest is379* available in the digest registers. Hence the delay to be sure we can380* read it.381*/382ndelay(1);383384for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)385op->hash[i] = readl(ss->base + SS_MD0 + i * 4);386387goto release_ss;388389/*390* hash_final: finalize hashing operation391*392* If we have some remaining bytes, we write them.393* Then ask the SS for finalizing the hashing operation394*395* I do not check RX FIFO size in this function since the size is 32396* after each enabling and this function neither write more than 32 words.397* If we come from the update part, we cannot have more than398* 3 remaining bytes to write and SS is fast enough to not care about it.399*/400401hash_final:402if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {403algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);404algt->stat_req++;405}406407/* write the remaining words of the wait buffer */408if (op->len) {409nwait = op->len / 4;410if (nwait) {411writesl(ss->base + SS_RXFIFO, op->buf, nwait);412op->byte_count += 4 * nwait;413}414415nbw = op->len - 4 * nwait;416if (nbw) {417wb = le32_to_cpup((__le32 *)(op->buf + nwait * 4));418wb &= GENMASK((nbw * 8) - 1, 0);419420op->byte_count += nbw;421}422}423424/* write the remaining bytes of the nbw buffer */425wb |= ((1 << 7) << (nbw * 8));426((__le32 *)bf)[j++] = cpu_to_le32(wb);427428/*429* number of space to pad to obtain 64o minus 8(size) minus 4 (final 1)430* I take the operations from other MD5/SHA1 implementations431*/432433/* last block size */434fill = 64 - (op->byte_count % 64);435min_fill = 2 * sizeof(u32) + (nbw ? 0 : sizeof(u32));436437/* if we can't fill all data, jump to the next 64 block */438if (fill < min_fill)439fill += 64;440441j += (fill - min_fill) / sizeof(u32);442443/* write the length of data */444if (op->mode == SS_OP_SHA1) {445__be64 *bits = (__be64 *)&bf[j];446*bits = cpu_to_be64(op->byte_count << 3);447j += 2;448} else {449__le64 *bits = (__le64 *)&bf[j];450*bits = cpu_to_le64(op->byte_count << 3);451j += 2;452}453writesl(ss->base + SS_RXFIFO, bf, j);454455/* Tell the SS to stop the hashing */456writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);457458/*459* Wait for SS to finish the hash.460* The timeout could happen only in case of bad overclocking461* or driver bug.462*/463i = 0;464do {465v = readl(ss->base + SS_CTL);466i++;467} while (i < SS_TIMEOUT && (v & SS_DATA_END));468if (unlikely(i >= SS_TIMEOUT)) {469dev_err_ratelimited(ss->dev,470"ERROR: hash end timeout %d>%d ctl=%x len=%u\n",471i, SS_TIMEOUT, v, areq->nbytes);472err = -EIO;473goto release_ss;474}475476/*477* The datasheet isn't very clear about when to retrieve the digest. The478* bit SS_DATA_END is cleared when the engine has processed the data and479* when the digest is computed *but* it doesn't mean the digest is480* available in the digest registers. Hence the delay to be sure we can481* read it.482*/483ndelay(1);484485/* Get the hash from the device */486if (op->mode == SS_OP_SHA1) {487for (i = 0; i < 5; i++) {488v = readl(ss->base + SS_MD0 + i * 4);489if (ss->variant->sha1_in_be)490put_unaligned_le32(v, areq->result + i * 4);491else492put_unaligned_be32(v, areq->result + i * 4);493}494} else {495for (i = 0; i < 4; i++) {496v = readl(ss->base + SS_MD0 + i * 4);497put_unaligned_le32(v, areq->result + i * 4);498}499}500501release_ss:502writel(0, ss->base + SS_CTL);503spin_unlock_bh(&ss->slock);504return err;505}506507int sun4i_hash_final(struct ahash_request *areq)508{509struct sun4i_req_ctx *op = ahash_request_ctx(areq);510511op->flags = SS_HASH_FINAL;512return sun4i_hash(areq);513}514515int sun4i_hash_update(struct ahash_request *areq)516{517struct sun4i_req_ctx *op = ahash_request_ctx(areq);518519op->flags = SS_HASH_UPDATE;520return sun4i_hash(areq);521}522523/* sun4i_hash_finup: finalize hashing operation after an update */524int sun4i_hash_finup(struct ahash_request *areq)525{526struct sun4i_req_ctx *op = ahash_request_ctx(areq);527528op->flags = SS_HASH_UPDATE | SS_HASH_FINAL;529return sun4i_hash(areq);530}531532/* combo of init/update/final functions */533int sun4i_hash_digest(struct ahash_request *areq)534{535int err;536struct sun4i_req_ctx *op = ahash_request_ctx(areq);537538err = sun4i_hash_init(areq);539if (err)540return err;541542op->flags = SS_HASH_UPDATE | SS_HASH_FINAL;543return sun4i_hash(areq);544}545546547