Commit 25bc9551 authored by Antoine Tenart's avatar Antoine Tenart Committed by Herbert Xu

crypto: inside-secure - improve the counter computation

A counter is given to the engine when finishing hash computation. It
currently uses the blocksize while it counts the number of 64 bytes
blocks given to the engine. This works well for all algorithms so far,
as SHA1, SHA224 and SHA256 all have a blocksize of 64 bytes, but others
algorithms such as SHA512 wouldn't work.
Signed-off-by: default avatarAntoine Tenart <antoine.tenart@bootlin.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent cda3e73a
...@@ -327,6 +327,11 @@ struct safexcel_context_record { ...@@ -327,6 +327,11 @@ struct safexcel_context_record {
#define CONTEXT_CONTROL_COUNTER_MODE BIT(10) #define CONTEXT_CONTROL_COUNTER_MODE BIT(10)
#define CONTEXT_CONTROL_HASH_STORE BIT(19) #define CONTEXT_CONTROL_HASH_STORE BIT(19)
/* The hash counter given to the engine in the context has a granularity of
* 64 bits.
*/
#define EIP197_COUNTER_BLOCK_SIZE 64
/* EIP197_CS_RAM_CTRL */ /* EIP197_CS_RAM_CTRL */
#define EIP197_TRC_ENABLE_0 BIT(4) #define EIP197_TRC_ENABLE_0 BIT(4)
#define EIP197_TRC_ENABLE_1 BIT(5) #define EIP197_TRC_ENABLE_1 BIT(5)
......
...@@ -72,8 +72,7 @@ static void safexcel_hash_token(struct safexcel_command_desc *cdesc, ...@@ -72,8 +72,7 @@ static void safexcel_hash_token(struct safexcel_command_desc *cdesc,
static void safexcel_context_control(struct safexcel_ahash_ctx *ctx, static void safexcel_context_control(struct safexcel_ahash_ctx *ctx,
struct safexcel_ahash_req *req, struct safexcel_ahash_req *req,
struct safexcel_command_desc *cdesc, struct safexcel_command_desc *cdesc,
unsigned int digestsize, unsigned int digestsize)
unsigned int blocksize)
{ {
int i; int i;
...@@ -107,7 +106,8 @@ static void safexcel_context_control(struct safexcel_ahash_ctx *ctx, ...@@ -107,7 +106,8 @@ static void safexcel_context_control(struct safexcel_ahash_ctx *ctx,
ctx->base.ctxr->data[i] = cpu_to_le32(req->state[i]); ctx->base.ctxr->data[i] = cpu_to_le32(req->state[i]);
if (req->finish) if (req->finish)
ctx->base.ctxr->data[i] = cpu_to_le32(req->processed / blocksize); ctx->base.ctxr->data[i] =
cpu_to_le32(req->processed / EIP197_COUNTER_BLOCK_SIZE);
} }
} else if (req->digest == CONTEXT_CONTROL_DIGEST_HMAC) { } else if (req->digest == CONTEXT_CONTROL_DIGEST_HMAC) {
cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(2 * req->state_sz / sizeof(u32)); cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(2 * req->state_sz / sizeof(u32));
...@@ -282,8 +282,7 @@ static int safexcel_ahash_send_req(struct crypto_async_request *async, int ring, ...@@ -282,8 +282,7 @@ static int safexcel_ahash_send_req(struct crypto_async_request *async, int ring,
send_command: send_command:
/* Setup the context options */ /* Setup the context options */
safexcel_context_control(ctx, req, first_cdesc, req->state_sz, safexcel_context_control(ctx, req, first_cdesc, req->state_sz);
crypto_ahash_blocksize(ahash));
/* Add the token */ /* Add the token */
safexcel_hash_token(first_cdesc, len, req->state_sz); safexcel_hash_token(first_cdesc, len, req->state_sz);
...@@ -335,7 +334,6 @@ static inline bool safexcel_ahash_needs_inv_get(struct ahash_request *areq) ...@@ -335,7 +334,6 @@ static inline bool safexcel_ahash_needs_inv_get(struct ahash_request *areq)
{ {
struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq)); struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
struct safexcel_ahash_req *req = ahash_request_ctx(areq); struct safexcel_ahash_req *req = ahash_request_ctx(areq);
struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
unsigned int state_w_sz = req->state_sz / sizeof(u32); unsigned int state_w_sz = req->state_sz / sizeof(u32);
int i; int i;
...@@ -344,7 +342,7 @@ static inline bool safexcel_ahash_needs_inv_get(struct ahash_request *areq) ...@@ -344,7 +342,7 @@ static inline bool safexcel_ahash_needs_inv_get(struct ahash_request *areq)
return true; return true;
if (ctx->base.ctxr->data[state_w_sz] != if (ctx->base.ctxr->data[state_w_sz] !=
cpu_to_le32(req->processed / crypto_ahash_blocksize(ahash))) cpu_to_le32(req->processed / EIP197_COUNTER_BLOCK_SIZE))
return true; return true;
return false; return false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment