aboutsummaryrefslogtreecommitdiff
path: root/src/libmbedcrypto.c
diff options
context:
space:
mode:
authorAnderson Toshiyuki Sasaki <ansasaki@redhat.com>2020-12-11 19:33:14 +0100
committerAnderson Toshiyuki Sasaki <ansasaki@redhat.com>2021-01-12 12:54:18 +0100
commitd4258d1461d0acdca758f8df30d2f40ea6b7bf16 (patch)
tree182eefa383fca1a9d15a1f3d3df7d41b39d8a767 /src/libmbedcrypto.c
parentc50cfe7fc70e06a388139149a2ebab526d692919 (diff)
downloadlibssh-d4258d1461d0acdca758f8df30d2f40ea6b7bf16.tar.gz
libssh-d4258d1461d0acdca758f8df30d2f40ea6b7bf16.tar.xz
libssh-d4258d1461d0acdca758f8df30d2f40ea6b7bf16.zip
libmbedcrypto: Fix chacha20-poly1305
Previously, the mbed TLS implementation wouldn't be use at all when available, being the internal implementation always used instead. This corrects few bugs and makes the mbed TLS implementation to be used when ChaCha20 and Poly1305 are available. This also makes the constant time comparison to be used when checking the authentication tag. Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Jakub Jelen <jjelen@redhat.com>
Diffstat (limited to 'src/libmbedcrypto.c')
-rw-r--r--src/libmbedcrypto.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/src/libmbedcrypto.c b/src/libmbedcrypto.c
index ee3fad79..79f8ecc2 100644
--- a/src/libmbedcrypto.c
+++ b/src/libmbedcrypto.c
@@ -994,9 +994,9 @@ chacha20_poly1305_set_iv(struct ssh_cipher_struct *cipher,
return SSH_ERROR;
}
- ret = mbedtls_chacha20_starts(&ctx->header_ctx, seqbuf, 0);
+ ret = mbedtls_chacha20_starts(&ctx->main_ctx, seqbuf, 0);
if (ret != 0) {
- SSH_LOG(SSH_LOG_WARNING, "mbedtls_chacha20_starts(header_ctx) failed");
+ SSH_LOG(SSH_LOG_WARNING, "mbedtls_chacha20_starts(main_ctx) failed");
return SSH_ERROR;
}
@@ -1126,7 +1126,7 @@ chacha20_poly1305_aead_decrypt(struct ssh_cipher_struct *cipher,
#endif /* DEBUG_CRYPTO */
/* Verify the calculated MAC matches the attached MAC */
- cmp = memcmp(tag, mac, POLY1305_TAGLEN);
+ cmp = secure_memcmp(tag, mac, POLY1305_TAGLEN);
if (cmp != 0) {
/* mac error */
SSH_LOG(SSH_LOG_PACKET, "poly1305 verify error");
@@ -1187,7 +1187,7 @@ chacha20_poly1305_aead_encrypt(struct ssh_cipher_struct *cipher,
/* We already did encrypt one block so the counter should be in the correct position */
ret = mbedtls_chacha20_update(&ctx->main_ctx, len - sizeof(uint32_t),
in_packet->payload, out_packet->payload);
- if (ret != 1) {
+ if (ret != 0) {
SSH_LOG(SSH_LOG_WARNING, "mbedtls_chacha20_update failed");
return;
}
@@ -1411,7 +1411,7 @@ int ssh_crypto_init(void)
mbedtls_ctr_drbg_free(&ssh_mbedtls_ctr_drbg);
}
-#if defined(MBEDTLS_CHACHA20_C) && defined(MBEDTLS_POLY1305_C)
+#if !(defined(MBEDTLS_CHACHA20_C) && defined(MBEDTLS_POLY1305_C))
for (i = 0; ssh_ciphertab[i].name != NULL; i++) {
int cmp;