Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/trust_token/voprf.c

raw
1 // Copyright (c) 2020, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/trust_token.h>
5
6 #include <openssl/bn.h>
7 #include <openssl/bytestring.h>
8 #include <openssl/ec.h>
9 #include <openssl/err.h>
10 #include <openssl/mem.h>
11 #include <openssl/nid.h>
12 #include <openssl/rand.h>
13 #include <openssl/sha.h>
14
15 #include "../ec_extra/internal.h"
16 #include "../fipsmodule/ec/internal.h"
17
18 #include "internal.h"
19
20
21 typedef int (*hash_to_group_func_t)(const EC_GROUP *group, EC_JACOBIAN *out,
22 const uint8_t t[TRUST_TOKEN_NONCE_SIZE]);
23 typedef int (*hash_to_scalar_func_t)(const EC_GROUP *group, EC_SCALAR *out,
24 uint8_t *buf, size_t len);
25
26 typedef struct {
27 const EC_GROUP *(*group_func)(void);
28
29 // hash_to_group implements the HashToGroup operation for VOPRFs. It returns
30 // one on success and zero on error.
31 hash_to_group_func_t hash_to_group;
32 // hash_to_scalar implements the HashToScalar operation for VOPRFs. It returns
33 // one on success and zero on error.
34 hash_to_scalar_func_t hash_to_scalar;
35 } VOPRF_METHOD;
36
37 static const uint8_t kDefaultAdditionalData[32] = {0};
38
39 static int cbb_add_point(CBB *out, const EC_GROUP *group,
40 const EC_AFFINE *point) {
41 uint8_t *p;
42 size_t len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
43 return CBB_add_space(out, &p, len) &&
44 ec_point_to_bytes(group, point, POINT_CONVERSION_UNCOMPRESSED, p,
45 len) == len &&
46 CBB_flush(out);
47 }
48
49 static int cbb_serialize_point(CBB *out, const EC_GROUP *group,
50 const EC_AFFINE *point) {
51 uint8_t *p;
52 size_t len = ec_point_byte_len(group, POINT_CONVERSION_COMPRESSED);
53 return CBB_add_u16(out, len) && CBB_add_space(out, &p, len) &&
54 ec_point_to_bytes(group, point, POINT_CONVERSION_COMPRESSED, p, len) ==
55 len &&
56 CBB_flush(out);
57 }
58
59 static int cbs_get_point(CBS *cbs, const EC_GROUP *group, EC_AFFINE *out) {
60 CBS child;
61 size_t plen = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
62 if (!CBS_get_bytes(cbs, &child, plen) ||
63 !ec_point_from_uncompressed(group, out, CBS_data(&child),
64 CBS_len(&child))) {
65 return 0;
66 }
67 return 1;
68 }
69
70 static int scalar_to_cbb(CBB *out, const EC_GROUP *group,
71 const EC_SCALAR *scalar) {
72 uint8_t *buf;
73 size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group));
74 if (!CBB_add_space(out, &buf, scalar_len)) {
75 return 0;
76 }
77 ec_scalar_to_bytes(group, buf, &scalar_len, scalar);
78 return 1;
79 }
80
81 static int scalar_from_cbs(CBS *cbs, const EC_GROUP *group, EC_SCALAR *out) {
82 size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group));
83 CBS tmp;
84 if (!CBS_get_bytes(cbs, &tmp, scalar_len)) {
85 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
86 return 0;
87 }
88
89 ec_scalar_from_bytes(group, out, CBS_data(&tmp), CBS_len(&tmp));
90 return 1;
91 }
92
93 static int voprf_calculate_key(const VOPRF_METHOD *method, CBB *out_private,
94 CBB *out_public, const EC_SCALAR *priv) {
95 const EC_GROUP *group = method->group_func();
96 EC_JACOBIAN pub;
97 EC_AFFINE pub_affine;
98 if (!ec_point_mul_scalar_base(group, &pub, priv) ||
99 !ec_jacobian_to_affine(group, &pub_affine, &pub)) {
100 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE);
101 return 0;
102 }
103
104 if (!scalar_to_cbb(out_private, group, priv) ||
105 !cbb_add_point(out_public, group, &pub_affine)) {
106 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BUFFER_TOO_SMALL);
107 return 0;
108 }
109
110 return 1;
111 }
112
113
114 static int voprf_generate_key(const VOPRF_METHOD *method, CBB *out_private,
115 CBB *out_public) {
116 EC_SCALAR priv;
117 if (!ec_random_nonzero_scalar(method->group_func(), &priv,
118 kDefaultAdditionalData)) {
119 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE);
120 return 0;
121 }
122 return voprf_calculate_key(method, out_private, out_public, &priv);
123 }
124
125 static int voprf_derive_key_from_secret(const VOPRF_METHOD *method,
126 CBB *out_private, CBB *out_public,
127 const uint8_t *secret,
128 size_t secret_len) {
129 static const uint8_t kKeygenLabel[] = "TrustTokenVOPRFKeyGen";
130
131 EC_SCALAR priv;
132 int ok = 0;
133 CBB cbb;
134 CBB_zero(&cbb);
135 uint8_t *buf = NULL;
136 size_t len;
137 if (!CBB_init(&cbb, 0) ||
138 !CBB_add_bytes(&cbb, kKeygenLabel, sizeof(kKeygenLabel)) ||
139 !CBB_add_bytes(&cbb, secret, secret_len) ||
140 !CBB_finish(&cbb, &buf, &len) ||
141 !method->hash_to_scalar(method->group_func(), &priv, buf, len)) {
142 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE);
143 goto err;
144 }
145
146 ok = voprf_calculate_key(method, out_private, out_public, &priv);
147
148 err:
149 CBB_cleanup(&cbb);
150 OPENSSL_free(buf);
151 return ok;
152 }
153
154 static int voprf_client_key_from_bytes(const VOPRF_METHOD *method,
155 TRUST_TOKEN_CLIENT_KEY *key,
156 const uint8_t *in, size_t len) {
157 const EC_GROUP *group = method->group_func();
158 if (!ec_point_from_uncompressed(group, &key->pubs, in, len)) {
159 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
160 return 0;
161 }
162
163 return 1;
164 }
165
166 static int voprf_issuer_key_from_bytes(const VOPRF_METHOD *method,
167 TRUST_TOKEN_ISSUER_KEY *key,
168 const uint8_t *in, size_t len) {
169 const EC_GROUP *group = method->group_func();
170 if (!ec_scalar_from_bytes(group, &key->xs, in, len)) {
171 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
172 return 0;
173 }
174
175 // Recompute the public key.
176 EC_JACOBIAN pub;
177 if (!ec_point_mul_scalar_base(group, &pub, &key->xs) ||
178 !ec_jacobian_to_affine(group, &key->pubs, &pub)) {
179 return 0;
180 }
181
182 return 1;
183 }
184
185 static STACK_OF(TRUST_TOKEN_PRETOKEN) *voprf_blind(const VOPRF_METHOD *method,
186 CBB *cbb, size_t count,
187 int include_message,
188 const uint8_t *msg,
189 size_t msg_len) {
190 SHA512_CTX hash_ctx;
191
192 const EC_GROUP *group = method->group_func();
193 STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens =
194 sk_TRUST_TOKEN_PRETOKEN_new_null();
195 if (pretokens == NULL) {
196 goto err;
197 }
198
199 for (size_t i = 0; i < count; i++) {
200 // Insert |pretoken| into |pretokens| early to simplify error-handling.
201 TRUST_TOKEN_PRETOKEN *pretoken =
202 OPENSSL_malloc(sizeof(TRUST_TOKEN_PRETOKEN));
203 if (pretoken == NULL ||
204 !sk_TRUST_TOKEN_PRETOKEN_push(pretokens, pretoken)) {
205 TRUST_TOKEN_PRETOKEN_free(pretoken);
206 goto err;
207 }
208
209 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(pretoken->salt, sizeof(pretoken->salt)));
210 if (include_message) {
211 assert(SHA512_DIGEST_LENGTH == TRUST_TOKEN_NONCE_SIZE);
212 SHA512_Init(&hash_ctx);
213 SHA512_Update(&hash_ctx, pretoken->salt, sizeof(pretoken->salt));
214 SHA512_Update(&hash_ctx, msg, msg_len);
215 SHA512_Final(pretoken->t, &hash_ctx);
216 } else {
217 OPENSSL_memcpy(pretoken->t, pretoken->salt, TRUST_TOKEN_NONCE_SIZE);
218 }
219
220 // We sample r in Montgomery form to simplify inverting.
221 EC_SCALAR r;
222 if (!ec_random_nonzero_scalar(group, &r,
223 kDefaultAdditionalData)) {
224 goto err;
225 }
226
227 // pretoken->r is rinv.
228 ec_scalar_inv0_montgomery(group, &pretoken->r, &r);
229 // Convert both out of Montgomery form.
230 ec_scalar_from_montgomery(group, &r, &r);
231 ec_scalar_from_montgomery(group, &pretoken->r, &pretoken->r);
232
233 // Tp is the blinded token in the VOPRF protocol.
234 EC_JACOBIAN P, Tp;
235 if (!method->hash_to_group(group, &P, pretoken->t) ||
236 !ec_point_mul_scalar(group, &Tp, &P, &r) ||
237 !ec_jacobian_to_affine(group, &pretoken->Tp, &Tp)) {
238 goto err;
239 }
240
241 if (!cbb_add_point(cbb, group, &pretoken->Tp)) {
242 goto err;
243 }
244 }
245
246 return pretokens;
247
248 err:
249 sk_TRUST_TOKEN_PRETOKEN_pop_free(pretokens, TRUST_TOKEN_PRETOKEN_free);
250 return NULL;
251 }
252
253 static int hash_to_scalar_dleq(const VOPRF_METHOD *method, EC_SCALAR *out,
254 const EC_AFFINE *X, const EC_AFFINE *T,
255 const EC_AFFINE *W, const EC_AFFINE *K0,
256 const EC_AFFINE *K1) {
257 static const uint8_t kDLEQLabel[] = "DLEQ";
258
259 const EC_GROUP *group = method->group_func();
260 int ok = 0;
261 CBB cbb;
262 CBB_zero(&cbb);
263 uint8_t *buf = NULL;
264 size_t len;
265 if (!CBB_init(&cbb, 0) ||
266 !CBB_add_bytes(&cbb, kDLEQLabel, sizeof(kDLEQLabel)) ||
267 !cbb_add_point(&cbb, group, X) ||
268 !cbb_add_point(&cbb, group, T) ||
269 !cbb_add_point(&cbb, group, W) ||
270 !cbb_add_point(&cbb, group, K0) ||
271 !cbb_add_point(&cbb, group, K1) ||
272 !CBB_finish(&cbb, &buf, &len) ||
273 !method->hash_to_scalar(group, out, buf, len)) {
274 goto err;
275 }
276
277 ok = 1;
278
279 err:
280 CBB_cleanup(&cbb);
281 OPENSSL_free(buf);
282 return ok;
283 }
284
285 static int hash_to_scalar_challenge(const VOPRF_METHOD *method, EC_SCALAR *out,
286 const EC_AFFINE *Bm, const EC_AFFINE *a0,
287 const EC_AFFINE *a1, const EC_AFFINE *a2,
288 const EC_AFFINE *a3) {
289 static const uint8_t kChallengeLabel[] = "Challenge";
290
291 const EC_GROUP *group = method->group_func();
292 CBB cbb;
293 uint8_t transcript[5 * EC_MAX_COMPRESSED + 2 + sizeof(kChallengeLabel) - 1];
294 size_t len;
295 if (!CBB_init_fixed(&cbb, transcript, sizeof(transcript)) ||
296 !cbb_serialize_point(&cbb, group, Bm) ||
297 !cbb_serialize_point(&cbb, group, a0) ||
298 !cbb_serialize_point(&cbb, group, a1) ||
299 !cbb_serialize_point(&cbb, group, a2) ||
300 !cbb_serialize_point(&cbb, group, a3) ||
301 !CBB_add_bytes(&cbb, kChallengeLabel, sizeof(kChallengeLabel) - 1) ||
302 !CBB_finish(&cbb, NULL, &len) ||
303 !method->hash_to_scalar(group, out, transcript, len)) {
304 return 0;
305 }
306
307 return 1;
308 }
309
310 static int hash_to_scalar_batch(const VOPRF_METHOD *method, EC_SCALAR *out,
311 const CBB *points, size_t index) {
312 static const uint8_t kDLEQBatchLabel[] = "DLEQ BATCH";
313 if (index > 0xffff) {
314 // The protocol supports only two-byte batches.
315 OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_OVERFLOW);
316 return 0;
317 }
318
319 int ok = 0;
320 CBB cbb;
321 CBB_zero(&cbb);
322 uint8_t *buf = NULL;
323 size_t len;
324 if (!CBB_init(&cbb, 0) ||
325 !CBB_add_bytes(&cbb, kDLEQBatchLabel, sizeof(kDLEQBatchLabel)) ||
326 !CBB_add_bytes(&cbb, CBB_data(points), CBB_len(points)) ||
327 !CBB_add_u16(&cbb, (uint16_t)index) ||
328 !CBB_finish(&cbb, &buf, &len) ||
329 !method->hash_to_scalar(method->group_func(), out, buf, len)) {
330 goto err;
331 }
332
333 ok = 1;
334
335 err:
336 CBB_cleanup(&cbb);
337 OPENSSL_free(buf);
338 return ok;
339 }
340
341 static int dleq_generate(const VOPRF_METHOD *method, CBB *cbb,
342 const TRUST_TOKEN_ISSUER_KEY *priv,
343 const EC_JACOBIAN *T, const EC_JACOBIAN *W) {
344 const EC_GROUP *group = method->group_func();
345
346 enum {
347 idx_T,
348 idx_W,
349 idx_k0,
350 idx_k1,
351 num_idx,
352 };
353 EC_JACOBIAN jacobians[num_idx];
354
355 // Setup the DLEQ proof.
356 EC_SCALAR r;
357 if (// r <- Zp
358 !ec_random_nonzero_scalar(group, &r, kDefaultAdditionalData) ||
359 // k0;k1 = r*(G;T)
360 !ec_point_mul_scalar_base(group, &jacobians[idx_k0], &r) ||
361 !ec_point_mul_scalar(group, &jacobians[idx_k1], T, &r)) {
362 return 0;
363 }
364
365 EC_AFFINE affines[num_idx];
366 jacobians[idx_T] = *T;
367 jacobians[idx_W] = *W;
368 if (!ec_jacobian_to_affine_batch(group, affines, jacobians, num_idx)) {
369 return 0;
370 }
371
372 // Compute c = Hc(...).
373 EC_SCALAR c;
374 if (!hash_to_scalar_dleq(method, &c, &priv->pubs, &affines[idx_T],
375 &affines[idx_W], &affines[idx_k0],
376 &affines[idx_k1])) {
377 return 0;
378 }
379
380
381 EC_SCALAR c_mont;
382 ec_scalar_to_montgomery(group, &c_mont, &c);
383
384 // u = r + c*xs
385 EC_SCALAR u;
386 ec_scalar_mul_montgomery(group, &u, &priv->xs, &c_mont);
387 ec_scalar_add(group, &u, &r, &u);
388
389 // Store DLEQ proof in transcript.
390 if (!scalar_to_cbb(cbb, group, &c) ||
391 !scalar_to_cbb(cbb, group, &u)) {
392 return 0;
393 }
394
395 return 1;
396 }
397
398 static int mul_public_2(const EC_GROUP *group, EC_JACOBIAN *out,
399 const EC_JACOBIAN *p0, const EC_SCALAR *scalar0,
400 const EC_JACOBIAN *p1, const EC_SCALAR *scalar1) {
401 EC_JACOBIAN points[2] = {*p0, *p1};
402 EC_SCALAR scalars[2] = {*scalar0, *scalar1};
403 return ec_point_mul_scalar_public_batch(group, out, /*g_scalar=*/NULL, points,
404 scalars, 2);
405 }
406
407 static int dleq_verify(const VOPRF_METHOD *method, CBS *cbs,
408 const TRUST_TOKEN_CLIENT_KEY *pub, const EC_JACOBIAN *T,
409 const EC_JACOBIAN *W) {
410 const EC_GROUP *group = method->group_func();
411
412
413 enum {
414 idx_T,
415 idx_W,
416 idx_k0,
417 idx_k1,
418 num_idx,
419 };
420 EC_JACOBIAN jacobians[num_idx];
421
422 // Decode the DLEQ proof.
423 EC_SCALAR c, u;
424 if (!scalar_from_cbs(cbs, group, &c) ||
425 !scalar_from_cbs(cbs, group, &u)) {
426 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
427 return 0;
428 }
429
430 // k0;k1 = u*(G;T) - c*(pub;W)
431 EC_JACOBIAN pubs;
432 ec_affine_to_jacobian(group, &pubs, &pub->pubs);
433 EC_SCALAR minus_c;
434 ec_scalar_neg(group, &minus_c, &c);
435 if (!ec_point_mul_scalar_public(group, &jacobians[idx_k0], &u, &pubs,
436 &minus_c) ||
437 !mul_public_2(group, &jacobians[idx_k1], T, &u, W, &minus_c)) {
438 return 0;
439 }
440
441 // Check the DLEQ proof.
442 EC_AFFINE affines[num_idx];
443 jacobians[idx_T] = *T;
444 jacobians[idx_W] = *W;
445 if (!ec_jacobian_to_affine_batch(group, affines, jacobians, num_idx)) {
446 return 0;
447 }
448
449 // Compute c = Hc(...).
450 EC_SCALAR calculated;
451 if (!hash_to_scalar_dleq(method, &calculated, &pub->pubs, &affines[idx_T],
452 &affines[idx_W], &affines[idx_k0],
453 &affines[idx_k1])) {
454 return 0;
455 }
456
457 // c == calculated
458 if (!ec_scalar_equal_vartime(group, &c, &calculated)) {
459 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_PROOF);
460 return 0;
461 }
462
463 return 1;
464 }
465
466 static int voprf_sign_tt(const VOPRF_METHOD *method,
467 const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
468 size_t num_requested, size_t num_to_issue) {
469 const EC_GROUP *group = method->group_func();
470 if (num_requested < num_to_issue) {
471 OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR);
472 return 0;
473 }
474
475 int ret = 0;
476 EC_JACOBIAN *BTs = OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN));
477 EC_JACOBIAN *Zs = OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN));
478 EC_SCALAR *es = OPENSSL_calloc(num_to_issue, sizeof(EC_SCALAR));
479 CBB batch_cbb;
480 CBB_zero(&batch_cbb);
481 if (!BTs ||
482 !Zs ||
483 !es ||
484 !CBB_init(&batch_cbb, 0) ||
485 !cbb_add_point(&batch_cbb, group, &key->pubs)) {
486 goto err;
487 }
488
489 for (size_t i = 0; i < num_to_issue; i++) {
490 EC_AFFINE BT_affine, Z_affine;
491 EC_JACOBIAN BT, Z;
492 if (!cbs_get_point(cbs, group, &BT_affine)) {
493 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
494 goto err;
495 }
496 ec_affine_to_jacobian(group, &BT, &BT_affine);
497 if (!ec_point_mul_scalar(group, &Z, &BT, &key->xs) ||
498 !ec_jacobian_to_affine(group, &Z_affine, &Z) ||
499 !cbb_add_point(cbb, group, &Z_affine)) {
500 goto err;
501 }
502
503 if (!cbb_add_point(&batch_cbb, group, &BT_affine) ||
504 !cbb_add_point(&batch_cbb, group, &Z_affine)) {
505 goto err;
506 }
507 BTs[i] = BT;
508 Zs[i] = Z;
509
510 if (!CBB_flush(cbb)) {
511 goto err;
512 }
513 }
514
515 // The DLEQ batching construction is described in appendix B of
516 // https://eprint.iacr.org/2020/072/20200324:214215. Note the additional
517 // computations all act on public inputs.
518 for (size_t i = 0; i < num_to_issue; i++) {
519 if (!hash_to_scalar_batch(method, &es[i], &batch_cbb, i)) {
520 goto err;
521 }
522 }
523
524 EC_JACOBIAN BT_batch, Z_batch;
525 if (!ec_point_mul_scalar_public_batch(group, &BT_batch,
526 /*g_scalar=*/NULL, BTs, es,
527 num_to_issue) ||
528 !ec_point_mul_scalar_public_batch(group, &Z_batch,
529 /*g_scalar=*/NULL, Zs, es,
530 num_to_issue)) {
531 goto err;
532 }
533
534 CBB proof;
535 if (!CBB_add_u16_length_prefixed(cbb, &proof) ||
536 !dleq_generate(method, &proof, key, &BT_batch, &Z_batch) ||
537 !CBB_flush(cbb)) {
538 goto err;
539 }
540
541 // Skip over any unused requests.
542 size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
543 if (!CBS_skip(cbs, point_len * (num_requested - num_to_issue))) {
544 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
545 goto err;
546 }
547
548 ret = 1;
549
550 err:
551 OPENSSL_free(BTs);
552 OPENSSL_free(Zs);
553 OPENSSL_free(es);
554 CBB_cleanup(&batch_cbb);
555 return ret;
556 }
557
558 static STACK_OF(TRUST_TOKEN) *voprf_unblind_tt(
559 const VOPRF_METHOD *method, const TRUST_TOKEN_CLIENT_KEY *key,
560 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
561 uint32_t key_id) {
562 const EC_GROUP *group = method->group_func();
563 if (count > sk_TRUST_TOKEN_PRETOKEN_num(pretokens)) {
564 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
565 return NULL;
566 }
567
568 int ok = 0;
569 STACK_OF(TRUST_TOKEN) *ret = sk_TRUST_TOKEN_new_null();
570 EC_JACOBIAN *BTs = OPENSSL_calloc(count, sizeof(EC_JACOBIAN));
571 EC_JACOBIAN *Zs = OPENSSL_calloc(count, sizeof(EC_JACOBIAN));
572 EC_SCALAR *es = OPENSSL_calloc(count, sizeof(EC_SCALAR));
573 CBB batch_cbb;
574 CBB_zero(&batch_cbb);
575 if (ret == NULL ||
576 BTs == NULL ||
577 Zs == NULL ||
578 es == NULL ||
579 !CBB_init(&batch_cbb, 0) ||
580 !cbb_add_point(&batch_cbb, group, &key->pubs)) {
581 goto err;
582 }
583
584 for (size_t i = 0; i < count; i++) {
585 const TRUST_TOKEN_PRETOKEN *pretoken =
586 sk_TRUST_TOKEN_PRETOKEN_value(pretokens, i);
587
588 EC_AFFINE Z_affine;
589 if (!cbs_get_point(cbs, group, &Z_affine)) {
590 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
591 goto err;
592 }
593
594 ec_affine_to_jacobian(group, &BTs[i], &pretoken->Tp);
595 ec_affine_to_jacobian(group, &Zs[i], &Z_affine);
596
597 if (!cbb_add_point(&batch_cbb, group, &pretoken->Tp) ||
598 !cbb_add_point(&batch_cbb, group, &Z_affine)) {
599 goto err;
600 }
601
602 // Unblind the token.
603 // pretoken->r is rinv.
604 EC_JACOBIAN N;
605 EC_AFFINE N_affine;
606 if (!ec_point_mul_scalar(group, &N, &Zs[i], &pretoken->r) ||
607 !ec_jacobian_to_affine(group, &N_affine, &N)) {
608 goto err;
609 }
610
611 // Serialize the token. Include |key_id| to avoid an extra copy in the layer
612 // above.
613 CBB token_cbb;
614 size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
615 if (!CBB_init(&token_cbb, 4 + TRUST_TOKEN_NONCE_SIZE + (2 + point_len)) ||
616 !CBB_add_u32(&token_cbb, key_id) ||
617 !CBB_add_bytes(&token_cbb, pretoken->salt, TRUST_TOKEN_NONCE_SIZE) ||
618 !cbb_add_point(&token_cbb, group, &N_affine) ||
619 !CBB_flush(&token_cbb)) {
620 CBB_cleanup(&token_cbb);
621 goto err;
622 }
623
624 TRUST_TOKEN *token =
625 TRUST_TOKEN_new(CBB_data(&token_cbb), CBB_len(&token_cbb));
626 CBB_cleanup(&token_cbb);
627 if (token == NULL ||
628 !sk_TRUST_TOKEN_push(ret, token)) {
629 TRUST_TOKEN_free(token);
630 goto err;
631 }
632 }
633
634 // The DLEQ batching construction is described in appendix B of
635 // https://eprint.iacr.org/2020/072/20200324:214215. Note the additional
636 // computations all act on public inputs.
637 for (size_t i = 0; i < count; i++) {
638 if (!hash_to_scalar_batch(method, &es[i], &batch_cbb, i)) {
639 goto err;
640 }
641 }
642
643 EC_JACOBIAN BT_batch, Z_batch;
644 if (!ec_point_mul_scalar_public_batch(group, &BT_batch,
645 /*g_scalar=*/NULL, BTs, es, count) ||
646 !ec_point_mul_scalar_public_batch(group, &Z_batch,
647 /*g_scalar=*/NULL, Zs, es, count)) {
648 goto err;
649 }
650
651 CBS proof;
652 if (!CBS_get_u16_length_prefixed(cbs, &proof) ||
653 !dleq_verify(method, &proof, key, &BT_batch, &Z_batch) ||
654 CBS_len(&proof) != 0) {
655 goto err;
656 }
657
658 ok = 1;
659
660 err:
661 OPENSSL_free(BTs);
662 OPENSSL_free(Zs);
663 OPENSSL_free(es);
664 CBB_cleanup(&batch_cbb);
665 if (!ok) {
666 sk_TRUST_TOKEN_pop_free(ret, TRUST_TOKEN_free);
667 ret = NULL;
668 }
669 return ret;
670 }
671
672 static void sha384_update_u16(SHA512_CTX *ctx, uint16_t v) {
673 uint8_t buf[2] = {v >> 8, v & 0xff};
674 SHA384_Update(ctx, buf, 2);
675 }
676
677 static void sha384_update_point_with_length(
678 SHA512_CTX *ctx, const EC_GROUP *group, const EC_AFFINE *point) {
679 uint8_t buf[EC_MAX_COMPRESSED];
680 size_t len = ec_point_to_bytes(group, point, POINT_CONVERSION_COMPRESSED,
681 buf, sizeof(buf));
682 assert(len > 0);
683 sha384_update_u16(ctx, (uint16_t)len);
684 SHA384_Update(ctx, buf, len);
685 }
686
687 static int compute_composite_seed(const VOPRF_METHOD *method,
688 uint8_t out[SHA384_DIGEST_LENGTH],
689 const EC_AFFINE *pub) {
690 const EC_GROUP *group = method->group_func();
691 static const uint8_t kSeedDST[] = "Seed-OPRFV1-\x01-P384-SHA384";
692
693 SHA512_CTX hash_ctx;
694 SHA384_Init(&hash_ctx);
695 sha384_update_point_with_length(&hash_ctx, group, pub);
696 sha384_update_u16(&hash_ctx, sizeof(kSeedDST) - 1);
697 SHA384_Update(&hash_ctx, kSeedDST, sizeof(kSeedDST) - 1);
698 SHA384_Final(out, &hash_ctx);
699
700 return 1;
701 }
702
703 static int compute_composite_element(const VOPRF_METHOD *method,
704 uint8_t seed[SHA384_DIGEST_LENGTH],
705 EC_SCALAR *di, size_t index,
706 const EC_AFFINE *C, const EC_AFFINE *D) {
707 static const uint8_t kCompositeLabel[] = "Composite";
708 const EC_GROUP *group = method->group_func();
709
710 if (index > UINT16_MAX) {
711 return 0;
712 }
713
714 CBB cbb;
715 uint8_t transcript[2 + SHA384_DIGEST_LENGTH + 2 + 2 * EC_MAX_COMPRESSED +
716 sizeof(kCompositeLabel) - 1];
717 size_t len;
718 if (!CBB_init_fixed(&cbb, transcript, sizeof(transcript)) ||
719 !CBB_add_u16(&cbb, SHA384_DIGEST_LENGTH) ||
720 !CBB_add_bytes(&cbb, seed, SHA384_DIGEST_LENGTH) ||
721 !CBB_add_u16(&cbb, index) ||
722 !cbb_serialize_point(&cbb, group, C) ||
723 !cbb_serialize_point(&cbb, group, D) ||
724 !CBB_add_bytes(&cbb, kCompositeLabel,
725 sizeof(kCompositeLabel) - 1) ||
726 !CBB_finish(&cbb, NULL, &len) ||
727 !method->hash_to_scalar(group, di, transcript, len)) {
728 return 0;
729 }
730
731 return 1;
732 }
733
734 static int generate_proof(const VOPRF_METHOD *method, CBB *cbb,
735 const TRUST_TOKEN_ISSUER_KEY *priv,
736 const EC_SCALAR *r, const EC_JACOBIAN *M,
737 const EC_JACOBIAN *Z) {
738 const EC_GROUP *group = method->group_func();
739
740 enum {
741 idx_M,
742 idx_Z,
743 idx_t2,
744 idx_t3,
745 num_idx,
746 };
747 EC_JACOBIAN jacobians[num_idx];
748
749 if (!ec_point_mul_scalar_base(group, &jacobians[idx_t2], r) ||
750 !ec_point_mul_scalar(group, &jacobians[idx_t3], M, r)) {
751 return 0;
752 }
753
754
755 EC_AFFINE affines[num_idx];
756 jacobians[idx_M] = *M;
757 jacobians[idx_Z] = *Z;
758 if (!ec_jacobian_to_affine_batch(group, affines, jacobians, num_idx)) {
759 return 0;
760 }
761
762 EC_SCALAR c;
763 if (!hash_to_scalar_challenge(method, &c, &priv->pubs, &affines[idx_M],
764 &affines[idx_Z], &affines[idx_t2],
765 &affines[idx_t3])) {
766 return 0;
767 }
768
769 EC_SCALAR c_mont;
770 ec_scalar_to_montgomery(group, &c_mont, &c);
771
772 // s = r - c*xs
773 EC_SCALAR s;
774 ec_scalar_mul_montgomery(group, &s, &priv->xs, &c_mont);
775 ec_scalar_sub(group, &s, r, &s);
776
777 // Store DLEQ proof in transcript.
778 if (!scalar_to_cbb(cbb, group, &c) ||
779 !scalar_to_cbb(cbb, group, &s)) {
780 return 0;
781 }
782
783 return 1;
784 }
785
786 static int verify_proof(const VOPRF_METHOD *method, CBS *cbs,
787 const TRUST_TOKEN_CLIENT_KEY *pub,
788 const EC_JACOBIAN *M, const EC_JACOBIAN *Z) {
789 const EC_GROUP *group = method->group_func();
790
791 enum {
792 idx_M,
793 idx_Z,
794 idx_t2,
795 idx_t3,
796 num_idx,
797 };
798 EC_JACOBIAN jacobians[num_idx];
799
800 EC_SCALAR c, s;
801 if (!scalar_from_cbs(cbs, group, &c) ||
802 !scalar_from_cbs(cbs, group, &s)) {
803 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
804 return 0;
805 }
806
807 EC_JACOBIAN pubs;
808 ec_affine_to_jacobian(group, &pubs, &pub->pubs);
809 if (!ec_point_mul_scalar_public(group, &jacobians[idx_t2], &s, &pubs,
810 &c) ||
811 !mul_public_2(group, &jacobians[idx_t3], M, &s, Z, &c)) {
812 return 0;
813 }
814
815 EC_AFFINE affines[num_idx];
816 jacobians[idx_M] = *M;
817 jacobians[idx_Z] = *Z;
818 if (!ec_jacobian_to_affine_batch(group, affines, jacobians, num_idx)) {
819 return 0;
820 }
821
822 EC_SCALAR expected_c;
823 if (!hash_to_scalar_challenge(method, &expected_c, &pub->pubs,
824 &affines[idx_M], &affines[idx_Z],
825 &affines[idx_t2], &affines[idx_t3])) {
826 return 0;
827 }
828
829 // c == expected_c
830 if (!ec_scalar_equal_vartime(group, &c, &expected_c)) {
831 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_PROOF);
832 return 0;
833 }
834
835 return 1;
836 }
837
838 static int voprf_sign_impl(const VOPRF_METHOD *method,
839 const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb,
840 CBS *cbs, size_t num_requested, size_t num_to_issue,
841 const EC_SCALAR *proof_scalar) {
842 const EC_GROUP *group = method->group_func();
843 if (num_requested < num_to_issue) {
844 OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR);
845 return 0;
846 }
847
848 int ret = 0;
849 EC_JACOBIAN *BTs = OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN));
850 EC_JACOBIAN *Zs = OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN));
851 EC_SCALAR *dis = OPENSSL_calloc(num_to_issue, sizeof(EC_SCALAR));
852 if (!BTs || !Zs || !dis) {
853 goto err;
854 }
855
856 uint8_t seed[SHA384_DIGEST_LENGTH];
857 if (!compute_composite_seed(method, seed, &key->pubs)) {
858 goto err;
859 }
860
861 // This implements the BlindEvaluateBatch as defined in section 4 of
862 // draft-robert-privacypass-batched-tokens-01, based on the constructions
863 // in draft-irtf-cfrg-voprf-21. To optimize the computation of the proof,
864 // the computation of di is done during the token signing and passed into
865 // the proof generation.
866 for (size_t i = 0; i < num_to_issue; i++) {
867 EC_AFFINE BT_affine, Z_affine;
868 EC_JACOBIAN BT, Z;
869 if (!cbs_get_point(cbs, group, &BT_affine)) {
870 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
871 goto err;
872 }
873 ec_affine_to_jacobian(group, &BT, &BT_affine);
874 if (!ec_point_mul_scalar(group, &Z, &BT, &key->xs) ||
875 !ec_jacobian_to_affine(group, &Z_affine, &Z) ||
876 !cbb_add_point(cbb, group, &Z_affine)) {
877 goto err;
878 }
879 BTs[i] = BT;
880 Zs[i] = Z;
881 if (!compute_composite_element(method, seed, &dis[i], i, &BT_affine,
882 &Z_affine)) {
883 goto err;
884 }
885
886 if (!CBB_flush(cbb)) {
887 goto err;
888 }
889 }
890
891 EC_JACOBIAN M, Z;
892 if (!ec_point_mul_scalar_public_batch(group, &M,
893 /*g_scalar=*/NULL, BTs, dis,
894 num_to_issue) ||
895 !ec_point_mul_scalar(group, &Z, &M, &key->xs)) {
896 goto err;
897 }
898
899 CBB proof;
900 if (!CBB_add_u16_length_prefixed(cbb, &proof) ||
901 !generate_proof(method, &proof, key, proof_scalar, &M, &Z) ||
902 !CBB_flush(cbb)) {
903 goto err;
904 }
905
906 // Skip over any unused requests.
907 size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
908 if (!CBS_skip(cbs, point_len * (num_requested - num_to_issue))) {
909 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
910 goto err;
911 }
912
913 ret = 1;
914
915 err:
916 OPENSSL_free(BTs);
917 OPENSSL_free(Zs);
918 OPENSSL_free(dis);
919 return ret;
920 }
921
922 static int voprf_sign(const VOPRF_METHOD *method,
923 const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
924 size_t num_requested, size_t num_to_issue) {
925 EC_SCALAR proof_scalar;
926 if (!ec_random_nonzero_scalar(method->group_func(), &proof_scalar,
927 kDefaultAdditionalData)) {
928 return 0;
929 }
930
931 return voprf_sign_impl(method, key, cbb, cbs, num_requested, num_to_issue,
932 &proof_scalar);
933 }
934
935 static int voprf_sign_with_proof_scalar_for_testing(
936 const VOPRF_METHOD *method, const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb,
937 CBS *cbs, size_t num_requested, size_t num_to_issue,
938 const uint8_t *proof_scalar_buf, size_t proof_scalar_len) {
939 EC_SCALAR proof_scalar;
940 if (!ec_scalar_from_bytes(method->group_func(), &proof_scalar,
941 proof_scalar_buf, proof_scalar_len)) {
942 return 0;
943 }
944 return voprf_sign_impl(method, key, cbb, cbs, num_requested, num_to_issue,
945 &proof_scalar);
946 }
947
948 static STACK_OF(TRUST_TOKEN) *voprf_unblind(
949 const VOPRF_METHOD *method, const TRUST_TOKEN_CLIENT_KEY *key,
950 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
951 uint32_t key_id) {
952 const EC_GROUP *group = method->group_func();
953 if (count > sk_TRUST_TOKEN_PRETOKEN_num(pretokens)) {
954 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
955 return NULL;
956 }
957
958 int ok = 0;
959 STACK_OF(TRUST_TOKEN) *ret = sk_TRUST_TOKEN_new_null();
960 EC_JACOBIAN *BTs = OPENSSL_calloc(count, sizeof(EC_JACOBIAN));
961 EC_JACOBIAN *Zs = OPENSSL_calloc(count, sizeof(EC_JACOBIAN));
962 EC_SCALAR *dis = OPENSSL_calloc(count, sizeof(EC_SCALAR));
963 if (ret == NULL || !BTs || !Zs || !dis) {
964 goto err;
965 }
966
967 uint8_t seed[SHA384_DIGEST_LENGTH];
968 if (!compute_composite_seed(method, seed, &key->pubs)) {
969 goto err;
970 }
971
972 for (size_t i = 0; i < count; i++) {
973 const TRUST_TOKEN_PRETOKEN *pretoken =
974 sk_TRUST_TOKEN_PRETOKEN_value(pretokens, i);
975
976 EC_AFFINE Z_affine;
977 if (!cbs_get_point(cbs, group, &Z_affine)) {
978 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
979 goto err;
980 }
981
982 ec_affine_to_jacobian(group, &BTs[i], &pretoken->Tp);
983 ec_affine_to_jacobian(group, &Zs[i], &Z_affine);
984 if (!compute_composite_element(method, seed, &dis[i], i, &pretoken->Tp,
985 &Z_affine)) {
986 goto err;
987 }
988
989 // Unblind the token.
990 // pretoken->r is rinv.
991 EC_JACOBIAN N;
992 EC_AFFINE N_affine;
993 if (!ec_point_mul_scalar(group, &N, &Zs[i], &pretoken->r) ||
994 !ec_jacobian_to_affine(group, &N_affine, &N)) {
995 goto err;
996 }
997
998 // Serialize the token. Include |key_id| to avoid an extra copy in the layer
999 // above.
1000 CBB token_cbb;
1001 size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
1002 if (!CBB_init(&token_cbb, 4 + TRUST_TOKEN_NONCE_SIZE + (2 + point_len)) ||
1003 !CBB_add_u32(&token_cbb, key_id) ||
1004 !CBB_add_bytes(&token_cbb, pretoken->salt, TRUST_TOKEN_NONCE_SIZE) ||
1005 !cbb_add_point(&token_cbb, group, &N_affine) ||
1006 !CBB_flush(&token_cbb)) {
1007 CBB_cleanup(&token_cbb);
1008 goto err;
1009 }
1010
1011 TRUST_TOKEN *token =
1012 TRUST_TOKEN_new(CBB_data(&token_cbb), CBB_len(&token_cbb));
1013 CBB_cleanup(&token_cbb);
1014 if (token == NULL ||
1015 !sk_TRUST_TOKEN_push(ret, token)) {
1016 TRUST_TOKEN_free(token);
1017 goto err;
1018 }
1019 }
1020
1021 EC_JACOBIAN M, Z;
1022 if (!ec_point_mul_scalar_public_batch(group, &M,
1023 /*g_scalar=*/NULL, BTs, dis,
1024 count) ||
1025 !ec_point_mul_scalar_public_batch(group, &Z,
1026 /*g_scalar=*/NULL, Zs, dis,
1027 count)) {
1028 goto err;
1029 }
1030
1031 CBS proof;
1032 if (!CBS_get_u16_length_prefixed(cbs, &proof) ||
1033 !verify_proof(method, &proof, key, &M, &Z) ||
1034 CBS_len(&proof) != 0) {
1035 goto err;
1036 }
1037
1038 ok = 1;
1039
1040 err:
1041 OPENSSL_free(BTs);
1042 OPENSSL_free(Zs);
1043 OPENSSL_free(dis);
1044 if (!ok) {
1045 sk_TRUST_TOKEN_pop_free(ret, TRUST_TOKEN_free);
1046 ret = NULL;
1047 }
1048 return ret;
1049 }
1050
1051 static int voprf_read(const VOPRF_METHOD *method,
1052 const TRUST_TOKEN_ISSUER_KEY *key,
1053 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
1054 const uint8_t *token, size_t token_len,
1055 int include_message, const uint8_t *msg, size_t msg_len) {
1056 const EC_GROUP *group = method->group_func();
1057 CBS cbs, salt;
1058 CBS_init(&cbs, token, token_len);
1059 EC_AFFINE Ws;
1060 if (!CBS_get_bytes(&cbs, &salt, TRUST_TOKEN_NONCE_SIZE) ||
1061 !cbs_get_point(&cbs, group, &Ws) ||
1062 CBS_len(&cbs) != 0) {
1063 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_TOKEN);
1064 return 0;
1065 }
1066
1067 if (include_message) {
1068 SHA512_CTX hash_ctx;
1069 assert(SHA512_DIGEST_LENGTH == TRUST_TOKEN_NONCE_SIZE);
1070 SHA512_Init(&hash_ctx);
1071 SHA512_Update(&hash_ctx, CBS_data(&salt), CBS_len(&salt));
1072 SHA512_Update(&hash_ctx, msg, msg_len);
1073 SHA512_Final(out_nonce, &hash_ctx);
1074 } else {
1075 OPENSSL_memcpy(out_nonce, CBS_data(&salt), CBS_len(&salt));
1076 }
1077
1078
1079 EC_JACOBIAN T;
1080 if (!method->hash_to_group(group, &T, out_nonce)) {
1081 return 0;
1082 }
1083
1084 EC_JACOBIAN Ws_calculated;
1085 if (!ec_point_mul_scalar(group, &Ws_calculated, &T, &key->xs) ||
1086 !ec_affine_jacobian_equal(group, &Ws, &Ws_calculated)) {
1087 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BAD_VALIDITY_CHECK);
1088 return 0;
1089 }
1090
1091 return 1;
1092 }
1093
1094
1095 // VOPRF experiment v2.
1096
1097 static int voprf_exp2_hash_to_group(const EC_GROUP *group, EC_JACOBIAN *out,
1098 const uint8_t t[TRUST_TOKEN_NONCE_SIZE]) {
1099 const uint8_t kHashTLabel[] = "TrustToken VOPRF Experiment V2 HashToGroup";
1100 return ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
1101 group, out, kHashTLabel, sizeof(kHashTLabel), t, TRUST_TOKEN_NONCE_SIZE);
1102 }
1103
1104 static int voprf_exp2_hash_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
1105 uint8_t *buf, size_t len) {
1106 const uint8_t kHashCLabel[] = "TrustToken VOPRF Experiment V2 HashToScalar";
1107 return ec_hash_to_scalar_p384_xmd_sha512_draft07(
1108 group, out, kHashCLabel, sizeof(kHashCLabel), buf, len);
1109 }
1110
1111 static VOPRF_METHOD voprf_exp2_method = {
1112 EC_group_p384, voprf_exp2_hash_to_group, voprf_exp2_hash_to_scalar};
1113
1114 int voprf_exp2_generate_key(CBB *out_private, CBB *out_public) {
1115 return voprf_generate_key(&voprf_exp2_method, out_private, out_public);
1116 }
1117
1118 int voprf_exp2_derive_key_from_secret(CBB *out_private, CBB *out_public,
1119 const uint8_t *secret,
1120 size_t secret_len) {
1121 return voprf_derive_key_from_secret(&voprf_exp2_method, out_private,
1122 out_public, secret, secret_len);
1123 }
1124
1125 int voprf_exp2_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
1126 const uint8_t *in, size_t len) {
1127 return voprf_client_key_from_bytes(&voprf_exp2_method, key, in, len);
1128 }
1129
1130 int voprf_exp2_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
1131 const uint8_t *in, size_t len) {
1132 return voprf_issuer_key_from_bytes(&voprf_exp2_method, key, in, len);
1133 }
1134
1135 STACK_OF(TRUST_TOKEN_PRETOKEN) *voprf_exp2_blind(CBB *cbb, size_t count,
1136 int include_message,
1137 const uint8_t *msg,
1138 size_t msg_len) {
1139 return voprf_blind(&voprf_exp2_method, cbb, count, include_message, msg,
1140 msg_len);
1141 }
1142
1143 int voprf_exp2_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
1144 size_t num_requested, size_t num_to_issue,
1145 uint8_t private_metadata) {
1146 if (private_metadata != 0) {
1147 return 0;
1148 }
1149 return voprf_sign_tt(&voprf_exp2_method, key, cbb, cbs, num_requested,
1150 num_to_issue);
1151 }
1152
1153 STACK_OF(TRUST_TOKEN) *voprf_exp2_unblind(
1154 const TRUST_TOKEN_CLIENT_KEY *key,
1155 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
1156 uint32_t key_id) {
1157 return voprf_unblind_tt(&voprf_exp2_method, key, pretokens, cbs, count,
1158 key_id);
1159 }
1160
1161 int voprf_exp2_read(const TRUST_TOKEN_ISSUER_KEY *key,
1162 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
1163 uint8_t *out_private_metadata, const uint8_t *token,
1164 size_t token_len, int include_message, const uint8_t *msg,
1165 size_t msg_len) {
1166 return voprf_read(&voprf_exp2_method, key, out_nonce, token, token_len,
1167 include_message, msg, msg_len);
1168 }
1169
1170 // VOPRF PST v1.
1171
1172 static int voprf_pst1_hash_to_group(const EC_GROUP *group, EC_JACOBIAN *out,
1173 const uint8_t t[TRUST_TOKEN_NONCE_SIZE]) {
1174 const uint8_t kHashTLabel[] = "HashToGroup-OPRFV1-\x01-P384-SHA384";
1175 return ec_hash_to_curve_p384_xmd_sha384_sswu(group, out, kHashTLabel,
1176 sizeof(kHashTLabel) - 1, t,
1177 TRUST_TOKEN_NONCE_SIZE);
1178 }
1179
1180 static int voprf_pst1_hash_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
1181 uint8_t *buf, size_t len) {
1182 const uint8_t kHashCLabel[] = "HashToScalar-OPRFV1-\x01-P384-SHA384";
1183 return ec_hash_to_scalar_p384_xmd_sha384(group, out, kHashCLabel,
1184 sizeof(kHashCLabel) - 1, buf, len);
1185 }
1186
1187 static VOPRF_METHOD voprf_pst1_method = {
1188 EC_group_p384, voprf_pst1_hash_to_group, voprf_pst1_hash_to_scalar};
1189
1190 int voprf_pst1_generate_key(CBB *out_private, CBB *out_public) {
1191 return voprf_generate_key(&voprf_pst1_method, out_private, out_public);
1192 }
1193
1194 int voprf_pst1_derive_key_from_secret(CBB *out_private, CBB *out_public,
1195 const uint8_t *secret,
1196 size_t secret_len) {
1197 return voprf_derive_key_from_secret(&voprf_pst1_method, out_private,
1198 out_public, secret, secret_len);
1199 }
1200
1201 int voprf_pst1_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
1202 const uint8_t *in, size_t len) {
1203 return voprf_client_key_from_bytes(&voprf_pst1_method, key, in, len);
1204 }
1205
1206 int voprf_pst1_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
1207 const uint8_t *in, size_t len) {
1208 return voprf_issuer_key_from_bytes(&voprf_pst1_method, key, in, len);
1209 }
1210
1211 STACK_OF(TRUST_TOKEN_PRETOKEN) *voprf_pst1_blind(CBB *cbb, size_t count,
1212 int include_message,
1213 const uint8_t *msg,
1214 size_t msg_len) {
1215 return voprf_blind(&voprf_pst1_method, cbb, count, include_message, msg,
1216 msg_len);
1217 }
1218
1219 int voprf_pst1_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
1220 size_t num_requested, size_t num_to_issue,
1221 uint8_t private_metadata) {
1222 if (private_metadata != 0) {
1223 return 0;
1224 }
1225 return voprf_sign(&voprf_pst1_method, key, cbb, cbs, num_requested,
1226 num_to_issue);
1227 }
1228
1229
1230 int voprf_pst1_sign_with_proof_scalar_for_testing(
1231 const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs, size_t num_requested,
1232 size_t num_to_issue, uint8_t private_metadata,
1233 const uint8_t *proof_scalar_buf, size_t proof_scalar_len) {
1234 if (private_metadata != 0) {
1235 return 0;
1236 }
1237 return voprf_sign_with_proof_scalar_for_testing(
1238 &voprf_pst1_method, key, cbb, cbs, num_requested, num_to_issue,
1239 proof_scalar_buf, proof_scalar_len);
1240 }
1241
1242 STACK_OF(TRUST_TOKEN) *voprf_pst1_unblind(
1243 const TRUST_TOKEN_CLIENT_KEY *key,
1244 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
1245 uint32_t key_id) {
1246 return voprf_unblind(&voprf_pst1_method, key, pretokens, cbs, count, key_id);
1247 }
1248
1249 int voprf_pst1_read(const TRUST_TOKEN_ISSUER_KEY *key,
1250 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
1251 uint8_t *out_private_metadata, const uint8_t *token,
1252 size_t token_len, int include_message, const uint8_t *msg,
1253 size_t msg_len) {
1254 return voprf_read(&voprf_pst1_method, key, out_nonce, token, token_len,
1255 include_message, msg, msg_len);
1256 }