summaryrefslogtreecommitdiff
path: root/regress/unittests/sshbuf/test_sshbuf_getput_crypto.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2014-05-15 15:17:15 +1000
committerDamien Miller <djm@mindrot.org>2014-05-15 15:17:15 +1000
commitdef1de086707b0e6b046fe7e115c60aca0227a99 (patch)
tree16a736c080243e1e80fd1ea850ca0e88d657c2cc /regress/unittests/sshbuf/test_sshbuf_getput_crypto.c
parent167685756fde8bc213a8df2c8e1848e312db0f46 (diff)
- (djm) [regress/unittests/Makefile]
[regress/unittests/Makefile.inc] [regress/unittests/sshbuf/Makefile] [regress/unittests/sshbuf/test_sshbuf.c] [regress/unittests/sshbuf/test_sshbuf_fixed.c] [regress/unittests/sshbuf/test_sshbuf_fuzz.c] [regress/unittests/sshbuf/test_sshbuf_getput_basic.c] [regress/unittests/sshbuf/test_sshbuf_getput_crypto.c] [regress/unittests/sshbuf/test_sshbuf_getput_fuzz.c] [regress/unittests/sshbuf/test_sshbuf_misc.c] [regress/unittests/sshbuf/tests.c] [regress/unittests/test_helper/Makefile] [regress/unittests/test_helper/fuzz.c] [regress/unittests/test_helper/test_helper.c] [regress/unittests/test_helper/test_helper.h] Import new unit tests from OpenBSD; not yet hooked up to build.
Diffstat (limited to 'regress/unittests/sshbuf/test_sshbuf_getput_crypto.c')
-rw-r--r--regress/unittests/sshbuf/test_sshbuf_getput_crypto.c398
1 files changed, 398 insertions, 0 deletions
diff --git a/regress/unittests/sshbuf/test_sshbuf_getput_crypto.c b/regress/unittests/sshbuf/test_sshbuf_getput_crypto.c
new file mode 100644
index 000000000..d7d4dc378
--- /dev/null
+++ b/regress/unittests/sshbuf/test_sshbuf_getput_crypto.c
@@ -0,0 +1,398 @@
1/* $OpenBSD: test_sshbuf_getput_crypto.c,v 1.1 2014/04/30 05:32:00 djm Exp $ */
2/*
3 * Regress test for sshbuf.h buffer API
4 *
5 * Placed in the public domain
6 */
7
8#include <sys/types.h>
9#include <sys/param.h>
10#include <stdio.h>
11#include <stdint.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include <openssl/bn.h>
16#include <openssl/ec.h>
17#include <openssl/objects.h>
18
19#include "test_helper.h"
20#include "ssherr.h"
21#include "sshbuf.h"
22
23void sshbuf_getput_crypto_tests(void);
24
25void
26sshbuf_getput_crypto_tests(void)
27{
28 struct sshbuf *p1;
29 const u_char *d;
30 size_t s;
31 BIGNUM *bn, *bn2, *bn_x, *bn_y;
32 /* This one has num_bits != num_bytes * 8 to test bignum1 encoding */
33 const char *hexbn1 = "0102030405060708090a0b0c0d0e0f10";
34 /* This one has MSB set to test bignum2 encoding negative-avoidance */
35 const char *hexbn2 = "f0e0d0c0b0a0908070605040302010007fff11";
36 u_char expbn1[] = {
37 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
38 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
39 };
40 u_char expbn2[] = {
41 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0, 0x90, 0x80,
42 0x70, 0x60, 0x50, 0x40, 0x30, 0x20, 0x10, 0x00,
43 0x7f, 0xff, 0x11
44 };
45 int ec256_nid = NID_X9_62_prime256v1;
46 char *ec256_x = "0C828004839D0106AA59575216191357"
47 "34B451459DADB586677EF9DF55784999";
48 char *ec256_y = "4D196B50F0B4E94B3C73E3A9D4CD9DF2"
49 "C8F9A35E42BDD047550F69D80EC23CD4";
50 u_char expec256[] = {
51 0x04,
52 0x0c, 0x82, 0x80, 0x04, 0x83, 0x9d, 0x01, 0x06,
53 0xaa, 0x59, 0x57, 0x52, 0x16, 0x19, 0x13, 0x57,
54 0x34, 0xb4, 0x51, 0x45, 0x9d, 0xad, 0xb5, 0x86,
55 0x67, 0x7e, 0xf9, 0xdf, 0x55, 0x78, 0x49, 0x99,
56 0x4d, 0x19, 0x6b, 0x50, 0xf0, 0xb4, 0xe9, 0x4b,
57 0x3c, 0x73, 0xe3, 0xa9, 0xd4, 0xcd, 0x9d, 0xf2,
58 0xc8, 0xf9, 0xa3, 0x5e, 0x42, 0xbd, 0xd0, 0x47,
59 0x55, 0x0f, 0x69, 0xd8, 0x0e, 0xc2, 0x3c, 0xd4
60 };
61 EC_KEY *eck;
62 EC_POINT *ecp;
63 int r;
64
65#define MKBN(b, bnn) \
66 do { \
67 bnn = NULL; \
68 ASSERT_INT_GT(BN_hex2bn(&bnn, b), 0); \
69 } while (0)
70
71 TEST_START("sshbuf_put_bignum1");
72 MKBN(hexbn1, bn);
73 p1 = sshbuf_new();
74 ASSERT_PTR_NE(p1, NULL);
75 ASSERT_INT_EQ(sshbuf_put_bignum1(p1, bn), 0);
76 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn1) + 2);
77 ASSERT_U16_EQ(PEEK_U16(sshbuf_ptr(p1)), (u_int16_t)BN_num_bits(bn));
78 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 2, expbn1, sizeof(expbn1));
79 BN_free(bn);
80 sshbuf_free(p1);
81 TEST_DONE();
82
83 TEST_START("sshbuf_put_bignum1 limited");
84 MKBN(hexbn1, bn);
85 p1 = sshbuf_new();
86 ASSERT_PTR_NE(p1, NULL);
87 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(expbn1) + 1), 0);
88 r = sshbuf_put_bignum1(p1, bn);
89 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
90 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
91 BN_free(bn);
92 sshbuf_free(p1);
93 TEST_DONE();
94
95 TEST_START("sshbuf_put_bignum1 bn2");
96 MKBN(hexbn2, bn);
97 p1 = sshbuf_new();
98 ASSERT_PTR_NE(p1, NULL);
99 ASSERT_INT_EQ(sshbuf_put_bignum1(p1, bn), 0);
100 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 2);
101 ASSERT_U16_EQ(PEEK_U16(sshbuf_ptr(p1)), (u_int16_t)BN_num_bits(bn));
102 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 2, expbn2, sizeof(expbn2));
103 BN_free(bn);
104 sshbuf_free(p1);
105 TEST_DONE();
106
107 TEST_START("sshbuf_put_bignum1 bn2 limited");
108 MKBN(hexbn2, bn);
109 p1 = sshbuf_new();
110 ASSERT_PTR_NE(p1, NULL);
111 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(expbn1) + 1), 0);
112 r = sshbuf_put_bignum1(p1, bn);
113 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
114 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
115 BN_free(bn);
116 sshbuf_free(p1);
117 TEST_DONE();
118
119 TEST_START("sshbuf_put_bignum2");
120 MKBN(hexbn1, bn);
121 p1 = sshbuf_new();
122 ASSERT_PTR_NE(p1, NULL);
123 ASSERT_INT_EQ(sshbuf_put_bignum2(p1, bn), 0);
124 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn1) + 4);
125 ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), (u_int32_t)BN_num_bytes(bn));
126 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 4, expbn1, sizeof(expbn1));
127 BN_free(bn);
128 sshbuf_free(p1);
129 TEST_DONE();
130
131 TEST_START("sshbuf_put_bignum2 limited");
132 MKBN(hexbn1, bn);
133 p1 = sshbuf_new();
134 ASSERT_PTR_NE(p1, NULL);
135 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(expbn1) + 3), 0);
136 r = sshbuf_put_bignum2(p1, bn);
137 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
138 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
139 BN_free(bn);
140 sshbuf_free(p1);
141 TEST_DONE();
142
143 TEST_START("sshbuf_put_bignum2 bn2");
144 MKBN(hexbn2, bn);
145 p1 = sshbuf_new();
146 ASSERT_PTR_NE(p1, NULL);
147 ASSERT_INT_EQ(sshbuf_put_bignum2(p1, bn), 0);
148 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 4 + 1); /* MSB */
149 ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), (u_int32_t)BN_num_bytes(bn) + 1);
150 ASSERT_U8_EQ(*(sshbuf_ptr(p1) + 4), 0x00);
151 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 5, expbn2, sizeof(expbn2));
152 BN_free(bn);
153 sshbuf_free(p1);
154 TEST_DONE();
155
156 TEST_START("sshbuf_put_bignum2 bn2 limited");
157 MKBN(hexbn2, bn);
158 p1 = sshbuf_new();
159 ASSERT_PTR_NE(p1, NULL);
160 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(expbn2) + 3), 0);
161 r = sshbuf_put_bignum2(p1, bn);
162 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
163 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
164 BN_free(bn);
165 sshbuf_free(p1);
166 TEST_DONE();
167
168 TEST_START("sshbuf_get_bignum1");
169 MKBN(hexbn1, bn);
170 p1 = sshbuf_new();
171 ASSERT_PTR_NE(p1, NULL);
172 ASSERT_INT_EQ(sshbuf_put_u16(p1, BN_num_bits(bn)), 0);
173 ASSERT_INT_EQ(sshbuf_put(p1, expbn1, sizeof(expbn1)), 0);
174 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2 + sizeof(expbn1));
175 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0xd00f), 0);
176 bn2 = BN_new();
177 ASSERT_INT_EQ(sshbuf_get_bignum1(p1, bn2), 0);
178 ASSERT_BIGNUM_EQ(bn, bn2);
179 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
180 BN_free(bn);
181 BN_free(bn2);
182 sshbuf_free(p1);
183 TEST_DONE();
184
185 TEST_START("sshbuf_get_bignum1 truncated");
186 MKBN(hexbn1, bn);
187 p1 = sshbuf_new();
188 ASSERT_PTR_NE(p1, NULL);
189 ASSERT_INT_EQ(sshbuf_put_u16(p1, BN_num_bits(bn)), 0);
190 ASSERT_INT_EQ(sshbuf_put(p1, expbn1, sizeof(expbn1) - 1), 0);
191 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2 + sizeof(expbn1) - 1);
192 bn2 = BN_new();
193 r = sshbuf_get_bignum1(p1, bn2);
194 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
195 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2 + sizeof(expbn1) - 1);
196 BN_free(bn);
197 BN_free(bn2);
198 sshbuf_free(p1);
199 TEST_DONE();
200
201 TEST_START("sshbuf_get_bignum1 giant");
202 MKBN(hexbn1, bn);
203 p1 = sshbuf_new();
204 ASSERT_PTR_NE(p1, NULL);
205 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0xffff), 0);
206 ASSERT_INT_EQ(sshbuf_reserve(p1, (0xffff + 7) / 8, NULL), 0);
207 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2 + ((0xffff + 7) / 8));
208 bn2 = BN_new();
209 r = sshbuf_get_bignum1(p1, bn2);
210 ASSERT_INT_EQ(r, SSH_ERR_BIGNUM_TOO_LARGE);
211 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2 + ((0xffff + 7) / 8));
212 BN_free(bn);
213 BN_free(bn2);
214 sshbuf_free(p1);
215 TEST_DONE();
216
217 TEST_START("sshbuf_get_bignum1 bn2");
218 MKBN(hexbn2, bn);
219 p1 = sshbuf_new();
220 ASSERT_PTR_NE(p1, NULL);
221 ASSERT_INT_EQ(sshbuf_put_u16(p1, BN_num_bits(bn)), 0);
222 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2)), 0);
223 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2 + sizeof(expbn2));
224 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0xd00f), 0);
225 bn2 = BN_new();
226 ASSERT_INT_EQ(sshbuf_get_bignum1(p1, bn2), 0);
227 ASSERT_BIGNUM_EQ(bn, bn2);
228 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
229 BN_free(bn);
230 BN_free(bn2);
231 sshbuf_free(p1);
232 TEST_DONE();
233
234 TEST_START("sshbuf_get_bignum1 bn2 truncated");
235 MKBN(hexbn2, bn);
236 p1 = sshbuf_new();
237 ASSERT_PTR_NE(p1, NULL);
238 ASSERT_INT_EQ(sshbuf_put_u16(p1, BN_num_bits(bn)), 0);
239 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2) - 1), 0);
240 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2 + sizeof(expbn2) - 1);
241 bn2 = BN_new();
242 r = sshbuf_get_bignum1(p1, bn2);
243 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
244 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2 + sizeof(expbn2) - 1);
245 BN_free(bn);
246 BN_free(bn2);
247 sshbuf_free(p1);
248 TEST_DONE();
249
250 TEST_START("sshbuf_get_bignum2");
251 MKBN(hexbn1, bn);
252 p1 = sshbuf_new();
253 ASSERT_PTR_NE(p1, NULL);
254 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn)), 0);
255 ASSERT_INT_EQ(sshbuf_put(p1, expbn1, sizeof(expbn1)), 0);
256 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4 + sizeof(expbn1));
257 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0xd00f), 0);
258 bn2 = BN_new();
259 ASSERT_INT_EQ(sshbuf_get_bignum2(p1, bn2), 0);
260 ASSERT_BIGNUM_EQ(bn, bn2);
261 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
262 BN_free(bn);
263 BN_free(bn2);
264 sshbuf_free(p1);
265 TEST_DONE();
266
267 TEST_START("sshbuf_get_bignum2 truncated");
268 MKBN(hexbn1, bn);
269 p1 = sshbuf_new();
270 ASSERT_PTR_NE(p1, NULL);
271 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn)), 0);
272 ASSERT_INT_EQ(sshbuf_put(p1, expbn1, sizeof(expbn1) - 1), 0);
273 bn2 = BN_new();
274 r = sshbuf_get_bignum2(p1, bn2);
275 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
276 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn1) + 3);
277 BN_free(bn);
278 BN_free(bn2);
279 sshbuf_free(p1);
280 TEST_DONE();
281
282 TEST_START("sshbuf_get_bignum2 giant");
283 MKBN(hexbn1, bn);
284 p1 = sshbuf_new();
285 ASSERT_PTR_NE(p1, NULL);
286 ASSERT_INT_EQ(sshbuf_put_u32(p1, 65536), 0);
287 ASSERT_INT_EQ(sshbuf_reserve(p1, 65536, NULL), 0);
288 bn2 = BN_new();
289 r = sshbuf_get_bignum2(p1, bn2);
290 ASSERT_INT_EQ(r, SSH_ERR_BIGNUM_TOO_LARGE);
291 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 65536 + 4);
292 BN_free(bn);
293 BN_free(bn2);
294 sshbuf_free(p1);
295 TEST_DONE();
296
297 TEST_START("sshbuf_get_bignum2 bn2");
298 MKBN(hexbn2, bn);
299 p1 = sshbuf_new();
300 ASSERT_PTR_NE(p1, NULL);
301 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn) + 1), 0); /* MSB */
302 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x00), 0);
303 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2)), 0);
304 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4 + 1 + sizeof(expbn2));
305 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0xd00f), 0);
306 bn2 = BN_new();
307 ASSERT_INT_EQ(sshbuf_get_bignum2(p1, bn2), 0);
308 ASSERT_BIGNUM_EQ(bn, bn2);
309 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
310 BN_free(bn);
311 BN_free(bn2);
312 sshbuf_free(p1);
313 TEST_DONE();
314
315 TEST_START("sshbuf_get_bignum2 bn2 truncated");
316 MKBN(hexbn2, bn);
317 p1 = sshbuf_new();
318 ASSERT_PTR_NE(p1, NULL);
319 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn) + 1), 0);
320 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x00), 0);
321 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2) - 1), 0);
322 bn2 = BN_new();
323 r = sshbuf_get_bignum2(p1, bn2);
324 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
325 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 1 + 4 - 1);
326 BN_free(bn);
327 BN_free(bn2);
328 sshbuf_free(p1);
329 TEST_DONE();
330
331 TEST_START("sshbuf_get_bignum2 bn2 negative");
332 MKBN(hexbn2, bn);
333 p1 = sshbuf_new();
334 ASSERT_PTR_NE(p1, NULL);
335 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn)), 0);
336 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2)), 0);
337 bn2 = BN_new();
338 r = sshbuf_get_bignum2(p1, bn2);
339 ASSERT_INT_EQ(r, SSH_ERR_BIGNUM_IS_NEGATIVE);
340 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 4);
341 BN_free(bn);
342 BN_free(bn2);
343 sshbuf_free(p1);
344 TEST_DONE();
345
346 TEST_START("sshbuf_put_ec");
347 eck = EC_KEY_new_by_curve_name(ec256_nid);
348 ASSERT_PTR_NE(eck, NULL);
349 ecp = EC_POINT_new(EC_KEY_get0_group(eck));
350 ASSERT_PTR_NE(ecp, NULL);
351 MKBN(ec256_x, bn_x);
352 MKBN(ec256_y, bn_y);
353 ASSERT_INT_EQ(EC_POINT_set_affine_coordinates_GFp(
354 EC_KEY_get0_group(eck), ecp, bn_x, bn_y, NULL), 1);
355 ASSERT_INT_EQ(EC_KEY_set_public_key(eck, ecp), 1);
356 BN_free(bn_x);
357 BN_free(bn_y);
358 EC_POINT_free(ecp);
359 p1 = sshbuf_new();
360 ASSERT_PTR_NE(p1, NULL);
361 ASSERT_INT_EQ(sshbuf_put_eckey(p1, eck), 0);
362 ASSERT_INT_EQ(sshbuf_get_string_direct(p1, &d, &s), 0);
363 ASSERT_SIZE_T_EQ(s, sizeof(expec256));
364 ASSERT_MEM_EQ(d, expec256, sizeof(expec256));
365 sshbuf_free(p1);
366 EC_KEY_free(eck);
367 TEST_DONE();
368
369 TEST_START("sshbuf_get_ec");
370 eck = EC_KEY_new_by_curve_name(ec256_nid);
371 ASSERT_PTR_NE(eck, NULL);
372 p1 = sshbuf_new();
373 ASSERT_PTR_NE(p1, NULL);
374 ASSERT_INT_EQ(sshbuf_put_string(p1, expec256, sizeof(expec256)), 0);
375 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expec256) + 4);
376 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x00), 0);
377 ASSERT_INT_EQ(sshbuf_get_eckey(p1, eck), 0);
378 bn_x = BN_new();
379 bn_y = BN_new();
380 ASSERT_PTR_NE(bn_x, NULL);
381 ASSERT_PTR_NE(bn_y, NULL);
382 ASSERT_INT_EQ(EC_POINT_get_affine_coordinates_GFp(
383 EC_KEY_get0_group(eck), EC_KEY_get0_public_key(eck),
384 bn_x, bn_y, NULL), 1);
385 MKBN(ec256_x, bn);
386 MKBN(ec256_y, bn2);
387 ASSERT_INT_EQ(BN_cmp(bn_x, bn), 0);
388 ASSERT_INT_EQ(BN_cmp(bn_y, bn2), 0);
389 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
390 sshbuf_free(p1);
391 EC_KEY_free(eck);
392 BN_free(bn_x);
393 BN_free(bn_y);
394 BN_free(bn);
395 BN_free(bn2);
396 TEST_DONE();
397}
398