ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
base64.cpp File Reference

Base64 encoder/decoder implementation. More...

#include "base64.h"
#include "protocore_config.h"
#include <stddef.h>
#include <string.h>

Go to the source code of this file.

Functions

void pc_base64_encode (const uint8_t *src, size_t src_len, char *dst)
 Encode src_len bytes of src as Base64.
 
size_t pc_base64_decode (const char *src, uint8_t *dst, size_t dst_cap)
 Decode a null-terminated Base64 string.
 
size_t pc_base64url_encode (const uint8_t *src, size_t src_len, char *dst)
 Encode src_len bytes as base64url (RFC 4648 section 5): '-' / '_' in place of '+' / '/', and no '=' padding.
 
size_t pc_base64url_decode (const char *src, size_t src_len, uint8_t *dst, size_t dst_cap)
 Decode src_len characters of base64url (RFC 4648 section 5, '-'/'_' alphabet; an '=' ends the input).
 

Detailed Description

Base64 encoder/decoder implementation.

Encode is a single portable software codec (RFC 4648) on every target: it is byte-identical to mbedTLS's but ~20x faster on the ESP32-S3 (docs/FEATURE_PERFORMANCE.md section 2), and it only ever encodes public data here (the SHA-1 in the WebSocket accept), so a data-dependent table lookup is fine.

Decode is one portable constant-time codec on every target (no mbedTLS delegation, no separate native path). It is the only base64 path that touches a secret - the Basic-auth credential (RFC 7617) and the JWT / JWS segments (RFC 7515, via base64url) - so the character -> value mapping is evaluated with branchless arithmetic range masks: no data-dependent branch and no data-indexed table, so neither the timing nor the cache footprint depends on the secret bytes. Padding / length handling may branch (the plaintext length mod 3 is public, not a secret). This both removes the mbedTLS base64 dependency and closes the timing gap on the base64url (JWT) path, which was previously a plain branchy software decoder.

Definition in file base64.cpp.

Function Documentation

◆ pc_base64_encode()

void pc_base64_encode ( const uint8_t *  src,
size_t  src_len,
char *  dst 
)

Encode src_len bytes of src as Base64.

Writes a null-terminated string into dst. dst must be at least ((src_len + 2) / 3) * 4 + 1 bytes.

Parameters
srcInput bytes.
src_lenNumber of input bytes.
dstOutput buffer (null-terminated Base64 string).

Definition at line 29 of file base64.cpp.

Referenced by pc_base64url_encode().

◆ pc_base64_decode()

size_t pc_base64_decode ( const char *  src,
uint8_t *  dst,
size_t  dst_cap 
)

Decode a null-terminated Base64 string.

Writes decoded bytes into dst, never writing more than dst_cap bytes. Returns the number of decoded bytes, or 0 on invalid input or if the decoded output would exceed dst_cap (the write is bounded - no overflow). The caller must leave room for any terminator it adds afterward (pass a capacity one less than the buffer size if it will null-terminate at the returned length).

Parameters
srcNull-terminated Base64 input string.
dstOutput byte buffer.
dst_capCapacity of dst in bytes.
Returns
Number of bytes written to dst, or 0 on error / overflow.

Definition at line 217 of file base64.cpp.

◆ pc_base64url_encode()

size_t pc_base64url_encode ( const uint8_t *  src,
size_t  src_len,
char *  dst 
)

Encode src_len bytes as base64url (RFC 4648 section 5): '-' / '_' in place of '+' / '/', and no '=' padding.

Writes a null-terminated string into dst, which must hold at least ((src_len + 2) / 3) * 4 + 1 bytes (the same as pc_base64_encode; the URL form is never longer).

Returns
the number of characters written.

Definition at line 304 of file base64.cpp.

References pc_base64_encode().

◆ pc_base64url_decode()

size_t pc_base64url_decode ( const char *  src,
size_t  src_len,
uint8_t *  dst,
size_t  dst_cap 
)

Decode src_len characters of base64url (RFC 4648 section 5, '-'/'_' alphabet; an '=' ends the input).

Strict: the standard '+'/'/' characters are rejected so a JWS/JWT segment is decoded as base64url only (RFC 7515), never as a mixed alphabet. Streaming decoder - no padding or buffer length restriction. Writes at most dst_cap bytes; returns the number written, or 0 on an invalid character or if the output would exceed dst_cap (the write is bounded - no overflow).

Definition at line 333 of file base64.cpp.