This sample hashes the two example messages of FIPS 180-4 with SHA-256, one of them two blocks long, then a thousand bytes both in one call and fed in uneven chunks: the context keeps a partial block until the next update completes it, so both digests are the same.
Boards with a hash accelerator use it for long runs of blocks, and software for the rest.
pbl samples samples/subsys/crypto/sha256
every board
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <pbl/crypto/sha256.h>
#include <pbl/util/math.h>
#define DATA_LEN 1000U
static uint8_t s_data[DATA_LEN];
static void prv_print(const char *what, const uint8_t digest[PBL_SHA256_DIGEST_SIZE]) {
printf("sha256: %s: ", what);
for (size_t i = 0; i < PBL_SHA256_DIGEST_SIZE; i++) {
printf("%02x", digest[i]);
}
printf("\n");
}
int main(void) {
static const char two_blocks[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
uint8_t digest[PBL_SHA256_DIGEST_SIZE], chunked[PBL_SHA256_DIGEST_SIZE];
struct pbl_sha256_ctx ctx;
size_t off = 0, chunk = 1;
pbl_sha256("abc", 3, digest);
prv_print("\"abc\"", digest);
pbl_sha256(two_blocks, strlen(two_blocks), digest);
prv_print("56 bytes, two blocks", digest);
for (size_t i = 0; i < DATA_LEN; i++) {
s_data[i] = (uint8_t)(i * 7U);
}
pbl_sha256(s_data, DATA_LEN, digest);
/* Partial blocks are kept in the context until the next update completes them. */
pbl_sha256_init(&ctx);
while (off < DATA_LEN) {
size_t len = MIN(chunk, DATA_LEN - off);
pbl_sha256_update(&ctx, &s_data[off], len);
off += len;
chunk = chunk * 7U % 97U + 1U;
}
pbl_sha256_final(&ctx, chunked);
printf("sha256: %u bytes in uneven chunks, the same as in one go: %s\n", DATA_LEN,
memcmp(digest, chunked, sizeof(digest)) == 0 ? "yes" : "no");
printf("Sample done\n");
return 0;
}
sha256: "abc": ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
sha256: 56 bytes, two blocks: 248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1
sha256: 1000 bytes in uneven chunks, the same as in one go: yes
Sample done
Generated from PebbleOS 7753e801d. This page is maintained in the pebbleos repository: docs/samples/subsys/crypto/sha256.md.