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

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

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);