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
42 #define LIBSSH2_HMAC_RIPEMD 1
45 #define LIBSSH2_BLOWFISH 1
47 #define LIBSSH2_CAST 1
48 #define LIBSSH2_3DES 1
53 #define MD5_DIGEST_LENGTH 16
54 #define SHA_DIGEST_LENGTH 20
56 #define libssh2_random(buf, len) \
57 (gcry_randomize ((buf), (len), GCRY_STRONG_RANDOM), 1)
59 #define libssh2_sha1_ctx gcry_md_hd_t
60 #define libssh2_sha1_init(ctx) gcry_md_open (ctx, GCRY_MD_SHA1, 0);
61 #define libssh2_sha1_update(ctx, data, len) gcry_md_write (ctx, data, len)
62 #define libssh2_sha1_final(ctx, out) \
63 memcpy (out, gcry_md_read (ctx, 0), 20), gcry_md_close (ctx)
64 #define libssh2_sha1(message, len, out) \
65 gcry_md_hash_buffer (GCRY_MD_SHA1, out, message, len)
67 #define libssh2_md5_ctx gcry_md_hd_t
68 #define libssh2_md5_init(ctx) gcry_md_open (ctx, GCRY_MD_MD5, 0);
69 #define libssh2_md5_update(ctx, data, len) gcry_md_write (ctx, data, len)
70 #define libssh2_md5_final(ctx, out) \
71 memcpy (out, gcry_md_read (ctx, 0), 20), gcry_md_close (ctx)
72 #define libssh2_md5(message, len, out) \
73 gcry_md_hash_buffer (GCRY_MD_MD5, out, message, len)
75 #define libssh2_hmac_ctx gcry_md_hd_t
76 #define libssh2_hmac_sha1_init(ctx, key, keylen) \
77 gcry_md_open (ctx, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC), \
78 gcry_md_setkey (*ctx, key, keylen)
79 #define libssh2_hmac_md5_init(ctx, key, keylen) \
80 gcry_md_open (ctx, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC), \
81 gcry_md_setkey (*ctx, key, keylen)
82 #define libssh2_hmac_ripemd160_init(ctx, key, keylen) \
83 gcry_md_open (ctx, GCRY_MD_RMD160, GCRY_MD_FLAG_HMAC), \
84 gcry_md_setkey (*ctx, key, keylen)
85 #define libssh2_hmac_update(ctx, data, datalen) \
86 gcry_md_write (ctx, data, datalen)
87 #define libssh2_hmac_final(ctx, data) \
88 memcpy (data, gcry_md_read (ctx, 0), \
89 gcry_md_get_algo_dlen (gcry_md_get_algo (ctx)))
90 #define libssh2_hmac_cleanup(ctx) gcry_md_close (*ctx);
92 #define libssh2_crypto_init() gcry_control (GCRYCTL_DISABLE_SECMEM)
94 #define libssh2_rsa_ctx struct gcry_sexp
96 int _libssh2_rsa_new(libssh2_rsa_ctx
** rsa
,
97 const unsigned char *edata
,
99 const unsigned char *ndata
,
101 const unsigned char *ddata
,
103 const unsigned char *pdata
,
105 const unsigned char *qdata
,
107 const unsigned char *e1data
,
109 const unsigned char *e2data
,
111 const unsigned char *coeffdata
, unsigned long coefflen
);
112 int _libssh2_rsa_new_private(libssh2_rsa_ctx
** rsa
,
113 LIBSSH2_SESSION
* session
,
114 FILE * fp
, unsigned const char *passphrase
);
115 int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx
* rsa
,
116 const unsigned char *sig
,
117 unsigned long sig_len
,
118 const unsigned char *m
, unsigned long m_len
);
119 int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION
* session
,
120 libssh2_rsa_ctx
* rsactx
,
121 const unsigned char *hash
,
122 unsigned long hash_len
,
123 unsigned char **signature
,
124 unsigned long *signature_len
);
126 #define _libssh2_rsa_free(rsactx) gcry_sexp_release (rsactx)
128 #define libssh2_dsa_ctx struct gcry_sexp
130 int _libssh2_dsa_new(libssh2_dsa_ctx
** dsa
,
131 const unsigned char *pdata
,
133 const unsigned char *qdata
,
135 const unsigned char *gdata
,
137 const unsigned char *ydata
,
139 const unsigned char *x
, unsigned long x_len
);
140 int _libssh2_dsa_new_private(libssh2_dsa_ctx
** dsa
,
141 LIBSSH2_SESSION
* session
,
142 FILE * fp
, unsigned const char *passphrase
);
143 int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx
* dsa
,
144 const unsigned char *sig
,
145 const unsigned char *m
, unsigned long m_len
);
146 int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx
* dsactx
,
147 const unsigned char *hash
,
148 unsigned long hash_len
, unsigned char *sig
);
150 #define _libssh2_dsa_free(dsactx) gcry_sexp_release (dsactx)
152 #define _libssh2_cipher_type(name) int name
153 #define _libssh2_cipher_ctx gcry_cipher_hd_t
155 #define _libssh2_cipher_aes256 GCRY_CIPHER_AES256
156 #define _libssh2_cipher_aes192 GCRY_CIPHER_AES192
157 #define _libssh2_cipher_aes128 GCRY_CIPHER_AES128
158 #define _libssh2_cipher_blowfish GCRY_CIPHER_BLOWFISH
159 #define _libssh2_cipher_arcfour GCRY_CIPHER_ARCFOUR
160 #define _libssh2_cipher_cast5 GCRY_CIPHER_CAST5
161 #define _libssh2_cipher_3des GCRY_CIPHER_3DES
163 int _libssh2_cipher_init(_libssh2_cipher_ctx
* h
,
164 _libssh2_cipher_type(algo
),
166 unsigned char *secret
, int encrypt
);
168 int _libssh2_cipher_crypt(_libssh2_cipher_ctx
* ctx
,
169 _libssh2_cipher_type(algo
),
170 int encrypt
, unsigned char *block
);
172 #define _libssh2_cipher_dtor(ctx) gcry_cipher_close(*(ctx))
174 #define _libssh2_bn struct gcry_mpi
175 #define _libssh2_bn_ctx int
176 #define _libssh2_bn_ctx_new() 0
177 #define _libssh2_bn_ctx_free(bnctx) 0
178 #define _libssh2_bn_init() gcry_mpi_new(0)
179 #define _libssh2_bn_rand(bn, bits, top, bottom) gcry_mpi_randomize (bn, bits, GCRY_WEAK_RANDOM)
180 #define _libssh2_bn_mod_exp(r, a, p, m, ctx) gcry_mpi_powm (r, a, p, m)
181 #define _libssh2_bn_set_word(bn, val) gcry_mpi_set_ui(bn, val)
182 #define _libssh2_bn_from_bin(bn, len, val) gcry_mpi_scan(&((bn)), GCRYMPI_FMT_USG, val, len, NULL)
183 #define _libssh2_bn_to_bin(bn, val) gcry_mpi_print (GCRYMPI_FMT_USG, val, _libssh2_bn_bytes(bn), NULL, bn)
184 #define _libssh2_bn_bytes(bn) (gcry_mpi_get_nbits (bn) / 8 + ((gcry_mpi_get_nbits (bn) % 8 == 0) ? 0 : 1))
185 #define _libssh2_bn_bits(bn) gcry_mpi_get_nbits (bn)
186 #define _libssh2_bn_free(bn) gcry_mpi_release(bn)