Skip to content

Commit f1b185e

Browse files
authored
Merge pull request #2577 from babarosaM33/master
Remove unnecessary memmove
2 parents f6bc4cc + 7fe555c commit f1b185e

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/aead.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,6 @@ aead_chunk_decrypt(cipher_ctx_t *ctx, uint8_t *p, uint8_t *c, uint8_t *n,
617617

618618
sodium_increment(n, nlen);
619619

620-
if (*clen > chunk_len)
621-
memmove(c, c + chunk_len, *clen - chunk_len);
622-
623620
*clen = *clen - chunk_len;
624621

625622
return CRYPTO_OK;
@@ -671,22 +668,27 @@ aead_decrypt(buffer_t *ciphertext, cipher_ctx_t *cipher_ctx, size_t capacity)
671668
}
672669

673670
size_t plen = 0;
671+
size_t cidx = 0;
674672
while (cipher_ctx->chunk->len > 0) {
675673
size_t chunk_clen = cipher_ctx->chunk->len;
676674
size_t chunk_plen = 0;
677675
err = aead_chunk_decrypt(cipher_ctx,
678676
(uint8_t *)plaintext->data + plen,
679-
(uint8_t *)cipher_ctx->chunk->data,
677+
(uint8_t *)cipher_ctx->chunk->data + cidx,
680678
cipher_ctx->nonce, &chunk_plen, &chunk_clen);
681679
if (err == CRYPTO_ERROR) {
682680
return err;
683681
} else if (err == CRYPTO_NEED_MORE) {
684682
if (plen == 0)
685683
return err;
686-
else
684+
else{
685+
memmove((uint8_t *)cipher_ctx->chunk->data,
686+
(uint8_t *)cipher_ctx->chunk->data + cidx, chunk_clen);
687687
break;
688+
}
688689
}
689690
cipher_ctx->chunk->len = chunk_clen;
691+
cidx += cipher_ctx->cipher->tag_len * 2 + CHUNK_SIZE_LEN + chunk_plen;
690692
plen += chunk_plen;
691693
}
692694
plaintext->len = plen;

0 commit comments

Comments
 (0)