ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
aesccm.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file aesccm.h
6 * @brief AEAD AES-CCM (NIST SP 800-38C / RFC 3610), 128- and 256-bit keys, detached tag.
7 *
8 * CCM = CTR encryption + CBC-MAC authentication under one key. SMB 3.x offers it as
9 * SMB2_ENCRYPTION_AES128_CCM (0x0001) and SMB2_ENCRYPTION_AES256_CCM (0x0003); the transport uses an
10 * 11-byte nonce and a 16-byte tag (MS-SMB2 ยง3.1.4.3). The tag is kept detached (returned separately, not
11 * appended to the ciphertext) because the SMB2 TRANSFORM_HEADER carries it in its own Signature field.
12 *
13 * On Arduino (ESP32) the block cipher is mbedtls_ccm, which routes AES through the hardware accelerator; on
14 * the native host a compact software AES (crypto/cipher/aes_block.h, shared with the GCM modules) drives the same
15 * CCM construction so the whole AEAD is unit-testable off-target. Pure, zero heap; host-tested against
16 * reference AES-CCM vectors (nonce 11, tag 16, AES-128 and AES-256).
17 *
18 * @author Douglas Quigg (dstroy0)
19 * @date 2026
20 */
21
22#ifndef PROTOCORE_AESCCM_H
23#define PROTOCORE_AESCCM_H
24
25#include "protocore_config.h"
26
27// SMB 3.x is the only consumer of AES-CCM today; gate it so non-SMB builds do not carry the code.
28#if PC_ENABLE_SMB
29
30#include <stddef.h>
31#include <stdint.h>
32#ifdef ARDUINO
33#include <mbedtls/ccm.h> // hardware-backed AES-CCM on ESP32
34#endif
35
36/** @brief AES-CCM authentication tag length used by SMB 3.x (bytes). */
37#define PC_AESCCM_TAG_LEN 16
38
39/**
40 * @brief Seal: AES-CCM encrypt-and-authenticate with a detached tag.
41 *
42 * @param key 16-byte (AES-128) or 32-byte (AES-256) key.
43 * @param key_len 16 or 32.
44 * @param nonce Nonce (7..13 bytes; SMB uses 11).
45 * @param nonce_len Nonce length.
46 * @param aad Additional authenticated data (may be NULL when @p aad_len is 0).
47 * @param aad_len AAD length (< 0xFF00).
48 * @param pt Plaintext.
49 * @param pt_len Plaintext length.
50 * @param ct_out Output: @p pt_len ciphertext bytes (may alias @p pt).
51 * @param tag_out Output: the 16-byte authentication tag.
52 * @return true on success, false on a bad argument.
53 */
54bool pc_aesccm_seal_tag(const uint8_t *key, size_t key_len, const uint8_t *nonce, size_t nonce_len, const uint8_t *aad,
55 size_t aad_len, const uint8_t *pt, size_t pt_len, uint8_t *ct_out,
56 uint8_t tag_out[PC_AESCCM_TAG_LEN]);
57
58/**
59 * @brief Open: AES-CCM verify-and-decrypt with a detached tag.
60 *
61 * The tag is recomputed over the recovered plaintext and compared in constant time; on mismatch @p out is
62 * zeroed and false is returned (fails closed - no unauthenticated plaintext is exposed to the caller).
63 * @p out receives @p ct_len plaintext bytes and may alias @p ct.
64 * @return true iff the tag is valid.
65 */
66bool pc_aesccm_open_tag(const uint8_t *key, size_t key_len, const uint8_t *nonce, size_t nonce_len, const uint8_t *aad,
67 size_t aad_len, const uint8_t *ct, size_t ct_len, const uint8_t tag[PC_AESCCM_TAG_LEN],
68 uint8_t *out);
69
70#endif // PC_ENABLE_SMB
71
72#endif // PROTOCORE_AESCCM_H
User-facing configuration for ProtoCore.