Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/trust_token/pmbtoken.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/bn/internal.h"
17 #include "../fipsmodule/ec/internal.h"
18
19 #include "internal.h"
20
21
22 typedef int (*hash_t_func_t)(const EC_GROUP *group, EC_JACOBIAN *out,
23 const uint8_t t[TRUST_TOKEN_NONCE_SIZE]);
24 typedef int (*hash_s_func_t)(const EC_GROUP *group, EC_JACOBIAN *out,
25 const EC_AFFINE *t,
26 const uint8_t s[TRUST_TOKEN_NONCE_SIZE]);
27 typedef int (*hash_c_func_t)(const EC_GROUP *group, EC_SCALAR *out,
28 uint8_t *buf, size_t len);
29 typedef int (*hash_to_scalar_func_t)(const EC_GROUP *group, EC_SCALAR *out,
30 uint8_t *buf, size_t len);
31
32 typedef struct {
33 const EC_GROUP *group;
34 EC_PRECOMP g_precomp;
35 EC_PRECOMP h_precomp;
36 EC_JACOBIAN h;
37 // hash_t implements the H_t operation in PMBTokens. It returns one on success
38 // and zero on error.
39 hash_t_func_t hash_t;
40 // hash_s implements the H_s operation in PMBTokens. It returns one on success
41 // and zero on error.
42 hash_s_func_t hash_s;
43 // hash_c implements the H_c operation in PMBTokens. It returns one on success
44 // and zero on error.
45 hash_c_func_t hash_c;
46 // hash_to_scalar implements the HashToScalar operation for PMBTokens. It
47 // returns one on success and zero on error.
48 hash_to_scalar_func_t hash_to_scalar;
49 int prefix_point : 1;
50 } PMBTOKEN_METHOD;
51
52 static const uint8_t kDefaultAdditionalData[32] = {0};
53
54 static int pmbtoken_init_method(PMBTOKEN_METHOD *method, const EC_GROUP *group,
55 const uint8_t *h_bytes, size_t h_len,
56 hash_t_func_t hash_t, hash_s_func_t hash_s,
57 hash_c_func_t hash_c,
58 hash_to_scalar_func_t hash_to_scalar,
59 int prefix_point) {
60 method->group = group;
61 method->hash_t = hash_t;
62 method->hash_s = hash_s;
63 method->hash_c = hash_c;
64 method->hash_to_scalar = hash_to_scalar;
65 method->prefix_point = prefix_point;
66
67 EC_AFFINE h;
68 if (!ec_point_from_uncompressed(method->group, &h, h_bytes, h_len)) {
69 return 0;
70 }
71 ec_affine_to_jacobian(method->group, &method->h, &h);
72
73 if (!ec_init_precomp(method->group, &method->g_precomp,
74 &method->group->generator.raw) ||
75 !ec_init_precomp(method->group, &method->h_precomp, &method->h)) {
76 return 0;
77 }
78 return 1;
79 }
80
81 static int derive_scalar_from_secret(const PMBTOKEN_METHOD *method,
82 EC_SCALAR *out, const uint8_t *secret,
83 size_t secret_len, uint8_t scalar_id) {
84 static const uint8_t kKeygenLabel[] = "TrustTokenPMBTokenKeyGen";
85
86 int ok = 0;
87 CBB cbb;
88 CBB_zero(&cbb);
89 uint8_t *buf = NULL;
90 size_t len;
91 if (!CBB_init(&cbb, 0) ||
92 !CBB_add_bytes(&cbb, kKeygenLabel, sizeof(kKeygenLabel)) ||
93 !CBB_add_u8(&cbb, scalar_id) ||
94 !CBB_add_bytes(&cbb, secret, secret_len) ||
95 !CBB_finish(&cbb, &buf, &len) ||
96 !method->hash_to_scalar(method->group, out, buf, len)) {
97 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE);
98 goto err;
99 }
100
101 ok = 1;
102
103 err:
104 CBB_cleanup(&cbb);
105 OPENSSL_free(buf);
106 return ok;
107 }
108
109 static int point_to_cbb(CBB *out, const EC_GROUP *group,
110 const EC_AFFINE *point) {
111 size_t len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
112 if (len == 0) {
113 return 0;
114 }
115 uint8_t *p;
116 return CBB_add_space(out, &p, len) &&
117 ec_point_to_bytes(group, point, POINT_CONVERSION_UNCOMPRESSED, p,
118 len) == len;
119 }
120
121 static int cbb_add_prefixed_point(CBB *out, const EC_GROUP *group,
122 const EC_AFFINE *point, int prefix_point) {
123 if (prefix_point) {
124 CBB child;
125 if (!CBB_add_u16_length_prefixed(out, &child) ||
126 !point_to_cbb(&child, group, point) ||
127 !CBB_flush(out)) {
128 return 0;
129 }
130 } else {
131 if (!point_to_cbb(out, group, point) ||
132 !CBB_flush(out)) {
133 return 0;
134 }
135 }
136
137 return 1;
138 }
139
140 static int cbs_get_prefixed_point(CBS *cbs, const EC_GROUP *group,
141 EC_AFFINE *out, int prefix_point) {
142 CBS child;
143 if (prefix_point) {
144 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
145 return 0;
146 }
147 } else {
148 size_t plen = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
149 if (!CBS_get_bytes(cbs, &child, plen)) {
150 return 0;
151 }
152 }
153
154 if (!ec_point_from_uncompressed(group, out, CBS_data(&child),
155 CBS_len(&child))) {
156 return 0;
157 }
158 return 1;
159 }
160
161 static int mul_public_3(const EC_GROUP *group, EC_JACOBIAN *out,
162 const EC_JACOBIAN *p0, const EC_SCALAR *scalar0,
163 const EC_JACOBIAN *p1, const EC_SCALAR *scalar1,
164 const EC_JACOBIAN *p2, const EC_SCALAR *scalar2) {
165 EC_JACOBIAN points[3] = {*p0, *p1, *p2};
166 EC_SCALAR scalars[3] = {*scalar0, *scalar1, *scalar2};
167 return ec_point_mul_scalar_public_batch(group, out, /*g_scalar=*/NULL, points,
168 scalars, 3);
169 }
170
171 static int pmbtoken_compute_keys(const PMBTOKEN_METHOD *method,
172 CBB *out_private, CBB *out_public,
173 const EC_SCALAR *x0, const EC_SCALAR *y0,
174 const EC_SCALAR *x1, const EC_SCALAR *y1,
175 const EC_SCALAR *xs, const EC_SCALAR *ys) {
176 const EC_GROUP *group = method->group;
177 EC_JACOBIAN pub[3];
178 if (!ec_point_mul_scalar_precomp(group, &pub[0], &method->g_precomp,
179 x0, &method->h_precomp, y0, NULL, NULL) ||
180 !ec_point_mul_scalar_precomp(group, &pub[1], &method->g_precomp,
181 x1, &method->h_precomp, y1, NULL, NULL) ||
182 !ec_point_mul_scalar_precomp(method->group, &pub[2], &method->g_precomp,
183 xs, &method->h_precomp, ys, NULL, NULL)) {
184 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE);
185 return 0;
186 }
187
188 const EC_SCALAR *scalars[] = {x0, y0, x1, y1, xs, ys};
189 size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group));
190 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(scalars); i++) {
191 uint8_t *buf;
192 if (!CBB_add_space(out_private, &buf, scalar_len)) {
193 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BUFFER_TOO_SMALL);
194 return 0;
195 }
196 ec_scalar_to_bytes(group, buf, &scalar_len, scalars[i]);
197 }
198
199 EC_AFFINE pub_affine[3];
200 if (!ec_jacobian_to_affine_batch(group, pub_affine, pub, 3)) {
201 return 0;
202 }
203
204 if (!cbb_add_prefixed_point(out_public, group, &pub_affine[0],
205 method->prefix_point) ||
206 !cbb_add_prefixed_point(out_public, group, &pub_affine[1],
207 method->prefix_point) ||
208 !cbb_add_prefixed_point(out_public, group, &pub_affine[2],
209 method->prefix_point)) {
210 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BUFFER_TOO_SMALL);
211 return 0;
212 }
213
214 return 1;
215 }
216
217 static int pmbtoken_generate_key(const PMBTOKEN_METHOD *method,
218 CBB *out_private, CBB *out_public) {
219 EC_SCALAR x0, y0, x1, y1, xs, ys;
220 if (!ec_random_nonzero_scalar(method->group, &x0, kDefaultAdditionalData) ||
221 !ec_random_nonzero_scalar(method->group, &y0, kDefaultAdditionalData) ||
222 !ec_random_nonzero_scalar(method->group, &x1, kDefaultAdditionalData) ||
223 !ec_random_nonzero_scalar(method->group, &y1, kDefaultAdditionalData) ||
224 !ec_random_nonzero_scalar(method->group, &xs, kDefaultAdditionalData) ||
225 !ec_random_nonzero_scalar(method->group, &ys, kDefaultAdditionalData)) {
226 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE);
227 return 0;
228 }
229
230 return pmbtoken_compute_keys(method, out_private, out_public, &x0, &y0, &x1,
231 &y1, &xs, &ys);
232 }
233
234 static int pmbtoken_derive_key_from_secret(const PMBTOKEN_METHOD *method,
235 CBB *out_private, CBB *out_public,
236 const uint8_t *secret,
237 size_t secret_len) {
238 EC_SCALAR x0, y0, x1, y1, xs, ys;
239 if (!derive_scalar_from_secret(method, &x0, secret, secret_len, 0) ||
240 !derive_scalar_from_secret(method, &y0, secret, secret_len, 1) ||
241 !derive_scalar_from_secret(method, &x1, secret, secret_len, 2) ||
242 !derive_scalar_from_secret(method, &y1, secret, secret_len, 3) ||
243 !derive_scalar_from_secret(method, &xs, secret, secret_len, 4) ||
244 !derive_scalar_from_secret(method, &ys, secret, secret_len, 5)) {
245 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_KEYGEN_FAILURE);
246 return 0;
247 }
248
249 return pmbtoken_compute_keys(method, out_private, out_public, &x0, &y0, &x1,
250 &y1, &xs, &ys);
251 }
252
253 static int pmbtoken_client_key_from_bytes(const PMBTOKEN_METHOD *method,
254 TRUST_TOKEN_CLIENT_KEY *key,
255 const uint8_t *in, size_t len) {
256 CBS cbs;
257 CBS_init(&cbs, in, len);
258 if (!cbs_get_prefixed_point(&cbs, method->group, &key->pub0,
259 method->prefix_point) ||
260 !cbs_get_prefixed_point(&cbs, method->group, &key->pub1,
261 method->prefix_point) ||
262 !cbs_get_prefixed_point(&cbs, method->group, &key->pubs,
263 method->prefix_point) ||
264 CBS_len(&cbs) != 0) {
265 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
266 return 0;
267 }
268
269 return 1;
270 }
271
272 static int pmbtoken_issuer_key_from_bytes(const PMBTOKEN_METHOD *method,
273 TRUST_TOKEN_ISSUER_KEY *key,
274 const uint8_t *in, size_t len) {
275 const EC_GROUP *group = method->group;
276 CBS cbs, tmp;
277 CBS_init(&cbs, in, len);
278 size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group));
279 EC_SCALAR *scalars[] = {&key->x0, &key->y0, &key->x1,
280 &key->y1, &key->xs, &key->ys};
281 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(scalars); i++) {
282 if (!CBS_get_bytes(&cbs, &tmp, scalar_len) ||
283 !ec_scalar_from_bytes(group, scalars[i], CBS_data(&tmp),
284 CBS_len(&tmp))) {
285 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
286 return 0;
287 }
288 }
289
290 // Recompute the public key.
291 EC_JACOBIAN pub[3];
292 EC_AFFINE pub_affine[3];
293 if (!ec_point_mul_scalar_precomp(group, &pub[0], &method->g_precomp, &key->x0,
294 &method->h_precomp, &key->y0, NULL, NULL) ||
295 !ec_init_precomp(group, &key->pub0_precomp, &pub[0]) ||
296 !ec_point_mul_scalar_precomp(group, &pub[1], &method->g_precomp, &key->x1,
297 &method->h_precomp, &key->y1, NULL, NULL) ||
298 !ec_init_precomp(group, &key->pub1_precomp, &pub[1]) ||
299 !ec_point_mul_scalar_precomp(group, &pub[2], &method->g_precomp, &key->xs,
300 &method->h_precomp, &key->ys, NULL, NULL) ||
301 !ec_init_precomp(group, &key->pubs_precomp, &pub[2]) ||
302 !ec_jacobian_to_affine_batch(group, pub_affine, pub, 3)) {
303 return 0;
304 }
305
306 key->pub0 = pub_affine[0];
307 key->pub1 = pub_affine[1];
308 key->pubs = pub_affine[2];
309 return 1;
310 }
311
312 static STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_blind(
313 const PMBTOKEN_METHOD *method, CBB *cbb, size_t count, int include_message,
314 const uint8_t *msg, size_t msg_len) {
315 SHA512_CTX hash_ctx;
316
317 const EC_GROUP *group = method->group;
318 STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens = sk_TRUST_TOKEN_PRETOKEN_new_null();
319 if (pretokens == NULL) {
320 goto err;
321 }
322
323 for (size_t i = 0; i < count; i++) {
324 // Insert |pretoken| into |pretokens| early to simplify error-handling.
325 TRUST_TOKEN_PRETOKEN *pretoken = OPENSSL_malloc(sizeof(TRUST_TOKEN_PRETOKEN));
326 if (pretoken == NULL ||
327 !sk_TRUST_TOKEN_PRETOKEN_push(pretokens, pretoken)) {
328 TRUST_TOKEN_PRETOKEN_free(pretoken);
329 goto err;
330 }
331
332 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(pretoken->salt, sizeof(pretoken->salt)));
333 if (include_message) {
334 assert(SHA512_DIGEST_LENGTH == TRUST_TOKEN_NONCE_SIZE);
335 SHA512_Init(&hash_ctx);
336 SHA512_Update(&hash_ctx, pretoken->salt, sizeof(pretoken->salt));
337 SHA512_Update(&hash_ctx, msg, msg_len);
338 SHA512_Final(pretoken->t, &hash_ctx);
339 } else {
340 OPENSSL_memcpy(pretoken->t, pretoken->salt, TRUST_TOKEN_NONCE_SIZE);
341 }
342
343 // We sample |pretoken->r| in Montgomery form to simplify inverting.
344 if (!ec_random_nonzero_scalar(group, &pretoken->r,
345 kDefaultAdditionalData)) {
346 goto err;
347 }
348
349 EC_SCALAR rinv;
350 ec_scalar_inv0_montgomery(group, &rinv, &pretoken->r);
351 // Convert both out of Montgomery form.
352 ec_scalar_from_montgomery(group, &pretoken->r, &pretoken->r);
353 ec_scalar_from_montgomery(group, &rinv, &rinv);
354
355 EC_JACOBIAN T, Tp;
356 if (!method->hash_t(group, &T, pretoken->t) ||
357 !ec_point_mul_scalar(group, &Tp, &T, &rinv) ||
358 !ec_jacobian_to_affine(group, &pretoken->Tp, &Tp)) {
359 goto err;
360 }
361
362 if (!cbb_add_prefixed_point(cbb, group, &pretoken->Tp,
363 method->prefix_point)) {
364 goto err;
365 }
366 }
367
368 return pretokens;
369
370 err:
371 sk_TRUST_TOKEN_PRETOKEN_pop_free(pretokens, TRUST_TOKEN_PRETOKEN_free);
372 return NULL;
373 }
374
375 static int scalar_to_cbb(CBB *out, const EC_GROUP *group,
376 const EC_SCALAR *scalar) {
377 uint8_t *buf;
378 size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group));
379 if (!CBB_add_space(out, &buf, scalar_len)) {
380 return 0;
381 }
382 ec_scalar_to_bytes(group, buf, &scalar_len, scalar);
383 return 1;
384 }
385
386 static int scalar_from_cbs(CBS *cbs, const EC_GROUP *group, EC_SCALAR *out) {
387 size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group));
388 CBS tmp;
389 if (!CBS_get_bytes(cbs, &tmp, scalar_len)) {
390 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
391 return 0;
392 }
393
394 ec_scalar_from_bytes(group, out, CBS_data(&tmp), CBS_len(&tmp));
395 return 1;
396 }
397
398 static int hash_c_dleq(const PMBTOKEN_METHOD *method, EC_SCALAR *out,
399 const EC_AFFINE *X, const EC_AFFINE *T,
400 const EC_AFFINE *S, const EC_AFFINE *W,
401 const EC_AFFINE *K0, const EC_AFFINE *K1) {
402 static const uint8_t kDLEQ2Label[] = "DLEQ2";
403
404 int ok = 0;
405 CBB cbb;
406 CBB_zero(&cbb);
407 uint8_t *buf = NULL;
408 size_t len;
409 if (!CBB_init(&cbb, 0) ||
410 !CBB_add_bytes(&cbb, kDLEQ2Label, sizeof(kDLEQ2Label)) ||
411 !point_to_cbb(&cbb, method->group, X) ||
412 !point_to_cbb(&cbb, method->group, T) ||
413 !point_to_cbb(&cbb, method->group, S) ||
414 !point_to_cbb(&cbb, method->group, W) ||
415 !point_to_cbb(&cbb, method->group, K0) ||
416 !point_to_cbb(&cbb, method->group, K1) ||
417 !CBB_finish(&cbb, &buf, &len) ||
418 !method->hash_c(method->group, out, buf, len)) {
419 goto err;
420 }
421
422 ok = 1;
423
424 err:
425 CBB_cleanup(&cbb);
426 OPENSSL_free(buf);
427 return ok;
428 }
429
430 static int hash_c_dleqor(const PMBTOKEN_METHOD *method, EC_SCALAR *out,
431 const EC_AFFINE *X0, const EC_AFFINE *X1,
432 const EC_AFFINE *T, const EC_AFFINE *S,
433 const EC_AFFINE *W, const EC_AFFINE *K00,
434 const EC_AFFINE *K01, const EC_AFFINE *K10,
435 const EC_AFFINE *K11) {
436 static const uint8_t kDLEQOR2Label[] = "DLEQOR2";
437
438 int ok = 0;
439 CBB cbb;
440 CBB_zero(&cbb);
441 uint8_t *buf = NULL;
442 size_t len;
443 if (!CBB_init(&cbb, 0) ||
444 !CBB_add_bytes(&cbb, kDLEQOR2Label, sizeof(kDLEQOR2Label)) ||
445 !point_to_cbb(&cbb, method->group, X0) ||
446 !point_to_cbb(&cbb, method->group, X1) ||
447 !point_to_cbb(&cbb, method->group, T) ||
448 !point_to_cbb(&cbb, method->group, S) ||
449 !point_to_cbb(&cbb, method->group, W) ||
450 !point_to_cbb(&cbb, method->group, K00) ||
451 !point_to_cbb(&cbb, method->group, K01) ||
452 !point_to_cbb(&cbb, method->group, K10) ||
453 !point_to_cbb(&cbb, method->group, K11) ||
454 !CBB_finish(&cbb, &buf, &len) ||
455 !method->hash_c(method->group, out, buf, len)) {
456 goto err;
457 }
458
459 ok = 1;
460
461 err:
462 CBB_cleanup(&cbb);
463 OPENSSL_free(buf);
464 return ok;
465 }
466
467 static int hash_c_batch(const PMBTOKEN_METHOD *method, EC_SCALAR *out,
468 const CBB *points, size_t index) {
469 static const uint8_t kDLEQBatchLabel[] = "DLEQ BATCH";
470 if (index > 0xffff) {
471 // The protocol supports only two-byte batches.
472 OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_OVERFLOW);
473 return 0;
474 }
475
476 int ok = 0;
477 CBB cbb;
478 CBB_zero(&cbb);
479 uint8_t *buf = NULL;
480 size_t len;
481 if (!CBB_init(&cbb, 0) ||
482 !CBB_add_bytes(&cbb, kDLEQBatchLabel, sizeof(kDLEQBatchLabel)) ||
483 !CBB_add_bytes(&cbb, CBB_data(points), CBB_len(points)) ||
484 !CBB_add_u16(&cbb, (uint16_t)index) ||
485 !CBB_finish(&cbb, &buf, &len) ||
486 !method->hash_c(method->group, out, buf, len)) {
487 goto err;
488 }
489
490 ok = 1;
491
492 err:
493 CBB_cleanup(&cbb);
494 OPENSSL_free(buf);
495 return ok;
496 }
497
498 // The DLEQ2 and DLEQOR2 constructions are described in appendix B of
499 // https://eprint.iacr.org/2020/072/20200324:214215. DLEQ2 is an instance of
500 // DLEQOR2 with only one value (n=1).
501
502 static int dleq_generate(const PMBTOKEN_METHOD *method, CBB *cbb,
503 const TRUST_TOKEN_ISSUER_KEY *priv,
504 const EC_JACOBIAN *T, const EC_JACOBIAN *S,
505 const EC_JACOBIAN *W, const EC_JACOBIAN *Ws,
506 uint8_t private_metadata) {
507 const EC_GROUP *group = method->group;
508
509 // We generate a DLEQ proof for the validity token and a DLEQOR2 proof for the
510 // private metadata token. To allow amortizing Jacobian-to-affine conversions,
511 // we compute Ki for both proofs first.
512 enum {
513 idx_T,
514 idx_S,
515 idx_W,
516 idx_Ws,
517 idx_Ks0,
518 idx_Ks1,
519 idx_Kb0,
520 idx_Kb1,
521 idx_Ko0,
522 idx_Ko1,
523 num_idx,
524 };
525 EC_JACOBIAN jacobians[num_idx];
526
527 // Setup the DLEQ proof.
528 EC_SCALAR ks0, ks1;
529 if (// ks0, ks1 <- Zp
530 !ec_random_nonzero_scalar(group, &ks0, kDefaultAdditionalData) ||
531 !ec_random_nonzero_scalar(group, &ks1, kDefaultAdditionalData) ||
532 // Ks = ks0*(G;T) + ks1*(H;S)
533 !ec_point_mul_scalar_precomp(group, &jacobians[idx_Ks0],
534 &method->g_precomp, &ks0, &method->h_precomp,
535 &ks1, NULL, NULL) ||
536 !ec_point_mul_scalar_batch(group, &jacobians[idx_Ks1], T, &ks0, S, &ks1,
537 NULL, NULL)) {
538 return 0;
539 }
540
541 // Setup the DLEQOR proof. First, select values of xb, yb (keys corresponding
542 // to the private metadata value) and pubo (public key corresponding to the
543 // other value) in constant time.
544 BN_ULONG mask = ((BN_ULONG)0) - (private_metadata & 1);
545 EC_PRECOMP pubo_precomp;
546 EC_SCALAR xb, yb;
547 ec_scalar_select(group, &xb, mask, &priv->x1, &priv->x0);
548 ec_scalar_select(group, &yb, mask, &priv->y1, &priv->y0);
549 ec_precomp_select(group, &pubo_precomp, mask, &priv->pub0_precomp,
550 &priv->pub1_precomp);
551
552 EC_SCALAR k0, k1, minus_co, uo, vo;
553 if (// k0, k1 <- Zp
554 !ec_random_nonzero_scalar(group, &k0, kDefaultAdditionalData) ||
555 !ec_random_nonzero_scalar(group, &k1, kDefaultAdditionalData) ||
556 // Kb = k0*(G;T) + k1*(H;S)
557 !ec_point_mul_scalar_precomp(group, &jacobians[idx_Kb0],
558 &method->g_precomp, &k0, &method->h_precomp,
559 &k1, NULL, NULL) ||
560 !ec_point_mul_scalar_batch(group, &jacobians[idx_Kb1], T, &k0, S, &k1,
561 NULL, NULL) ||
562 // co, uo, vo <- Zp
563 !ec_random_nonzero_scalar(group, &minus_co, kDefaultAdditionalData) ||
564 !ec_random_nonzero_scalar(group, &uo, kDefaultAdditionalData) ||
565 !ec_random_nonzero_scalar(group, &vo, kDefaultAdditionalData) ||
566 // Ko = uo*(G;T) + vo*(H;S) - co*(pubo;W)
567 !ec_point_mul_scalar_precomp(group, &jacobians[idx_Ko0],
568 &method->g_precomp, &uo, &method->h_precomp,
569 &vo, &pubo_precomp, &minus_co) ||
570 !ec_point_mul_scalar_batch(group, &jacobians[idx_Ko1], T, &uo, S, &vo, W,
571 &minus_co)) {
572 return 0;
573 }
574
575 EC_AFFINE affines[num_idx];
576 jacobians[idx_T] = *T;
577 jacobians[idx_S] = *S;
578 jacobians[idx_W] = *W;
579 jacobians[idx_Ws] = *Ws;
580 if (!ec_jacobian_to_affine_batch(group, affines, jacobians, num_idx)) {
581 return 0;
582 }
583
584 // Select the K corresponding to K0 and K1 in constant-time.
585 EC_AFFINE K00, K01, K10, K11;
586 ec_affine_select(group, &K00, mask, &affines[idx_Ko0], &affines[idx_Kb0]);
587 ec_affine_select(group, &K01, mask, &affines[idx_Ko1], &affines[idx_Kb1]);
588 ec_affine_select(group, &K10, mask, &affines[idx_Kb0], &affines[idx_Ko0]);
589 ec_affine_select(group, &K11, mask, &affines[idx_Kb1], &affines[idx_Ko1]);
590
591 // Compute c = Hc(...) for the two proofs.
592 EC_SCALAR cs, c;
593 if (!hash_c_dleq(method, &cs, &priv->pubs, &affines[idx_T], &affines[idx_S],
594 &affines[idx_Ws], &affines[idx_Ks0], &affines[idx_Ks1]) ||
595 !hash_c_dleqor(method, &c, &priv->pub0, &priv->pub1, &affines[idx_T],
596 &affines[idx_S], &affines[idx_W], &K00, &K01, &K10,
597 &K11)) {
598 return 0;
599 }
600
601 // Compute cb, ub, and ub for the two proofs. In each of these products, only
602 // one operand is in Montgomery form, so the product does not need to be
603 // converted.
604
605 EC_SCALAR cs_mont;
606 ec_scalar_to_montgomery(group, &cs_mont, &cs);
607
608 // us = ks0 + cs*xs
609 EC_SCALAR us, vs;
610 ec_scalar_mul_montgomery(group, &us, &priv->xs, &cs_mont);
611 ec_scalar_add(group, &us, &ks0, &us);
612
613 // vs = ks1 + cs*ys
614 ec_scalar_mul_montgomery(group, &vs, &priv->ys, &cs_mont);
615 ec_scalar_add(group, &vs, &ks1, &vs);
616
617 // Store DLEQ2 proof in transcript.
618 if (!scalar_to_cbb(cbb, group, &cs) ||
619 !scalar_to_cbb(cbb, group, &us) ||
620 !scalar_to_cbb(cbb, group, &vs)) {
621 return 0;
622 }
623
624 // cb = c - co
625 EC_SCALAR cb, ub, vb;
626 ec_scalar_add(group, &cb, &c, &minus_co);
627
628 EC_SCALAR cb_mont;
629 ec_scalar_to_montgomery(group, &cb_mont, &cb);
630
631 // ub = k0 + cb*xb
632 ec_scalar_mul_montgomery(group, &ub, &xb, &cb_mont);
633 ec_scalar_add(group, &ub, &k0, &ub);
634
635 // vb = k1 + cb*yb
636 ec_scalar_mul_montgomery(group, &vb, &yb, &cb_mont);
637 ec_scalar_add(group, &vb, &k1, &vb);
638
639 // Select c, u, v in constant-time.
640 EC_SCALAR co, c0, c1, u0, u1, v0, v1;
641 ec_scalar_neg(group, &co, &minus_co);
642 ec_scalar_select(group, &c0, mask, &co, &cb);
643 ec_scalar_select(group, &u0, mask, &uo, &ub);
644 ec_scalar_select(group, &v0, mask, &vo, &vb);
645 ec_scalar_select(group, &c1, mask, &cb, &co);
646 ec_scalar_select(group, &u1, mask, &ub, &uo);
647 ec_scalar_select(group, &v1, mask, &vb, &vo);
648
649 // Store DLEQOR2 proof in transcript.
650 if (!scalar_to_cbb(cbb, group, &c0) ||
651 !scalar_to_cbb(cbb, group, &c1) ||
652 !scalar_to_cbb(cbb, group, &u0) ||
653 !scalar_to_cbb(cbb, group, &u1) ||
654 !scalar_to_cbb(cbb, group, &v0) ||
655 !scalar_to_cbb(cbb, group, &v1)) {
656 return 0;
657 }
658
659 return 1;
660 }
661
662 static int dleq_verify(const PMBTOKEN_METHOD *method, CBS *cbs,
663 const TRUST_TOKEN_CLIENT_KEY *pub, const EC_JACOBIAN *T,
664 const EC_JACOBIAN *S, const EC_JACOBIAN *W,
665 const EC_JACOBIAN *Ws) {
666 const EC_GROUP *group = method->group;
667 const EC_JACOBIAN *g = &group->generator.raw;
668
669 // We verify a DLEQ proof for the validity token and a DLEQOR2 proof for the
670 // private metadata token. To allow amortizing Jacobian-to-affine conversions,
671 // we compute Ki for both proofs first. Additionally, all inputs to this
672 // function are public, so we can use the faster variable-time
673 // multiplications.
674 enum {
675 idx_T,
676 idx_S,
677 idx_W,
678 idx_Ws,
679 idx_Ks0,
680 idx_Ks1,
681 idx_K00,
682 idx_K01,
683 idx_K10,
684 idx_K11,
685 num_idx,
686 };
687 EC_JACOBIAN jacobians[num_idx];
688
689 // Decode the DLEQ proof.
690 EC_SCALAR cs, us, vs;
691 if (!scalar_from_cbs(cbs, group, &cs) ||
692 !scalar_from_cbs(cbs, group, &us) ||
693 !scalar_from_cbs(cbs, group, &vs)) {
694 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
695 return 0;
696 }
697
698 // Ks = us*(G;T) + vs*(H;S) - cs*(pubs;Ws)
699 EC_JACOBIAN pubs;
700 ec_affine_to_jacobian(group, &pubs, &pub->pubs);
701 EC_SCALAR minus_cs;
702 ec_scalar_neg(group, &minus_cs, &cs);
703 if (!mul_public_3(group, &jacobians[idx_Ks0], g, &us, &method->h, &vs, &pubs,
704 &minus_cs) ||
705 !mul_public_3(group, &jacobians[idx_Ks1], T, &us, S, &vs, Ws,
706 &minus_cs)) {
707 return 0;
708 }
709
710 // Decode the DLEQOR proof.
711 EC_SCALAR c0, c1, u0, u1, v0, v1;
712 if (!scalar_from_cbs(cbs, group, &c0) ||
713 !scalar_from_cbs(cbs, group, &c1) ||
714 !scalar_from_cbs(cbs, group, &u0) ||
715 !scalar_from_cbs(cbs, group, &u1) ||
716 !scalar_from_cbs(cbs, group, &v0) ||
717 !scalar_from_cbs(cbs, group, &v1)) {
718 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
719 return 0;
720 }
721
722 EC_JACOBIAN pub0, pub1;
723 ec_affine_to_jacobian(group, &pub0, &pub->pub0);
724 ec_affine_to_jacobian(group, &pub1, &pub->pub1);
725 EC_SCALAR minus_c0, minus_c1;
726 ec_scalar_neg(group, &minus_c0, &c0);
727 ec_scalar_neg(group, &minus_c1, &c1);
728 if (// K0 = u0*(G;T) + v0*(H;S) - c0*(pub0;W)
729 !mul_public_3(group, &jacobians[idx_K00], g, &u0, &method->h, &v0, &pub0,
730 &minus_c0) ||
731 !mul_public_3(group, &jacobians[idx_K01], T, &u0, S, &v0, W, &minus_c0) ||
732 // K1 = u1*(G;T) + v1*(H;S) - c1*(pub1;W)
733 !mul_public_3(group, &jacobians[idx_K10], g, &u1, &method->h, &v1, &pub1,
734 &minus_c1) ||
735 !mul_public_3(group, &jacobians[idx_K11], T, &u1, S, &v1, W, &minus_c1)) {
736 return 0;
737 }
738
739 EC_AFFINE affines[num_idx];
740 jacobians[idx_T] = *T;
741 jacobians[idx_S] = *S;
742 jacobians[idx_W] = *W;
743 jacobians[idx_Ws] = *Ws;
744 if (!ec_jacobian_to_affine_batch(group, affines, jacobians, num_idx)) {
745 return 0;
746 }
747
748 // Check the DLEQ proof.
749 EC_SCALAR calculated;
750 if (!hash_c_dleq(method, &calculated, &pub->pubs, &affines[idx_T],
751 &affines[idx_S], &affines[idx_Ws], &affines[idx_Ks0],
752 &affines[idx_Ks1])) {
753 return 0;
754 }
755
756 // cs == calculated
757 if (!ec_scalar_equal_vartime(group, &cs, &calculated)) {
758 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_PROOF);
759 return 0;
760 }
761
762 // Check the DLEQOR proof.
763 if (!hash_c_dleqor(method, &calculated, &pub->pub0, &pub->pub1,
764 &affines[idx_T], &affines[idx_S], &affines[idx_W],
765 &affines[idx_K00], &affines[idx_K01], &affines[idx_K10],
766 &affines[idx_K11])) {
767 return 0;
768 }
769
770 // c0 + c1 == calculated
771 EC_SCALAR c;
772 ec_scalar_add(group, &c, &c0, &c1);
773 if (!ec_scalar_equal_vartime(group, &c, &calculated)) {
774 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_PROOF);
775 return 0;
776 }
777
778 return 1;
779 }
780
781 static int pmbtoken_sign(const PMBTOKEN_METHOD *method,
782 const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
783 size_t num_requested, size_t num_to_issue,
784 uint8_t private_metadata) {
785 const EC_GROUP *group = method->group;
786 if (num_requested < num_to_issue) {
787 OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR);
788 return 0;
789 }
790
791 int ret = 0;
792 EC_JACOBIAN *Tps = OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN));
793 EC_JACOBIAN *Sps = OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN));
794 EC_JACOBIAN *Wps = OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN));
795 EC_JACOBIAN *Wsps = OPENSSL_calloc(num_to_issue, sizeof(EC_JACOBIAN));
796 EC_SCALAR *es = OPENSSL_calloc(num_to_issue, sizeof(EC_SCALAR));
797 CBB batch_cbb;
798 CBB_zero(&batch_cbb);
799 if (!Tps ||
800 !Sps ||
801 !Wps ||
802 !Wsps ||
803 !es ||
804 !CBB_init(&batch_cbb, 0) ||
805 !point_to_cbb(&batch_cbb, method->group, &key->pubs) ||
806 !point_to_cbb(&batch_cbb, method->group, &key->pub0) ||
807 !point_to_cbb(&batch_cbb, method->group, &key->pub1)) {
808 goto err;
809 }
810
811 for (size_t i = 0; i < num_to_issue; i++) {
812 EC_AFFINE Tp_affine;
813 EC_JACOBIAN Tp;
814 if (!cbs_get_prefixed_point(cbs, group, &Tp_affine, method->prefix_point)) {
815 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
816 goto err;
817 }
818 ec_affine_to_jacobian(group, &Tp, &Tp_affine);
819
820 EC_SCALAR xb, yb;
821 BN_ULONG mask = ((BN_ULONG)0) - (private_metadata & 1);
822 ec_scalar_select(group, &xb, mask, &key->x1, &key->x0);
823 ec_scalar_select(group, &yb, mask, &key->y1, &key->y0);
824
825 uint8_t s[TRUST_TOKEN_NONCE_SIZE];
826 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(s, TRUST_TOKEN_NONCE_SIZE));
827 // The |jacobians| and |affines| contain Sp, Wp, and Wsp.
828 EC_JACOBIAN jacobians[3];
829 EC_AFFINE affines[3];
830 if (!method->hash_s(group, &jacobians[0], &Tp_affine, s) ||
831 !ec_point_mul_scalar_batch(group, &jacobians[1], &Tp, &xb,
832 &jacobians[0], &yb, NULL, NULL) ||
833 !ec_point_mul_scalar_batch(group, &jacobians[2], &Tp, &key->xs,
834 &jacobians[0], &key->ys, NULL, NULL) ||
835 !ec_jacobian_to_affine_batch(group, affines, jacobians, 3) ||
836 !CBB_add_bytes(cbb, s, TRUST_TOKEN_NONCE_SIZE) ||
837 !cbb_add_prefixed_point(cbb, group, &affines[1],
838 method->prefix_point) ||
839 !cbb_add_prefixed_point(cbb, group, &affines[2],
840 method->prefix_point)) {
841 goto err;
842 }
843
844 if (!point_to_cbb(&batch_cbb, group, &Tp_affine) ||
845 !point_to_cbb(&batch_cbb, group, &affines[0]) ||
846 !point_to_cbb(&batch_cbb, group, &affines[1]) ||
847 !point_to_cbb(&batch_cbb, group, &affines[2])) {
848 goto err;
849 }
850 Tps[i] = Tp;
851 Sps[i] = jacobians[0];
852 Wps[i] = jacobians[1];
853 Wsps[i] = jacobians[2];
854
855 if (!CBB_flush(cbb)) {
856 goto err;
857 }
858 }
859
860 // The DLEQ batching construction is described in appendix B of
861 // https://eprint.iacr.org/2020/072/20200324:214215. Note the additional
862 // computations all act on public inputs.
863 for (size_t i = 0; i < num_to_issue; i++) {
864 if (!hash_c_batch(method, &es[i], &batch_cbb, i)) {
865 goto err;
866 }
867 }
868
869 EC_JACOBIAN Tp_batch, Sp_batch, Wp_batch, Wsp_batch;
870 if (!ec_point_mul_scalar_public_batch(group, &Tp_batch,
871 /*g_scalar=*/NULL, Tps, es,
872 num_to_issue) ||
873 !ec_point_mul_scalar_public_batch(group, &Sp_batch,
874 /*g_scalar=*/NULL, Sps, es,
875 num_to_issue) ||
876 !ec_point_mul_scalar_public_batch(group, &Wp_batch,
877 /*g_scalar=*/NULL, Wps, es,
878 num_to_issue) ||
879 !ec_point_mul_scalar_public_batch(group, &Wsp_batch,
880 /*g_scalar=*/NULL, Wsps, es,
881 num_to_issue)) {
882 goto err;
883 }
884
885 CBB proof;
886 if (!CBB_add_u16_length_prefixed(cbb, &proof) ||
887 !dleq_generate(method, &proof, key, &Tp_batch, &Sp_batch, &Wp_batch,
888 &Wsp_batch, private_metadata) ||
889 !CBB_flush(cbb)) {
890 goto err;
891 }
892
893 // Skip over any unused requests.
894 size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
895 size_t token_len = point_len;
896 if (method->prefix_point) {
897 token_len += 2;
898 }
899 if (!CBS_skip(cbs, token_len * (num_requested - num_to_issue))) {
900 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
901 goto err;
902 }
903
904 ret = 1;
905
906 err:
907 OPENSSL_free(Tps);
908 OPENSSL_free(Sps);
909 OPENSSL_free(Wps);
910 OPENSSL_free(Wsps);
911 OPENSSL_free(es);
912 CBB_cleanup(&batch_cbb);
913 return ret;
914 }
915
916 static STACK_OF(TRUST_TOKEN) *pmbtoken_unblind(
917 const PMBTOKEN_METHOD *method, const TRUST_TOKEN_CLIENT_KEY *key,
918 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
919 uint32_t key_id) {
920 const EC_GROUP *group = method->group;
921 if (count > sk_TRUST_TOKEN_PRETOKEN_num(pretokens)) {
922 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
923 return NULL;
924 }
925
926 int ok = 0;
927 STACK_OF(TRUST_TOKEN) *ret = sk_TRUST_TOKEN_new_null();
928 EC_JACOBIAN *Tps = OPENSSL_calloc(count, sizeof(EC_JACOBIAN));
929 EC_JACOBIAN *Sps = OPENSSL_calloc(count, sizeof(EC_JACOBIAN));
930 EC_JACOBIAN *Wps = OPENSSL_calloc(count, sizeof(EC_JACOBIAN));
931 EC_JACOBIAN *Wsps = OPENSSL_calloc(count, sizeof(EC_JACOBIAN));
932 EC_SCALAR *es = OPENSSL_calloc(count, sizeof(EC_SCALAR));
933 CBB batch_cbb;
934 CBB_zero(&batch_cbb);
935 if (ret == NULL ||
936 Tps == NULL ||
937 Sps == NULL ||
938 Wps == NULL ||
939 Wsps == NULL ||
940 es == NULL ||
941 !CBB_init(&batch_cbb, 0) ||
942 !point_to_cbb(&batch_cbb, method->group, &key->pubs) ||
943 !point_to_cbb(&batch_cbb, method->group, &key->pub0) ||
944 !point_to_cbb(&batch_cbb, method->group, &key->pub1)) {
945 goto err;
946 }
947
948 for (size_t i = 0; i < count; i++) {
949 const TRUST_TOKEN_PRETOKEN *pretoken =
950 sk_TRUST_TOKEN_PRETOKEN_value(pretokens, i);
951
952 uint8_t s[TRUST_TOKEN_NONCE_SIZE];
953 EC_AFFINE Wp_affine, Wsp_affine;
954 if (!CBS_copy_bytes(cbs, s, TRUST_TOKEN_NONCE_SIZE) ||
955 !cbs_get_prefixed_point(cbs, group, &Wp_affine, method->prefix_point) ||
956 !cbs_get_prefixed_point(cbs, group, &Wsp_affine,
957 method->prefix_point)) {
958 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
959 goto err;
960 }
961
962 ec_affine_to_jacobian(group, &Tps[i], &pretoken->Tp);
963 ec_affine_to_jacobian(group, &Wps[i], &Wp_affine);
964 ec_affine_to_jacobian(group, &Wsps[i], &Wsp_affine);
965 if (!method->hash_s(group, &Sps[i], &pretoken->Tp, s)) {
966 goto err;
967 }
968
969 EC_AFFINE Sp_affine;
970 if (!point_to_cbb(&batch_cbb, group, &pretoken->Tp) ||
971 !ec_jacobian_to_affine(group, &Sp_affine, &Sps[i]) ||
972 !point_to_cbb(&batch_cbb, group, &Sp_affine) ||
973 !point_to_cbb(&batch_cbb, group, &Wp_affine) ||
974 !point_to_cbb(&batch_cbb, group, &Wsp_affine)) {
975 goto err;
976 }
977
978 // Unblind the token.
979 EC_JACOBIAN jacobians[3];
980 EC_AFFINE affines[3];
981 if (!ec_point_mul_scalar(group, &jacobians[0], &Sps[i], &pretoken->r) ||
982 !ec_point_mul_scalar(group, &jacobians[1], &Wps[i], &pretoken->r) ||
983 !ec_point_mul_scalar(group, &jacobians[2], &Wsps[i], &pretoken->r) ||
984 !ec_jacobian_to_affine_batch(group, affines, jacobians, 3)) {
985 goto err;
986 }
987
988 // Serialize the token. Include |key_id| to avoid an extra copy in the layer
989 // above.
990 CBB token_cbb;
991 size_t point_len = ec_point_byte_len(group, POINT_CONVERSION_UNCOMPRESSED);
992 if (!CBB_init(&token_cbb,
993 4 + TRUST_TOKEN_NONCE_SIZE + 3 * (2 + point_len)) ||
994 !CBB_add_u32(&token_cbb, key_id) ||
995 !CBB_add_bytes(&token_cbb, pretoken->salt, TRUST_TOKEN_NONCE_SIZE) ||
996 !cbb_add_prefixed_point(&token_cbb, group, &affines[0],
997 method->prefix_point) ||
998 !cbb_add_prefixed_point(&token_cbb, group, &affines[1],
999 method->prefix_point) ||
1000 !cbb_add_prefixed_point(&token_cbb, group, &affines[2],
1001 method->prefix_point) ||
1002 !CBB_flush(&token_cbb)) {
1003 CBB_cleanup(&token_cbb);
1004 goto err;
1005 }
1006
1007 TRUST_TOKEN *token =
1008 TRUST_TOKEN_new(CBB_data(&token_cbb), CBB_len(&token_cbb));
1009 CBB_cleanup(&token_cbb);
1010 if (token == NULL ||
1011 !sk_TRUST_TOKEN_push(ret, token)) {
1012 TRUST_TOKEN_free(token);
1013 goto err;
1014 }
1015 }
1016
1017 // The DLEQ batching construction is described in appendix B of
1018 // https://eprint.iacr.org/2020/072/20200324:214215. Note the additional
1019 // computations all act on public inputs.
1020 for (size_t i = 0; i < count; i++) {
1021 if (!hash_c_batch(method, &es[i], &batch_cbb, i)) {
1022 goto err;
1023 }
1024 }
1025
1026 EC_JACOBIAN Tp_batch, Sp_batch, Wp_batch, Wsp_batch;
1027 if (!ec_point_mul_scalar_public_batch(group, &Tp_batch,
1028 /*g_scalar=*/NULL, Tps, es, count) ||
1029 !ec_point_mul_scalar_public_batch(group, &Sp_batch,
1030 /*g_scalar=*/NULL, Sps, es, count) ||
1031 !ec_point_mul_scalar_public_batch(group, &Wp_batch,
1032 /*g_scalar=*/NULL, Wps, es, count) ||
1033 !ec_point_mul_scalar_public_batch(group, &Wsp_batch,
1034 /*g_scalar=*/NULL, Wsps, es, count)) {
1035 goto err;
1036 }
1037
1038 CBS proof;
1039 if (!CBS_get_u16_length_prefixed(cbs, &proof) ||
1040 !dleq_verify(method, &proof, key, &Tp_batch, &Sp_batch, &Wp_batch,
1041 &Wsp_batch) ||
1042 CBS_len(&proof) != 0) {
1043 goto err;
1044 }
1045
1046 ok = 1;
1047
1048 err:
1049 OPENSSL_free(Tps);
1050 OPENSSL_free(Sps);
1051 OPENSSL_free(Wps);
1052 OPENSSL_free(Wsps);
1053 OPENSSL_free(es);
1054 CBB_cleanup(&batch_cbb);
1055 if (!ok) {
1056 sk_TRUST_TOKEN_pop_free(ret, TRUST_TOKEN_free);
1057 ret = NULL;
1058 }
1059 return ret;
1060 }
1061
1062 static int pmbtoken_read(const PMBTOKEN_METHOD *method,
1063 const TRUST_TOKEN_ISSUER_KEY *key,
1064 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
1065 uint8_t *out_private_metadata, const uint8_t *token,
1066 size_t token_len, int include_message,
1067 const uint8_t *msg, size_t msg_len) {
1068 const EC_GROUP *group = method->group;
1069 CBS cbs, salt;
1070 CBS_init(&cbs, token, token_len);
1071 EC_AFFINE S, W, Ws;
1072 if (!CBS_get_bytes(&cbs, &salt, TRUST_TOKEN_NONCE_SIZE) ||
1073 !cbs_get_prefixed_point(&cbs, group, &S, method->prefix_point) ||
1074 !cbs_get_prefixed_point(&cbs, group, &W, method->prefix_point) ||
1075 !cbs_get_prefixed_point(&cbs, group, &Ws, method->prefix_point) ||
1076 CBS_len(&cbs) != 0) {
1077 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_TOKEN);
1078 return 0;
1079 }
1080
1081 if (include_message) {
1082 SHA512_CTX hash_ctx;
1083 assert(SHA512_DIGEST_LENGTH == TRUST_TOKEN_NONCE_SIZE);
1084 SHA512_Init(&hash_ctx);
1085 SHA512_Update(&hash_ctx, CBS_data(&salt), CBS_len(&salt));
1086 SHA512_Update(&hash_ctx, msg, msg_len);
1087 SHA512_Final(out_nonce, &hash_ctx);
1088 } else {
1089 OPENSSL_memcpy(out_nonce, CBS_data(&salt), CBS_len(&salt));
1090 }
1091
1092 EC_JACOBIAN T;
1093 if (!method->hash_t(group, &T, out_nonce)) {
1094 return 0;
1095 }
1096
1097 // We perform three multiplications with S and T. This is enough that it is
1098 // worth using |ec_point_mul_scalar_precomp|.
1099 EC_JACOBIAN S_jacobian;
1100 EC_PRECOMP S_precomp, T_precomp;
1101 ec_affine_to_jacobian(group, &S_jacobian, &S);
1102 if (!ec_init_precomp(group, &S_precomp, &S_jacobian) ||
1103 !ec_init_precomp(group, &T_precomp, &T)) {
1104 return 0;
1105 }
1106
1107 EC_JACOBIAN Ws_calculated;
1108 // Check the validity of the token.
1109 if (!ec_point_mul_scalar_precomp(group, &Ws_calculated, &T_precomp, &key->xs,
1110 &S_precomp, &key->ys, NULL, NULL) ||
1111 !ec_affine_jacobian_equal(group, &Ws, &Ws_calculated)) {
1112 OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BAD_VALIDITY_CHECK);
1113 return 0;
1114 }
1115
1116 EC_JACOBIAN W0, W1;
1117 if (!ec_point_mul_scalar_precomp(group, &W0, &T_precomp, &key->x0, &S_precomp,
1118 &key->y0, NULL, NULL) ||
1119 !ec_point_mul_scalar_precomp(group, &W1, &T_precomp, &key->x1, &S_precomp,
1120 &key->y1, NULL, NULL)) {
1121 return 0;
1122 }
1123
1124 const int is_W0 = ec_affine_jacobian_equal(group, &W, &W0);
1125 const int is_W1 = ec_affine_jacobian_equal(group, &W, &W1);
1126 const int is_valid = is_W0 ^ is_W1;
1127 if (!is_valid) {
1128 // Invalid tokens will fail the validity check above.
1129 OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR);
1130 return 0;
1131 }
1132
1133 *out_private_metadata = is_W1;
1134 return 1;
1135 }
1136
1137
1138 // PMBTokens experiment v1.
1139
1140 static int pmbtoken_exp1_hash_t(const EC_GROUP *group, EC_JACOBIAN *out,
1141 const uint8_t t[TRUST_TOKEN_NONCE_SIZE]) {
1142 const uint8_t kHashTLabel[] = "PMBTokens Experiment V1 HashT";
1143 return ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
1144 group, out, kHashTLabel, sizeof(kHashTLabel), t, TRUST_TOKEN_NONCE_SIZE);
1145 }
1146
1147 static int pmbtoken_exp1_hash_s(const EC_GROUP *group, EC_JACOBIAN *out,
1148 const EC_AFFINE *t,
1149 const uint8_t s[TRUST_TOKEN_NONCE_SIZE]) {
1150 const uint8_t kHashSLabel[] = "PMBTokens Experiment V1 HashS";
1151 int ret = 0;
1152 CBB cbb;
1153 uint8_t *buf = NULL;
1154 size_t len;
1155 if (!CBB_init(&cbb, 0) ||
1156 !point_to_cbb(&cbb, group, t) ||
1157 !CBB_add_bytes(&cbb, s, TRUST_TOKEN_NONCE_SIZE) ||
1158 !CBB_finish(&cbb, &buf, &len) ||
1159 !ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
1160 group, out, kHashSLabel, sizeof(kHashSLabel), buf, len)) {
1161 goto err;
1162 }
1163
1164 ret = 1;
1165
1166 err:
1167 OPENSSL_free(buf);
1168 CBB_cleanup(&cbb);
1169 return ret;
1170 }
1171
1172 static int pmbtoken_exp1_hash_c(const EC_GROUP *group, EC_SCALAR *out,
1173 uint8_t *buf, size_t len) {
1174 const uint8_t kHashCLabel[] = "PMBTokens Experiment V1 HashC";
1175 return ec_hash_to_scalar_p384_xmd_sha512_draft07(
1176 group, out, kHashCLabel, sizeof(kHashCLabel), buf, len);
1177 }
1178
1179 static int pmbtoken_exp1_hash_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
1180 uint8_t *buf, size_t len) {
1181 const uint8_t kHashLabel[] = "PMBTokens Experiment V1 HashToScalar";
1182 return ec_hash_to_scalar_p384_xmd_sha512_draft07(
1183 group, out, kHashLabel, sizeof(kHashLabel), buf, len);
1184 }
1185
1186 static int pmbtoken_exp1_ok = 0;
1187 static PMBTOKEN_METHOD pmbtoken_exp1_method;
1188 static CRYPTO_once_t pmbtoken_exp1_method_once = CRYPTO_ONCE_INIT;
1189
1190 static void pmbtoken_exp1_init_method_impl(void) {
1191 // This is the output of |ec_hash_to_scalar_p384_xmd_sha512_draft07| with DST
1192 // "PMBTokens Experiment V1 HashH" and message "generator".
1193 static const uint8_t kH[] = {
1194 0x04, 0x82, 0xd5, 0x68, 0xf5, 0x39, 0xf6, 0x08, 0x19, 0xa1, 0x75,
1195 0x9f, 0x98, 0xb5, 0x10, 0xf5, 0x0b, 0x9d, 0x2b, 0xe1, 0x64, 0x4d,
1196 0x02, 0x76, 0x18, 0x11, 0xf8, 0x2f, 0xd3, 0x33, 0x25, 0x1f, 0x2c,
1197 0xb8, 0xf6, 0xf1, 0x9e, 0x93, 0x85, 0x79, 0xb3, 0xb7, 0x81, 0xa3,
1198 0xe6, 0x23, 0xc3, 0x1c, 0xff, 0x03, 0xd9, 0x40, 0x6c, 0xec, 0xe0,
1199 0x4d, 0xea, 0xdf, 0x9d, 0x94, 0xd1, 0x87, 0xab, 0x27, 0xf7, 0x4f,
1200 0x53, 0xea, 0xa3, 0x18, 0x72, 0xb9, 0xd1, 0x56, 0xa0, 0x4e, 0x81,
1201 0xaa, 0xeb, 0x1c, 0x22, 0x6d, 0x39, 0x1c, 0x5e, 0xb1, 0x27, 0xfc,
1202 0x87, 0xc3, 0x95, 0xd0, 0x13, 0xb7, 0x0b, 0x5c, 0xc7,
1203 };
1204
1205 pmbtoken_exp1_ok = pmbtoken_init_method(
1206 &pmbtoken_exp1_method, EC_group_p384(), kH, sizeof(kH),
1207 pmbtoken_exp1_hash_t, pmbtoken_exp1_hash_s, pmbtoken_exp1_hash_c,
1208 pmbtoken_exp1_hash_to_scalar, 1);
1209 }
1210
1211 static int pmbtoken_exp1_init_method(void) {
1212 CRYPTO_once(&pmbtoken_exp1_method_once, pmbtoken_exp1_init_method_impl);
1213 if (!pmbtoken_exp1_ok) {
1214 OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR);
1215 return 0;
1216 }
1217 return 1;
1218 }
1219
1220 int pmbtoken_exp1_generate_key(CBB *out_private, CBB *out_public) {
1221 if (!pmbtoken_exp1_init_method()) {
1222 return 0;
1223 }
1224
1225 return pmbtoken_generate_key(&pmbtoken_exp1_method, out_private, out_public);
1226 }
1227
1228 int pmbtoken_exp1_derive_key_from_secret(CBB *out_private, CBB *out_public,
1229 const uint8_t *secret,
1230 size_t secret_len) {
1231 if (!pmbtoken_exp1_init_method()) {
1232 return 0;
1233 }
1234
1235 return pmbtoken_derive_key_from_secret(&pmbtoken_exp1_method, out_private,
1236 out_public, secret, secret_len);
1237 }
1238
1239 int pmbtoken_exp1_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
1240 const uint8_t *in, size_t len) {
1241 if (!pmbtoken_exp1_init_method()) {
1242 return 0;
1243 }
1244 return pmbtoken_client_key_from_bytes(&pmbtoken_exp1_method, key, in, len);
1245 }
1246
1247 int pmbtoken_exp1_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
1248 const uint8_t *in, size_t len) {
1249 if (!pmbtoken_exp1_init_method()) {
1250 return 0;
1251 }
1252 return pmbtoken_issuer_key_from_bytes(&pmbtoken_exp1_method, key, in, len);
1253 }
1254
1255 STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_exp1_blind(CBB *cbb, size_t count,
1256 int include_message,
1257 const uint8_t *msg,
1258 size_t msg_len) {
1259 if (!pmbtoken_exp1_init_method()) {
1260 return NULL;
1261 }
1262 return pmbtoken_blind(&pmbtoken_exp1_method, cbb, count, include_message, msg,
1263 msg_len);
1264 }
1265
1266 int pmbtoken_exp1_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
1267 size_t num_requested, size_t num_to_issue,
1268 uint8_t private_metadata) {
1269 if (!pmbtoken_exp1_init_method()) {
1270 return 0;
1271 }
1272 return pmbtoken_sign(&pmbtoken_exp1_method, key, cbb, cbs, num_requested,
1273 num_to_issue, private_metadata);
1274 }
1275
1276 STACK_OF(TRUST_TOKEN) *pmbtoken_exp1_unblind(
1277 const TRUST_TOKEN_CLIENT_KEY *key,
1278 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
1279 uint32_t key_id) {
1280 if (!pmbtoken_exp1_init_method()) {
1281 return NULL;
1282 }
1283 return pmbtoken_unblind(&pmbtoken_exp1_method, key, pretokens, cbs, count,
1284 key_id);
1285 }
1286
1287 int pmbtoken_exp1_read(const TRUST_TOKEN_ISSUER_KEY *key,
1288 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
1289 uint8_t *out_private_metadata, const uint8_t *token,
1290 size_t token_len, int include_message,
1291 const uint8_t *msg, size_t msg_len) {
1292 if (!pmbtoken_exp1_init_method()) {
1293 return 0;
1294 }
1295 return pmbtoken_read(&pmbtoken_exp1_method, key, out_nonce,
1296 out_private_metadata, token, token_len, include_message,
1297 msg, msg_len);
1298 }
1299
1300 int pmbtoken_exp1_get_h_for_testing(uint8_t out[97]) {
1301 if (!pmbtoken_exp1_init_method()) {
1302 return 0;
1303 }
1304 EC_AFFINE h;
1305 return ec_jacobian_to_affine(pmbtoken_exp1_method.group, &h,
1306 &pmbtoken_exp1_method.h) &&
1307 ec_point_to_bytes(pmbtoken_exp1_method.group, &h,
1308 POINT_CONVERSION_UNCOMPRESSED, out, 97) == 97;
1309 }
1310
1311 // PMBTokens experiment v2.
1312
1313 static int pmbtoken_exp2_hash_t(const EC_GROUP *group, EC_JACOBIAN *out,
1314 const uint8_t t[TRUST_TOKEN_NONCE_SIZE]) {
1315 const uint8_t kHashTLabel[] = "PMBTokens Experiment V2 HashT";
1316 return ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
1317 group, out, kHashTLabel, sizeof(kHashTLabel), t, TRUST_TOKEN_NONCE_SIZE);
1318 }
1319
1320 static int pmbtoken_exp2_hash_s(const EC_GROUP *group, EC_JACOBIAN *out,
1321 const EC_AFFINE *t,
1322 const uint8_t s[TRUST_TOKEN_NONCE_SIZE]) {
1323 const uint8_t kHashSLabel[] = "PMBTokens Experiment V2 HashS";
1324 int ret = 0;
1325 CBB cbb;
1326 uint8_t *buf = NULL;
1327 size_t len;
1328 if (!CBB_init(&cbb, 0) ||
1329 !point_to_cbb(&cbb, group, t) ||
1330 !CBB_add_bytes(&cbb, s, TRUST_TOKEN_NONCE_SIZE) ||
1331 !CBB_finish(&cbb, &buf, &len) ||
1332 !ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
1333 group, out, kHashSLabel, sizeof(kHashSLabel), buf, len)) {
1334 goto err;
1335 }
1336
1337 ret = 1;
1338
1339 err:
1340 OPENSSL_free(buf);
1341 CBB_cleanup(&cbb);
1342 return ret;
1343 }
1344
1345 static int pmbtoken_exp2_hash_c(const EC_GROUP *group, EC_SCALAR *out,
1346 uint8_t *buf, size_t len) {
1347 const uint8_t kHashCLabel[] = "PMBTokens Experiment V2 HashC";
1348 return ec_hash_to_scalar_p384_xmd_sha512_draft07(
1349 group, out, kHashCLabel, sizeof(kHashCLabel), buf, len);
1350 }
1351
1352 static int pmbtoken_exp2_hash_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
1353 uint8_t *buf, size_t len) {
1354 const uint8_t kHashLabel[] = "PMBTokens Experiment V2 HashToScalar";
1355 return ec_hash_to_scalar_p384_xmd_sha512_draft07(
1356 group, out, kHashLabel, sizeof(kHashLabel), buf, len);
1357 }
1358
1359 static int pmbtoken_exp2_ok = 0;
1360 static PMBTOKEN_METHOD pmbtoken_exp2_method;
1361 static CRYPTO_once_t pmbtoken_exp2_method_once = CRYPTO_ONCE_INIT;
1362
1363 static void pmbtoken_exp2_init_method_impl(void) {
1364 // This is the output of |ec_hash_to_scalar_p384_xmd_sha512_draft07| with DST
1365 // "PMBTokens Experiment V2 HashH" and message "generator".
1366 static const uint8_t kH[] = {
1367 0x04, 0xbc, 0x27, 0x24, 0x99, 0xfa, 0xc9, 0xa4, 0x74, 0x6f, 0xf9,
1368 0x07, 0x81, 0x55, 0xf8, 0x1f, 0x6f, 0xda, 0x09, 0xe7, 0x8c, 0x5d,
1369 0x9e, 0x4e, 0x14, 0x7c, 0x53, 0x14, 0xbc, 0x7e, 0x29, 0x57, 0x92,
1370 0x17, 0x94, 0x6e, 0xd2, 0xdf, 0xa5, 0x31, 0x1b, 0x4e, 0xb7, 0xfc,
1371 0x93, 0xe3, 0x6e, 0x14, 0x1f, 0x4f, 0x14, 0xf3, 0xe5, 0x47, 0x61,
1372 0x1c, 0x2c, 0x72, 0x25, 0xf0, 0x4a, 0x45, 0x23, 0x2d, 0x57, 0x93,
1373 0x0e, 0xb2, 0x55, 0xb8, 0x57, 0x25, 0x4c, 0x1e, 0xdb, 0xfd, 0x58,
1374 0x70, 0x17, 0x9a, 0xbb, 0x9e, 0x5e, 0x93, 0x9e, 0x92, 0xd3, 0xe8,
1375 0x25, 0x62, 0xbf, 0x59, 0xb2, 0xd2, 0x3d, 0x71, 0xff
1376 };
1377
1378 pmbtoken_exp2_ok = pmbtoken_init_method(
1379 &pmbtoken_exp2_method, EC_group_p384(), kH, sizeof(kH),
1380 pmbtoken_exp2_hash_t, pmbtoken_exp2_hash_s, pmbtoken_exp2_hash_c,
1381 pmbtoken_exp2_hash_to_scalar, 0);
1382 }
1383
1384 static int pmbtoken_exp2_init_method(void) {
1385 CRYPTO_once(&pmbtoken_exp2_method_once, pmbtoken_exp2_init_method_impl);
1386 if (!pmbtoken_exp2_ok) {
1387 OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR);
1388 return 0;
1389 }
1390 return 1;
1391 }
1392
1393 int pmbtoken_exp2_generate_key(CBB *out_private, CBB *out_public) {
1394 if (!pmbtoken_exp2_init_method()) {
1395 return 0;
1396 }
1397
1398 return pmbtoken_generate_key(&pmbtoken_exp2_method, out_private, out_public);
1399 }
1400
1401
1402 int pmbtoken_exp2_derive_key_from_secret(CBB *out_private, CBB *out_public,
1403 const uint8_t *secret,
1404 size_t secret_len) {
1405 if (!pmbtoken_exp2_init_method()) {
1406 return 0;
1407 }
1408
1409 return pmbtoken_derive_key_from_secret(&pmbtoken_exp2_method, out_private,
1410 out_public, secret, secret_len);
1411 }
1412
1413 int pmbtoken_exp2_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
1414 const uint8_t *in, size_t len) {
1415 if (!pmbtoken_exp2_init_method()) {
1416 return 0;
1417 }
1418 return pmbtoken_client_key_from_bytes(&pmbtoken_exp2_method, key, in, len);
1419 }
1420
1421 int pmbtoken_exp2_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
1422 const uint8_t *in, size_t len) {
1423 if (!pmbtoken_exp2_init_method()) {
1424 return 0;
1425 }
1426 return pmbtoken_issuer_key_from_bytes(&pmbtoken_exp2_method, key, in, len);
1427 }
1428
1429 STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_exp2_blind(CBB *cbb, size_t count,
1430 int include_message,
1431 const uint8_t *msg,
1432 size_t msg_len) {
1433 if (!pmbtoken_exp2_init_method()) {
1434 return NULL;
1435 }
1436 return pmbtoken_blind(&pmbtoken_exp2_method, cbb, count, include_message, msg,
1437 msg_len);
1438 }
1439
1440 int pmbtoken_exp2_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
1441 size_t num_requested, size_t num_to_issue,
1442 uint8_t private_metadata) {
1443 if (!pmbtoken_exp2_init_method()) {
1444 return 0;
1445 }
1446 return pmbtoken_sign(&pmbtoken_exp2_method, key, cbb, cbs, num_requested,
1447 num_to_issue, private_metadata);
1448 }
1449
1450 STACK_OF(TRUST_TOKEN) *pmbtoken_exp2_unblind(
1451 const TRUST_TOKEN_CLIENT_KEY *key,
1452 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
1453 uint32_t key_id) {
1454 if (!pmbtoken_exp2_init_method()) {
1455 return NULL;
1456 }
1457 return pmbtoken_unblind(&pmbtoken_exp2_method, key, pretokens, cbs, count,
1458 key_id);
1459 }
1460
1461 int pmbtoken_exp2_read(const TRUST_TOKEN_ISSUER_KEY *key,
1462 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
1463 uint8_t *out_private_metadata, const uint8_t *token,
1464 size_t token_len, int include_message,
1465 const uint8_t *msg, size_t msg_len) {
1466 if (!pmbtoken_exp2_init_method()) {
1467 return 0;
1468 }
1469 return pmbtoken_read(&pmbtoken_exp2_method, key, out_nonce,
1470 out_private_metadata, token, token_len, include_message,
1471 msg, msg_len);
1472 }
1473
1474 int pmbtoken_exp2_get_h_for_testing(uint8_t out[97]) {
1475 if (!pmbtoken_exp2_init_method()) {
1476 return 0;
1477 }
1478 EC_AFFINE h;
1479 return ec_jacobian_to_affine(pmbtoken_exp2_method.group, &h,
1480 &pmbtoken_exp2_method.h) &&
1481 ec_point_to_bytes(pmbtoken_exp2_method.group, &h,
1482 POINT_CONVERSION_UNCOMPRESSED, out, 97) == 97;
1483 }
1484
1485 // PMBTokens PST v1.
1486
1487 static int pmbtoken_pst1_hash_t(const EC_GROUP *group, EC_JACOBIAN *out,
1488 const uint8_t t[TRUST_TOKEN_NONCE_SIZE]) {
1489 const uint8_t kHashTLabel[] = "PMBTokens PST V1 HashT";
1490 return ec_hash_to_curve_p384_xmd_sha384_sswu(
1491 group, out, kHashTLabel, sizeof(kHashTLabel), t, TRUST_TOKEN_NONCE_SIZE);
1492 }
1493
1494 static int pmbtoken_pst1_hash_s(const EC_GROUP *group, EC_JACOBIAN *out,
1495 const EC_AFFINE *t,
1496 const uint8_t s[TRUST_TOKEN_NONCE_SIZE]) {
1497 const uint8_t kHashSLabel[] = "PMBTokens PST V1 HashS";
1498 int ret = 0;
1499 CBB cbb;
1500 uint8_t *buf = NULL;
1501 size_t len;
1502 if (!CBB_init(&cbb, 0) ||
1503 !point_to_cbb(&cbb, group, t) ||
1504 !CBB_add_bytes(&cbb, s, TRUST_TOKEN_NONCE_SIZE) ||
1505 !CBB_finish(&cbb, &buf, &len) ||
1506 !ec_hash_to_curve_p384_xmd_sha384_sswu(
1507 group, out, kHashSLabel, sizeof(kHashSLabel), buf, len)) {
1508 goto err;
1509 }
1510
1511 ret = 1;
1512
1513 err:
1514 OPENSSL_free(buf);
1515 CBB_cleanup(&cbb);
1516 return ret;
1517 }
1518
1519 static int pmbtoken_pst1_hash_c(const EC_GROUP *group, EC_SCALAR *out,
1520 uint8_t *buf, size_t len) {
1521 const uint8_t kHashCLabel[] = "PMBTokens PST V1 HashC";
1522 return ec_hash_to_scalar_p384_xmd_sha384(
1523 group, out, kHashCLabel, sizeof(kHashCLabel), buf, len);
1524 }
1525
1526 static int pmbtoken_pst1_hash_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
1527 uint8_t *buf, size_t len) {
1528 const uint8_t kHashLabel[] = "PMBTokens PST V1 HashToScalar";
1529 return ec_hash_to_scalar_p384_xmd_sha384(
1530 group, out, kHashLabel, sizeof(kHashLabel), buf, len);
1531 }
1532
1533 static int pmbtoken_pst1_ok = 0;
1534 static PMBTOKEN_METHOD pmbtoken_pst1_method;
1535 static CRYPTO_once_t pmbtoken_pst1_method_once = CRYPTO_ONCE_INIT;
1536
1537 static void pmbtoken_pst1_init_method_impl(void) {
1538 // This is the output of |ec_hash_to_scalar_p384_xmd_sha384| with DST
1539 // "PMBTokens PST V1 HashH" and message "generator".
1540 static const uint8_t kH[] = {
1541 0x04, 0x4c, 0xfa, 0xd4, 0x33, 0x6d, 0x8c, 0x4e, 0x18, 0xce, 0x1a,
1542 0x82, 0x7b, 0x53, 0x8c, 0xf8, 0x63, 0x18, 0xe5, 0xa3, 0x96, 0x0d,
1543 0x05, 0xde, 0xf4, 0x83, 0xa7, 0xd8, 0xde, 0x9c, 0x50, 0x81, 0x38,
1544 0xc9, 0x38, 0x25, 0xa3, 0x70, 0x97, 0xc1, 0x1c, 0x33, 0x2e, 0x83,
1545 0x68, 0x64, 0x9c, 0x53, 0x73, 0xc3, 0x03, 0xc1, 0xa9, 0xd8, 0x92,
1546 0xa2, 0x32, 0xf4, 0x22, 0x40, 0x07, 0x2d, 0x9b, 0x6f, 0xab, 0xff,
1547 0x2a, 0x92, 0x03, 0xb1, 0x73, 0x09, 0x1a, 0x6a, 0x4a, 0xc2, 0x4c,
1548 0xac, 0x13, 0x59, 0xf4, 0x28, 0x0e, 0x78, 0x69, 0xa5, 0xdf, 0x0d,
1549 0x74, 0xeb, 0x14, 0xca, 0x8a, 0x32, 0xbb, 0xd3, 0x91
1550 };
1551
1552 pmbtoken_pst1_ok = pmbtoken_init_method(
1553 &pmbtoken_pst1_method, EC_group_p384(), kH, sizeof(kH),
1554 pmbtoken_pst1_hash_t, pmbtoken_pst1_hash_s, pmbtoken_pst1_hash_c,
1555 pmbtoken_pst1_hash_to_scalar, 0);
1556 }
1557
1558 static int pmbtoken_pst1_init_method(void) {
1559 CRYPTO_once(&pmbtoken_pst1_method_once, pmbtoken_pst1_init_method_impl);
1560 if (!pmbtoken_pst1_ok) {
1561 OPENSSL_PUT_ERROR(TRUST_TOKEN, ERR_R_INTERNAL_ERROR);
1562 return 0;
1563 }
1564 return 1;
1565 }
1566
1567 int pmbtoken_pst1_generate_key(CBB *out_private, CBB *out_public) {
1568 if (!pmbtoken_pst1_init_method()) {
1569 return 0;
1570 }
1571
1572 return pmbtoken_generate_key(&pmbtoken_pst1_method, out_private, out_public);
1573 }
1574
1575
1576 int pmbtoken_pst1_derive_key_from_secret(CBB *out_private, CBB *out_public,
1577 const uint8_t *secret,
1578 size_t secret_len) {
1579 if (!pmbtoken_pst1_init_method()) {
1580 return 0;
1581 }
1582
1583 return pmbtoken_derive_key_from_secret(&pmbtoken_pst1_method, out_private,
1584 out_public, secret, secret_len);
1585 }
1586
1587 int pmbtoken_pst1_client_key_from_bytes(TRUST_TOKEN_CLIENT_KEY *key,
1588 const uint8_t *in, size_t len) {
1589 if (!pmbtoken_pst1_init_method()) {
1590 return 0;
1591 }
1592 return pmbtoken_client_key_from_bytes(&pmbtoken_pst1_method, key, in, len);
1593 }
1594
1595 int pmbtoken_pst1_issuer_key_from_bytes(TRUST_TOKEN_ISSUER_KEY *key,
1596 const uint8_t *in, size_t len) {
1597 if (!pmbtoken_pst1_init_method()) {
1598 return 0;
1599 }
1600 return pmbtoken_issuer_key_from_bytes(&pmbtoken_pst1_method, key, in, len);
1601 }
1602
1603 STACK_OF(TRUST_TOKEN_PRETOKEN) *pmbtoken_pst1_blind(CBB *cbb, size_t count,
1604 int include_message,
1605 const uint8_t *msg,
1606 size_t msg_len) {
1607 if (!pmbtoken_pst1_init_method()) {
1608 return NULL;
1609 }
1610 return pmbtoken_blind(&pmbtoken_pst1_method, cbb, count, include_message, msg,
1611 msg_len);
1612 }
1613
1614 int pmbtoken_pst1_sign(const TRUST_TOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
1615 size_t num_requested, size_t num_to_issue,
1616 uint8_t private_metadata) {
1617 if (!pmbtoken_pst1_init_method()) {
1618 return 0;
1619 }
1620 return pmbtoken_sign(&pmbtoken_pst1_method, key, cbb, cbs, num_requested,
1621 num_to_issue, private_metadata);
1622 }
1623
1624 STACK_OF(TRUST_TOKEN) *pmbtoken_pst1_unblind(
1625 const TRUST_TOKEN_CLIENT_KEY *key,
1626 const STACK_OF(TRUST_TOKEN_PRETOKEN) *pretokens, CBS *cbs, size_t count,
1627 uint32_t key_id) {
1628 if (!pmbtoken_pst1_init_method()) {
1629 return NULL;
1630 }
1631 return pmbtoken_unblind(&pmbtoken_pst1_method, key, pretokens, cbs, count,
1632 key_id);
1633 }
1634
1635 int pmbtoken_pst1_read(const TRUST_TOKEN_ISSUER_KEY *key,
1636 uint8_t out_nonce[TRUST_TOKEN_NONCE_SIZE],
1637 uint8_t *out_private_metadata, const uint8_t *token,
1638 size_t token_len, int include_message,
1639 const uint8_t *msg, size_t msg_len) {
1640 if (!pmbtoken_pst1_init_method()) {
1641 return 0;
1642 }
1643 return pmbtoken_read(&pmbtoken_pst1_method, key, out_nonce,
1644 out_private_metadata, token, token_len, include_message,
1645 msg, msg_len);
1646 }
1647
1648 int pmbtoken_pst1_get_h_for_testing(uint8_t out[97]) {
1649 if (!pmbtoken_pst1_init_method()) {
1650 return 0;
1651 }
1652 EC_AFFINE h;
1653 return ec_jacobian_to_affine(pmbtoken_pst1_method.group, &h,
1654 &pmbtoken_pst1_method.h) &&
1655 ec_point_to_bytes(pmbtoken_pst1_method.group, &h,
1656 POINT_CONVERSION_UNCOMPRESSED, out, 97) == 97;
1657 }