---
title: "AES"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/crypto/aes/
last_updated: 2026-11-18
sdk_version: 4.44
---
# AES

Source: https://developer.repebble.com/pebbleos/docs/samples/subsys/crypto/aes/

This sample encrypts the example block of FIPS 197 with [AES](https://developer.repebble.com/pebbleos/docs/subsystems/crypto.md)-128 and AES-256, from a key schedule expanded once with `pbl_aes_expandkey()`, and decrypts it back.

It then encrypts three blocks in ECB mode. ECB encrypts each block on its own, so the first and last blocks, which are equal, encrypt to equal blocks: a pattern in the plaintext shows through, which is why ECB is a building block for other modes rather than a way to encrypt messages. Lengths that are not whole blocks are refused.

Run:

`pbl samples samples/subsys/crypto/aes`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<string.h\>#include \<pbl/crypto/aes.h\>#define MSG\_LEN (3U \* PBL\_AES\_BLOCK\_SIZE)/\* The example vectors of FIPS 197, appendix C. \*/static const uint8\_t s\_plain[PBL\_AES\_BLOCK\_SIZE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,};static struct pbl\_aes\_ctx s\_ctx;static void prv\_print(const char \*what, const uint8\_t block[PBL\_AES\_BLOCK\_SIZE]) {printf("aes: %s: ", what);for (size\_t i = 0; i \< PBL\_AES\_BLOCK\_SIZE; i++) {printf("%02x", block[i]);}printf("\n");}int main(void) {uint8\_t key[PBL\_AES\_KEYSIZE\_256], block[PBL\_AES\_BLOCK\_SIZE], back[PBL\_AES\_BLOCK\_SIZE];uint8\_t msg[MSG\_LEN], enc[MSG\_LEN], dec[MSG\_LEN];int ret;for (size\_t i = 0; i \< sizeof(key); i++) {key[i] = (uint8\_t)i;}/\* The key schedule is computed once, then used for any number of blocks. \*/ret = pbl\_aes\_expandkey(&s\_ctx, key, PBL\_AES\_KEYSIZE\_128);if (ret \< 0) {fprintf(stderr, "aes: key expansion failed (%d)\n", ret);return ret;}pbl\_aes\_encrypt(&s\_ctx, block, s\_plain);prv\_print("AES-128", block);ret = pbl\_aes\_expandkey(&s\_ctx, key, PBL\_AES\_KEYSIZE\_256);if (ret \< 0) {fprintf(stderr, "aes: key expansion failed (%d)\n", ret);return ret;}pbl\_aes\_encrypt(&s\_ctx, block, s\_plain);prv\_print("AES-256", block);pbl\_aes\_decrypt(&s\_ctx, back, block);printf("aes: decrypted back to the plaintext: %s\n",memcmp(back, s\_plain, sizeof(back)) == 0 ? "yes" : "no");/\* ECB encrypts each block on its own: the first and last blocks are the same. \*/memcpy(msg, "Sixteen bytes...and sixteen moreSixteen bytes...", MSG\_LEN);ret = pbl\_aes\_ecb\_encrypt(&s\_ctx, enc, msg, MSG\_LEN);if (ret == 0) {ret = pbl\_aes\_ecb\_decrypt(&s\_ctx, dec, enc, MSG\_LEN);}if (ret \< 0) {fprintf(stderr, "aes: ECB failed (%d)\n", ret);return ret;}printf("aes: %u bytes in ECB mode, decrypted back: %s\n", MSG\_LEN,memcmp(dec, msg, MSG\_LEN) == 0 ? "yes" : "no");printf("aes: equal blocks encrypt to equal blocks in ECB mode: %s\n",memcmp(enc, &enc[2 \* PBL\_AES\_BLOCK\_SIZE], PBL\_AES\_BLOCK\_SIZE) == 0 ? "yes" : "no");ret = pbl\_aes\_ecb\_encrypt(&s\_ctx, enc, msg, 20);printf("aes: 20 bytes, not whole blocks: %s\n", ret == -EINVAL ? "-EINVAL" : "accepted");printf("Sample done\n");return 0;}
```

## Output

```
aes: AES-128: 69c4e0d86a7b0430d8cdb78070b4c55a
aes: AES-256: 8ea2b7ca516745bfeafc49904b496089
aes: decrypted back to the plaintext: yes
aes: 48 bytes in ECB mode, decrypted back: yes
aes: equal blocks encrypt to equal blocks in ECB mode: yes
aes: 20 bytes, not whole blocks: -EINVAL
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/subsys/crypto/aes.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/subsys/crypto/aes.md).
