chiark / gitweb /
069840e7358d623218aa83072261b5d4e565b7f3
[tripe] / tripe.h
1 /* -*-c-*-
2  *
3  * $Id: tripe.h,v 1.12 2003/04/06 10:25:17 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.12  2003/04/06 10:25:17  mdw
33  * Support Linux TUN/TAP device.  Fix some bugs.
34  *
35  * Revision 1.11  2002/01/13 14:57:42  mdw
36  * Fix crap typo.
37  *
38  * Revision 1.10  2002/01/13 14:54:58  mdw
39  * Provide MGF macros.
40  *
41  * Revision 1.9  2001/06/22 19:40:36  mdw
42  * Support expiry of other peers' public keys.
43  *
44  * Revision 1.8  2001/06/19 22:10:57  mdw
45  * Some more constants for the algorithms.  Document the packet format
46  * change for non-malleability.  Moved @buf@ definitions to separate header
47  * file.
48  *
49  * Revision 1.7  2001/03/03 12:07:08  mdw
50  * Rename word get and put functions now that there's 16-bit support.
51  *
52  * Revision 1.6  2001/02/19 19:11:09  mdw
53  * Output buffering on admin connections.
54  *
55  * Revision 1.5  2001/02/16 21:41:43  mdw
56  * Major changes.  See source files for details.
57  *
58  * Revision 1.4  2001/02/05 19:56:37  mdw
59  * Sequence number protection, and BSD tunnels.
60  *
61  * Revision 1.3  2001/02/04 01:17:55  mdw
62  * Create a configuration header file to tidy up command lines.
63  *
64  * Revision 1.2  2001/02/03 22:40:29  mdw
65  * Put timer information into the entropy pool when packets are received
66  * and on similar events.  Reseed the generator on the interval timer.
67  *
68  * Revision 1.1  2001/02/03 20:26:37  mdw
69  * Initial checkin.
70  *
71  */
72
73 #ifndef TRIPE_H
74 #define TRIPE_H
75
76 #ifdef __cplusplus
77   extern "C" {
78 #endif
79
80 /*----- Header files ------------------------------------------------------*/
81
82 #include "config.h"
83
84 #include <assert.h>
85 #include <ctype.h>
86 #include <errno.h>
87 #include <signal.h>
88 #include <stdarg.h>
89 #include <stddef.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <time.h>
94
95 #include <sys/types.h>
96 #include <sys/time.h>
97 #include <unistd.h>
98 #include <fcntl.h>
99 #include <sys/stat.h>
100
101 #include <sys/socket.h>
102 #include <sys/un.h>
103 #include <netinet/in.h>
104 #include <arpa/inet.h>
105 #include <netdb.h>
106
107 #if TUN_TYPE == TUN_LINUX
108 #  include <linux/if.h>
109 #  include <linux/if_tun.h>
110 #endif
111
112 #include <pwd.h>
113 #include <grp.h>
114
115 #include <mLib/alloc.h>
116 #include <mLib/arena.h>
117 #include <mLib/bres.h>
118 #include <mLib/dstr.h>
119 #include <mLib/env.h>
120 #include <mLib/fdflags.h>
121 #include <mLib/fwatch.h>
122 #include <mLib/mdwopt.h>
123 #include <mLib/quis.h>
124 #include <mLib/report.h>
125 #include <mLib/sel.h>
126 #include <mLib/selbuf.h>
127 #include <mLib/sig.h>
128 #include <mLib/str.h>
129 #include <mLib/sub.h>
130 #include <mLib/trace.h>
131
132 #include <catacomb/gcipher.h>
133 #include <catacomb/gmac.h>
134 #include <catacomb/grand.h>
135 #include <catacomb/key.h>
136 #include <catacomb/paranoia.h>
137
138 #include <catacomb/noise.h>
139 #include <catacomb/rand.h>
140
141 #include <catacomb/mp.h>
142 #include <catacomb/mpmont.h>
143 #include <catacomb/mprand.h>
144 #include <catacomb/dh.h>
145
146 #include "buf.h"
147 #include "util.h"
148
149 #undef sun
150
151 /*----- Magic numbers -----------------------------------------------------*/
152
153 /* --- Tunnel types --- */
154
155 #define TUN_NOTDEF 0
156 #define TUN_UNET 1
157 #define TUN_BSD 2
158 #define TUN_LINUX 3
159
160 /* --- Trace flags --- */
161
162 #define T_TUNNEL 1u
163 #define T_PEER 2u
164 #define T_PACKET 4u
165 #define T_ADMIN 8u
166 #define T_CRYPTO 16u
167 #define T_KEYSET 32u
168 #define T_KEYEXCH 64u
169 #define T_KEYMGMT 128u
170
171 #define T_ALL 255u
172
173 /* --- Units --- */
174
175 #define SEC(n) (n##u)
176 #define MIN(n) (n##u * 60u)
177 #define MEG(n) (n##ul * 1024ul * 1024ul)
178
179 /* --- Other things --- */
180
181 #define PKBUFSZ 65536
182
183 /*----- TrIPE protocol ----------------------------------------------------*/
184
185 /* --- TrIPE message format --- *
186  *
187  * A packet begins with a single-byte message type.  The top four bits are a
188  * category code used to send the message to the right general place in the
189  * code; the bottom bits identify the actual message type.
190  */
191
192 #define MSG_CATMASK 0xf0
193 #define MSG_TYPEMASK 0x0f
194
195 /* --- Encrypted message packets --- *
196  *
197  * Messages of category @MSG_PACKET@ contain encrypted network packets.  The
198  * message content is a symmetric-encrypted block (see below).  Reception of
199  * a packet encrypted under a new key implicitly permits that key to be used
200  * to send further packets.
201  *
202  * The only packet type accepted is zero.
203  *
204  * Packets may be encrypted under any live keyset, but should use the most
205  * recent one.
206  */
207
208 #define MSG_PACKET 0x00
209
210 /* --- Key exchange packets --- */
211
212 #define MSG_KEYEXCH 0x10
213
214 #define KX_PRECHAL 0u
215 #define KX_COOKIE 1u
216 #define KX_CHAL 2u
217 #define KX_REPLY 3u
218 #define KX_SWITCH 4u
219 #define KX_SWITCHOK 5u
220 #define KX_NMSG 6u
221
222 /* --- Symmetric encryption and keysets --- *
223  *
224  * Packets consist of an 80-bit MAC, a 32-bit sequence number, and the
225  * encrypted payload.
226  *
227  * The plaintext is encrypted using Blowfish in CBC mode with ciphertext
228  * stealing (as described in [Schneier].  The initialization vector is
229  * selected randomly, and prepended to the actual ciphertext.
230  *
231  * The MAC is computed using the HMAC construction with RIPEMD160 over the
232  * sequence number and the ciphertext (with IV); the first 80 bits of the
233  * output are used.  (This is the minimum allowed by the draft FIPS for HMAC,
234  * and the recommended truncation.)
235  *
236  * A keyset consists of
237  *
238  *   * an integrity (MAC) key;
239  *   * a confidentiality (encryption) key; and
240  *   * a sequence numbering space
241  *
242  * in each direction.  The packets sent by a host encrypted under a
243  * particular keyset are assigned consecutive sequence numbers starting from
244  * zero.  The receiving host must ensure that it only accepts each packet at
245  * most once.  It should maintain a window of sequence numbers: packets with
246  * numbers beyond the end of the window are accepted and cause the window to
247  * be advanced; packets with numbers before the start of the window are
248  * rejected; packets with numbers which appear within the window are accepted
249  * only if the number has not been seen before.
250  *
251  * When a host sends a @KX_SWITCH@ or @KX_SWITCHOK@ message, it installs the
252  * newly-negotiated keyset in a `listen-only' state: it may not send a packet
253  * encrypted under the keyset until either it has received a @KX_SWITCH@ or
254  * @KX_SWITCHOK@ message, or a @MSG_PACKET@ encrypted under the keyset, from
255  * its peer.
256  */
257
258 /*----- Cipher selections -------------------------------------------------*/
259
260 #include <catacomb/blowfish.h>
261 #include <catacomb/blowfish-cbc.h>
262 #include <catacomb/blowfish-counter.h>
263 #include <catacomb/rmd160.h>
264 #include <catacomb/rmd160-hmac.h>
265
266 #define CIPHER (&blowfish_cbc)
267 #define MAC (&rmd160_hmac)
268
269 #define HASH_CTX rmd160_ctx
270 #define HASH_INIT rmd160_init
271 #define HASH rmd160_hash
272 #define HASH_STRING(c, s) HASH((c), s, sizeof(s))
273 #define HASH_DONE rmd160_done
274 #define HASHSZ RMD160_HASHSZ
275
276 #define MGF_CTX blowfish_counterctx
277 #define MGF_INIT blowfish_counterinit
278 #define MGF_CRYPT blowfish_counterencrypt
279
280 #define SEQSZ 4
281 #define IVSZ BLOWFISH_BLKSZ
282 #define MACSZ 10
283
284 /*----- Data structures ---------------------------------------------------*/
285
286 /* --- Socket addresses --- *
287  *
288  * A magic union of supported socket addresses.
289  */
290
291 typedef union addr {
292   struct sockaddr sa;
293   struct sockaddr_in sin;
294 } addr;
295
296 /* --- A symmetric keyset --- *
297  *
298  * A keyset contains a set of symmetric keys for encrypting and decrypting
299  * packets.  Keysets are stored in a list, sorted in reverse order of
300  * creation, so that the most recent keyset (the one most likely to be used)
301  * is first.
302  *
303  * Each keyset has a time limit and a data limit.  The keyset is destroyed
304  * when either it has existed for too long, or it has been used to encrypt
305  * too much data.  New key exchanges are triggered when keys are close to
306  * expiry.
307  */
308
309 typedef struct keyset {
310   struct keyset *next;                  /* Next active keyset in the list */
311   unsigned ref;                         /* Reference count for keyset */
312   time_t t_exp;                         /* Expiry time for this keyset */
313   unsigned long sz_exp;                 /* Data limit for the keyset */
314   T( unsigned seq; )                    /* Sequence number for tracing */
315   unsigned f;                           /* Various useful flags */
316   gcipher *cin, *cout;                  /* Keyset ciphers for encryption */
317   gmac *min, *mout;                     /* Keyset MACs for integrity */
318   uint32 oseq;                          /* Outbound sequence number */
319   uint32 iseq, iwin;                    /* Inbound sequence number */
320 } keyset;
321
322 #define KS_SEQWINSZ 32                  /* Bits in sequence number window */
323
324 #define KSF_LISTEN 1u                   /* Don't encrypt packets yet */
325 #define KSF_LINK 2u                     /* Key is in a linked list */
326
327 /* --- Key exchange --- *
328  *
329  * TrIPE uses the Wrestlers Protocol for its key exchange.  The Wrestlers
330  * Protocol has a number of desirable features (e.g., perfect forward
331  * secrecy, and zero-knowledge authentication) which make it attractive for
332  * use in TrIPE.  The Wrestlers Protocol was designed by Mark Wooding and
333  * Clive Jones.
334  */
335
336 #define KX_NCHAL 16u
337 #define KX_THRESH 4u
338
339 typedef struct kxchal {
340   struct keyexch *kx;                   /* Pointer back to key exchange */
341   mp *c;                                /* Responder's challenge */
342   mp *r;                                /* My reply to the challenge */
343   keyset *ks;                           /* Pointer to temporary keyset */
344   unsigned f;                           /* Various useful flags */
345   sel_timer t;                          /* Response timer for challenge */
346   octet hc[HASHSZ];                     /* Hash of his challenge */
347   mp *ck;                               /* The check value */
348   octet hswrq_in[HASHSZ];               /* Inbound switch request message */
349   octet hswok_in[HASHSZ];               /* Inbound switch confirmation */
350   octet hswrq_out[HASHSZ];              /* Outbound switch request message */
351   octet hswok_out[HASHSZ];              /* Outbound switch confirmation */
352 } kxchal;
353
354 typedef struct keyexch {
355   struct peer *p;                       /* Pointer back to the peer */
356   keyset **ks;                          /* Peer's list of keysets */
357   unsigned f;                           /* Various useful flags */
358   unsigned s;                           /* Current state in exchange */
359   sel_timer t;                          /* Timer for next exchange */
360   dh_pub kpub;                          /* Peer's public key */
361   time_t texp_kpub;                     /* Expiry time for public key */
362   mp *alpha;                            /* My temporary secret */
363   mp *c;                                /* My challenge */
364   mp *rx;                               /* The expected response */
365   unsigned nr;                          /* Number of extant responses */
366   time_t t_valid;                       /* When this exchange goes bad */
367   octet hc[HASHSZ];                     /* Hash of my challenge */
368   kxchal *r[KX_NCHAL];                  /* Array of challenges */
369 } keyexch;
370
371 #define KXF_TIMER 1u                    /* Waiting for a timer to go off */
372 #define KXF_DEAD 2u                     /* The key-exchanger isn't up */
373 #define KXF_PUBKEY 4u                   /* Key exchanger has a public key */
374
375 enum {
376   KXS_DEAD,                             /* Uninitialized state (magical) */
377   KXS_CHAL,                             /* Main answer-challenges state */
378   KXS_COMMIT,                           /* Committed: send switch request */
379   KXS_SWITCH                            /* Switched: send confirmation */
380 };
381
382 /* --- Tunnel structure --- *
383  *
384  * Used to maintain system-specific information about the tunnel interface.
385  */
386
387 typedef struct tunnel {
388 #if TUN_TYPE == TUN_UNET 
389   sel_file f;                           /* Selector for Usernet device */
390   struct peer *p;                       /* Pointer to my peer */
391 #elif TUN_TYPE == TUN_LINUX
392   sel_file f;                           /* Selector for TUN/TAP device */
393   struct peer *p;                       /* Pointer to my peer */
394   char ifn[IFNAMSIZ];                   /* Interface name buffer */
395 #elif TUN_TYPE == TUN_BSD
396   sel_file f;                           /* Selector for tunnel device */
397   struct peer *p;                       /* Pointer to my peer */
398   unsigned n;                           /* Number of my tunnel device */
399 #else
400 #  error "No support for this tunnel type"
401 #endif
402 } tunnel;
403
404 /* --- Peer statistics --- *
405  *
406  * Contains various interesting and not-so-interesting statistics about a
407  * peer.  This is updated by various parts of the code.  The format of the
408  * structure isn't considered private, and @p_stats@ returns a pointer to the
409  * statistics block for a given peer.
410  */
411
412 typedef struct stats {
413   unsigned long sz_in, sz_out;          /* Size of all data in and out */
414   unsigned long sz_kxin, sz_kxout;      /* Size of key exchange messages */
415   unsigned long sz_ipin, sz_ipout;      /* Size of encapsulated IP packets */
416   time_t t_start, t_last;               /* Time peer created, last recv */
417   unsigned long n_reject;               /* Number of rejected packets */
418   unsigned long n_in, n_out;            /* Number of packets in and out */
419   unsigned long n_kxin, n_kxout;        /* Number of key exchange packets */
420   unsigned long n_ipin, n_ipout;        /* Number of encrypted packets */
421 } stats;
422
423 /* --- Peer structure --- *
424  *
425  * The main structure which glues everything else together.
426  */
427
428 typedef struct peer {
429   struct peer *next, *prev;             /* Links to next and previous */
430   char *name;                           /* Name of this peer */
431   tunnel t;                             /* Tunnel for local packets */
432   keyset *ks;                           /* List head for keysets */
433   buf b;                                /* Buffer for sending packets */
434   addr peer;                            /* Peer socket address */
435   size_t sasz;                          /* Socket address size */
436   stats st;                             /* Statistics */
437   keyexch kx;                           /* Key exchange protocol block */
438 } peer;
439
440 /* --- Admin structure --- */
441
442 #define OBUFSZ 16384u
443
444 typedef struct obuf {
445   struct obuf *next;                    /* Next buffer in list */
446   char *p_in, *p_out;                   /* Pointers into the buffer */
447   char buf[OBUFSZ];                     /* The actual buffer */
448 } obuf;
449
450 typedef struct admin {
451   struct admin *next, *prev;            /* Links to next and previous */
452   unsigned f;                           /* Various useful flags */
453 #ifndef NTRACE
454   unsigned seq;                         /* Sequence number for tracing */
455 #endif
456   char *pname;                          /* Peer name to create */
457   char *paddr;                          /* Address string to resolve */
458   obuf *o_head, *o_tail;                /* Output buffer list */
459   selbuf b;                             /* Line buffer for commands */
460   sel_file w;                           /* Selector for write buffering */
461   bres_client r;                        /* Background resolver task */
462   sel_timer t;                          /* Timer for resolver */
463   addr peer;                            /* Address to set */
464   size_t sasz;                          /* Size of the address */
465 } admin;
466
467 #define AF_DEAD 1u                      /* Destroy this admin block */
468 #define AF_LOCK 2u                      /* Don't destroy it yet */
469
470 /*----- Global variables --------------------------------------------------*/
471
472 extern sel_state sel;                   /* Global I/O event state */
473 extern dh_priv kpriv;                   /* Our private key */
474 extern mpmont mg;                       /* Montgomery context for DH group */
475 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
476
477 #ifndef NTRACE
478 extern const trace_opt tr_opts[];       /* Trace options array */
479 extern unsigned tr_flags;               /* Trace options flags */
480 #endif
481
482 /*----- Other macros ------------------------------------------------------*/
483
484 #define TIMER noise_timer(RAND_GLOBAL)
485
486 /*----- Key management ----------------------------------------------------*/
487
488 /* --- @km_interval@ --- *
489  *
490  * Arguments:   ---
491  *
492  * Returns:     Zero if OK, nonzero to force reloading of keys.
493  *
494  * Use:         Called on the interval timer to perform various useful jobs.
495  */
496
497 extern int km_interval(void);
498
499 /* --- @km_init@ --- *
500  *
501  * Arguments:   @const char *kr_priv@ = private keyring file
502  *              @const char *kr_pub@ = public keyring file
503  *              @const char *tag@ = tag to load
504  *
505  * Returns:     ---
506  *
507  * Use:         Initializes, and loads the private key.
508  */
509
510 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
511                     const char */*tag*/);
512
513 /* --- @km_getpubkey@ --- *
514  *
515  * Arguments:   @const char *tag@ = public key tag to load
516  *              @dh_pub *kpub@ = where to put the public key
517  *              @time_t *t_exp@ = where to put the expiry time
518  *
519  * Returns:     Zero if OK, nonzero if it failed.
520  *
521  * Use:         Fetches a public key from the keyring.
522  */
523
524 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/,
525                         time_t */*t_exp*/);
526
527 /*----- Key exchange ------------------------------------------------------*/
528
529 /* --- @kx_start@ --- *
530  *
531  * Arguments:   @keyexch *kx@ = pointer to key exchange context
532  *
533  * Returns:     ---
534  *
535  * Use:         Stimulates a key exchange.  If a key exchage is in progress,
536  *              a new challenge is sent (unless the quiet timer forbids
537  *              this); if no exchange is in progress, one is commenced.
538  */
539
540 extern void kx_start(keyexch */*kx*/);
541
542 /* --- @kx_message@ --- *
543  *
544  * Arguments:   @keyexch *kx@ = pointer to key exchange context
545  *              @unsigned msg@ = the message code
546  *              @buf *b@ = pointer to buffer containing the packet
547  *
548  * Returns:     ---
549  *
550  * Use:         Reads a packet containing key exchange messages and handles
551  *              it.
552  */
553
554 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
555
556 /* --- @kx_free@ --- *
557  *
558  * Arguments:   @keyexch *kx@ = pointer to key exchange context
559  *
560  * Returns:     ---
561  *
562  * Use:         Frees everything in a key exchange context.
563  */
564
565 extern void kx_free(keyexch */*kx*/);
566
567 /* --- @kx_newkeys@ --- *
568  *
569  * Arguments:   @keyexch *kx@ = pointer to key exchange context
570  *
571  * Returns:     ---
572  *
573  * Use:         Informs the key exchange module that its keys may have
574  *              changed.  If fetching the new keys fails, the peer will be
575  *              destroyed, we log messages and struggle along with the old
576  *              keys.
577  */
578
579 extern void kx_newkeys(keyexch */*kx*/);
580
581 /* --- @kx_init@ --- *
582  *
583  * Arguments:   @keyexch *kx@ = pointer to key exchange context
584  *              @peer *p@ = pointer to peer context
585  *              @keyset **ks@ = pointer to keyset list
586  *
587  * Returns:     Zero if OK, nonzero if it failed.
588  *
589  * Use:         Initializes a key exchange module.  The module currently
590  *              contains no keys, and will attempt to initiate a key
591  *              exchange.
592  */
593
594 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
595
596 /*----- Keysets and symmetric cryptography --------------------------------*/
597
598 /* --- @ks_drop@ --- *
599  *
600  * Arguments:   @keyset *ks@ = pointer to a keyset
601  *
602  * Returns:     ---
603  *
604  * Use:         Decrements a keyset's reference counter.  If the counter hits
605  *              zero, the keyset is freed.
606  */
607
608 extern void ks_drop(keyset */*ks*/);
609
610 /* --- @ks_gen@ --- *
611  *
612  * Arguments:   @const void *k@ = pointer to key material
613  *              @size_t x, y, z@ = offsets into key material (see below)
614  *
615  * Returns:     A pointer to the new keyset.
616  *
617  * Use:         Derives a new keyset from the given key material.  The
618  *              offsets @x@, @y@ and @z@ separate the key material into three
619  *              parts.  Between the @k@ and @k + x@ is `my' contribution to
620  *              the key material; between @k + x@ and @k + y@ is `your'
621  *              contribution; and between @k + y@ and @k + z@ is a shared
622  *              value we made together.  These are used to construct two
623  *              pairs of symmetric keys.  Each pair consists of an encryption
624  *              key and a message authentication key.  One pair is used for
625  *              outgoing messages, the other for incoming messages.
626  *
627  *              The new key is marked so that it won't be selected for output
628  *              by @ksl_encrypt@.  You can still encrypt data with it by
629  *              calling @ks_encrypt@ directly.
630  */
631
632 extern keyset *ks_gen(const void */*k*/,
633                       size_t /*x*/, size_t /*y*/, size_t /*z*/);
634
635 /* --- @ks_tregen@ --- *
636  *
637  * Arguments:   @keyset *ks@ = pointer to a keyset
638  *
639  * Returns:     The time at which moves ought to be made to replace this key.
640  */
641
642 extern time_t ks_tregen(keyset */*ks*/);
643
644 /* --- @ks_activate@ --- *
645  *
646  * Arguments:   @keyset *ks@ = pointer to a keyset
647  *
648  * Returns:     ---
649  *
650  * Use:         Activates a keyset, so that it can be used for encrypting
651  *              outgoing messages.
652  */
653
654 extern void ks_activate(keyset */*ks*/);
655
656 /* --- @ks_encrypt@ --- *
657  *
658  * Arguments:   @keyset *ks@ = pointer to a keyset
659  *              @buf *b@ = pointer to input buffer
660  *              @buf *bb@ = pointer to output buffer
661  *
662  * Returns:     Zero if OK, nonzero if the key needs replacing.  If the
663  *              encryption failed, the output buffer is broken and zero is
664  *              returned.
665  *
666  * Use:         Encrypts a block of data using the key.  Note that the `key
667  *              ought to be replaced' notification is only ever given once
668  *              for each key.  Also note that this call forces a keyset to be
669  *              used even if it's marked as not for data output.
670  */
671
672 extern int ks_encrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
673
674 /* --- @ks_decrypt@ --- *
675  *
676  * Arguments:   @keyset *ks@ = pointer to a keyset
677  *              @buf *b@ = pointer to an input buffer
678  *              @buf *bb@ = pointer to an output buffer
679  *
680  * Returns:     Zero on success, or nonzero if there was some problem.
681  *
682  * Use:         Attempts to decrypt a message using a given key.  Note that
683  *              requesting decryption with a key directly won't clear a
684  *              marking that it's not for encryption.
685  */
686
687 extern int ks_decrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
688
689 /* --- @ksl_free@ --- *
690  *
691  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
692  *
693  * Returns:     ---
694  *
695  * Use:         Frees (releases references to) all of the keys in a keyset.
696  */
697
698 extern void ksl_free(keyset **/*ksroot*/);
699
700 /* --- @ksl_link@ --- *
701  *
702  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
703  *              @keyset *ks@ = pointer to a keyset
704  *
705  * Returns:     ---
706  *
707  * Use:         Links a keyset into a list.  A keyset can only be on one list
708  *              at a time.  Bad things happen otherwise.
709  */
710
711 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
712
713 /* --- @ksl_prune@ --- *
714  *
715  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
716  *
717  * Returns:     ---
718  *
719  * Use:         Prunes the keyset list by removing keys which mustn't be used
720  *              any more.
721  */
722
723 extern void ksl_prune(keyset **/*ksroot*/);
724
725 /* --- @ksl_encrypt@ --- *
726  *
727  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
728  *              @buf *b@ = pointer to input buffer
729  *              @buf *bb@ = pointer to output buffer
730  *
731  * Returns:     Nonzero if a new key is needed.
732  *
733  * Use:         Encrypts a packet.
734  */
735
736 extern int ksl_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
737
738 /* --- @ksl_decrypt@ --- *
739  *
740  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
741  *              @buf *b@ = pointer to input buffer
742  *              @buf *bb@ = pointer to output buffer
743  *
744  * Returns:     Nonzero if the packet couldn't be decrypted.
745  *
746  * Use:         Decrypts a packet.
747  */
748
749 extern int ksl_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
750
751 /*----- Administration interface ------------------------------------------*/
752
753 /* --- @a_warn@ --- *
754  *
755  * Arguments:   @const char *fmt@ = pointer to format string
756  *              @...@ = other arguments
757  *
758  * Returns:     ---
759  *
760  * Use:         Informs all admin connections of a warning.
761  */
762
763 extern void a_warn(const char */*fmt*/, ...);
764
765 /* --- @a_create@ --- *
766  *
767  * Arguments:   @int fd_in, fd_out@ = file descriptors to use
768  *
769  * Returns:     ---
770  *
771  * Use:         Creates a new admin connection.
772  */
773
774 extern void a_create(int /*fd_in*/, int /*fd_out*/);
775
776 /* --- @a_quit@ --- *
777  *
778  * Arguments:   ---
779  *
780  * Returns:     ---
781  *
782  * Use:         Shuts things down nicely.
783  */
784
785 extern void a_quit(void);
786
787 /* --- @a_daemon@ --- *
788  *
789  * Arguments:   ---
790  *
791  * Returns:     ---
792  *
793  * Use:         Informs the admin module that it's a daemon.
794  */
795
796 extern void a_daemon(void);
797
798 /* --- @a_init@ --- *
799  *
800  * Arguments:   @const char *sock@ = socket name to create
801  *
802  * Returns:     ---
803  *
804  * Use:         Creates the admin listening socket.
805  */
806
807 extern void a_init(const char */*sock*/);
808
809 /*----- Peer management ---------------------------------------------------*/
810
811 /* --- @p_txstart@ --- *
812  *
813  * Arguments:   @peer *p@ = pointer to peer block
814  *              @unsigned msg@ = message type code
815  *
816  * Returns:     A pointer to a buffer to write to.
817  *
818  * Use:         Starts sending to a peer.  Only one send can happen at a
819  *              time.
820  */
821
822 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
823
824 /* --- @p_txend@ --- *
825  *
826  * Arguments:   @peer *p@ = pointer to peer block
827  *
828  * Returns:     ---
829  *
830  * Use:         Sends a packet to the peer.
831  */
832
833 extern void p_txend(peer */*p*/);
834
835 /* --- @p_tun@ --- *
836  *
837  * Arguments:   @peer *p@ = pointer to peer block
838  *              @buf *b@ = buffer containing incoming packet
839  *
840  * Returns:     ---
841  *
842  * Use:         Handles a packet which needs to be sent to a peer.
843  */
844
845 extern void p_tun(peer */*p*/, buf */*b*/);
846
847 /* --- @p_interval@ --- *
848  *
849  * Arguments:   ---
850  *
851  * Returns:     ---
852  *
853  * Use:         Called periodically to do tidying.
854  */
855
856 extern void p_interval(void);
857
858 /* --- @p_stats@ --- *
859  *
860  * Arguments:   @peer *p@ = pointer to a peer block
861  *
862  * Returns:     A pointer to the peer's statistics.
863  */
864
865 extern stats *p_stats(peer */*p*/);
866
867 /* --- @p_ifname@ --- *
868  *
869  * Arguments:   @peer *p@ = pointer to a peer block
870  *
871  * Returns:     A pointer to the peer's interface name.
872  */
873
874 extern const char *p_ifname(peer */*p*/);
875
876 /* --- @p_addr@ --- *
877  *
878  * Arguments:   @peer *p@ = pointer to a peer block
879  *
880  * Returns:     A pointer to the peer's address.
881  */
882
883 extern const addr *p_addr(peer */*p*/);
884
885 /* --- @p_init@ --- *
886  *
887  * Arguments:   @unsigned port@ = port number to listen to
888  *
889  * Returns:     ---
890  *
891  * Use:         Initializes the peer system; creates the socket.
892  */
893
894 extern void p_init(unsigned /*port*/);
895
896 /* --- @p_port@ --- *
897  *
898  * Arguments:   ---
899  *
900  * Returns:     Port number used for socket.
901  */
902
903 unsigned p_port(void);
904
905 /* --- @p_create@ --- *
906  *
907  * Arguments:   @const char *name@ = name for this peer
908  *              @struct sockaddr *sa@ = socket address of peer
909  *              @size_t sz@ = size of socket address
910  *
911  * Returns:     Pointer to the peer block, or null if it failed.
912  *
913  * Use:         Creates a new named peer block.  No peer is actually attached
914  *              by this point.
915  */
916
917 extern peer *p_create(const char */*name*/,
918                       struct sockaddr */*sa*/, size_t /*sz*/);
919
920 /* --- @p_name@ --- *
921  *
922  * Arguments:   @peer *p@ = pointer to a peer block
923  *
924  * Returns:     A pointer to the peer's name.
925  */
926
927 extern const char *p_name(peer */*p*/);
928
929 /* --- @p_find@ --- *
930  *
931  * Arguments:   @const char *name@ = name to look up
932  *
933  * Returns:     Pointer to the peer block, or null if not found.
934  *
935  * Use:         Finds a peer by name.
936  */
937
938 extern peer *p_find(const char */*name*/);
939
940 /* --- @p_destroy@ --- *
941  *
942  * Arguments:   @peer *p@ = pointer to a peer
943  *
944  * Returns:     ---
945  *
946  * Use:         Destroys a peer.
947  */
948
949 extern void p_destroy(peer */*p*/);
950
951 /* --- @p_first@, @p_next@ --- *
952  *
953  * Arguments:   @peer *p@ = a peer block
954  *
955  * Returns:     @peer_first@ returns the first peer in some ordering;
956  *              @peer_next@ returns the peer following a given one in the
957  *              same ordering.  Null is returned for the end of the list.
958  */
959
960 extern peer *p_first(void);
961 extern peer *p_next(peer */*p*/);
962
963 /*----- Tunnel interface --------------------------------------------------*/
964
965 /* --- @tun_init@ --- *
966  *
967  * Arguments:   ---
968  *
969  * Returns:     ---
970  *
971  * Use:         Initializes the tunneling system.  Maybe this will require
972  *              opening file descriptors or something.
973  */
974
975 extern void tun_init(void);
976
977 /* --- @tun_create@ --- *
978  *
979  * Arguments:   @tunnel *t@ = pointer to tunnel block
980  *              @peer *p@ = pointer to peer block
981  *
982  * Returns:     Zero if it worked, nonzero on failure.
983  *
984  * Use:         Initializes a new tunnel.
985  */
986
987 extern int tun_create(tunnel */*t*/, peer */*p*/);
988
989 /* --- @tun_ifname@ --- *
990  *
991  * Arguments:   @tunnel *t@ = pointer to tunnel block
992  *
993  * Returns:     A pointer to the tunnel's interface name.
994  */
995
996 extern const char *tun_ifname(tunnel */*t*/);
997
998 /* --- @tun_inject@ --- *
999  *
1000  * Arguments:   @tunnel *t@ = pointer to tunnel block
1001  *              @buf *b@ = buffer to send
1002  *
1003  * Returns:     ---
1004  *
1005  * Use:         Injects a packet into the local network stack.
1006  */
1007
1008 extern void tun_inject(tunnel */*t*/, buf */*b*/);
1009
1010 /* --- @tun_destroy@ --- *
1011  *
1012  * Arguments:   @tunnel *t@ = pointer to tunnel block
1013  *
1014  * Returns:     ---
1015  *
1016  * Use:         Destroys a tunnel.
1017  */
1018
1019 extern void tun_destroy(tunnel */*t*/);
1020
1021 /*----- Other handy utilities ---------------------------------------------*/
1022
1023 /* --- @mpstr@ --- *
1024  *
1025  * Arguments:   @mp *m@ = a multiprecision integer
1026  *
1027  * Returns:     A pointer to the integer's textual representation.
1028  *
1029  * Use:         Converts a multiprecision integer to a string.  Corrupts
1030  *              @buf_t@.
1031  */
1032
1033 extern const char *mpstr(mp */*m*/);
1034
1035 /* --- @timestr@ --- *
1036  *
1037  * Arguments:   @time_t t@ = a time to convert
1038  *
1039  * Returns:     A pointer to a textual representation of the time.
1040  *
1041  * Use:         Converts a time to a textual representation.  Corrupts
1042  *              @buf_t@.
1043  */
1044
1045 extern const char *timestr(time_t /*t*/);
1046
1047 /*----- That's all, folks -------------------------------------------------*/
1048
1049 #ifdef __cplusplus
1050   }
1051 #endif
1052
1053 #endif