1 /* Copyright (C) 2009, 2010 Simon Josefsson
2  * Copyright (C) 2006, 2007 The Written Word, Inc.  All rights reserved.
3  * Copyright (C) 2010-2019 Daniel Stenberg
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  *   Redistributions of source code must retain the above
10  *   copyright notice, this list of conditions and the
11  *   following disclaimer.
12  *
13  *   Redistributions in binary form must reproduce the above
14  *   copyright notice, this list of conditions and the following
15  *   disclaimer in the documentation and/or other materials
16  *   provided with the distribution.
17  *
18  *   Neither the name of the copyright holder nor the names
19  *   of any other contributors may be used to endorse or
20  *   promote products derived from this software without
21  *   specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  */
38 #ifndef LIBSSH2_CRYPTO_H
39 #define LIBSSH2_CRYPTO_H
40 
41 #ifdef LIBSSH2_OPENSSL
42 #include "openssl.h"
43 #endif
44 
45 #ifdef LIBSSH2_LIBGCRYPT
46 #include "libgcrypt.h"
47 #endif
48 
49 #ifdef LIBSSH2_WINCNG
50 #include "wincng.h"
51 #endif
52 
53 #ifdef LIBSSH2_OS400QC3
54 #include "os400qc3.h"
55 #endif
56 
57 #ifdef LIBSSH2_MBEDTLS
58 #include "mbedtls.h"
59 #endif
60 
61 #define LIBSSH2_ED25519_KEY_LEN 32
62 #define LIBSSH2_ED25519_PRIVATE_KEY_LEN 64
63 #define LIBSSH2_ED25519_SIG_LEN 64
64 
65 #if LIBSSH2_RSA
66 int _libssh2_rsa_new(libssh2_rsa_ctx ** rsa,
67                      const unsigned char *edata,
68                      unsigned long elen,
69                      const unsigned char *ndata,
70                      unsigned long nlen,
71                      const unsigned char *ddata,
72                      unsigned long dlen,
73                      const unsigned char *pdata,
74                      unsigned long plen,
75                      const unsigned char *qdata,
76                      unsigned long qlen,
77                      const unsigned char *e1data,
78                      unsigned long e1len,
79                      const unsigned char *e2data,
80                      unsigned long e2len,
81                      const unsigned char *coeffdata, unsigned long coefflen);
82 int _libssh2_rsa_new_private(libssh2_rsa_ctx ** rsa,
83                              LIBSSH2_SESSION * session,
84                              const char *filename,
85                              unsigned const char *passphrase);
86 int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx * rsa,
87                              const unsigned char *sig,
88                              unsigned long sig_len,
89                              const unsigned char *m, unsigned long m_len);
90 int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session,
91                            libssh2_rsa_ctx * rsactx,
92                            const unsigned char *hash,
93                            size_t hash_len,
94                            unsigned char **signature,
95                            size_t *signature_len);
96 int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx ** rsa,
97                                         LIBSSH2_SESSION * session,
98                                         const char *filedata,
99                                         size_t filedata_len,
100                                         unsigned const char *passphrase);
101 #endif
102 
103 #if LIBSSH2_DSA
104 int _libssh2_dsa_new(libssh2_dsa_ctx ** dsa,
105                      const unsigned char *pdata,
106                      unsigned long plen,
107                      const unsigned char *qdata,
108                      unsigned long qlen,
109                      const unsigned char *gdata,
110                      unsigned long glen,
111                      const unsigned char *ydata,
112                      unsigned long ylen,
113                      const unsigned char *x, unsigned long x_len);
114 int _libssh2_dsa_new_private(libssh2_dsa_ctx ** dsa,
115                              LIBSSH2_SESSION * session,
116                              const char *filename,
117                              unsigned const char *passphrase);
118 int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx * dsactx,
119                              const unsigned char *sig,
120                              const unsigned char *m, unsigned long m_len);
121 int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx * dsactx,
122                            const unsigned char *hash,
123                            unsigned long hash_len, unsigned char *sig);
124 int _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx ** dsa,
125                                         LIBSSH2_SESSION * session,
126                                         const char *filedata,
127                                         size_t filedata_len,
128                                         unsigned const char *passphrase);
129 #endif
130 
131 #if LIBSSH2_ECDSA
132 int
133 _libssh2_ecdsa_curve_name_with_octal_new(libssh2_ecdsa_ctx ** ecdsactx,
134                                          const unsigned char *k,
135                                          size_t k_len,
136                                          libssh2_curve_type type);
137 int
138 _libssh2_ecdsa_new_private(libssh2_ecdsa_ctx ** ec_ctx,
139                            LIBSSH2_SESSION * session,
140                            const char *filename,
141                            unsigned const char *passphrase);
142 
143 int
144 _libssh2_ecdsa_verify(libssh2_ecdsa_ctx * ctx,
145                       const unsigned char *r, size_t r_len,
146                       const unsigned char *s, size_t s_len,
147                       const unsigned char *m, size_t m_len);
148 
149 int
150 _libssh2_ecdsa_create_key(LIBSSH2_SESSION *session,
151                           _libssh2_ec_key **out_private_key,
152                           unsigned char **out_public_key_octal,
153                           size_t *out_public_key_octal_len,
154                           libssh2_curve_type curve_type);
155 
156 int
157 _libssh2_ecdh_gen_k(_libssh2_bn **k, _libssh2_ec_key *private_key,
158                     const unsigned char *server_public_key,
159                     size_t server_public_key_len);
160 
161 int
162 _libssh2_ecdsa_sign(LIBSSH2_SESSION *session, libssh2_ecdsa_ctx *ec_ctx,
163                     const unsigned char *hash, unsigned long hash_len,
164                     unsigned char **signature, size_t *signature_len);
165 
166 int _libssh2_ecdsa_new_private_frommemory(libssh2_ecdsa_ctx ** ec_ctx,
167                                           LIBSSH2_SESSION * session,
168                                           const char *filedata,
169                                           size_t filedata_len,
170                                           unsigned const char *passphrase);
171 
172 libssh2_curve_type
173 _libssh2_ecdsa_get_curve_type(libssh2_ecdsa_ctx *ec_ctx);
174 
175 int
176 _libssh2_ecdsa_curve_type_from_name(const char *name,
177                                     libssh2_curve_type *out_type);
178 
179 #endif /* LIBSSH2_ECDSA */
180 
181 #if LIBSSH2_ED25519
182 
183 int
184 _libssh2_curve25519_new(LIBSSH2_SESSION *session, libssh2_ed25519_ctx **ctx,
185                         uint8_t **out_public_key, uint8_t **out_private_key);
186 
187 int
188 _libssh2_curve25519_gen_k(_libssh2_bn **k,
189                           uint8_t private_key[LIBSSH2_ED25519_KEY_LEN],
190                           uint8_t server_public_key[LIBSSH2_ED25519_KEY_LEN]);
191 
192 int
193 _libssh2_ed25519_verify(libssh2_ed25519_ctx *ctx, const uint8_t *s,
194                         size_t s_len, const uint8_t *m, size_t m_len);
195 
196 int
197 _libssh2_ed25519_new_private(libssh2_ed25519_ctx **ed_ctx,
198                             LIBSSH2_SESSION *session,
199                             const char *filename, const uint8_t *passphrase);
200 
201 int
202 _libssh2_ed25519_new_public(libssh2_ed25519_ctx **ed_ctx,
203                             LIBSSH2_SESSION *session,
204                             const unsigned char *raw_pub_key,
205                             const uint8_t key_len);
206 
207 int
208 _libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session,
209                       uint8_t **out_sig, size_t *out_sig_len,
210                       const uint8_t *message, size_t message_len);
211 
212 int
213 _libssh2_ed25519_new_private_frommemory(libssh2_ed25519_ctx **ed_ctx,
214                                         LIBSSH2_SESSION *session,
215                                         const char *filedata,
216                                         size_t filedata_len,
217                                         unsigned const char *passphrase);
218 
219 #endif /* LIBSSH2_ED25519 */
220 
221 
222 int _libssh2_cipher_init(_libssh2_cipher_ctx * h,
223                          _libssh2_cipher_type(algo),
224                          unsigned char *iv,
225                          unsigned char *secret, int encrypt);
226 
227 int _libssh2_cipher_crypt(_libssh2_cipher_ctx * ctx,
228                           _libssh2_cipher_type(algo),
229                           int encrypt, unsigned char *block, size_t blocksize);
230 
231 int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session,
232                               unsigned char **method,
233                               size_t *method_len,
234                               unsigned char **pubkeydata,
235                               size_t *pubkeydata_len,
236                               const char *privatekey,
237                               const char *passphrase);
238 
239 int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session,
240                                     unsigned char **method,
241                                     size_t *method_len,
242                                     unsigned char **pubkeydata,
243                                     size_t *pubkeydata_len,
244                                     const char *privatekeydata,
245                                     size_t privatekeydata_len,
246                                     const char *passphrase);
247 
248 #endif
249