ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
crypto_scratch.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 crypto_scratch.h
6 * @brief Constant-time comparison for secret-dependent checks.
7 *
8 * Was also the home of the shared `crypto_work` scratch buffer and the secure wipe. Both have moved:
9 * crypto operations now borrow their working sets from the secure pool (server/mmgr/secure.h), which
10 * wipes on release, so no fixed buffer and no hand-assigned offsets remain. pc_secure_wipe() lives
11 * there too - zeroing storage is a memory-manager operation. What is left here is pc_ct_eq, which is
12 * genuinely a crypto concern. The file name now overstates its contents; renaming it is a follow-up.
13 *
14 * @author Douglas Quigg (dstroy0)
15 * @date 2026
16 */
17
18#ifndef PROTOCORE_CRYPTO_SCRATCH_H
19#define PROTOCORE_CRYPTO_SCRATCH_H
20
21#include "protocore_config.h"
23#include <stddef.h>
24#include <stdint.h>
25
26/**
27 * @brief Constant-time equality of two @p n-byte buffers: returns true iff every byte matches, in time
28 * independent of where (or whether) they first differ.
29 *
30 * Use this for every secret-dependent comparison - AEAD tags, MACs, digests, signature check values - so a
31 * timing side channel cannot reveal how many leading bytes matched. Never use memcmp() for those (it returns
32 * early on the first mismatch). The XOR-accumulate has no data-dependent branch; only the final all-zero test
33 * (the intended result) is a comparison.
34 */
35static inline bool pc_ct_eq(const void *a, const void *b, size_t n)
36{
37 const uint8_t *pa = (const uint8_t *)a;
38 const uint8_t *pb = (const uint8_t *)b;
39 uint8_t diff = 0;
40 for (size_t i = 0; i < n; i++)
41 {
42 diff |= (uint8_t)(pa[i] ^ pb[i]);
43 }
44 return diff == 0;
45}
46
47#endif // PROTOCORE_CRYPTO_SCRATCH_H
User-facing configuration for ProtoCore.
A byte region whose run length is bound in both directions.