1 /* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org>
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 "libssh2_priv.h"
40 #ifdef LIBSSH2_MAC_NONE
41 /* {{{ libssh2_mac_none_MAC
42 * Minimalist MAC: No MAC
45 libssh2_mac_none_MAC(LIBSSH2_SESSION
* session
, unsigned char *buf
,
46 unsigned long seqno
, const unsigned char *packet
,
47 unsigned long packet_len
, const unsigned char *addtl
,
48 unsigned long addtl_len
, void **abstract
)
56 static LIBSSH2_MAC_METHOD libssh2_mac_method_none
= {
64 #endif /* LIBSSH2_MAC_NONE */
66 /* {{{ libssh2_mac_method_common_init
67 * Initialize simple mac methods
70 libssh2_mac_method_common_init(LIBSSH2_SESSION
* session
, unsigned char *key
,
71 int *free_key
, void **abstract
)
82 /* {{{ libssh2_mac_method_common_dtor
83 * Cleanup simple mac methods
86 libssh2_mac_method_common_dtor(LIBSSH2_SESSION
* session
, void **abstract
)
89 LIBSSH2_FREE(session
, *abstract
);
98 /* {{{ libssh2_mac_method_hmac_sha1_hash
99 * Calculate hash using full sha1 value
102 libssh2_mac_method_hmac_sha1_hash(LIBSSH2_SESSION
* session
,
103 unsigned char *buf
, unsigned long seqno
,
104 const unsigned char *packet
,
105 unsigned long packet_len
,
106 const unsigned char *addtl
,
107 unsigned long addtl_len
, void **abstract
)
109 libssh2_hmac_ctx ctx
;
110 unsigned char seqno_buf
[4];
113 libssh2_htonu32(seqno_buf
, seqno
);
115 libssh2_hmac_sha1_init(&ctx
, *abstract
, 20);
116 libssh2_hmac_update(ctx
, seqno_buf
, 4);
117 libssh2_hmac_update(ctx
, packet
, packet_len
);
118 if (addtl
&& addtl_len
) {
119 libssh2_hmac_update(ctx
, addtl
, addtl_len
);
121 libssh2_hmac_final(ctx
, buf
);
122 libssh2_hmac_cleanup(&ctx
);
129 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_sha1
= {
133 libssh2_mac_method_common_init
,
134 libssh2_mac_method_hmac_sha1_hash
,
135 libssh2_mac_method_common_dtor
,
138 /* {{{ libssh2_mac_method_hmac_sha1_96_hash
139 * Calculate hash using first 96 bits of sha1 value
142 libssh2_mac_method_hmac_sha1_96_hash(LIBSSH2_SESSION
* session
,
143 unsigned char *buf
, unsigned long seqno
,
144 const unsigned char *packet
,
145 unsigned long packet_len
,
146 const unsigned char *addtl
,
147 unsigned long addtl_len
, void **abstract
)
149 unsigned char temp
[SHA_DIGEST_LENGTH
];
151 libssh2_mac_method_hmac_sha1_hash(session
, temp
, seqno
, packet
, packet_len
,
152 addtl
, addtl_len
, abstract
);
153 memcpy(buf
, (char *) temp
, 96 / 8);
160 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_sha1_96
= {
164 libssh2_mac_method_common_init
,
165 libssh2_mac_method_hmac_sha1_96_hash
,
166 libssh2_mac_method_common_dtor
,
169 /* {{{ libssh2_mac_method_hmac_md5_hash
170 * Calculate hash using full md5 value
173 libssh2_mac_method_hmac_md5_hash(LIBSSH2_SESSION
* session
, unsigned char *buf
,
175 const unsigned char *packet
,
176 unsigned long packet_len
,
177 const unsigned char *addtl
,
178 unsigned long addtl_len
, void **abstract
)
180 libssh2_hmac_ctx ctx
;
181 unsigned char seqno_buf
[4];
184 libssh2_htonu32(seqno_buf
, seqno
);
186 libssh2_hmac_md5_init(&ctx
, *abstract
, 16);
187 libssh2_hmac_update(ctx
, seqno_buf
, 4);
188 libssh2_hmac_update(ctx
, packet
, packet_len
);
189 if (addtl
&& addtl_len
) {
190 libssh2_hmac_update(ctx
, addtl
, addtl_len
);
192 libssh2_hmac_final(ctx
, buf
);
193 libssh2_hmac_cleanup(&ctx
);
200 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_md5
= {
204 libssh2_mac_method_common_init
,
205 libssh2_mac_method_hmac_md5_hash
,
206 libssh2_mac_method_common_dtor
,
209 /* {{{ libssh2_mac_method_hmac_md5_96_hash
210 * Calculate hash using first 96 bits of md5 value
213 libssh2_mac_method_hmac_md5_96_hash(LIBSSH2_SESSION
* session
,
214 unsigned char *buf
, unsigned long seqno
,
215 const unsigned char *packet
,
216 unsigned long packet_len
,
217 const unsigned char *addtl
,
218 unsigned long addtl_len
, void **abstract
)
220 unsigned char temp
[MD5_DIGEST_LENGTH
];
222 libssh2_mac_method_hmac_md5_hash(session
, temp
, seqno
, packet
, packet_len
,
223 addtl
, addtl_len
, abstract
);
224 memcpy(buf
, (char *) temp
, 96 / 8);
231 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_md5_96
= {
235 libssh2_mac_method_common_init
,
236 libssh2_mac_method_hmac_md5_96_hash
,
237 libssh2_mac_method_common_dtor
,
240 #if LIBSSH2_HMAC_RIPEMD
241 /* {{{ libssh2_mac_method_hmac_ripemd160_hash
242 * Calculate hash using ripemd160 value
245 libssh2_mac_method_hmac_ripemd160_hash(LIBSSH2_SESSION
* session
,
246 unsigned char *buf
, unsigned long seqno
,
247 const unsigned char *packet
,
248 unsigned long packet_len
,
249 const unsigned char *addtl
,
250 unsigned long addtl_len
,
253 libssh2_hmac_ctx ctx
;
254 unsigned char seqno_buf
[4];
257 libssh2_htonu32(seqno_buf
, seqno
);
259 libssh2_hmac_ripemd160_init(&ctx
, *abstract
, 20);
260 libssh2_hmac_update(ctx
, seqno_buf
, 4);
261 libssh2_hmac_update(ctx
, packet
, packet_len
);
262 if (addtl
&& addtl_len
) {
263 libssh2_hmac_update(ctx
, addtl
, addtl_len
);
265 libssh2_hmac_final(ctx
, buf
);
266 libssh2_hmac_cleanup(&ctx
);
273 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_ripemd160
= {
277 libssh2_mac_method_common_init
,
278 libssh2_mac_method_hmac_ripemd160_hash
,
279 libssh2_mac_method_common_dtor
,
282 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_ripemd160_openssh_com
= {
283 "hmac-ripemd160@openssh.com",
286 libssh2_mac_method_common_init
,
287 libssh2_mac_method_hmac_ripemd160_hash
,
288 libssh2_mac_method_common_dtor
,
290 #endif /* LIBSSH2_HMAC_RIPEMD */
292 static const LIBSSH2_MAC_METHOD
*_libssh2_mac_methods
[] = {
293 &libssh2_mac_method_hmac_sha1
,
294 &libssh2_mac_method_hmac_sha1_96
,
295 &libssh2_mac_method_hmac_md5
,
296 &libssh2_mac_method_hmac_md5_96
,
297 #ifdef LIBSSH2_HMAC_RIPEMD
298 &libssh2_mac_method_hmac_ripemd160
,
299 &libssh2_mac_method_hmac_ripemd160_openssh_com
,
300 #endif /* LIBSSH2_HMAC_RIPEMD */
301 #ifdef LIBSSH2_MAC_NONE
302 &libssh2_mac_method_none
,
303 #endif /* LIBSSH2_MAC_NONE */
307 const LIBSSH2_MAC_METHOD
**
308 libssh2_mac_methods(void)
310 return _libssh2_mac_methods
;