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:37 UTC

[mynewt-core] branch master updated (fb33f18 -> 791b0cf)

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

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


    from fb33f18  hw: driver: crypto: Mbed TLS alt improvements
     new d806377  apps: hash_test: add small varlen inputs test
     new 791b0cf  hw: drivers: hash_kinetis: fix small buffer hashing

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 apps/hash_test/src/main.c                       | 68 +++++++++++++++++++++++++
 hw/drivers/hash/hash_kinetis/src/hash_kinetis.c | 22 ++++++--
 2 files changed, 87 insertions(+), 3 deletions(-)


[mynewt-core] 01/02: apps: hash_test: add small varlen inputs test

Posted by ut...@apache.org.
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 d806377ca6eca58f71d86c9e6407899316ea76aa
Author: Fabio Utzig <ut...@apache.org>
AuthorDate: Thu Feb 18 20:30:40 2021 -0300

    apps: hash_test: add small varlen inputs test
    
    Add a new test that hashes multiple small variable length strings into a
    digest, where each string is smaller than SHA-256 block length.
    
    Signed-off-by: Fabio Utzig <ut...@apache.org>
---
 apps/hash_test/src/main.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/apps/hash_test/src/main.c b/apps/hash_test/src/main.c
index 88c3a2b..9366c40 100755
--- a/apps/hash_test/src/main.c
+++ b/apps/hash_test/src/main.c
@@ -254,6 +254,71 @@ run_stream_test(struct hash_dev *hash)
     }
 }
 
+void
+run_varlength_sha256_test(struct hash_dev *hash)
+{
+    struct hash_generic_context ctx;
+    int i;
+    int rc;
+    uint16_t algo;
+    char *string;
+    uint8_t outbuf[SHA256_DIGEST_LEN];
+
+    char *strings[] = {
+        "All that is gold does not glitter,",
+        "Not all those who wander are lost;",
+        "The old that is strong does not wither,",
+        "Deep roots are not reached by the frost.",
+        "From the ashes a fire shall be woken,",
+        "A light from the shadows shall spring;",
+        "Renewed shall be blade that was broken,",
+        "The crownless again shall be king.",
+    };
+
+    uint8_t digest[SHA256_DIGEST_LEN] = "\x83\x6c\x57\x9d\x0c\x13\xec\x71"
+                                        "\x9f\x1d\x38\xf7\x34\xeb\x09\x4f"
+                                        "\x83\x6e\xe9\x06\xc7\xda\x78\x71"
+                                        "\x04\x87\x2b\xcf\x2d\x09\x84\x3d";
+
+    algo = HASH_ALGO_SHA256;
+
+    if (!hash_has_support(hash, algo)) {
+        printf("unsupported\n");
+        return;
+    }
+
+    rc = hash_custom_start(hash, &ctx, algo);
+    if (rc) {
+        printf("failure\n");
+        return;
+    }
+
+    for (i = 0; i < sizeof strings / sizeof strings[0]; i++) {
+        string = strings[i];
+        printf("  %s: ", string);
+
+        rc = hash_custom_update(hash, &ctx, algo, string, strlen(string));
+        if (rc) {
+            printf("failure\n");
+            return;
+        } else {
+            printf("ok\n");
+        }
+    }
+
+    rc = hash_custom_finish(hash, &ctx, algo, outbuf);
+    if (rc) {
+        printf("failure\n");
+        return;
+    }
+
+    if (memcmp(outbuf, digest, SHA256_DIGEST_LEN) == 0) {
+        printf("digest: ok\n");
+    } else {
+        printf("digest: invalid\n");
+    }
+}
+
 typedef void (* hash_start_func_t)(void *, void *);
 typedef void (* hash_update_func_t)(void *, const uint8_t *, uint32_t);
 typedef void (* hash_finish_func_t)(void *, uint8_t *);
@@ -429,6 +494,9 @@ main(void)
     printf("\n=== SHA-256 of 1000000 'a' letters ===\n");
     run_stream_test(hash);
 
+    printf("\n=== SHA-256 of variable length strings ===\n");
+    run_varlength_sha256_test(hash);
+
     mbedtls_sha256_init(&mbed_sha256);
     tc_sha256_init(&tc_sha256);
 


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

Posted by ut...@apache.org.
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;