3 * $Id: tripe.h,v 1.2 2001/02/03 22:40:29 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.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.
36 * Revision 1.1 2001/02/03 20:26:37 mdw
48 /*----- Header files ------------------------------------------------------*/
61 #include <sys/types.h>
67 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
76 #include <mLib/alloc.h>
77 #include <mLib/arena.h>
78 #include <mLib/bres.h>
79 #include <mLib/dstr.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>
87 #include <mLib/selbuf.h>
91 #include <mLib/trace.h>
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>
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>
106 #include <catacomb/mp.h>
107 #include <catacomb/mpmont.h>
108 #include <catacomb/mprand.h>
109 #include <catacomb/dh.h>
115 /*----- Magic numbers -----------------------------------------------------*/
117 /* --- Tunnel types --- */
123 /* --- Trace flags --- */
131 #define T_KEYEXCH 64u
132 #define T_KEYMGMT 128u
138 #define SEC(n) (n##u)
139 #define MIN(n) (n##u * 60u)
140 #define MEG(n) (n##ul * 1024ul * 1024ul)
142 /* --- Other things --- */
144 #define PKBUFSZ 65536
146 /*----- TrIPE protocol ----------------------------------------------------*/
148 /* --- TrIPE packet format --- *
150 * A packet begins with a single-byte packet type. The remaining data
151 * depends on the packet type.
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.
159 #define MSG_PRECHALLENGE 1u
160 /* Followed by the challenge only. Useful for bootstrapping the system.
163 #define MSG_CHALLENGE 2u
164 /* Followed by a response hash and a large-integer challenge.
167 #define MSG_RESPONSE 3u
168 /* Followed by a large-integer response.
171 /*----- Data structures ---------------------------------------------------*/
175 * Buffers provide a simple stream-like interface for building and parsing
180 octet *base, *p, *limit; /* Pointers to the buffer */
181 unsigned f; /* Various flags */
184 #define BF_BROKEN 1u /* Buffer is broken */
186 /* --- Socket addresses --- *
188 * A magic union of supported socket addresses.
193 struct sockaddr_in sin;
196 /* --- A symmetric keyset --- *
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)
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
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 */
214 unsigned seq; /* Sequence number for tracing */
216 gcipher *c; /* Keyset cipher for encryption */
217 gmac *m; /* Keyset MAC for integrity */
220 /* --- Key exchange --- *
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
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 */
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 */
251 /* --- Tunnel structure --- *
253 * Used to maintain system-specific information about the tunnel interface.
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 */
261 # error "No support for this tunnel type"
265 /* --- Peer structure --- *
267 * The main structure which glues everything else together.
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 */
281 /* --- Admin structure --- */
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 */
288 unsigned seq; /* Sequence number for tracing */
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 */
298 /*----- Global variables --------------------------------------------------*/
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 */
306 extern const trace_opt tr_opts[]; /* Trace options array */
307 extern unsigned tr_flags; /* Trace options flags */
310 /*----- Other macros ------------------------------------------------------*/
312 #define TIMER noise_timer(RAND_GLOBAL)
314 /*----- Key management ----------------------------------------------------*/
316 /* --- @km_interval@ --- *
320 * Returns: Zero if OK, nonzero to force reloading of keys.
322 * Use: Called on the interval timer to perform various useful jobs.
325 extern int km_interval(void);
327 /* --- @km_init@ --- *
329 * Arguments: @const char *kr_priv@ = private keyring file
330 * @const char *kr_pub@ = public keyring file
331 * @const char *tag@ = tag to load
335 * Use: Initializes, and loads the private key.
338 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
339 const char */*tag*/);
341 /* --- @km_getpubkey@ --- *
343 * Arguments: @const char *tag@ = public key tag to load
344 * @dh_pub *kpub@ = where to put the public key
346 * Returns: Zero if OK, nonzero if it failed.
348 * Use: Fetches a public key from the keyring.
351 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/);
353 /*----- Key exchange ------------------------------------------------------*/
355 /* --- @kx_start@ --- *
357 * Arguments: @keyexch *kx@ = pointer to key exchange context
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.
366 extern void kx_start(keyexch */*kx*/);
368 /* --- @kx_prechallenge@ --- *
370 * Arguments: @keyexch *kx@ = pointer to key exhange context
371 * @buf *b@ = pointer to buffer containing the packet
375 * Use: Reads a prechallenge packet from the buffer and handles it.
378 extern void kx_prechallenge(keyexch */*kx*/, buf */*b*/);
380 /* --- @kx_challenge@ --- *
382 * Arguments: @keyexch *kx@ = pointer to key exchange context
383 * @buf *b@ = a buffer containing the packet to read
387 * Use: Reads a challenge from the buffer and handles it.
390 extern void kx_challenge(keyexch */*kx*/, buf */*b*/);
392 /* --- @kx_response@ --- *
394 * Arguments: @keyexch *kx@ = pointer to key exchange context
395 * @buf *b@ = a buffer containing the packet to read
399 * Use: Reads a response from the buffer and handles it.
402 extern void kx_response(keyexch */*kx*/, buf */*b*/);
404 /* --- @kx_free@ --- *
406 * Arguments: @keyexch *kx@ = pointer to key exchange context
410 * Use: Frees everything in a key exchange context.
413 extern void kx_free(keyexch */*kx*/);
415 /* --- @kx_newkeys@ --- *
417 * Arguments: @keyexch *kx@ = pointer to key exchange context
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
427 extern void kx_newkeys(keyexch */*kx*/);
429 /* --- @kx_init@ --- *
431 * Arguments: @keyexch *kx@ = pointer to key exchange context
432 * @peer *p@ = pointer to peer context
433 * @keyset **ks@ = pointer to keyset list
435 * Returns: Zero if OK, nonzero if it failed.
437 * Use: Initializes a key exchange module. The module currently
438 * contains no keys, and will attempt to initiate a key
442 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
444 /*----- Keysets and symmetric cryptography --------------------------------*/
446 /* --- @ks_free@ --- *
448 * Arguments: @keyset **ksroot@ = pointer to keyset list head
452 * Use: Frees all of the keys in a keyset.
455 extern void ks_free(keyset **/*ksroot*/);
457 /* --- @ks_prune@ --- *
459 * Arguments: @keyset **ksroot@ = pointer to keyset list head
463 * Use: Prunes the keyset list by removing keys which mustn't be used
467 extern void ks_prune(keyset **/*ksroot*/);
469 /* --- @ks_gen@ --- *
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
475 * Returns: The regeneration time for the new key.
477 * Use: Derives a keyset from the given key material and adds it to
481 extern time_t ks_gen(keyset **/*ksroot*/, const void */*k*/, size_t /*sz*/);
483 /* --- @ks_encrypt@ --- *
485 * Arguments: @keyset **ksroot@ = pointer to keyset list head
486 * @buf *b@ = pointer to input buffer
487 * @buf *bb@ = pointer to output buffer
489 * Returns: Nonzero if a new key is needed.
491 * Use: Encrypts a packet.
494 extern int ks_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
496 /* --- @ks_decrypt@ --- *
498 * Arguments: @keyset **ksroot@ = pointer to keyset list head
499 * @buf *b@ = pointer to input buffer
500 * @buf *bb@ = pointer to output buffer
502 * Returns: Nonzero if the packet couldn't be decrypted.
504 * Use: Decrypts a packet.
507 extern int ks_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
509 /*----- Administration interface ------------------------------------------*/
511 /* --- @a_warn@ --- *
513 * Arguments: @const char *fmt@ = pointer to format string
514 * @...@ = other arguments
518 * Use: Informs all admin connections of a warning.
521 extern void a_warn(const char */*fmt*/, ...);
523 /* --- @a_create@ --- *
525 * Arguments: @int fd_in, fd_out@ = file descriptors to use
529 * Use: Creates a new admin connection.
532 extern void a_create(int /*fd_in*/, int /*fd_out*/);
534 /* --- @a_quit@ --- *
540 * Use: Shuts things down nicely.
543 extern void a_quit(void);
545 /* --- @a_daemon@ --- *
551 * Use: Informs the admin module that it's a daemon.
554 extern void a_daemon(void);
556 /* --- @a_init@ --- *
558 * Arguments: @const char *sock@ = socket name to create
562 * Use: Creates the admin listening socket.
565 extern void a_init(const char */*sock*/);
567 /*----- Peer management ---------------------------------------------------*/
569 /* --- @p_txstart@ --- *
571 * Arguments: @peer *p@ = pointer to peer block
572 * @unsigned msg@ = message type code
574 * Returns: A pointer to a buffer to write to.
576 * Use: Starts sending to a peer. Only one send can happen at a
580 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
582 /* --- @p_txend@ --- *
584 * Arguments: @peer *p@ = pointer to peer block
588 * Use: Sends a packet to the peer.
591 extern void p_txend(peer */*p*/);
595 * Arguments: @peer *p@ = pointer to peer block
596 * @buf *b@ = buffer containing incoming packet
600 * Use: Handles a packet which needs to be sent to a peer.
603 extern void p_tun(peer */*p*/, buf */*b*/);
605 /* --- @p_interval@ --- *
611 * Use: Called periodically to do tidying.
614 extern void p_interval(void);
616 /* --- @p_ifname@ --- *
618 * Arguments: @peer *p@ = pointer to a peer block
620 * Returns: A pointer to the peer's interface name.
623 extern const char *p_ifname(peer */*p*/);
625 /* --- @p_addr@ --- *
627 * Arguments: @peer *p@ = pointer to a peer block
629 * Returns: A pointer to the peer's address.
632 extern const addr *p_addr(peer */*p*/);
634 /* --- @p_init@ --- *
636 * Arguments: @unsigned port@ = port number to listen to
640 * Use: Initializes the peer system; creates the socket.
643 extern void p_init(unsigned /*port*/);
645 /* --- @p_port@ --- *
649 * Returns: Port number used for socket.
652 unsigned p_port(void);
654 /* --- @p_create@ --- *
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
660 * Returns: Pointer to the peer block, or null if it failed.
662 * Use: Creates a new named peer block. No peer is actually attached
666 extern peer *p_create(const char */*name*/,
667 struct sockaddr */*sa*/, size_t /*sz*/);
669 /* --- @p_name@ --- *
671 * Arguments: @peer *p@ = pointer to a peer block
673 * Returns: A pointer to the peer's name.
676 extern const char *p_name(peer */*p*/);
678 /* --- @p_find@ --- *
680 * Arguments: @const char *name@ = name to look up
682 * Returns: Pointer to the peer block, or null if not found.
684 * Use: Finds a peer by name.
687 extern peer *p_find(const char */*name*/);
689 /* --- @p_destroy@ --- *
691 * Arguments: @peer *p@ = pointer to a peer
695 * Use: Destroys a peer.
698 extern void p_destroy(peer */*p*/);
700 /* --- @p_first@, @p_next@ --- *
702 * Arguments: @peer *p@ = a peer block
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.
709 extern peer *p_first(void);
710 extern peer *p_next(peer */*p*/);
712 /*----- Tunnel interface --------------------------------------------------*/
714 /* --- @tun_init@ --- *
720 * Use: Initializes the tunneling system. Maybe this will require
721 * opening file descriptors or something.
724 extern void tun_init(void);
726 /* --- @tun_create@ --- *
728 * Arguments: @tunnel *t@ = pointer to tunnel block
729 * @peer *p@ = pointer to peer block
731 * Returns: Zero if it worked, nonzero on failure.
733 * Use: Initializes a new tunnel.
736 extern int tun_create(tunnel */*t*/, peer */*p*/);
738 /* --- @tun_ifname@ --- *
740 * Arguments: @tunnel *t@ = pointer to tunnel block
742 * Returns: A pointer to the tunnel's interface name.
745 extern const char *tun_ifname(tunnel */*t*/);
747 /* --- @tun_inject@ --- *
749 * Arguments: @tunnel *t@ = pointer to tunnel block
750 * @buf *b@ = buffer to send
754 * Use: Injects a packet into the local network stack.
757 extern void tun_inject(tunnel */*t*/, buf */*b*/);
759 /* --- @tun_destroy@ --- *
761 * Arguments: @tunnel *t@ = pointer to tunnel block
765 * Use: Destroys a tunnel.
768 extern void tun_destroy(tunnel */*t*/);
770 /*----- Buffer handling ---------------------------------------------------*/
772 /* --- Useful macros --- */
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))
784 #define BENSURE(b, sz) \
785 (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
787 /* --- @buf_init@ --- *
789 * Arguments: @buf *b@ = pointer to a buffer block
790 * @void *p@ = pointer to a buffer
791 * @size_t sz@ = size of the buffer
795 * Use: Initializes the buffer block appropriately.
798 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
800 /* --- @buf_break@ --- *
802 * Arguments: @buf *b@ = pointer to a buffer block
804 * Returns: Some negative value.
806 * Use: Marks a buffer as broken.
809 extern int buf_break(buf */*b*/);
811 /* --- @buf_ensure@ --- *
813 * Arguments: @buf *b@ = pointer to a buffer block
814 * @size_t sz@ = size of data wanted
816 * Returns: Zero if it worked, nonzero if there wasn't enough space.
818 * Use: Ensures that there are @sz@ bytes still in the buffer.
821 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
823 /* --- @buf_get@ --- *
825 * Arguments: @buf *b@ = pointer to a buffer block
826 * @void *p@ = pointer to a buffer
827 * @size_t sz@ = size of the buffer
829 * Returns: Zero if it worked, nonzero if there wasn't enough data.
831 * Use: Fetches data from the buffer into some other place.
834 extern int buf_get(buf */*b*/, void */*p*/, size_t /*sz*/);
836 /* --- @buf_put@ --- *
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
842 * Returns: Zero if it worked, nonzero if there wasn't enough space.
844 * Use: Fetches data from some place and puts it in the buffer
847 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
849 /* --- @buf_getbyte@ --- *
851 * Arguments: @buf *b@ = pointer to a buffer block
853 * Returns: A byte, or less than zero if there wasn't a byte there.
855 * Use: Gets a single byte from a buffer.
858 extern int buf_getbyte(buf */*b*/);
860 /* --- @buf_putbyte@ --- *
862 * Arguments: @buf *b@ = pointer to a buffer block
863 * @int ch@ = byte to write
865 * Returns: Zero if OK, nonzero if there wasn't enough space.
867 * Use: Puts a single byte in a buffer.
870 extern int buf_putbyte(buf */*b*/, int /*ch*/);
872 /* --- @buf_getword@ --- *
874 * Arguments: @buf *b@ = pointer to a buffer block
875 * @uint32 *w@ = where to put the word
877 * Returns: Zero if OK, or nonzero if there wasn't a word there.
879 * Use: Gets a 32-bit word from a buffer.
882 extern int buf_getword(buf */*b*/, uint32 */*w*/);
884 /* --- @buf_putword@ --- *
886 * Arguments: @buf *b@ = pointer to a buffer block
887 * @uint32 w@ = word to write
889 * Returns: Zero if OK, nonzero if there wasn't enough space.
891 * Use: Puts a 32-but word in a buffer.
894 extern int buf_putword(buf */*b*/, uint32 /*w*/);
896 /* --- @buf_getmp@ --- *
898 * Arguments: @buf *b@ = pointer to a buffer block
900 * Returns: A multiprecision integer, or null if there wasn't one there.
902 * Use: Gets a multiprecision integer from a buffer.
905 extern mp *buf_getmp(buf */*b*/, mp */*d*/);
907 /* --- @buf_putmp@ --- *
909 * Arguments: @buf *b@ = pointer to a buffer block
910 * @mp *m@ = a multiprecision integer
912 * Returns: Zero if it worked, nonzero if there wasn't enough space.
914 * Use: Puts a multiprecision integer to a buffer.
917 extern int buf_putmp(buf */*b*/, mp */*m*/);
919 /*----- Other handy utilities ---------------------------------------------*/
923 * Arguments: @mp *m@ = a multiprecision integer
925 * Returns: A pointer to the integer's textual representation.
927 * Use: Converts a multiprecision integer to a string. Corrupts
931 extern const char *mpstr(mp */*m*/);
933 /*----- That's all, folks -------------------------------------------------*/