Memory requirements

RAM and flash memories are often the most critical resources of a microcontroller. A larger library footprint can even require a switch to a more expensive microcontroller model with more memory.

ocrypto is very memory-efficient, both regarding code size and RAM usage. For example, the crypto code needed for a HomeKit Light Bulb profile implementation requires the following memory for a Cortex-M4F core (all numbers in bytes):

  • code size: 18,600
  • static variables: 0
  • stack size: 3,236 

In order to make memory consumption (and speed) predictable, the ocrypto library does not use any dynamic memory allocation. Neither does it allocate any static variables. By default, its crypto functions only use the stack.

There are applications where stack real estate comes at a premium, e.g., when using an RTOS and many threads with only 1 KB of stack space for each thread. For such challenging situations, ocrypto provides an alternate set of functions for algorithms that require (relatively) large chunks of memory. When calling these alternate functions, the caller provides a context memory block that need not reside on the stack.

As an example, the stack-based Ed25519 sign function requires 1152 bytes of stack space, which is already extremely frugal for this algorithm. However, the alternate implementation of Ed25519 brings this down even much further, to 240 bytes of stack space plus 896 bytes of context. This leaves valuable room on the stack for application variables.

These stack numbers have been measured with a Keil compiler.