From b0f798aa6667a8dd2523b8ffddb585cf2dacfdf9 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 19 Jun 2014 00:28:02 -0700 Subject: [PATCH] + crypto_stream_chacha20_xor_ic() to set the initial value of the block counter --- src/libsodium/crypto_stream/chacha20/ref/api.h | 8 ++++---- .../chacha20/ref/stream_chacha20_ref.c | 17 +++++++++++++---- .../chacha20/stream_chacha20_api.c | 11 ++++++++++- .../include/sodium/crypto_stream_chacha20.h | 6 ++++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/libsodium/crypto_stream/chacha20/ref/api.h b/src/libsodium/crypto_stream/chacha20/ref/api.h index 4aed8bfb..3d858670 100644 --- a/src/libsodium/crypto_stream/chacha20/ref/api.h +++ b/src/libsodium/crypto_stream/chacha20/ref/api.h @@ -6,7 +6,7 @@ crypto_stream_chacha20_ref(unsigned char *c, unsigned long long clen, const unsigned char *n, const unsigned char *k); int -crypto_stream_chacha20_ref_xor(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *k); - +crypto_stream_chacha20_ref_xor_ic(unsigned char *c, const unsigned char *m, + unsigned long long mlen, + const unsigned char *n, uint64_t ic, + const unsigned char *k); diff --git a/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c b/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c index dc04b536..7c7d1a59 100644 --- a/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c +++ b/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c @@ -248,19 +248,28 @@ crypto_stream_chacha20_ref(unsigned char *c, unsigned long long clen, } int -crypto_stream_chacha20_ref_xor(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *k) +crypto_stream_chacha20_ref_xor_ic(unsigned char *c, const unsigned char *m, + unsigned long long mlen, + const unsigned char *n, uint64_t ic, + const unsigned char *k) { struct chacha_ctx ctx; + uint8_t ic_bytes[8]; + uint32_t ic_high; + uint32_t ic_low; if (!mlen) { return 0; } + ic_high = U32V(ic >> 32); + ic_low = U32V(ic); + U32TO8_LITTLE(&ic_bytes[0], ic_low); + U32TO8_LITTLE(&ic_bytes[4], ic_high); chacha_keysetup(&ctx, k); - chacha_ivsetup(&ctx, n, NULL); + chacha_ivsetup(&ctx, n, ic_bytes); chacha_encrypt_bytes(&ctx, m, c, mlen); sodium_memzero(&ctx, sizeof ctx); + sodium_memzero(ic_bytes, sizeof ic_bytes); return 0; } diff --git a/src/libsodium/crypto_stream/chacha20/stream_chacha20_api.c b/src/libsodium/crypto_stream/chacha20/stream_chacha20_api.c index bc7ae706..412cdfab 100644 --- a/src/libsodium/crypto_stream/chacha20/stream_chacha20_api.c +++ b/src/libsodium/crypto_stream/chacha20/stream_chacha20_api.c @@ -18,10 +18,19 @@ crypto_stream_chacha20(unsigned char *c, unsigned long long clen, return crypto_stream_chacha20_ref(c, clen, n, k); } +int +crypto_stream_chacha20_xor_ic(unsigned char *c, const unsigned char *m, + unsigned long long mlen, + const unsigned char *n, uint64_t ic, + const unsigned char *k) +{ + return crypto_stream_chacha20_ref_xor_ic(c, m, mlen, n, ic, k); +} + int crypto_stream_chacha20_xor(unsigned char *c, const unsigned char *m, unsigned long long mlen, const unsigned char *n, const unsigned char *k) { - return crypto_stream_chacha20_ref_xor(c, m, mlen, n, k); + return crypto_stream_chacha20_ref_xor_ic(c, m, mlen, n, 0U, k); } diff --git a/src/libsodium/include/sodium/crypto_stream_chacha20.h b/src/libsodium/include/sodium/crypto_stream_chacha20.h index df66d554..6b577fcc 100644 --- a/src/libsodium/include/sodium/crypto_stream_chacha20.h +++ b/src/libsodium/include/sodium/crypto_stream_chacha20.h @@ -10,6 +10,7 @@ */ #include +#include #include "export.h" #ifdef __cplusplus @@ -36,6 +37,11 @@ int crypto_stream_chacha20_xor(unsigned char *c, const unsigned char *m, unsigned long long mlen, const unsigned char *n, const unsigned char *k); +SODIUM_EXPORT +int crypto_stream_chacha20_xor_ic(unsigned char *c, const unsigned char *m, + unsigned long long mlen, + const unsigned char *n, uint64_t ic, + const unsigned char *k); #ifdef __cplusplus } #endif