Coverage Report

Created: 2025-10-10 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssh/cipher.c
Line
Count
Source
1
/* $OpenBSD: cipher.c,v 1.125 2025/09/02 11:08:34 djm Exp $ */
2
/*
3
 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4
 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5
 *                    All rights reserved
6
 *
7
 * As far as I am concerned, the code I have written for this software
8
 * can be used freely for any purpose.  Any derived versions of this
9
 * software must be clearly marked as such, and if the derived work is
10
 * incompatible with the protocol description in the RFC file, it must be
11
 * called by a name other than "ssh" or "Secure Shell".
12
 *
13
 *
14
 * Copyright (c) 1999 Niels Provos.  All rights reserved.
15
 * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
16
 *
17
 * Redistribution and use in source and binary forms, with or without
18
 * modification, are permitted provided that the following conditions
19
 * are met:
20
 * 1. Redistributions of source code must retain the above copyright
21
 *    notice, this list of conditions and the following disclaimer.
22
 * 2. Redistributions in binary form must reproduce the above copyright
23
 *    notice, this list of conditions and the following disclaimer in the
24
 *    documentation and/or other materials provided with the distribution.
25
 *
26
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
 */
37
38
#include "includes.h"
39
40
#include <sys/types.h>
41
42
#include <string.h>
43
#include <stdarg.h>
44
#include <stdio.h>
45
46
#include "cipher.h"
47
#include "misc.h"
48
#include "sshbuf.h"
49
#include "ssherr.h"
50
#include "digest.h"
51
52
#include "openbsd-compat/openssl-compat.h"
53
54
#ifndef WITH_OPENSSL
55
#define EVP_CIPHER_CTX void
56
#endif
57
58
struct sshcipher_ctx {
59
  int plaintext;
60
  int encrypt;
61
  EVP_CIPHER_CTX *evp;
62
  struct chachapoly_ctx *cp_ctx;
63
  struct aesctr_ctx ac_ctx; /* XXX union with evp? */
64
  const struct sshcipher *cipher;
65
};
66
67
struct sshcipher {
68
  char  *name;
69
  u_int block_size;
70
  u_int key_len;
71
  u_int iv_len;   /* defaults to block_size */
72
  u_int auth_len;
73
  u_int flags;
74
0
#define CFLAG_CBC   (1<<0)
75
0
#define CFLAG_CHACHAPOLY  (1<<1)
76
0
#define CFLAG_AESCTR    (1<<2)
77
0
#define CFLAG_NONE    (1<<3)
78
0
#define CFLAG_INTERNAL 0
79
#ifdef WITH_OPENSSL
80
  const EVP_CIPHER  *(*evptype)(void);
81
#else
82
  void  *ignored;
83
#endif
84
};
85
86
static const struct sshcipher ciphers[] = {
87
#ifdef WITH_OPENSSL
88
#ifndef OPENSSL_NO_DES
89
  { "3des-cbc",   8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
90
#endif
91
  { "aes128-cbc",   16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
92
  { "aes192-cbc",   16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
93
  { "aes256-cbc",   16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
94
  { "aes128-ctr",   16, 16, 0, 0, 0, EVP_aes_128_ctr },
95
  { "aes192-ctr",   16, 24, 0, 0, 0, EVP_aes_192_ctr },
96
  { "aes256-ctr",   16, 32, 0, 0, 0, EVP_aes_256_ctr },
97
  { "aes128-gcm@openssh.com",
98
        16, 16, 12, 16, 0, EVP_aes_128_gcm },
99
  { "aes256-gcm@openssh.com",
100
        16, 32, 12, 16, 0, EVP_aes_256_gcm },
101
#else
102
  { "aes128-ctr",   16, 16, 0, 0, CFLAG_AESCTR, NULL },
103
  { "aes192-ctr",   16, 24, 0, 0, CFLAG_AESCTR, NULL },
104
  { "aes256-ctr",   16, 32, 0, 0, CFLAG_AESCTR, NULL },
105
#endif
106
  { "chacha20-poly1305@openssh.com",
107
        8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
108
  { "none",   8, 0, 0, 0, CFLAG_NONE, NULL },
109
110
  { NULL,     0, 0, 0, 0, 0, NULL }
111
};
112
113
/*--*/
114
115
/* Returns a comma-separated list of supported ciphers. */
116
char *
117
cipher_alg_list(char sep, int auth_only)
118
0
{
119
0
  char *ret = NULL;
120
0
  const struct sshcipher *c;
121
0
  char sep_str[2] = {sep, '\0'};
122
123
0
  for (c = ciphers; c->name != NULL; c++) {
124
0
    if ((c->flags & CFLAG_INTERNAL) != 0)
125
0
      continue;
126
0
    if (auth_only && c->auth_len == 0)
127
0
      continue;
128
0
    xextendf(&ret, sep_str, "%s", c->name);
129
0
  }
130
0
  return ret;
131
0
}
132
133
const char *
134
compression_alg_list(int compression)
135
0
{
136
0
#ifdef WITH_ZLIB
137
0
  return compression ? "zlib@openssh.com,none" :
138
0
      "none,zlib@openssh.com";
139
#else
140
  return "none";
141
#endif
142
0
}
143
144
u_int
145
cipher_blocksize(const struct sshcipher *c)
146
0
{
147
0
  return (c->block_size);
148
0
}
149
150
u_int
151
cipher_keylen(const struct sshcipher *c)
152
0
{
153
0
  return (c->key_len);
154
0
}
155
156
u_int
157
cipher_seclen(const struct sshcipher *c)
158
0
{
159
0
  if (strcmp("3des-cbc", c->name) == 0)
160
0
    return 14;
161
0
  return cipher_keylen(c);
162
0
}
163
164
u_int
165
cipher_authlen(const struct sshcipher *c)
166
0
{
167
0
  return (c->auth_len);
168
0
}
169
170
u_int
171
cipher_ivlen(const struct sshcipher *c)
172
0
{
173
  /*
174
   * Default is cipher block size, except for chacha20+poly1305 that
175
   * needs no IV. XXX make iv_len == -1 default?
176
   */
177
0
  return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
178
0
      c->iv_len : c->block_size;
179
0
}
180
181
u_int
182
cipher_is_cbc(const struct sshcipher *c)
183
0
{
184
0
  return (c->flags & CFLAG_CBC) != 0;
185
0
}
186
187
u_int
188
cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
189
0
{
190
0
  return cc->plaintext;
191
0
}
192
193
const struct sshcipher *
194
cipher_by_name(const char *name)
195
0
{
196
0
  const struct sshcipher *c;
197
0
  for (c = ciphers; c->name != NULL; c++)
198
0
    if (strcmp(c->name, name) == 0)
199
0
      return c;
200
0
  return NULL;
201
0
}
202
203
0
#define CIPHER_SEP  ","
204
int
205
ciphers_valid(const char *names)
206
0
{
207
0
  const struct sshcipher *c;
208
0
  char *cipher_list, *cp;
209
0
  char *p;
210
211
0
  if (names == NULL || strcmp(names, "") == 0)
212
0
    return 0;
213
0
  if ((cipher_list = cp = strdup(names)) == NULL)
214
0
    return 0;
215
0
  for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
216
0
      (p = strsep(&cp, CIPHER_SEP))) {
217
0
    c = cipher_by_name(p);
218
0
    if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
219
0
      free(cipher_list);
220
0
      return 0;
221
0
    }
222
0
  }
223
0
  free(cipher_list);
224
0
  return 1;
225
0
}
226
227
const char *
228
cipher_warning_message(const struct sshcipher_ctx *cc)
229
0
{
230
0
  if (cc == NULL || cc->cipher == NULL)
231
0
    return NULL;
232
  /* XXX repurpose for CBC warning */
233
0
  return NULL;
234
0
}
235
236
int
237
cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
238
    const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
239
    int do_encrypt)
240
0
{
241
0
  struct sshcipher_ctx *cc = NULL;
242
0
  int ret = SSH_ERR_INTERNAL_ERROR;
243
0
#ifdef WITH_OPENSSL
244
0
  const EVP_CIPHER *type;
245
0
  int klen;
246
0
#endif
247
248
0
  *ccp = NULL;
249
0
  if ((cc = calloc(1, sizeof(*cc))) == NULL)
250
0
    return SSH_ERR_ALLOC_FAIL;
251
252
0
  cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
253
0
  cc->encrypt = do_encrypt;
254
255
0
  if (keylen < cipher->key_len ||
256
0
      (iv != NULL && ivlen < cipher_ivlen(cipher))) {
257
0
    ret = SSH_ERR_INVALID_ARGUMENT;
258
0
    goto out;
259
0
  }
260
261
0
  cc->cipher = cipher;
262
0
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
263
0
    cc->cp_ctx = chachapoly_new(key, keylen);
264
0
    ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT;
265
0
    goto out;
266
0
  }
267
0
  if ((cc->cipher->flags & CFLAG_NONE) != 0) {
268
0
    ret = 0;
269
0
    goto out;
270
0
  }
271
#ifndef WITH_OPENSSL
272
  if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
273
    aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
274
    aesctr_ivsetup(&cc->ac_ctx, iv);
275
    ret = 0;
276
    goto out;
277
  }
278
  ret = SSH_ERR_INVALID_ARGUMENT;
279
  goto out;
280
#else /* WITH_OPENSSL */
281
0
  type = (*cipher->evptype)();
282
0
  if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
283
0
    ret = SSH_ERR_ALLOC_FAIL;
284
0
    goto out;
285
0
  }
