// SPDX-License-Identifier: GPL-2.0 /* * sun8i-ss-prng.c - hardware cryptographic offloader for * Allwinner A80/A83T SoC * * Copyright (C) 2015-2020 Corentin Labbe <clabbe@baylibre.com> * * This file handle the PRNG found in the SS * * You could find a link for the datasheet in Documentation/arch/arm/sunxi.rst
*/ #include"sun8i-ss.h" #include <linux/dma-mapping.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/pm_runtime.h> #include <crypto/internal/rng.h>
algt = container_of(alg, struct sun8i_ss_alg_template, alg.rng);
ss = algt->ss;
if (ctx->slen == 0) {
dev_err(ss->dev, "The PRNG is not seeded\n"); return -EINVAL;
}
/* The SS does not give an updated seed, so we need to get a new one. * So we will ask for an extra PRNG_SEED_SIZE data. * We want dlen + seedsize rounded up to a multiple of PRNG_DATA_SIZE
*/
todo = dlen + PRNG_SEED_SIZE + PRNG_DATA_SIZE;
todo -= todo % PRNG_DATA_SIZE;
todo_with_padding = ALIGN(todo, dma_get_cache_alignment()); if (todo_with_padding < todo || todo < dlen) return -EOVERFLOW;
d = kzalloc(todo_with_padding, GFP_KERNEL); if (!d) return -ENOMEM;
mutex_lock(&ss->mlock);
writel(dma_iv, ss->base + SS_IV_ADR_REG); /* the PRNG act badly (failing rngtest) without SS_KEY_ADR_REG set */
writel(dma_iv, ss->base + SS_KEY_ADR_REG);
writel(dma_dst, ss->base + SS_DST_ADR_REG);
writel(todo / 4, ss->base + SS_LEN_ADR_REG);
reinit_completion(&ss->flows[flow].complete);
ss->flows[flow].status = 0; /* Be sure all data is written before enabling the task */
wmb();
writel(v, ss->base + SS_CTL_REG);
wait_for_completion_interruptible_timeout(&ss->flows[flow].complete,
msecs_to_jiffies(todo)); if (ss->flows[flow].status == 0) {
dev_err(ss->dev, "DMA timeout for PRNG (size=%u)\n", todo);
err = -EFAULT;
} /* Since cipher and hash use the linux/cryptoengine and that we have * a cryptoengine per flow, we are sure that they will issue only one * request per flow. * Since the cryptoengine wait for completion before submitting a new * one, the mlock could be left just after the final writel. * But cryptoengine cannot handle crypto_rng, so we need to be sure * nothing will use our flow. * The easiest way is to grab mlock until the hardware end our requests. * We could have used a per flow lock, but this would increase * complexity. * The drawback is that no request could be handled for the other flow.
*/
mutex_unlock(&ss->mlock);
pm_runtime_put(ss->dev);
err_pm:
dma_unmap_single(ss->dev, dma_dst, todo, DMA_FROM_DEVICE);
err_iv:
dma_unmap_single(ss->dev, dma_iv, ctx->slen, DMA_TO_DEVICE);
if (!err) {
memcpy(dst, d, dlen); /* Update seed */
memcpy(ctx->seed, d + dlen, ctx->slen);
}
err_free:
kfree_sensitive(d);
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.