2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-07-17 22:39:41 +08:00
|
|
|
/*
|
|
|
|
* sun4i-ss-cipher.c - hardware cryptographic accelerator for Allwinner A20 SoC
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
|
|
|
|
*
|
|
|
|
* This file add support for AES cipher with 128,192,256 bits
|
|
|
|
* keysize in CBC and ECB mode.
|
|
|
|
* Add support also for DES and 3DES in CBC and ECB mode.
|
|
|
|
*
|
2019-04-15 02:51:10 +08:00
|
|
|
* You could find the datasheet in Documentation/arm/sunxi.rst
|
2015-07-17 22:39:41 +08:00
|
|
|
*/
|
|
|
|
#include "sun4i-ss.h"
|
|
|
|
|
2019-06-17 21:25:17 +08:00
|
|
|
static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
2015-07-17 22:39:41 +08:00
|
|
|
struct sun4i_ss_ctx *ss = op->ss;
|
2017-06-02 03:39:01 +08:00
|
|
|
unsigned int ivsize = crypto_skcipher_ivsize(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
u32 mode = ctx->mode;
|
|
|
|
/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
|
|
|
|
u32 rx_cnt = SS_RX_DEFAULT;
|
|
|
|
u32 tx_cnt = 0;
|
|
|
|
u32 spaces;
|
|
|
|
u32 v;
|
2016-08-10 17:45:29 +08:00
|
|
|
int err = 0;
|
|
|
|
unsigned int i;
|
2017-06-02 03:39:01 +08:00
|
|
|
unsigned int ileft = areq->cryptlen;
|
|
|
|
unsigned int oleft = areq->cryptlen;
|
2015-07-17 22:39:41 +08:00
|
|
|
unsigned int todo;
|
2024-06-11 20:26:44 +08:00
|
|
|
unsigned long pi = 0, po = 0; /* progress for in and out */
|
|
|
|
bool miter_err;
|
2015-07-17 22:39:41 +08:00
|
|
|
struct sg_mapping_iter mi, mo;
|
|
|
|
unsigned int oi, oo; /* offset for in and out */
|
2016-03-23 23:11:24 +08:00
|
|
|
unsigned long flags;
|
2015-07-17 22:39:41 +08:00
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
if (!areq->cryptlen)
|
2015-07-17 22:39:41 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!areq->src || !areq->dst) {
|
|
|
|
dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:11:24 +08:00
|
|
|
spin_lock_irqsave(&ss->slock, flags);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
2024-06-11 20:26:44 +08:00
|
|
|
for (i = 0; i < op->keylen / 4; i++)
|
|
|
|
writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
if (areq->iv) {
|
2015-07-17 22:39:41 +08:00
|
|
|
for (i = 0; i < 4 && i < ivsize / 4; i++) {
|
2017-06-02 03:39:01 +08:00
|
|
|
v = *(u32 *)(areq->iv + i * 4);
|
2024-06-11 20:26:44 +08:00
|
|
|
writesl(ss->base + SS_IV0 + i * 4, &v, 1);
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
writel(mode, ss->base + SS_CTL);
|
|
|
|
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
ileft = areq->cryptlen / 4;
|
|
|
|
oleft = areq->cryptlen / 4;
|
2015-07-17 22:39:41 +08:00
|
|
|
oi = 0;
|
|
|
|
oo = 0;
|
|
|
|
do {
|
2024-06-11 20:26:44 +08:00
|
|
|
if (ileft) {
|
|
|
|
sg_miter_start(&mi, areq->src, sg_nents(areq->src),
|
|
|
|
SG_MITER_FROM_SG | SG_MITER_ATOMIC);
|
|
|
|
if (pi)
|
|
|
|
sg_miter_skip(&mi, pi);
|
|
|
|
miter_err = sg_miter_next(&mi);
|
|
|
|
if (!miter_err || !mi.addr) {
|
|
|
|
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
|
|
|
|
err = -EINVAL;
|
|
|
|
goto release_ss;
|
|
|
|
}
|
|
|
|
todo = min(rx_cnt, ileft);
|
|
|
|
todo = min_t(size_t, todo, (mi.length - oi) / 4);
|
|
|
|
if (todo) {
|
|
|
|
ileft -= todo;
|
|
|
|
writesl(ss->base + SS_RXFIFO, mi.addr + oi, todo);
|
|
|
|
oi += todo * 4;
|
|
|
|
}
|
|
|
|
if (oi == mi.length) {
|
|
|
|
pi += mi.length;
|
|
|
|
oi = 0;
|
|
|
|
}
|
|
|
|
sg_miter_stop(&mi);
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
spaces = readl(ss->base + SS_FCSR);
|
|
|
|
rx_cnt = SS_RXFIFO_SPACES(spaces);
|
|
|
|
tx_cnt = SS_TXFIFO_SPACES(spaces);
|
|
|
|
|
2024-06-11 20:26:44 +08:00
|
|
|
sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
|
|
|
|
SG_MITER_TO_SG | SG_MITER_ATOMIC);
|
|
|
|
if (po)
|
|
|
|
sg_miter_skip(&mo, po);
|
|
|
|
miter_err = sg_miter_next(&mo);
|
|
|
|
if (!miter_err || !mo.addr) {
|
|
|
|
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
|
|
|
|
err = -EINVAL;
|
|
|
|
goto release_ss;
|
|
|
|
}
|
2024-06-11 20:08:33 +08:00
|
|
|
todo = min(tx_cnt, oleft);
|
|
|
|
todo = min_t(size_t, todo, (mo.length - oo) / 4);
|
2017-06-02 03:38:54 +08:00
|
|
|
if (todo) {
|
2015-07-17 22:39:41 +08:00
|
|
|
oleft -= todo;
|
|
|
|
readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
|
|
|
|
oo += todo * 4;
|
|
|
|
}
|
|
|
|
if (oo == mo.length) {
|
|
|
|
oo = 0;
|
2024-06-11 20:26:44 +08:00
|
|
|
po += mo.length;
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
2024-06-11 20:26:44 +08:00
|
|
|
sg_miter_stop(&mo);
|
2017-06-02 03:38:54 +08:00
|
|
|
} while (oleft);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
if (areq->iv) {
|
2015-07-17 22:39:41 +08:00
|
|
|
for (i = 0; i < 4 && i < ivsize / 4; i++) {
|
|
|
|
v = readl(ss->base + SS_IV0 + i * 4);
|
2017-06-02 03:39:01 +08:00
|
|
|
*(u32 *)(areq->iv + i * 4) = v;
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
release_ss:
|
|
|
|
writel(0, ss->base + SS_CTL);
|
2016-03-23 23:11:24 +08:00
|
|
|
spin_unlock_irqrestore(&ss->slock, flags);
|
2015-07-17 22:39:41 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:25:17 +08:00
|
|
|
|
|
|
|
static int noinline_for_stack sun4i_ss_cipher_poll_fallback(struct skcipher_request *areq)
|
|
|
|
{
|
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
|
|
|
|
SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, op->fallback_tfm);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
skcipher_request_set_sync_tfm(subreq, op->fallback_tfm);
|
|
|
|
skcipher_request_set_callback(subreq, areq->base.flags, NULL,
|
|
|
|
NULL);
|
|
|
|
skcipher_request_set_crypt(subreq, areq->src, areq->dst,
|
|
|
|
areq->cryptlen, areq->iv);
|
|
|
|
if (ctx->mode & SS_DECRYPTION)
|
|
|
|
err = crypto_skcipher_decrypt(subreq);
|
|
|
|
else
|
|
|
|
err = crypto_skcipher_encrypt(subreq);
|
|
|
|
skcipher_request_zero(subreq);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-07-17 22:39:41 +08:00
|
|
|
/* Generic function that support SG with size not multiple of 4 */
|
2017-06-02 03:39:01 +08:00
|
|
|
static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
2015-07-17 22:39:41 +08:00
|
|
|
struct sun4i_ss_ctx *ss = op->ss;
|
|
|
|
int no_chunk = 1;
|
|
|
|
struct scatterlist *in_sg = areq->src;
|
|
|
|
struct scatterlist *out_sg = areq->dst;
|
2017-06-02 03:39:01 +08:00
|
|
|
unsigned int ivsize = crypto_skcipher_ivsize(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
|
2019-04-18 16:17:35 +08:00
|
|
|
struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
|
|
|
|
struct sun4i_ss_alg_template *algt;
|
2015-07-17 22:39:41 +08:00
|
|
|
u32 mode = ctx->mode;
|
|
|
|
/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
|
|
|
|
u32 rx_cnt = SS_RX_DEFAULT;
|
|
|
|
u32 tx_cnt = 0;
|
|
|
|
u32 v;
|
|
|
|
u32 spaces;
|
2016-08-10 17:45:29 +08:00
|
|
|
int err = 0;
|
|
|
|
unsigned int i;
|
2017-06-02 03:39:01 +08:00
|
|
|
unsigned int ileft = areq->cryptlen;
|
|
|
|
unsigned int oleft = areq->cryptlen;
|
2015-07-17 22:39:41 +08:00
|
|
|
unsigned int todo;
|
|
|
|
struct sg_mapping_iter mi, mo;
|
2024-06-11 20:26:44 +08:00
|
|
|
unsigned long pi = 0, po = 0; /* progress for in and out */
|
|
|
|
bool miter_err;
|
2015-07-17 22:39:41 +08:00
|
|
|
unsigned int oi, oo; /* offset for in and out */
|
|
|
|
unsigned int ob = 0; /* offset in buf */
|
|
|
|
unsigned int obo = 0; /* offset in bufo*/
|
|
|
|
unsigned int obl = 0; /* length of data in bufo */
|
2016-03-23 23:11:24 +08:00
|
|
|
unsigned long flags;
|
2024-06-11 20:26:44 +08:00
|
|
|
bool need_fallback = false;
|
2015-07-17 22:39:41 +08:00
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
if (!areq->cryptlen)
|
2015-07-17 22:39:41 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!areq->src || !areq->dst) {
|
|
|
|
dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:17:35 +08:00
|
|
|
algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
|
|
|
|
if (areq->cryptlen % algt->alg.crypto.base.cra_blocksize)
|
|
|
|
need_fallback = true;
|
|
|
|
|
2015-07-17 22:39:41 +08:00
|
|
|
/*
|
|
|
|
* if we have only SGs with size multiple of 4,
|
|
|
|
* we can use the SS optimized function
|
|
|
|
*/
|
|
|
|
while (in_sg && no_chunk == 1) {
|
2024-06-11 20:26:44 +08:00
|
|
|
if ((in_sg->length | in_sg->offset) & 3u)
|
2015-07-17 22:39:41 +08:00
|
|
|
no_chunk = 0;
|
|
|
|
in_sg = sg_next(in_sg);
|
|
|
|
}
|
|
|
|
while (out_sg && no_chunk == 1) {
|
2024-06-11 20:26:44 +08:00
|
|
|
if ((out_sg->length | out_sg->offset) & 3u)
|
2015-07-17 22:39:41 +08:00
|
|
|
no_chunk = 0;
|
|
|
|
out_sg = sg_next(out_sg);
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:17:35 +08:00
|
|
|
if (no_chunk == 1 && !need_fallback)
|
2015-07-17 22:39:41 +08:00
|
|
|
return sun4i_ss_opti_poll(areq);
|
|
|
|
|
2019-06-17 21:25:17 +08:00
|
|
|
if (need_fallback)
|
|
|
|
return sun4i_ss_cipher_poll_fallback(areq);
|
2019-04-18 16:17:35 +08:00
|
|
|
|
2016-03-23 23:11:24 +08:00
|
|
|
spin_lock_irqsave(&ss->slock, flags);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
2024-06-11 20:26:44 +08:00
|
|
|
for (i = 0; i < op->keylen / 4; i++)
|
|
|
|
writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
if (areq->iv) {
|
2015-07-17 22:39:41 +08:00
|
|
|
for (i = 0; i < 4 && i < ivsize / 4; i++) {
|
2017-06-02 03:39:01 +08:00
|
|
|
v = *(u32 *)(areq->iv + i * 4);
|
2024-06-11 20:26:44 +08:00
|
|
|
writesl(ss->base + SS_IV0 + i * 4, &v, 1);
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
writel(mode, ss->base + SS_CTL);
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
ileft = areq->cryptlen;
|
|
|
|
oleft = areq->cryptlen;
|
2015-07-17 22:39:41 +08:00
|
|
|
oi = 0;
|
|
|
|
oo = 0;
|
|
|
|
|
2017-06-02 03:38:54 +08:00
|
|
|
while (oleft) {
|
|
|
|
if (ileft) {
|
2024-06-11 20:26:44 +08:00
|
|
|
sg_miter_start(&mi, areq->src, sg_nents(areq->src),
|
|
|
|
SG_MITER_FROM_SG | SG_MITER_ATOMIC);
|
|
|
|
if (pi)
|
|
|
|
sg_miter_skip(&mi, pi);
|
|
|
|
miter_err = sg_miter_next(&mi);
|
|
|
|
if (!miter_err || !mi.addr) {
|
|
|
|
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
|
|
|
|
err = -EINVAL;
|
|
|
|
goto release_ss;
|
|
|
|
}
|
2015-07-17 22:39:41 +08:00
|
|
|
/*
|
|
|
|
* todo is the number of consecutive 4byte word that we
|
|
|
|
* can read from current SG
|
|
|
|
*/
|
2024-06-11 20:08:33 +08:00
|
|
|
todo = min(rx_cnt, ileft / 4);
|
|
|
|
todo = min_t(size_t, todo, (mi.length - oi) / 4);
|
2017-06-02 03:38:54 +08:00
|
|
|
if (todo && !ob) {
|
2015-07-17 22:39:41 +08:00
|
|
|
writesl(ss->base + SS_RXFIFO, mi.addr + oi,
|
|
|
|
todo);
|
|
|
|
ileft -= todo * 4;
|
|
|
|
oi += todo * 4;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* not enough consecutive bytes, so we need to
|
|
|
|
* linearize in buf. todo is in bytes
|
|
|
|
* After that copy, if we have a multiple of 4
|
|
|
|
* we need to be able to write all buf in one
|
|
|
|
* pass, so it is why we min() with rx_cnt
|
|
|
|
*/
|
2024-06-11 20:08:33 +08:00
|
|
|
todo = min(rx_cnt * 4 - ob, ileft);
|
|
|
|
todo = min_t(size_t, todo, mi.length - oi);
|
2024-06-11 20:26:44 +08:00
|
|
|
memcpy(ss->buf + ob, mi.addr + oi, todo);
|
2015-07-17 22:39:41 +08:00
|
|
|
ileft -= todo;
|
|
|
|
oi += todo;
|
|
|
|
ob += todo;
|
2017-06-02 03:38:54 +08:00
|
|
|
if (!(ob % 4)) {
|
2024-06-11 20:26:44 +08:00
|
|
|
writesl(ss->base + SS_RXFIFO, ss->buf,
|
2015-07-17 22:39:41 +08:00
|
|
|
ob / 4);
|
|
|
|
ob = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (oi == mi.length) {
|
2024-06-11 20:26:44 +08:00
|
|
|
pi += mi.length;
|
2015-07-17 22:39:41 +08:00
|
|
|
oi = 0;
|
|
|
|
}
|
2024-06-11 20:26:44 +08:00
|
|
|
sg_miter_stop(&mi);
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
spaces = readl(ss->base + SS_FCSR);
|
|
|
|
rx_cnt = SS_RXFIFO_SPACES(spaces);
|
|
|
|
tx_cnt = SS_TXFIFO_SPACES(spaces);
|
|
|
|
|
2017-06-02 03:38:54 +08:00
|
|
|
if (!tx_cnt)
|
2015-07-17 22:39:41 +08:00
|
|
|
continue;
|
2024-06-11 20:26:44 +08:00
|
|
|
sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
|
|
|
|
SG_MITER_TO_SG | SG_MITER_ATOMIC);
|
|
|
|
if (po)
|
|
|
|
sg_miter_skip(&mo, po);
|
|
|
|
miter_err = sg_miter_next(&mo);
|
|
|
|
if (!miter_err || !mo.addr) {
|
|
|
|
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
|
|
|
|
err = -EINVAL;
|
|
|
|
goto release_ss;
|
|
|
|
}
|
2015-07-17 22:39:41 +08:00
|
|
|
/* todo in 4bytes word */
|
2024-06-11 20:08:33 +08:00
|
|
|
todo = min(tx_cnt, oleft / 4);
|
|
|
|
todo = min_t(size_t, todo, (mo.length - oo) / 4);
|
2024-06-11 20:26:44 +08:00
|
|
|
|
2017-06-02 03:38:54 +08:00
|
|
|
if (todo) {
|
2015-07-17 22:39:41 +08:00
|
|
|
readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
|
|
|
|
oleft -= todo * 4;
|
|
|
|
oo += todo * 4;
|
|
|
|
if (oo == mo.length) {
|
2024-06-11 20:26:44 +08:00
|
|
|
po += mo.length;
|
2015-07-17 22:39:41 +08:00
|
|
|
oo = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* read obl bytes in bufo, we read at maximum for
|
|
|
|
* emptying the device
|
|
|
|
*/
|
2024-06-11 20:26:44 +08:00
|
|
|
readsl(ss->base + SS_TXFIFO, ss->bufo, tx_cnt);
|
2015-07-17 22:39:41 +08:00
|
|
|
obl = tx_cnt * 4;
|
|
|
|
obo = 0;
|
|
|
|
do {
|
|
|
|
/*
|
|
|
|
* how many bytes we can copy ?
|
|
|
|
* no more than remaining SG size
|
|
|
|
* no more than remaining buffer
|
|
|
|
* no need to test against oleft
|
|
|
|
*/
|
2024-06-11 20:08:33 +08:00
|
|
|
todo = min_t(size_t,
|
|
|
|
mo.length - oo, obl - obo);
|
2024-06-11 20:26:44 +08:00
|
|
|
memcpy(mo.addr + oo, ss->bufo + obo, todo);
|
2015-07-17 22:39:41 +08:00
|
|
|
oleft -= todo;
|
|
|
|
obo += todo;
|
|
|
|
oo += todo;
|
|
|
|
if (oo == mo.length) {
|
2024-06-11 20:26:44 +08:00
|
|
|
po += mo.length;
|
2015-07-17 22:39:41 +08:00
|
|
|
sg_miter_next(&mo);
|
|
|
|
oo = 0;
|
|
|
|
}
|
|
|
|
} while (obo < obl);
|
|
|
|
/* bufo must be fully used here */
|
|
|
|
}
|
2024-06-11 20:26:44 +08:00
|
|
|
sg_miter_stop(&mo);
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
2017-06-02 03:39:01 +08:00
|
|
|
if (areq->iv) {
|
2015-07-17 22:39:41 +08:00
|
|
|
for (i = 0; i < 4 && i < ivsize / 4; i++) {
|
|
|
|
v = readl(ss->base + SS_IV0 + i * 4);
|
2017-06-02 03:39:01 +08:00
|
|
|
*(u32 *)(areq->iv + i * 4) = v;
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
release_ss:
|
|
|
|
writel(0, ss->base + SS_CTL);
|
2016-03-23 23:11:24 +08:00
|
|
|
spin_unlock_irqrestore(&ss->slock, flags);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CBC AES */
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ECB AES */
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_ecb_aes_decrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CBC DES */
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_cbc_des_decrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ECB DES */
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_ecb_des_decrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CBC 3DES */
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_cbc_des3_decrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ECB 3DES */
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_ecb_des3_decrypt(struct skcipher_request *areq)
|
2015-07-17 22:39:41 +08:00
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
|
|
|
struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
|
|
|
|
op->keymode;
|
|
|
|
return sun4i_ss_cipher_poll(areq);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
|
|
|
|
{
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
|
|
|
|
struct sun4i_ss_alg_template *algt;
|
2019-04-18 16:17:35 +08:00
|
|
|
const char *name = crypto_tfm_alg_name(tfm);
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
memset(op, 0, sizeof(struct sun4i_tfm_ctx));
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
algt = container_of(tfm->__crt_alg, struct sun4i_ss_alg_template,
|
|
|
|
alg.crypto.base);
|
2015-07-17 22:39:41 +08:00
|
|
|
op->ss = algt->ss;
|
|
|
|
|
2017-06-02 03:39:01 +08:00
|
|
|
crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
|
|
|
|
sizeof(struct sun4i_cipher_req_ctx));
|
2015-07-17 22:39:41 +08:00
|
|
|
|
2019-04-18 16:17:35 +08:00
|
|
|
op->fallback_tfm = crypto_alloc_sync_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
|
|
|
|
if (IS_ERR(op->fallback_tfm)) {
|
|
|
|
dev_err(op->ss->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
|
|
|
|
name, PTR_ERR(op->fallback_tfm));
|
|
|
|
return PTR_ERR(op->fallback_tfm);
|
|
|
|
}
|
|
|
|
|
2015-07-17 22:39:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:17:35 +08:00
|
|
|
void sun4i_ss_cipher_exit(struct crypto_tfm *tfm)
|
|
|
|
{
|
|
|
|
struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
|
|
|
|
crypto_free_sync_skcipher(op->fallback_tfm);
|
|
|
|
}
|
|
|
|
|
2015-07-17 22:39:41 +08:00
|
|
|
/* check and set the AES key, prepare the mode to be used */
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
2015-07-17 22:39:41 +08:00
|
|
|
unsigned int keylen)
|
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
2015-07-17 22:39:41 +08:00
|
|
|
struct sun4i_ss_ctx *ss = op->ss;
|
|
|
|
|
|
|
|
switch (keylen) {
|
|
|
|
case 128 / 8:
|
|
|
|
op->keymode = SS_AES_128BITS;
|
|
|
|
break;
|
|
|
|
case 192 / 8:
|
|
|
|
op->keymode = SS_AES_192BITS;
|
|
|
|
break;
|
|
|
|
case 256 / 8:
|
|
|
|
op->keymode = SS_AES_256BITS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
|
2017-06-02 03:39:01 +08:00
|
|
|
crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
|
2015-07-17 22:39:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
op->keylen = keylen;
|
|
|
|
memcpy(op->key, key, keylen);
|
2019-04-18 16:17:35 +08:00
|
|
|
|
|
|
|
crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
|
|
|
|
crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
|
|
|
|
|
|
|
|
return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen);
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check and set the DES key, prepare the mode to be used */
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
2015-07-17 22:39:41 +08:00
|
|
|
unsigned int keylen)
|
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
2019-08-15 17:01:04 +08:00
|
|
|
int err;
|
2015-07-17 22:39:41 +08:00
|
|
|
|
2019-08-15 17:01:04 +08:00
|
|
|
err = verify_skcipher_des_key(tfm, key);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
op->keylen = keylen;
|
|
|
|
memcpy(op->key, key, keylen);
|
2019-04-18 16:17:35 +08:00
|
|
|
|
|
|
|
crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
|
|
|
|
crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
|
|
|
|
|
|
|
|
return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen);
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check and set the 3DES key, prepare the mode to be used */
|
2017-06-02 03:39:01 +08:00
|
|
|
int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
2015-07-17 22:39:41 +08:00
|
|
|
unsigned int keylen)
|
|
|
|
{
|
2017-06-02 03:39:01 +08:00
|
|
|
struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
|
2019-04-11 16:51:19 +08:00
|
|
|
int err;
|
|
|
|
|
2019-08-15 17:01:04 +08:00
|
|
|
err = verify_skcipher_des3_key(tfm, key);
|
|
|
|
if (err)
|
2019-04-11 16:51:19 +08:00
|
|
|
return err;
|
2015-07-17 22:39:41 +08:00
|
|
|
|
|
|
|
op->keylen = keylen;
|
|
|
|
memcpy(op->key, key, keylen);
|
2019-04-18 16:17:35 +08:00
|
|
|
|
|
|
|
crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
|
|
|
|
crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
|
|
|
|
|
|
|
|
return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen);
|
|
|
|
|
2015-07-17 22:39:41 +08:00
|
|
|
}
|