crypt: Move implementation-independent code to separate file.
[paraslash.git] / crypt.h
1 /*
2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file crypt.h Public crypto interface. */
8
9
10 /** \cond used to distinguish between loading of private/public key */
11 #define LOAD_PUBLIC_KEY 0
12 #define LOAD_PRIVATE_KEY 1
13 #define CHALLENGE_SIZE 64
14 /** \endcond **/
15
16 /* asymetric (public key) crypto */
17
18 /** Opaque structure for public and private keys. */
19 struct asymmetric_key;
20
21 int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
22 unsigned len, unsigned char *outbuf);
23 int priv_decrypt(const char *key_file, unsigned char *outbuf,
24 unsigned char *inbuf, int inlen);
25 int get_asymmetric_key(const char *key_file, int private,
26 struct asymmetric_key **result);
27 void free_asymmetric_key(struct asymmetric_key *key);
28
29 /* random numbers */
30 void get_random_bytes_or_die(unsigned char *buf, int num);
31 void init_random_seed_or_die(void);
32
33 /* stream cipher declarations and prototypes */
34
35 /** Opaque structure for stream ciphers. */
36 struct stream_cipher;
37 /** Number of bytes of the session key for stream ciphers. */
38 #define SESSION_KEY_LEN 32
39 /**
40 * Used for client-server communication encryption.
41 *
42 * The traffic between (the forked child of) para_server and the remote client
43 * process is crypted by a symmetric session key. This structure contains the
44 * keys for the stream cipher and the file descriptor for which these keys
45 * should be used.
46 */
47 struct stream_cipher_context {
48 /** The socket file descriptor. */
49 int fd;
50 /** Key used for receiving data. */
51 struct stream_cipher *recv;
52 /** Key used for sending data. */
53 struct stream_cipher *send;
54 };
55 struct stream_cipher *sc_new(const unsigned char *data, int len);
56 void sc_free(struct stream_cipher *sc);
57 int sc_send_bin_buffer(struct stream_cipher_context *scc, const char *buf,
58 size_t len);
59 int sc_send_buffer(struct stream_cipher_context *scc, const char *buf);
60 __printf_2_3 int sc_send_va_buffer(struct stream_cipher_context *scc,
61 const char *fmt, ...);
62 int sc_recv_bin_buffer(struct stream_cipher_context *scc, char *buf,
63 size_t size);
64 int sc_recv_buffer(struct stream_cipher_context *scc, char *buf, size_t size);
65
66 /* hashing */
67
68 /** Size of the hash value in bytes. */
69 #define HASH_SIZE 20
70
71 void hash_function(const char *data, unsigned long len, unsigned char *hash);
72 void hash_to_asc(unsigned char *hash, char *asc);
73 int hash_compare(unsigned char *h1, unsigned char *h2);