+/* -*-c-*-
+ *
+ * $Id: tripe.h,v 1.1 2001/02/03 20:26:37 mdw Exp $
+ *
+ * Main header file for TrIPE
+ *
+ * (c) 2001 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Trivial IP Encryption (TrIPE).
+ *
+ * TrIPE is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * TrIPE is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with TrIPE; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: tripe.h,v $
+ * Revision 1.1 2001/02/03 20:26:37 mdw
+ * Initial checkin.
+ *
+ */
+
+#ifndef TRIPE_H
+#define TRIPE_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
+#include <pwd.h>
+#include <grp.h>
+
+#include <mLib/alloc.h>
+#include <mLib/arena.h>
+#include <mLib/bres.h>
+#include <mLib/dstr.h>
+#include <mLib/env.h>
+#include <mLib/fdflags.h>
+#include <mLib/fwatch.h>
+#include <mLib/mdwopt.h>
+#include <mLib/quis.h>
+#include <mLib/report.h>
+#include <mLib/sel.h>
+#include <mLib/selbuf.h>
+#include <mLib/sig.h>
+#include <mLib/str.h>
+#include <mLib/sub.h>
+#include <mLib/trace.h>
+
+#include <catacomb/gcipher.h>
+#include <catacomb/gmac.h>
+#include <catacomb/grand.h>
+#include <catacomb/key.h>
+#include <catacomb/paranoia.h>
+
+#include <catacomb/blowfish.h>
+#include <catacomb/blowfish-cbc.h>
+#include <catacomb/noise.h>
+#include <catacomb/rand.h>
+#include <catacomb/rmd160.h>
+#include <catacomb/rmd160-hmac.h>
+
+#include <catacomb/mp.h>
+#include <catacomb/mpmont.h>
+#include <catacomb/mprand.h>
+#include <catacomb/dh.h>
+
+#include "util.h"
+
+#undef sun
+
+/*----- Magic numbers -----------------------------------------------------*/
+
+/* --- Tunnel types --- */
+
+#define TUN_NOTDEF 0
+#define TUN_UNET 1
+#define TUN_BSD 2
+
+/* --- Trace flags --- */
+
+#define T_TUNNEL 1u
+#define T_PEER 2u
+#define T_PACKET 4u
+#define T_ADMIN 8u
+#define T_CRYPTO 16u
+#define T_KEYSET 32u
+#define T_KEYEXCH 64u
+#define T_KEYMGMT 128u
+
+#define T_ALL 255u
+
+/* --- Units --- */
+
+#define SEC(n) (n##u)
+#define MIN(n) (n##u * 60u)
+#define MEG(n) (n##ul * 1024ul * 1024ul)
+
+/* --- Other things --- */
+
+#define PKBUFSZ 65536
+
+/*----- TrIPE protocol ----------------------------------------------------*/
+
+/* --- TrIPE packet format --- *
+ *
+ * A packet begins with a single-byte packet type. The remaining data
+ * depends on the packet type.
+ */
+
+#define MSG_PACKET 0u
+/* Followed by a 64-bit MAC and an encrypted packet. The MAC is used as an
+ * IV for a 64-bit block cipher in CBC-stealing mode.
+ */
+
+#define MSG_PRECHALLENGE 1u
+/* Followed by the challenge only. Useful for bootstrapping the system.
+ */
+
+#define MSG_CHALLENGE 2u
+/* Followed by a response hash and a large-integer challenge.
+ */
+
+#define MSG_RESPONSE 3u
+/* Followed by a large-integer response.
+ */
+
+/*----- Data structures ---------------------------------------------------*/
+
+/* --- Buffers --- *
+ *
+ * Buffers provide a simple stream-like interface for building and parsing
+ * packets.
+ */
+
+typedef struct buf {
+ octet *base, *p, *limit; /* Pointers to the buffer */
+ unsigned f; /* Various flags */
+} buf;
+
+#define BF_BROKEN 1u /* Buffer is broken */
+
+/* --- Socket addresses --- *
+ *
+ * A magic union of supported socket addresses.
+ */
+
+typedef union addr {
+ struct sockaddr sa;
+ struct sockaddr_in sin;
+} addr;
+
+/* --- A symmetric keyset --- *
+ *
+ * A keyset contains a set of symmetric keys for encrypting and decrypting
+ * packets. Keysets are stored in a list, sorted in reverse order of
+ * creation, so that the most recent keyset (the one most likely to be used)
+ * is first.
+ *
+ * Each keyset has a time limit and a data limit. The keyset is destroyed
+ * when either it has existed for too long, or it has been used to encrypt
+ * too much data. New key exchanges are triggered when keys are close to
+ * expiry.
+ */
+
+typedef struct keyset {
+ struct keyset *next; /* Next active keyset in the list */
+ time_t t_exp; /* Expiry time for this keyset */
+ unsigned long sz_exp; /* Data limit for the keyset */
+#ifndef NTRACE
+ unsigned seq; /* Sequence number for tracing */
+#endif
+ gcipher *c; /* Keyset cipher for encryption */
+ gmac *m; /* Keyset MAC for integrity */
+} keyset;
+
+/* --- Key exchange --- *
+ *
+ * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
+ * Protocol has a number of desirable features (e.g., perfect forward
+ * secrecy, and zero-knowledge authentication) which make it attractive for
+ * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
+ * Clive Jones.
+ */
+
+typedef struct keyexch {
+ keyset **ks; /* Peer's list of keysets */
+ struct peer *p; /* Pointer back to the peer */
+ unsigned f; /* Various useful flags */
+ sel_timer t; /* Timer for next exchange */
+ dh_pub kpub; /* Peer's public key */
+ mp *my_x, *my_gx, *my_gxy; /* My half of the exchange */
+ octet my_h[RMD160_HASHSZ]; /* My challenge hash */
+ mp *your_gx, *your_gxy; /* Your half of the exchange */
+ octet your_h[RMD160_HASHSZ]; /* Your challenge hash */
+ time_t t_valid; /* When this exchange goes bad */
+ time_t t_qchal, t_qresp; /* Quiet timers for packet types */
+ time_t t_newchal; /* When to accept a new challenge */
+} keyexch;
+
+#define KXF_TIMER 1u /* Waiting for a timer to go off */
+#define KXF_INIT 2u /* Big numbers are initialized */
+#define KXF_MYH 4u /* My hash has been computed */
+#define KXF_YOURH 8u /* Your hash has been received */
+#define KXF_REPLY 16u /* Received your response OK */
+#define KXF_DONE 32u /* Key exchange completed */
+
+/* --- Tunnel structure --- *
+ *
+ * Used to maintain system-specific information about the tunnel interface.
+ */
+
+typedef struct tunnel {
+#if TUN_TYPE == TUN_UNET
+ sel_file f; /* Selector for Usernet device */
+ struct peer *p; /* Pointer to my peer */
+#else
+# error "No support for this tunnel type"
+#endif
+} tunnel;
+
+/* --- Peer structure --- *
+ *
+ * The main structure which glues everything else together.
+ */
+
+typedef struct peer {
+ struct peer *next, *prev; /* Links to next and previous */
+ char *name; /* Name of this peer */
+ tunnel t; /* Tunnel for local packets */
+ keyset *ks; /* List head for keysets */
+ keyexch kx; /* Key exchange protocol block */
+ buf b; /* Buffer for sending packets */
+ addr peer; /* Peer socket address */
+ size_t sasz; /* Socket address size */
+} peer;
+
+/* --- Admin structure --- */
+
+typedef struct admin {
+ struct admin *next, *prev; /* Links to next and previous */
+ selbuf b; /* Line buffer for commands */
+ int fd; /* File descriptor for output */
+#ifndef NTRACE
+ unsigned seq; /* Sequence number for tracing */
+#endif
+ char *pname; /* Peer name to create */
+ char *paddr; /* Address string to resolve */
+ bres_client r; /* Background resolver task */
+ sel_timer t; /* Timer for resolver */
+ addr peer; /* Address to set */
+ size_t sasz; /* Size of the address */
+} admin;
+
+/*----- Global variables --------------------------------------------------*/
+
+extern sel_state sel; /* Global I/O event state */
+extern dh_priv kpriv; /* Our private key */
+extern mpmont mg; /* Montgomery context for DH group */
+extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ]; /* Big packet buffers */
+
+#ifndef NTRACE
+extern const trace_opt tr_opts[]; /* Trace options array */
+extern unsigned tr_flags; /* Trace options flags */
+#endif
+
+/*----- Key management ----------------------------------------------------*/
+
+/* --- @km_interval@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: Zero if OK, nonzero to force reloading of keys.
+ *
+ * Use: Called on the interval timer to perform various useful jobs.
+ */
+
+extern int km_interval(void);
+
+/* --- @km_init@ --- *
+ *
+ * Arguments: @const char *kr_priv@ = private keyring file
+ * @const char *kr_pub@ = public keyring file
+ * @const char *tag@ = tag to load
+ *
+ * Returns: ---
+ *
+ * Use: Initializes, and loads the private key.
+ */
+
+extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
+ const char */*tag*/);
+
+/* --- @km_getpubkey@ --- *
+ *
+ * Arguments: @const char *tag@ = public key tag to load
+ * @dh_pub *kpub@ = where to put the public key
+ *
+ * Returns: Zero if OK, nonzero if it failed.
+ *
+ * Use: Fetches a public key from the keyring.
+ */
+
+extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/);
+
+/*----- Key exchange ------------------------------------------------------*/
+
+/* --- @kx_start@ --- *
+ *
+ * Arguments: @keyexch *kx@ = pointer to key exchange context
+ *
+ * Returns: ---
+ *
+ * Use: Stimulates a key exchange. If a key exchage is in progress,
+ * a new challenge is sent (unless the quiet timer forbids
+ * this); if no exchange is in progress, one is commenced.
+ */
+
+extern void kx_start(keyexch */*kx*/);
+
+/* --- @kx_prechallenge@ --- *
+ *
+ * Arguments: @keyexch *kx@ = pointer to key exhange context
+ * @buf *b@ = pointer to buffer containing the packet
+ *
+ * Returns: ---
+ *
+ * Use: Reads a prechallenge packet from the buffer and handles it.
+ */
+
+extern void kx_prechallenge(keyexch */*kx*/, buf */*b*/);
+
+/* --- @kx_challenge@ --- *
+ *
+ * Arguments: @keyexch *kx@ = pointer to key exchange context
+ * @buf *b@ = a buffer containing the packet to read
+ *
+ * Returns: ---
+ *
+ * Use: Reads a challenge from the buffer and handles it.
+ */
+
+extern void kx_challenge(keyexch */*kx*/, buf */*b*/);
+
+/* --- @kx_response@ --- *
+ *
+ * Arguments: @keyexch *kx@ = pointer to key exchange context
+ * @buf *b@ = a buffer containing the packet to read
+ *
+ * Returns: ---
+ *
+ * Use: Reads a response from the buffer and handles it.
+ */
+
+extern void kx_response(keyexch */*kx*/, buf */*b*/);
+
+/* --- @kx_free@ --- *
+ *
+ * Arguments: @keyexch *kx@ = pointer to key exchange context
+ *
+ * Returns: ---
+ *
+ * Use: Frees everything in a key exchange context.
+ */
+
+extern void kx_free(keyexch */*kx*/);
+
+/* --- @kx_newkeys@ --- *
+ *
+ * Arguments: @keyexch *kx@ = pointer to key exchange context
+ *
+ * Returns: ---
+ *
+ * Use: Informs the key exchange module that its keys may have
+ * changed. If fetching the new keys fails, the peer will be
+ * destroyed, we log messages and struggle along with the old
+ * keys.
+ */
+
+extern void kx_newkeys(keyexch */*kx*/);
+
+/* --- @kx_init@ --- *
+ *
+ * Arguments: @keyexch *kx@ = pointer to key exchange context
+ * @peer *p@ = pointer to peer context
+ * @keyset **ks@ = pointer to keyset list
+ *
+ * Returns: Zero if OK, nonzero if it failed.
+ *
+ * Use: Initializes a key exchange module. The module currently
+ * contains no keys, and will attempt to initiate a key
+ * exchange.
+ */
+
+extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
+
+/*----- Keysets and symmetric cryptography --------------------------------*/
+
+/* --- @ks_free@ --- *
+ *
+ * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ *
+ * Returns: ---
+ *
+ * Use: Frees all of the keys in a keyset.
+ */
+
+extern void ks_free(keyset **/*ksroot*/);
+
+/* --- @ks_prune@ --- *
+ *
+ * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ *
+ * Returns: ---
+ *
+ * Use: Prunes the keyset list by removing keys which mustn't be used
+ * any more.
+ */
+
+extern void ks_prune(keyset **/*ksroot*/);
+
+/* --- @ks_gen@ --- *
+ *
+ * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * @const void *k@ = pointer to key material
+ * @size_t sz@ = size of the key material
+ *
+ * Returns: The regeneration time for the new key.
+ *
+ * Use: Derives a keyset from the given key material and adds it to
+ * the list.
+ */
+
+extern time_t ks_gen(keyset **/*ksroot*/, const void */*k*/, size_t /*sz*/);
+
+/* --- @ks_encrypt@ --- *
+ *
+ * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * @buf *b@ = pointer to input buffer
+ * @buf *bb@ = pointer to output buffer
+ *
+ * Returns: Nonzero if a new key is needed.
+ *
+ * Use: Encrypts a packet.
+ */
+
+extern int ks_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
+
+/* --- @ks_decrypt@ --- *
+ *
+ * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * @buf *b@ = pointer to input buffer
+ * @buf *bb@ = pointer to output buffer
+ *
+ * Returns: Nonzero if the packet couldn't be decrypted.
+ *
+ * Use: Decrypts a packet.
+ */
+
+extern int ks_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
+
+/*----- Administration interface ------------------------------------------*/
+
+/* --- @a_warn@ --- *
+ *
+ * Arguments: @const char *fmt@ = pointer to format string
+ * @...@ = other arguments
+ *
+ * Returns: ---
+ *
+ * Use: Informs all admin connections of a warning.
+ */
+
+extern void a_warn(const char */*fmt*/, ...);
+
+/* --- @a_create@ --- *
+ *
+ * Arguments: @int fd_in, fd_out@ = file descriptors to use
+ *
+ * Returns: ---
+ *
+ * Use: Creates a new admin connection.
+ */
+
+extern void a_create(int /*fd_in*/, int /*fd_out*/);
+
+/* --- @a_quit@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Shuts things down nicely.
+ */
+
+extern void a_quit(void);
+
+/* --- @a_daemon@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Informs the admin module that it's a daemon.
+ */
+
+extern void a_daemon(void);
+
+/* --- @a_init@ --- *
+ *
+ * Arguments: @const char *sock@ = socket name to create
+ *
+ * Returns: ---
+ *
+ * Use: Creates the admin listening socket.
+ */
+
+extern void a_init(const char */*sock*/);
+
+/*----- Peer management ---------------------------------------------------*/
+
+/* --- @p_txstart@ --- *
+ *
+ * Arguments: @peer *p@ = pointer to peer block
+ * @unsigned msg@ = message type code
+ *
+ * Returns: A pointer to a buffer to write to.
+ *
+ * Use: Starts sending to a peer. Only one send can happen at a
+ * time.
+ */
+
+extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
+
+/* --- @p_txend@ --- *
+ *
+ * Arguments: @peer *p@ = pointer to peer block
+ *
+ * Returns: ---
+ *
+ * Use: Sends a packet to the peer.
+ */
+
+extern void p_txend(peer */*p*/);
+
+/* --- @p_tun@ --- *
+ *
+ * Arguments: @peer *p@ = pointer to peer block
+ * @buf *b@ = buffer containing incoming packet
+ *
+ * Returns: ---
+ *
+ * Use: Handles a packet which needs to be sent to a peer.
+ */
+
+extern void p_tun(peer */*p*/, buf */*b*/);
+
+/* --- @p_interval@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Called periodically to do tidying.
+ */
+
+extern void p_interval(void);
+
+/* --- @p_ifname@ --- *
+ *
+ * Arguments: @peer *p@ = pointer to a peer block
+ *
+ * Returns: A pointer to the peer's interface name.
+ */
+
+extern const char *p_ifname(peer */*p*/);
+
+/* --- @p_addr@ --- *
+ *
+ * Arguments: @peer *p@ = pointer to a peer block
+ *
+ * Returns: A pointer to the peer's address.
+ */
+
+extern const addr *p_addr(peer */*p*/);
+
+/* --- @p_init@ --- *
+ *
+ * Arguments: @unsigned port@ = port number to listen to
+ *
+ * Returns: ---
+ *
+ * Use: Initializes the peer system; creates the socket.
+ */
+
+extern void p_init(unsigned /*port*/);
+
+/* --- @p_port@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: Port number used for socket.
+ */
+
+unsigned p_port(void);
+
+/* --- @p_create@ --- *
+ *
+ * Arguments: @const char *name@ = name for this peer
+ * @struct sockaddr *sa@ = socket address of peer
+ * @size_t sz@ = size of socket address
+ *
+ * Returns: Pointer to the peer block, or null if it failed.
+ *
+ * Use: Creates a new named peer block. No peer is actually attached
+ * by this point.
+ */
+
+extern peer *p_create(const char */*name*/,
+ struct sockaddr */*sa*/, size_t /*sz*/);
+
+/* --- @p_name@ --- *
+ *
+ * Arguments: @peer *p@ = pointer to a peer block
+ *
+ * Returns: A pointer to the peer's name.
+ */
+
+extern const char *p_name(peer */*p*/);
+
+/* --- @p_find@ --- *
+ *
+ * Arguments: @const char *name@ = name to look up
+ *
+ * Returns: Pointer to the peer block, or null if not found.
+ *
+ * Use: Finds a peer by name.
+ */
+
+extern peer *p_find(const char */*name*/);
+
+/* --- @p_destroy@ --- *
+ *
+ * Arguments: @peer *p@ = pointer to a peer
+ *
+ * Returns: ---
+ *
+ * Use: Destroys a peer.
+ */
+
+extern void p_destroy(peer */*p*/);
+
+/* --- @p_first@, @p_next@ --- *
+ *
+ * Arguments: @peer *p@ = a peer block
+ *
+ * Returns: @peer_first@ returns the first peer in some ordering;
+ * @peer_next@ returns the peer following a given one in the
+ * same ordering. Null is returned for the end of the list.
+ */
+
+extern peer *p_first(void);
+extern peer *p_next(peer */*p*/);
+
+/*----- Tunnel interface --------------------------------------------------*/
+
+/* --- @tun_init@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Initializes the tunneling system. Maybe this will require
+ * opening file descriptors or something.
+ */
+
+extern void tun_init(void);
+
+/* --- @tun_create@ --- *
+ *
+ * Arguments: @tunnel *t@ = pointer to tunnel block
+ * @peer *p@ = pointer to peer block
+ *
+ * Returns: Zero if it worked, nonzero on failure.
+ *
+ * Use: Initializes a new tunnel.
+ */
+
+extern int tun_create(tunnel */*t*/, peer */*p*/);
+
+/* --- @tun_ifname@ --- *
+ *
+ * Arguments: @tunnel *t@ = pointer to tunnel block
+ *
+ * Returns: A pointer to the tunnel's interface name.
+ */
+
+extern const char *tun_ifname(tunnel */*t*/);
+
+/* --- @tun_inject@ --- *
+ *
+ * Arguments: @tunnel *t@ = pointer to tunnel block
+ * @buf *b@ = buffer to send
+ *
+ * Returns: ---
+ *
+ * Use: Injects a packet into the local network stack.
+ */
+
+extern void tun_inject(tunnel */*t*/, buf */*b*/);
+
+/* --- @tun_destroy@ --- *
+ *
+ * Arguments: @tunnel *t@ = pointer to tunnel block
+ *
+ * Returns: ---
+ *
+ * Use: Destroys a tunnel.
+ */
+
+extern void tun_destroy(tunnel */*t*/);
+
+/*----- Buffer handling ---------------------------------------------------*/
+
+/* --- Useful macros --- */
+
+#define BBASE(b) ((b)->base)
+#define BLIM(b) ((b)->limit)
+#define BCUR(b) ((b)->p)
+#define BSZ(b) ((b)->limit - (b)->base)
+#define BLEN(b) ((b)->p - (b)->base)
+#define BLEFT(b) ((b)->limit - (b)->p)
+#define BSTEP(b, sz) ((b)->p += (sz))
+#define BBAD(b) ((b)->f & BF_BROKEN)
+#define BOK(b) (!BBAD(b))
+
+#define BENSURE(b, sz) \
+ (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
+
+/* --- @buf_init@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ * @void *p@ = pointer to a buffer
+ * @size_t sz@ = size of the buffer
+ *
+ * Returns: ---
+ *
+ * Use: Initializes the buffer block appropriately.
+ */
+
+extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
+
+/* --- @buf_break@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ *
+ * Returns: Some negative value.
+ *
+ * Use: Marks a buffer as broken.
+ */
+
+extern int buf_break(buf */*b*/);
+
+/* --- @buf_ensure@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ * @size_t sz@ = size of data wanted
+ *
+ * Returns: Zero if it worked, nonzero if there wasn't enough space.
+ *
+ * Use: Ensures that there are @sz@ bytes still in the buffer.
+ */
+
+extern int buf_ensure(buf */*b*/, size_t /*sz*/);
+
+/* --- @buf_get@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ * @void *p@ = pointer to a buffer
+ * @size_t sz@ = size of the buffer
+ *
+ * Returns: Zero if it worked, nonzero if there wasn't enough data.
+ *
+ * Use: Fetches data from the buffer into some other place.
+ */
+
+extern int buf_get(buf */*b*/, void */*p*/, size_t /*sz*/);
+
+/* --- @buf_put@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ * @const void *p@ = pointer to a buffer
+ * @size_t sz@ = size of the buffer
+ *
+ * Returns: Zero if it worked, nonzero if there wasn't enough space.
+ *
+ * Use: Fetches data from some place and puts it in the buffer
+ */
+
+extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
+
+/* --- @buf_getbyte@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ *
+ * Returns: A byte, or less than zero if there wasn't a byte there.
+ *
+ * Use: Gets a single byte from a buffer.
+ */
+
+extern int buf_getbyte(buf */*b*/);
+
+/* --- @buf_putbyte@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ * @int ch@ = byte to write
+ *
+ * Returns: Zero if OK, nonzero if there wasn't enough space.
+ *
+ * Use: Puts a single byte in a buffer.
+ */
+
+extern int buf_putbyte(buf */*b*/, int /*ch*/);
+
+/* --- @buf_getword@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ * @uint32 *w@ = where to put the word
+ *
+ * Returns: Zero if OK, or nonzero if there wasn't a word there.
+ *
+ * Use: Gets a 32-bit word from a buffer.
+ */
+
+extern int buf_getword(buf */*b*/, uint32 */*w*/);
+
+/* --- @buf_putword@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ * @uint32 w@ = word to write
+ *
+ * Returns: Zero if OK, nonzero if there wasn't enough space.
+ *
+ * Use: Puts a 32-but word in a buffer.
+ */
+
+extern int buf_putword(buf */*b*/, uint32 /*w*/);
+
+/* --- @buf_getmp@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ *
+ * Returns: A multiprecision integer, or null if there wasn't one there.
+ *
+ * Use: Gets a multiprecision integer from a buffer.
+ */
+
+extern mp *buf_getmp(buf */*b*/, mp */*d*/);
+
+/* --- @buf_putmp@ --- *
+ *
+ * Arguments: @buf *b@ = pointer to a buffer block
+ * @mp *m@ = a multiprecision integer
+ *
+ * Returns: Zero if it worked, nonzero if there wasn't enough space.
+ *
+ * Use: Puts a multiprecision integer to a buffer.
+ */
+
+extern int buf_putmp(buf */*b*/, mp */*m*/);
+
+/*----- Other handy utilities ---------------------------------------------*/
+
+/* --- @mpstr@ --- *
+ *
+ * Arguments: @mp *m@ = a multiprecision integer
+ *
+ * Returns: A pointer to the integer's textual representation.
+ *
+ * Use: Converts a multiprecision integer to a string. Corrupts
+ * @buf_o@.
+ */
+
+extern const char *mpstr(mp */*m*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif