Merge branch 'maint-0.4.5' into maint-0.4.6
[tor.git] / src / test / test_relaycrypt.c
blob3a615c53a35872950c42cfa853fc6d0fa7e74e89
1 /* Copyright 2001-2004 Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define CRYPT_PATH_PRIVATE
8 #include "core/or/or.h"
9 #include "core/or/circuitbuild.h"
10 #define CIRCUITLIST_PRIVATE
11 #include "core/or/circuitlist.h"
12 #include "lib/crypt_ops/crypto_rand.h"
13 #include "core/or/relay.h"
14 #include "core/crypto/relay_crypto.h"
15 #include "core/or/crypt_path.h"
16 #include "core/or/cell_st.h"
17 #include "core/or/or_circuit_st.h"
18 #include "core/or/origin_circuit_st.h"
20 #include "test/test.h"
22 static const char KEY_MATERIAL[3][CPATH_KEY_MATERIAL_LEN] = {
23 " 'My public key is in this signed x509 object', said Tom assertively.",
24 "'Let's chart the pedal phlanges in the tomb', said Tom cryptographically",
25 " 'Segmentation fault bugs don't _just happen_', said Tom seethingly.",
28 typedef struct testing_circuitset_t {
29 or_circuit_t *or_circ[3];
30 origin_circuit_t *origin_circ;
31 } testing_circuitset_t;
33 static int testing_circuitset_teardown(const struct testcase_t *testcase,
34 void *ptr);
36 static void *
37 testing_circuitset_setup(const struct testcase_t *testcase)
39 testing_circuitset_t *cs = tor_malloc_zero(sizeof(testing_circuitset_t));
40 int i;
42 for (i=0; i<3; ++i) {
43 cs->or_circ[i] = or_circuit_new(0, NULL);
44 tt_int_op(0, OP_EQ,
45 relay_crypto_init(&cs->or_circ[i]->crypto,
46 KEY_MATERIAL[i], sizeof(KEY_MATERIAL[i]),
47 0, 0));
50 cs->origin_circ = origin_circuit_new();
51 cs->origin_circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
52 for (i=0; i<3; ++i) {
53 crypt_path_t *hop = tor_malloc_zero(sizeof(*hop));
54 relay_crypto_init(&hop->pvt_crypto, KEY_MATERIAL[i],
55 sizeof(KEY_MATERIAL[i]), 0, 0);
56 hop->state = CPATH_STATE_OPEN;
57 cpath_extend_linked_list(&cs->origin_circ->cpath, hop);
58 tt_ptr_op(hop, OP_EQ, cs->origin_circ->cpath->prev);
61 return cs;
62 done:
63 testing_circuitset_teardown(testcase, cs);
64 return NULL;
67 static int
68 testing_circuitset_teardown(const struct testcase_t *testcase, void *ptr)
70 (void)testcase;
71 testing_circuitset_t *cs = ptr;
72 int i;
73 for (i=0; i<3; ++i) {
74 circuit_free_(TO_CIRCUIT(cs->or_circ[i]));
76 circuit_free_(TO_CIRCUIT(cs->origin_circ));
77 tor_free(cs);
78 return 1;
81 static const struct testcase_setup_t relaycrypt_setup = {
82 testing_circuitset_setup, testing_circuitset_teardown
85 /* Test encrypting a cell to the final hop on a circuit, decrypting it
86 * at each hop, and recognizing it at the other end. Then do it again
87 * and again as the state evolves. */
88 static void
89 test_relaycrypt_outbound(void *arg)
91 testing_circuitset_t *cs = arg;
92 tt_assert(cs);
94 relay_header_t rh;
95 cell_t orig;
96 cell_t encrypted;
97 int i, j;
99 for (i = 0; i < 50; ++i) {
100 crypto_rand((char *)&orig, sizeof(orig));
102 relay_header_unpack(&rh, orig.payload);
103 rh.recognized = 0;
104 memset(rh.integrity, 0, sizeof(rh.integrity));
105 relay_header_pack(orig.payload, &rh);
107 memcpy(&encrypted, &orig, sizeof(orig));
109 /* Encrypt the cell to the last hop */
110 relay_encrypt_cell_outbound(&encrypted, cs->origin_circ,
111 cs->origin_circ->cpath->prev);
113 for (j = 0; j < 3; ++j) {
114 crypt_path_t *layer_hint = NULL;
115 char recognized = 0;
116 int r = relay_decrypt_cell(TO_CIRCUIT(cs->or_circ[j]),
117 &encrypted,
118 CELL_DIRECTION_OUT,
119 &layer_hint, &recognized);
120 tt_int_op(r, OP_EQ, 0);
121 tt_ptr_op(layer_hint, OP_EQ, NULL);
122 tt_int_op(recognized != 0, OP_EQ, j == 2);
125 tt_mem_op(orig.payload, OP_EQ, encrypted.payload, CELL_PAYLOAD_SIZE);
128 done:
132 /* As above, but simulate inbound cells from the last hop. */
133 static void
134 test_relaycrypt_inbound(void *arg)
136 testing_circuitset_t *cs = arg;
137 tt_assert(cs);
139 relay_header_t rh;
140 cell_t orig;
141 cell_t encrypted;
142 int i, j;
144 for (i = 0; i < 50; ++i) {
145 crypto_rand((char *)&orig, sizeof(orig));
147 relay_header_unpack(&rh, orig.payload);
148 rh.recognized = 0;
149 memset(rh.integrity, 0, sizeof(rh.integrity));
150 relay_header_pack(orig.payload, &rh);
152 memcpy(&encrypted, &orig, sizeof(orig));
154 /* Encrypt the cell to the last hop */
155 relay_encrypt_cell_inbound(&encrypted, cs->or_circ[2]);
157 crypt_path_t *layer_hint = NULL;
158 char recognized = 0;
159 int r;
160 for (j = 1; j >= 0; --j) {
161 r = relay_decrypt_cell(TO_CIRCUIT(cs->or_circ[j]),
162 &encrypted,
163 CELL_DIRECTION_IN,
164 &layer_hint, &recognized);
165 tt_int_op(r, OP_EQ, 0);
166 tt_ptr_op(layer_hint, OP_EQ, NULL);
167 tt_int_op(recognized, OP_EQ, 0);
170 relay_decrypt_cell(TO_CIRCUIT(cs->origin_circ),
171 &encrypted,
172 CELL_DIRECTION_IN,
173 &layer_hint, &recognized);
174 tt_int_op(r, OP_EQ, 0);
175 tt_int_op(recognized, OP_EQ, 1);
176 tt_ptr_op(layer_hint, OP_EQ, cs->origin_circ->cpath->prev);
178 tt_mem_op(orig.payload, OP_EQ, encrypted.payload, CELL_PAYLOAD_SIZE);
180 done:
184 #define TEST(name) \
185 { # name, test_relaycrypt_ ## name, 0, &relaycrypt_setup, NULL }
187 struct testcase_t relaycrypt_tests[] = {
188 TEST(outbound),
189 TEST(inbound),
190 END_OF_TESTCASES