]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - crypt.c
Replace direct use of RC4 by stream cipher abstraction.
[paraslash.git] / crypt.c
diff --git a/crypt.c b/crypt.c
index 8986d0e7a5bc5bbab4a011fc93b8410fd758ea1a..206b93486914aaf7896b0f6109bdd7aa57c235c9 100644 (file)
--- a/crypt.c
+++ b/crypt.c
@@ -220,6 +220,34 @@ int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
 }
 
 #define RC4_ALIGN 8
 }
 
 #define RC4_ALIGN 8
+struct stream_cipher {
+       RC4_KEY key;
+};
+
+/**
+ * Allocate and initialize a stream cipher structure.
+ *
+ * \param data The key.
+ * \param len The size of the key.
+ *
+ * \return A new stream cipher structure.
+ */
+struct stream_cipher *stream_cipher_new(const unsigned char *data, int len)
+{
+       struct stream_cipher *sc = para_malloc(sizeof(*sc));
+       RC4_set_key(&sc->key, len, data);
+       return sc;
+}
+
+/**
+ * Deallocate a stream cipher structure.
+ *
+ * \param sc A stream cipher previously obtained by stream_cipher_new().
+ */
+void stream_cipher_free(struct stream_cipher *sc)
+{
+       free(sc);
+}
 
 /**
  * Encrypt and send a buffer.
 
 /**
  * Encrypt and send a buffer.
@@ -241,10 +269,10 @@ int rc4_send_bin_buffer(struct rc4_context *rc4c, const char *buf, size_t len)
 
        assert(len);
        tmp = para_malloc(l2);
 
        assert(len);
        tmp = para_malloc(l2);
-       RC4(&rc4c->send_key, l1, (const unsigned char *)buf, tmp);
+       RC4(&rc4c->send->key, l1, (const unsigned char *)buf, tmp);
        if (len > l1) {
                memcpy(remainder, buf + l1, len - l1);
        if (len > l1) {
                memcpy(remainder, buf + l1, len - l1);
-               RC4(&rc4c->send_key, len - l1, remainder, tmp + l1);
+               RC4(&rc4c->send->key, len - l1, remainder, tmp + l1);
        }
        ret = write_all(rc4c->fd, (char *)tmp, &len);
        free(tmp);
        }
        ret = write_all(rc4c->fd, (char *)tmp, &len);
        free(tmp);
@@ -301,7 +329,7 @@ int rc4_recv_bin_buffer(struct rc4_context *rc4c, char *buf, size_t size)
        ssize_t ret = recv(rc4c->fd, tmp, size, 0);
 
        if (ret > 0)
        ssize_t ret = recv(rc4c->fd, tmp, size, 0);
 
        if (ret > 0)
-               RC4(&rc4c->recv_key, ret, tmp, (unsigned char *)buf);
+               RC4(&rc4c->recv->key, ret, tmp, (unsigned char *)buf);
        else if (ret < 0)
                ret = -ERRNO_TO_PARA_ERROR(errno);
        free(tmp);
        else if (ret < 0)
                ret = -ERRNO_TO_PARA_ERROR(errno);
        free(tmp);