#ifndef VL_PROTOCOL_H #define VL_PROTOCOL_H #include #include #include "mbedtls/md.h" /* All functions return 0 on success, nonzero on failure. Not board firmware. * String outputs include NUL, binary outputs do not. Never transmit the NUL. * Caller owns buffers; inputs/outputs must not overlap. */ int vl_key_from_hex(const char *hex, uint8_t key[32]); /* rng must fill ALL n bytes from the hardware/OS secure RNG; return 0 or fail. */ typedef int (*vl_rng_fn)(void *context, uint8_t *bytes, size_t n); int vl_nonce(vl_rng_fn rng, void *context, char out[23]); int vl_uuid4(vl_rng_fn rng, void *context, char out[37]); int vl_sha256_hex(const void *bytes, size_t length, char out[65]); int vl_http_sign(const uint8_t key[32], const char *method, const char *path, const char *device_id, const char *key_version, const char *timestamp_ms, const char *nonce, const char *request_id, const void *body, size_t body_length, char signature_hex[65]); int vl_ble_pack(const void *json_utf8, size_t length, uint8_t *frame, size_t capacity, size_t *frame_length); /* Stream OTA bytes with bounded memory. Always call mbedtls_md_free after * begin, even if an update/finish fails. Do not begin twice on a live context. */ int vl_sha256_begin(mbedtls_md_context_t *context); int vl_sha256_update(mbedtls_md_context_t *context, const void *bytes, size_t n); int vl_sha256_finish(mbedtls_md_context_t *context, char out[65]); /* Read already downloaded bytes (e.g. flash/file): 0=success, *n=0 means EOF. * On each call write at most capacity bytes; return nonzero on I/O failure. */ typedef int (*vl_read_fn)(void *context, uint8_t *out, size_t capacity, size_t *n); int vl_ota_verify(vl_read_fn read, void *context, uint64_t expected_size, const char *expected_sha256); #endif