Skip to content

Commit 2d73e1b

Browse files
committed
crypto: atmel-sha: Add zero length message digest support for hmac
Add softare padding to hmac-sha digest for zero length messages. Using the atmel_sha_fill_padding() to fill the buffer with a padded empty message with a length of the block size. Create a temporary scatter list from the padded buffer to pass into the data processing functions. Signed-off-by: Ryan Wanner <Ryan.Wanner@microchip.com>
1 parent 2cb8e62 commit 2d73e1b

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

drivers/crypto/atmel-sha.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,14 +1948,32 @@ static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
19481948
struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
19491949
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
19501950
struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1951+
struct scatterlist *sgbuf;
19511952
size_t hs = ctx->hash_size;
19521953
size_t i, num_words = hs / sizeof(u32);
19531954
bool use_dma = false;
19541955
u32 mr;
19551956

19561957
/* Special case for empty message. */
1957-
if (!req->nbytes)
1958-
return atmel_sha_complete(dd, -EINVAL); // TODO:
1958+
if (!req->nbytes) {
1959+
req->nbytes = 0;
1960+
ctx->bufcnt = 0;
1961+
ctx->digcnt[0] = 0;
1962+
ctx->digcnt[1] = 0;
1963+
switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
1964+
case SHA_FLAGS_SHA1:
1965+
case SHA_FLAGS_SHA224:
1966+
case SHA_FLAGS_SHA256:
1967+
atmel_sha_fill_padding(ctx, 64);
1968+
break;
1969+
1970+
case SHA_FLAGS_SHA384:
1971+
case SHA_FLAGS_SHA512:
1972+
atmel_sha_fill_padding(ctx, 128);
1973+
break;
1974+
}
1975+
sg_init_one(&dd->tmp, ctx->buffer, ctx->bufcnt);
1976+
}
19591977

19601978
/* Check DMA threshold and alignment. */
19611979
if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
@@ -1985,12 +2003,20 @@ static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
19852003

19862004
atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
19872005

2006+
/* Special case for empty message. */
2007+
if (!req->nbytes) {
2008+
sgbuf = &dd->tmp;
2009+
req->nbytes = ctx->bufcnt;
2010+
} else {
2011+
sgbuf = req->src;
2012+
}
2013+
19882014
/* Process data. */
19892015
if (use_dma)
1990-
return atmel_sha_dma_start(dd, req->src, req->nbytes,
2016+
return atmel_sha_dma_start(dd, sgbuf, req->nbytes,
19912017
atmel_sha_hmac_final_done);
19922018

1993-
return atmel_sha_cpu_start(dd, req->src, req->nbytes, false, true,
2019+
return atmel_sha_cpu_start(dd, sgbuf, req->nbytes, false, true,
19942020
atmel_sha_hmac_final_done);
19952021
}
19962022

0 commit comments

Comments
 (0)