Debian Codegraph @

regress/unittests/sshbuf/test_sshbuf_getput_crypto.c

raw
1 /* $OpenBSD: test_sshbuf_getput_crypto.c,v 1.5 2026/03/06 06:57:33 dtucker Exp $ */
2 /*
3 * Regress test for sshbuf.h buffer API
4 *
5 * Placed in the public domain
6 */
7
8 #include "includes.h"
9
10 #ifdef WITH_OPENSSL
11
12 #include <sys/types.h>
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include <openssl/bn.h>
19 #include <openssl/objects.h>
20 #ifdef OPENSSL_HAS_NISTP256
21 # include <openssl/ec.h>
22 #endif
23 #include "openbsd-compat/openssl-compat.h"
24
25 #include "../test_helper/test_helper.h"
26 #include "ssherr.h"
27 #include "sshbuf.h"
28
29 void sshbuf_getput_crypto_tests(void);
30
31 void
32 sshbuf_getput_crypto_tests(void)
33 {
34 struct sshbuf *p1;
35 BIGNUM *bn, *bn2;
36 const char *hexbn1 = "0102030405060708090a0b0c0d0e0f10";
37 /* This one has MSB set to test bignum2 encoding negative-avoidance */
38 const char *hexbn2 = "f0e0d0c0b0a0908070605040302010007fff11";
39 u_char expbn1[] = {
40 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
41 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
42 };
43 u_char expbn2[] = {
44 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0, 0x90, 0x80,
45 0x70, 0x60, 0x50, 0x40, 0x30, 0x20, 0x10, 0x00,
46 0x7f, 0xff, 0x11
47 };
48 #if defined(OPENSSL_HAS_ECC) && defined(OPENSSL_HAS_NISTP256)
49 const u_char *d;
50 size_t s;
51 BIGNUM *bn_x, *bn_y;
52 int ec256_nid = NID_X9_62_prime256v1;
53 char *ec256_x = "0C828004839D0106AA59575216191357"
54 "34B451459DADB586677EF9DF55784999";
55 char *ec256_y = "4D196B50F0B4E94B3C73E3A9D4CD9DF2"
56 "C8F9A35E42BDD047550F69D80EC23CD4";
57 u_char expec256[] = {
58 0x04,
59 0x0c, 0x82, 0x80, 0x04, 0x83, 0x9d, 0x01, 0x06,
60 0xaa, 0x59, 0x57, 0x52, 0x16, 0x19, 0x13, 0x57,
61 0x34, 0xb4, 0x51, 0x45, 0x9d, 0xad, 0xb5, 0x86,
62 0x67, 0x7e, 0xf9, 0xdf, 0x55, 0x78, 0x49, 0x99,
63 0x4d, 0x19, 0x6b, 0x50, 0xf0, 0xb4, 0xe9, 0x4b,
64 0x3c, 0x73, 0xe3, 0xa9, 0xd4, 0xcd, 0x9d, 0xf2,
65 0xc8, 0xf9, 0xa3, 0x5e, 0x42, 0xbd, 0xd0, 0x47,
66 0x55, 0x0f, 0x69, 0xd8, 0x0e, 0xc2, 0x3c, 0xd4
67 };
68 EC_KEY *eck;
69 EC_POINT *ecp;
70 #endif
71 int r;
72
73 #define MKBN(b, bnn) \
74 do { \
75 bnn = NULL; \
76 ASSERT_INT_GT(BN_hex2bn(&bnn, b), 0); \
77 } while (0)
78
79 TEST_START("sshbuf_put_bignum2");
80 MKBN(hexbn1, bn);
81 p1 = sshbuf_new();
82 ASSERT_PTR_NE(p1, NULL);
83 ASSERT_INT_EQ(sshbuf_put_bignum2(p1, bn), 0);
84 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn1) + 4);
85 ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), (uint32_t)BN_num_bytes(bn));
86 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 4, expbn1, sizeof(expbn1));
87 BN_free(bn);
88 sshbuf_free(p1);
89 TEST_DONE();
90
91 TEST_START("sshbuf_put_bignum2 limited");
92 MKBN(hexbn1, bn);
93 p1 = sshbuf_new();
94 ASSERT_PTR_NE(p1, NULL);
95 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(expbn1) + 3), 0);
96 r = sshbuf_put_bignum2(p1, bn);
97 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
98 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
99 BN_free(bn);
100 sshbuf_free(p1);
101 TEST_DONE();
102
103 TEST_START("sshbuf_put_bignum2 bn2");
104 MKBN(hexbn2, bn);
105 p1 = sshbuf_new();
106 ASSERT_PTR_NE(p1, NULL);
107 ASSERT_INT_EQ(sshbuf_put_bignum2(p1, bn), 0);
108 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 4 + 1); /* MSB */
109 ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), (uint32_t)BN_num_bytes(bn) + 1);
110 ASSERT_U8_EQ(*(sshbuf_ptr(p1) + 4), 0x00);
111 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 5, expbn2, sizeof(expbn2));
112 BN_free(bn);
113 sshbuf_free(p1);
114 TEST_DONE();
115
116 TEST_START("sshbuf_put_bignum2 bn2 limited");
117 MKBN(hexbn2, bn);
118 p1 = sshbuf_new();
119 ASSERT_PTR_NE(p1, NULL);
120 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(expbn2) + 3), 0);
121 r = sshbuf_put_bignum2(p1, bn);
122 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
123 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
124 BN_free(bn);
125 sshbuf_free(p1);
126 TEST_DONE();
127
128 TEST_START("sshbuf_get_bignum2");
129 MKBN(hexbn1, bn);
130 p1 = sshbuf_new();
131 ASSERT_PTR_NE(p1, NULL);
132 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn)), 0);
133 ASSERT_INT_EQ(sshbuf_put(p1, expbn1, sizeof(expbn1)), 0);
134 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4 + sizeof(expbn1));
135 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0xd00f), 0);
136 bn2 = NULL;
137 ASSERT_INT_EQ(sshbuf_get_bignum2(p1, &bn2), 0);
138 ASSERT_BIGNUM_EQ(bn, bn2);
139 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
140 BN_free(bn);
141 BN_free(bn2);
142 sshbuf_free(p1);
143 TEST_DONE();
144
145 TEST_START("sshbuf_get_bignum2 truncated");
146 MKBN(hexbn1, bn);
147 p1 = sshbuf_new();
148 ASSERT_PTR_NE(p1, NULL);
149 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn)), 0);
150 ASSERT_INT_EQ(sshbuf_put(p1, expbn1, sizeof(expbn1) - 1), 0);
151 bn2 = NULL;
152 r = sshbuf_get_bignum2(p1, &bn2);
153 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
154 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn1) + 3);
155 BN_free(bn);
156 BN_free(bn2);
157 sshbuf_free(p1);
158 TEST_DONE();
159
160 TEST_START("sshbuf_get_bignum2 giant");
161 MKBN(hexbn1, bn);
162 p1 = sshbuf_new();
163 ASSERT_PTR_NE(p1, NULL);
164 ASSERT_INT_EQ(sshbuf_put_u32(p1, 65536), 0);
165 ASSERT_INT_EQ(sshbuf_reserve(p1, 65536, NULL), 0);
166 bn2 = NULL;
167 r = sshbuf_get_bignum2(p1, &bn2);
168 ASSERT_INT_EQ(r, SSH_ERR_BIGNUM_TOO_LARGE);
169 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 65536 + 4);
170 BN_free(bn);
171 BN_free(bn2);
172 sshbuf_free(p1);
173 TEST_DONE();
174
175 TEST_START("sshbuf_get_bignum2 bn2");
176 MKBN(hexbn2, bn);
177 p1 = sshbuf_new();
178 ASSERT_PTR_NE(p1, NULL);
179 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn) + 1), 0); /* MSB */
180 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x00), 0);
181 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2)), 0);
182 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4 + 1 + sizeof(expbn2));
183 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0xd00f), 0);
184 bn2 = NULL;
185 ASSERT_INT_EQ(sshbuf_get_bignum2(p1, &bn2), 0);
186 ASSERT_BIGNUM_EQ(bn, bn2);
187 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
188 BN_free(bn);
189 BN_free(bn2);
190 sshbuf_free(p1);
191 TEST_DONE();
192
193 TEST_START("sshbuf_get_bignum2 bn2 truncated");
194 MKBN(hexbn2, bn);
195 p1 = sshbuf_new();
196 ASSERT_PTR_NE(p1, NULL);
197 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn) + 1), 0);
198 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x00), 0);
199 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2) - 1), 0);
200 bn2 = NULL;
201 r = sshbuf_get_bignum2(p1, &bn2);
202 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
203 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 1 + 4 - 1);
204 BN_free(bn);
205 BN_free(bn2);
206 sshbuf_free(p1);
207 TEST_DONE();
208
209 TEST_START("sshbuf_get_bignum2 bn2 negative");
210 MKBN(hexbn2, bn);
211 p1 = sshbuf_new();
212 ASSERT_PTR_NE(p1, NULL);
213 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn)), 0);
214 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2)), 0);
215 bn2 = NULL;
216 r = sshbuf_get_bignum2(p1, &bn2);
217 ASSERT_INT_EQ(r, SSH_ERR_BIGNUM_IS_NEGATIVE);
218 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 4);
219 BN_free(bn);
220 BN_free(bn2);
221 sshbuf_free(p1);
222 TEST_DONE();
223
224 #if defined(OPENSSL_HAS_ECC) && defined(OPENSSL_HAS_NISTP256)
225 TEST_START("sshbuf_put_ec");
226 eck = EC_KEY_new_by_curve_name(ec256_nid);
227 ASSERT_PTR_NE(eck, NULL);
228 ecp = EC_POINT_new(EC_KEY_get0_group(eck));
229 ASSERT_PTR_NE(ecp, NULL);
230 MKBN(ec256_x, bn_x);
231 MKBN(ec256_y, bn_y);
232 ASSERT_INT_EQ(EC_POINT_set_affine_coordinates(
233 EC_KEY_get0_group(eck), ecp, bn_x, bn_y, NULL), 1);
234 ASSERT_INT_EQ(EC_KEY_set_public_key(eck, ecp), 1);
235 BN_free(bn_x);
236 BN_free(bn_y);
237 EC_POINT_free(ecp);
238 p1 = sshbuf_new();
239 ASSERT_PTR_NE(p1, NULL);
240 ASSERT_INT_EQ(sshbuf_put_eckey(p1, eck), 0);
241 ASSERT_INT_EQ(sshbuf_get_string_direct(p1, &d, &s), 0);
242 ASSERT_SIZE_T_EQ(s, sizeof(expec256));
243 ASSERT_MEM_EQ(d, expec256, sizeof(expec256));
244 sshbuf_free(p1);
245 EC_KEY_free(eck);
246 TEST_DONE();
247
248 TEST_START("sshbuf_get_ec");
249 eck = EC_KEY_new_by_curve_name(ec256_nid);
250 ASSERT_PTR_NE(eck, NULL);
251 p1 = sshbuf_new();
252 ASSERT_PTR_NE(p1, NULL);
253 ASSERT_INT_EQ(sshbuf_put_string(p1, expec256, sizeof(expec256)), 0);
254 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expec256) + 4);
255 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x00), 0);
256 ASSERT_INT_EQ(sshbuf_get_eckey(p1, eck), 0);
257 bn_x = BN_new();
258 bn_y = BN_new();
259 ASSERT_PTR_NE(bn_x, NULL);
260 ASSERT_PTR_NE(bn_y, NULL);
261 ASSERT_INT_EQ(EC_POINT_get_affine_coordinates(
262 EC_KEY_get0_group(eck), EC_KEY_get0_public_key(eck),
263 bn_x, bn_y, NULL), 1);
264 MKBN(ec256_x, bn);
265 MKBN(ec256_y, bn2);
266 ASSERT_INT_EQ(BN_cmp(bn_x, bn), 0);
267 ASSERT_INT_EQ(BN_cmp(bn_y, bn2), 0);
268 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
269 sshbuf_free(p1);
270 EC_KEY_free(eck);
271 BN_free(bn_x);
272 BN_free(bn_y);
273 BN_free(bn);
274 BN_free(bn2);
275 TEST_DONE();
276 #endif
277 }
278
279 #endif /* WITH_OPENSSL */