Rename all algorithm-specific ctx to state
This commit is contained in:
parent
2b76f3df5b
commit
230dedac16
14 changed files with 406 additions and 404 deletions
|
@ -34,17 +34,17 @@ static int
|
|||
chacha20_anycrypt_init(void *arg, void *initparams)
|
||||
{
|
||||
struct lc_chacha20_params *params = initparams;
|
||||
struct chacha20_ctx *ctx = arg;
|
||||
struct chacha20_state *state = arg;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < CHACHA20_BLOCKLEN_WORDS; i++)
|
||||
ctx->s[i] = 0;
|
||||
state->s[i] = 0;
|
||||
for (i = 0; i < CHACHA20_KEY_WORDS; i++)
|
||||
ctx->k[i] = load32le(¶ms->key[i * 4]);
|
||||
ctx->n[0] = params->counter;
|
||||
state->k[i] = load32le(¶ms->key[i * 4]);
|
||||
state->n[0] = params->counter;
|
||||
for (i = 1; i < CHACHA20_NONCE_WORDS; i++)
|
||||
ctx->n[i] = load32le(¶ms->nonce[(i - 1) * 4]);
|
||||
ctx->mlen = 0;
|
||||
state->n[i] = load32le(¶ms->nonce[(i - 1) * 4]);
|
||||
state->mlen = 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -53,31 +53,31 @@ static int
|
|||
xchacha20_anycrypt_init(void *arg, void *initparams)
|
||||
{
|
||||
struct lc_xchacha20_params *params = initparams;
|
||||
struct chacha20_ctx *ctx = arg;
|
||||
struct chacha20_state *state = arg;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < CHACHA20_BLOCKLEN_WORDS; i++)
|
||||
ctx->s[i] = 0;
|
||||
state->s[i] = 0;
|
||||
for (i = 0; i < CHACHA20_KEY_WORDS; i++)
|
||||
ctx->k[i] = load32le(¶ms->key[i * 4]);
|
||||
state->k[i] = load32le(¶ms->key[i * 4]);
|
||||
for (i = 0; i < CHACHA20_NONCE_WORDS; i++)
|
||||
ctx->n[i] = load32le(¶ms->nonce[i * 4]);
|
||||
ctx->mlen = 0;
|
||||
state->n[i] = load32le(¶ms->nonce[i * 4]);
|
||||
state->mlen = 0;
|
||||
|
||||
hchacha20_block(ctx);
|
||||
hchacha20_block(state);
|
||||
|
||||
ctx->k[0] = ctx->s[0];
|
||||
ctx->k[1] = ctx->s[1];
|
||||
ctx->k[2] = ctx->s[2];
|
||||
ctx->k[3] = ctx->s[3];
|
||||
ctx->k[4] = ctx->s[12];
|
||||
ctx->k[5] = ctx->s[13];
|
||||
ctx->k[6] = ctx->s[14];
|
||||
ctx->k[7] = ctx->s[15];
|
||||
ctx->n[0] = params->counter;
|
||||
ctx->n[1] = 0;
|
||||
ctx->n[2] = load32le(¶ms->nonce[16]);
|
||||
ctx->n[3] = load32le(¶ms->nonce[20]);
|
||||
state->k[0] = state->s[0];
|
||||
state->k[1] = state->s[1];
|
||||
state->k[2] = state->s[2];
|
||||
state->k[3] = state->s[3];
|
||||
state->k[4] = state->s[12];
|
||||
state->k[5] = state->s[13];
|
||||
state->k[6] = state->s[14];
|
||||
state->k[7] = state->s[15];
|
||||
state->n[0] = params->counter;
|
||||
state->n[1] = 0;
|
||||
state->n[2] = load32le(¶ms->nonce[16]);
|
||||
state->n[3] = load32le(¶ms->nonce[20]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -86,52 +86,52 @@ static int
|
|||
chacha20_anycrypt_update(void *arg, uint8_t *out, size_t *outlen,
|
||||
const uint8_t *in, size_t inlen)
|
||||
{
|
||||
struct chacha20_ctx *ctx = arg;
|
||||
struct chacha20_state *state = arg;
|
||||
size_t i, blocks;
|
||||
uint32_t h;
|
||||
|
||||
*outlen = 0;
|
||||
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) - ctx->mlen)
|
||||
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) - state->mlen)
|
||||
return 0;
|
||||
blocks = (inlen + ctx->mlen + LC_CHACHA20_BLOCKLEN - 1) /
|
||||
blocks = (inlen + state->mlen + LC_CHACHA20_BLOCKLEN - 1) /
|
||||
LC_CHACHA20_BLOCKLEN;
|
||||
if (blocks + ctx->n[0] > CHACHA20_CTRMAX)
|
||||
if (blocks + state->n[0] > CHACHA20_CTRMAX)
|
||||
return 0;
|
||||
|
||||
*outlen = ctx->mlen + inlen -
|
||||
((ctx->mlen + inlen) % LC_CHACHA20_BLOCKLEN);
|
||||
*outlen = state->mlen + inlen -
|
||||
((state->mlen + inlen) % LC_CHACHA20_BLOCKLEN);
|
||||
if (out == NULL)
|
||||
return 1;
|
||||
|
||||
for (i = 0; i + ctx->mlen < LC_CHACHA20_BLOCKLEN && i < inlen; i++)
|
||||
ctx->m[i + ctx->mlen] = in[i];
|
||||
ctx->mlen += i;
|
||||
for (i = 0; i + state->mlen < LC_CHACHA20_BLOCKLEN && i < inlen; i++)
|
||||
state->m[i + state->mlen] = in[i];
|
||||
state->mlen += i;
|
||||
in += i;
|
||||
inlen -= i;
|
||||
|
||||
if (ctx->mlen == LC_CHACHA20_BLOCKLEN) {
|
||||
chacha20_block(ctx);
|
||||
ctx->n[0]++;
|
||||
if (state->mlen == LC_CHACHA20_BLOCKLEN) {
|
||||
chacha20_block(state);
|
||||
state->n[0]++;
|
||||
|
||||
for (i = 0; i < CHACHA20_BLOCKLEN_WORDS; i++) {
|
||||
h = load32le(&ctx->m[i * 4]);
|
||||
h ^= ctx->s[i];
|
||||
h = load32le(&state->m[i * 4]);
|
||||
h ^= state->s[i];
|
||||
store32le(&out[i * 4], h);
|
||||
}
|
||||
out += LC_CHACHA20_BLOCKLEN;
|
||||
ctx->mlen = 0;
|
||||
state->mlen = 0;
|
||||
}
|
||||
|
||||
if (inlen == 0)
|
||||
return 1;
|
||||
|
||||
while (inlen >= LC_CHACHA20_BLOCKLEN) {
|
||||
chacha20_block(ctx);
|
||||
ctx->n[0]++;
|
||||
chacha20_block(state);
|
||||
state->n[0]++;
|
||||
|
||||
for (i = 0; i < CHACHA20_BLOCKLEN_WORDS; i++) {
|
||||
h = load32le(&in[i * 4]);
|
||||
h ^= ctx->s[i];
|
||||
h ^= state->s[i];
|
||||
store32le(&out[i * 4], h);
|
||||
}
|
||||
out += LC_CHACHA20_BLOCKLEN;
|
||||
|
@ -140,8 +140,8 @@ chacha20_anycrypt_update(void *arg, uint8_t *out, size_t *outlen,
|
|||
}
|
||||
|
||||
for (i = 0; i < inlen; i++)
|
||||
ctx->m[i] = in[i];
|
||||
ctx->mlen = inlen;
|
||||
state->m[i] = in[i];
|
||||
state->mlen = inlen;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -149,32 +149,32 @@ chacha20_anycrypt_update(void *arg, uint8_t *out, size_t *outlen,
|
|||
static int
|
||||
chacha20_anycrypt_final(void *arg, uint8_t *out, size_t *outlen)
|
||||
{
|
||||
struct chacha20_ctx *ctx = arg;
|
||||
struct chacha20_state *state = arg;
|
||||
size_t i, off;
|
||||
uint32_t h;
|
||||
uint8_t s[4];
|
||||
|
||||
*outlen = ctx->mlen;
|
||||
*outlen = state->mlen;
|
||||
if (out == NULL)
|
||||
return 1;
|
||||
|
||||
if (ctx->mlen > 0)
|
||||
chacha20_block(ctx);
|
||||
if (state->mlen > 0)
|
||||
chacha20_block(state);
|
||||
|
||||
for (i = 0; i < ctx->mlen / 4; i++) {
|
||||
h = load32le(&ctx->m[i * 4]);
|
||||
h ^= ctx->s[i];
|
||||
for (i = 0; i < state->mlen / 4; i++) {
|
||||
h = load32le(&state->m[i * 4]);
|
||||
h ^= state->s[i];
|
||||
store32le(&out[i * 4], h);
|
||||
}
|
||||
off = i * 4;
|
||||
ctx->mlen -= off;
|
||||
state->mlen -= off;
|
||||
out += off;
|
||||
|
||||
store32le(&s[0], ctx->s[i]);
|
||||
for (i = 0; i < ctx->mlen; i++)
|
||||
out[i] = ctx->m[i + off] ^ s[i];
|
||||
store32le(&s[0], state->s[i]);
|
||||
for (i = 0; i < state->mlen; i++)
|
||||
out[i] = state->m[i + off] ^ s[i];
|
||||
|
||||
lc_scrub(ctx, sizeof(*ctx));
|
||||
lc_scrub(state, sizeof(*state));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -183,7 +183,7 @@ static int
|
|||
chacha20_anycrypt(uint8_t *out, size_t *outlen, void *initparams,
|
||||
const uint8_t *in, size_t inlen)
|
||||
{
|
||||
struct chacha20_ctx ctx;
|
||||
struct chacha20_state state;
|
||||
size_t l0, l1;
|
||||
int rc;
|
||||
|
||||
|
@ -199,9 +199,9 @@ chacha20_anycrypt(uint8_t *out, size_t *outlen, void *initparams,
|
|||
return 1;
|
||||
}
|
||||
|
||||
rc = chacha20_anycrypt_init(&ctx, initparams) &&
|
||||
chacha20_anycrypt_update(&ctx, out, &l0, in, inlen) &&
|
||||
chacha20_anycrypt_final(&ctx, out + l0, &l1);
|
||||
rc = chacha20_anycrypt_init(&state, initparams) &&
|
||||
chacha20_anycrypt_update(&state, out, &l0, in, inlen) &&
|
||||
chacha20_anycrypt_final(&state, out + l0, &l1);
|
||||
|
||||
if (rc)
|
||||
*outlen = l0 + l1;
|
||||
|
@ -213,7 +213,7 @@ static int
|
|||
xchacha20_anycrypt(uint8_t *out, size_t *outlen, void *initparams,
|
||||
const uint8_t *in, size_t inlen)
|
||||
{
|
||||
struct chacha20_ctx ctx;
|
||||
struct chacha20_state state;
|
||||
size_t l0, l1;
|
||||
int rc;
|
||||
|
||||
|
@ -229,9 +229,9 @@ xchacha20_anycrypt(uint8_t *out, size_t *outlen, void *initparams,
|
|||
return 1;
|
||||
}
|
||||
|
||||
rc = xchacha20_anycrypt_init(&ctx, initparams) &&
|
||||
chacha20_anycrypt_update(&ctx, out, &l0, in, inlen) &&
|
||||
chacha20_anycrypt_final(&ctx, out + l0, &l1);
|
||||
rc = xchacha20_anycrypt_init(&state, initparams) &&
|
||||
chacha20_anycrypt_update(&state, out, &l0, in, inlen) &&
|
||||
chacha20_anycrypt_final(&state, out + l0, &l1);
|
||||
|
||||
if (rc)
|
||||
*outlen = l0 + l1;
|
||||
|
@ -251,7 +251,7 @@ static struct lc_cipher_impl chacha20_impl = {
|
|||
.decrypt_final = &chacha20_anycrypt_final,
|
||||
.decrypt = &chacha20_anycrypt,
|
||||
|
||||
.argsz = sizeof(struct chacha20_ctx),
|
||||
.argsz = sizeof(struct chacha20_state),
|
||||
.blocklen = LC_CHACHA20_BLOCKLEN,
|
||||
};
|
||||
|
||||
|
@ -266,7 +266,7 @@ static struct lc_cipher_impl xchacha20_impl = {
|
|||
.decrypt_final = &chacha20_anycrypt_final,
|
||||
.decrypt = &xchacha20_anycrypt,
|
||||
|
||||
.argsz = sizeof(struct chacha20_ctx),
|
||||
.argsz = sizeof(struct chacha20_state),
|
||||
.blocklen = LC_XCHACHA20_BLOCKLEN,
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue