1 /* Copyright (C) 2006, 2007 The Written Word, Inc. All rights reserved.
2 * Author: Simon Josefsson
4 * Redistribution and use in source and binary forms,
5 * with or without modification, are permitted provided
6 * that the following conditions are met:
8 * Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the
10 * following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * Neither the name of the copyright holder nor the names
18 * of any other contributors may be used to endorse or
19 * promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38 #include <openssl/opensslconf.h>
39 #include <openssl/sha.h>
40 #ifndef OPENSSL_NO_MD5
41 #include <openssl/md5.h>
43 #include <openssl/evp.h>
44 #include <openssl/hmac.h>
45 #include <openssl/bn.h>
46 #include <openssl/pem.h>
47 #include <openssl/rand.h>
50 # define LIBSSH2_RSA 0
52 # define LIBSSH2_RSA 1
56 # define LIBSSH2_DSA 0
58 # define LIBSSH2_DSA 1
62 # define LIBSSH2_MD5 0
64 # define LIBSSH2_MD5 1
67 #ifdef OPENSSL_NO_RIPEMD
68 # define LIBSSH2_HMAC_RIPEMD 0
70 # define LIBSSH2_HMAC_RIPEMD 1
73 #if OPENSSL_VERSION_NUMBER >= 0x00907000L && !defined(OPENSSL_NO_AES)
74 # define LIBSSH2_AES 1
76 # define LIBSSH2_AES 0
79 #ifdef OPENSSL_NO_BLOWFISH
80 # define LIBSSH2_BLOWFISH 0
82 # define LIBSSH2_BLOWFISH 1
86 # define LIBSSH2_RC4 0
88 # define LIBSSH2_RC4 1
91 #ifdef OPENSSL_NO_CAST
92 # define LIBSSH2_CAST 0
94 # define LIBSSH2_CAST 1
98 # define LIBSSH2_3DES 0
100 # define LIBSSH2_3DES 1
103 #define libssh2_random(buf, len) \
104 RAND_bytes ((buf), (len))
106 #define libssh2_sha1_ctx SHA_CTX
107 #define libssh2_sha1_init(ctx) SHA1_Init(ctx)
108 #define libssh2_sha1_update(ctx, data, len) SHA1_Update(&(ctx), data, len)
109 #define libssh2_sha1_final(ctx, out) SHA1_Final(out, &(ctx))
110 #define libssh2_sha1(message, len, out) SHA1(message, len, out)
112 #define libssh2_md5_ctx MD5_CTX
113 #define libssh2_md5_init(ctx) MD5_Init(ctx)
114 #define libssh2_md5_update(ctx, data, len) MD5_Update(&(ctx), data, len)
115 #define libssh2_md5_final(ctx, out) MD5_Final(out, &(ctx))
116 #define libssh2_md5(message, len, out) MD5(message, len, out)
118 #define libssh2_hmac_ctx HMAC_CTX
119 #define libssh2_hmac_sha1_init(ctx, key, keylen) \
120 HMAC_Init(ctx, key, keylen, EVP_sha1())
121 #define libssh2_hmac_md5_init(ctx, key, keylen) \
122 HMAC_Init(ctx, key, keylen, EVP_md5())
123 #define libssh2_hmac_ripemd160_init(ctx, key, keylen) \
124 HMAC_Init(ctx, key, keylen, EVP_ripemd160())
125 #define libssh2_hmac_update(ctx, data, datalen) \
126 HMAC_Update(&(ctx), data, datalen)
127 #define libssh2_hmac_final(ctx, data) HMAC_Final(&(ctx), data, NULL)
128 #define libssh2_hmac_cleanup(ctx) HMAC_cleanup(ctx)
130 #define libssh2_crypto_init()
132 #define libssh2_rsa_ctx RSA
134 int _libssh2_rsa_new(libssh2_rsa_ctx
** rsa
,
135 const unsigned char *edata
,
137 const unsigned char *ndata
,
139 const unsigned char *ddata
,
141 const unsigned char *pdata
,
143 const unsigned char *qdata
,
145 const unsigned char *e1data
,
147 const unsigned char *e2data
,
149 const unsigned char *coeffdata
, unsigned long coefflen
);
150 int _libssh2_rsa_new_private(libssh2_rsa_ctx
** rsa
,
151 LIBSSH2_SESSION
* session
,
152 FILE * fp
, unsigned const char *passphrase
);
153 int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx
* rsa
,
154 const unsigned char *sig
,
155 unsigned long sig_len
,
156 const unsigned char *m
, unsigned long m_len
);
157 int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION
* session
,
158 libssh2_rsa_ctx
* rsactx
,
159 const unsigned char *hash
,
160 unsigned long hash_len
,
161 unsigned char **signature
,
162 unsigned long *signature_len
);
164 #define _libssh2_rsa_free(rsactx) RSA_free(rsactx)
166 #define libssh2_dsa_ctx DSA
168 int _libssh2_dsa_new(libssh2_dsa_ctx
** dsa
,
169 const unsigned char *pdata
,
171 const unsigned char *qdata
,
173 const unsigned char *gdata
,
175 const unsigned char *ydata
,
177 const unsigned char *x
, unsigned long x_len
);
178 int _libssh2_dsa_new_private(libssh2_dsa_ctx
** dsa
,
179 LIBSSH2_SESSION
* session
,
180 FILE * fp
, unsigned const char *passphrase
);
181 int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx
* dsactx
,
182 const unsigned char *sig
,
183 const unsigned char *m
, unsigned long m_len
);
184 int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx
* dsactx
,
185 const unsigned char *hash
,
186 unsigned long hash_len
, unsigned char *sig
);
188 #define _libssh2_dsa_free(dsactx) DSA_free(dsactx)
190 #define _libssh2_cipher_type(name) const EVP_CIPHER *(*name)(void)
191 #define _libssh2_cipher_ctx EVP_CIPHER_CTX
193 #define _libssh2_cipher_aes256 EVP_aes_256_cbc
194 #define _libssh2_cipher_aes192 EVP_aes_192_cbc
195 #define _libssh2_cipher_aes128 EVP_aes_128_cbc
196 #define _libssh2_cipher_blowfish EVP_bf_cbc
197 #define _libssh2_cipher_arcfour EVP_rc4
198 #define _libssh2_cipher_cast5 EVP_cast5_cbc
199 #define _libssh2_cipher_3des EVP_des_ede3_cbc
201 int _libssh2_cipher_init(_libssh2_cipher_ctx
* h
,
202 _libssh2_cipher_type(algo
),
204 unsigned char *secret
, int encrypt
);
206 int _libssh2_cipher_crypt(_libssh2_cipher_ctx
* ctx
,
207 _libssh2_cipher_type(algo
),
208 int encrypt
, unsigned char *block
);
210 #define _libssh2_cipher_dtor(ctx) EVP_CIPHER_CTX_cleanup(ctx)
212 #define _libssh2_bn BIGNUM
213 #define _libssh2_bn_ctx BN_CTX
214 #define _libssh2_bn_ctx_new() BN_CTX_new()
215 #define _libssh2_bn_ctx_free(bnctx) BN_CTX_free(bnctx)
216 #define _libssh2_bn_init() BN_new()
217 #define _libssh2_bn_rand(bn, bits, top, bottom) BN_rand(bn, bits, top, bottom)
218 #define _libssh2_bn_mod_exp(r, a, p, m, ctx) BN_mod_exp(r, a, p, m, ctx)
219 #define _libssh2_bn_set_word(bn, val) BN_set_word(bn, val)
220 #define _libssh2_bn_from_bin(bn, len, val) BN_bin2bn(val, len, bn)
221 #define _libssh2_bn_to_bin(bn, val) BN_bn2bin(bn, val)
222 #define _libssh2_bn_bytes(bn) BN_num_bytes(bn)
223 #define _libssh2_bn_bits(bn) BN_num_bits(bn)
224 #define _libssh2_bn_free(bn) BN_clear_free(bn)