Coverage Report

Created: 2026-03-09 06:40

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.126 2026/02/14 00:18:34 jsg 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
51
#include "openbsd-compat/openssl-compat.h"
52
53
#ifndef WITH_OPENSSL
54
#define EVP_CIPHER_CTX void
55
#endif
56
57
struct sshcipher_ctx {
58
  int plaintext;
59
  int encrypt;
60
  EVP_CIPHER_CTX *evp;
61
  struct chachapoly_ctx *cp_ctx;
62
  struct aesctr_ctx ac_ctx; /* XXX union with evp? */
63
  const struct sshcipher *cipher;
64
};
65
66
struct sshcipher {
67
  char  *name;
68
  u_int block_size;
69
  u_int key_len;
70
  u_int iv_len;   /* defaults to block_size */
71
  u_int auth_len;
72
  u_int flags;
73
229
#define CFLAG_CBC   (1<<0)
74
2.66M
#define CFLAG_CHACHAPOLY  (1<<1)
75
402k
#define CFLAG_AESCTR    (1<<2)
76
2.59M
#define CFLAG_NONE    (1<<3)
77
0
#define CFLAG_INTERNAL 0
78
#ifdef WITH_OPENSSL
79
  const EVP_CIPHER  *(*evptype)(void);
80
#else
81
  void  *ignored;
82
#endif
83
};
84
85
static const struct sshcipher ciphers[] = {
86
#ifdef WITH_OPENSSL
87
#ifndef OPENSSL_NO_DES
88
  { "3des-cbc",   8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
89
#endif
90
  { "aes128-cbc",   16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
91
  { "aes192-cbc",   16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
92
  { "aes256-cbc",   16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
93
  { "aes128-ctr",   16, 16, 0, 0, 0, EVP_aes_128_ctr },
94
  { "aes192-ctr",   16, 24, 0, 0, 0, EVP_aes_192_ctr },
95
  { "aes256-ctr",   16, 32, 0, 0, 0, EVP_aes_256_ctr },
96
  { "aes128-gcm@openssh.com",
97
        16, 16, 12, 16, 0, EVP_aes_128_gcm },
98
  { "aes256-gcm@openssh.com",
99
        16, 32, 12, 16, 0, EVP_aes_256_gcm },
100
#else
101
  { "aes128-ctr",   16, 16, 0, 0, CFLAG_AESCTR, NULL },
102
  { "aes192-ctr",   16, 24, 0, 0, CFLAG_AESCTR, NULL },
103
  { "aes256-ctr",   16, 32, 0, 0, CFLAG_AESCTR, NULL },
104
#endif
105
  { "chacha20-poly1305@openssh.com",
106
        8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
107
  { "none",   8, 0, 0, 0, CFLAG_NONE, NULL },
108
109
  { NULL,     0, 0, 0, 0, 0, NULL }
110
};
111
112
/*--*/
113
114
/* Returns a comma-separated list of supported ciphers. */
115
char *
116
cipher_alg_list(char sep, int auth_only)
117
0
{
118
0
  char *ret = NULL;
119
0
  const struct sshcipher *c;
120
0
  char sep_str[2] = {sep, '\0'};
121
122
0
  for (c = ciphers; c->name != NULL; c++) {
123
0
    if ((c->flags & CFLAG_INTERNAL) != 0)
124
0
      continue;
125
0
    if (auth_only && c->auth_len == 0)
126
0
      continue;
127
0
    xextendf(&ret, sep_str, "%s", c->name);
128
0
  }
129
0
  return ret;
130
0
}
131
132
const char *
133
compression_alg_list(int compression)
134
0
{
135
0
#ifdef WITH_ZLIB
136
0
  return compression ? "zlib@openssh.com,none" :
137
0
      "none,zlib@openssh.com";
138
#else
139
  return "none";
140
#endif
141
0
}
142
143
u_int
144
cipher_blocksize(const struct sshcipher *c)
145
45.4k
{
146
45.4k
  return (c->block_size);
147
45.4k
}
148
149
u_int
150
cipher_keylen(const struct sshcipher *c)
151
87.3k
{
152
87.3k
  return (c->key_len);
153
87.3k
}
154
155
u_int
156
cipher_seclen(const struct sshcipher *c)
157
41.9k
{
158
41.9k
  if (strcmp("3des-cbc", c->name) == 0)
159
0
    return 14;
160
41.9k
  return cipher_keylen(c);
161
41.9k
}
162
163
u_int
164
cipher_authlen(const struct sshcipher *c)
165
246k
{
166
246k
  return (c->auth_len);
167
246k
}
168
169
u_int
170
cipher_ivlen(const struct sshcipher *c)
171
64.2k
{
172
  /*
173
   * Default is cipher block size, except for chacha20+poly1305 that
174
   * needs no IV. XXX make iv_len == -1 default?
175
   */
176
64.2k
  return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
177
64.2k
      c->iv_len : c->block_size;
178
64.2k
}
179
180
u_int
181
cipher_is_cbc(const struct sshcipher *c)
182
229
{
183
229
  return (c->flags & CFLAG_CBC) != 0;
184
229
}
185
186
u_int
187
cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
188
199k
{
189
199k
  return cc->plaintext;
190
199k
}
191
192
const struct sshcipher *
193
cipher_by_name(const char *name)
194
237k
{
195
237k
  const struct sshcipher *c;
196
2.61M
  for (c = ciphers; c->name != NULL; c++)
197
2.61M
    if (strcmp(c->name, name) == 0)
198
237k
      return c;
199
0
  return NULL;
200
237k
}
201
202
0
#define CIPHER_SEP  ","
203
int
204
ciphers_valid(const char *names)
205
0
{
206
0
  const struct sshcipher *c;
207
0
  char *cipher_list, *cp;
208
0
  char *p;
209
210
0
  if (names == NULL || strcmp(names, "") == 0)
211
0
    return 0;
212
0
  if ((cipher_list = cp = strdup(names)) == NULL)
213
0
    return 0;
214
0
  for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
215
0
      (p = strsep(&cp, CIPHER_SEP))) {
216
0
    c = cipher_by_name(p);
217
0
    if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
218
0
      free(cipher_list);
219
0
      return 0;
220
0
    }
221
0
  }
222
0
  free(cipher_list);
223
0
  return 1;
224
0
}
225
226
const char *
227
cipher_warning_message(const struct sshcipher_ctx *cc)
228
1.33k
{
229
1.33k
  if (cc == NULL || cc->cipher == NULL)
230
0
    return NULL;
231
  /* XXX repurpose for CBC warning */
232
1.33k
  return NULL;
233
1.33k
}
234
235
int
236
cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
237
    const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
238
    int do_encrypt)
239
402k
{
240
402k
  struct sshcipher_ctx *cc = NULL;
241
402k
  int ret = SSH_ERR_INTERNAL_ERROR;
242
402k
#ifdef WITH_OPENSSL
243
402k
  const EVP_CIPHER *type;
244
402k
  int klen;
245
402k
#endif
246
247
402k
  *ccp = NULL;
248
402k
  if ((cc = calloc(1, sizeof(*cc))) == NULL)
249
0
    return SSH_ERR_ALLOC_FAIL;
250
251
402k
  cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
252
402k
  cc->encrypt = do_encrypt;
253
254
402k
  if (keylen < cipher->key_len ||
255
402k
      (iv != NULL && ivlen < cipher_ivlen(cipher))) {
256
0
    ret = SSH_ERR_INVALID_ARGUMENT;
257
0
    goto out;
258
0
  }
259
260
402k
  cc->cipher = cipher;
261
402k
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
262
0
    cc->cp_ctx = chachapoly_new(key, keylen);
263
0
    ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT;
264
0
    goto out;
265
0
  }
266
402k
  if ((cc->cipher->flags & CFLAG_NONE) != 0) {
267
402k
    ret = 0;
268
402k
    goto out;
269
402k
  }
270
#ifndef WITH_OPENSSL
271
  if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
272
    aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
273
    aesctr_ivsetup(&cc->ac_ctx, iv);
274
    ret = 0;
275
    goto out;
276
  }
277
  ret = SSH_ERR_INVALID_ARGUMENT;
278
  goto out;
279
#else /* WITH_OPENSSL */
280
0
  type = (*cipher->evptype)();
281
0
  if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
282
0
    ret = SSH_ERR_ALLOC_FAIL;
283
0
    goto out;
284
0
  }
