pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Threads
      • Mutexes
      • Semaphores
      • Message queues
      • Poll groups
      • Timers
      • Work queues
      • Delayed work
      • Heaps
      • Time
      • Idle states
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • CRC
      • SHA-256
      • AES
      • Files
      • Settings
      • Translations
      • Input events
      • Loader
      • Log levels
      • Profiler
      • Shell commands
      • Task watchdog
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • GPU
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Rust
      • Kernel benchmark
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

SHA-256

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.

Run:

pbl samples samples/subsys/crypto/sha256

Boards:

every board

Sources

src/main.c
#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;
}

Output

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.

Overview

  • Sources
  • Output