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