Route PMBToken calls through TRUST_TOKEN_METHOD.

Change-Id: I8b87484ea94cf1f931fa66216aab4654abe26bd3
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/41068
Commit-Queue: Steven Valdez <svaldez@google.com>
Reviewed-by: Steven Valdez <svaldez@google.com>
diff --git a/crypto/trust_token/internal.h b/crypto/trust_token/internal.h
index 56c750a..f542c9a 100644
--- a/crypto/trust_token/internal.h
+++ b/crypto/trust_token/internal.h
@@ -72,72 +72,88 @@
 
 DEFINE_STACK_OF(PMBTOKEN_PRETOKEN)
 
-// pmbtoken_generate_key generates a fresh keypair and writes their serialized
-// forms into |out_private| and |out_public|. It returns one on success and zero
-// on failure.
+// The following functions implement the corresponding |TRUST_TOKENS_METHOD|
+// functions for |TRUST_TOKENS_experiment_v0|'s PMBTokens construction.
 int pmbtoken_generate_key(CBB *out_private, CBB *out_public);
-
-// pmbtoken_client_key_from_bytes decodes a client key from |in| and sets |key|
-// to the resulting key. It returns one on success and zero
-// on failure.
 int pmbtoken_client_key_from_bytes(PMBTOKEN_CLIENT_KEY *key, const uint8_t *in,
                                    size_t len);
-
-// pmbtoken_issuer_key_from_bytes decodes a issuer key from |in| and sets |key|
-// to the resulting key. It returns one on success and zero
-// on failure.
 int pmbtoken_issuer_key_from_bytes(PMBTOKEN_ISSUER_KEY *key, const uint8_t *in,
                                    size_t len);
-
-// pmbtoken_blind generates a new issuance request for |count| tokens. On
-// success, it returns a newly-allocated |STACK_OF(PMBTOKEN_PRETOKEN)| and
-// writes a request to the issuer to |cbb|. On failure, it returns NULL. The
-// |STACK_OF(PMBTOKEN_PRETOKEN)|s should be passed to |pmbtoken_unblind| when
-// the server responds.
-//
-// This function implements the AT.Usr0 operation.
-STACK_OF(PMBTOKEN_PRETOKEN) *pmbtoken_blind(CBB *cbb, size_t count);
-
-// pmbtoken_sign parses a request for |num_requested| tokens from |cbs| and
-// issues |num_to_issue| tokens with |key| and a private metadata value of
-// |private_metadata|. It then writes the response to |cbb|. It returns one on
-// success and zero on failure.
-//
-// This function implements the AT.Sig operation.
+STACK_OF(PMBTOKEN_PRETOKEN) * pmbtoken_blind(CBB *cbb, size_t count);
 int pmbtoken_sign(const PMBTOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
                   size_t num_requested, size_t num_to_issue,
                   uint8_t private_metadata);
-
-// pmbtoken_unblind processes an issuance response for |count| tokens from |cbs|
-// and unblinds the signed tokens. |pretokens| are the pre-tokens returned from
-// the corresponding |pmbtoken_blind| call. On success, the function returns a
-// newly-allocated |STACK_OF(TRUST_TOKEN)| containing the resulting tokens. Each
-// token's serialization will have |key_id| prepended. Otherwise, it returns
-// NULL.
-//
-// This function implements the AT.Usr1 operation.
 STACK_OF(TRUST_TOKEN) *
     pmbtoken_unblind(const PMBTOKEN_CLIENT_KEY *key,
-                     const STACK_OF(PMBTOKEN_PRETOKEN) *pretokens, CBS *cbs,
+                     const STACK_OF(PMBTOKEN_PRETOKEN) * pretokens, CBS *cbs,
                      size_t count, uint32_t key_id);
-
-// pmbtoken_read parses a PMBToken from |token| and verifies it using |key|. On
-// success, it returns one and stores the nonce and private metadata bit in
-// |out_nonce| and |*out_private_metadata|. Otherwise, it returns zero. Note
-// that, unlike the output of |pmbtoken_unblind|, |token| does not have a
-// four-byte key ID prepended.
 int pmbtoken_read(const PMBTOKEN_ISSUER_KEY *key,
                   uint8_t out_nonce[PMBTOKEN_NONCE_SIZE],
                   uint8_t *out_private_metadata, const uint8_t *token,
                   size_t token_len);
 
 
