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