285
0
  if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
286
0
      (do_encrypt == CIPHER_ENCRYPT)) == 0) {
287
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
288
0
    goto out;
289
0
  }
290
0
  if (cipher_authlen(cipher) &&
291
0
      EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
292
0
      -1, (u_char *)iv) <= 0) {
293
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
294
0
    goto out;
295
0
  }
296
0
  klen = EVP_CIPHER_CTX_key_length(cc->evp);
297
0
  if (klen > 0 && keylen != (u_int)klen) {
298
0
    if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
299
0
      ret = SSH_ERR_LIBCRYPTO_ERROR;
300
0
      goto out;
301
0
    }
302
0
  }
303
0
  if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
304
0
    ret = SSH_ERR_LIBCRYPTO_ERROR;
305
0
    goto out;
306
0
  }
307
0
  ret = 0;
308
0
#endif /* WITH_OPENSSL */
309
402k
 out:
310
402k
  if (ret == 0) {
311
    /* success */
312
402k
    *ccp = cc;
313
402k
  } else {
314
0
    if (cc != NULL) {
315
0
#ifdef WITH_OPENSSL
316
0
      EVP_CIPHER_CTX_free(cc->evp);
317
0
#endif /* WITH_OPENSSL */
318
0
      freezero(cc, sizeof(*cc));
319
0
    }
320
0
  }
