Let crypto_core_hsalsa20() accept NULL for the default constants

This commit is contained in:
Frank Denis
2016-02-27 16:19:38 +01:00
parent bb596e8eb7
commit 4e9b0b67ce
12 changed files with 45 additions and 84 deletions
@@ -2,9 +2,6 @@
#include "crypto_core_hsalsa20.h"
#include "crypto_scalarmult_curve25519.h"
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
static const unsigned char n[16] = {0};
int crypto_box_curve25519xsalsa20poly1305_beforenm(
@@ -17,5 +14,5 @@ int crypto_box_curve25519xsalsa20poly1305_beforenm(
if (crypto_scalarmult_curve25519(s,sk,pk) != 0) {
return -1;
}
return crypto_core_hsalsa20(k,n,s,sigma);
return crypto_core_hsalsa20(k,n,s,NULL);
}
@@ -8,6 +8,7 @@ Public domain.
#include "../../sodium/common.h"
#define ROUNDS 20
#define U32C(v) (v##U)
typedef unsigned int uint32;
@@ -26,22 +27,29 @@ int crypto_core_hsalsa20(
uint32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
int i;
x0 = LOAD32_LE(c + 0);
if (c == NULL) {
x0 = U32C(0x61707865);
x5 = U32C(0x3320646e);
x10 = U32C(0x79622d32);
x15 = U32C(0x6b206574);
} else {
x0 = LOAD32_LE(c + 0);
x5 = LOAD32_LE(c + 4);
x10 = LOAD32_LE(c + 8);
x15 = LOAD32_LE(c + 12);
}
x1 = LOAD32_LE(k + 0);
x2 = LOAD32_LE(k + 4);
x3 = LOAD32_LE(k + 8);
x4 = LOAD32_LE(k + 12);
x5 = LOAD32_LE(c + 4);
x6 = LOAD32_LE(in + 0);
x7 = LOAD32_LE(in + 4);
x8 = LOAD32_LE(in + 8);
x9 = LOAD32_LE(in + 12);
x10 = LOAD32_LE(c + 8);
x11 = LOAD32_LE(k + 16);
x12 = LOAD32_LE(k + 20);
x13 = LOAD32_LE(k + 24);
x14 = LOAD32_LE(k + 28);
x15 = LOAD32_LE(c + 12);
x6 = LOAD32_LE(in + 0);
x7 = LOAD32_LE(in + 4);
x8 = LOAD32_LE(in + 8);
x9 = LOAD32_LE(in + 12);
for (i = ROUNDS;i > 0;i -= 2) {
x4 ^= rotate( x0+x12, 7);
@@ -11,10 +11,6 @@
#include "crypto_stream_salsa20.h"
#include "utils.h"
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
int
crypto_secretbox_detached(unsigned char *c, unsigned char *mac,
const unsigned char *m,
@@ -27,7 +23,7 @@ crypto_secretbox_detached(unsigned char *c, unsigned char *mac,
unsigned long long i;
unsigned long long mlen0;
crypto_core_hsalsa20(subkey, n, k, sigma);
crypto_core_hsalsa20(subkey, n, k, NULL);
if (((uintptr_t) c >= (uintptr_t) m &&
(uintptr_t) c - (uintptr_t) m < mlen) ||
@@ -93,7 +89,7 @@ crypto_secretbox_open_detached(unsigned char *m, const unsigned char *c,
unsigned long long i;
unsigned long long mlen0;
crypto_core_hsalsa20(subkey, n, k, sigma);
crypto_core_hsalsa20(subkey, n, k, NULL);
crypto_stream_salsa20(block0, crypto_stream_salsa20_KEYBYTES,
n + 16, subkey);
if (crypto_onetimeauth_poly1305_verify(mac, c, clen, block0) != 0) {
@@ -44,29 +44,21 @@ typedef struct chacha_ctx chacha_ctx;
a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
static void
chacha_keysetup(chacha_ctx *ctx, const u8 *k)
{
const unsigned char *constants;
ctx->input[4] = LOAD32_LE(k + 0);
ctx->input[5] = LOAD32_LE(k + 4);
ctx->input[6] = LOAD32_LE(k + 8);
ctx->input[7] = LOAD32_LE(k + 12);
k += 16;
constants = sigma;
ctx->input[8] = LOAD32_LE(k + 0);
ctx->input[9] = LOAD32_LE(k + 4);
ctx->input[10] = LOAD32_LE(k + 8);
ctx->input[11] = LOAD32_LE(k + 12);
ctx->input[0] = LOAD32_LE(constants + 0);
ctx->input[1] = LOAD32_LE(constants + 4);
ctx->input[2] = LOAD32_LE(constants + 8);
ctx->input[3] = LOAD32_LE(constants + 12);
ctx->input[0] = U32C(0x61707865);
ctx->input[1] = U32C(0x3320646e);
ctx->input[2] = U32C(0x79622d32);
ctx->input[3] = U32C(0x6b206574);
ctx->input[4] = LOAD32_LE(k + 0);
ctx->input[5] = LOAD32_LE(k + 4);
ctx->input[6] = LOAD32_LE(k + 8);
ctx->input[7] = LOAD32_LE(k + 12);
ctx->input[8] = LOAD32_LE(k + 16);
ctx->input[9] = LOAD32_LE(k + 20);
ctx->input[10] = LOAD32_LE(k + 24);
ctx->input[11] = LOAD32_LE(k + 28);
}
static void
@@ -12,10 +12,6 @@ Public domain.
typedef unsigned int uint32;
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
int crypto_stream_salsa20(
unsigned char *c,unsigned long long clen,
const unsigned char *n,
@@ -35,7 +31,7 @@ int crypto_stream_salsa20(
for (i = 8;i < 16;++i) in[i] = 0;
while (clen >= 64) {
crypto_core_salsa20(c,in,kcopy,sigma);
crypto_core_salsa20(c,in,kcopy,NULL);
u = 1;
for (i = 8;i < 16;++i) {
@@ -49,7 +45,7 @@ int crypto_stream_salsa20(
}
if (clen) {
crypto_core_salsa20(block,in,kcopy,sigma);
crypto_core_salsa20(block,in,kcopy,NULL);
for (i = 0;i < (unsigned int) clen;++i) c[i] = block[i];
}
sodium_memzero(block, sizeof block);
@@ -14,10 +14,6 @@ Public domain.
typedef unsigned int uint32;
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
int crypto_stream_salsa20_xor_ic(
unsigned char *c,
const unsigned char *m,unsigned long long mlen,
@@ -41,7 +37,7 @@ int crypto_stream_salsa20_xor_ic(
}
while (mlen >= 64) {
crypto_core_salsa20(block,in,kcopy,sigma);
crypto_core_salsa20(block,in,kcopy,NULL);
for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i];
u = 1;
@@ -57,7 +53,7 @@ int crypto_stream_salsa20_xor_ic(
}
if (mlen) {
crypto_core_salsa20(block,in,kcopy,sigma);
crypto_core_salsa20(block,in,kcopy,NULL);
for (i = 0;i < (unsigned int) mlen;++i) c[i] = m[i] ^ block[i];
}
sodium_memzero(block, sizeof block);
@@ -10,10 +10,6 @@ Public domain.
typedef unsigned int uint32;
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
int crypto_stream_salsa2012(
unsigned char *c,unsigned long long clen,
const unsigned char *n,
@@ -33,7 +29,7 @@ int crypto_stream_salsa2012(
for (i = 8;i < 16;++i) in[i] = 0;
while (clen >= 64) {
crypto_core_salsa2012(c,in,kcopy,sigma);
crypto_core_salsa2012(c,in,kcopy,NULL);
u = 1;
for (i = 8;i < 16;++i) {
@@ -47,7 +43,7 @@ int crypto_stream_salsa2012(
}
if (clen) {
crypto_core_salsa2012(block,in,kcopy,sigma);
crypto_core_salsa2012(block,in,kcopy,NULL);
for (i = 0;i < (unsigned int) clen;++i) c[i] = block[i];
}
sodium_memzero(block, sizeof block);
@@ -10,10 +10,6 @@ Public domain.
typedef unsigned int uint32;
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
int crypto_stream_salsa2012_xor(
unsigned char *c,
const unsigned char *m,unsigned long long mlen,
@@ -34,7 +30,7 @@ int crypto_stream_salsa2012_xor(
for (i = 8;i < 16;++i) in[i] = 0;
while (mlen >= 64) {
crypto_core_salsa2012(block,in,kcopy,sigma);
crypto_core_salsa2012(block,in,kcopy,NULL);
for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i];
u = 1;
@@ -50,7 +46,7 @@ int crypto_stream_salsa2012_xor(
}
if (mlen) {
crypto_core_salsa2012(block,in,kcopy,sigma);
crypto_core_salsa2012(block,in,kcopy,NULL);
for (i = 0;i < (unsigned int) mlen;++i) c[i] = m[i] ^ block[i];
}
sodium_memzero(block, sizeof block);
@@ -10,10 +10,6 @@ Public domain.
typedef unsigned int uint32;
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
int crypto_stream_salsa208(
unsigned char *c,unsigned long long clen,
const unsigned char *n,
@@ -33,7 +29,7 @@ int crypto_stream_salsa208(
for (i = 8;i < 16;++i) in[i] = 0;
while (clen >= 64) {
crypto_core_salsa208(c,in,kcopy,sigma);
crypto_core_salsa208(c,in,kcopy,NULL);
u = 1;
for (i = 8;i < 16;++i) {
@@ -47,7 +43,7 @@ int crypto_stream_salsa208(
}
if (clen) {
crypto_core_salsa208(block,in,kcopy,sigma);
crypto_core_salsa208(block,in,kcopy,NULL);
for (i = 0;i < (unsigned int) clen;++i) c[i] = block[i];
}
sodium_memzero(block, sizeof block);
@@ -10,10 +10,6 @@ Public domain.
typedef unsigned int uint32;
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
int crypto_stream_salsa208_xor(
unsigned char *c,
const unsigned char *m,unsigned long long mlen,
@@ -34,7 +30,7 @@ int crypto_stream_salsa208_xor(
for (i = 8;i < 16;++i) in[i] = 0;
while (mlen >= 64) {
crypto_core_salsa208(block,in,kcopy,sigma);
crypto_core_salsa208(block,in,kcopy,NULL);
for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i];
u = 1;
@@ -50,7 +46,7 @@ int crypto_stream_salsa208_xor(
}
if (mlen) {
crypto_core_salsa208(block,in,kcopy,sigma);
crypto_core_salsa208(block,in,kcopy,NULL);
for (i = 0;i < (unsigned int) mlen;++i) c[i] = m[i] ^ block[i];
}
sodium_memzero(block, sizeof block);
@@ -9,10 +9,6 @@ Public domain.
#include "crypto_stream_xsalsa20.h"
#include "utils.h"
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
int crypto_stream_xsalsa20(
unsigned char *c,unsigned long long clen,
const unsigned char *n,
@@ -21,7 +17,7 @@ int crypto_stream_xsalsa20(
{
unsigned char subkey[32];
int ret;
crypto_core_hsalsa20(subkey,n,k,sigma);
crypto_core_hsalsa20(subkey,n,k,NULL);
ret = crypto_stream_salsa20(c,clen,n + 16,subkey);
sodium_memzero(subkey, sizeof subkey);
return ret;
@@ -9,10 +9,6 @@ Public domain.
#include "crypto_stream_xsalsa20.h"
#include "utils.h"
static const unsigned char sigma[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
int crypto_stream_xsalsa20_xor_ic(
unsigned char *c,
const unsigned char *m,unsigned long long mlen,
@@ -22,7 +18,7 @@ int crypto_stream_xsalsa20_xor_ic(
{
unsigned char subkey[32];
int ret;
crypto_core_hsalsa20(subkey,n,k,sigma);
crypto_core_hsalsa20(subkey,n,k,NULL);
ret = crypto_stream_salsa20_xor_ic(c,m,mlen,n + 16,ic,subkey);
sodium_memzero(subkey, sizeof subkey);
return ret;