Skip to content

Commit

Permalink
Fix incremental decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
jedisct1 committed Aug 22, 2023
1 parent 1ce38ab commit 9738e77
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 93 deletions.
43 changes: 21 additions & 22 deletions src/aegis128l/aegis128l_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,37 +374,36 @@ state_decrypt_detached_update(aegis128l_state *st_, uint8_t *m, size_t mlen_max,
CRYPTO_ALIGN(RATE) uint8_t dst[RATE];
size_t i = 0;
size_t left;
const size_t mlen = clen;

*written = 0;
st->mlen += mlen;
st->mlen += clen;

if (st->pos != 0) {
const size_t available = (sizeof st->buf) - st->pos;
const size_t n = clen < available ? clen : available;

if (n != 0) {
memcpy(st->buf + st->pos, m + i, n);
memcpy(st->buf + st->pos, c, n);
c += n;
clen -= n;
st->pos += n;
}
if (st->pos == (sizeof st->buf)) {
if (m != NULL) {
if (mlen_max < RATE) {
errno = ERANGE;
return -1;
}
mlen_max -= RATE;
aegis128l_dec(m, st->buf, st->state);
} else {
aegis128l_dec(dst, st->buf, st->state);
if (st->pos < (sizeof st->buf)) {
return 0;
}
st->pos = 0;
if (m != NULL) {
if (mlen_max < RATE) {
errno = ERANGE;
return -1;
}
*written += RATE;
c += RATE;
st->pos = 0;
mlen_max -= RATE;
aegis128l_dec(m, st->buf, st->state);
m += RATE;
} else {
return 0;
aegis128l_dec(dst, st->buf, st->state);
}
*written += RATE;
}
if (m != NULL) {
if (mlen_max < (clen % RATE)) {
Expand All @@ -420,7 +419,7 @@ state_decrypt_detached_update(aegis128l_state *st_, uint8_t *m, size_t mlen_max,
}
}
*written += i;
left = mlen % RATE;
left = clen % RATE;
if (left) {
memcpy(st->buf, c + i, left);
st->pos = left;
Expand All @@ -439,12 +438,12 @@ state_decrypt_detached_final(aegis128l_state *st_, uint8_t *m, size_t mlen_max,
int ret;

*written = 0;
if (mlen_max < st->pos) {
errno = ERANGE;
return -1;
}
if (st->pos != 0) {
if (m != NULL) {
if (mlen_max < st->pos) {
errno = ERANGE;
return -1;
}
aegis128l_declast(m, st->buf, st->pos, st->state);
} else {
aegis128l_declast(dst, st->buf, st->pos, st->state);
Expand Down
44 changes: 22 additions & 22 deletions src/aegis128x2/aegis128x2_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,38 +404,38 @@ state_decrypt_detached_update(aegis128x2_state *st_, uint8_t *m, size_t mlen_max
CRYPTO_ALIGN(RATE) uint8_t dst[RATE];
size_t i = 0;
size_t left;
const size_t mlen = clen;

*written = 0;
st->mlen += mlen;
st->mlen += clen;

if (st->pos != 0) {
const size_t available = (sizeof st->buf) - st->pos;
const size_t n = clen < available ? clen : available;

if (n != 0) {
memcpy(st->buf + st->pos, m + i, n);
memcpy(st->buf + st->pos, c, n);
c += n;
clen -= n;
st->pos += n;
}
if (st->pos == (sizeof st->buf)) {
if (m != NULL) {
if (mlen_max < RATE) {
errno = ERANGE;
return -1;
}
mlen_max -= RATE;
aegis128x2_dec(m, st->buf, st->state);
} else {
aegis128x2_dec(dst, st->buf, st->state);
if (st->pos < (sizeof st->buf)) {
return 0;
}
st->pos = 0;
if (m != NULL) {
if (mlen_max < RATE) {
errno = ERANGE;
return -1;
}
*written += RATE;
c += RATE;
st->pos = 0;
mlen_max -= RATE;
aegis128x2_dec(m, st->buf, st->state);
m += RATE;
} else {
return 0;
aegis128x2_dec(dst, st->buf, st->state);
}
*written += RATE;
}

if (m != NULL) {
if (mlen_max < (clen % RATE)) {
errno = ERANGE;
Expand All @@ -450,7 +450,7 @@ state_decrypt_detached_update(aegis128x2_state *st_, uint8_t *m, size_t mlen_max
}
}
*written += i;
left = mlen % RATE;
left = clen % RATE;
if (left) {
memcpy(st->buf, c + i, left);
st->pos = left;
Expand All @@ -469,12 +469,12 @@ state_decrypt_detached_final(aegis128x2_state *st_, uint8_t *m, size_t mlen_max,
int ret;

*written = 0;
if (mlen_max < st->pos) {
errno = ERANGE;
return -1;
}
if (st->pos != 0) {
if (m != NULL) {
if (mlen_max < st->pos) {
errno = ERANGE;
return -1;
}
aegis128x2_declast(m, st->buf, st->pos, st->state);
} else {
aegis128x2_declast(dst, st->buf, st->pos, st->state);
Expand Down
44 changes: 22 additions & 22 deletions src/aegis128x4/aegis128x4_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,38 +417,38 @@ state_decrypt_detached_update(aegis128x4_state *st_, uint8_t *m, size_t mlen_max
CRYPTO_ALIGN(RATE) uint8_t dst[RATE];
size_t i = 0;
size_t left;
const size_t mlen = clen;

*written = 0;
st->mlen += mlen;
st->mlen += clen;

if (st->pos != 0) {
const size_t available = (sizeof st->buf) - st->pos;
const size_t n = clen < available ? clen : available;

if (n != 0) {
memcpy(st->buf + st->pos, m + i, n);
memcpy(st->buf + st->pos, c, n);
c += n;
clen -= n;
st->pos += n;
}
if (st->pos == (sizeof st->buf)) {
if (m != NULL) {
if (mlen_max < RATE) {
errno = ERANGE;
return -1;
}
mlen_max -= RATE;
aegis128x4_dec(m, st->buf, st->state);
} else {
aegis128x4_dec(dst, st->buf, st->state);
if (st->pos < (sizeof st->buf)) {
return 0;
}
st->pos = 0;
if (m != NULL) {
if (mlen_max < RATE) {
errno = ERANGE;
return -1;
}
*written += RATE;
c += RATE;
st->pos = 0;
mlen_max -= RATE;
aegis128x4_dec(m, st->buf, st->state);
m += RATE;
} else {
return 0;
aegis128x4_dec(dst, st->buf, st->state);
}
*written += RATE;
}

if (m != NULL) {
if (mlen_max < (clen % RATE)) {
errno = ERANGE;
Expand All @@ -463,7 +463,7 @@ state_decrypt_detached_update(aegis128x4_state *st_, uint8_t *m, size_t mlen_max
}
}
*written += i;
left = mlen % RATE;
left = clen % RATE;
if (left) {
memcpy(st->buf, c + i, left);
st->pos = left;
Expand All @@ -482,12 +482,12 @@ state_decrypt_detached_final(aegis128x4_state *st_, uint8_t *m, size_t mlen_max,
int ret;

*written = 0;
if (mlen_max < st->pos) {
errno = ERANGE;
return -1;
}
if (st->pos != 0) {
if (m != NULL) {
if (mlen_max < st->pos) {
errno = ERANGE;
return -1;
}
aegis128x4_declast(m, st->buf, st->pos, st->state);
} else {
aegis128x4_declast(dst, st->buf, st->pos, st->state);
Expand Down
50 changes: 23 additions & 27 deletions src/aegis256/aegis256_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,42 +359,38 @@ state_decrypt_detached_update(aegis256_state *st_, uint8_t *m, size_t mlen_max,
CRYPTO_ALIGN(RATE) uint8_t dst[RATE];
size_t i = 0;
size_t left;
const size_t mlen = clen;

*written = 0;
if (mlen_max < (clen % RATE)) {
errno = ERANGE;
return -1;
}
st->mlen += mlen;
st->mlen += clen;

if (st->pos != 0) {
const size_t available = (sizeof st->buf) - st->pos;
const size_t n = clen < available ? clen : available;

if (n != 0) {
memcpy(st->buf + st->pos, m + i, n);
memcpy(st->buf + st->pos, c, n);
c += n;
clen -= n;
st->pos += n;
}
if (st->pos == (sizeof st->buf)) {
if (m != NULL) {
if (mlen_max < RATE) {
errno = ERANGE;
return -1;
}
mlen_max -= RATE;
aegis256_dec(m, st->buf, st->state);
} else {
aegis256_dec(dst, st->buf, st->state);
if (st->pos < (sizeof st->buf)) {
return 0;
}
st->pos = 0;
if (m != NULL) {
if (mlen_max < RATE) {
errno = ERANGE;
return -1;
}
*written += RATE;
c += RATE;
st->pos = 0;
mlen_max -= RATE;
aegis256_dec(m, st->buf, st->state);
m += RATE;
} else {
return 0;
aegis256_dec(dst, st->buf, st->state);
}
*written += RATE;
}

if (m != NULL) {
if (mlen_max < (clen % RATE)) {
errno = ERANGE;
Expand All @@ -409,7 +405,7 @@ state_decrypt_detached_update(aegis256_state *st_, uint8_t *m, size_t mlen_max,
}
}
*written += i;
left = mlen % RATE;
left = clen % RATE;
if (left) {
memcpy(st->buf, c + i, left);
st->pos = left;
Expand All @@ -421,19 +417,19 @@ static int
state_decrypt_detached_final(aegis256_state *st_, uint8_t *m, size_t mlen_max, size_t *written,
const uint8_t *mac, size_t maclen)
{
CRYPTO_ALIGN(RATE) uint8_t computed_mac[32];
CRYPTO_ALIGN(16) uint8_t computed_mac[32];
CRYPTO_ALIGN(RATE) uint8_t dst[RATE];
_aegis256_state *const st =
(_aegis256_state *) ((((uintptr_t) &st_->opaque) + (RATE - 1)) & ~(uintptr_t) (RATE - 1));
int ret;

*written = 0;
if (mlen_max < st->pos) {
errno = ERANGE;
return -1;
}
if (st->pos != 0) {
if (m != NULL) {
if (mlen_max < st->pos) {
errno = ERANGE;
return -1;
}
aegis256_declast(m, st->buf, st->pos, st->state);
} else {
aegis256_declast(dst, st->buf, st->pos, st->state);
Expand Down
Loading

0 comments on commit 9738e77

Please sign in to comment.