client: Do not leak buffer tree node on exit.
[paraslash.git] / crypt.h
diff --git a/crypt.h b/crypt.h
index 13a5505c9f56336ac6db43ab91fbbb65f965c065..21abe41f68cd8c5b335cc133b5c6f3050869c4ee 100644 (file)
--- a/crypt.h
+++ b/crypt.h
@@ -20,8 +20,12 @@ void free_asymmetric_key(struct asymmetric_key *key);
 void get_random_bytes_or_die(unsigned char *buf, int num);
 void init_random_seed_or_die(void);
 
+/** Opaque structure for stream cipher crypto. */
 struct stream_cipher;
 
+/** Number of bytes of the session key. */
+#define SESSION_KEY_LEN 32
+
 /**
  * Used on the server-side for client-server communication encryption.
  *
@@ -30,7 +34,7 @@ struct stream_cipher;
  * keys for the stream cipher and the file descriptor for which these keys
  * should be used.
  */
-struct rc4_context {
+struct stream_cipher_context {
        /** The socket file descriptor. */
        int fd;
        /** Key used for receiving data. */
@@ -39,17 +43,69 @@ struct rc4_context {
        struct stream_cipher *send;
 };
 
-struct stream_cipher *stream_cipher_new(const unsigned char *data, int len);
-void stream_cipher_free(struct stream_cipher *sc);
+struct stream_cipher *sc_new(const unsigned char *data, int len);
+void sc_free(struct stream_cipher *sc);
 
-int rc4_send_bin_buffer(struct rc4_context *rc4c, const char *buf, size_t len);
-int rc4_send_buffer(struct rc4_context *rc4c, const char *buf);
-__printf_2_3 int rc4_send_va_buffer(struct rc4_context *rc4c, const char *fmt, ...);
-int rc4_recv_bin_buffer(struct rc4_context *rcc, char *buf, size_t size);
-int rc4_recv_buffer(struct rc4_context *rcc, char *buf, size_t size);
+int sc_send_bin_buffer(struct stream_cipher_context *scc, const char *buf,
+               size_t len);
+int sc_send_buffer(struct stream_cipher_context *scc, const char *buf);
+__printf_2_3 int sc_send_va_buffer(struct stream_cipher_context *scc,
+               const char *fmt, ...);
+int sc_recv_bin_buffer(struct stream_cipher_context *scc, char *buf,
+               size_t size);
+int sc_recv_buffer(struct stream_cipher_context *scc, char *buf, size_t size);
 
 /** \cond used to distinguish between loading of private/public key */
 #define LOAD_PUBLIC_KEY 0
 #define LOAD_PRIVATE_KEY 1
 #define CHALLENGE_SIZE 64
 /** \endcond **/
+
+/** Size of the hash value in bytes. */
+#define HASH_SIZE 20
+
+void hash_function(const char *data, unsigned long len, unsigned char *hash);
+
+/**
+ * Compare two hashes.
+ *
+ * \param h1 Pointer to the first hash value.
+ * \param h2 Pointer to the second hash value.
+ *
+ * \return 1, -1, or zero, depending on whether \a h1 is greater than,
+ * less than or equal to h2, respectively.
+ */
+_static_inline_ int hash_compare(unsigned char *h1, unsigned char *h2)
+{
+       int i;
+
+       for (i = 0; i < HASH_SIZE; i++) {
+               if (h1[i] < h2[i])
+                       return -1;
+               if (h1[i] > h2[i])
+                       return 1;
+       }
+       return 0;
+}
+
+/**
+ * Convert a hash value to ascii format.
+ *
+ * \param hash the hash value.
+ * \param asc Result pointer.
+ *
+ * \a asc must point to an area of at least 2 * \p HASH_SIZE + 1 bytes which
+ * will be filled by the function with the ascii representation of the hash
+ * value given by \a hash, and a terminating \p NULL byte.
+ */
+_static_inline_ void hash_to_asc(unsigned char *hash, char *asc)
+{
+       int i;
+       const char hexchar[] = "0123456789abcdef";
+
+       for (i = 0; i < HASH_SIZE; i++) {
+               asc[2 * i] = hexchar[hash[i] >> 4];
+               asc[2 * i + 1] = hexchar[hash[i] & 0xf];
+       }
+       asc[2 * HASH_SIZE] = '\0';
+}