3 * $Id: tripe.h,v 1.1 2001/02/03 20:26:37 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.1 2001/02/03 20:26:37 mdw
44 /*----- Header files ------------------------------------------------------*/
57 #include <sys/types.h>
63 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
72 #include <mLib/alloc.h>
73 #include <mLib/arena.h>
74 #include <mLib/bres.h>
75 #include <mLib/dstr.h>
77 #include <mLib/fdflags.h>
78 #include <mLib/fwatch.h>
79 #include <mLib/mdwopt.h>
80 #include <mLib/quis.h>
81 #include <mLib/report.h>
83 #include <mLib/selbuf.h>
87 #include <mLib/trace.h>
89 #include <catacomb/gcipher.h>
90 #include <catacomb/gmac.h>
91 #include <catacomb/grand.h>
92 #include <catacomb/key.h>
93 #include <catacomb/paranoia.h>
95 #include <catacomb/blowfish.h>
96 #include <catacomb/blowfish-cbc.h>
97 #include <catacomb/noise.h>
98 #include <catacomb/rand.h>
99 #include <catacomb/rmd160.h>
100 #include <catacomb/rmd160-hmac.h>
102 #include <catacomb/mp.h>
103 #include <catacomb/mpmont.h>
104 #include <catacomb/mprand.h>
105 #include <catacomb/dh.h>
111 /*----- Magic numbers -----------------------------------------------------*/
113 /* --- Tunnel types --- */
119 /* --- Trace flags --- */
127 #define T_KEYEXCH 64u
128 #define T_KEYMGMT 128u
134 #define SEC(n) (n##u)
135 #define MIN(n) (n##u * 60u)
136 #define MEG(n) (n##ul * 1024ul * 1024ul)
138 /* --- Other things --- */
140 #define PKBUFSZ 65536
142 /*----- TrIPE protocol ----------------------------------------------------*/
144 /* --- TrIPE packet format --- *
146 * A packet begins with a single-byte packet type. The remaining data
147 * depends on the packet type.
150 #define MSG_PACKET 0u
151 /* Followed by a 64-bit MAC and an encrypted packet. The MAC is used as an
152 * IV for a 64-bit block cipher in CBC-stealing mode.
155 #define MSG_PRECHALLENGE 1u
156 /* Followed by the challenge only. Useful for bootstrapping the system.
159 #define MSG_CHALLENGE 2u
160 /* Followed by a response hash and a large-integer challenge.
163 #define MSG_RESPONSE 3u
164 /* Followed by a large-integer response.
167 /*----- Data structures ---------------------------------------------------*/
171 * Buffers provide a simple stream-like interface for building and parsing
176 octet *base, *p, *limit; /* Pointers to the buffer */
177 unsigned f; /* Various flags */
180 #define BF_BROKEN 1u /* Buffer is broken */
182 /* --- Socket addresses --- *
184 * A magic union of supported socket addresses.
189 struct sockaddr_in sin;
192 /* --- A symmetric keyset --- *
194 * A keyset contains a set of symmetric keys for encrypting and decrypting
195 * packets. Keysets are stored in a list, sorted in reverse order of
196 * creation, so that the most recent keyset (the one most likely to be used)
199 * Each keyset has a time limit and a data limit. The keyset is destroyed
200 * when either it has existed for too long, or it has been used to encrypt
201 * too much data. New key exchanges are triggered when keys are close to
205 typedef struct keyset {
206 struct keyset *next; /* Next active keyset in the list */
207 time_t t_exp; /* Expiry time for this keyset */
208 unsigned long sz_exp; /* Data limit for the keyset */
210 unsigned seq; /* Sequence number for tracing */
212 gcipher *c; /* Keyset cipher for encryption */
213 gmac *m; /* Keyset MAC for integrity */
216 /* --- Key exchange --- *
218 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
219 * Protocol has a number of desirable features (e.g., perfect forward
220 * secrecy, and zero-knowledge authentication) which make it attractive for
221 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
225 typedef struct keyexch {
226 keyset **ks; /* Peer's list of keysets */
227 struct peer *p; /* Pointer back to the peer */
228 unsigned f; /* Various useful flags */
229 sel_timer t; /* Timer for next exchange */
230 dh_pub kpub; /* Peer's public key */
231 mp *my_x, *my_gx, *my_gxy; /* My half of the exchange */
232 octet my_h[RMD160_HASHSZ]; /* My challenge hash */
233 mp *your_gx, *your_gxy; /* Your half of the exchange */
234 octet your_h[RMD160_HASHSZ]; /* Your challenge hash */
235 time_t t_valid; /* When this exchange goes bad */
236 time_t t_qchal, t_qresp; /* Quiet timers for packet types */
237 time_t t_newchal; /* When to accept a new challenge */
240 #define KXF_TIMER 1u /* Waiting for a timer to go off */
241 #define KXF_INIT 2u /* Big numbers are initialized */
242 #define KXF_MYH 4u /* My hash has been computed */
243 #define KXF_YOURH 8u /* Your hash has been received */
244 #define KXF_REPLY 16u /* Received your response OK */
245 #define KXF_DONE 32u /* Key exchange completed */
247 /* --- Tunnel structure --- *
249 * Used to maintain system-specific information about the tunnel interface.
252 typedef struct tunnel {
253 #if TUN_TYPE == TUN_UNET
254 sel_file f; /* Selector for Usernet device */
255 struct peer *p; /* Pointer to my peer */
257 # error "No support for this tunnel type"
261 /* --- Peer structure --- *
263 * The main structure which glues everything else together.
266 typedef struct peer {
267 struct peer *next, *prev; /* Links to next and previous */
268 char *name; /* Name of this peer */
269 tunnel t; /* Tunnel for local packets */
270 keyset *ks; /* List head for keysets */
271 keyexch kx; /* Key exchange protocol block */
272 buf b; /* Buffer for sending packets */
273 addr peer; /* Peer socket address */
274 size_t sasz; /* Socket address size */
277 /* --- Admin structure --- */
279 typedef struct admin {
280 struct admin *next, *prev; /* Links to next and previous */
281 selbuf b; /* Line buffer for commands */
282 int fd; /* File descriptor for output */
284 unsigned seq; /* Sequence number for tracing */
286 char *pname; /* Peer name to create */
287 char *paddr; /* Address string to resolve */
288 bres_client r; /* Background resolver task */
289 sel_timer t; /* Timer for resolver */
290 addr peer; /* Address to set */
291 size_t sasz; /* Size of the address */
294 /*----- Global variables --------------------------------------------------*/
296 extern sel_state sel; /* Global I/O event state */
297 extern dh_priv kpriv; /* Our private key */
298 extern mpmont mg; /* Montgomery context for DH group */
299 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ]; /* Big packet buffers */
302 extern const trace_opt tr_opts[]; /* Trace options array */
303 extern unsigned tr_flags; /* Trace options flags */
306 /*----- Key management ----------------------------------------------------*/
308 /* --- @km_interval@ --- *
312 * Returns: Zero if OK, nonzero to force reloading of keys.
314 * Use: Called on the interval timer to perform various useful jobs.
317 extern int km_interval(void);
319 /* --- @km_init@ --- *
321 * Arguments: @const char *kr_priv@ = private keyring file
322 * @const char *kr_pub@ = public keyring file
323 * @const char *tag@ = tag to load
327 * Use: Initializes, and loads the private key.
330 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
331 const char */*tag*/);
333 /* --- @km_getpubkey@ --- *
335 * Arguments: @const char *tag@ = public key tag to load
336 * @dh_pub *kpub@ = where to put the public key
338 * Returns: Zero if OK, nonzero if it failed.
340 * Use: Fetches a public key from the keyring.
343 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/);
345 /*----- Key exchange ------------------------------------------------------*/
347 /* --- @kx_start@ --- *
349 * Arguments: @keyexch *kx@ = pointer to key exchange context
353 * Use: Stimulates a key exchange. If a key exchage is in progress,
354 * a new challenge is sent (unless the quiet timer forbids
355 * this); if no exchange is in progress, one is commenced.
358 extern void kx_start(keyexch */*kx*/);
360 /* --- @kx_prechallenge@ --- *
362 * Arguments: @keyexch *kx@ = pointer to key exhange context
363 * @buf *b@ = pointer to buffer containing the packet
367 * Use: Reads a prechallenge packet from the buffer and handles it.
370 extern void kx_prechallenge(keyexch */*kx*/, buf */*b*/);
372 /* --- @kx_challenge@ --- *
374 * Arguments: @keyexch *kx@ = pointer to key exchange context
375 * @buf *b@ = a buffer containing the packet to read
379 * Use: Reads a challenge from the buffer and handles it.
382 extern void kx_challenge(keyexch */*kx*/, buf */*b*/);
384 /* --- @kx_response@ --- *
386 * Arguments: @keyexch *kx@ = pointer to key exchange context
387 * @buf *b@ = a buffer containing the packet to read
391 * Use: Reads a response from the buffer and handles it.
394 extern void kx_response(keyexch */*kx*/, buf */*b*/);
396 /* --- @kx_free@ --- *
398 * Arguments: @keyexch *kx@ = pointer to key exchange context
402 * Use: Frees everything in a key exchange context.
405 extern void kx_free(keyexch */*kx*/);
407 /* --- @kx_newkeys@ --- *
409 * Arguments: @keyexch *kx@ = pointer to key exchange context
413 * Use: Informs the key exchange module that its keys may have
414 * changed. If fetching the new keys fails, the peer will be
415 * destroyed, we log messages and struggle along with the old
419 extern void kx_newkeys(keyexch */*kx*/);
421 /* --- @kx_init@ --- *
423 * Arguments: @keyexch *kx@ = pointer to key exchange context
424 * @peer *p@ = pointer to peer context
425 * @keyset **ks@ = pointer to keyset list
427 * Returns: Zero if OK, nonzero if it failed.
429 * Use: Initializes a key exchange module. The module currently
430 * contains no keys, and will attempt to initiate a key
434 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
436 /*----- Keysets and symmetric cryptography --------------------------------*/
438 /* --- @ks_free@ --- *
440 * Arguments: @keyset **ksroot@ = pointer to keyset list head
444 * Use: Frees all of the keys in a keyset.
447 extern void ks_free(keyset **/*ksroot*/);
449 /* --- @ks_prune@ --- *
451 * Arguments: @keyset **ksroot@ = pointer to keyset list head
455 * Use: Prunes the keyset list by removing keys which mustn't be used
459 extern void ks_prune(keyset **/*ksroot*/);
461 /* --- @ks_gen@ --- *
463 * Arguments: @keyset **ksroot@ = pointer to keyset list head
464 * @const void *k@ = pointer to key material
465 * @size_t sz@ = size of the key material
467 * Returns: The regeneration time for the new key.
469 * Use: Derives a keyset from the given key material and adds it to
473 extern time_t ks_gen(keyset **/*ksroot*/, const void */*k*/, size_t /*sz*/);
475 /* --- @ks_encrypt@ --- *
477 * Arguments: @keyset **ksroot@ = pointer to keyset list head
478 * @buf *b@ = pointer to input buffer
479 * @buf *bb@ = pointer to output buffer
481 * Returns: Nonzero if a new key is needed.
483 * Use: Encrypts a packet.
486 extern int ks_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
488 /* --- @ks_decrypt@ --- *
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 the packet couldn't be decrypted.
496 * Use: Decrypts a packet.
499 extern int ks_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
501 /*----- Administration interface ------------------------------------------*/
503 /* --- @a_warn@ --- *
505 * Arguments: @const char *fmt@ = pointer to format string
506 * @...@ = other arguments
510 * Use: Informs all admin connections of a warning.
513 extern void a_warn(const char */*fmt*/, ...);
515 /* --- @a_create@ --- *
517 * Arguments: @int fd_in, fd_out@ = file descriptors to use
521 * Use: Creates a new admin connection.
524 extern void a_create(int /*fd_in*/, int /*fd_out*/);
526 /* --- @a_quit@ --- *
532 * Use: Shuts things down nicely.
535 extern void a_quit(void);
537 /* --- @a_daemon@ --- *
543 * Use: Informs the admin module that it's a daemon.
546 extern void a_daemon(void);
548 /* --- @a_init@ --- *
550 * Arguments: @const char *sock@ = socket name to create
554 * Use: Creates the admin listening socket.
557 extern void a_init(const char */*sock*/);
559 /*----- Peer management ---------------------------------------------------*/
561 /* --- @p_txstart@ --- *
563 * Arguments: @peer *p@ = pointer to peer block
564 * @unsigned msg@ = message type code
566 * Returns: A pointer to a buffer to write to.
568 * Use: Starts sending to a peer. Only one send can happen at a
572 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
574 /* --- @p_txend@ --- *
576 * Arguments: @peer *p@ = pointer to peer block
580 * Use: Sends a packet to the peer.
583 extern void p_txend(peer */*p*/);
587 * Arguments: @peer *p@ = pointer to peer block
588 * @buf *b@ = buffer containing incoming packet
592 * Use: Handles a packet which needs to be sent to a peer.
595 extern void p_tun(peer */*p*/, buf */*b*/);
597 /* --- @p_interval@ --- *
603 * Use: Called periodically to do tidying.
606 extern void p_interval(void);
608 /* --- @p_ifname@ --- *
610 * Arguments: @peer *p@ = pointer to a peer block
612 * Returns: A pointer to the peer's interface name.
615 extern const char *p_ifname(peer */*p*/);
617 /* --- @p_addr@ --- *
619 * Arguments: @peer *p@ = pointer to a peer block
621 * Returns: A pointer to the peer's address.
624 extern const addr *p_addr(peer */*p*/);
626 /* --- @p_init@ --- *
628 * Arguments: @unsigned port@ = port number to listen to
632 * Use: Initializes the peer system; creates the socket.
635 extern void p_init(unsigned /*port*/);
637 /* --- @p_port@ --- *
641 * Returns: Port number used for socket.
644 unsigned p_port(void);
646 /* --- @p_create@ --- *
648 * Arguments: @const char *name@ = name for this peer
649 * @struct sockaddr *sa@ = socket address of peer
650 * @size_t sz@ = size of socket address
652 * Returns: Pointer to the peer block, or null if it failed.
654 * Use: Creates a new named peer block. No peer is actually attached
658 extern peer *p_create(const char */*name*/,
659 struct sockaddr */*sa*/, size_t /*sz*/);
661 /* --- @p_name@ --- *
663 * Arguments: @peer *p@ = pointer to a peer block
665 * Returns: A pointer to the peer's name.
668 extern const char *p_name(peer */*p*/);
670 /* --- @p_find@ --- *
672 * Arguments: @const char *name@ = name to look up
674 * Returns: Pointer to the peer block, or null if not found.
676 * Use: Finds a peer by name.
679 extern peer *p_find(const char */*name*/);
681 /* --- @p_destroy@ --- *
683 * Arguments: @peer *p@ = pointer to a peer
687 * Use: Destroys a peer.
690 extern void p_destroy(peer */*p*/);
692 /* --- @p_first@, @p_next@ --- *
694 * Arguments: @peer *p@ = a peer block
696 * Returns: @peer_first@ returns the first peer in some ordering;
697 * @peer_next@ returns the peer following a given one in the
698 * same ordering. Null is returned for the end of the list.
701 extern peer *p_first(void);
702 extern peer *p_next(peer */*p*/);
704 /*----- Tunnel interface --------------------------------------------------*/
706 /* --- @tun_init@ --- *
712 * Use: Initializes the tunneling system. Maybe this will require
713 * opening file descriptors or something.
716 extern void tun_init(void);
718 /* --- @tun_create@ --- *
720 * Arguments: @tunnel *t@ = pointer to tunnel block
721 * @peer *p@ = pointer to peer block
723 * Returns: Zero if it worked, nonzero on failure.
725 * Use: Initializes a new tunnel.
728 extern int tun_create(tunnel */*t*/, peer */*p*/);
730 /* --- @tun_ifname@ --- *
732 * Arguments: @tunnel *t@ = pointer to tunnel block
734 * Returns: A pointer to the tunnel's interface name.
737 extern const char *tun_ifname(tunnel */*t*/);
739 /* --- @tun_inject@ --- *
741 * Arguments: @tunnel *t@ = pointer to tunnel block
742 * @buf *b@ = buffer to send
746 * Use: Injects a packet into the local network stack.
749 extern void tun_inject(tunnel */*t*/, buf */*b*/);
751 /* --- @tun_destroy@ --- *
753 * Arguments: @tunnel *t@ = pointer to tunnel block
757 * Use: Destroys a tunnel.
760 extern void tun_destroy(tunnel */*t*/);
762 /*----- Buffer handling ---------------------------------------------------*/
764 /* --- Useful macros --- */
766 #define BBASE(b) ((b)->base)
767 #define BLIM(b) ((b)->limit)
768 #define BCUR(b) ((b)->p)
769 #define BSZ(b) ((b)->limit - (b)->base)
770 #define BLEN(b) ((b)->p - (b)->base)
771 #define BLEFT(b) ((b)->limit - (b)->p)
772 #define BSTEP(b, sz) ((b)->p += (sz))
773 #define BBAD(b) ((b)->f & BF_BROKEN)
774 #define BOK(b) (!BBAD(b))
776 #define BENSURE(b, sz) \
777 (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
779 /* --- @buf_init@ --- *
781 * Arguments: @buf *b@ = pointer to a buffer block
782 * @void *p@ = pointer to a buffer
783 * @size_t sz@ = size of the buffer
787 * Use: Initializes the buffer block appropriately.
790 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
792 /* --- @buf_break@ --- *
794 * Arguments: @buf *b@ = pointer to a buffer block
796 * Returns: Some negative value.
798 * Use: Marks a buffer as broken.
801 extern int buf_break(buf */*b*/);
803 /* --- @buf_ensure@ --- *
805 * Arguments: @buf *b@ = pointer to a buffer block
806 * @size_t sz@ = size of data wanted
808 * Returns: Zero if it worked, nonzero if there wasn't enough space.
810 * Use: Ensures that there are @sz@ bytes still in the buffer.
813 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
815 /* --- @buf_get@ --- *
817 * Arguments: @buf *b@ = pointer to a buffer block
818 * @void *p@ = pointer to a buffer
819 * @size_t sz@ = size of the buffer
821 * Returns: Zero if it worked, nonzero if there wasn't enough data.
823 * Use: Fetches data from the buffer into some other place.
826 extern int buf_get(buf */*b*/, void */*p*/, size_t /*sz*/);
828 /* --- @buf_put@ --- *
830 * Arguments: @buf *b@ = pointer to a buffer block
831 * @const 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 space.
836 * Use: Fetches data from some place and puts it in the buffer
839 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
841 /* --- @buf_getbyte@ --- *
843 * Arguments: @buf *b@ = pointer to a buffer block
845 * Returns: A byte, or less than zero if there wasn't a byte there.
847 * Use: Gets a single byte from a buffer.
850 extern int buf_getbyte(buf */*b*/);
852 /* --- @buf_putbyte@ --- *
854 * Arguments: @buf *b@ = pointer to a buffer block
855 * @int ch@ = byte to write
857 * Returns: Zero if OK, nonzero if there wasn't enough space.
859 * Use: Puts a single byte in a buffer.
862 extern int buf_putbyte(buf */*b*/, int /*ch*/);
864 /* --- @buf_getword@ --- *
866 * Arguments: @buf *b@ = pointer to a buffer block
867 * @uint32 *w@ = where to put the word
869 * Returns: Zero if OK, or nonzero if there wasn't a word there.
871 * Use: Gets a 32-bit word from a buffer.
874 extern int buf_getword(buf */*b*/, uint32 */*w*/);
876 /* --- @buf_putword@ --- *
878 * Arguments: @buf *b@ = pointer to a buffer block
879 * @uint32 w@ = word to write
881 * Returns: Zero if OK, nonzero if there wasn't enough space.
883 * Use: Puts a 32-but word in a buffer.
886 extern int buf_putword(buf */*b*/, uint32 /*w*/);
888 /* --- @buf_getmp@ --- *
890 * Arguments: @buf *b@ = pointer to a buffer block
892 * Returns: A multiprecision integer, or null if there wasn't one there.
894 * Use: Gets a multiprecision integer from a buffer.
897 extern mp *buf_getmp(buf */*b*/, mp */*d*/);
899 /* --- @buf_putmp@ --- *
901 * Arguments: @buf *b@ = pointer to a buffer block
902 * @mp *m@ = a multiprecision integer
904 * Returns: Zero if it worked, nonzero if there wasn't enough space.
906 * Use: Puts a multiprecision integer to a buffer.
909 extern int buf_putmp(buf */*b*/, mp */*m*/);
911 /*----- Other handy utilities ---------------------------------------------*/
915 * Arguments: @mp *m@ = a multiprecision integer
917 * Returns: A pointer to the integer's textual representation.
919 * Use: Converts a multiprecision integer to a string. Corrupts
923 extern const char *mpstr(mp */*m*/);
925 /*----- That's all, folks -------------------------------------------------*/