286
0
  if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
287
0
      (do_encrypt == CIPHER_ENCRYPT)) == 0) {
288
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
289
0
    goto out;
290
0
  }
291
0
  if (cipher_authlen(cipher) &&
292
0
      EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
293
0
      -1, (u_char *)iv) <= 0) {
294
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
295
0
    goto out;
296
0
  }
297
0
  klen = EVP_CIPHER_CTX_key_length(cc->evp);
298
0
  if (klen > 0 && keylen != (u_int)klen) {
299
0
    if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
300
0
      ret = SSH_ERR_LIBCRYPTO_ERROR;
301
0
      goto out;
302
0
    }
303
0
  }
304
0
  if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
305
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
306
0
    goto out;
307
0
  }
308
0
  ret = 0;
309
0
#endif /* WITH_OPENSSL */
310
0
 out:
311
0
  if (ret == 0) {
312
    /* success */
313
0
    *ccp = cc;
314
0
  } else {
315
0
    if (cc != NULL) {
316
0
#ifdef WITH_OPENSSL
317
0
      EVP_CIPHER_CTX_free(cc->evp);
318
0
#endif /* WITH_OPENSSL */
319
0
      freezero(cc, sizeof(*cc));
320
0
    }
321
0
  }
322
0
  return ret;
323
0
}
324
325
/*
326
 * cipher_crypt() operates as following:
327
 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
328
 * These bytes are treated as additional authenticated data for
329
 * authenticated encryption modes.
330
 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
331
 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
332
 * This tag is written on encryption and verified on decryption.
333
 * Both 'aadlen' and 'authlen' can be set to 0.
334
 */
