libssh 0.11.0
The SSH library
Loading...
Searching...
No Matches
crypto.h
1/*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2003-2009 by Aris Adamantiadis
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * crypto.h is an include file for internal cryptographic structures of libssh
23 */
24
25#ifndef _CRYPTO_H_
26#define _CRYPTO_H_
27
28#include <stdbool.h>
29#include "config.h"
30
31#ifdef HAVE_LIBGCRYPT
32#include <gcrypt.h>
33#elif defined(HAVE_LIBMBEDCRYPTO)
34#include <mbedtls/gcm.h>
35#endif
36#include "libssh/wrapper.h"
37
38#ifdef cbc_encrypt
39#undef cbc_encrypt
40#endif
41#ifdef cbc_decrypt
42#undef cbc_decrypt
43#endif
44
45#ifdef HAVE_OPENSSL_ECDH_H
46#include <openssl/ecdh.h>
47#endif
48#include "libssh/curve25519.h"
49#include "libssh/dh.h"
50#include "libssh/ecdh.h"
51#include "libssh/kex.h"
52#include "libssh/sntrup761.h"
53#ifdef HAVE_MLKEM
54#include "libssh/mlkem768.h"
55#endif
56
57#define DIGEST_MAX_LEN 64
58
59#define AES_GCM_TAGLEN 16
60#define AES_GCM_IVLEN 12
61
62enum ssh_key_exchange_e {
63 /* diffie-hellman-group1-sha1 */
64 SSH_KEX_DH_GROUP1_SHA1 = 1,
65 /* diffie-hellman-group14-sha1 */
66 SSH_KEX_DH_GROUP14_SHA1,
67#ifdef WITH_GEX
68 /* diffie-hellman-group-exchange-sha1 */
69 SSH_KEX_DH_GEX_SHA1,
70 /* diffie-hellman-group-exchange-sha256 */
71 SSH_KEX_DH_GEX_SHA256,
72#endif /* WITH_GEX */
73 /* ecdh-sha2-nistp256 */
74 SSH_KEX_ECDH_SHA2_NISTP256,
75 /* ecdh-sha2-nistp384 */
76 SSH_KEX_ECDH_SHA2_NISTP384,
77 /* ecdh-sha2-nistp521 */
78 SSH_KEX_ECDH_SHA2_NISTP521,
79 /* curve25519-sha256@libssh.org */
80 SSH_KEX_CURVE25519_SHA256_LIBSSH_ORG,
81 /* curve25519-sha256 */
82 SSH_KEX_CURVE25519_SHA256,
83 /* diffie-hellman-group16-sha512 */
84 SSH_KEX_DH_GROUP16_SHA512,
85 /* diffie-hellman-group18-sha512 */
86 SSH_KEX_DH_GROUP18_SHA512,
87 /* diffie-hellman-group14-sha256 */
88 SSH_KEX_DH_GROUP14_SHA256,
89 /* sntrup761x25519-sha512@openssh.com */
90 SSH_KEX_SNTRUP761X25519_SHA512_OPENSSH_COM,
91 /* sntrup761x25519-sha512 */
92 SSH_KEX_SNTRUP761X25519_SHA512,
93#ifdef HAVE_MLKEM
94 /* mlkem768x25519-sha256 */
95 SSH_KEX_MLKEM768X25519_SHA256,
96#endif /* HAVE_MLKEM */
97};
98
99enum ssh_cipher_e {
100 SSH_NO_CIPHER=0,
101#ifdef HAVE_BLOWFISH
102 SSH_BLOWFISH_CBC,
103#endif /* HAVE_BLOWFISH */
104 SSH_3DES_CBC,
105 SSH_AES128_CBC,
106 SSH_AES192_CBC,
107 SSH_AES256_CBC,
108 SSH_AES128_CTR,
109 SSH_AES192_CTR,
110 SSH_AES256_CTR,
111 SSH_AEAD_AES128_GCM,
112 SSH_AEAD_AES256_GCM,
113 SSH_AEAD_CHACHA20_POLY1305
114};
115
116struct dh_ctx;
117
118struct ssh_crypto_struct {
119 bignum shared_secret;
120 struct dh_ctx *dh_ctx;
121#ifdef WITH_GEX
122 size_t dh_pmin; size_t dh_pn; size_t dh_pmax; /* preferred group parameters */
123#endif /* WITH_GEX */
124#ifdef HAVE_ECDH
125#ifdef HAVE_OPENSSL_ECC
126#if OPENSSL_VERSION_NUMBER < 0x30000000L
127 EC_KEY *ecdh_privkey;
128#else
129 EVP_PKEY *ecdh_privkey;
130#endif /* OPENSSL_VERSION_NUMBER */
131#elif defined HAVE_GCRYPT_ECC
132 gcry_sexp_t ecdh_privkey;
133#elif defined HAVE_LIBMBEDCRYPTO
134 mbedtls_ecp_keypair *ecdh_privkey;
135#endif
136 ssh_string ecdh_client_pubkey;
137 ssh_string ecdh_server_pubkey;
138#endif
139#ifdef HAVE_CURVE25519
140#ifdef HAVE_LIBCRYPTO
141 EVP_PKEY *curve25519_privkey;
142#elif defined(HAVE_GCRYPT_CURVE25519)
143 gcry_sexp_t curve25519_privkey;
144#else
145 ssh_curve25519_privkey curve25519_privkey;
146#endif
147 ssh_curve25519_pubkey curve25519_client_pubkey;
148 ssh_curve25519_pubkey curve25519_server_pubkey;
149#endif
150#ifdef HAVE_MLKEM
151 ssh_mlkem768_privkey mlkem768_client_privkey;
152 ssh_mlkem768_pubkey mlkem768_client_pubkey;
153 ssh_mlkem768_ciphertext mlkem768_ciphertext;
154#endif
155#ifdef HAVE_SNTRUP761
156 ssh_sntrup761_privkey sntrup761_privkey;
157 ssh_sntrup761_pubkey sntrup761_client_pubkey;
158 ssh_sntrup761_ciphertext sntrup761_ciphertext;
159#endif
160 ssh_string dh_server_signature; /* information used by dh_handshake. */
161 size_t session_id_len;
162 unsigned char *session_id;
163 size_t digest_len; /* len of the secret hash */
164 unsigned char *secret_hash; /* Secret hash is same as session id until re-kex */
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; /* the cipher structures/objects */
173 enum ssh_hmac_e in_hmac, out_hmac; /* the MAC algorithms used */
174 bool in_hmac_etm, out_hmac_etm; /* Whether EtM mode is used or not */
175
176 ssh_key server_pubkey;
177 int do_compress_out; /* idem */
178 int do_compress_in; /* don't set them, set the option instead */
179 int delayed_compress_in; /* Use of zlib@openssh.org */
180 int delayed_compress_out;
181 void *compress_out_ctx; /* don't touch it */
182 void *compress_in_ctx; /* really, don't */
183 /* kex sent by server, client, and mutually elected methods */
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; /* Digest type for session keys derivation */
189 enum ssh_crypto_direction_e used; /* Is this crypto still used for either of directions? */
190};
191
192struct ssh_cipher_struct {
193 const char *name; /* ssh name of the algorithm */
194 unsigned int blocksize; /* blocksize of the algo */
195 enum ssh_cipher_e ciphertype;
196 uint32_t lenfield_blocksize; /* blocksize of the packet length field */
197 size_t keylen; /* length of the key structure */
198#ifdef HAVE_LIBGCRYPT
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;
205 EVP_CIPHER_CTX *ctx;
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;
210#ifdef MBEDTLS_GCM_C
211 mbedtls_gcm_context gcm_ctx;
212 unsigned char last_iv[AES_GCM_IVLEN];
213#endif /* MBEDTLS_GCM_C */
214#endif
215 struct chacha20_poly1305_keysched *chacha20_schedule;
216 unsigned int keysize; /* bytes of key used. != keylen */
217 size_t tag_size; /* overhead required for tag */
218 /* Counters for rekeying initialization */
219 uint32_t packets;
220 uint64_t blocks;
221 /* Rekeying limit for the cipher or manually enforced */
222 uint64_t max_blocks;
223 /* sets the new key for immediate use */
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,
227 void *in,
228 void *out,
229 size_t len);
230 void (*decrypt)(struct ssh_cipher_struct *cipher,
231 void *in,
232 void *out,
233 size_t len);
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);
241};
242
243#ifdef __cplusplus
244extern "C" {
245#endif
246
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);
252
253int secure_memcmp(const void *s1, const void *s2, size_t n);
254
255void compress_cleanup(struct ssh_crypto_struct *crypto);
256
257#ifdef __cplusplus
258}
259#endif
260
261#endif /* _CRYPTO_H_ */