321
402k
  return ret;
322
0
}
323
324
/*
325
 * cipher_crypt() operates as following:
326
 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
327
 * These bytes are treated as additional authenticated data for
328
 * authenticated encryption modes.
329
 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
330
 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
331
 * This tag is written on encryption and verified on decryption.
332
 * Both 'aadlen' and 'authlen' can be set to 0.
333
 */
334
int
335
cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
336
   const u_char *src, u_int len, u_int aadlen, u_int authlen)
337
1.79M
{
338
1.79M
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
339
0
    return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src,
340
0
        len, aadlen, authlen, cc->encrypt);
341
0
  }
342
1.79M
  if ((cc->cipher->flags & CFLAG_NONE) != 0) {
343
1.79M
    memcpy(dest, src, aadlen + len);
344
1.79M
    return 0;
345
1.79M
  }
346
#ifndef WITH_OPENSSL
347
  if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
348
    if (aadlen)
349
      memcpy(dest, src, aadlen);
350
    aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
351
        dest + aadlen, len);
352
    return 0;
353
  }
354
  return SSH_ERR_INVALID_ARGUMENT;
355
#else
356
0
  if (authlen) {
357
0
    u_char lastiv[1];
358
359
0
    if (authlen != cipher_authlen(cc->cipher))
360
0
      return SSH_ERR_INVALID_ARGUMENT;
361
    /* increment IV */
362
0
    if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
363
0
        1, lastiv) <= 0)
364
0
      return SSH_ERR_LIBCRYPTO_ERROR;
365
    /* set tag on decryption */
366
0
    if (!cc->encrypt &&
367
0
        EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
368
0
        authlen, (u_char *)src + aadlen + len) <= 0)
369
0
      return SSH_ERR_LIBCRYPTO_ERROR;
370
0
  }
371
0
  if (aadlen) {
372
0
    if (authlen &&
373
0
        EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
374
0
      return SSH_ERR_LIBCRYPTO_ERROR;
375
0
    memcpy(dest, src, aadlen);
376
0
  }
377
0
  if (len % cc->cipher->block_size)
378
0
    return SSH_ERR_INVALID_ARGUMENT;
379
0
  if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
380
0
      len) < 0)
381
0
    return SSH_ERR_LIBCRYPTO_ERROR;
382
0
  if (authlen) {
383
    /* compute tag (on encrypt) or verify tag (on decrypt) */
384
0
    if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
385
0
      return cc->encrypt ?
386
0
          SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
387
0
    if (cc->encrypt &&
388
0
        EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
389
0
        authlen, dest + aadlen + len) <= 0)
