// SPDX-License-Identifier: GPL-2.0 // // Cryptographic API. // // Support for Samsung S5PV210 and Exynos HW acceleration. // // Copyright (C) 2011 NetUP Inc. All rights reserved. // Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. // // Hash part based on omap-sham.c driver.
/* * HASH bit numbers, used by device, setting in dev->hash_flags with * functions set_bit(), clear_bit() or tested with test_bit() or BIT(), * to keep HASH state BUSY or FREE, or to signal state from irq_handler * to hash_tasklet. SGS keep track of allocated memory for scatterlist
*/ #define HASH_FLAGS_BUSY 0 #define HASH_FLAGS_FINAL 1 #define HASH_FLAGS_DMA_ACTIVE 2 #define HASH_FLAGS_OUTPUT_READY 3 #define HASH_FLAGS_DMA_READY 4 #define HASH_FLAGS_SGS_COPIED 5 #define HASH_FLAGS_SGS_ALLOCED 6
/** * struct samsung_aes_variant - platform specific SSS driver data * @aes_offset: AES register offset from SSS module's base. * @hash_offset: HASH register offset from SSS module's base. * @clk_names: names of clocks needed to run SSS IP * * Specifies platform specific configuration of SSS module. * Note: A structure for driver specific platform data is used for future * expansion of its usage.
*/ struct samsung_aes_variant { unsignedint aes_offset; unsignedint hash_offset; constchar *clk_names[2];
};
struct s5p_aes_reqctx { unsignedlong mode;
};
struct s5p_aes_ctx { struct s5p_aes_dev *dev;
u8 aes_key[AES_MAX_KEY_SIZE];
u8 nonce[CTR_RFC3686_NONCE_SIZE]; int keylen;
};
/** * struct s5p_aes_dev - Crypto device state container * @dev: Associated device * @clk: Clock for accessing hardware * @pclk: APB bus clock necessary to access the hardware * @ioaddr: Mapped IO memory region * @aes_ioaddr: Per-varian offset for AES block IO memory * @irq_fc: Feed control interrupt line * @req: Crypto request currently handled by the device * @ctx: Configuration for currently handled crypto request * @sg_src: Scatter list with source data for currently handled block * in device. This is DMA-mapped into device. * @sg_dst: Scatter list with destination data for currently handled block * in device. This is DMA-mapped into device. * @sg_src_cpy: In case of unaligned access, copied scatter list * with source data. * @sg_dst_cpy: In case of unaligned access, copied scatter list * with destination data. * @tasklet: New request scheduling jib * @queue: Crypto queue * @busy: Indicates whether the device is currently handling some request * thus it uses some of the fields from this state, like: * req, ctx, sg_src/dst (and copies). This essentially * protects against concurrent access to these fields. * @lock: Lock for protecting both access to device hardware registers * and fields related to current request (including the busy field). * @res: Resources for hash. * @io_hash_base: Per-variant offset for HASH block IO memory. * @hash_lock: Lock for protecting hash_req, hash_queue and hash_flags * variable. * @hash_flags: Flags for current HASH op. * @hash_queue: Async hash queue. * @hash_tasklet: New HASH request scheduling job. * @xmit_buf: Buffer for current HASH request transfer into SSS block. * @hash_req: Current request sending to SSS HASH block. * @hash_sg_iter: Scatterlist transferred through DMA into SSS HASH block. * @hash_sg_cnt: Counter for hash_sg_iter. * * @use_hash: true if HASH algs enabled
*/ struct s5p_aes_dev { struct device *dev; struct clk *clk; struct clk *pclk; void __iomem *ioaddr; void __iomem *aes_ioaddr; int irq_fc;
/** * struct s5p_hash_reqctx - HASH request context * @dd: Associated device * @op_update: Current request operation (OP_UPDATE or OP_FINAL) * @digcnt: Number of bytes processed by HW (without buffer[] ones) * @digest: Digest message or IV for partial result * @nregs: Number of HW registers for digest or IV read/write * @engine: Bits for selecting type of HASH in SSS block * @sg: sg for DMA transfer * @sg_len: Length of sg for DMA transfer * @sgl: sg for joining buffer and req->src scatterlist * @skip: Skip offset in req->src for current op * @total: Total number of bytes for current request * @finup: Keep state for finup or final. * @error: Keep track of error. * @bufcnt: Number of bytes holded in buffer[] * @buffer: For byte(s) from end of req->src in UPDATE op
*/ struct s5p_hash_reqctx { struct s5p_aes_dev *dd; bool op_update;
if (dev->sg_dst_cpy) {
dev_dbg(dev->dev, "Copying %d bytes of output data back to original place\n",
dev->req->cryptlen);
memcpy_to_sglist(dev->req->dst, 0, sg_virt(dev->sg_dst_cpy),
dev->req->cryptlen);
}
s5p_free_sg_cpy(dev, &dev->sg_src_cpy);
s5p_free_sg_cpy(dev, &dev->sg_dst_cpy); if (reqctx->mode & FLAGS_AES_CBC)
memcpy_fromio(req->iv, dev->aes_ioaddr + SSS_REG_AES_IV_DATA(0), AES_BLOCK_SIZE);
/* Calls the completion. Cannot be called with dev->lock hold. */ staticvoid s5p_aes_complete(struct skcipher_request *req, int err)
{
skcipher_request_complete(req, err);
}
if (!dma_map_sg(dev->dev, sg, 1, DMA_TO_DEVICE)) return -ENOMEM;
dev->sg_src = sg;
return 0;
}
/* * Returns -ERRNO on error (mapping of new data failed). * On success returns: * - 0 if there is no more data, * - 1 if new transmitting (output) data is ready and its address+length * have to be written to device (by calling s5p_set_dma_outdata()).
*/ staticint s5p_aes_tx(struct s5p_aes_dev *dev)
{ int ret = 0;
s5p_unset_outdata(dev);
if (!sg_is_last(dev->sg_dst)) {
ret = s5p_set_outdata(dev, sg_next(dev->sg_dst)); if (!ret)
ret = 1;
}
return ret;
}
/* * Returns -ERRNO on error (mapping of new data failed). * On success returns: * - 0 if there is no more data, * - 1 if new receiving (input) data is ready and its address+length * have to be written to device (by calling s5p_set_dma_indata()).
*/ staticint s5p_aes_rx(struct s5p_aes_dev *dev/*, bool *set_dma*/)
{ int ret = 0;
s5p_unset_indata(dev);
if (!sg_is_last(dev->sg_src)) {
ret = s5p_set_indata(dev, sg_next(dev->sg_src)); if (!ret)
ret = 1;
}
/** * s5p_hash_rx() - get next hash_sg_iter * @dev: device * * Return: * 2 if there is no more data and it is UPDATE op * 1 if new receiving (input) data is ready and can be written to device * 0 if there is no more data and it is FINAL op
*/ staticint s5p_hash_rx(struct s5p_aes_dev *dev)
{ if (dev->hash_sg_cnt > 0) {
dev->hash_sg_iter = sg_next(dev->hash_sg_iter); return 1;
}
set_bit(HASH_FLAGS_DMA_READY, &dev->hash_flags); if (test_bit(HASH_FLAGS_FINAL, &dev->hash_flags)) return 0;
/* * Handle rx or tx interrupt. If there is still data (scatterlist did not * reach end), then map next scatterlist entry. * In case of such mapping error, s5p_aes_complete() should be called. * * If there is no more data in tx scatter list, call s5p_aes_complete() * and schedule new tasklet. * * Handle hx interrupt. If there is still data map next entry.
*/
status = SSS_READ(dev, FCINTSTAT); if (status & SSS_FCINTSTAT_BRDMAINT)
err_dma_rx = s5p_aes_rx(dev);
if (status & SSS_FCINTSTAT_BTDMAINT) { if (sg_is_last(dev->sg_dst))
tx_end = true;
err_dma_tx = s5p_aes_tx(dev);
}
if (status & SSS_FCINTSTAT_HRDMAINT)
err_dma_hx = s5p_hash_rx(dev);
if (tx_end) {
s5p_sg_done(dev); if (err_dma_hx == 1)
s5p_set_dma_hashdata(dev, dev->hash_sg_iter);
spin_unlock_irqrestore(&dev->lock, flags);
s5p_aes_complete(dev->req, 0); /* Device is still busy */
tasklet_schedule(&dev->tasklet);
} else { /* * Writing length of DMA block (either receiving or * transmitting) will start the operation immediately, so this * should be done at the end (even after clearing pending * interrupts to not miss the interrupt).
*/ if (err_dma_tx == 1)
s5p_set_dma_outdata(dev, dev->sg_dst); if (err_dma_rx == 1)
s5p_set_dma_indata(dev, dev->sg_src); if (err_dma_hx == 1)
s5p_set_dma_hashdata(dev, dev->hash_sg_iter);
hash_irq_end: /* * Note about else if: * when hash_sg_iter reaches end and its UPDATE op, * issue SSS_HASH_PAUSE and wait for HPART irq
*/ if (hx_end)
tasklet_schedule(&dev->hash_tasklet); elseif (err_dma_hx == 2)
s5p_hash_write(dev, SSS_REG_HASH_CTRL_PAUSE,
SSS_HASH_PAUSE);
/** * s5p_hash_write_ctrl() - prepare HASH block in SecSS for processing * @dd: secss device * @length: length for request * @final: true if final op * * Prepare SSS HASH block for processing bytes in DMA mode. If it is called * after previous updates, fill up IV words. For final, calculate and set * lengths for HASH so SecSS can finalize hash. For partial, set SSS HASH * length as 2^63 so it will be never reached and set to zero prelow and * prehigh. * * This function does not start DMA transfer.
*/ staticvoid s5p_hash_write_ctrl(struct s5p_aes_dev *dd, size_t length, bool final)
{ struct s5p_hash_reqctx *ctx = ahash_request_ctx(dd->hash_req);
u32 prelow, prehigh, low, high;
u32 configflags, swapflags;
u64 tmplen;
configflags = ctx->engine | SSS_HASH_INIT_BIT;
if (likely(ctx->digcnt)) {
s5p_hash_write_ctx_iv(dd, ctx);
configflags |= SSS_HASH_USER_IV_EN;
}
if (final) { /* number of bytes for last part */
low = length;
high = 0; /* total number of bits prev hashed */
tmplen = ctx->digcnt * 8;
prelow = (u32)tmplen;
prehigh = (u32)(tmplen >> 32);
} else {
prelow = 0;
prehigh = 0;
low = 0;
high = BIT(31);
}
/** * s5p_hash_copy_sgs() - copy request's bytes into new buffer * @ctx: request context * @sg: source scatterlist request * @new_len: number of bytes to process from sg * * Allocate new buffer, copy data for HASH into it. If there was xmit_buf * filled, copy it first, then copy data from sg into it. Prepare one sgl[0] * with allocated buffer. * * Set bit in dd->hash_flag so we can free it after irq ends processing.
*/ staticint s5p_hash_copy_sgs(struct s5p_hash_reqctx *ctx, struct scatterlist *sg, unsignedint new_len)
{ unsignedint pages, len; void *buf;
len = new_len + ctx->bufcnt;
pages = get_order(len);
buf = (void *)__get_free_pages(GFP_ATOMIC, pages); if (!buf) {
dev_err(ctx->dd->dev, "alloc pages for unaligned case.\n");
ctx->error = true; return -ENOMEM;
}
if (ctx->bufcnt)
memcpy(buf, ctx->dd->xmit_buf, ctx->bufcnt);
/** * s5p_hash_copy_sg_lists() - copy sg list and make fixes in copy * @ctx: request context * @sg: source scatterlist request * @new_len: number of bytes to process from sg * * Allocate new scatterlist table, copy data for HASH into it. If there was * xmit_buf filled, prepare it first, then copy page, length and offset from * source sg into it, adjusting begin and/or end for skip offset and * hash_later value. * * Resulting sg table will be assigned to ctx->sg. Set flag so we can free * it after irq ends processing.
*/ staticint s5p_hash_copy_sg_lists(struct s5p_hash_reqctx *ctx, struct scatterlist *sg, unsignedint new_len)
{ unsignedint skip = ctx->skip, n = sg_nents(sg); struct scatterlist *tmp; unsignedint len;
/** * s5p_hash_prepare_sgs() - prepare sg for processing * @ctx: request context * @sg: source scatterlist request * @new_len: number of bytes to process from sg * @final: final flag * * Check two conditions: (1) if buffers in sg have len aligned data, and (2) * sg table have good aligned elements (list_ok). If one of this checks fails, * then either (1) allocates new buffer for data with s5p_hash_copy_sgs, copy * data into this buffer and prepare request in sgl, or (2) allocates new sg * table and prepare sg elements. * * For digest or finup all conditions can be good, and we may not need any * fixes.
*/ staticint s5p_hash_prepare_sgs(struct s5p_hash_reqctx *ctx, struct scatterlist *sg, unsignedint new_len, bool final)
{ unsignedint skip = ctx->skip, nbytes = new_len, n = 0; bool aligned = true, list_ok = true; struct scatterlist *sg_tmp = sg;
if (!sg || !sg->length || !new_len) return 0;
if (skip || !final)
list_ok = false;
while (nbytes > 0 && sg_tmp) {
n++; if (skip >= sg_tmp->length) {
skip -= sg_tmp->length; if (!sg_tmp->length) {
aligned = false; break;
}
} else { if (!IS_ALIGNED(sg_tmp->length - skip, BUFLEN)) {
aligned = false; break;
}
/* * Have aligned data from previous operation and/or current * Note: will enter here only if (digest or finup) and aligned
*/ if (ctx->bufcnt) {
ctx->sg_len = n;
sg_init_table(ctx->sgl, 2);
sg_set_buf(ctx->sgl, ctx->dd->xmit_buf, ctx->bufcnt);
sg_chain(ctx->sgl, 2, sg);
ctx->sg = ctx->sgl;
ctx->sg_len++;
} else {
ctx->sg = sg;
ctx->sg_len = n;
}
return 0;
}
/** * s5p_hash_prepare_request() - prepare request for processing * @req: AHASH request * @update: true if UPDATE op * * Note 1: we can have update flag _and_ final flag at the same time. * Note 2: we enter here when digcnt > BUFLEN (=HASH_BLOCK_SIZE) or * either req->nbytes or ctx->bufcnt + req->nbytes is > BUFLEN or * we have final op
*/ staticint s5p_hash_prepare_request(struct ahash_request *req, bool update)
{ struct s5p_hash_reqctx *ctx = ahash_request_ctx(req); bool final = ctx->finup; int xmit_len, hash_later, nbytes; int ret;
if (update)
nbytes = req->nbytes; else
nbytes = 0;
ctx->total = nbytes + ctx->bufcnt; if (!ctx->total) return 0;
if (nbytes && (!IS_ALIGNED(ctx->bufcnt, BUFLEN))) { /* bytes left from previous request, so fill up to BUFLEN */ int len = BUFLEN - ctx->bufcnt % BUFLEN;
hash_later = ctx->total - xmit_len; /* copy hash_later bytes from end of req->src */ /* previous bytes are in xmit_buf, so no overwrite */
memcpy_from_sglist(ctx->buffer, req->src,
req->nbytes - hash_later, hash_later);
}
if (xmit_len > BUFLEN) {
ret = s5p_hash_prepare_sgs(ctx, req->src, nbytes - hash_later,
final); if (ret) return ret;
} else { /* have buffered data only */ if (unlikely(!ctx->bufcnt)) { /* first update didn't fill up buffer */
memcpy_from_sglist(ctx->dd->xmit_buf, req->src,
0, xmit_len);
}
if (req->base.complete)
ahash_request_complete(req, err);
}
/** * s5p_hash_handle_queue() - handle hash queue * @dd: device s5p_aes_dev * @req: AHASH request * * If req!=NULL enqueue it on dd->queue, if FLAGS_BUSY is not set on the * device then processes the first request from the dd->queue * * Returns: see s5p_hash_final below.
*/ staticint s5p_hash_handle_queue(struct s5p_aes_dev *dd, struct ahash_request *req)
{ struct crypto_async_request *async_req, *backlog; struct s5p_hash_reqctx *ctx; unsignedlong flags; int err = 0, ret = 0;
retry:
spin_lock_irqsave(&dd->hash_lock, flags); if (req)
ret = ahash_enqueue_request(&dd->hash_queue, req);
if (test_bit(HASH_FLAGS_BUSY, &dd->hash_flags)) {
spin_unlock_irqrestore(&dd->hash_lock, flags); return ret;
}
backlog = crypto_get_backlog(&dd->hash_queue);
async_req = crypto_dequeue_request(&dd->hash_queue); if (async_req)
set_bit(HASH_FLAGS_BUSY, &dd->hash_flags);
spin_unlock_irqrestore(&dd->hash_lock, flags);
if (!async_req) return ret;
if (backlog)
crypto_request_complete(backlog, -EINPROGRESS);
if (!test_bit(HASH_FLAGS_BUSY, &dd->hash_flags)) {
s5p_hash_handle_queue(dd, NULL); return;
}
if (test_bit(HASH_FLAGS_DMA_READY, &dd->hash_flags)) { if (test_and_clear_bit(HASH_FLAGS_DMA_ACTIVE,
&dd->hash_flags)) {
s5p_hash_update_dma_stop(dd);
}
if (test_and_clear_bit(HASH_FLAGS_OUTPUT_READY,
&dd->hash_flags)) { /* hash or semi-hash ready */
clear_bit(HASH_FLAGS_DMA_READY, &dd->hash_flags); goto finish;
}
}
/** * s5p_hash_update() - process the hash input data * @req: AHASH request * * If request will fit in buffer, copy it and return immediately * else enqueue it with OP_UPDATE. * * Returns: see s5p_hash_final below.
*/ staticint s5p_hash_update(struct ahash_request *req)
{ struct s5p_hash_reqctx *ctx = ahash_request_ctx(req);
/** * s5p_hash_final() - close up hash and calculate digest * @req: AHASH request * * Note: in final req->src do not have any data, and req->nbytes can be * non-zero. * * If there were no input data processed yet and the buffered hash data is * less than BUFLEN (64) then calculate the final hash immediately by using * SW algorithm fallback. * * Otherwise enqueues the current AHASH request with OP_FINAL operation op * and finalize hash message in HW. Note that if digcnt!=0 then there were * previous update op, so there are always some buffered bytes in ctx->buffer, * which means that ctx->bufcnt!=0 * * Returns: * 0 if the request has been processed immediately, * -EINPROGRESS if the operation has been queued for later execution or is set * to processing by HW, * -EBUSY if queue is full and request should be resubmitted later, * other negative values denotes an error.
*/ staticint s5p_hash_final(struct ahash_request *req)
{ struct s5p_hash_reqctx *ctx = ahash_request_ctx(req);
ctx->finup = true; if (ctx->error) return -EINVAL; /* uncompleted hash is not needed */
/** * s5p_hash_finup() - process last req->src and calculate digest * @req: AHASH request containing the last update data * * Return values: see s5p_hash_final above.
*/ staticint s5p_hash_finup(struct ahash_request *req)
{ struct s5p_hash_reqctx *ctx = ahash_request_ctx(req); int err1, err2;
/* * final() has to be always called to cleanup resources even if * update() failed, except EINPROGRESS or calculate digest for small * size
*/
err2 = s5p_hash_final(req);
tctx->dd = s5p_dev; /* Allocate a fallback and abort if it failed. */
tctx->fallback = crypto_alloc_shash(alg_name, 0,
CRYPTO_ALG_NEED_FALLBACK); if (IS_ERR(tctx->fallback)) {
pr_err("fallback alloc fails for '%s'\n", alg_name); return PTR_ERR(tctx->fallback);
}
dev->sg_src_cpy = NULL;
sg = req->src; if (!s5p_is_sg_aligned(sg)) {
dev_dbg(dev->dev, "At least one unaligned source scatter list, making a copy\n");
err = s5p_make_sg_cpy(dev, sg, &dev->sg_src_cpy); if (err) return err;
dev->sg_dst_cpy = NULL;
sg = req->dst; if (!s5p_is_sg_aligned(sg)) {
dev_dbg(dev->dev, "At least one unaligned dest scatter list, making a copy\n");
err = s5p_make_sg_cpy(dev, sg, &dev->sg_dst_cpy); if (err) return err;
/* This sets bit [13:12] to 00, which selects 128-bit counter */
aes_control = SSS_AES_KEY_CHANGE_MODE; if (mode & FLAGS_AES_DECRYPT)
aes_control |= SSS_AES_MODE_DECRYPT;
/* as a variant it is possible to use byte swapping on DMA side */
aes_control |= SSS_AES_BYTESWAP_DI
| SSS_AES_BYTESWAP_DO
| SSS_AES_BYTESWAP_IV
| SSS_AES_BYTESWAP_KEY
| SSS_AES_BYTESWAP_CNT;
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); if (!pdata) return -ENOMEM;
variant = find_s5p_sss_version(pdev);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) return -EINVAL;
/* * Note: HASH and PRNG uses the same registers in secss, avoid * overwrite each other. This will drop HASH when CONFIG_EXYNOS_RNG * is enabled in config. We need larger size for HASH registers in * secss, current describe only AES/DES
*/ if (IS_ENABLED(CONFIG_CRYPTO_DEV_EXYNOS_HASH)) { if (variant == &exynos_aes_data) {
res->end += 0x300;
pdata->use_hash = true;
}
}
pdata->res = res;
pdata->ioaddr = devm_ioremap_resource(dev, res); if (IS_ERR(pdata->ioaddr)) { if (!pdata->use_hash) return PTR_ERR(pdata->ioaddr); /* try AES without HASH */
res->end -= 0x300;
pdata->use_hash = false;
pdata->ioaddr = devm_ioremap_resource(dev, res); if (IS_ERR(pdata->ioaddr)) return PTR_ERR(pdata->ioaddr);
}
pdata->clk = devm_clk_get(dev, variant->clk_names[0]); if (IS_ERR(pdata->clk)) return dev_err_probe(dev, PTR_ERR(pdata->clk), "failed to find secss clock %s\n",
variant->clk_names[0]);
for (i = 0; i < ARRAY_SIZE(algs); i++)
crypto_unregister_skcipher(&algs[i]);
tasklet_kill(&pdata->tasklet); if (pdata->use_hash) { for (i = ARRAY_SIZE(algs_sha1_md5_sha256) - 1; i >= 0; i--)
crypto_unregister_ahash(&algs_sha1_md5_sha256[i]);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.