forked from wolfSSL/wolfssl
Refactor of the ChaCha hard coded variables.
This commit is contained in:
@@ -77,12 +77,12 @@
|
|||||||
*/
|
*/
|
||||||
int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
|
int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
|
||||||
{
|
{
|
||||||
word32 temp[3]; /* used for alignment of memory */
|
word32 temp[CHACHA_IV_WORDS];/* used for alignment of memory */
|
||||||
|
|
||||||
#ifdef CHACHA_AEAD_TEST
|
#ifdef CHACHA_AEAD_TEST
|
||||||
word32 i;
|
word32 i;
|
||||||
printf("NONCE : ");
|
printf("NONCE : ");
|
||||||
for (i = 0; i < 12; i++) {
|
for (i = 0; i < CHACHA_IV_BYTES; i++) {
|
||||||
printf("%02x", inIv[i]);
|
printf("%02x", inIv[i]);
|
||||||
}
|
}
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
@@ -91,12 +91,12 @@ int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
|
|||||||
if (ctx == NULL)
|
if (ctx == NULL)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
XMEMCPY(temp, inIv, 12);
|
XMEMCPY(temp, inIv, CHACHA_IV_BYTES);
|
||||||
|
|
||||||
ctx->X[12] = counter; /* block counter */
|
ctx->X[CHACHA_IV_BYTES+0] = counter; /* block counter */
|
||||||
ctx->X[13] = LITTLE32(temp[0]); /* fixed variable from nonce */
|
ctx->X[CHACHA_IV_BYTES+1] = LITTLE32(temp[0]); /* fixed variable from nonce */
|
||||||
ctx->X[14] = LITTLE32(temp[1]); /* counter from nonce */
|
ctx->X[CHACHA_IV_BYTES+2] = LITTLE32(temp[1]); /* counter from nonce */
|
||||||
ctx->X[15] = LITTLE32(temp[2]); /* counter from nonce */
|
ctx->X[CHACHA_IV_BYTES+3] = LITTLE32(temp[2]); /* counter from nonce */
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -174,12 +174,13 @@ int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
|
|||||||
/**
|
/**
|
||||||
* Converts word into bytes with rotations having been done.
|
* Converts word into bytes with rotations having been done.
|
||||||
*/
|
*/
|
||||||
static INLINE void wc_Chacha_wordtobyte(word32 output[16], const word32 input[16])
|
static INLINE void wc_Chacha_wordtobyte(word32 output[CHACHA_CHUNK_WORDS],
|
||||||
|
const word32 input[CHACHA_CHUNK_WORDS])
|
||||||
{
|
{
|
||||||
word32 x[16];
|
word32 x[CHACHA_CHUNK_WORDS];
|
||||||
word32 i;
|
word32 i;
|
||||||
|
|
||||||
for (i = 0; i < 16; i++) {
|
for (i = 0; i < CHACHA_CHUNK_WORDS; i++) {
|
||||||
x[i] = input[i];
|
x[i] = input[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,11 +195,11 @@ static INLINE void wc_Chacha_wordtobyte(word32 output[16], const word32 input[16
|
|||||||
QUARTERROUND(3, 4, 9, 14)
|
QUARTERROUND(3, 4, 9, 14)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < 16; i++) {
|
for (i = 0; i < CHACHA_CHUNK_WORDS; i++) {
|
||||||
x[i] = PLUS(x[i], input[i]);
|
x[i] = PLUS(x[i], input[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < 16; i++) {
|
for (i = 0; i < CHACHA_CHUNK_WORDS; i++) {
|
||||||
output[i] = LITTLE32(x[i]);
|
output[i] = LITTLE32(x[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -210,7 +211,7 @@ static void wc_Chacha_encrypt_bytes(ChaCha* ctx, const byte* m, byte* c,
|
|||||||
word32 bytes)
|
word32 bytes)
|
||||||
{
|
{
|
||||||
byte* output;
|
byte* output;
|
||||||
word32 temp[16]; /* used to make sure aligned */
|
word32 temp[CHACHA_CHUNK_WORDS]; /* used to make sure aligned */
|
||||||
word32 i;
|
word32 i;
|
||||||
|
|
||||||
output = (byte*)temp;
|
output = (byte*)temp;
|
||||||
@@ -218,19 +219,19 @@ static void wc_Chacha_encrypt_bytes(ChaCha* ctx, const byte* m, byte* c,
|
|||||||
if (!bytes) return;
|
if (!bytes) return;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
wc_Chacha_wordtobyte(temp, ctx->X);
|
wc_Chacha_wordtobyte(temp, ctx->X);
|
||||||
ctx->X[12] = PLUSONE(ctx->X[12]);
|
ctx->X[CHACHA_IV_BYTES] = PLUSONE(ctx->X[CHACHA_IV_BYTES]);
|
||||||
if (bytes <= 64) {
|
if (bytes <= CHACHA_CHUNK_BYTES) {
|
||||||
for (i = 0; i < bytes; ++i) {
|
for (i = 0; i < bytes; ++i) {
|
||||||
c[i] = m[i] ^ output[i];
|
c[i] = m[i] ^ output[i];
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (i = 0; i < 64; ++i) {
|
for (i = 0; i < CHACHA_CHUNK_BYTES; ++i) {
|
||||||
c[i] = m[i] ^ output[i];
|
c[i] = m[i] ^ output[i];
|
||||||
}
|
}
|
||||||
bytes -= 64;
|
bytes -= CHACHA_CHUNK_BYTES;
|
||||||
c += 64;
|
c += CHACHA_CHUNK_BYTES;
|
||||||
m += 64;
|
m += CHACHA_CHUNK_BYTES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -30,12 +30,20 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Size of the IV */
|
||||||
|
#define CHACHA_IV_WORDS 3
|
||||||
|
#define CHACHA_IV_BYTES (CHACHA_IV_WORDS * sizeof(word32))
|
||||||
|
|
||||||
|
/* Size of ChaCha chunks */
|
||||||
|
#define CHACHA_CHUNK_WORDS 16
|
||||||
|
#define CHACHA_CHUNK_BYTES (CHACHA_CHUNK_WORDS * sizeof(word32))
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
CHACHA_ENC_TYPE = 7 /* cipher unique type */
|
CHACHA_ENC_TYPE = 7 /* cipher unique type */
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct ChaCha {
|
typedef struct ChaCha {
|
||||||
word32 X[16]; /* state of cipher */
|
word32 X[CHACHA_CHUNK_WORDS]; /* state of cipher */
|
||||||
} ChaCha;
|
} ChaCha;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user