Coverage Report

Created: 2025-07-01 07:00

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