390
0
      return SSH_ERR_LIBCRYPTO_ERROR;
391
0
  }
392
0
  return 0;
393
0
#endif
394
0
}
395
396
/* Extract the packet length, including any decryption necessary beforehand */
397
int
398
cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
399
    const u_char *cp, u_int len)
400
199
{
401
199
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
402
0
    return chachapoly_get_length(cc->cp_ctx, plenp, seqnr,
403
0
        cp, len);
404
199
  if (len < 4)
405
0
    return SSH_ERR_MESSAGE_INCOMPLETE;
406
199
  *plenp = PEEK_U32(cp);
407
199
  return 0;
408
199
}
409
410
void
411
cipher_free(struct sshcipher_ctx *cc)
412
402k
{
413
402k
  if (cc == NULL)
414
0
    return;
415
402k
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
416
0
    chachapoly_free(cc->cp_ctx);
417
0
    cc->cp_ctx = NULL;
418
402k
  } else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
419
0
    explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
420
402k
#ifdef WITH_OPENSSL
421
402k
  EVP_CIPHER_CTX_free(cc->evp);
422
402k
  cc->evp = NULL;
423
402k
#endif
424
402k
  freezero(cc, sizeof(*cc));
425
402k
}
426
427
int
428
cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
429
0
{
430
0
#ifdef WITH_OPENSSL
431
0
  const struct sshcipher *c = cc->cipher;
432
0
  int evplen;
433
0
#endif
434
435
0
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
436
0
    if (len != 0)
437
0
      return SSH_ERR_INVALID_ARGUMENT;
438
0
    return 0;
439
0
  }
440
0
  if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
441
0
    if (len != sizeof(cc->ac_ctx.ctr))
442
0
      return SSH_ERR_INVALID_ARGUMENT;
443
0
    memcpy(iv, cc->ac_ctx.ctr, len);
444
0
    return 0;
445
0
  }
446
0
  if ((cc->cipher->flags & CFLAG_NONE) != 0)
447
0
    return 0;
448
449
0
#ifdef WITH_OPENSSL
450
0
  evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
451
0
  if (evplen == 0)
452
0
    return 0;
453
0
  else if (evplen < 0)
454
0
    return SSH_ERR_LIBCRYPTO_ERROR;
455
0
  if ((size_t)evplen != len)
456
0
    return SSH_ERR_INVALID_ARGUMENT;
457
0
  if (cipher_authlen(c)) {
458
0
    if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, len,
459
0
        iv) <= 0)
460
0
      return SSH_ERR_LIBCRYPTO_ERROR;
461
0
  } else if (EVP_CIPHER_CTX_get_iv(cc->evp, iv, len) <= 0)
462
0
    return SSH_ERR_LIBCRYPTO_ERROR;
463
0
#endif
464
0
  return 0;
465
0
}
466
467
int
468
cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
469
0
{
470
0
#ifdef WITH_OPENSSL
471
0
  const struct sshcipher *c = cc->cipher;
472
0
  int evplen = 0;
473
0
#endif
474
475
0
  if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
476
0
    return 0;
477
0
  if ((cc->cipher->flags & CFLAG_NONE) != 0)
478
0
    return 0;
479
480
0
#ifdef WITH_OPENSSL
481
0
  evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
482
0
  if (evplen <= 0)
483
0
    return SSH_ERR_LIBCRYPTO_ERROR;
484
0
  if ((size_t)evplen != len)
485
0
    return SSH_ERR_INVALID_ARGUMENT;
486
0
  if (cipher_authlen(c)) {
487
    /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
488
0
    if (EVP_CIPHER_CTX_ctrl(cc->evp,
489
0
        EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv) <= 0)
490
0
      return SSH_ERR_LIBCRYPTO_ERROR;
491
0
  } else if (!EVP_CIPHER_CTX_set_iv(cc->evp, iv, evplen))
492
0
    return SSH_ERR_LIBCRYPTO_ERROR;
493
0
#endif
494
0
  return 0;
495
0
}