Switch the default crypto provider

This document describes how to switch the default rustls crypto provider in Rust. You might want to switch the default crypto provider if your application has specific requirements that the default provider doesn't meet.

The Rust client libraries use Rustls to secure the communication between your application and Google Cloud. Rustls can use different providers for cryptographic functions. By default, the client libraries enable the aws-lc-rs provider. The client libraries use this provider by default because it's based on Google's BoringSSL crypto library. It's also the only Rust crypto provider that is FIPS certified.

Nevertheless, some applications might need to use a different provider. This document shows you how to disable the default provider and configure your own. The examples use google-cloud-storage with ring as an alternative crypto provider. You can customize this process for other client libraries. Consult the rustls documentation for additional providers.

Use the client libraries with the default crypto provider

To use the default crypto provider, add the client library as a dependency to your Cargo.toml file:

[dependencies]
google-cloud-storage = { version = "1" }

Use the default crypto provider with other default features disabled

Some applications prefer to disable all the default features in their dependencies and then only enable the features they want to use. For example, your Cargo.toml might look like this:

[dependencies]
google-cloud-storage = { version = "1", default-features = false }

To enable only the default provider feature, use:

[dependencies]
google-cloud-storage = { version = "1", default-features = false, features = ["default-rustls-provider"] }

Use your own crypto provider

To select your own provider:

  1. Add Rust with the default features disabled and include any dependencies needed for your provider. This example uses ring:

    You must use the same version of `rustls` as `google-cloud-storage`.
    [dependencies]
    google-cloud-storage = { version = "1", default-features = false }
    rustls               = { version = "0.23", features = ["ring"] }
    
  2. Change your main() function to install this provider:

    use rustls::crypto::{CryptoProvider, ring::default_provider};
    
    #[tokio::main]
    async fn main() -> anyhow::Result<()> {
        // Install a default crypto provider.
        CryptoProvider::install_default(default_provider())
            .map_err(|_| anyhow::anyhow!("default crypto provider already installed"))?;
        // ... ... ...
        Ok(())
    }