Coverage Report

Created: 2023-06-07 07:10

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