*/
#define RC4_ALIGN 8
-int sc_send_bin_buffer(struct stream_cipher_context *scc, char *buf,
- size_t len)
-{
- 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);
- if (len > l1) {
- memcpy(remainder, buf + l1, len - l1);
- RC4(&scc->send->key, len - l1, remainder, tmp + 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;
-}
-
void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
{
size_t len = src->iov_len, l1, l2;
*/
void sc_free(struct stream_cipher *sc);
-/**
- * Encrypt and send a buffer.
- *
- * \param scc The context.
- * \param buf The buffer to send.
- * \param len The size of \a buf in bytes.
- *
- * \return The return value of the underyling call to write_all().
- *
- * \sa \ref write_all(), RC4(3).
- */
-int sc_send_bin_buffer(struct stream_cipher_context *scc, char *buf,
- size_t len);
-
-/**
- * Encrypt and send a \p NULL-terminated buffer.
- *
- * \param scc The context.
- * \param buf The buffer to send.
- *
- * \return The return value of the underyling call to sc_send_bin_buffer().
- */
-int sc_send_buffer(struct stream_cipher_context *scc, char *buf);
-
-/**
- * Format, encrypt and send a buffer.
- *
- * \param scc The context.
- * \param fmt A format string.
- *
- * \return The return value of the underyling call to sc_send_buffer().
- */
-__printf_2_3 int sc_send_va_buffer(struct stream_cipher_context *scc,
- const char *fmt, ...);
-
-/**
- * Receive a buffer and decrypt it.
- *
- * \param scc The context.
- * \param buf The buffer to write the decrypted data to.
- * \param size The size of \a buf.
- *
- * \return The number of bytes received on success, negative on errors, zero if
- * the peer has performed an orderly shutdown.
- *
- * \sa recv(2), RC4(3).
- */
-int sc_recv_bin_buffer(struct stream_cipher_context *scc, char *buf,
- size_t size);
-
-/**
- * Receive a buffer, decrypt it and write terminating NULL byte.
- *
- * \param scc The context.
- * \param buf The buffer to write the decrypted data to.
- * \param size The size of \a buf.
- *
- * Read at most \a size - 1 bytes from file descriptor given by \a scc, decrypt
- * the received data and write a NULL byte at the end of the decrypted data.
- *
- * \return The return value of the underlying call to \ref
- * sc_recv_bin_buffer().
- */
-int sc_recv_buffer(struct stream_cipher_context *scc, char *buf, size_t size);
-
/** Size of the hash value in bytes. */
#define HASH_SIZE 20
-
/**
* Compute the hash of the given input data.
*
}
return 0;
}
-
-int sc_recv_buffer(struct stream_cipher_context *scc, char *buf, size_t size)
-{
- int n;
-
- assert(size);
- n = sc_recv_bin_buffer(scc, buf, size - 1);
- if (n >= 0)
- buf[n] = '\0';
- else
- *buf = '\0';
- return n;
-}
-
-int sc_send_buffer(struct stream_cipher_context *scc, char *buf)
-{
- size_t len = strlen(buf);
- int ret = sc_send_bin_buffer(scc, buf, len);
-
- if (ret < 0 || ret == len)
- return ret;
- return -E_SHORT_WRITE;
-}
-
-__printf_2_3 int sc_send_va_buffer(struct stream_cipher_context *scc,
- const char *fmt, ...)
-{
- char *msg;
- int ret;
- va_list ap;
-
- va_start(ap, fmt);
- ret = xvasprintf(&msg, fmt, ap);
- va_end(ap);
- ret = sc_send_bin_buffer(scc, msg, ret);
- free(msg);
- return ret;
-}
free(sc);
}
-int sc_send_bin_buffer(struct stream_cipher_context *scc, char *buf,
- size_t size)
-{
- gcry_error_t gret;
- int ret;
- unsigned char *tmp = para_malloc(size);
-
- assert(size);
- gret = gcry_cipher_encrypt(scc->send->handle, tmp, size,
- (unsigned char *)buf, size);
- assert(gret == 0);
- ret = xwrite(scc->fd, (char *)tmp, size);
- free(tmp);
- return ret;
-}
-
-int sc_recv_bin_buffer(struct stream_cipher_context *scc, char *buf,
- size_t size)
-{
- gcry_error_t gret;
- ssize_t ret = recv(scc->fd, buf, size, 0);
-
- if (ret < 0)
- ret = -ERRNO_TO_PARA_ERROR(errno);
- if (ret <= 0)
- return ret;
- /* perform in-place encryption */
- gret = gcry_cipher_encrypt(scc->recv->handle, (unsigned char *)buf, ret,
- NULL, 0);
- assert(gret == 0);
- return ret;
-}
-
void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
{
gcry_cipher_hd_t handle = sc->handle;