src/v/crypto/README.md
The Redpanda crypto library provides a convenient API for any subsystem that desires to use a cryptographic function. It will abstract away the actual implementation (OpenSSL).
This library provides the following implementations:
This library currently supports the following digest types:
There are two ways of performing a digest: one-shot and multi-part.
For one-shot, you can do the following:
#include "crypto/crypto.h"
bytes msg = {...};
auto digest = crypto::digest(crypto::digest_type::SHA256, msg);
For multi-part operation, you can do the following:
#include "crypto/crypto.h"
bytes msg = {...};
crypto::digest_ctx ctx(crypto::digest_type::SHA256);
ctx.update(msg);
auto digest = std::move(ctx).final();
Like digests, this library can perform HMAC two ways: one-shot and multi-part.
For one-shot:
#include "crypto/crypto.h"
bytes key = {...};
bytes msg = {...};
auto sig = crypto::hmac(crypto::digest_type::SHA256, key, msg);
For multi-part:
#include "crypto/crypto.h"
bytes key = {...};
bytes msg = {...};
crypto::hmac_ctx ctx(crypto::digest_type::SHA256, key);
ctx.update(msg);
auto sig = std::move(ctx).final();
Currently, this library only supports RSA keys. There are two ways of loading a key from a buffer.
First, if the key is held within a buffer in PKCS8 format, you can use
crypto::load_key:
#include "crypto/crypto.h"
bytes rsa_priv_key {...};
auto key = crypto::key::load_key(
rsa_priv_key,
crypto::format_type::PEM,
crypto::is_private_key_t::yes);
The above function can determine the type of key held in the buffer but the caller is responsible for indicating the format the key is in (PEM or DER) and whether or not it's the public half of the key or the private key.
The other way of loading a key is by its parts. Currently only RSA public key loading is available:
#include "crypto/crypto.h"
bytes modulus {...};
bytes public_exponent {...};
auto key = crypto::key::load_rsa_public_key(modulus, public_exponent);
Performing signature verification can be done either one-shot or multi part.
One-shot:
#include "crypto/crypto.h"
auto key = {...};
bytes msg {...};
bytes sig {...};
auto sig_verified = crypto::verify_signature(
crypto::digest_type::SHA256,
key,
msg,
sig
);
For multi-part:
#include "crypto/crypto.h"
auto key = {...};
bytes msg {...};
bytes sig {...};
crypto::verify_ctx ctx(crypto::digest_type::SHA256, key);
ctx.update(msg);
bool verified = std::move(ctx).final(sig);
To get cryptographically secure random data, please use
crypto::generate_random. There are two functions you can use. One will
place random data into the provided buffer and another will return an allocated
buffer with random data. Both functions also have a flag to indicate if the caller
wants to use the 'private' DRBG. For more information on this please refer to
OpenSSL's documentation
on this.