chiark / gitweb /
40ad1c1ef7e7cd6c4283221adf9a2c0c9e8d6e21
[tripe] / tripe.h
1 /* -*-c-*-
2  *
3  * $Id: tripe.h,v 1.2 2001/02/03 22:40:29 mdw Exp $
4  *
5  * Main header file for TrIPE
6  *
7  * (c) 2001 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Trivial IP Encryption (TrIPE).
13  *
14  * TrIPE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * TrIPE is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with TrIPE; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: tripe.h,v $
32  * Revision 1.2  2001/02/03 22:40:29  mdw
33  * Put timer information into the entropy pool when packets are received
34  * and on similar events.  Reseed the generator on the interval timer.
35  *
36  * Revision 1.1  2001/02/03 20:26:37  mdw
37  * Initial checkin.
38  *
39  */
40
41 #ifndef TRIPE_H
42 #define TRIPE_H
43
44 #ifdef __cplusplus
45   extern "C" {
46 #endif
47
48 /*----- Header files ------------------------------------------------------*/
49
50 #include <assert.h>
51 #include <ctype.h>
52 #include <errno.h>
53 #include <signal.h>
54 #include <stdarg.h>
55 #include <stddef.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <time.h>
60
61 #include <sys/types.h>
62 #include <sys/time.h>
63 #include <unistd.h>
64 #include <fcntl.h>
65 #include <sys/stat.h>
66
67 #include <sys/socket.h>
68 #include <sys/un.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71 #include <netdb.h>
72
73 #include <pwd.h>
74 #include <grp.h>
75
76 #include <mLib/alloc.h>
77 #include <mLib/arena.h>
78 #include <mLib/bres.h>
79 #include <mLib/dstr.h>
80 #include <mLib/env.h>
81 #include <mLib/fdflags.h>
82 #include <mLib/fwatch.h>
83 #include <mLib/mdwopt.h>
84 #include <mLib/quis.h>
85 #include <mLib/report.h>
86 #include <mLib/sel.h>
87 #include <mLib/selbuf.h>
88 #include <mLib/sig.h>
89 #include <mLib/str.h>
90 #include <mLib/sub.h>
91 #include <mLib/trace.h>
92
93 #include <catacomb/gcipher.h>
94 #include <catacomb/gmac.h>
95 #include <catacomb/grand.h>
96 #include <catacomb/key.h>
97 #include <catacomb/paranoia.h>
98
99 #include <catacomb/blowfish.h>
100 #include <catacomb/blowfish-cbc.h>
101 #include <catacomb/noise.h>
102 #include <catacomb/rand.h>
103 #include <catacomb/rmd160.h>
104 #include <catacomb/rmd160-hmac.h>
105
106 #include <catacomb/mp.h>
107 #include <catacomb/mpmont.h>
108 #include <catacomb/mprand.h>
109 #include <catacomb/dh.h>
110
111 #include "util.h"
112
113 #undef sun
114
115 /*----- Magic numbers -----------------------------------------------------*/
116
117 /* --- Tunnel types --- */
118
119 #define TUN_NOTDEF 0
120 #define TUN_UNET 1
121 #define TUN_BSD 2
122
123 /* --- Trace flags --- */
124
125 #define T_TUNNEL 1u
126 #define T_PEER 2u
127 #define T_PACKET 4u
128 #define T_ADMIN 8u
129 #define T_CRYPTO 16u
130 #define T_KEYSET 32u
131 #define T_KEYEXCH 64u
132 #define T_KEYMGMT 128u
133
134 #define T_ALL 255u
135
136 /* --- Units --- */
137
138 #define SEC(n) (n##u)
139 #define MIN(n) (n##u * 60u)
140 #define MEG(n) (n##ul * 1024ul * 1024ul)
141
142 /* --- Other things --- */
143
144 #define PKBUFSZ 65536
145
146 /*----- TrIPE protocol ----------------------------------------------------*/
147
148 /* --- TrIPE packet format --- *
149  *
150  * A packet begins with a single-byte packet type.  The remaining data
151  * depends on the packet type.
152  */
153
154 #define MSG_PACKET 0u
155 /* Followed by a 64-bit MAC and an encrypted packet.  The MAC is used as an
156  * IV for a 64-bit block cipher in CBC-stealing mode.
157  */
158
159 #define MSG_PRECHALLENGE 1u
160 /* Followed by the challenge only.  Useful for bootstrapping the system.
161  */
162
163 #define MSG_CHALLENGE 2u
164 /* Followed by a response hash and a large-integer challenge.
165  */
166
167 #define MSG_RESPONSE 3u
168 /* Followed by a large-integer response.
169  */
170
171 /*----- Data structures ---------------------------------------------------*/
172
173 /* --- Buffers --- *
174  *
175  * Buffers provide a simple stream-like interface for building and parsing
176  * packets.
177  */
178
179 typedef struct buf {
180   octet *base, *p, *limit;              /* Pointers to the buffer */
181   unsigned f;                           /* Various flags */
182 } buf;
183
184 #define BF_BROKEN 1u                    /* Buffer is broken */
185
186 /* --- Socket addresses --- *
187  *
188  * A magic union of supported socket addresses.
189  */
190
191 typedef union addr {
192   struct sockaddr sa;
193   struct sockaddr_in sin;
194 } addr;
195
196 /* --- A symmetric keyset --- *
197  *
198  * A keyset contains a set of symmetric keys for encrypting and decrypting
199  * packets.  Keysets are stored in a list, sorted in reverse order of
200  * creation, so that the most recent keyset (the one most likely to be used)
201  * is first.
202  *
203  * Each keyset has a time limit and a data limit.  The keyset is destroyed
204  * when either it has existed for too long, or it has been used to encrypt
205  * too much data.  New key exchanges are triggered when keys are close to
206  * expiry.
207  */
208
209 typedef struct keyset {
210   struct keyset *next;                  /* Next active keyset in the list */
211   time_t t_exp;                         /* Expiry time for this keyset */
212   unsigned long sz_exp;                 /* Data limit for the keyset */
213 #ifndef NTRACE
214   unsigned seq;                         /* Sequence number for tracing */
215 #endif
216   gcipher *c;                           /* Keyset cipher for encryption */
217   gmac *m;                              /* Keyset MAC for integrity */
218 } keyset;
219
220 /* --- Key exchange --- *
221  *
222  * TrIPE uses the Wrestlers Protocol for its key exchange.  The Wrestlers
223  * Protocol has a number of desirable features (e.g., perfect forward
224  * secrecy, and zero-knowledge authentication) which make it attractive for
225  * use in TrIPE.  The Wrestlers Protocol was designed by Mark Wooding and
226  * Clive Jones.
227  */
228
229 typedef struct keyexch {
230   keyset **ks;                          /* Peer's list of keysets */
231   struct peer *p;                       /* Pointer back to the peer */
232   unsigned f;                           /* Various useful flags */
233   sel_timer t;                          /* Timer for next exchange */
234   dh_pub kpub;                          /* Peer's public key */
235   mp *my_x, *my_gx, *my_gxy;            /* My half of the exchange */
236   octet my_h[RMD160_HASHSZ];            /* My challenge hash */
237   mp *your_gx, *your_gxy;               /* Your half of the exchange */
238   octet your_h[RMD160_HASHSZ];          /* Your challenge hash */
239   time_t t_valid;                       /* When this exchange goes bad */
240   time_t t_qchal, t_qresp;              /* Quiet timers for packet types */
241   time_t t_newchal;                     /* When to accept a new challenge */
242 } keyexch;
243
244 #define KXF_TIMER 1u                    /* Waiting for a timer to go off */
245 #define KXF_INIT 2u                     /* Big numbers are initialized */
246 #define KXF_MYH 4u                      /* My hash has been computed */
247 #define KXF_YOURH 8u                    /* Your hash has been received */
248 #define KXF_REPLY 16u                   /* Received your response OK */
249 #define KXF_DONE 32u                    /* Key exchange completed */
250
251 /* --- Tunnel structure --- *
252  *
253  * Used to maintain system-specific information about the tunnel interface.
254  */
255
256 typedef struct tunnel {
257 #if TUN_TYPE == TUN_UNET
258   sel_file f;                           /* Selector for Usernet device */
259   struct peer *p;                       /* Pointer to my peer */
260 #else
261 #  error "No support for this tunnel type"
262 #endif
263 } tunnel;
264
265 /* --- Peer structure --- *
266  *
267  * The main structure which glues everything else together.
268  */
269
270 typedef struct peer {
271   struct peer *next, *prev;             /* Links to next and previous */
272   char *name;                           /* Name of this peer */
273   tunnel t;                             /* Tunnel for local packets */
274   keyset *ks;                           /* List head for keysets */
275   keyexch kx;                           /* Key exchange protocol block */
276   buf b;                                /* Buffer for sending packets */
277   addr peer;                            /* Peer socket address */
278   size_t sasz;                          /* Socket address size */
279 } peer;
280
281 /* --- Admin structure --- */
282
283 typedef struct admin {
284   struct admin *next, *prev;            /* Links to next and previous */
285   selbuf b;                             /* Line buffer for commands */
286   int fd;                               /* File descriptor for output */
287 #ifndef NTRACE
288   unsigned seq;                         /* Sequence number for tracing */
289 #endif
290   char *pname;                          /* Peer name to create */
291   char *paddr;                          /* Address string to resolve */
292   bres_client r;                        /* Background resolver task */
293   sel_timer t;                          /* Timer for resolver */
294   addr peer;                            /* Address to set */
295   size_t sasz;                          /* Size of the address */
296 } admin;
297
298 /*----- Global variables --------------------------------------------------*/
299
300 extern sel_state sel;                   /* Global I/O event state */
301 extern dh_priv kpriv;                   /* Our private key */
302 extern mpmont mg;                       /* Montgomery context for DH group */
303 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ]; /* Big packet buffers */
304
305 #ifndef NTRACE
306 extern const trace_opt tr_opts[];       /* Trace options array */
307 extern unsigned tr_flags;               /* Trace options flags */
308 #endif
309
310 /*----- Other macros ------------------------------------------------------*/
311
312 #define TIMER noise_timer(RAND_GLOBAL)
313
314 /*----- Key management ----------------------------------------------------*/
315
316 /* --- @km_interval@ --- *
317  *
318  * Arguments:   ---
319  *
320  * Returns:     Zero if OK, nonzero to force reloading of keys.
321  *
322  * Use:         Called on the interval timer to perform various useful jobs.
323  */
324
325 extern int km_interval(void);
326
327 /* --- @km_init@ --- *
328  *
329  * Arguments:   @const char *kr_priv@ = private keyring file
330  *              @const char *kr_pub@ = public keyring file
331  *              @const char *tag@ = tag to load
332  *
333  * Returns:     ---
334  *
335  * Use:         Initializes, and loads the private key.
336  */
337
338 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
339                     const char */*tag*/);
340
341 /* --- @km_getpubkey@ --- *
342  *
343  * Arguments:   @const char *tag@ = public key tag to load
344  *              @dh_pub *kpub@ = where to put the public key
345  *
346  * Returns:     Zero if OK, nonzero if it failed.
347  *
348  * Use:         Fetches a public key from the keyring.
349  */
350
351 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/);
352
353 /*----- Key exchange ------------------------------------------------------*/
354
355 /* --- @kx_start@ --- *
356  *
357  * Arguments:   @keyexch *kx@ = pointer to key exchange context
358  *
359  * Returns:     ---
360  *
361  * Use:         Stimulates a key exchange.  If a key exchage is in progress,
362  *              a new challenge is sent (unless the quiet timer forbids
363  *              this); if no exchange is in progress, one is commenced.
364  */
365
366 extern void kx_start(keyexch */*kx*/);
367
368 /* --- @kx_prechallenge@ --- *
369  *
370  * Arguments:   @keyexch *kx@ = pointer to key exhange context
371  *              @buf *b@ = pointer to buffer containing the packet
372  *
373  * Returns:     ---
374  *
375  * Use:         Reads a prechallenge packet from the buffer and handles it.
376  */
377
378 extern void kx_prechallenge(keyexch */*kx*/, buf */*b*/);
379
380 /* --- @kx_challenge@ --- *
381  *
382  * Arguments:   @keyexch *kx@ = pointer to key exchange context
383  *              @buf *b@ = a buffer containing the packet to read
384  *
385  * Returns:     ---
386  *
387  * Use:         Reads a challenge from the buffer and handles it.
388  */
389
390 extern void kx_challenge(keyexch */*kx*/, buf */*b*/);
391
392 /* --- @kx_response@ --- *
393  *
394  * Arguments:   @keyexch *kx@ = pointer to key exchange context
395  *              @buf *b@ = a buffer containing the packet to read
396  *
397  * Returns:     ---
398  *
399  * Use:         Reads a response from the buffer and handles it.
400  */
401
402 extern void kx_response(keyexch */*kx*/, buf */*b*/);
403
404 /* --- @kx_free@ --- *
405  *
406  * Arguments:   @keyexch *kx@ = pointer to key exchange context
407  *
408  * Returns:     ---
409  *
410  * Use:         Frees everything in a key exchange context.
411  */
412
413 extern void kx_free(keyexch */*kx*/);
414
415 /* --- @kx_newkeys@ --- *
416  *
417  * Arguments:   @keyexch *kx@ = pointer to key exchange context
418  *
419  * Returns:     ---
420  *
421  * Use:         Informs the key exchange module that its keys may have
422  *              changed.  If fetching the new keys fails, the peer will be
423  *              destroyed, we log messages and struggle along with the old
424  *              keys.
425  */
426
427 extern void kx_newkeys(keyexch */*kx*/);
428
429 /* --- @kx_init@ --- *
430  *
431  * Arguments:   @keyexch *kx@ = pointer to key exchange context
432  *              @peer *p@ = pointer to peer context
433  *              @keyset **ks@ = pointer to keyset list
434  *
435  * Returns:     Zero if OK, nonzero if it failed.
436  *
437  * Use:         Initializes a key exchange module.  The module currently
438  *              contains no keys, and will attempt to initiate a key
439  *              exchange.
440  */
441
442 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
443
444 /*----- Keysets and symmetric cryptography --------------------------------*/
445
446 /* --- @ks_free@ --- *
447  *
448  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
449  *
450  * Returns:     ---
451  *
452  * Use:         Frees all of the keys in a keyset.
453  */
454
455 extern void ks_free(keyset **/*ksroot*/);
456
457 /* --- @ks_prune@ --- *
458  *
459  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
460  *
461  * Returns:     ---
462  *
463  * Use:         Prunes the keyset list by removing keys which mustn't be used
464  *              any more.
465  */
466
467 extern void ks_prune(keyset **/*ksroot*/);
468
469 /* --- @ks_gen@ --- *
470  *
471  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
472  *              @const void *k@ = pointer to key material
473  *              @size_t sz@ = size of the key material
474  *
475  * Returns:     The regeneration time for the new key.
476  *
477  * Use:         Derives a keyset from the given key material and adds it to
478  *              the list.
479  */
480
481 extern time_t ks_gen(keyset **/*ksroot*/, const void */*k*/, size_t /*sz*/);
482
483 /* --- @ks_encrypt@ --- *
484  *
485  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
486  *              @buf *b@ = pointer to input buffer
487  *              @buf *bb@ = pointer to output buffer
488  *
489  * Returns:     Nonzero if a new key is needed.
490  *
491  * Use:         Encrypts a packet.
492  */
493
494 extern int ks_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
495
496 /* --- @ks_decrypt@ --- *
497  *
498  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
499  *              @buf *b@ = pointer to input buffer
500  *              @buf *bb@ = pointer to output buffer
501  *
502  * Returns:     Nonzero if the packet couldn't be decrypted.
503  *
504  * Use:         Decrypts a packet.
505  */
506
507 extern int ks_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
508
509 /*----- Administration interface ------------------------------------------*/
510
511 /* --- @a_warn@ --- *
512  *
513  * Arguments:   @const char *fmt@ = pointer to format string
514  *              @...@ = other arguments
515  *
516  * Returns:     ---
517  *
518  * Use:         Informs all admin connections of a warning.
519  */
520
521 extern void a_warn(const char */*fmt*/, ...);
522
523 /* --- @a_create@ --- *
524  *
525  * Arguments:   @int fd_in, fd_out@ = file descriptors to use
526  *
527  * Returns:     ---
528  *
529  * Use:         Creates a new admin connection.
530  */
531
532 extern void a_create(int /*fd_in*/, int /*fd_out*/);
533
534 /* --- @a_quit@ --- *
535  *
536  * Arguments:   ---
537  *
538  * Returns:     ---
539  *
540  * Use:         Shuts things down nicely.
541  */
542
543 extern void a_quit(void);
544
545 /* --- @a_daemon@ --- *
546  *
547  * Arguments:   ---
548  *
549  * Returns:     ---
550  *
551  * Use:         Informs the admin module that it's a daemon.
552  */
553
554 extern void a_daemon(void);
555
556 /* --- @a_init@ --- *
557  *
558  * Arguments:   @const char *sock@ = socket name to create
559  *
560  * Returns:     ---
561  *
562  * Use:         Creates the admin listening socket.
563  */
564
565 extern void a_init(const char */*sock*/);
566
567 /*----- Peer management ---------------------------------------------------*/
568
569 /* --- @p_txstart@ --- *
570  *
571  * Arguments:   @peer *p@ = pointer to peer block
572  *              @unsigned msg@ = message type code
573  *
574  * Returns:     A pointer to a buffer to write to.
575  *
576  * Use:         Starts sending to a peer.  Only one send can happen at a
577  *              time.
578  */
579
580 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
581
582 /* --- @p_txend@ --- *
583  *
584  * Arguments:   @peer *p@ = pointer to peer block
585  *
586  * Returns:     ---
587  *
588  * Use:         Sends a packet to the peer.
589  */
590
591 extern void p_txend(peer */*p*/);
592
593 /* --- @p_tun@ --- *
594  *
595  * Arguments:   @peer *p@ = pointer to peer block
596  *              @buf *b@ = buffer containing incoming packet
597  *
598  * Returns:     ---
599  *
600  * Use:         Handles a packet which needs to be sent to a peer.
601  */
602
603 extern void p_tun(peer */*p*/, buf */*b*/);
604
605 /* --- @p_interval@ --- *
606  *
607  * Arguments:   ---
608  *
609  * Returns:     ---
610  *
611  * Use:         Called periodically to do tidying.
612  */
613
614 extern void p_interval(void);
615
616 /* --- @p_ifname@ --- *
617  *
618  * Arguments:   @peer *p@ = pointer to a peer block
619  *
620  * Returns:     A pointer to the peer's interface name.
621  */
622
623 extern const char *p_ifname(peer */*p*/);
624
625 /* --- @p_addr@ --- *
626  *
627  * Arguments:   @peer *p@ = pointer to a peer block
628  *
629  * Returns:     A pointer to the peer's address.
630  */
631
632 extern const addr *p_addr(peer */*p*/);
633
634 /* --- @p_init@ --- *
635  *
636  * Arguments:   @unsigned port@ = port number to listen to
637  *
638  * Returns:     ---
639  *
640  * Use:         Initializes the peer system; creates the socket.
641  */
642
643 extern void p_init(unsigned /*port*/);
644
645 /* --- @p_port@ --- *
646  *
647  * Arguments:   ---
648  *
649  * Returns:     Port number used for socket.
650  */
651
652 unsigned p_port(void);
653
654 /* --- @p_create@ --- *
655  *
656  * Arguments:   @const char *name@ = name for this peer
657  *              @struct sockaddr *sa@ = socket address of peer
658  *              @size_t sz@ = size of socket address
659  *
660  * Returns:     Pointer to the peer block, or null if it failed.
661  *
662  * Use:         Creates a new named peer block.  No peer is actually attached
663  *              by this point.
664  */
665
666 extern peer *p_create(const char */*name*/,
667                       struct sockaddr */*sa*/, size_t /*sz*/);
668
669 /* --- @p_name@ --- *
670  *
671  * Arguments:   @peer *p@ = pointer to a peer block
672  *
673  * Returns:     A pointer to the peer's name.
674  */
675
676 extern const char *p_name(peer */*p*/);
677
678 /* --- @p_find@ --- *
679  *
680  * Arguments:   @const char *name@ = name to look up
681  *
682  * Returns:     Pointer to the peer block, or null if not found.
683  *
684  * Use:         Finds a peer by name.
685  */
686
687 extern peer *p_find(const char */*name*/);
688
689 /* --- @p_destroy@ --- *
690  *
691  * Arguments:   @peer *p@ = pointer to a peer
692  *
693  * Returns:     ---
694  *
695  * Use:         Destroys a peer.
696  */
697
698 extern void p_destroy(peer */*p*/);
699
700 /* --- @p_first@, @p_next@ --- *
701  *
702  * Arguments:   @peer *p@ = a peer block
703  *
704  * Returns:     @peer_first@ returns the first peer in some ordering;
705  *              @peer_next@ returns the peer following a given one in the
706  *              same ordering.  Null is returned for the end of the list.
707  */
708
709 extern peer *p_first(void);
710 extern peer *p_next(peer */*p*/);
711
712 /*----- Tunnel interface --------------------------------------------------*/
713
714 /* --- @tun_init@ --- *
715  *
716  * Arguments:   ---
717  *
718  * Returns:     ---
719  *
720  * Use:         Initializes the tunneling system.  Maybe this will require
721  *              opening file descriptors or something.
722  */
723
724 extern void tun_init(void);
725
726 /* --- @tun_create@ --- *
727  *
728  * Arguments:   @tunnel *t@ = pointer to tunnel block
729  *              @peer *p@ = pointer to peer block
730  *
731  * Returns:     Zero if it worked, nonzero on failure.
732  *
733  * Use:         Initializes a new tunnel.
734  */
735
736 extern int tun_create(tunnel */*t*/, peer */*p*/);
737
738 /* --- @tun_ifname@ --- *
739  *
740  * Arguments:   @tunnel *t@ = pointer to tunnel block
741  *
742  * Returns:     A pointer to the tunnel's interface name.
743  */
744
745 extern const char *tun_ifname(tunnel */*t*/);
746
747 /* --- @tun_inject@ --- *
748  *
749  * Arguments:   @tunnel *t@ = pointer to tunnel block
750  *              @buf *b@ = buffer to send
751  *
752  * Returns:     ---
753  *
754  * Use:         Injects a packet into the local network stack.
755  */
756
757 extern void tun_inject(tunnel */*t*/, buf */*b*/);
758
759 /* --- @tun_destroy@ --- *
760  *
761  * Arguments:   @tunnel *t@ = pointer to tunnel block
762  *
763  * Returns:     ---
764  *
765  * Use:         Destroys a tunnel.
766  */
767
768 extern void tun_destroy(tunnel */*t*/);
769
770 /*----- Buffer handling ---------------------------------------------------*/
771
772 /* --- Useful macros --- */
773
774 #define BBASE(b) ((b)->base)
775 #define BLIM(b) ((b)->limit)
776 #define BCUR(b) ((b)->p)
777 #define BSZ(b) ((b)->limit - (b)->base)
778 #define BLEN(b) ((b)->p - (b)->base)
779 #define BLEFT(b) ((b)->limit - (b)->p)
780 #define BSTEP(b, sz) ((b)->p += (sz))
781 #define BBAD(b) ((b)->f & BF_BROKEN)
782 #define BOK(b) (!BBAD(b))
783
784 #define BENSURE(b, sz)                                                  \
785   (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
786
787 /* --- @buf_init@ --- *
788  *
789  * Arguments:   @buf *b@ = pointer to a buffer block
790  *              @void *p@ = pointer to a buffer
791  *              @size_t sz@ = size of the buffer
792  *
793  * Returns:     ---
794  *
795  * Use:         Initializes the buffer block appropriately.
796  */
797
798 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
799
800 /* --- @buf_break@ --- *
801  *
802  * Arguments:   @buf *b@ = pointer to a buffer block
803  *
804  * Returns:     Some negative value.
805  *
806  * Use:         Marks a buffer as broken.
807  */
808
809 extern int buf_break(buf */*b*/);
810
811 /* --- @buf_ensure@ --- *
812  *
813  * Arguments:   @buf *b@ = pointer to a buffer block
814  *              @size_t sz@ = size of data wanted
815  *
816  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
817  *
818  * Use:         Ensures that there are @sz@ bytes still in the buffer.
819  */
820
821 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
822
823 /* --- @buf_get@ --- *
824  *
825  * Arguments:   @buf *b@ = pointer to a buffer block
826  *              @void *p@ = pointer to a buffer
827  *              @size_t sz@ = size of the buffer
828  *
829  * Returns:     Zero if it worked, nonzero if there wasn't enough data.
830  *
831  * Use:         Fetches data from the buffer into some other place.
832  */
833
834 extern int buf_get(buf */*b*/, void */*p*/, size_t /*sz*/);
835
836 /* --- @buf_put@ --- *
837  *
838  * Arguments:   @buf *b@ = pointer to a buffer block
839  *              @const void *p@ = pointer to a buffer
840  *              @size_t sz@ = size of the buffer
841  *
842  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
843  *
844  * Use:         Fetches data from some place and puts it in the buffer
845  */
846
847 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
848
849 /* --- @buf_getbyte@ --- *
850  *
851  * Arguments:   @buf *b@ = pointer to a buffer block
852  *
853  * Returns:     A byte, or less than zero if there wasn't a byte there.
854  *
855  * Use:         Gets a single byte from a buffer.
856  */
857
858 extern int buf_getbyte(buf */*b*/);
859
860 /* --- @buf_putbyte@ --- *
861  *
862  * Arguments:   @buf *b@ = pointer to a buffer block
863  *              @int ch@ = byte to write
864  *
865  * Returns:     Zero if OK, nonzero if there wasn't enough space.
866  *
867  * Use:         Puts a single byte in a buffer.
868  */
869
870 extern int buf_putbyte(buf */*b*/, int /*ch*/);
871
872 /* --- @buf_getword@ --- *
873  *
874  * Arguments:   @buf *b@ = pointer to a buffer block
875  *              @uint32 *w@ = where to put the word
876  *
877  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
878  *
879  * Use:         Gets a 32-bit word from a buffer.
880  */
881
882 extern int buf_getword(buf */*b*/, uint32 */*w*/);
883
884 /* --- @buf_putword@ --- *
885  *
886  * Arguments:   @buf *b@ = pointer to a buffer block
887  *              @uint32 w@ = word to write
888  *
889  * Returns:     Zero if OK, nonzero if there wasn't enough space.
890  *
891  * Use:         Puts a 32-but word in a buffer.
892  */
893
894 extern int buf_putword(buf */*b*/, uint32 /*w*/);
895
896 /* --- @buf_getmp@ --- *
897  *
898  * Arguments:   @buf *b@ = pointer to a buffer block
899  *
900  * Returns:     A multiprecision integer, or null if there wasn't one there.
901  *
902  * Use:         Gets a multiprecision integer from a buffer.
903  */
904
905 extern mp *buf_getmp(buf */*b*/, mp */*d*/);
906
907 /* --- @buf_putmp@ --- *
908  *
909  * Arguments:   @buf *b@ = pointer to a buffer block
910  *              @mp *m@ = a multiprecision integer
911  *
912  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
913  *
914  * Use:         Puts a multiprecision integer to a buffer.
915  */
916
917 extern int buf_putmp(buf */*b*/, mp */*m*/);
918
919 /*----- Other handy utilities ---------------------------------------------*/
920
921 /* --- @mpstr@ --- *
922  *
923  * Arguments:   @mp *m@ = a multiprecision integer
924  *
925  * Returns:     A pointer to the integer's textual representation.
926  *
927  * Use:         Converts a multiprecision integer to a string.  Corrupts
928  *              @buf_o@.
929  */
930
931 extern const char *mpstr(mp */*m*/);
932
933 /*----- That's all, folks -------------------------------------------------*/
934
935 #ifdef __cplusplus
936   }
937 #endif
938
939 #endif