33#elif defined(HAVE_LIBMBEDCRYPTO)
34#include <mbedtls/gcm.h>
36#include "libssh/wrapper.h"
45#ifdef HAVE_OPENSSL_ECDH_H
46#include <openssl/ecdh.h>
48#include "libssh/curve25519.h"
50#include "libssh/ecdh.h"
51#include "libssh/kex.h"
52#include "libssh/sntrup761.h"
54#include "libssh/mlkem768.h"
57#define DIGEST_MAX_LEN 64
59#define AES_GCM_TAGLEN 16
60#define AES_GCM_IVLEN 12
62enum ssh_key_exchange_e {
64 SSH_KEX_DH_GROUP1_SHA1 = 1,
66 SSH_KEX_DH_GROUP14_SHA1,
71 SSH_KEX_DH_GEX_SHA256,
74 SSH_KEX_ECDH_SHA2_NISTP256,
76 SSH_KEX_ECDH_SHA2_NISTP384,
78 SSH_KEX_ECDH_SHA2_NISTP521,
80 SSH_KEX_CURVE25519_SHA256_LIBSSH_ORG,
82 SSH_KEX_CURVE25519_SHA256,
84 SSH_KEX_DH_GROUP16_SHA512,
86 SSH_KEX_DH_GROUP18_SHA512,
88 SSH_KEX_DH_GROUP14_SHA256,
90 SSH_KEX_SNTRUP761X25519_SHA512_OPENSSH_COM,
92 SSH_KEX_SNTRUP761X25519_SHA512,
95 SSH_KEX_MLKEM768X25519_SHA256,
113 SSH_AEAD_CHACHA20_POLY1305
118struct ssh_crypto_struct {
119 bignum shared_secret;
120 struct dh_ctx *dh_ctx;
122 size_t dh_pmin;
size_t dh_pn;
size_t dh_pmax;
125#ifdef HAVE_OPENSSL_ECC
126#if OPENSSL_VERSION_NUMBER < 0x30000000L
127 EC_KEY *ecdh_privkey;
129 EVP_PKEY *ecdh_privkey;
131#elif defined HAVE_GCRYPT_ECC
132 gcry_sexp_t ecdh_privkey;
133#elif defined HAVE_LIBMBEDCRYPTO
134 mbedtls_ecp_keypair *ecdh_privkey;
136 ssh_string ecdh_client_pubkey;
137 ssh_string ecdh_server_pubkey;
139#ifdef HAVE_CURVE25519
141 EVP_PKEY *curve25519_privkey;
142#elif defined(HAVE_GCRYPT_CURVE25519)
143 gcry_sexp_t curve25519_privkey;
145 ssh_curve25519_privkey curve25519_privkey;
147 ssh_curve25519_pubkey curve25519_client_pubkey;
148 ssh_curve25519_pubkey curve25519_server_pubkey;
151 ssh_mlkem768_privkey mlkem768_client_privkey;
152 ssh_mlkem768_pubkey mlkem768_client_pubkey;
153 ssh_mlkem768_ciphertext mlkem768_ciphertext;
156 ssh_sntrup761_privkey sntrup761_privkey;
157 ssh_sntrup761_pubkey sntrup761_client_pubkey;
158 ssh_sntrup761_ciphertext sntrup761_ciphertext;
160 ssh_string dh_server_signature;
161 size_t session_id_len;
162 unsigned char *session_id;
164 unsigned char *secret_hash;
165 unsigned char *encryptIV;
166 unsigned char *decryptIV;
167 unsigned char *decryptkey;
168 unsigned char *encryptkey;
169 unsigned char *encryptMAC;
170 unsigned char *decryptMAC;
171 unsigned char hmacbuf[DIGEST_MAX_LEN];
172 struct ssh_cipher_struct *in_cipher, *out_cipher;
173 enum ssh_hmac_e in_hmac, out_hmac;
174 bool in_hmac_etm, out_hmac_etm;
176 ssh_key server_pubkey;
179 int delayed_compress_in;
180 int delayed_compress_out;
181 void *compress_out_ctx;
182 void *compress_in_ctx;
184 struct ssh_kex_struct server_kex;
185 struct ssh_kex_struct client_kex;
186 char *kex_methods[SSH_KEX_METHODS];
187 enum ssh_key_exchange_e kex_type;
188 enum ssh_kdf_digest digest_type;
189 enum ssh_crypto_direction_e used;
192struct ssh_cipher_struct {
194 unsigned int blocksize;
195 enum ssh_cipher_e ciphertype;
196 uint32_t lenfield_blocksize;
199 gcry_cipher_hd_t *key;
200 unsigned char last_iv[AES_GCM_IVLEN];
201#elif defined HAVE_LIBCRYPTO
202 struct ssh_3des_key_schedule *des3_key;
203 struct ssh_aes_key_schedule *aes_key;
204 const EVP_CIPHER *cipher;
206#elif defined HAVE_LIBMBEDCRYPTO
207 mbedtls_cipher_context_t encrypt_ctx;
208 mbedtls_cipher_context_t decrypt_ctx;
209 mbedtls_cipher_type_t type;
211 mbedtls_gcm_context gcm_ctx;
212 unsigned char last_iv[AES_GCM_IVLEN];
215 struct chacha20_poly1305_keysched *chacha20_schedule;
216 unsigned int keysize;
224 int (*set_encrypt_key)(
struct ssh_cipher_struct *cipher,
void *key,
void *IV);
225 int (*set_decrypt_key)(
struct ssh_cipher_struct *cipher,
void *key,
void *IV);
226 void (*encrypt)(
struct ssh_cipher_struct *cipher,
230 void (*decrypt)(
struct ssh_cipher_struct *cipher,
234 void (*aead_encrypt)(
struct ssh_cipher_struct *cipher,
void *in,
void *out,
235 size_t len, uint8_t *mac, uint64_t seq);
236 int (*aead_decrypt_length)(
struct ssh_cipher_struct *cipher,
void *in,
237 uint8_t *out,
size_t len, uint64_t seq);
238 int (*aead_decrypt)(
struct ssh_cipher_struct *cipher,
void *complete_packet, uint8_t *out,
239 size_t encrypted_size, uint64_t seq);
240 void (*cleanup)(
struct ssh_cipher_struct *cipher);
247const struct ssh_cipher_struct *ssh_get_chacha20poly1305_cipher(
void);
248int sshkdf_derive_key(
struct ssh_crypto_struct *crypto,
249 unsigned char *key,
size_t key_len,
250 uint8_t key_type,
unsigned char *output,
251 size_t requested_len);
253int secure_memcmp(
const void *s1,
const void *s2,
size_t n);
255void compress_cleanup(
struct ssh_crypto_struct *crypto);