From a1b3da7dd990a97e0b9e8fe2933b5f2806186c2d Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 2 Feb 2015 21:01:14 +0100 Subject: [PATCH] Add crypto_stream_xsalsa20_ic() --- .../crypto_stream/xsalsa20/ref/api.h | 1 + .../crypto_stream/xsalsa20/ref/xor_xsalsa20.c | 22 ++++++++++++++----- .../include/sodium/crypto_stream_xsalsa20.h | 6 +++++ test/default/stream.c | 14 +++++++++++- test/default/stream.exp | 1 + 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/libsodium/crypto_stream/xsalsa20/ref/api.h b/src/libsodium/crypto_stream/xsalsa20/ref/api.h index 58915f31..1812709e 100644 --- a/src/libsodium/crypto_stream/xsalsa20/ref/api.h +++ b/src/libsodium/crypto_stream/xsalsa20/ref/api.h @@ -2,6 +2,7 @@ #include "crypto_stream_xsalsa20.h" #define crypto_stream crypto_stream_xsalsa20 +#define crypto_stream_xor_ic crypto_stream_xsalsa20_xor_ic #define crypto_stream_xor crypto_stream_xsalsa20_xor #define crypto_stream_KEYBYTES crypto_stream_xsalsa20_KEYBYTES #define crypto_stream_NONCEBYTES crypto_stream_xsalsa20_NONCEBYTES diff --git a/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c b/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c index b77b9f78..4dd2fadd 100644 --- a/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c +++ b/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c @@ -13,6 +13,21 @@ static const unsigned char sigma[16] = { 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' }; +int crypto_stream_xor_ic( + unsigned char *c, + const unsigned char *m,unsigned long long mlen, + const unsigned char *n,uint64_t ic, + const unsigned char *k +) +{ + unsigned char subkey[32]; + int ret; + crypto_core_hsalsa20(subkey,n,k,sigma); + ret = crypto_stream_salsa20_xor_ic(c,m,mlen,n + 16,ic,subkey); + sodium_memzero(subkey, sizeof subkey); + return ret; +} + int crypto_stream_xor( unsigned char *c, const unsigned char *m,unsigned long long mlen, @@ -20,10 +35,5 @@ int crypto_stream_xor( const unsigned char *k ) { - unsigned char subkey[32]; - int ret; - crypto_core_hsalsa20(subkey,n,k,sigma); - ret = crypto_stream_salsa20_xor(c,m,mlen,n + 16,subkey); - sodium_memzero(subkey, sizeof subkey); - return ret; + return crypto_stream_xor_ic(c, m, mlen, n, 0ULL, k); } diff --git a/src/libsodium/include/sodium/crypto_stream_xsalsa20.h b/src/libsodium/include/sodium/crypto_stream_xsalsa20.h index f7ea1449..c09425a0 100644 --- a/src/libsodium/include/sodium/crypto_stream_xsalsa20.h +++ b/src/libsodium/include/sodium/crypto_stream_xsalsa20.h @@ -10,6 +10,7 @@ */ #include +#include #include "export.h" #ifdef __cplusplus @@ -36,6 +37,11 @@ int crypto_stream_xsalsa20_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_xsalsa20_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 diff --git a/test/default/stream.c b/test/default/stream.c index 5e3d5dca..b2786975 100644 --- a/test/default/stream.c +++ b/test/default/stream.c @@ -19,13 +19,25 @@ int main(void) { int i; - crypto_stream(output, 4194304, nonce, firstkey); + crypto_stream(output, sizeof output, nonce, firstkey); crypto_hash_sha256(h, output, sizeof output); for (i = 0; i < 32; ++i) printf("%02x", h[i]); printf("\n"); + assert(sizeof output > 4000); + + crypto_stream_xsalsa20_xor_ic(output, output, 4000, nonce, 0U, firstkey); + for (i = 0; i < 4000; ++i) + assert(output[i] == 0); + + crypto_stream_xsalsa20_xor_ic(output, output, 4000, nonce, 1U, firstkey); + crypto_hash_sha256(h, output, sizeof output); + for (i = 0; i < 32; ++i) + printf("%02x", h[i]); + printf("\n"); + assert(crypto_stream_keybytes() > 0U); assert(crypto_stream_noncebytes() > 0U); assert(strcmp(crypto_stream_primitive(), "xsalsa20") == 0); diff --git a/test/default/stream.exp b/test/default/stream.exp index 5fa208c1..23054b75 100644 --- a/test/default/stream.exp +++ b/test/default/stream.exp @@ -1 +1,2 @@ 662b9d0e3463029156069b12f918691a98f7dfb2ca0393c96bbfc6b1fbd630a2 +0cc9ffaf60a99d221b548e9762385a231121ab226d1c610d2661ced26b6ad5ee