Add crypto_stream_xsalsa20_ic()

This commit is contained in:
Frank Denis 2015-02-02 21:01:14 +01:00
parent 666fd4323b
commit a1b3da7dd9
5 changed files with 37 additions and 7 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -10,6 +10,7 @@
*/
#include <stddef.h>
#include <stdint.h>
#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

View File

@ -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);

View File

@ -1 +1,2 @@
662b9d0e3463029156069b12f918691a98f7dfb2ca0393c96bbfc6b1fbd630a2
0cc9ffaf60a99d221b548e9762385a231121ab226d1c610d2661ced26b6ad5ee