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
,
37 testing_circuitset_setup(const struct testcase_t
*testcase
)
39 testing_circuitset_t
*cs
= tor_malloc_zero(sizeof(testing_circuitset_t
));
43 cs
->or_circ
[i
] = or_circuit_new(0, NULL
);
45 relay_crypto_init(&cs
->or_circ
[i
]->crypto
,
46 KEY_MATERIAL
[i
], sizeof(KEY_MATERIAL
[i
]),
50 cs
->origin_circ
= origin_circuit_new();
51 cs
->origin_circ
->base_
.purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
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
);
63 testing_circuitset_teardown(testcase
, cs
);
68 testing_circuitset_teardown(const struct testcase_t
*testcase
, void *ptr
)
71 testing_circuitset_t
*cs
= ptr
;
74 circuit_free_(TO_CIRCUIT(cs
->or_circ
[i
]));
76 circuit_free_(TO_CIRCUIT(cs
->origin_circ
));
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. */
89 test_relaycrypt_outbound(void *arg
)
91 testing_circuitset_t
*cs
= arg
;
99 for (i
= 0; i
< 50; ++i
) {
100 crypto_rand((char *)&orig
, sizeof(orig
));
102 relay_header_unpack(&rh
, orig
.payload
);
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
;
116 int r
= relay_decrypt_cell(TO_CIRCUIT(cs
->or_circ
[j
]),
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
);
132 /* As above, but simulate inbound cells from the last hop. */
134 test_relaycrypt_inbound(void *arg
)
136 testing_circuitset_t
*cs
= arg
;
144 for (i
= 0; i
< 50; ++i
) {
145 crypto_rand((char *)&orig
, sizeof(orig
));
147 relay_header_unpack(&rh
, orig
.payload
);
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
;
160 for (j
= 1; j
>= 0; --j
) {
161 r
= relay_decrypt_cell(TO_CIRCUIT(cs
->or_circ
[j
]),
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
),
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
);
185 { # name, test_relaycrypt_ ## name, 0, &relaycrypt_setup, NULL }
187 struct testcase_t relaycrypt_tests
[] = {