335
int
336
cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
337
   const u_char *src, u_int len, u_int aadlen, u_int authlen)
338
0
{
339
0
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
340
0
    return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src,
341
0
        len, aadlen, authlen, cc->encrypt);
342
0
  }
343
0
  if ((cc->cipher->flags & CFLAG_NONE) != 0) {
344
0
    memcpy(dest, src, aadlen + len);
345
0
    return 0;
346
0
  }
347
#ifndef WITH_OPENSSL
348
  if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
349
    if (aadlen)
350
      memcpy(dest, src, aadlen);
351
    aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
352
        dest + aadlen, len);
353
    return 0;
354
  }
355
  return SSH_ERR_INVALID_ARGUMENT;
356
#else
357
0
  if (authlen) {
358
0
    u_char lastiv[1];
359
360
0
    if (authlen != cipher_authlen(cc->cipher))
361
0
      return SSH_ERR_INVALID_ARGUMENT;
362
    /* increment IV */
363
0
    if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
364
0
        1, lastiv) <= 0)
365
0
      return SSH_ERR_LIBCRYPTO_ERROR;
366
    /* set tag on decryption */
367
0
    if (!cc->encrypt &&
368
0
        EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
369
0
        authlen, (u_char *)src + aadlen + len) <= 0)
370
0
      return SSH_ERR_LIBCRYPTO_ERROR;
371
0
  }
372
0
  if (aadlen) {
373
0
    if (authlen &&
374
0
        EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
375
0
      return SSH_ERR_LIBCRYPTO_ERROR;
376
0
    memcpy(dest, src, aadlen);
377
0
  }
378
0
  if (len % cc->cipher->block_size)
379
0
    return SSH_ERR_INVALID_ARGUMENT;
380
0
  if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
381
0
      len) < 0)
