3 * $Id: tripe.h,v 1.3 2001/02/04 01:17:55 mdw Exp $
5 * Main header file for TrIPE
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 2001/02/04 01:17:55 mdw
33 * Create a configuration header file to tidy up command lines.
35 * Revision 1.2 2001/02/03 22:40:29 mdw
36 * Put timer information into the entropy pool when packets are received
37 * and on similar events. Reseed the generator on the interval timer.
39 * Revision 1.1 2001/02/03 20:26:37 mdw
51 /*----- Header files ------------------------------------------------------*/
66 #include <sys/types.h>
72 #include <sys/socket.h>
74 #include <netinet/in.h>
75 #include <arpa/inet.h>
81 #include <mLib/alloc.h>
82 #include <mLib/arena.h>
83 #include <mLib/bres.h>
84 #include <mLib/dstr.h>
86 #include <mLib/fdflags.h>
87 #include <mLib/fwatch.h>
88 #include <mLib/mdwopt.h>
89 #include <mLib/quis.h>
90 #include <mLib/report.h>
92 #include <mLib/selbuf.h>
96 #include <mLib/trace.h>
98 #include <catacomb/gcipher.h>
99 #include <catacomb/gmac.h>
100 #include <catacomb/grand.h>
101 #include <catacomb/key.h>
102 #include <catacomb/paranoia.h>
104 #include <catacomb/blowfish.h>
105 #include <catacomb/blowfish-cbc.h>
106 #include <catacomb/noise.h>
107 #include <catacomb/rand.h>
108 #include <catacomb/rmd160.h>
109 #include <catacomb/rmd160-hmac.h>
111 #include <catacomb/mp.h>
112 #include <catacomb/mpmont.h>
113 #include <catacomb/mprand.h>
114 #include <catacomb/dh.h>
120 /*----- Magic numbers -----------------------------------------------------*/
122 /* --- Tunnel types --- */
128 /* --- Trace flags --- */
136 #define T_KEYEXCH 64u
137 #define T_KEYMGMT 128u
143 #define SEC(n) (n##u)
144 #define MIN(n) (n##u * 60u)
145 #define MEG(n) (n##ul * 1024ul * 1024ul)
147 /* --- Other things --- */
149 #define PKBUFSZ 65536
151 /*----- TrIPE protocol ----------------------------------------------------*/
153 /* --- TrIPE packet format --- *
155 * A packet begins with a single-byte packet type. The remaining data
156 * depends on the packet type.
159 #define MSG_PACKET 0u
160 /* Followed by a 64-bit MAC and an encrypted packet. The MAC is used as an
161 * IV for a 64-bit block cipher in CBC-stealing mode.
164 #define MSG_PRECHALLENGE 1u
165 /* Followed by the challenge only. Useful for bootstrapping the system.
168 #define MSG_CHALLENGE 2u
169 /* Followed by a response hash and a large-integer challenge.
172 #define MSG_RESPONSE 3u
173 /* Followed by a large-integer response.
176 /*----- Data structures ---------------------------------------------------*/
180 * Buffers provide a simple stream-like interface for building and parsing
185 octet *base, *p, *limit; /* Pointers to the buffer */
186 unsigned f; /* Various flags */
189 #define BF_BROKEN 1u /* Buffer is broken */
191 /* --- Socket addresses --- *
193 * A magic union of supported socket addresses.
198 struct sockaddr_in sin;
201 /* --- A symmetric keyset --- *
203 * A keyset contains a set of symmetric keys for encrypting and decrypting
204 * packets. Keysets are stored in a list, sorted in reverse order of
205 * creation, so that the most recent keyset (the one most likely to be used)
208 * Each keyset has a time limit and a data limit. The keyset is destroyed
209 * when either it has existed for too long, or it has been used to encrypt
210 * too much data. New key exchanges are triggered when keys are close to
214 typedef struct keyset {
215 struct keyset *next; /* Next active keyset in the list */
216 time_t t_exp; /* Expiry time for this keyset */
217 unsigned long sz_exp; /* Data limit for the keyset */
219 unsigned seq; /* Sequence number for tracing */
221 gcipher *c; /* Keyset cipher for encryption */
222 gmac *m; /* Keyset MAC for integrity */
225 /* --- Key exchange --- *
227 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
228 * Protocol has a number of desirable features (e.g., perfect forward
229 * secrecy, and zero-knowledge authentication) which make it attractive for
230 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
234 typedef struct keyexch {
235 keyset **ks; /* Peer's list of keysets */
236 struct peer *p; /* Pointer back to the peer */
237 unsigned f; /* Various useful flags */
238 sel_timer t; /* Timer for next exchange */
239 dh_pub kpub; /* Peer's public key */
240 mp *my_x, *my_gx, *my_gxy; /* My half of the exchange */
241 octet my_h[RMD160_HASHSZ]; /* My challenge hash */
242 mp *your_gx, *your_gxy; /* Your half of the exchange */
243 octet your_h[RMD160_HASHSZ]; /* Your challenge hash */
244 time_t t_valid; /* When this exchange goes bad */
245 time_t t_qchal, t_qresp; /* Quiet timers for packet types */
246 time_t t_newchal; /* When to accept a new challenge */
249 #define KXF_TIMER 1u /* Waiting for a timer to go off */
250 #define KXF_INIT 2u /* Big numbers are initialized */
251 #define KXF_MYH 4u /* My hash has been computed */
252 #define KXF_YOURH 8u /* Your hash has been received */
253 #define KXF_REPLY 16u /* Received your response OK */
254 #define KXF_DONE 32u /* Key exchange completed */
256 /* --- Tunnel structure --- *
258 * Used to maintain system-specific information about the tunnel interface.
261 typedef struct tunnel {
262 #if TUN_TYPE == TUN_UNET
263 sel_file f; /* Selector for Usernet device */
264 struct peer *p; /* Pointer to my peer */
266 # error "No support for this tunnel type"
270 /* --- Peer structure --- *
272 * The main structure which glues everything else together.
275 typedef struct peer {
276 struct peer *next, *prev; /* Links to next and previous */
277 char *name; /* Name of this peer */
278 tunnel t; /* Tunnel for local packets */
279 keyset *ks; /* List head for keysets */
280 keyexch kx; /* Key exchange protocol block */
281 buf b; /* Buffer for sending packets */
282 addr peer; /* Peer socket address */
283 size_t sasz; /* Socket address size */
286 /* --- Admin structure --- */
288 typedef struct admin {
289 struct admin *next, *prev; /* Links to next and previous */
290 selbuf b; /* Line buffer for commands */
291 int fd; /* File descriptor for output */
293 unsigned seq; /* Sequence number for tracing */
295 char *pname; /* Peer name to create */
296 char *paddr; /* Address string to resolve */
297 bres_client r; /* Background resolver task */
298 sel_timer t; /* Timer for resolver */
299 addr peer; /* Address to set */
300 size_t sasz; /* Size of the address */
303 /*----- Global variables --------------------------------------------------*/
305 extern sel_state sel; /* Global I/O event state */
306 extern dh_priv kpriv; /* Our private key */
307 extern mpmont mg; /* Montgomery context for DH group */
308 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ]; /* Big packet buffers */
311 extern const trace_opt tr_opts[]; /* Trace options array */
312 extern unsigned tr_flags; /* Trace options flags */
315 /*----- Other macros ------------------------------------------------------*/
317 #define TIMER noise_timer(RAND_GLOBAL)
319 /*----- Key management ----------------------------------------------------*/
321 /* --- @km_interval@ --- *
325 * Returns: Zero if OK, nonzero to force reloading of keys.
327 * Use: Called on the interval timer to perform various useful jobs.
330 extern int km_interval(void);
332 /* --- @km_init@ --- *
334 * Arguments: @const char *kr_priv@ = private keyring file
335 * @const char *kr_pub@ = public keyring file
336 * @const char *tag@ = tag to load
340 * Use: Initializes, and loads the private key.
343 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
344 const char */*tag*/);
346 /* --- @km_getpubkey@ --- *
348 * Arguments: @const char *tag@ = public key tag to load
349 * @dh_pub *kpub@ = where to put the public key
351 * Returns: Zero if OK, nonzero if it failed.
353 * Use: Fetches a public key from the keyring.
356 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/);
358 /*----- Key exchange ------------------------------------------------------*/
360 /* --- @kx_start@ --- *
362 * Arguments: @keyexch *kx@ = pointer to key exchange context
366 * Use: Stimulates a key exchange. If a key exchage is in progress,
367 * a new challenge is sent (unless the quiet timer forbids
368 * this); if no exchange is in progress, one is commenced.
371 extern void kx_start(keyexch */*kx*/);
373 /* --- @kx_prechallenge@ --- *
375 * Arguments: @keyexch *kx@ = pointer to key exhange context
376 * @buf *b@ = pointer to buffer containing the packet
380 * Use: Reads a prechallenge packet from the buffer and handles it.
383 extern void kx_prechallenge(keyexch */*kx*/, buf */*b*/);
385 /* --- @kx_challenge@ --- *
387 * Arguments: @keyexch *kx@ = pointer to key exchange context
388 * @buf *b@ = a buffer containing the packet to read
392 * Use: Reads a challenge from the buffer and handles it.
395 extern void kx_challenge(keyexch */*kx*/, buf */*b*/);
397 /* --- @kx_response@ --- *
399 * Arguments: @keyexch *kx@ = pointer to key exchange context
400 * @buf *b@ = a buffer containing the packet to read
404 * Use: Reads a response from the buffer and handles it.
407 extern void kx_response(keyexch */*kx*/, buf */*b*/);
409 /* --- @kx_free@ --- *
411 * Arguments: @keyexch *kx@ = pointer to key exchange context
415 * Use: Frees everything in a key exchange context.
418 extern void kx_free(keyexch */*kx*/);
420 /* --- @kx_newkeys@ --- *
422 * Arguments: @keyexch *kx@ = pointer to key exchange context
426 * Use: Informs the key exchange module that its keys may have
427 * changed. If fetching the new keys fails, the peer will be
428 * destroyed, we log messages and struggle along with the old
432 extern void kx_newkeys(keyexch */*kx*/);
434 /* --- @kx_init@ --- *
436 * Arguments: @keyexch *kx@ = pointer to key exchange context
437 * @peer *p@ = pointer to peer context
438 * @keyset **ks@ = pointer to keyset list
440 * Returns: Zero if OK, nonzero if it failed.
442 * Use: Initializes a key exchange module. The module currently
443 * contains no keys, and will attempt to initiate a key
447 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
449 /*----- Keysets and symmetric cryptography --------------------------------*/
451 /* --- @ks_free@ --- *
453 * Arguments: @keyset **ksroot@ = pointer to keyset list head
457 * Use: Frees all of the keys in a keyset.
460 extern void ks_free(keyset **/*ksroot*/);
462 /* --- @ks_prune@ --- *
464 * Arguments: @keyset **ksroot@ = pointer to keyset list head
468 * Use: Prunes the keyset list by removing keys which mustn't be used
472 extern void ks_prune(keyset **/*ksroot*/);
474 /* --- @ks_gen@ --- *
476 * Arguments: @keyset **ksroot@ = pointer to keyset list head
477 * @const void *k@ = pointer to key material
478 * @size_t sz@ = size of the key material
480 * Returns: The regeneration time for the new key.
482 * Use: Derives a keyset from the given key material and adds it to
486 extern time_t ks_gen(keyset **/*ksroot*/, const void */*k*/, size_t /*sz*/);
488 /* --- @ks_encrypt@ --- *
490 * Arguments: @keyset **ksroot@ = pointer to keyset list head
491 * @buf *b@ = pointer to input buffer
492 * @buf *bb@ = pointer to output buffer
494 * Returns: Nonzero if a new key is needed.
496 * Use: Encrypts a packet.
499 extern int ks_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
501 /* --- @ks_decrypt@ --- *
503 * Arguments: @keyset **ksroot@ = pointer to keyset list head
504 * @buf *b@ = pointer to input buffer
505 * @buf *bb@ = pointer to output buffer
507 * Returns: Nonzero if the packet couldn't be decrypted.
509 * Use: Decrypts a packet.
512 extern int ks_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
514 /*----- Administration interface ------------------------------------------*/
516 /* --- @a_warn@ --- *
518 * Arguments: @const char *fmt@ = pointer to format string
519 * @...@ = other arguments
523 * Use: Informs all admin connections of a warning.
526 extern void a_warn(const char */*fmt*/, ...);
528 /* --- @a_create@ --- *
530 * Arguments: @int fd_in, fd_out@ = file descriptors to use
534 * Use: Creates a new admin connection.
537 extern void a_create(int /*fd_in*/, int /*fd_out*/);
539 /* --- @a_quit@ --- *
545 * Use: Shuts things down nicely.
548 extern void a_quit(void);
550 /* --- @a_daemon@ --- *
556 * Use: Informs the admin module that it's a daemon.
559 extern void a_daemon(void);
561 /* --- @a_init@ --- *
563 * Arguments: @const char *sock@ = socket name to create
567 * Use: Creates the admin listening socket.
570 extern void a_init(const char */*sock*/);
572 /*----- Peer management ---------------------------------------------------*/
574 /* --- @p_txstart@ --- *
576 * Arguments: @peer *p@ = pointer to peer block
577 * @unsigned msg@ = message type code
579 * Returns: A pointer to a buffer to write to.
581 * Use: Starts sending to a peer. Only one send can happen at a
585 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
587 /* --- @p_txend@ --- *
589 * Arguments: @peer *p@ = pointer to peer block
593 * Use: Sends a packet to the peer.
596 extern void p_txend(peer */*p*/);
600 * Arguments: @peer *p@ = pointer to peer block
601 * @buf *b@ = buffer containing incoming packet
605 * Use: Handles a packet which needs to be sent to a peer.
608 extern void p_tun(peer */*p*/, buf */*b*/);
610 /* --- @p_interval@ --- *
616 * Use: Called periodically to do tidying.
619 extern void p_interval(void);
621 /* --- @p_ifname@ --- *
623 * Arguments: @peer *p@ = pointer to a peer block
625 * Returns: A pointer to the peer's interface name.
628 extern const char *p_ifname(peer */*p*/);
630 /* --- @p_addr@ --- *
632 * Arguments: @peer *p@ = pointer to a peer block
634 * Returns: A pointer to the peer's address.
637 extern const addr *p_addr(peer */*p*/);
639 /* --- @p_init@ --- *
641 * Arguments: @unsigned port@ = port number to listen to
645 * Use: Initializes the peer system; creates the socket.
648 extern void p_init(unsigned /*port*/);
650 /* --- @p_port@ --- *
654 * Returns: Port number used for socket.
657 unsigned p_port(void);
659 /* --- @p_create@ --- *
661 * Arguments: @const char *name@ = name for this peer
662 * @struct sockaddr *sa@ = socket address of peer
663 * @size_t sz@ = size of socket address
665 * Returns: Pointer to the peer block, or null if it failed.
667 * Use: Creates a new named peer block. No peer is actually attached
671 extern peer *p_create(const char */*name*/,
672 struct sockaddr */*sa*/, size_t /*sz*/);
674 /* --- @p_name@ --- *
676 * Arguments: @peer *p@ = pointer to a peer block
678 * Returns: A pointer to the peer's name.
681 extern const char *p_name(peer */*p*/);
683 /* --- @p_find@ --- *
685 * Arguments: @const char *name@ = name to look up
687 * Returns: Pointer to the peer block, or null if not found.
689 * Use: Finds a peer by name.
692 extern peer *p_find(const char */*name*/);
694 /* --- @p_destroy@ --- *
696 * Arguments: @peer *p@ = pointer to a peer
700 * Use: Destroys a peer.
703 extern void p_destroy(peer */*p*/);
705 /* --- @p_first@, @p_next@ --- *
707 * Arguments: @peer *p@ = a peer block
709 * Returns: @peer_first@ returns the first peer in some ordering;
710 * @peer_next@ returns the peer following a given one in the
711 * same ordering. Null is returned for the end of the list.
714 extern peer *p_first(void);
715 extern peer *p_next(peer */*p*/);
717 /*----- Tunnel interface --------------------------------------------------*/
719 /* --- @tun_init@ --- *
725 * Use: Initializes the tunneling system. Maybe this will require
726 * opening file descriptors or something.
729 extern void tun_init(void);
731 /* --- @tun_create@ --- *
733 * Arguments: @tunnel *t@ = pointer to tunnel block
734 * @peer *p@ = pointer to peer block
736 * Returns: Zero if it worked, nonzero on failure.
738 * Use: Initializes a new tunnel.
741 extern int tun_create(tunnel */*t*/, peer */*p*/);
743 /* --- @tun_ifname@ --- *
745 * Arguments: @tunnel *t@ = pointer to tunnel block
747 * Returns: A pointer to the tunnel's interface name.
750 extern const char *tun_ifname(tunnel */*t*/);
752 /* --- @tun_inject@ --- *
754 * Arguments: @tunnel *t@ = pointer to tunnel block
755 * @buf *b@ = buffer to send
759 * Use: Injects a packet into the local network stack.
762 extern void tun_inject(tunnel */*t*/, buf */*b*/);
764 /* --- @tun_destroy@ --- *
766 * Arguments: @tunnel *t@ = pointer to tunnel block
770 * Use: Destroys a tunnel.
773 extern void tun_destroy(tunnel */*t*/);
775 /*----- Buffer handling ---------------------------------------------------*/
777 /* --- Useful macros --- */
779 #define BBASE(b) ((b)->base)
780 #define BLIM(b) ((b)->limit)
781 #define BCUR(b) ((b)->p)
782 #define BSZ(b) ((b)->limit - (b)->base)
783 #define BLEN(b) ((b)->p - (b)->base)
784 #define BLEFT(b) ((b)->limit - (b)->p)
785 #define BSTEP(b, sz) ((b)->p += (sz))
786 #define BBAD(b) ((b)->f & BF_BROKEN)
787 #define BOK(b) (!BBAD(b))
789 #define BENSURE(b, sz) \
790 (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
792 /* --- @buf_init@ --- *
794 * Arguments: @buf *b@ = pointer to a buffer block
795 * @void *p@ = pointer to a buffer
796 * @size_t sz@ = size of the buffer
800 * Use: Initializes the buffer block appropriately.
803 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
805 /* --- @buf_break@ --- *
807 * Arguments: @buf *b@ = pointer to a buffer block
809 * Returns: Some negative value.
811 * Use: Marks a buffer as broken.
814 extern int buf_break(buf */*b*/);
816 /* --- @buf_ensure@ --- *
818 * Arguments: @buf *b@ = pointer to a buffer block
819 * @size_t sz@ = size of data wanted
821 * Returns: Zero if it worked, nonzero if there wasn't enough space.
823 * Use: Ensures that there are @sz@ bytes still in the buffer.
826 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
828 /* --- @buf_get@ --- *
830 * Arguments: @buf *b@ = pointer to a buffer block
831 * @void *p@ = pointer to a buffer
832 * @size_t sz@ = size of the buffer
834 * Returns: Zero if it worked, nonzero if there wasn't enough data.
836 * Use: Fetches data from the buffer into some other place.
839 extern int buf_get(buf */*b*/, void */*p*/, size_t /*sz*/);
841 /* --- @buf_put@ --- *
843 * Arguments: @buf *b@ = pointer to a buffer block
844 * @const void *p@ = pointer to a buffer
845 * @size_t sz@ = size of the buffer
847 * Returns: Zero if it worked, nonzero if there wasn't enough space.
849 * Use: Fetches data from some place and puts it in the buffer
852 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
854 /* --- @buf_getbyte@ --- *
856 * Arguments: @buf *b@ = pointer to a buffer block
858 * Returns: A byte, or less than zero if there wasn't a byte there.
860 * Use: Gets a single byte from a buffer.
863 extern int buf_getbyte(buf */*b*/);
865 /* --- @buf_putbyte@ --- *
867 * Arguments: @buf *b@ = pointer to a buffer block
868 * @int ch@ = byte to write
870 * Returns: Zero if OK, nonzero if there wasn't enough space.
872 * Use: Puts a single byte in a buffer.
875 extern int buf_putbyte(buf */*b*/, int /*ch*/);
877 /* --- @buf_getword@ --- *
879 * Arguments: @buf *b@ = pointer to a buffer block
880 * @uint32 *w@ = where to put the word
882 * Returns: Zero if OK, or nonzero if there wasn't a word there.
884 * Use: Gets a 32-bit word from a buffer.
887 extern int buf_getword(buf */*b*/, uint32 */*w*/);
889 /* --- @buf_putword@ --- *
891 * Arguments: @buf *b@ = pointer to a buffer block
892 * @uint32 w@ = word to write
894 * Returns: Zero if OK, nonzero if there wasn't enough space.
896 * Use: Puts a 32-but word in a buffer.
899 extern int buf_putword(buf */*b*/, uint32 /*w*/);
901 /* --- @buf_getmp@ --- *
903 * Arguments: @buf *b@ = pointer to a buffer block
905 * Returns: A multiprecision integer, or null if there wasn't one there.
907 * Use: Gets a multiprecision integer from a buffer.
910 extern mp *buf_getmp(buf */*b*/, mp */*d*/);
912 /* --- @buf_putmp@ --- *
914 * Arguments: @buf *b@ = pointer to a buffer block
915 * @mp *m@ = a multiprecision integer
917 * Returns: Zero if it worked, nonzero if there wasn't enough space.
919 * Use: Puts a multiprecision integer to a buffer.
922 extern int buf_putmp(buf */*b*/, mp */*m*/);
924 /*----- Other handy utilities ---------------------------------------------*/
928 * Arguments: @mp *m@ = a multiprecision integer
930 * Returns: A pointer to the integer's textual representation.
932 * Use: Converts a multiprecision integer to a string. Corrupts
936 extern const char *mpstr(mp */*m*/);
938 /*----- That's all, folks -------------------------------------------------*/