0.2.6.5-rc will come out today, because why not.
[tor.git] / src / test / test_introduce.c
blob0cab8ef4cc74240c92123cd602feb39998d0a0dd
1 /* Copyright (c) 2012-2015, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
5 #include "crypto.h"
6 #include "or.h"
7 #include "test.h"
9 #define RENDSERVICE_PRIVATE
10 #include "rendservice.h"
12 extern const char AUTHORITY_SIGNKEY_1[];
14 static uint8_t v0_test_plaintext[] =
15 /* 20 bytes of rendezvous point nickname */
16 { 0x4e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65,
17 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18 0x00, 0x00, 0x00, 0x00,
19 /* 20 bytes dummy rendezvous cookie */
20 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
21 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
22 0x10, 0x11, 0x12, 0x13,
23 /* 128 bytes dummy DH handshake data */
24 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
25 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
26 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
27 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
28 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
30 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
31 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
32 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
33 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
34 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
35 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
36 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
37 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
38 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
39 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
41 static uint8_t v1_test_plaintext[] =
42 /* Version byte */
43 { 0x01,
44 /* 42 bytes of dummy rendezvous point hex digest */
45 0x24, 0x30, 0x30, 0x30, 0x31, 0x30, 0x32, 0x30,
46 0x33, 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x30,
47 0x37, 0x30, 0x38, 0x30, 0x39, 0x30, 0x41, 0x30,
48 0x42, 0x30, 0x43, 0x30, 0x44, 0x30, 0x45, 0x30,
49 0x46, 0x31, 0x30, 0x31, 0x31, 0x31, 0x32, 0x31,
50 0x33, 0x00,
51 /* 20 bytes dummy rendezvous cookie */
52 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
53 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
54 0x10, 0x11, 0x12, 0x13,
55 /* 128 bytes dummy DH handshake data */
56 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
57 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
58 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
59 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
60 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
61 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
62 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
63 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
64 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
65 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
66 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
67 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
68 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
69 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
70 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
71 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
73 static uint8_t v2_test_plaintext[] =
74 /* Version byte */
75 { 0x02,
76 /* 4 bytes rendezvous point's IP address */
77 0xc0, 0xa8, 0x00, 0x01,
78 /* 2 bytes rendezvous point's OR port */
79 0x23, 0x5a,
80 /* 20 bytes dummy rendezvous point's identity digest */
81 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
82 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
83 0x10, 0x11, 0x12, 0x13,
84 /* 2 bytes length of onion key */
85 0x00, 0x8c,
86 /* Onion key (140 bytes taken from live test) */
87 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xb1,
88 0xcd, 0x46, 0xa9, 0x18, 0xd2, 0x0f, 0x01, 0xf8,
89 0xb2, 0xad, 0xa4, 0x79, 0xb4, 0xbb, 0x4b, 0xf4,
90 0x54, 0x1e, 0x3f, 0x03, 0x54, 0xcf, 0x7c, 0xb6,
91 0xb5, 0xf0, 0xfe, 0xed, 0x4b, 0x7d, 0xd7, 0x61,
92 0xdb, 0x6d, 0xd9, 0x19, 0xe2, 0x72, 0x04, 0xaa,
93 0x3e, 0x89, 0x26, 0x14, 0x62, 0x9a, 0x6c, 0x11,
94 0x0b, 0x35, 0x99, 0x2c, 0x9f, 0x2c, 0x64, 0xa1,
95 0xd9, 0xe2, 0x88, 0xce, 0xf6, 0x54, 0xfe, 0x1d,
96 0x37, 0x5e, 0x6d, 0x73, 0x95, 0x54, 0x90, 0xf0,
97 0x7b, 0xfa, 0xd4, 0x44, 0xac, 0xb2, 0x23, 0x9f,
98 0x75, 0x36, 0xe2, 0x78, 0x62, 0x82, 0x80, 0xa4,
99 0x23, 0x22, 0xc9, 0xbf, 0xc4, 0x36, 0xd1, 0x31,
100 0x33, 0x8e, 0x64, 0xb4, 0xa9, 0x74, 0xa1, 0xcb,
101 0x42, 0x8d, 0x60, 0xc7, 0xbb, 0x8e, 0x6e, 0x0f,
102 0x36, 0x74, 0x8e, 0xf4, 0x08, 0x99, 0x06, 0x92,
103 0xb1, 0x3f, 0xb3, 0xdd, 0xed, 0xf7, 0xc9, 0x02,
104 0x03, 0x01, 0x00, 0x01,
105 /* 20 bytes dummy rendezvous cookie */
106 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
107 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
108 0x10, 0x11, 0x12, 0x13,
109 /* 128 bytes dummy DH handshake data */
110 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
111 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
112 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
113 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
114 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
115 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
116 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
117 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
118 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
119 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
120 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
121 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
122 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
123 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
124 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
125 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
127 static uint8_t v3_no_auth_test_plaintext[] =
128 /* Version byte */
129 { 0x03,
130 /* Auth type (0 for no auth len/auth data) */
131 0x00,
132 /* Timestamp */
133 0x50, 0x0b, 0xb5, 0xaa,
134 /* 4 bytes rendezvous point's IP address */
135 0xc0, 0xa8, 0x00, 0x01,
136 /* 2 bytes rendezvous point's OR port */
137 0x23, 0x5a,
138 /* 20 bytes dummy rendezvous point's identity digest */
139 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
140 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
141 0x10, 0x11, 0x12, 0x13,
142 /* 2 bytes length of onion key */
143 0x00, 0x8c,
144 /* Onion key (140 bytes taken from live test) */
145 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xb1,
146 0xcd, 0x46, 0xa9, 0x18, 0xd2, 0x0f, 0x01, 0xf8,
147 0xb2, 0xad, 0xa4, 0x79, 0xb4, 0xbb, 0x4b, 0xf4,
148 0x54, 0x1e, 0x3f, 0x03, 0x54, 0xcf, 0x7c, 0xb6,
149 0xb5, 0xf0, 0xfe, 0xed, 0x4b, 0x7d, 0xd7, 0x61,
150 0xdb, 0x6d, 0xd9, 0x19, 0xe2, 0x72, 0x04, 0xaa,
151 0x3e, 0x89, 0x26, 0x14, 0x62, 0x9a, 0x6c, 0x11,
152 0x0b, 0x35, 0x99, 0x2c, 0x9f, 0x2c, 0x64, 0xa1,
153 0xd9, 0xe2, 0x88, 0xce, 0xf6, 0x54, 0xfe, 0x1d,
154 0x37, 0x5e, 0x6d, 0x73, 0x95, 0x54, 0x90, 0xf0,
155 0x7b, 0xfa, 0xd4, 0x44, 0xac, 0xb2, 0x23, 0x9f,
156 0x75, 0x36, 0xe2, 0x78, 0x62, 0x82, 0x80, 0xa4,
157 0x23, 0x22, 0xc9, 0xbf, 0xc4, 0x36, 0xd1, 0x31,
158 0x33, 0x8e, 0x64, 0xb4, 0xa9, 0x74, 0xa1, 0xcb,
159 0x42, 0x8d, 0x60, 0xc7, 0xbb, 0x8e, 0x6e, 0x0f,
160 0x36, 0x74, 0x8e, 0xf4, 0x08, 0x99, 0x06, 0x92,
161 0xb1, 0x3f, 0xb3, 0xdd, 0xed, 0xf7, 0xc9, 0x02,
162 0x03, 0x01, 0x00, 0x01,
163 /* 20 bytes dummy rendezvous cookie */
164 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
165 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
166 0x10, 0x11, 0x12, 0x13,
167 /* 128 bytes dummy DH handshake data */
168 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
169 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
170 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
171 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
172 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
173 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
174 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
175 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
176 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
177 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
178 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
179 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
180 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
181 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
182 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
183 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
185 static uint8_t v3_basic_auth_test_plaintext[] =
186 /* Version byte */
187 { 0x03,
188 /* Auth type (1 for REND_BASIC_AUTH) */
189 0x01,
190 /* Auth len (must be 16 bytes for REND_BASIC_AUTH) */
191 0x00, 0x10,
192 /* Auth data (a 16-byte dummy descriptor cookie) */
193 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
194 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
195 /* Timestamp */
196 0x50, 0x0b, 0xb5, 0xaa,
197 /* 4 bytes rendezvous point's IP address */
198 0xc0, 0xa8, 0x00, 0x01,
199 /* 2 bytes rendezvous point's OR port */
200 0x23, 0x5a,
201 /* 20 bytes dummy rendezvous point's identity digest */
202 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
203 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
204 0x10, 0x11, 0x12, 0x13,
205 /* 2 bytes length of onion key */
206 0x00, 0x8c,
207 /* Onion key (140 bytes taken from live test) */
208 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xb1,
209 0xcd, 0x46, 0xa9, 0x18, 0xd2, 0x0f, 0x01, 0xf8,
210 0xb2, 0xad, 0xa4, 0x79, 0xb4, 0xbb, 0x4b, 0xf4,
211 0x54, 0x1e, 0x3f, 0x03, 0x54, 0xcf, 0x7c, 0xb6,
212 0xb5, 0xf0, 0xfe, 0xed, 0x4b, 0x7d, 0xd7, 0x61,
213 0xdb, 0x6d, 0xd9, 0x19, 0xe2, 0x72, 0x04, 0xaa,
214 0x3e, 0x89, 0x26, 0x14, 0x62, 0x9a, 0x6c, 0x11,
215 0x0b, 0x35, 0x99, 0x2c, 0x9f, 0x2c, 0x64, 0xa1,
216 0xd9, 0xe2, 0x88, 0xce, 0xf6, 0x54, 0xfe, 0x1d,
217 0x37, 0x5e, 0x6d, 0x73, 0x95, 0x54, 0x90, 0xf0,
218 0x7b, 0xfa, 0xd4, 0x44, 0xac, 0xb2, 0x23, 0x9f,
219 0x75, 0x36, 0xe2, 0x78, 0x62, 0x82, 0x80, 0xa4,
220 0x23, 0x22, 0xc9, 0xbf, 0xc4, 0x36, 0xd1, 0x31,
221 0x33, 0x8e, 0x64, 0xb4, 0xa9, 0x74, 0xa1, 0xcb,
222 0x42, 0x8d, 0x60, 0xc7, 0xbb, 0x8e, 0x6e, 0x0f,
223 0x36, 0x74, 0x8e, 0xf4, 0x08, 0x99, 0x06, 0x92,
224 0xb1, 0x3f, 0xb3, 0xdd, 0xed, 0xf7, 0xc9, 0x02,
225 0x03, 0x01, 0x00, 0x01,
226 /* 20 bytes dummy rendezvous cookie */
227 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
228 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
229 0x10, 0x11, 0x12, 0x13,
230 /* 128 bytes dummy DH handshake data */
231 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
232 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
233 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
234 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
235 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
236 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
237 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
238 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
239 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
240 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
241 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
242 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
243 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
244 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
245 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
246 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
248 static void do_decrypt_test(uint8_t *plaintext, size_t plaintext_len);
249 static void do_early_parse_test(uint8_t *plaintext, size_t plaintext_len);
250 static void do_late_parse_test(uint8_t *plaintext, size_t plaintext_len);
251 static void do_parse_test(uint8_t *plaintext, size_t plaintext_len, int phase);
252 static ssize_t make_intro_from_plaintext(
253 void *buf, size_t len, crypto_pk_t *key, void **cell_out);
255 #define EARLY_PARSE_ONLY 1
256 #define DECRYPT_ONLY 2
257 #define ALL_PARSING 3
259 static void
260 do_early_parse_test(uint8_t *plaintext, size_t plaintext_len)
262 do_parse_test(plaintext, plaintext_len, EARLY_PARSE_ONLY);
265 static void
266 do_decrypt_test(uint8_t *plaintext, size_t plaintext_len)
268 do_parse_test(plaintext, plaintext_len, DECRYPT_ONLY);
271 static void
272 do_late_parse_test(uint8_t *plaintext, size_t plaintext_len)
274 do_parse_test(plaintext, plaintext_len, ALL_PARSING);
277 /** Test utility function: checks that the <b>plaintext_len</b>-byte string at
278 * <b>plaintext</b> is at least superficially parseable.
280 static void
281 do_parse_test(uint8_t *plaintext, size_t plaintext_len, int phase)
283 crypto_pk_t *k = NULL;
284 ssize_t r;
285 uint8_t *cell = NULL;
286 size_t cell_len;
287 rend_intro_cell_t *parsed_req = NULL;
288 char *err_msg = NULL;
289 char digest[DIGEST_LEN];
291 /* Get a key */
292 k = crypto_pk_new();
293 tt_assert(k);
294 r = crypto_pk_read_private_key_from_string(k, AUTHORITY_SIGNKEY_1, -1);
295 tt_assert(!r);
297 /* Get digest for future comparison */
298 r = crypto_pk_get_digest(k, digest);
299 tt_assert(r >= 0);
301 /* Make a cell out of it */
302 r = make_intro_from_plaintext(
303 plaintext, plaintext_len,
304 k, (void **)(&cell));
305 tt_assert(r > 0);
306 tt_assert(cell);
307 cell_len = r;
309 /* Do early parsing */
310 parsed_req = rend_service_begin_parse_intro(cell, cell_len, 2, &err_msg);
311 tt_assert(parsed_req);
312 tt_assert(!err_msg);
313 tt_mem_op(parsed_req->pk,OP_EQ, digest, DIGEST_LEN);
314 tt_assert(parsed_req->ciphertext);
315 tt_assert(parsed_req->ciphertext_len > 0);
317 if (phase == EARLY_PARSE_ONLY)
318 goto done;
320 /* Do decryption */
321 r = rend_service_decrypt_intro(parsed_req, k, &err_msg);
322 tt_assert(!r);
323 tt_assert(!err_msg);
324 tt_assert(parsed_req->plaintext);
325 tt_assert(parsed_req->plaintext_len > 0);
327 if (phase == DECRYPT_ONLY)
328 goto done;
330 /* Do late parsing */
331 r = rend_service_parse_intro_plaintext(parsed_req, &err_msg);
332 tt_assert(!r);
333 tt_assert(!err_msg);
334 tt_assert(parsed_req->parsed);
336 done:
337 tor_free(cell);
338 crypto_pk_free(k);
339 rend_service_free_intro(parsed_req);
340 tor_free(err_msg);
343 /** Given the plaintext of the encrypted part of an INTRODUCE1/2 and a key,
344 * construct the encrypted cell for testing.
347 static ssize_t
348 make_intro_from_plaintext(
349 void *buf, size_t len, crypto_pk_t *key, void **cell_out)
351 char *cell = NULL;
352 ssize_t cell_len = -1, r;
353 /* Assemble key digest and ciphertext, then construct the cell */
354 ssize_t ciphertext_size;
356 if (!(buf && key && len > 0 && cell_out)) goto done;
359 * Figure out an upper bound on how big the ciphertext will be
360 * (see crypto_pk_public_hybrid_encrypt())
362 ciphertext_size = PKCS1_OAEP_PADDING_OVERHEAD;
363 ciphertext_size += crypto_pk_keysize(key);
364 ciphertext_size += CIPHER_KEY_LEN;
365 ciphertext_size += len;
368 * Allocate space for the cell
370 cell = tor_malloc(DIGEST_LEN + ciphertext_size);
372 /* Compute key digest (will be first DIGEST_LEN octets of cell) */
373 r = crypto_pk_get_digest(key, cell);
374 tt_assert(r >= 0);
376 /* Do encryption */
377 r = crypto_pk_public_hybrid_encrypt(
378 key, cell + DIGEST_LEN, ciphertext_size,
379 buf, len,
380 PK_PKCS1_OAEP_PADDING, 0);
381 tt_assert(r >= 0);
383 /* Figure out cell length */
384 cell_len = DIGEST_LEN + r;
386 /* Output the cell */
387 *cell_out = cell;
389 done:
390 return cell_len;
393 /** Test v0 INTRODUCE2 parsing through decryption only
396 static void
397 test_introduce_decrypt_v0(void *arg)
399 (void)arg;
400 do_decrypt_test(v0_test_plaintext, sizeof(v0_test_plaintext));
403 /** Test v1 INTRODUCE2 parsing through decryption only
406 static void
407 test_introduce_decrypt_v1(void *arg)
409 (void)arg;
410 do_decrypt_test(v1_test_plaintext, sizeof(v1_test_plaintext));
413 /** Test v2 INTRODUCE2 parsing through decryption only
416 static void
417 test_introduce_decrypt_v2(void *arg)
419 (void)arg;
420 do_decrypt_test(v2_test_plaintext, sizeof(v2_test_plaintext));
423 /** Test v3 INTRODUCE2 parsing through decryption only
426 static void
427 test_introduce_decrypt_v3(void *arg)
429 (void)arg;
430 do_decrypt_test(
431 v3_no_auth_test_plaintext, sizeof(v3_no_auth_test_plaintext));
432 do_decrypt_test(
433 v3_basic_auth_test_plaintext, sizeof(v3_basic_auth_test_plaintext));
436 /** Test v0 INTRODUCE2 parsing through early parsing only
439 static void
440 test_introduce_early_parse_v0(void *arg)
442 (void)arg;
443 do_early_parse_test(v0_test_plaintext, sizeof(v0_test_plaintext));
446 /** Test v1 INTRODUCE2 parsing through early parsing only
449 static void
450 test_introduce_early_parse_v1(void *arg)
452 (void)arg;
453 do_early_parse_test(v1_test_plaintext, sizeof(v1_test_plaintext));
456 /** Test v2 INTRODUCE2 parsing through early parsing only
459 static void
460 test_introduce_early_parse_v2(void *arg)
462 (void)arg;
463 do_early_parse_test(v2_test_plaintext, sizeof(v2_test_plaintext));
466 /** Test v3 INTRODUCE2 parsing through early parsing only
469 static void
470 test_introduce_early_parse_v3(void *arg)
472 (void)arg;
473 do_early_parse_test(
474 v3_no_auth_test_plaintext, sizeof(v3_no_auth_test_plaintext));
475 do_early_parse_test(
476 v3_basic_auth_test_plaintext, sizeof(v3_basic_auth_test_plaintext));
479 /** Test v0 INTRODUCE2 parsing
482 static void
483 test_introduce_late_parse_v0(void *arg)
485 (void)arg;
486 do_late_parse_test(v0_test_plaintext, sizeof(v0_test_plaintext));
489 /** Test v1 INTRODUCE2 parsing
492 static void
493 test_introduce_late_parse_v1(void *arg)
495 (void)arg;
496 do_late_parse_test(v1_test_plaintext, sizeof(v1_test_plaintext));
499 /** Test v2 INTRODUCE2 parsing
502 static void
503 test_introduce_late_parse_v2(void *arg)
505 (void)arg;
506 do_late_parse_test(v2_test_plaintext, sizeof(v2_test_plaintext));
509 /** Test v3 INTRODUCE2 parsing
512 static void
513 test_introduce_late_parse_v3(void *arg)
515 (void)arg;
516 do_late_parse_test(
517 v3_no_auth_test_plaintext, sizeof(v3_no_auth_test_plaintext));
518 do_late_parse_test(
519 v3_basic_auth_test_plaintext, sizeof(v3_basic_auth_test_plaintext));
522 #define INTRODUCE_LEGACY(name) \
523 { #name, test_introduce_ ## name , 0, NULL, NULL }
525 struct testcase_t introduce_tests[] = {
526 INTRODUCE_LEGACY(early_parse_v0),
527 INTRODUCE_LEGACY(early_parse_v1),
528 INTRODUCE_LEGACY(early_parse_v2),
529 INTRODUCE_LEGACY(early_parse_v3),
530 INTRODUCE_LEGACY(decrypt_v0),
531 INTRODUCE_LEGACY(decrypt_v1),
532 INTRODUCE_LEGACY(decrypt_v2),
533 INTRODUCE_LEGACY(decrypt_v3),
534 INTRODUCE_LEGACY(late_parse_v0),
535 INTRODUCE_LEGACY(late_parse_v1),
536 INTRODUCE_LEGACY(late_parse_v2),
537 INTRODUCE_LEGACY(late_parse_v3),
538 END_OF_TESTCASES