382
0
    return SSH_ERR_LIBCRYPTO_ERROR;
383
0
  if (authlen) {
384
    /* compute tag (on encrypt) or verify tag (on decrypt) */
385
0
    if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
386
0
      return cc->encrypt ?
387
0
          SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
388
0
    if (cc->encrypt &&
389
0
        EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
390
0
        authlen, dest + aadlen + len) <= 0)
391
0
      return SSH_ERR_LIBCRYPTO_ERROR;
392
0
  }
393
0
  return 0;
394
0
#endif
395
0
}
396
397
/* Extract the packet length, including any decryption necessary beforehand */
398
int
399
cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
400
    const u_char *cp, u_int len)
401
0
{
402
0
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
403
0
    return chachapoly_get_length(cc->cp_ctx, plenp, seqnr,
404
0
        cp, len);
405
0
  if (len < 4)
406
0
    return SSH_ERR_MESSAGE_INCOMPLETE;
407
0
  *plenp = PEEK_U32(cp);
408
0
  return 0;
409
0
}
410
411
void
412
cipher_free(struct sshcipher_ctx *cc)
413
0
{
414
0
  if (cc == NULL)
415
0
    return;
416
0
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
417
0
    chachapoly_free(cc->cp_ctx);
418
0
    cc->cp_ctx = NULL;
419
0
  } else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
420
0
    explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
421
0
#ifdef WITH_OPENSSL
422
0
  EVP_CIPHER_CTX_free(cc->evp);
423
0
  cc->evp = NULL;
424
0
#endif
425
0
  freezero(cc, sizeof(*cc));
426
0
}
427
428
int
429
cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
430
0
{
431
0
#ifdef WITH_OPENSSL
432
0
  const struct sshcipher *c = cc->cipher;
433
0
  int evplen;
434
0
#endif
435
436
0
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
437
0
    if (len != 0)
438
0
      return SSH_ERR_INVALID_ARGUMENT;
439
0
    return 0;
440
0
  }
441
0
  if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
442
0
    if (len != sizeof(cc->ac_ctx.ctr))
443
0
      return SSH_ERR_INVALID_ARGUMENT;
444
0
    memcpy(iv, cc->ac_ctx.ctr, len);
445
0
    return 0;
446
0
  }
447
0
  if ((cc->cipher->flags & CFLAG_NONE) != 0)
448
0
    return 0;
449
450
0
#ifdef WITH_OPENSSL
451
0
  evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
452
0
  if (evplen == 0)
453
0
    return 0;
454
0
  else if (evplen < 0)
455
0
    return SSH_ERR_LIBCRYPTO_ERROR;
456
0
  if ((size_t)evplen != len)
457
0
    return SSH_ERR_INVALID_ARGUMENT;
458
0
  if (cipher_authlen(c)) {
459
0
    if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, len,
460
0
        iv) <= 0)
461
0
      return SSH_ERR_LIBCRYPTO_ERROR;
462
0
  } else if (EVP_CIPHER_CTX_get_iv(cc->evp, iv, len) <= 0)
463
0
    return SSH_ERR_LIBCRYPTO_ERROR;
464
0
#endif
465
0
  return 0;
466
0
}
467
468
int
469
cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
470
0
{
471
0
#ifdef WITH_OPENSSL
472
0
  const struct sshcipher *c = cc->cipher;
473
0
  int evplen = 0;
474
0
#endif
475
476
0
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
477
0
    return 0;
478
0
  if ((cc->cipher->flags & CFLAG_NONE) != 0)
479
0
    return 0;
480
481
0
#ifdef WITH_OPENSSL
482
0
  evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
483
0
  if (evplen <= 0)
484
0
    return SSH_ERR_LIBCRYPTO_ERROR;
485
0
  if ((size_t)evplen != len)
486
0
    return SSH_ERR_INVALID_ARGUMENT;
487
0
  if (cipher_authlen(c)) {
488
    /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
489
0
    if (EVP_CIPHER_CTX_ctrl(cc->evp,
490
0
        EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv) <= 0)
491
0
      return SSH_ERR_LIBCRYPTO_ERROR;
492
0
  } else if (!EVP_CIPHER_CTX_set_iv(cc->evp, iv, evplen))
493
0
    return SSH_ERR_LIBCRYPTO_ERROR;
494
0
#endif
495
0
  return 0;
496
0
}