Section 16.13

TinyCrypt

Full name: TinyCrypt: a coined name from tiny and crypt, short for cryptography; it is not an abbreviation.

Definition

TinyCrypt is a small library of cryptographic algorithms §14.1, written in C for devices with little memory and first published by Intel. It implements a fixed set of standard algorithms, among them SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), HMAC §5.8 (hash-based message authentication code), AES (Advanced Encryption Standard) with 128-bit keys in several modes, and elliptic-curve §19.1.5 key agreement and signatures on the curve P-256. A build can include the algorithms it uses and their required components. The library uses no dynamic memory allocation. Intel no longer maintains it.

The idea

A microcontroller §6.1 with 256 KiB of flash memory §6.6 must authenticate firmware updates with HMAC-SHA-256 §5.9 and encrypt messages with AES. A general-purpose cryptographic library brings more code than this device needs, a heap for memory allocated at run time, and many options to configure. TinyCrypt’s design answers that: each algorithm in its own source file, few dependencies between algorithms, no memory allocated at run time, and a short interface per algorithm.

Naming the library answers little, though. A library is a menu; the device image is what was served. What decides behavior is which copy of the source was used, with which fixes; which algorithms were compiled; which functions the linker §12.4 placed in the image; and how the calling code handles each function’s buffers and return values. The picture stops at cost: a dish nobody ordered costs nothing, while compiled code that is never called can still occupy the image if the linker cannot tell that it is unused.

How it works

The library provides:

The SHA-256 interface keeps its working data in a state structure that the caller allocates. The call tc_sha256_init(&s) prepares the state s; tc_sha256_update(&s, data, len) adds len bytes; and tc_sha256_final(digest, &s) writes the 32-byte result to digest. Three conventions need care:

Tying the name to a device image takes five checks:

  1. Version: identify the exact source, an upstream release or a copy kept, and possibly changed, inside another project such as an operating system.

  2. Configuration: find which source files the build compiled. Source files include sha256.c or hmac.c, and build options can select both an algorithm and the components it needs.

  3. Linking: inspect the linker’s map of the build to see which input sections it placed in the final image. If symbols are available, a defined symbol for a function such as tc_sha256_final helps locate it.

  4. Calls: check which function each caller actually calls, since another library in the same image may provide the same algorithm.

  5. Testing: run known-answer tests, published pairs of input and output, on the built code.

Worked example

A known-answer test of SHA-256 uses the 3-byte input abc, the bytes 0x61 0x62 0x63, from the examples that NIST publishes for the SHA-256 standard [7, 464]. SHA-256 processes 64-byte blocks. Padding appends the byte 0x80, then zero bytes, then the input’s length in bits, 3×8=243 \times 8 = 24, as an 8-byte number. That takes 64−3−1−8=5264 - 3 - 1 - 8 = 52 zero bytes, so the padded input is exactly one block. The expected digest, 32 bytes, is:

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

The test calls tc_sha256_init, then tc_sha256_update with the 3 bytes, then tc_sha256_final with a 32-byte buffer. It passes if every call returned 1 and the buffer equals the expected digest.

Padding boundaries need their own tests. An input of 55 bytes needs 55+1+8=6455 + 1 + 8 = 64 bytes, one block. An input of 56 bytes needs 56+1+8=6556 + 1 + 8 = 65 bytes, more than one block, so it is padded to two blocks, 128 bytes. A test set that never crosses this boundary never runs the two-block padding path.

Two calling mistakes show what a correct algorithm cannot prevent. A caller that keeps a 16-byte tag declares uint8_t tag[16] and passes it to tc_sha256_final, which writes 32 bytes: the last 32−16=1632 - 16 = 16 bytes land in whatever memory follows the array §14.5, a buffer overflow §4.4. A caller that asks tc_hmac_final for a 16-byte tag gets the return value 0 and an unwritten buffer. Code written for the 0-means-success convention, if (tc_hmac_final(tag, 16, &h) == 0), treats that failure as success and sends uninitialized bytes as the tag.

Stated precisely

The contract of tc_sha256_final(digest, s) is: if s points to a state prepared by tc_sha256_init and fed by tc_sha256_update, and digest points to at least 32 writable bytes, the call writes the SHA-256 digest of all the bytes passed to the update calls, in order, to digest[0] to digest[31], and returns 1. If either pointer is null, it returns 0. If the buffer is shorter than 32 bytes, the call writes past its end, and the program’s behavior is undefined.

A known-answer test checks one equation, f(x0)=y0f(x_0) = y_0, for one input x0x_0 and its published output y0y_0. Passing it shows that the compiled code agrees with the standard on x0x_0; it shows nothing about any other input. A set of tests that covers every padding case raises confidence but remains a finite sample. Known-answer tests also say nothing about side channels §1.29, effects of a computation other than its output, such as its running time, from which an attacker may learn secrets.

Limits and common mistakes

The three SHA-256 calls use TC_CRYPTO_SUCCESS (1) for success. Read the contract for each other function. For example, TinyCrypt’s byte comparison helper, _compare, returns 0 when the inputs are equal and a nonzero value when they differ.

Intel has stopped development: the repository states that Intel no longer provides maintenance, bug fixes or new releases. Fixes exist only in forks, so the copy in use decides which bugs remain.

The algorithm set is small: AES only with 128-bit keys, elliptic curves only P-256. Other algorithms need another library, such as Mbed TLS §12.20 (TLS: Transport Layer Security).

Elliptic-curve key generation and ECDSA signing need random numbers from a source that the platform supplies. With a weak source the private key can leak: reusing the same secret signing value for two different message digests can expose the private key.

A notice in the source repository tells you a library is available. To establish which parts reached the device image, trace the linked build back to its inputs. A linker map can identify included object files and sections even when the distributed image has had its symbol table §12.8 removed. Inlining can also remove a separately named function while retaining its instructions.

A correct algorithm does not make a correct protocol. Which bytes are authenticated, how keys are stored, and whether a received tag is checked with a constant-time comparison §4.33 are decided outside the library.

Questions answered

Which algorithm, build configuration, and implementation version are relevant?

The relevant ones are those of the device image that actually runs: the algorithms whose code the build compiled and linked and the program calls, with the build configuration used for that image, from the exact copy of the source it was built from. A library’s name, its repository’s list of algorithms or a mention in a third-party notice settles none of these.

To establish them for TinyCrypt:

  1. version: record where the copy came from, an upstream release or the commit of a fork kept inside another project, because Intel no longer maintains TinyCrypt and forks differ;

  2. configuration: list the build options, the source files compiled and their dependencies;

  3. linked code: inspect the matching linker map and any retained symbols for the functions used, such as tc_sha256_final, and check that each caller calls TinyCrypt and not another library that offers the same algorithm;

  4. use: for each call, check the buffer lengths, such as the 32 bytes that tc_sha256_final always writes, and interpret the return value according to that function’s contract;

  5. correctness: run known-answer tests on that same build.

Related entries: Linker (§12.4), Dead-code elimination (§12.11), Mbed TLS (§12.20), HMAC (§5.8), Constant-time cryptographic implementation (§5.36), Test oracle (§10.5), SHA-256 (§5.2), Symbol table (§12.8). Further reading: [7, 62, 464].