Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/trust_token/internal.h

raw
1 // Copyright (c) 2019, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #ifndef OPENSSL_HEADER_TRUST_TOKEN_INTERNAL_H
5 #define OPENSSL_HEADER_TRUST_TOKEN_INTERNAL_H
6
7 #include <openssl/base.h>
8 #include <openssl/ec.h>
9 #include <openssl/ec_key.h>
10 #include <openssl/nid.h>
11
12 #include "../fipsmodule/ec/internal.h"
13
14 #include <openssl/trust_token.h>
15
16
17 #if defined(__cplusplus)
18 extern "C" {
19 #endif
20
21
22 // For the following cryptographic schemes, we use P-384 instead of our usual
23 // choice of P-256. See Appendix I of
24 // https://eprint.iacr.org/2020/072/20200324:214215 which describes two attacks
25 // which may affect smaller curves. In particular, p-1 for P-256 is smooth,
26 // giving a low complexity for the p-1 attack. P-384's p-1 has a 281-bit prime
27 // factor,
28 // 3055465788140352002733946906144561090641249606160407884365391979704929268480326390471.
29 // This lower-bounds the p-1 attack at O(2^140). The p+1 attack is lower-bounded
30 // by O(p^(1/3)) or O(2^128), so we do not need to check the smoothness of p+1.
31
32
33 // TRUST_TOKEN_NONCE_SIZE is the size of nonces used as part of the Trust_Token
34 // protocol.
35 #define TRUST_TOKEN_NONCE_SIZE 64
36
37 typedef struct {
38 // TODO(https://crbug.com/boringssl/334): These should store |EC_PRECOMP| so
39 // that |TRUST_TOKEN_finish_issuance| can use |ec_point_mul_scalar_precomp|.
40 EC_AFFINE pub0;
41 EC_AFFINE pub1;
42 EC_AFFINE pubs;
43 } TRUST_TOKEN_CLIENT_KEY;
44
45 typedef struct {
46 EC_SCALAR x0;
47 EC_SCALAR y0;
48 EC_SCALAR x1;
49 EC_SCALAR y1;
50 EC_SCALAR xs;
51 EC_SCALAR ys;
52 EC_AFFINE pub0;
53 EC_PRECOMP pub0_precomp;
54 EC_AFFINE pub1;
55 EC_PRECOMP pub1_precomp;
56 EC_AFFINE pubs;
57 EC_PRECOMP pubs_precomp;
58 } TRUST_TOKEN_ISSUER_KEY;
59
60 // TRUST_TOKEN_PRETOKEN represents the intermediate state a client keeps during
61 // a Trust_Token issuance operation.
62 typedef struct pmb_pretoken_st {
63 uint8_t salt[TRUST_TOKEN_NONCE_SIZE];
64 uint8_t t[TRUST_TOKEN_NONCE_SIZE];
65 EC_SCALAR r;
66 EC_AFFINE Tp;
67 } TRUST_TOKEN_PRETOKEN;
68
69 // TRUST_TOKEN_PRETOKEN_free releases the memory associated with |token|.
70 OPENSSL_EXPORT void TRUST_TOKEN_PRETOKEN_free(TRUST_TOKEN_PRETOKEN *token);
71
72 DEFINE_STACK_OF(TRUST_TOKEN_PRETOKEN)
73
74
75 // PMBTokens.
76 //
77 // PMBTokens is described in https://eprint.iacr.org/2020/072/20200324:214215
78 // and provides anonymous tokens with private metadata. We implement the
79 // construction with validity verification, described in appendix H,
80 // construction 6.
81
82 // The following functions implement the corresponding |TRUST_TOKENS_METHOD|
83 // functions for |TRUST_TOKENS_experiment_v1|'s PMBTokens construction which
84 // uses P-384.
85 int pmbtoken_exp1_generate_key(CBB *out_private, CBB *out_public);
86 int pmbtoken_exp1_derive_key_from_secret(CBB *out_private, CBB *out_public,
87 const uint8_t *secret,
88 size_t secret_len);
89 int pmbtoken_exp1_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
90 const uint8_t *in, size_t len);
91 int pmbtoken_exp1_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
92 const uint8_t *in, size_t len);
93 STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_exp1_blind(CBB *cbb, size_t count,
94 int include_message,
95 const uint8_t *msg,
96 size_t msg_len);
97 int pmbtoken_exp1_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
98 size_t num_requested, size_t num_to_issue,
99 uint8_t private_metadata);
100 STACK_OF(TRUST_TOKEN) *pmbtoken_exp1_unblind(
101 const TRUST_TOKEN_CLIENT_KEY *key,
102 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
103 uint32_t key_id);
104 int pmbtoken_exp1_read(const TRUST_TOKEN_ISSUER_KEY *key,
105 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
106 uint8_t *out_private_metadata, const uint8_t *token,
107 size_t token_len, int include_message,
108 const uint8_t *msg, size_t msg_len);
109
110 // pmbtoken_exp1_get_h_for_testing returns H in uncompressed coordinates. This
111 // function is used to confirm H was computed as expected.
112 OPENSSL_EXPORT int pmbtoken_exp1_get_h_for_testing(uint8_t out[97]);
113
114 // The following functions implement the corresponding |TRUST_TOKENS_METHOD|
115 // functions for |TRUST_TOKENS_experiment_v2|'s PMBTokens construction which
116 // uses P-384.
117 int pmbtoken_exp2_generate_key(CBB *out_private, CBB *out_public);
118 int pmbtoken_exp2_derive_key_from_secret(CBB *out_private, CBB *out_public,
119 const uint8_t *secret,
120 size_t secret_len);
121 int pmbtoken_exp2_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
122 const uint8_t *in, size_t len);
123 int pmbtoken_exp2_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
124 const uint8_t *in, size_t len);
125 STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_exp2_blind(CBB *cbb, size_t count,
126 int include_message,
127 const uint8_t *msg,
128 size_t msg_len);
129 int pmbtoken_exp2_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
130 size_t num_requested, size_t num_to_issue,
131 uint8_t private_metadata);
132 STACK_OF(TRUST_TOKEN) *pmbtoken_exp2_unblind(
133 const TRUST_TOKEN_CLIENT_KEY *key,
134 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
135 uint32_t key_id);
136 int pmbtoken_exp2_read(const TRUST_TOKEN_ISSUER_KEY *key,
137 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
138 uint8_t *out_private_metadata, const uint8_t *token,
139 size_t token_len, int include_message,
140 const uint8_t *msg, size_t msg_len);
141
142 // pmbtoken_exp2_get_h_for_testing returns H in uncompressed coordinates. This
143 // function is used to confirm H was computed as expected.
144 OPENSSL_EXPORT int pmbtoken_exp2_get_h_for_testing(uint8_t out[97]);
145
146 // The following functions implement the corresponding |TRUST_TOKENS_METHOD|
147 // functions for |TRUST_TOKENS_pst_v1|'s PMBTokens construction which uses
148 // P-384.
149 int pmbtoken_pst1_generate_key(CBB *out_private, CBB *out_public);
150 int pmbtoken_pst1_derive_key_from_secret(CBB *out_private, CBB *out_public,
151 const uint8_t *secret,
152 size_t secret_len);
153 int pmbtoken_pst1_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
154 const uint8_t *in, size_t len);
155 int pmbtoken_pst1_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
156 const uint8_t *in, size_t len);
157 STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_pst1_blind(CBB *cbb, size_t count,
158 int include_message,
159 const uint8_t *msg,
160 size_t msg_len);
161 int pmbtoken_pst1_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
162 size_t num_requested, size_t num_to_issue,
163 uint8_t private_metadata);
164 STACK_OF(TRUST_TOKEN) *pmbtoken_pst1_unblind(
165 const TRUST_TOKEN_CLIENT_KEY *key,
166 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
167 uint32_t key_id);
168 int pmbtoken_pst1_read(const TRUST_TOKEN_ISSUER_KEY *key,
169 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
170 uint8_t *out_private_metadata, const uint8_t *token,
171 size_t token_len, int include_message,
172 const uint8_t *msg, size_t msg_len);
173
174 // pmbtoken_pst1_get_h_for_testing returns H in uncompressed coordinates. This
175 // function is used to confirm H was computed as expected.
176 OPENSSL_EXPORT int pmbtoken_pst1_get_h_for_testing(uint8_t out[97]);
177
178
179 // VOPRF.
180 //
181 // VOPRFs are described in https://tools.ietf.org/html/draft-irtf-cfrg-voprf-04
182 // and provide anonymous tokens. This implementation uses TrustToken DSTs and
183 // the DLEQ batching primitive from
184 // https://eprint.iacr.org/2020/072/20200324:214215.
185 // VOPRF only uses the |pub|' field of the TRUST_TOKEN_CLIENT_KEY and
186 // |xs|/|pubs| fields of the TRUST_TOKEN_ISSUER_KEY.
187
188 // The following functions implement the corresponding |TRUST_TOKENS_METHOD|
189 // functions for |TRUST_TOKENS_experiment_v2|'s VOPRF construction which uses
190 // P-384.
191 int voprf_exp2_generate_key(CBB *out_private, CBB *out_public);
192 int voprf_exp2_derive_key_from_secret(CBB *out_private, CBB *out_public,
193 const uint8_t *secret, size_t secret_len);
194 int voprf_exp2_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
195 const uint8_t *in, size_t len);
196 int voprf_exp2_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
197 const uint8_t *in, size_t len);
198 STACK_OF(TRUST_TOKEN_PRETOKEN) *voprf_exp2_blind(CBB *cbb, size_t count,
199 int include_message,
200 const uint8_t *msg,
201 size_t msg_len);
202 int voprf_exp2_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
203 size_t num_requested, size_t num_to_issue,
204 uint8_t private_metadata);
205 STACK_OF(TRUST_TOKEN) *voprf_exp2_unblind(
206 const TRUST_TOKEN_CLIENT_KEY *key,
207 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
208 uint32_t key_id);
209 int voprf_exp2_read(const TRUST_TOKEN_ISSUER_KEY *key,
210 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
211 uint8_t *out_private_metadata, const uint8_t *token,
212 size_t token_len, int include_message, const uint8_t *msg,
213 size_t msg_len);
214
215 // The following functions implement the corresponding |TRUST_TOKENS_METHOD|
216 // functions for |TRUST_TOKENS_pst_v1|'s VOPRF construction which uses P-384.
217 int voprf_pst1_generate_key(CBB *out_private, CBB *out_public);
218 int voprf_pst1_derive_key_from_secret(CBB *out_private, CBB *out_public,
219 const uint8_t *secret, size_t secret_len);
220 int voprf_pst1_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
221 const uint8_t *in, size_t len);
222 int voprf_pst1_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
223 const uint8_t *in, size_t len);
224 STACK_OF(TRUST_TOKEN_PRETOKEN) *voprf_pst1_blind(CBB *cbb, size_t count,
225 int include_message,
226 const uint8_t *msg,
227 size_t msg_len);
228 int voprf_pst1_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
229 size_t num_requested, size_t num_to_issue,
230 uint8_t private_metadata);
231 OPENSSL_EXPORT int voprf_pst1_sign_with_proof_scalar_for_testing(
232 const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs, size_t num_requested,
233 size_t num_to_issue, uint8_t private_metadata,
234 const uint8_t *proof_scalar_buf, size_t proof_scalar_len);
235 STACK_OF(TRUST_TOKEN) *voprf_pst1_unblind(
236 const TRUST_TOKEN_CLIENT_KEY *key,
237 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
238 uint32_t key_id);
239 int voprf_pst1_read(const TRUST_TOKEN_ISSUER_KEY *key,
240 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
241 uint8_t *out_private_metadata, const uint8_t *token,
242 size_t token_len, int include_message, const uint8_t *msg,
243 size_t msg_len);
244
245
246 // Trust Tokens internals.
247
248 struct trust_token_method_st {
249 // generate_key generates a fresh keypair and writes their serialized
250 // forms into |out_private| and |out_public|. It returns one on success and
251 // zero on failure.
252 int (*generate_key)(CBB *out_private, CBB *out_public);
253
254 // derive_key_from_secret deterministically derives a keypair based on
255 // |secret| and writes their serialized forms into |out_private| and
256 // |out_public|. It returns one on success and zero on failure.
257 int (*derive_key_from_secret)(CBB *out_private, CBB *out_public,
258 const uint8_t *secret, size_t secret_len);
259
260 // client_key_from_bytes decodes a client key from |in| and sets |key|
261 // to the resulting key. It returns one on success and zero
262 // on failure.
263 int (*client_key_from_bytes)(TRUST_TOKEN_CLIENT_KEY *key, const uint8_t *in,
264 size_t len);
265
266 // issuer_key_from_bytes decodes a issuer key from |in| and sets |key|
267 // to the resulting key. It returns one on success and zero
268 // on failure.
269 int (*issuer_key_from_bytes)(TRUST_TOKEN_ISSUER_KEY *key, const uint8_t *in,
270 size_t len);
271
272 // blind generates a new issuance request for |count| tokens. If
273 // |include_message| is set, then |msg| is used to derive the token nonces. On
274 // success, it returns a newly-allocated |STACK_OF(TRUST_TOKEN_PRETOKEN)| and
275 // writes a request to the issuer to |cbb|. On failure, it returns NULL. The
276 // |STACK_OF(TRUST_TOKEN_PRETOKEN)|s should be passed to |pmbtoken_unblind|
277 // when the server responds.
278 //
279 // This function implements the AT.Usr0 operation.
280 STACK_OF(TRUST_TOKEN_PRETOKEN) *(*blind)(CBB *cbb, size_t count,
281 int include_message,
282 const uint8_t *msg, size_t msg_len);
283
284 // sign parses a request for |num_requested| tokens from |cbs| and
285 // issues |num_to_issue| tokens with |key| and a private metadata value of
286 // |private_metadata|. It then writes the response to |cbb|. It returns one on
287 // success and zero on failure.
288 //
289 // This function implements the AT.Sig operation.
290 int (*sign)(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
291 size_t num_requested, size_t num_to_issue,
292 uint8_t private_metadata);
293
294 // unblind processes an issuance response for |count| tokens from |cbs|
295 // and unblinds the signed tokens. |pretokens| are the pre-tokens returned
296 // from the corresponding |blind| call. On success, the function returns a
297 // newly-allocated |STACK_OF(TRUST_TOKEN)| containing the resulting tokens.
298 // Each token's serialization will have |key_id| prepended. Otherwise, it
299 // returns NULL.
300 //
301 // This function implements the AT.Usr1 operation.
302 STACK_OF(TRUST_TOKEN) *(*unblind)(
303 const TRUST_TOKEN_CLIENT_KEY *key,
304 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
305 uint32_t key_id);
306
307 // read parses a token from |token| and verifies it using |key|. If
308 // |include_message| is set, then the nonce is derived from |msg| and the salt
309 // in the token. On success, it returns one and stores the nonce and private
310 // metadata bit in |out_nonce| and |*out_private_metadata|. Otherwise, it
311 // returns zero. Note that, unlike the output of |unblind|, |token| does not
312 // have a four-byte key ID prepended.
313 int (*read)(const TRUST_TOKEN_ISSUER_KEY *key,
314 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
315 uint8_t *out_private_metadata, const uint8_t *token,
316 size_t token_len, int include_message, const uint8_t *msg,
317 size_t msg_len);
318
319 // whether the construction supports private metadata.
320 int has_private_metadata;
321
322 // max keys that can be configured.
323 size_t max_keys;
324
325 // whether the SRR is part of the protocol.
326 int has_srr;
327 };
328
329 // Structure representing a single Trust Token public key with the specified ID.
330 struct trust_token_client_key_st {
331 uint32_t id;
332 TRUST_TOKEN_CLIENT_KEY key;
333 };
334
335 // Structure representing a single Trust Token private key with the specified
336 // ID.
337 struct trust_token_issuer_key_st {
338 uint32_t id;
339 TRUST_TOKEN_ISSUER_KEY key;
340 };
341
342 struct trust_token_client_st {
343 const TRUST_TOKEN_METHOD *method;
344
345 // max_batchsize is the maximum supported batchsize.
346 uint16_t max_batchsize;
347
348 // keys is the set of public keys that are supported by the client for
349 // issuance/redemptions.
350 struct trust_token_client_key_st keys[6];
351
352 // num_keys is the number of keys currently configured.
353 size_t num_keys;
354
355 // pretokens is the intermediate state during an active issuance.
356 STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens;
357
358 // srr_key is the public key used to verify the signature of the SRR.
359 EVP_PKEY *srr_key;
360 };
361
362
363 struct trust_token_issuer_st {
364 const TRUST_TOKEN_METHOD *method;
365
366 // max_batchsize is the maximum supported batchsize.
367 uint16_t max_batchsize;
368
369 // keys is the set of private keys that are supported by the issuer for
370 // issuance/redemptions. The public metadata is an index into this list of
371 // keys.
372 struct trust_token_issuer_key_st keys[6];
373
374 // num_keys is the number of keys currently configured.
375 size_t num_keys;
376
377 // srr_key is the private key used to sign the SRR.
378 EVP_PKEY *srr_key;
379
380 // metadata_key is the secret material used to encode the private metadata bit
381 // in the SRR.
382 uint8_t *metadata_key;
383 size_t metadata_key_len;
384 };
385
386
387 #if defined(__cplusplus)
388 } // extern C
389
390 extern "C++" {
391
392 BSSL_NAMESPACE_BEGIN
393
394 BORINGSSL_MAKE_DELETER(TRUST_TOKEN_PRETOKEN, TRUST_TOKEN_PRETOKEN_free)
395
396 BSSL_NAMESPACE_END
397
398 } // extern C++
399 #endif
400
401 #endif // OPENSSL_HEADER_TRUST_TOKEN_INTERNAL_H