Coverage Report

Created: 2026-07-15 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hpn-ssh/cipher-chachapoly-libcrypto.c
Line
Count
Source
1
/* $OpenBSD: cipher-chachapoly-libcrypto.c,v 1.3 2026/02/14 00:18:34 jsg Exp $ */
2
/*
3
 * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
#include "includes.h"
19
#ifdef WITH_OPENSSL
20
#include "openbsd-compat/openssl-compat.h"
21
#endif
22
23
#if defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20)
24
25
#include <sys/types.h>
26
#include <string.h>
27
#include <stdio.h>  /* needed for misc.h */
28
29
#include <openssl/evp.h>
30
31
#include "sshbuf.h"
32
#include "ssherr.h"
33
#include "cipher-chachapoly.h"
34
35
36
/* using the EVP_MAC interface for poly1305 is significantly
37
 * faster than the version bundled with OpenSSH. However,
38
 * this interface is only available in OpenSSL 3.0+
39
 * -cjr 10/21/2022 */
40
struct chachapoly_ctx {
41
  EVP_CIPHER_CTX *main_evp, *header_evp;
42
#ifdef OPENSSL_HAVE_POLY_EVP
43
  EVP_MAC_CTX    *poly_ctx;
44
#else
45
  char           *poly_ctx;
46
#endif
47
};
48
49
struct chachapoly_ctx *
50
chachapoly_new(const u_char *key, u_int keylen)
51
0
{
52
0
  struct chachapoly_ctx *ctx;
53
54
0
  if (keylen != (32 + 32)) /* 2 x 256 bit keys */
55
0
    return NULL;
56
0
  if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
57
0
    return NULL;
58
0
  if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL ||
59
0
      (ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL)
60
0
    goto fail;
61
0
  if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1))
62
0
    goto fail;
63
0
  if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1))
64
0
    goto fail;
65
0
  if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16)
66
0
    goto fail;
67
#ifdef OPENSSL_HAVE_POLY_EVP
68
  EVP_MAC *mac = NULL;
69
  if ((mac = EVP_MAC_fetch(NULL, "POLY1305", NULL)) == NULL)
70
    goto fail;
71
  if ((ctx->poly_ctx = EVP_MAC_CTX_new(mac)) == NULL)
72
    goto fail;
73
#else
74
0
  ctx->poly_ctx = NULL;
75
0
#endif
76
0
  return ctx;
77
0
 fail:
78
0
  chachapoly_free(ctx);
79
0
  return NULL;
80
0
}
81
82
void
83
chachapoly_free(struct chachapoly_ctx *cpctx)
84
0
{
85
0
  if (cpctx == NULL)
86
0
    return;
87
0
  EVP_CIPHER_CTX_free(cpctx->main_evp);
88
0
  EVP_CIPHER_CTX_free(cpctx->header_evp);
89
#ifdef OPENSSL_HAVE_POLY_EVP
90
  EVP_MAC_CTX_free(cpctx->poly_ctx);
91
#endif
92
0
  freezero(cpctx, sizeof(*cpctx));
93
0
}
94
95
/*
96
 * chachapoly_crypt() operates as following:
97
 * En/decrypt with header key 'aadlen' bytes from 'src', storing result
98
 * to 'dest'. The ciphertext here is treated as additional authenticated
99
 * data for MAC calculation.
100
 * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
101
 * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
102
 * tag. This tag is written on encryption and verified on decryption.
103
 */
104
int
105
chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
106
    const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
107
0
{
108
0
  u_char seqbuf[16]; /* layout: u64 counter || u64 seqno */
109
0
  int r = SSH_ERR_INTERNAL_ERROR;
110
0
  u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
111
112
  /*
113
   * Run ChaCha20 once to generate the Poly1305 key. The IV is the
114
   * packet sequence number.
115
   */
116
0
  memset(seqbuf, 0, sizeof(seqbuf));
117
0
  POKE_U64(seqbuf + 8, seqnr);
118
0
  memset(poly_key, 0, sizeof(poly_key));
119
0
  if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
120
0
      EVP_Cipher(ctx->main_evp, poly_key,
121
0
      poly_key, sizeof(poly_key)) < 0) {
122
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
123
0
    goto out;
124
0
  }
125
126
  /* If decrypting, check tag before anything else */
127
0
  if (!do_encrypt) {
128
0
    const u_char *tag = src + aadlen + len;
129
0
    poly1305_auth(ctx->poly_ctx, expected_tag, src, aadlen + len, poly_key);
130
0
    if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
131
0
      r = SSH_ERR_MAC_INVALID;
132
0
      goto out;
133
0
    }
134
0
  }
135
136
  /* Crypt additional data */
137
0
  if (aadlen) {
138
0
    if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 1) ||
139
0
        EVP_Cipher(ctx->header_evp, dest, src, aadlen) < 0) {
140
0
      r = SSH_ERR_LIBCRYPTO_ERROR;
141
0
      goto out;
142
0
    }
143
0
  }
144
145
  /* Set Chacha's block counter to 1 */
146
0
  seqbuf[0] = 1;
147
0
  if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
148
0
      EVP_Cipher(ctx->main_evp, dest + aadlen, src + aadlen, len) < 0) {
149
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
150
0
    goto out;
151
0
  }
152
153
  /* If encrypting, calculate and append tag */
154
0
  if (do_encrypt) {
155
0
    poly1305_auth(ctx->poly_ctx, dest + aadlen + len, dest, aadlen + len,
156
0
      poly_key);
157
0
  }
158
0
  r = 0;
159
0
 out:
160
0
  explicit_bzero(expected_tag, sizeof(expected_tag));
161
0
  explicit_bzero(seqbuf, sizeof(seqbuf));
162
0
  explicit_bzero(poly_key, sizeof(poly_key));
163
0
  return r;
164
0
}
165
166
/* Decrypt and extract the encrypted packet length */
167
int
168
chachapoly_get_length(struct chachapoly_ctx *ctx,
169
    u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
170
0
{
171
0
  u_char buf[4], seqbuf[16];
172
173
0
  if (len < 4)
174
0
    return SSH_ERR_MESSAGE_INCOMPLETE;
175
0
  memset(seqbuf, 0, sizeof(seqbuf));
176
0
  POKE_U64(seqbuf + 8, seqnr);
177
0
  if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 0))
178
0
    return SSH_ERR_LIBCRYPTO_ERROR;
179
0
  if (EVP_Cipher(ctx->header_evp, buf, (u_char *)cp, sizeof(buf)) < 0)
180
0
    return SSH_ERR_LIBCRYPTO_ERROR;
181
0
  *plenp = PEEK_U32(buf);
182
0
  return 0;
183
0
}
184
#endif /* defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20) */