-
 // Trust Tokens internals.
 
 struct trust_token_method_st {
-  // TODO(davidben): Add functions here to swap out the PMBTokens mechanism.
-  char empty;
+  // generate_key generates a fresh keypair and writes their serialized
+  // forms into |out_private| and |out_public|. It returns one on success and
+  // zero on failure.
+  int (*generate_key)(CBB *out_private, CBB *out_public);
+
+  // client_key_from_bytes decodes a client key from |in| and sets |key|
+  // to the resulting key. It returns one on success and zero
+  // on failure.
+  int (*client_key_from_bytes)(PMBTOKEN_CLIENT_KEY *key, const uint8_t *in,
+                               size_t len);
+
+  // issuer_key_from_bytes decodes a issuer key from |in| and sets |key|
+  // to the resulting key. It returns one on success and zero
+  // on failure.
+  int (*issuer_key_from_bytes)(PMBTOKEN_ISSUER_KEY *key, const uint8_t *in,
+                               size_t len);
+
+  // blind generates a new issuance request for |count| tokens. On
+  // success, it returns a newly-allocated |STACK_OF(PMBTOKEN_PRETOKEN)| and
+  // writes a request to the issuer to |cbb|. On failure, it returns NULL. The
+  // |STACK_OF(PMBTOKEN_PRETOKEN)|s should be passed to |pmbtoken_unblind| when
+  // the server responds.
+  //
+  // This function implements the AT.Usr0 operation.
+  STACK_OF(PMBTOKEN_PRETOKEN) *(*blind)(CBB *cbb, size_t count);
+
+  // sign parses a request for |num_requested| tokens from |cbs| and
+  // issues |num_to_issue| tokens with |key| and a private metadata value of
+  // |private_metadata|. It then writes the response to |cbb|. It returns one on
+  // success and zero on failure.
+  //
+  // This function implements the AT.Sig operation.
+  int (*sign)(const PMBTOKEN_ISSUER_KEY *key, CBB *cbb, CBS *cbs,
+              size_t num_requested, size_t num_to_issue,
+              uint8_t private_metadata);
+
+  // unblind processes an issuance response for |count| tokens from |cbs|
+  // and unblinds the signed tokens. |pretokens| are the pre-tokens returned
+  // from the corresponding |blind| call. On success, the function returns a
+  // newly-allocated |STACK_OF(TRUST_TOKEN)| containing the resulting tokens.
+  // Each token's serialization will have |key_id| prepended. Otherwise, it
+  // returns NULL.
+  //
+  // This function implements the AT.Usr1 operation.
+  STACK_OF(TRUST_TOKEN) *
+      (*unblind)(const PMBTOKEN_CLIENT_KEY *key,
+                 const STACK_OF(PMBTOKEN_PRETOKEN) * pretokens, CBS *cbs,
+                 size_t count, uint32_t key_id);
+
+  // read parses a PMBToken from |token| and verifies it using |key|. On
+  // success, it returns one and stores the nonce and private metadata bit in
+  // |out_nonce| and |*out_private_metadata|. Otherwise, it returns zero. Note
+  // that, unlike the output of |unblind|, |token| does not have a
+  // four-byte key ID prepended.
+  int (*read)(const PMBTOKEN_ISSUER_KEY *key,
+              uint8_t out_nonce[PMBTOKEN_NONCE_SIZE],
+              uint8_t *out_private_metadata, const uint8_t *token,
+              size_t token_len);
 };
 
 // Structure representing a single Trust Token public key with the specified ID.
diff --git a/crypto/trust_token/trust_token.c b/crypto/trust_token/trust_token.c
index f34b41b..a18fb20 100644
--- a/crypto/trust_token/trust_token.c
+++ b/crypto/trust_token/trust_token.c
@@ -28,7 +28,15 @@
 // construction.
 
 const TRUST_TOKEN_METHOD *TRUST_TOKEN_experiment_v0(void) {
-  static const TRUST_TOKEN_METHOD kMethod = {0};
+  static const TRUST_TOKEN_METHOD kMethod = {
+      pmbtoken_generate_key,
+      pmbtoken_client_key_from_bytes,
+      pmbtoken_issuer_key_from_bytes,
+      pmbtoken_blind,
+      pmbtoken_sign,
+      pmbtoken_unblind,
+      pmbtoken_read,
+  };
   return &kMethod;
 }
 
@@ -75,7 +83,7 @@
     goto err;
   }
 
-  if (!pmbtoken_generate_key(&priv_cbb, &pub_cbb)) {
+  if (!method->generate_key(&priv_cbb, &pub_cbb)) {
     goto err;
   }
 
@@ -133,8 +141,8 @@
   CBS_init(&cbs, key, key_len);
   uint32_t key_id;
   if (!CBS_get_u32(&cbs, &key_id) ||
-      !pmbtoken_client_key_from_bytes(&key_s->key, CBS_data(&cbs),
-                                      CBS_len(&cbs))) {
+      !ctx->method->client_key_from_bytes(&key_s->key, CBS_data(&cbs),
+                                          CBS_len(&cbs))) {
     OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
     return 0;
   }
@@ -166,7 +174,7 @@
     goto err;
   }
 
-  pretokens = pmbtoken_blind(&request, count);
+  pretokens = ctx->method->blind(&request, count);
   if (pretokens == NULL) {
     goto err;
   }
@@ -223,7 +231,7 @@
   }
 
   STACK_OF(TRUST_TOKEN) *tokens =
-      pmbtoken_unblind(&key->key, ctx->pretokens, &in, count, key_id);
+      ctx->method->unblind(&key->key, ctx->pretokens, &in, count, key_id);
   if (tokens == NULL) {
     return NULL;
   }
@@ -348,8 +356,8 @@
   CBS_init(&cbs, key, key_len);
   uint32_t key_id;
   if (!CBS_get_u32(&cbs, &key_id) ||
-      !pmbtoken_issuer_key_from_bytes(&key_s->key, CBS_data(&cbs),
-                                      CBS_len(&cbs))) {
+      !ctx->method->issuer_key_from_bytes(&key_s->key, CBS_data(&cbs),
+                                          CBS_len(&cbs))) {
     OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_DECODE_FAILURE);
     return 0;
   }
@@ -430,8 +438,8 @@
     goto err;
   }
 
-  if (!pmbtoken_sign(&key->key, &response, &in, num_requested, num_to_issue,
-                     private_metadata)) {
+  if (!ctx->method->sign(&key->key, &response, &in, num_requested, num_to_issue,
+                         private_metadata)) {
     goto err;
   }
 
@@ -530,8 +538,8 @@
       trust_token_issuer_get_key(ctx, public_metadata);
   uint8_t nonce[PMBTOKEN_NONCE_SIZE];
   if (key == NULL ||
-      !pmbtoken_read(&key->key, nonce, &private_metadata, CBS_data(&token_cbs),
-                     CBS_len(&token_cbs))) {
+      !ctx->method->read(&key->key, nonce, &private_metadata,
+                         CBS_data(&token_cbs), CBS_len(&token_cbs))) {
     OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_INVALID_TOKEN);
     return 0;
   }