You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ut...@apache.org on 2021/02/19 09:35:39 UTC

[mynewt-core] 02/02: hw: drivers: hash_kinetis: fix small buffer hashing

This is an automated email from the ASF dual-hosted git repository.

utzig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 791b0cf547ca9cb234cf39f0fdc4144970389db4
Author: Fabio Utzig <ut...@apache.org>
AuthorDate: Thu Feb 18 20:32:01 2021 -0300

    hw: drivers: hash_kinetis: fix small buffer hashing
    
    Hashing a message with multiple blocks of length<=64 (SHA-256 block
    length) was broken due to proper usage of the mmCAU padding. This commit
    fixes the handling of the pad data.
    
    Signed-off-by: Fabio Utzig <ut...@apache.org>
---
 hw/drivers/hash/hash_kinetis/src/hash_kinetis.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/hw/drivers/hash/hash_kinetis/src/hash_kinetis.c b/hw/drivers/hash/hash_kinetis/src/hash_kinetis.c
index 170e253..a9fbb13 100644
--- a/hw/drivers/hash/hash_kinetis/src/hash_kinetis.c
+++ b/hw/drivers/hash/hash_kinetis/src/hash_kinetis.c
@@ -68,29 +68,45 @@ kinetis_hash_start(struct hash_dev *hash, void *ctx, uint16_t algo)
     return 0;
 }
 
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
 static int
 kinetis_hash_update(struct hash_dev *hash, void *ctx, uint16_t algo,
-        const void *inbuf, uint32_t inlen)
+                    const void *inbuf, uint32_t inlen)
 {
     uint32_t i;
     uint32_t remain;
     struct hash_sha256_context *sha256ctx;
     uint8_t *u8p;
+    uint32_t len;
 
     sha256ctx = (struct hash_sha256_context *)ctx;
     i = 0;
     remain = inlen;
     u8p = (uint8_t *)inbuf;
 
+    if (sha256ctx->remain > 0) {
+        len = MIN(SHA256_BLOCK_LEN - sha256ctx->remain, inlen);
+        memcpy(&sha256ctx->pad[sha256ctx->remain], &u8p[0], len);
+        sha256ctx->remain += len;
+        remain -= len;
+
+        if (sha256ctx->remain == SHA256_BLOCK_LEN) {
+            cau_sha256_hash_n(sha256ctx->pad, 1, sha256ctx->output);
+            sha256ctx->remain = 0;
+            sha256ctx->len += SHA256_BLOCK_LEN;
+            i = len;
+        }
+    }
+
     while (remain >= SHA256_BLOCK_LEN) {
         cau_sha256_hash_n((const unsigned char *)&u8p[i], 1,
                 sha256ctx->output);
         remain -= SHA256_BLOCK_LEN;
         i += SHA256_BLOCK_LEN;
+        sha256ctx->len += SHA256_BLOCK_LEN;
     }
 
-    sha256ctx->len += i;
-
     if (remain > 0) {
         memcpy(sha256ctx->pad, &u8p[i], remain);
         sha256ctx->remain = remain;