openaliro
Aliro reader: UWB/CCC core and ESP32-S3/C5/C6 port
Loading...
Searching...
No Matches
aliro_prim.h
1/*
2 * Copyright (c) 2026 asxeem
3 * SPDX-License-Identifier: ISC
4 *
5 * aliro_prim — the AEAD + elliptic-curve + RNG primitive interface used by the
6 * Aliro credential-auth composition (aliro_crypto.c). Two backends implement it:
7 * - aliro_prim_psa.c on the ESP32 target (mbedTLS-PSA)
8 * - a host double in the test build (for the secure-channel nonce/AAD tests)
9 *
10 * Hashing/KDF is NOT here; that is the portable aliro_hash.c, shared by both.
11 * All returns: 0 on success, negative on failure (AEAD decrypt returns <0 on a
12 * tag mismatch and must be treated as a hard auth failure).
13 */
14#pragma once
15
16#include <stddef.h>
17#include <stdint.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#define ALIRO_P256_SCALAR 32u /* private scalar / coordinate */
24#define ALIRO_P256_POINT 65u /* uncompressed point: 0x04 | X32 | Y32 */
25#define ALIRO_P256_SIG 64u /* raw ECDSA r|s */
26#define ALIRO_GCM_TAG 16u
27
28/* Initialise the backend (idempotent). Call once before any other call. */
29int aliro_prim_init(void);
30
31/* CSPRNG. */
32int aliro_random(uint8_t *out, size_t len);
33
34/* AES-256-GCM. tag_len must be <= 16. Decrypt verifies the tag. */
35int aliro_aes256_gcm_encrypt(const uint8_t key[32], const uint8_t *nonce, size_t nonce_len,
36 const uint8_t *aad, size_t aad_len, const uint8_t *pt, size_t pt_len,
37 uint8_t *ct, uint8_t *tag, size_t tag_len);
38int aliro_aes256_gcm_decrypt(const uint8_t key[32], const uint8_t *nonce, size_t nonce_len,
39 const uint8_t *aad, size_t aad_len, const uint8_t *ct, size_t ct_len,
40 const uint8_t *tag, size_t tag_len, uint8_t *pt);
41
42/* AES-128-ECB, one block (the BLE advertisement Dynamic Tag, aliro_advtag.c). */
43int aliro_aes128_ecb_encrypt(const uint8_t key[16], const uint8_t in[16], uint8_t out[16]);
44
45/* P-256 ephemeral key pair: priv = 32-byte scalar, pub = 65-byte point. */
46int aliro_ec_p256_keygen(uint8_t priv[ALIRO_P256_SCALAR], uint8_t pub[ALIRO_P256_POINT]);
47
48/* Derive the 65-byte uncompressed public key from a 32-byte P-256 private
49 * scalar (used to recover the reader group key X from the provisioned
50 * signingKey; verificationKey = pub(signingKey)). */
51int aliro_ec_p256_pub_from_priv(const uint8_t priv[ALIRO_P256_SCALAR],
52 uint8_t pub[ALIRO_P256_POINT]);
53
54/* ECDH: shared_x = X coordinate (32 bytes) of priv * peer_pub. */
55int aliro_ecdh_p256(const uint8_t priv[ALIRO_P256_SCALAR], const uint8_t peer_pub[ALIRO_P256_POINT],
56 uint8_t shared_x[ALIRO_P256_SCALAR]);
57
58/* ECDSA-P256-SHA256 over the raw message (hashing is internal). sig = r|s. */
59int aliro_ecdsa_p256_sign(const uint8_t priv[ALIRO_P256_SCALAR], const uint8_t *msg, size_t msg_len,
60 uint8_t sig[ALIRO_P256_SIG]);
61/* ECDSA-P256 over an already computed SHA-256 digest. sig = r|s. */
62int aliro_ecdsa_p256_sign_hash(const uint8_t priv[ALIRO_P256_SCALAR], const uint8_t hash[32],
63 uint8_t sig[ALIRO_P256_SIG]);
64int aliro_ecdsa_p256_verify(const uint8_t pub[ALIRO_P256_POINT], const uint8_t *msg, size_t msg_len,
65 const uint8_t sig[ALIRO_P256_SIG]);
66
67#ifdef __cplusplus
68}
69#endif