Application layer encryption

  • Standard Payments APIs utilize either PGP or JWE for application layer encryption to ensure secure communication.

  • PGP encryption involves exchanging public keys with Google and using specific algorithms for signing and encrypting payloads; private keys must expire within 1-2 years.

  • JWE encryption, coupled with JWS signing, employs JSON Web Tokens for encrypting and signing payloads with specific algorithm requirements and key management practices.

  • Both PGP and JWE/JWS necessitate exchanging sandbox keys for development and production keys for live environments prior to launch.

Standard Payments APIs support either PGP or JWE for application layer encryption.

PGP encryption

PGP is a standard set of encryption, decryption, and signing algorithms which provide cryptographic privacy and authentication.

When using PGP to encrypt payloads, partners must support:

  • Encrypting and decrypting payloads with multiple PGP keys.
  • Signing payloads with multiple PGP keys.
  • Verifying a payload with multiple signatures, any one of which can be the signature with the key provided by Google.
  • Decryption of Web-safe base64 encoded payloads.

Each PGP key bundle provided to Google must contain exactly two keys: one primary key for signing and certification ([SC] / CAN_SIGN) and one subkey for encryption ([E] with ENCRYPT_COMMS, ENCRYPT_STORAGE, or both). On each key rotation, you must generate a brand new two-key bundle instead of appending new subkeys to an existing key. Private keys must be RSA keys (3072-bit or larger recommended; 2048-bit minimum) that expire in one year with a maximum lifetime of two years.

Before beginning development you need to exchange PGP keys with Google. In this step, you generate a PGP public-private key pair, provide the public key to Google, and receive a public key back from Google. During development, you will only need to exchange sandbox keys used for development and testing outside of production. Before production testing and launch, you will need to perform another exchange of production keys.

Generating a new PGP key

When generating a PGP key bundle for Google, follow these requirements:

  • Key type and size: Use an RSA 3072-bit or larger key (2048-bit RSA minimum). DSA keys are not accepted.
  • Bundle cardinality: Include exactly two keys in the bundle (at most one key per purpose). Don't accumulate historical subkeys across rotations; generate a brand new two-key bundle for each key rotation.
  • Purpose separation: Don't use a single multi-purpose key that combines signing (SIGN_DATA / CAN_SIGN) and encryption (ENCRYPT_COMMS or ENCRYPT_STORAGE). One key must be designated for signing and certification ([SC] / CAN_SIGN) and the other for encryption ([E] with ENCRYPT_COMMS, ENCRYPT_STORAGE, or both).

Assuming you have a GPG binary in your system path, run gpg --full-generate-key to create a compliant key bundle. When prompted, select (1) RSA and RSA (default), a key size of 3072 bits (or at least 2048 bits), and an expiration of 1 to 2 years (for example, 14m):

$ gpg --full-generate-key
Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1
What keysize do you want? (3072) 3072
Requested keysize is 3072 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 14m
Key expires at {date}
Is this correct? (y/N) y
# Finish providing user ID details

public and secret key created and signed.

pub   rsa3072 2022-11-04 [SC] [expires: 2023-12-29]
      ABCDEF1234567890ABCDEF1234567890
uid                      name <email>
sub   rsa3072 2022-11-04 [E] [expires: 2023-12-29]

This output confirms that exactly two keys were created in the bundle: a primary key labeled pub with [SC] (Signing and Certification) and a subkey labeled sub with [E] (Encryption). Don't use the same key for both signing and encryption, and don't keep adding additional sub keys during key rotations; generate a brand new two-key bundle instead.

Before sharing an exported public key with Google, inspect the bundle without importing it:

gpg --show-keys <key.asc>

Verify that the output lists only one pub key with [SC] and one sub key with [E].

Key Rotation

To ensure uninterrupted service during key rotations, the system supports multiple active PGP keys for both partners and Google.

  • You can sign payloads with both old and new keys during their rotation. Google will validate the signature against any active public key Google has on file.

  • When you need to rotate your Google public keys, you can encrypt payloads to both old and new Google public keys. Google can decrypt with either key.

  • PGP key IDs within the message itself enable automatic key identification, eliminating the need for payload version fields or coordinated switchover times.

PGP Library Configuration

Sending Payloads

  1. When signing, you should use SHA384 as the digest algorithm; don't use SHA1 or MD5
  2. When encrypting, you should use AES256 as the symmetric encryption algorithm; don't use CAST5 or IDEA
  3. When encrypting or signing messages, be sure to select the sub key with the corresponding purpose; use the CAN_SIGN key for signing and the ENCRYPT_COMMS/ENCRYPT_STORAGE key for encrypting

Receiving Payloads

  1. When verifying a payload, make sure your library supports modern hash algorithms like SHA384. Google uses SHA384 on all new keys.
  2. When decrypting a payload, make sure your library supports modern symmetric encryption algorithms like AES256. Google uses AES256 on all new keys.

GPG Payload Encryption Example

The following command is an example of how to select secure options when using GPG. It is expected that this operation is performed in a trusted environment where people don't have access to the private keys or sensitive input files.

gpg --output signed-and-encrypted.pgp \
  --sign --digest-algo SHA384 \
  --encrypt --cipher-algo AES256 \
  --armor \
  --recipient {key_id} \
  input.txt

GPG will automatically select the right key from the bundle for each operation you ask it to perform.

JWE encryption with JWS signing

JSON Web Encryption (JWE) is a standard defined by rfc7516 for encrypting content at the application level. JSON Web Signature (JWS) is a standard defined by rfc7515 for signing content at the application level.

Requests and responses will be JWE tokens encrypted using asymmetric (public key) encryption with the "Compact Serialization" option. The JWE token will contain the signed payload as a JWS token. JWS also uses asymmetric keys; private key for signing and public key for verification.

When sending a payload, sign the payload first and then encrypt it. When receiving a payload, decrypt it first and then verify the signature.

When using JWE, Partners must support the following options:

  • Compact Serialization.
  • Decrypting payloads from one of multiple JWE keys.
  • The RSA-OAEP, RSA-OAEP-256, or ECDH-ES algorithm for key management.
  • The A256GCM, A128GCM, A128CBC-HS256, or A256CBC-HS512 algorithm for content encryption.
    • Populated in the enc header.
  • kid header to identify the public encryption key.
  • Message payloads that use JWE encryption must use the content type application/jose; charset=utf-8.

When using JWS, Partners must support the following options:

  • Compact Serialization.
  • Verifying payloads from one of multiple JWS keys.
  • The HS256, HS384, HS512, RS256, RS384, RS512, ES256, PS256, PS384, or PS512 algorithm for signature creation.
  • kid header to identify the private signing key.

JWE/JWS strings will be encoded as UTF-8 strings and their payloads may be arbitrary bytes.

Private keys must be RSA/ECDH-ES keys that expire in one year with a max lifetime of two years. All private key identities must always stay on the partner's server and, accordingly, all signature values must be calculated on the partner's server.

Before beginning development you need to exchange JWE and JWS keys with Google. Keys should be exchanged in JWK format, as defined in rfc7517. In this step, you generate a public-private key pair, provide the public key to Google, and receive a public key back from Google. During development, you will only need to exchange sandbox keys used for development and testing outside of production. Before production testing and launch, you will need to perform another exchange of production keys.