libft_ssl/doc/generic_interface.tex

53 lines
2.8 KiB
TeX

\section{Generic Digest Interface}
All hash algorithms in \texttt{libft\_ssl} are exposed through a single,
uniform interface built around the \texttt{struct digest\_algo} type. This
structure holds the algorithm's metadata (its name, digest size and block size)
along with three function pointers: \texttt{init}, \texttt{update} and
\texttt{final}. This design allows any algorithm to be driven through the same
calling convention without the caller needing to know which one is in use. The
associated context is held in a \texttt{union digest\_ctx}, which overlays the
per-algorithm state structures so that a single allocation covers all supported
algorithms.
\vspace{1em}
\texttt{struct digest\_algo} describes a hash algorithm as a set of metadata
and three function pointers. The \texttt{name} field identifies the algorithm.
The \texttt{digest\_size} and \texttt{block\_size} fields express its output
length and internal block size in bytes. The three function pointers
\texttt{init}, \texttt{update} and \texttt{final} define the algorithm's
lifecycle: \texttt{init} sets the context to its initial state, \texttt{update}
feeds an arbitrary amount of data into it, and \texttt{final} produces the
digest and resets the context. All three operate on a \texttt{void~*} context
pointer, which allows the interface to remain algorithm-agnostic.
\vspace{1em}
The \texttt{union digest\_ctx} type provides a single allocation large enough
to hold the context of any supported algorithm. Because only one algorithm is
active at a time, overlaying the per-algorithm structures in a union avoids the
overhead of a separate heap allocation while keeping the calling code uniform.
The active member is always the one matching the \texttt{struct digest\_algo}
being used.
\vspace{1em}
Each supported algorithm is registered in \texttt{digest\_algos.h} through an
X-macro list. This file defines a single macro \texttt{DIGEST\_ALGOS(X)} that
expands \texttt{X} once per algorithm, passing its name, digest size and block
size. Consuming this list with a different definition of \texttt{X} generates
the corresponding code or data without repetition --- the global \texttt{struct
digest\_algo} instances in \texttt{libft\_ssl.c} are produced this way. Adding
a new algorithm to the library reduces to adding one line to this list.
\vspace{1em}
All three algorithms follow the Merkle-Damgård construction. The message is
split into fixed-size blocks and processed sequentially. After each block, the
compressed output is combined with the previous state to produce the new state
--- this chaining ensures that the final digest depends on every bit of the
input. The exact combination operation is algorithm-specific: MD5 and SHA-256
use an additive feedforward, while Whirlpool uses the Miyaguchi-Preneel scheme.
\newpage