We now build libssh2 in Xcode and it's a much better UB/10.4 citizen
[printdrop.git] / Vendor / libssh2 / Source / mac.c
1 /* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org>
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms,
5 * with or without modification, are permitted provided
6 * that the following conditions are met:
7 *
8 * Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the
10 * following disclaimer.
11 *
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.
16 *
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.
21 *
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
35 * OF SUCH DAMAGE.
36 */
37
38 #include "libssh2_priv.h"
39
40 #ifdef LIBSSH2_MAC_NONE
41 /* {{{ libssh2_mac_none_MAC
42 * Minimalist MAC: No MAC
43 */
44 static int
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)
49 {
50 return 0;
51 }
52
53 /* }}} */
54
55
56 static LIBSSH2_MAC_METHOD libssh2_mac_method_none = {
57 "none",
58 0,
59 0,
60 NULL,
61 libssh2_mac_none_MAC,
62 NULL
63 };
64 #endif /* LIBSSH2_MAC_NONE */
65
66 /* {{{ libssh2_mac_method_common_init
67 * Initialize simple mac methods
68 */
69 static int
70 libssh2_mac_method_common_init(LIBSSH2_SESSION * session, unsigned char *key,
71 int *free_key, void **abstract)
72 {
73 *abstract = key;
74 *free_key = 0;
75 (void) session;
76
77 return 0;
78 }
79
80 /* }}} */
81
82 /* {{{ libssh2_mac_method_common_dtor
83 * Cleanup simple mac methods
84 */
85 static int
86 libssh2_mac_method_common_dtor(LIBSSH2_SESSION * session, void **abstract)
87 {
88 if (*abstract) {
89 LIBSSH2_FREE(session, *abstract);
90 }
91 *abstract = NULL;
92
93 return 0;
94 }
95
96 /* }}} */
97
98 /* {{{ libssh2_mac_method_hmac_sha1_hash
99 * Calculate hash using full sha1 value
100 */
101 static int
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)
108 {
109 libssh2_hmac_ctx ctx;
110 unsigned char seqno_buf[4];
111 (void) session;
112
113 libssh2_htonu32(seqno_buf, seqno);
114
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);
120 }
121 libssh2_hmac_final(ctx, buf);
122 libssh2_hmac_cleanup(&ctx);
123
124 return 0;
125 }
126
127 /* }}} */
128
129 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_sha1 = {
130 "hmac-sha1",
131 20,
132 20,
133 libssh2_mac_method_common_init,
134 libssh2_mac_method_hmac_sha1_hash,
135 libssh2_mac_method_common_dtor,
136 };
137
138 /* {{{ libssh2_mac_method_hmac_sha1_96_hash
139 * Calculate hash using first 96 bits of sha1 value
140 */
141 static int
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)
148 {
149 unsigned char temp[SHA_DIGEST_LENGTH];
150
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);
154
155 return 0;
156 }
157
158 /* }}} */
159
160 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_sha1_96 = {
161 "hmac-sha1-96",
162 12,
163 20,
164 libssh2_mac_method_common_init,
165 libssh2_mac_method_hmac_sha1_96_hash,
166 libssh2_mac_method_common_dtor,
167 };
168
169 /* {{{ libssh2_mac_method_hmac_md5_hash
170 * Calculate hash using full md5 value
171 */
172 static int
173 libssh2_mac_method_hmac_md5_hash(LIBSSH2_SESSION * session, unsigned char *buf,
174 unsigned long seqno,
175 const unsigned char *packet,
176 unsigned long packet_len,
177 const unsigned char *addtl,
178 unsigned long addtl_len, void **abstract)
179 {
180 libssh2_hmac_ctx ctx;
181 unsigned char seqno_buf[4];
182 (void) session;
183
184 libssh2_htonu32(seqno_buf, seqno);
185
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);
191 }
192 libssh2_hmac_final(ctx, buf);
193 libssh2_hmac_cleanup(&ctx);
194
195 return 0;
196 }
197
198 /* }}} */
199
200 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_md5 = {
201 "hmac-md5",
202 16,
203 16,
204 libssh2_mac_method_common_init,
205 libssh2_mac_method_hmac_md5_hash,
206 libssh2_mac_method_common_dtor,
207 };
208
209 /* {{{ libssh2_mac_method_hmac_md5_96_hash
210 * Calculate hash using first 96 bits of md5 value
211 */
212 static int
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)
219 {
220 unsigned char temp[MD5_DIGEST_LENGTH];
221
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);
225
226 return 0;
227 }
228
229 /* }}} */
230
231 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_md5_96 = {
232 "hmac-md5-96",
233 12,
234 16,
235 libssh2_mac_method_common_init,
236 libssh2_mac_method_hmac_md5_96_hash,
237 libssh2_mac_method_common_dtor,
238 };
239
240 #if LIBSSH2_HMAC_RIPEMD
241 /* {{{ libssh2_mac_method_hmac_ripemd160_hash
242 * Calculate hash using ripemd160 value
243 */
244 static int
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,
251 void **abstract)
252 {
253 libssh2_hmac_ctx ctx;
254 unsigned char seqno_buf[4];
255 (void) session;
256
257 libssh2_htonu32(seqno_buf, seqno);
258
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);
264 }
265 libssh2_hmac_final(ctx, buf);
266 libssh2_hmac_cleanup(&ctx);
267
268 return 0;
269 }
270
271 /* }}} */
272
273 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_ripemd160 = {
274 "hmac-ripemd160",
275 20,
276 20,
277 libssh2_mac_method_common_init,
278 libssh2_mac_method_hmac_ripemd160_hash,
279 libssh2_mac_method_common_dtor,
280 };
281
282 static const LIBSSH2_MAC_METHOD libssh2_mac_method_hmac_ripemd160_openssh_com = {
283 "hmac-ripemd160@openssh.com",
284 20,
285 20,
286 libssh2_mac_method_common_init,
287 libssh2_mac_method_hmac_ripemd160_hash,
288 libssh2_mac_method_common_dtor,
289 };
290 #endif /* LIBSSH2_HMAC_RIPEMD */
291
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 */
304 NULL
305 };
306
307 const LIBSSH2_MAC_METHOD **
308 libssh2_mac_methods(void)
309 {
310 return _libssh2_mac_methods;
311 }