Coverage Report

Created: 2026-03-21 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssh/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
struct chachapoly_ctx {
36
  EVP_CIPHER_CTX *main_evp, *header_evp;
37
};
38
39
struct chachapoly_ctx *
40
chachapoly_new(const u_char *key, u_int keylen)
41
0
{
42
0
  struct chachapoly_ctx *ctx;
43
44
0
  if (keylen != (32 + 32)) /* 2 x 256 bit keys */
45
0
    return NULL;
46
0
  if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
47
0
    return NULL;
48
0
  if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL ||
49
0
      (ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL)
50
0
    goto fail;
51
0
  if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1))
52
0
    goto fail;
53
0
  if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1))
54
0
    goto fail;
55
0
  if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16)
56
0
    goto fail;
57
0
  return ctx;
58
0
 fail:
59
0
  chachapoly_free(ctx);
60
0
  return NULL;
61
0
}
62
63
void
64
chachapoly_free(struct chachapoly_ctx *cpctx)
65
0
{
66
0
  if (cpctx == NULL)
67
0
    return;
68
0
  EVP_CIPHER_CTX_free(cpctx->main_evp);
69
0
  EVP_CIPHER_CTX_free(cpctx->header_evp);
70
0
  freezero(cpctx, sizeof(*cpctx));
71
0
}
72
73
/*
74
 * chachapoly_crypt() operates as following:
75
 * En/decrypt with header key 'aadlen' bytes from 'src', storing result
76
 * to 'dest'. The ciphertext here is treated as additional authenticated
77
 * data for MAC calculation.
78
 * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
79
 * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
80
 * tag. This tag is written on encryption and verified on decryption.
81
 */
82
int
83
chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
84
    const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
85
0
{
86
0
  u_char seqbuf[16]; /* layout: u64 counter || u64 seqno */
87
0
  int r = SSH_ERR_INTERNAL_ERROR;
88
0
  u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
89
90
  /*
91
   * Run ChaCha20 once to generate the Poly1305 key. The IV is the
92
   * packet sequence number.
93
   */
94
0
  memset(seqbuf, 0, sizeof(seqbuf));
95
0
  POKE_U64(seqbuf + 8, seqnr);
96
0
  memset(poly_key, 0, sizeof(poly_key));
97
0
  if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
98
0
      EVP_Cipher(ctx->main_evp, poly_key,
99
0
      poly_key, sizeof(poly_key)) < 0) {
100
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
101
0
    goto out;
102
0
  }
103
104
  /* If decrypting, check tag before anything else */
105
0
  if (!do_encrypt) {
106
0
    const u_char *tag = src + aadlen + len;
107
108
0
    poly1305_auth(expected_tag, src, aadlen + len, poly_key);
109
0
    if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
110
0
      r = SSH_ERR_MAC_INVALID;
111
0
      goto out;
112
0
    }
113
0
  }
114
115
  /* Crypt additional data */
116
0
  if (aadlen) {
117
0
    if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 1) ||
118
0
        EVP_Cipher(ctx->header_evp, dest, src, aadlen) < 0) {
119
0
      r = SSH_ERR_LIBCRYPTO_ERROR;
120
0
      goto out;
121
0
    }
122
0
  }
123
124
  /* Set Chacha's block counter to 1 */
125
0
  seqbuf[0] = 1;
126
0
  if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
127
0
      EVP_Cipher(ctx->main_evp, dest + aadlen, src + aadlen, len) < 0) {
128
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
129
0
    goto out;
130
0
  }
131
132
  /* If encrypting, calculate and append tag */
133
0
  if (do_encrypt) {
134
0
    poly1305_auth(dest + aadlen + len, dest, aadlen + len,
135
0
        poly_key);
136
0
  }
137
0
  r = 0;
138
0
 out:
139
0
  explicit_bzero(expected_tag, sizeof(expected_tag));
140
0
  explicit_bzero(seqbuf, sizeof(seqbuf));
141
0
  explicit_bzero(poly_key, sizeof(poly_key));
142
0
  return r;
143
0
}
144
145
/* Decrypt and extract the encrypted packet length */
146
int
147
chachapoly_get_length(struct chachapoly_ctx *ctx,
148
    u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
149
0
{
150
0
  u_char buf[4], seqbuf[16];
151
152
0
  if (len < 4)
153
0
    return SSH_ERR_MESSAGE_INCOMPLETE;
154
0
  memset(seqbuf, 0, sizeof(seqbuf));
155
0
  POKE_U64(seqbuf + 8, seqnr);
156
0
  if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 0))
157
0
    return SSH_ERR_LIBCRYPTO_ERROR;
158
0
  if (EVP_Cipher(ctx->header_evp, buf, (u_char *)cp, sizeof(buf)) < 0)
159
0
    return SSH_ERR_LIBCRYPTO_ERROR;
160
0
  *plenp = PEEK_U32(buf);
161
0
  return 0;
162
0
}
163
#endif /* defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20) */