#include "includes.h"
#ifdef WITH_OPENSSL
#include "openbsd-compat/openssl-compat.h"
#endif
#if defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20)
#include <sys/types.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <openssl/evp.h>
#include "log.h"
#include "sshbuf.h"
#include "ssherr.h"
#include "cipher-chachapoly.h"
struct chachapoly_ctx {
EVP_CIPHER_CTX *main_evp, *header_evp;
};
struct chachapoly_ctx *
chachapoly_new(const u_char *key, u_int keylen)
{
struct chachapoly_ctx *ctx;
if (keylen != (32 + 32))
return NULL;
if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
return NULL;
if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL ||
(ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL)
goto fail;
if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1))
goto fail;
if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1))
goto fail;
if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16)
goto fail;
return ctx;
fail:
chachapoly_free(ctx);
return NULL;
}
void
chachapoly_free(struct chachapoly_ctx *cpctx)
{
if (cpctx == NULL)
return;
EVP_CIPHER_CTX_free(cpctx->main_evp);
EVP_CIPHER_CTX_free(cpctx->header_evp);
freezero(cpctx, sizeof(*cpctx));
}
int
chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
{
u_char seqbuf[16];
int r = SSH_ERR_INTERNAL_ERROR;
u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
memset(seqbuf, 0, sizeof(seqbuf));
POKE_U64(seqbuf + 8, seqnr);
memset(poly_key, 0, sizeof(poly_key));
if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
EVP_Cipher(ctx->main_evp, poly_key,
poly_key, sizeof(poly_key)) < 0) {
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if (!do_encrypt) {
const u_char *tag = src + aadlen + len;
poly1305_auth(expected_tag, src, aadlen + len, poly_key);
if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
r = SSH_ERR_MAC_INVALID;
goto out;
}
}
if (aadlen) {
if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 1) ||
EVP_Cipher(ctx->header_evp, dest, src, aadlen) < 0) {
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
}
seqbuf[0] = 1;
if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
EVP_Cipher(ctx->main_evp, dest + aadlen, src + aadlen, len) < 0) {
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if (do_encrypt) {
poly1305_auth(dest + aadlen + len, dest, aadlen + len,
poly_key);
}
r = 0;
out:
explicit_bzero(expected_tag, sizeof(expected_tag));
explicit_bzero(seqbuf, sizeof(seqbuf));
explicit_bzero(poly_key, sizeof(poly_key));
return r;
}
int
chachapoly_get_length(struct chachapoly_ctx *ctx,
u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
{
u_char buf[4], seqbuf[16];
if (len < 4)
return SSH_ERR_MESSAGE_INCOMPLETE;
memset(seqbuf, 0, sizeof(seqbuf));
POKE_U64(seqbuf + 8, seqnr);
if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 0))
return SSH_ERR_LIBCRYPTO_ERROR;
if (EVP_Cipher(ctx->header_evp, buf, (u_char *)cp, sizeof(buf)) < 0)
return SSH_ERR_LIBCRYPTO_ERROR;
*plenp = PEEK_U32(buf);
return 0;
}
#endif