#include <grpc/support/port_platform.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <grpc/event_engine/port.h>
#include <grpc/grpc.h>
Go to the source code of this file.
Classes | |
struct | gsec_aead_crypter |
struct | gsec_aead_crypter_vtable |
struct | iovec |
Typedefs | |
typedef struct gsec_aead_crypter | gsec_aead_crypter |
typedef struct gsec_aead_crypter_vtable | gsec_aead_crypter_vtable |
Functions | |
grpc_status_code | gsec_aead_crypter_decrypt (gsec_aead_crypter *crypter, const uint8_t *nonce, size_t nonce_length, const uint8_t *aad, size_t aad_length, const uint8_t *ciphertext_and_tag, size_t ciphertext_and_tag_length, uint8_t *plaintext, size_t plaintext_length, size_t *bytes_written, char **error_details) |
grpc_status_code | gsec_aead_crypter_decrypt_iovec (gsec_aead_crypter *crypter, const uint8_t *nonce, size_t nonce_length, const struct iovec *aad_vec, size_t aad_vec_length, const struct iovec *ciphertext_vec, size_t ciphertext_vec_length, struct iovec plaintext_vec, size_t *plaintext_bytes_written, char **error_details) |
void | gsec_aead_crypter_destroy (gsec_aead_crypter *crypter) |
grpc_status_code | gsec_aead_crypter_encrypt (gsec_aead_crypter *crypter, const uint8_t *nonce, size_t nonce_length, const uint8_t *aad, size_t aad_length, const uint8_t *plaintext, size_t plaintext_length, uint8_t *ciphertext_and_tag, size_t ciphertext_and_tag_length, size_t *bytes_written, char **error_details) |
grpc_status_code | gsec_aead_crypter_encrypt_iovec (gsec_aead_crypter *crypter, const uint8_t *nonce, size_t nonce_length, const struct iovec *aad_vec, size_t aad_vec_length, const struct iovec *plaintext_vec, size_t plaintext_vec_length, struct iovec ciphertext_vec, size_t *ciphertext_bytes_written, char **error_details) |
grpc_status_code | gsec_aead_crypter_key_length (const gsec_aead_crypter *crypter, size_t *key_length_to_return, char **error_details) |
grpc_status_code | gsec_aead_crypter_max_ciphertext_and_tag_length (const gsec_aead_crypter *crypter, size_t plaintext_length, size_t *max_ciphertext_and_tag_length_to_return, char **error_details) |
grpc_status_code | gsec_aead_crypter_max_plaintext_length (const gsec_aead_crypter *crypter, size_t ciphertext_and_tag_length, size_t *max_plaintext_length_to_return, char **error_details) |
grpc_status_code | gsec_aead_crypter_nonce_length (const gsec_aead_crypter *crypter, size_t *nonce_length_to_return, char **error_details) |
grpc_status_code | gsec_aead_crypter_tag_length (const gsec_aead_crypter *crypter, size_t *tag_length_to_return, char **error_details) |
grpc_status_code | gsec_aes_gcm_aead_crypter_create (const uint8_t *key, size_t key_length, size_t nonce_length, size_t tag_length, bool rekey, gsec_aead_crypter **crypter, char **error_details) |
Variables | |
const size_t | kAes128GcmKeyLength = 16 |
const size_t | kAes128GcmRekeyKeyLength = 44 |
const size_t | kAes256GcmKeyLength = 32 |
const size_t | kAesGcmNonceLength = 12 |
const size_t | kAesGcmTagLength = 16 |
typedef struct gsec_aead_crypter gsec_aead_crypter |
typedef struct gsec_aead_crypter_vtable gsec_aead_crypter_vtable |
The gsec_aead_crypter is an API for different AEAD implementations such as AES_GCM. It encapsulates all AEAD-related operations in the format of V-table that stores pointers to functions implementing those operations. It also provides helper functions to wrap each of those function pointers.
A typical usage of this object would be:
// Declare a gsec_aead_crypter object, and create and assign an instance // of specific AEAD implementation e.g., AES_GCM to it. We assume both // key and nonce contain cryptographically secure random bytes, and the key // can be derived from an upper-layer application. gsec_aead_crypter* crypter; char* error_in_creation; // User can populate the message with any 100 bytes data. uint8_t* message = gpr_malloc(100); grpc_status_code creation_status = gsec_aes_gcm_aead_crypter_create(key, kAes128GcmKeyLength, kAesGcmNonceLength, kAesGcmTagLength, &crypter, false, 0 &error_in_creation);
if (creation_status == GRPC_STATUS_OK) { // Allocate a correct amount of memory to hold a ciphertext. size_t clength = 0; gsec_aead_crypter_max_ciphertext_and_tag_length(crypter, 100, &clength, nullptr); uint8_t* ciphertext = gpr_malloc(clength);
// Perform encryption size_t num_encrypted_bytes = 0; char* error_in_encryption = nullptr; grpc_status_code status = gsec_aead_crypter_encrypt(crypter, nonce, kAesGcmNonceLength, nullptr, 0, message, 100, ciphertext, clength, &num_encrypted_bytes, &error_in_encryption); if (status == GRPC_STATUS_OK) { // Allocate a correct amount of memory to hold a plaintext. size_t plength = 0; gsec_aead_crypter_max_plaintext_length(crypter, num_encrypted_bytes, &plength, nullptr); uint8_t* plaintext = gpr_malloc(plength);
// Perform decryption. size_t num_decrypted_bytes = 0; char* error_in_decryption = nullptr; status = gsec_aead_crypter_decrypt(crypter, nonce, kAesGcmNonceLength, nullptr, 0, ciphertext, num_encrypted_bytes, plaintext, plength, &num_decrypted_bytes, &error_in_decryption); if (status != GRPC_STATUS_OK) { fprintf(stderr, "AEAD decrypt operation failed with error code:" "%d, message: %s\n", status, error_in_decryption); } ... gpr_free(plaintext); gpr_free(error_in_decryption); } else { fprintf(stderr, "AEAD encrypt operation failed with error code:" "%d, message: %s\n", status, error_in_encryption); } ... gpr_free(ciphertext); gpr_free(error_in_encryption); } else { fprintf(stderr, "Creation of AEAD crypter instance failed with error code:" "%d, message: %s\n", creation_status, error_in_creation); }
// Destruct AEAD crypter instance. if (creation_status == GRPC_STATUS_OK) { gsec_aead_crypter_destroy(crypter); } gpr_free(error_in_creation);
grpc_status_code gsec_aead_crypter_decrypt | ( | gsec_aead_crypter * | crypter, |
const uint8_t * | nonce, | ||
size_t | nonce_length, | ||
const uint8_t * | aad, | ||
size_t | aad_length, | ||
const uint8_t * | ciphertext_and_tag, | ||
size_t | ciphertext_and_tag_length, | ||
uint8_t * | plaintext, | ||
size_t | plaintext_length, | ||
size_t * | bytes_written, | ||
char ** | error_details | ||
) |
This method performs an AEAD decrypt operation.
On the success of decryption, the method returns GRPC_STATUS_OK. Otherwise, it returns an error status code along with its details specified in error_details (if error_details is not nullptr).
grpc_status_code gsec_aead_crypter_decrypt_iovec | ( | gsec_aead_crypter * | crypter, |
const uint8_t * | nonce, | ||
size_t | nonce_length, | ||
const struct iovec * | aad_vec, | ||
size_t | aad_vec_length, | ||
const struct iovec * | ciphertext_vec, | ||
size_t | ciphertext_vec_length, | ||
struct iovec | plaintext_vec, | ||
size_t * | plaintext_bytes_written, | ||
char ** | error_details | ||
) |
This method performs an AEAD decrypt operation.
On the success of decryption, the method returns GRPC_STATUS_OK. Otherwise, it returns an error status code along with its details specified in error_details (if error_details is not nullptr).
void gsec_aead_crypter_destroy | ( | gsec_aead_crypter * | crypter | ) |
grpc_status_code gsec_aead_crypter_encrypt | ( | gsec_aead_crypter * | crypter, |
const uint8_t * | nonce, | ||
size_t | nonce_length, | ||
const uint8_t * | aad, | ||
size_t | aad_length, | ||
const uint8_t * | plaintext, | ||
size_t | plaintext_length, | ||
uint8_t * | ciphertext_and_tag, | ||
size_t | ciphertext_and_tag_length, | ||
size_t * | bytes_written, | ||
char ** | error_details | ||
) |
This method performs an AEAD encrypt operation.
On the success of encryption, the method returns GRPC_STATUS_OK. Otherwise, it returns an error status code along with its details specified in error_details (if error_details is not nullptr).
grpc_status_code gsec_aead_crypter_encrypt_iovec | ( | gsec_aead_crypter * | crypter, |
const uint8_t * | nonce, | ||
size_t | nonce_length, | ||
const struct iovec * | aad_vec, | ||
size_t | aad_vec_length, | ||
const struct iovec * | plaintext_vec, | ||
size_t | plaintext_vec_length, | ||
struct iovec | ciphertext_vec, | ||
size_t * | ciphertext_bytes_written, | ||
char ** | error_details | ||
) |
This method performs an AEAD encrypt operation.
On the success of encryption, the method returns GRPC_STATUS_OK. Otherwise, it returns an error status code along with its details specified in error_details (if error_details is not nullptr).
grpc_status_code gsec_aead_crypter_key_length | ( | const gsec_aead_crypter * | crypter, |
size_t * | key_length_to_return, | ||
char ** | error_details | ||
) |
This method returns a valid size of key array used at the construction of AEAD crypter instance. It is also the size that should be passed to encrypt and decrypt methods executed on the instance.
On the success of execution, the method returns GRPC_STATUS_OK. Otherwise, it returns an error status code along with its details specified in error_details (if error_details is not nullptr).
grpc_status_code gsec_aead_crypter_max_ciphertext_and_tag_length | ( | const gsec_aead_crypter * | crypter, |
size_t | plaintext_length, | ||
size_t * | max_ciphertext_and_tag_length_to_return, | ||
char ** | error_details | ||
) |
This method computes the size of ciphertext+tag buffer that must be passed to gsec_aead_crypter_encrypt function to ensure correct encryption of a plaintext. The actual size of ciphertext+tag written to the buffer could be smaller.
On the success of execution, the method returns GRPC_STATUS_OK. Otherwise, it returns an error status code along with its details specified in error_details (if error_details is not nullptr).
grpc_status_code gsec_aead_crypter_max_plaintext_length | ( | const gsec_aead_crypter * | crypter, |
size_t | ciphertext_and_tag_length, | ||
size_t * | max_plaintext_length_to_return, | ||
char ** | error_details | ||
) |
This method computes the size of plaintext buffer that must be passed to gsec_aead_crypter_decrypt function to ensure correct decryption of a ciphertext. The actual size of plaintext written to the buffer could be smaller.
On the success of execution, the method returns GRPC_STATUS_OK. Otherwise, it returns an error status code along with its details specified in error_details (if error_details is not nullptr).
grpc_status_code gsec_aead_crypter_nonce_length | ( | const gsec_aead_crypter * | crypter, |
size_t * | nonce_length_to_return, | ||
char ** | error_details | ||
) |
This method returns a valid size of nonce array used at the construction of AEAD crypter instance. It is also the size that should be passed to encrypt and decrypt methods executed on the instance.
On the success of execution, the method returns GRPC_STATUS_OK. Otherwise, it returns an error status code along with its details specified in error_details (if error_details is not nullptr).
grpc_status_code gsec_aead_crypter_tag_length | ( | const gsec_aead_crypter * | crypter, |
size_t * | tag_length_to_return, | ||
char ** | error_details | ||
) |
This method returns a valid size of tag array used at the construction of AEAD crypter instance. It is also the size that should be passed to encrypt and decrypt methods executed on the instance.
On the success of execution, the method returns GRPC_STATUS_OK. Otherwise, it returns an error status code along with its details specified in error_details (if error_details is not nullptr).
grpc_status_code gsec_aes_gcm_aead_crypter_create | ( | const uint8_t * | key, |
size_t | key_length, | ||
size_t | nonce_length, | ||
size_t | tag_length, | ||
bool | rekey, | ||
gsec_aead_crypter ** | crypter, | ||
char ** | error_details | ||
) |
This method creates an AEAD crypter instance of AES-GCM encryption scheme which supports 16 and 32 bytes long keys, 12 and 16 bytes long nonces, and 16 bytes long tags. It should be noted that once the lengths of key, nonce, and tag are determined at construction time, they cannot be modified later.
On success of instance creation, it stores the address of instance at crypter. Otherwise, it returns an error status code together with its details specified in error_details.
Definition at line 633 of file aes_gcm.cc.
const size_t kAesGcmNonceLength = 12 |