libssh 0.7.3

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 "config.h"
29
30 #ifdef HAVE_LIBGCRYPT
31 #include <gcrypt.h>
32 #endif
33 #include "libssh/wrapper.h"
34
35 #ifdef cbc_encrypt
36 #undef cbc_encrypt
37 #endif
38 #ifdef cbc_decrypt
39 #undef cbc_decrypt
40 #endif
41
42 #ifdef HAVE_OPENSSL_ECDH_H
43 #include <openssl/ecdh.h>
44 #endif
45 #include "libssh/ecdh.h"
46 #include "libssh/kex.h"
47 #include "libssh/curve25519.h"
48
49 #define DIGEST_MAX_LEN 64
50
51 enum ssh_key_exchange_e {
52 /* diffie-hellman-group1-sha1 */
53 SSH_KEX_DH_GROUP1_SHA1=1,
54 /* diffie-hellman-group14-sha1 */
55 SSH_KEX_DH_GROUP14_SHA1,
56 /* ecdh-sha2-nistp256 */
57 SSH_KEX_ECDH_SHA2_NISTP256,
58 /* curve25519-sha256@libssh.org */
59 SSH_KEX_CURVE25519_SHA256_LIBSSH_ORG
60 };
61
62 struct ssh_crypto_struct {
63 bignum e,f,x,k,y;
64 #ifdef HAVE_ECDH
65 EC_KEY *ecdh_privkey;
66 ssh_string ecdh_client_pubkey;
67 ssh_string ecdh_server_pubkey;
68 #endif
69 #ifdef HAVE_CURVE25519
70 ssh_curve25519_privkey curve25519_privkey;
71 ssh_curve25519_pubkey curve25519_client_pubkey;
72 ssh_curve25519_pubkey curve25519_server_pubkey;
73 #endif
74 ssh_string dh_server_signature; /* information used by dh_handshake. */
75 size_t digest_len; /* len of all the fields below */
76 unsigned char *session_id;
77 unsigned char *secret_hash; /* Secret hash is same as session id until re-kex */
78 unsigned char *encryptIV;
79 unsigned char *decryptIV;
80 unsigned char *decryptkey;
81 unsigned char *encryptkey;
82 unsigned char *encryptMAC;
83 unsigned char *decryptMAC;
84 unsigned char hmacbuf[DIGEST_MAX_LEN];
85 struct ssh_cipher_struct *in_cipher, *out_cipher; /* the cipher structures/objects */
86 enum ssh_hmac_e in_hmac, out_hmac; /* the MAC algorithms used */
87
88 ssh_string server_pubkey;
89 const char *server_pubkey_type;
90 int do_compress_out; /* idem */
91 int do_compress_in; /* don't set them, set the option instead */
92 int delayed_compress_in; /* Use of zlib@openssh.org */
93 int delayed_compress_out;
94 void *compress_out_ctx; /* don't touch it */
95 void *compress_in_ctx; /* really, don't */
96 /* kex sent by server, client, and mutually elected methods */
97 struct ssh_kex_struct server_kex;
98 struct ssh_kex_struct client_kex;
99 char *kex_methods[SSH_KEX_METHODS];
100 enum ssh_key_exchange_e kex_type;
101 enum ssh_mac_e mac_type; /* Mac operations to use for key gen */
102 };
103
104 struct ssh_cipher_struct {
105 const char *name; /* ssh name of the algorithm */
106 unsigned int blocksize; /* blocksize of the algo */
107 unsigned int keylen; /* length of the key structure */
108 #ifdef HAVE_LIBGCRYPT
109 gcry_cipher_hd_t *key;
110 #elif defined HAVE_LIBCRYPTO
111 void *key; /* a key buffer allocated for the algo */
112 void *IV;
113 #endif
114 unsigned int keysize; /* bytes of key used. != keylen */
115 /* sets the new key for immediate use */
116 int (*set_encrypt_key)(struct ssh_cipher_struct *cipher, void *key, void *IV);
117 int (*set_decrypt_key)(struct ssh_cipher_struct *cipher, void *key, void *IV);
118 void (*encrypt)(struct ssh_cipher_struct *cipher, void *in, void *out,
119 unsigned long len);
120 void (*decrypt)(struct ssh_cipher_struct *cipher, void *in, void *out,
121 unsigned long len);
122 };
123
124 /* vim: set ts=2 sw=2 et cindent: */
125 #endif /* _CRYPTO_H_ */