Artifact 67169d48eb8640d10f66e04f80e5d31b5777b728ea8687a88de26ab0b889fa72:
- File
vendor/bearssl/inc/bearssl_hmac.h
— part of check-in
[27d847f693]
at
2026-11-18 16:19:19
on branch trunk
— suit syncs and clones over https
suit sync, suit clone and suit tend now work with https:// remotes. http:// and file:// keep libfossil's built-in channel.
libfossil is the private byot build 2f20f44ba4 in place of trunk f2983dcd78, approved while suit is being proven; only libfossil.c and libfossil.h differ from trunk's files. Its stream channel does all of the HTTP: request headers, Content-Length, chunked and close-delimited replies (the last only after a clean close), the reply-size limit and the timeout. suit supplies the transport: src/tls.c, over non-blocking TCP and BearSSL 0.6, now vendored.
- TLS 1.2 only, ECDHE key exchange, AES-GCM or ChaCha20-Poly1305. A server offering only TLS 1.3, RSA key exchange or CBC is refused. - The chain is verified against trust anchors and the host name against the certificate. Anchors come from the system's PEM bundle read at run time (SSL_CERT_FILE, then the usual macOS and Linux paths), or from a per-wardrobe CA file set with --ca, which replaces the bundle. No roots are compiled in and there is no way to trust anything. - A certificate file must be a regular file owned by the user or root, writable by no one else, in a folder that passes the same check; --ca refuses a symlink, and a FIFO is refused at once. - Redirects are refused, never followed, and the message names the new address. No proxy support. - Nothing is sent before the handshake has verified. The login goes out only as libfossil's login card. Commands that read the sync secret turn core files off first. - Every connect, read, write and name lookup waits in poll with a 30 s idle limit, and a whole command stops after 10 minutes. getaddrinfo has no deadline, so it runs in a forked child that is killed and reaped. - BearSSL makes no system call of its own: it is built without its own randomness or clock, and suit seeds it and gives it the time through its fault wrappers. - Exit 18 now means the secure connection failed (handshake, certificate, host name). 17 stays the network and 19 a refused login.
A per-wardrobe CA file is kept in cafiles in suit's home folder, one NAME PATH line each. suit-run is built without the network code and is unchanged at 35496 bytes; the speed budget passes. Release suit went from 2291288 to 2490024 bytes.
An independent security review found no critical or high issues. Its one medium finding, a CA file trusted without checking its owner, mode or type, is fixed here, as are its low ones: separate storage for each set of anchors, core files off, and the release check now covering every test-only switch.
Checked against a real server: an anonymous clone of this repository over https from its forge, 8.4 s, verified with stock fossil.
A new suite, test/https.sh, with 170 cases, runs local servers with test certificates made at test time, including servers that misbehave on purpose. It prints skip lines where openssl or nc is missing. A new fuzz target covers the bundle reader.
3788 unit checks (was 3669), 625 shell cases, 1153 store cases, 985 placement cases, 378 project cases, 236 usage cases, 288 curate cases, 376 curate-apply cases and 170 https cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (76 minutes, on a loaded machine). 100% coverage in all 34 source files. Three store cases that expected "cannot use https yet" now expect the refusal for a missing certificate bundle. (user: dmestas, size: 8413) [annotate] [blame] [check-ins using]
/* * Copyright (c) 2016 Thomas Pornin <[email protected]> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #ifndef BR_BEARSSL_HMAC_H__ #define BR_BEARSSL_HMAC_H__ #include <stddef.h> #include <stdint.h> #include "bearssl_hash.h" #ifdef __cplusplus extern "C" { #endif /** \file bearssl_hmac.h * * # HMAC * * HMAC is initialized with a key and an underlying hash function; it * then fills a "key context". That context contains the processed * key. * * With the key context, a HMAC context can be initialized to process * the input bytes and obtain the MAC output. The key context is not * modified during that process, and can be reused. * * IMPORTANT: HMAC shall be used only with functions that have the * following properties: * * - hash output size does not exceed 64 bytes; * - hash internal state size does not exceed 64 bytes; * - internal block length is a power of 2 between 16 and 256 bytes. */ /** * \brief HMAC key context. * * The HMAC key context is initialised with a hash function implementation * and a secret key. Contents are opaque (callers should not access them * directly). The caller is responsible for allocating the context where * appropriate. Context initialisation and usage incurs no dynamic * allocation, so there is no release function. */ typedef struct { #ifndef BR_DOXYGEN_IGNORE const br_hash_class *dig_vtable; unsigned char ksi[64], kso[64]; #endif } br_hmac_key_context; /** * \brief HMAC key context initialisation. * * Initialise the key context with the provided key, using the hash function * identified by `digest_vtable`. This supports arbitrary key lengths. * * \param kc HMAC key context to initialise. * \param digest_vtable pointer to the hash function implementation vtable. * \param key pointer to the HMAC secret key. * \param key_len HMAC secret key length (in bytes). */ void br_hmac_key_init(br_hmac_key_context *kc, const br_hash_class *digest_vtable, const void *key, size_t key_len); /* * \brief Get the underlying hash function. * * This function returns a pointer to the implementation vtable of the * hash function used for this HMAC key context. * * \param kc HMAC key context. * \return the hash function implementation. */ static inline const br_hash_class *br_hmac_key_get_digest( const br_hmac_key_context *kc) { return kc->dig_vtable; } /** * \brief HMAC computation context. * * The HMAC computation context maintains the state for a single HMAC * computation. It is modified as input bytes are injected. The context * is caller-allocated and has no release function since it does not * dynamically allocate external resources. Its contents are opaque. */ typedef struct { #ifndef BR_DOXYGEN_IGNORE br_hash_compat_context dig; unsigned char kso[64]; size_t out_len; #endif } br_hmac_context; /** * \brief HMAC computation initialisation. * * Initialise a HMAC context with a key context. The key context is * unmodified. Relevant data from the key context is immediately copied; * the key context can thus be independently reused, modified or released * without impacting this HMAC computation. * * An explicit output length can be specified; the actual output length * will be the minimum of that value and the natural HMAC output length. * If `out_len` is 0, then the natural HMAC output length is selected. The * "natural output length" is the output length of the underlying hash * function. * * \param ctx HMAC context to initialise. * \param kc HMAC key context (already initialised with the key). * \param out_len HMAC output length (0 to select "natural length"). */ void br_hmac_init(br_hmac_context *ctx, const br_hmac_key_context *kc, size_t out_len); /** * \brief Get the HMAC output size. * * The HMAC output size is the number of bytes that will actually be * produced with `br_hmac_out()` with the provided context. This function * MUST NOT be called on a non-initialised HMAC computation context. * The returned value is the minimum of the HMAC natural length (output * size of the underlying hash function) and the `out_len` parameter which * was used with the last `br_hmac_init()` call on that context (if the * initialisation `out_len` parameter was 0, then this function will * return the HMAC natural length). * * \param ctx the (already initialised) HMAC computation context. * \return the HMAC actual output size. */ static inline size_t br_hmac_size(br_hmac_context *ctx) { return ctx->out_len; } /* * \brief Get the underlying hash function. * * This function returns a pointer to the implementation vtable of the * hash function used for this HMAC context. * * \param hc HMAC context. * \return the hash function implementation. */ static inline const br_hash_class *br_hmac_get_digest( const br_hmac_context *hc) { return hc->dig.vtable; } /** * \brief Inject some bytes in HMAC. * * The provided `len` bytes are injected as extra input in the HMAC * computation incarnated by the `ctx` HMAC context. It is acceptable * that `len` is zero, in which case `data` is ignored (and may be * `NULL`) and this function does nothing. */ void br_hmac_update(br_hmac_context *ctx, const void *data, size_t len); /** * \brief Compute the HMAC output. * * The destination buffer MUST be large enough to accommodate the result; * its length is at most the "natural length" of HMAC (i.e. the output * length of the underlying hash function). The context is NOT modified; * further bytes may be processed. Thus, "partial HMAC" values can be * efficiently obtained. * * Returned value is the output length (in bytes). * * \param ctx HMAC computation context. * \param out destination buffer for the HMAC output. * \return the produced value length (in bytes). */ size_t br_hmac_out(const br_hmac_context *ctx, void *out); /** * \brief Constant-time HMAC computation. * * This function compute the HMAC output in constant time. Some extra * input bytes are processed, then the output is computed. The extra * input consists in the `len` bytes pointed to by `data`. The `len` * parameter must lie between `min_len` and `max_len` (inclusive); * `max_len` bytes are actually read from `data`. Computing time (and * memory access pattern) will not depend upon the data byte contents or * the value of `len`. * * The output is written in the `out` buffer, that MUST be large enough * to receive it. * * The difference `max_len - min_len` MUST be less than 2<sup>30</sup> * (i.e. about one gigabyte). * * This function computes the output properly only if the underlying * hash function uses MD padding (i.e. MD5, SHA-1, SHA-224, SHA-256, * SHA-384 or SHA-512). * * The provided context is NOT modified. * * \param ctx the (already initialised) HMAC computation context. * \param data the extra input bytes. * \param len the extra input length (in bytes). * \param min_len minimum extra input length (in bytes). * \param max_len maximum extra input length (in bytes). * \param out destination buffer for the HMAC output. * \return the produced value length (in bytes). */ size_t br_hmac_outCT(const br_hmac_context *ctx, const void *data, size_t len, size_t min_len, size_t max_len, void *out); #ifdef __cplusplus } #endif #endif