Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
relay_crypto_cgo.h
Go to the documentation of this file.
1/* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2025, The Tor Project, Inc. */
5/* See LICENSE for licensing information */
6
7/**
8 * \file relay_crypto_cgo.h
9 * \brief Header file for relay_crypto_cgo.c.
10 **/
11
12#ifndef TOR_RELAY_CRYPTO_CGO_H
13#define TOR_RELAY_CRYPTO_CGO_H
14
16
17/**
18 * State to implement forward _or_ reverse crypto between a client and a single
19 * hop on a circuit.
20 *
21 * (There needs to be one of these for each direction.
22 */
23typedef struct cgo_crypt_t cgo_crypt_t;
24
25typedef enum {
26 CGO_MODE_CLIENT_FORWARD,
27 CGO_MODE_CLIENT_BACKWARD,
28 CGO_MODE_RELAY_FORWARD,
29 CGO_MODE_RELAY_BACKWARD,
30} cgo_mode_t;
31
32/**
33 * Length of a CGO cell tag.
34 *
35 * This is the value used for authenticated SENDMES.
36 **/
37#define CGO_TAG_LEN 16
38
39struct cell_t;
40
41size_t cgo_key_material_len(int aesbits);
42cgo_crypt_t * cgo_crypt_new(cgo_mode_t mode, int aesbits,
43 const uint8_t *keys, size_t keylen);
45#define cgo_crypt_free(cgo) \
46 FREE_AND_NULL(cgo_crypt_t, cgo_crypt_free_, (cgo))
47
48void cgo_crypt_relay_forward(cgo_crypt_t *cgo, struct cell_t *cell,
49 const uint8_t **recognized_tag_out);
50void cgo_crypt_relay_backward(cgo_crypt_t *cgo, struct cell_t *cell);
51void cgo_crypt_relay_originate(cgo_crypt_t *cgo, struct cell_t *cell,
52 const uint8_t **tag_out);
53void cgo_crypt_client_forward(cgo_crypt_t *cgo, struct cell_t *cell);
54void cgo_crypt_client_originate(cgo_crypt_t *cgo, struct cell_t *cell,
55 const uint8_t **tag_out);
56void cgo_crypt_client_backward(cgo_crypt_t *cgo, struct cell_t *cell,
57 const uint8_t **recognized_tag_out);
58
59#ifdef RELAY_CRYPTO_CGO_PRIVATE
60/* Internal types and definitions for CGO encryption algorithms.
61 *
62 * Where reasonable, the identifiers here are chosen to match those
63 * in the spec (proposal 359), which in turn were chosen to match
64 * those in the paper.
65 */
66
67/**
68 * Tweakable block cipher, following the LRW2 construction,
69 * instantiated with AES.
70 *
71 * Any given instance can be used for encryption _or_ decryption,
72 * not both.
73 */
74typedef struct cgo_et_t {
75 /**
76 * AES block cipher instance
77 */
78 aes_raw_t *kb;
79 /**
80 * Polyval instance, with expanded key.
81 */
82 polyvalx_t ku;
83} cgo_et_t;
84/**
85 * Keyed pseudorandom function, based on polyval and AES-CTR.
86 */
87typedef struct cgo_prf_t {
88 /**
89 * AES stream cipher: may be 128, 192, or 256 bits.
90 */
92 /**
93 * Polyval instance.
94 */
96} cgo_prf_t;
97/**
98 * Rugged tweakable pseudorandom permutation, using the UIV+ construction.
99 *
100 * This is, roughly, a wide-block cipher where _encryption_
101 * is non-malleable, but where _decryption_ is malleable.
102 *
103 * UIV+ is the basis of CGO encryption, though it is used in different
104 * ways for each of the relay operations.
105 */
106typedef struct cgo_uiv_t {
107 /**
108 * Tweakable block cipher instance.
109 */
110 cgo_et_t j;
111 /**
112 * PRF instance.
113 */
114 cgo_prf_t s;
115#ifdef TOR_UNIT_TESTS
116 /** Testing only: Copy of keys used to instantiate this UIV.
117 * We use this in tests so that we can confirm the correctness
118 * of cgo_uiv_update().
119 */
120 uint8_t uiv_keys_[32 * 2 + 16 * 2];
121#endif
122} cgo_uiv_t;
123/**
124 * Length of the 'h' component of uiv_tweak_t.
125 */
126#define ET_TWEAK_LEN_H 16
127/**
128 * Length of the 'x_r' component of et_tweak_t.
129 */
130#define ET_TWEAK_LEN_X_R 493
131
132/**
133 * Tweak for the UIV+ wide-block cipher.
134 */
135typedef struct uiv_tweak_t {
136 /** H component of the wide-block cipher.
137 *
138 * This must be ET_TWEAK_LEN_H bytes long.
139 **/
140 const uint8_t *h;
141 /** Additional data component of the wide-block cipher.
142 * This value is sent to the cell command (RELAY or RELAY_EARLY)
143 * for each relay cell.
144 */
145 const uint8_t cmd;
146} uiv_tweak_t;
147/**
148 * Tweak for the ET tweakable block cipher.
149 */
150typedef struct et_tweak_t {
151 /** Components from the UIV+ tweak. */
152 uiv_tweak_t uiv;
153 /**
154 * X_R component of the ET tweak.
155 *
156 * This must be X_R bytes long.
157 */
158 const uint8_t *x_r;
159} et_tweak_t;
160
161/** Length of expected input to the PRF. */
162#define PRF_INPUT_LEN 16
163/** Output length for cgo_prf_xor_t0(). */
164#define PRF_T0_DATA_LEN 493
165
166/** Length of block handled by uiv instantiation. */
167#define UIV_BLOCK_LEN 509
168
169STATIC int cgo_et_init(cgo_et_t *et, int aesbits, bool encrypt,
170 const uint8_t *key);
171STATIC void cgo_et_set_key(cgo_et_t *et, int aesbits, bool encrypt,
172 const uint8_t *key);
173STATIC void cgo_et_encrypt(cgo_et_t *et, const et_tweak_t tweak,
174 uint8_t *block);
175STATIC void cgo_et_decrypt(cgo_et_t *et, const et_tweak_t tweak,
176 uint8_t *block);
177STATIC void cgo_et_clear(cgo_et_t *et);
178
179STATIC int cgo_prf_init(cgo_prf_t *prf, int aesbits,
180 const uint8_t *key);
181STATIC void cgo_prf_set_key(cgo_prf_t *prf, int aesbits,
182 const uint8_t *key);
183STATIC void cgo_prf_xor_t0(cgo_prf_t *prf, const uint8_t *input,
184 uint8_t *data);
185STATIC void cgo_prf_gen_t1(cgo_prf_t *prf, const uint8_t *input,
186 uint8_t *buf, size_t n);
187STATIC void cgo_prf_clear(cgo_prf_t *prf);
188
189STATIC int cgo_uiv_init(cgo_uiv_t *uiv, int aesbits, bool encrypt,
190 const uint8_t *key);
191STATIC void cgo_uiv_encrypt(cgo_uiv_t *uiv, const uiv_tweak_t tweak,
192 uint8_t *cell_body);
193STATIC void cgo_uiv_decrypt(cgo_uiv_t *uiv, const uiv_tweak_t tweak,
194 uint8_t *cell_body);
195STATIC void cgo_uiv_update(cgo_uiv_t *uiv, int aesbits, bool encrypt,
196 uint8_t *nonce);
197STATIC void cgo_uiv_clear(cgo_uiv_t *uiv);
198
199struct cgo_crypt_t {
200 cgo_uiv_t uiv;
201 uint8_t nonce[CGO_TAG_LEN];
202 uint8_t tprime[CGO_TAG_LEN];
203 /**
204 * Stored version of the last incoming cell tag.
205 * Only used for cgo_crypt_relay_fwd, where this information is not
206 * otherwise available after encryption.
207 */
208 uint8_t last_tag_relay_fwd[CGO_TAG_LEN];
209 uint8_t aes_bytes;
210};
211#endif
212
213#endif /* !defined(TOR_RELAY_CRYPTO_CGO_H) */
STATIC int cgo_et_init(cgo_et_t *et, int aesbits, bool encrypt, const uint8_t *key)
STATIC int cgo_uiv_init(cgo_uiv_t *uiv, int aesbits, bool encrypt, const uint8_t *key)
STATIC void cgo_prf_xor_t0(cgo_prf_t *prf, const uint8_t *input, uint8_t *data)
STATIC void cgo_uiv_clear(cgo_uiv_t *uiv)
STATIC void cgo_et_decrypt(cgo_et_t *et, const et_tweak_t tweak, uint8_t *block)
STATIC void cgo_et_set_key(cgo_et_t *et, int aesbits, bool encrypt, const uint8_t *key)
STATIC void cgo_uiv_encrypt(cgo_uiv_t *uiv, const uiv_tweak_t tweak, uint8_t *cell_body)
STATIC void cgo_et_clear(cgo_et_t *et)
STATIC int cgo_prf_init(cgo_prf_t *prf, int aesbits, const uint8_t *key)
STATIC void cgo_prf_clear(cgo_prf_t *prf)
STATIC void cgo_uiv_update(cgo_uiv_t *uiv, int aesbits, bool encrypt, uint8_t *nonce)
STATIC void cgo_prf_set_key(cgo_prf_t *prf, int aesbits, const uint8_t *key)
STATIC void cgo_prf_gen_t1(cgo_prf_t *prf, const uint8_t *input, uint8_t *buf, size_t n)
STATIC void cgo_et_encrypt(cgo_et_t *et, const et_tweak_t tweak, uint8_t *block)
STATIC void cgo_uiv_decrypt(cgo_uiv_t *uiv, const uiv_tweak_t tweak, uint8_t *cell_body)
void cgo_crypt_client_backward(cgo_crypt_t *cgo, struct cell_t *cell, const uint8_t **recognized_tag_out)
void cgo_crypt_client_originate(cgo_crypt_t *cgo, struct cell_t *cell, const uint8_t **tag_out)
#define CGO_TAG_LEN
void cgo_crypt_client_forward(cgo_crypt_t *cgo, struct cell_t *cell)
size_t cgo_key_material_len(int aesbits)
void cgo_crypt_relay_forward(cgo_crypt_t *cgo, struct cell_t *cell, const uint8_t **recognized_tag_out)
struct cgo_crypt_t cgo_crypt_t
void cgo_crypt_free_(cgo_crypt_t *cgo)
cgo_crypt_t * cgo_crypt_new(cgo_mode_t mode, int aesbits, const uint8_t *keys, size_t keylen)
void cgo_crypt_relay_backward(cgo_crypt_t *cgo, struct cell_t *cell)
void cgo_crypt_relay_originate(cgo_crypt_t *cgo, struct cell_t *cell, const uint8_t **tag_out)
Definition: cell_st.h:17
Macros to implement mocking and selective exposure for the test code.
#define STATIC
Definition: testsupport.h:32