Move sha1.[ch] to crypt.[ch] and make crypto API independent of sha1.
[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 Prototypes for paraslash crypto functions. */
8
9 /** Opaque structure for public and private keys. */
10 struct asymmetric_key;
11
12 int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
13 unsigned len, unsigned char *outbuf);
14 int priv_decrypt(const char *key_file, unsigned char *outbuf,
15 unsigned char *inbuf, int inlen);
16 int get_asymmetric_key(const char *key_file, int private,
17 struct asymmetric_key **result);
18 void free_asymmetric_key(struct asymmetric_key *key);
19
20 void get_random_bytes_or_die(unsigned char *buf, int num);
21 void init_random_seed_or_die(void);
22
23 /** Opaque structure for stream cipher crypto. */
24 struct stream_cipher;
25
26 /** Number of bytes of the session key. */
27 #define SESSION_KEY_LEN 32
28
29 /**
30 * Used on the server-side for client-server communication encryption.
31 *
32 * The traffic between (the forked child of) para_server and the remote client
33 * process is crypted by a symmetric session key. This structure contains the
34 * keys for the stream cipher and the file descriptor for which these keys
35 * should be used.
36 */
37 struct stream_cipher_context {
38 /** The socket file descriptor. */
39 int fd;
40 /** Key used for receiving data. */
41 struct stream_cipher *recv;
42 /** Key used for sending data. */
43 struct stream_cipher *send;
44 };
45
46 struct stream_cipher *sc_new(const unsigned char *data, int len);
47 void sc_free(struct stream_cipher *sc);
48
49 int sc_send_bin_buffer(struct stream_cipher_context *scc, const char *buf,
50 size_t len);
51 int sc_send_buffer(struct stream_cipher_context *scc, const char *buf);
52 __printf_2_3 int sc_send_va_buffer(struct stream_cipher_context *scc,
53 const char *fmt, ...);
54 int sc_recv_bin_buffer(struct stream_cipher_context *scc, char *buf,
55 size_t size);
56 int sc_recv_buffer(struct stream_cipher_context *scc, char *buf, size_t size);
57
58 /** \cond used to distinguish between loading of private/public key */
59 #define LOAD_PUBLIC_KEY 0
60 #define LOAD_PRIVATE_KEY 1
61 #define CHALLENGE_SIZE 64
62 /** \endcond **/
63
64 /** Size of the hash value in bytes. */
65 #define HASH_SIZE 20
66
67 void hash_function(const char *data, unsigned long len, unsigned char *hash);
68
69 /**
70 * Compare two hashes.
71 *
72 * \param h1 Pointer to the first hash value.
73 * \param h2 Pointer to the second hash value.
74 *
75 * \return 1, -1, or zero, depending on whether \a h1 is greater than,
76 * less than or equal to h2, respectively.
77 */
78 _static_inline_ int hash_compare(unsigned char *h1, unsigned char *h2)
79 {
80 int i;
81
82 for (i = 0; i < HASH_SIZE; i++) {
83 if (h1[i] < h2[i])
84 return -1;
85 if (h1[i] > h2[i])
86 return 1;
87 }
88 return 0;
89 }
90
91 /**
92 * Convert a hash value to ascii format.
93 *
94 * \param hash the hash value.
95 * \param asc Result pointer.
96 *
97 * \a asc must point to an area of at least 2 * \p HASH_SIZE + 1 bytes which
98 * will be filled by the function with the ascii representation of the hash
99 * value given by \a hash, and a terminating \p NULL byte.
100 */
101 _static_inline_ void hash_to_asc(unsigned char *hash, char *asc)
102 {
103 int i;
104 const char hexchar[] = "0123456789abcdef";
105
106 for (i = 0; i < HASH_SIZE; i++) {
107 asc[2 * i] = hexchar[hash[i] >> 4];
108 asc[2 * i + 1] = hexchar[hash[i] & 0xf];
109 }
110 asc[2 * HASH_SIZE] = '\0';
111 }