/*
- * Copyright (C) 2005-2012 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
*result = rsa;
return 1;
fail:
- if (rsa)
- RSA_free(rsa);
+ RSA_free(rsa);
return ret;
}
*/
#define RC4_ALIGN 8
-int sc_send_bin_buffer(struct stream_cipher_context *scc, char *buf,
- size_t len)
+void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
{
- int ret;
- unsigned char *tmp;
- static unsigned char remainder[RC4_ALIGN];
- size_t l1 = ROUND_DOWN(len, RC4_ALIGN), l2 = ROUND_UP(len, RC4_ALIGN);
-
- assert(len);
- tmp = para_malloc(l2);
- RC4(&scc->send->key, l1, (const unsigned char *)buf, tmp);
+ size_t len = src->iov_len, l1, l2;
+ RC4_KEY *key = &sc->key;
+
+ assert(len > 0);
+ assert(len < ((typeof(src->iov_len))-1) / 2);
+ l1 = ROUND_DOWN(len, RC4_ALIGN);
+ l2 = ROUND_UP(len, RC4_ALIGN);
+
+ *dst = (typeof(*dst)) {
+ /* Add one for the terminating zero byte. */
+ .iov_base = para_malloc(l2 + 1),
+ .iov_len = len
+ };
+ RC4(key, l1, src->iov_base, dst->iov_base);
if (len > l1) {
- memcpy(remainder, buf + l1, len - l1);
- RC4(&scc->send->key, len - l1, remainder, tmp + l1);
+ unsigned char remainder[RC4_ALIGN] = "";
+ memcpy(remainder, src->iov_base + l1, len - l1);
+ RC4(key, len - l1, remainder, dst->iov_base + l1);
}
- ret = xwrite(scc->fd, (char *)tmp, len);
- free(tmp);
- return ret;
-}
-
-int sc_recv_bin_buffer(struct stream_cipher_context *scc, char *buf,
- size_t size)
-{
- unsigned char *tmp = para_malloc(ROUND_UP(size, RC4_ALIGN));
- ssize_t ret = recv(scc->fd, tmp, size, 0);
-
- if (ret > 0)
- RC4(&scc->recv->key, ret, tmp, (unsigned char *)buf);
- else if (ret < 0)
- ret = -ERRNO_TO_PARA_ERROR(errno);
- free(tmp);
- return ret;
+ ((char *)dst->iov_base)[len] = '\0';
}
void hash_function(const char *data, unsigned long len, unsigned char *hash)