+ if (flen < 0)
+ return -E_ENCRYPT;
+ ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
+ RSA_PKCS1_OAEP_PADDING);
+ return ret < 0? -E_ENCRYPT : ret;
+}
+
+#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 *sc_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 sc_new().
+ */
+void sc_free(struct stream_cipher *sc)
+{
+ free(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, const 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);