X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=crypt.c;h=206b93486914aaf7896b0f6109bdd7aa57c235c9;hp=8986d0e7a5bc5bbab4a011fc93b8410fd758ea1a;hb=037059c8a25ce134af1eaa6c3fa8ac96b9f7e0b6;hpb=d09716570fc81b71d6ad3d5f543b5f8acf1a5e33 diff --git a/crypt.c b/crypt.c index 8986d0e7..206b9348 100644 --- a/crypt.c +++ b/crypt.c @@ -220,6 +220,34 @@ int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf, } #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. @@ -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); - 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); - 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); @@ -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) - 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);