Go to the documentation of this file.
59 char* error_msg =
nullptr;
64 error_msg[
mem->length] =
'\0';
71 if (error_details ==
nullptr) {
75 if (
error == 0 && error_msg !=
nullptr) {
76 *error_details =
static_cast<char*
>(
gpr_malloc(strlen(error_msg) + 1));
77 memcpy(*error_details, error_msg, strlen(error_msg) + 1);
81 if (openssl_errors !=
nullptr && error_msg !=
nullptr) {
82 size_t len = strlen(error_msg) + strlen(openssl_errors) + 2;
84 snprintf(*error_details,
len + 1,
"%s, %s", error_msg, openssl_errors);
91 size_t* max_ciphertext_and_tag_length,
char** error_details) {
92 if (max_ciphertext_and_tag_length ==
nullptr) {
100 *max_ciphertext_and_tag_length =
101 plaintext_length + aes_gcm_crypter->
tag_length;
107 size_t* max_plaintext_length,
char** error_details) {
108 if (max_plaintext_length ==
nullptr) {
115 if (ciphertext_and_tag_length < aes_gcm_crypter->tag_length) {
116 *max_plaintext_length = 0;
118 "ciphertext_and_tag_length is smaller than tag_length.", error_details);
121 *max_plaintext_length =
122 ciphertext_and_tag_length - aes_gcm_crypter->
tag_length;
128 char** error_details) {
129 if (nonce_length ==
nullptr) {
142 char** error_details) {
143 if (key_length ==
nullptr) {
156 char** error_details) {
157 if (tag_length ==
nullptr) {
172 memcpy(&mask1, mask,
sizeof(mask1));
173 memcpy(&mask2, mask +
sizeof(mask1),
sizeof(mask2));
176 memcpy(&nonce1, nonce,
sizeof(nonce1));
177 memcpy(&nonce2, nonce +
sizeof(nonce1),
sizeof(nonce2));
181 memcpy(
dst +
sizeof(nonce1), &nonce2,
sizeof(nonce2));
188 unsigned char ctr = 1;
189 #if OPENSSL_VERSION_NUMBER < 0x10100000L
201 if (hmac ==
nullptr) {
218 char** error_details) {
247 const struct iovec* aad_vec,
size_t aad_vec_length,
248 const struct iovec* plaintext_vec,
size_t plaintext_vec_length,
249 struct iovec ciphertext_vec,
size_t* ciphertext_bytes_written,
250 char** error_details) {
254 if (nonce ==
nullptr) {
262 if (aad_vec_length > 0 && aad_vec ==
nullptr) {
267 if (plaintext_vec_length > 0 && plaintext_vec ==
nullptr) {
269 "Non-zero plaintext_vec_length but plaintext_vec is nullptr.",
273 if (ciphertext_bytes_written ==
nullptr) {
277 *ciphertext_bytes_written = 0;
284 const uint8_t* nonce_aead = nonce;
289 nonce_aead = nonce_masked;
299 for (
i = 0;
i < aad_vec_length;
i++) {
301 size_t aad_length = aad_vec[
i].
iov_len;
302 if (aad_length == 0) {
305 size_t aad_bytes_read = 0;
306 if (aad ==
nullptr) {
311 reinterpret_cast<int*
>(&aad_bytes_read), aad,
312 static_cast<int>(aad_length)) ||
313 aad_bytes_read != aad_length) {
320 size_t ciphertext_length = ciphertext_vec.
iov_len;
326 for (
i = 0;
i < plaintext_vec_length;
i++) {
328 size_t plaintext_length = plaintext_vec[
i].
iov_len;
330 if (plaintext_length == 0) {
336 if (ciphertext_length < plaintext_length) {
338 "ciphertext is not large enough to hold the result.", error_details);
342 int bytes_to_write =
static_cast<int>(plaintext_length);
355 int bytes_written_temp = 0;
357 &bytes_written_temp)) {
361 if (bytes_written_temp != 0) {
379 *ciphertext_bytes_written = ciphertext_vec.
iov_len - ciphertext_length;
385 const struct iovec* aad_vec,
size_t aad_vec_length,
386 const struct iovec* ciphertext_vec,
size_t ciphertext_vec_length,
387 struct iovec plaintext_vec,
size_t* plaintext_bytes_written,
388 char** error_details) {
392 if (nonce ==
nullptr) {
400 if (aad_vec_length > 0 && aad_vec ==
nullptr) {
405 if (ciphertext_vec_length > 0 && ciphertext_vec ==
nullptr) {
407 "Non-zero plaintext_vec_length but plaintext_vec is nullptr.",
413 size_t total_ciphertext_length = 0;
415 for (
i = 0;
i < ciphertext_vec_length;
i++) {
416 total_ciphertext_length += ciphertext_vec[
i].
iov_len;
423 if (plaintext_bytes_written ==
nullptr) {
427 *plaintext_bytes_written = 0;
435 const uint8_t* nonce_aead = nonce;
440 nonce_aead = nonce_masked;
449 for (
i = 0;
i < aad_vec_length;
i++) {
451 size_t aad_length = aad_vec[
i].
iov_len;
452 if (aad_length == 0) {
455 size_t aad_bytes_read = 0;
456 if (aad ==
nullptr) {
461 reinterpret_cast<int*
>(&aad_bytes_read), aad,
462 static_cast<int>(aad_length)) ||
463 aad_bytes_read != aad_length) {
471 size_t plaintext_length = plaintext_vec.
iov_len;
472 if (plaintext_length > 0 &&
plaintext ==
nullptr) {
474 "plaintext is nullptr, but plaintext_length is positive.",
479 size_t ciphertext_length = 0;
484 ciphertext_length = ciphertext_vec[
i].
iov_len;
486 if (ciphertext_length == 0) {
494 size_t bytes_to_write = ciphertext_length;
499 if (plaintext_length < bytes_to_write) {
501 "Not enough plaintext buffer to hold encrypted ciphertext.",
507 static_cast<int>(bytes_to_write))) {
525 "Not enough plaintext buffer to hold encrypted ciphertext.",
532 if (ciphertext_length > 0) {
534 tag_tmp += ciphertext_length;
535 total_ciphertext_length -= ciphertext_length;
537 for (;
i < ciphertext_vec_length;
i++) {
539 ciphertext_length = ciphertext_vec[
i].
iov_len;
541 if (ciphertext_length == 0) {
549 tag_tmp += ciphertext_length;
550 total_ciphertext_length -= ciphertext_length;
558 int bytes_written_temp = 0;
560 &bytes_written_temp)) {
562 if (plaintext_vec.
iov_base !=
nullptr) {
567 if (bytes_written_temp != 0) {
573 *plaintext_bytes_written = plaintext_vec.
iov_len - plaintext_length;
599 bool is_rekey = aes_gcm_crypter->
rekey_data !=
nullptr;
608 const uint8_t* aead_key = aes_gcm_crypter->
key;
617 aead_key = aead_key_rekey;
636 size_t tag_length,
bool rekey,
638 char** error_details) {
639 if (
key ==
nullptr) {
643 if (crypter ==
nullptr) {
654 "Invalid key and/or nonce and/or tag length are provided at AEAD "
655 "crypter instance construction time.",
677 aes_gcm_crypter->
key =
688 *crypter = &aes_gcm_crypter->
crypter;
static grpc_status_code gsec_aes_gcm_aead_crypter_nonce_length(const gsec_aead_crypter *crypter, size_t *nonce_length, char **error_details)
return memset(p, 0, total)
constexpr size_t kKdfCounterOffset
static grpc_status_code aes_gcm_derive_aead_key(uint8_t *dst, const uint8_t *kdf_key, const uint8_t *kdf_counter)
GPRAPI void gpr_free(void *ptr)
static grpc_status_code gsec_aes_gcm_aead_crypter_max_ciphertext_and_tag_length(const gsec_aead_crypter *crypter, size_t plaintext_length, size_t *max_ciphertext_and_tag_length, char **error_details)
const size_t kAes256GcmKeyLength
static grpc_status_code aes_gcm_rekey_if_required(gsec_aes_gcm_aead_crypter *aes_gcm_crypter, const uint8_t *nonce, char **error_details)
GPRAPI void * gpr_malloc(size_t size)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_128_gcm(void)
#define EVP_EncryptUpdate
constexpr size_t kKdfCounterLen
#define EVP_CIPHER_CTX_new
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)
@ GRPC_STATUS_INVALID_ARGUMENT
static void gsec_aes_gcm_aead_crypter_destroy(gsec_aead_crypter *crypter)
const OPENSSL_EXPORT EVP_MD * EVP_sha256(void)
#define EVP_CIPHER_CTX_ctrl
#define EVP_DecryptInit_ex
#define EVP_CTRL_GCM_SET_TAG
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define EVP_CTRL_GCM_GET_TAG
const size_t kAesGcmNonceLength
constexpr size_t kKdfKeyLen
static void * tag(intptr_t t)
const size_t kAes128GcmRekeyKeyLength
static const gsec_aead_crypter_vtable vtable
constexpr size_t kRekeyAeadKeyLen
uint8_t kdf_counter[kKdfCounterLen]
unsigned __int64 uint64_t
uint8_t nonce_mask[kAesGcmNonceLength]
#define EVP_EncryptInit_ex
static grpc_status_code gsec_aes_gcm_aead_crypter_key_length(const gsec_aead_crypter *crypter, size_t *key_length, char **error_details)
static char * aes_gcm_get_openssl_errors()
static grpc_status_code aes_gcm_new_evp_cipher_ctx(gsec_aes_gcm_aead_crypter *aes_gcm_crypter, char **error_details)
static grpc_status_code gsec_aes_gcm_aead_crypter_max_plaintext_length(const gsec_aead_crypter *crypter, size_t ciphertext_and_tag_length, size_t *max_plaintext_length, char **error_details)
#define EVP_EncryptFinal_ex
#define EVP_DecryptUpdate
gsec_aead_crypter crypter
const struct gsec_aead_crypter_vtable * vtable
gsec_aes_gcm_aead_rekey_data * rekey_data
#define EVP_CTRL_GCM_SET_IVLEN
#define EVP_DecryptFinal_ex
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_256_gcm(void)
@ GRPC_STATUS_FAILED_PRECONDITION
static grpc_status_code gsec_aes_gcm_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)
static size_t bytes_written
static grpc_status_code gsec_aes_gcm_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)
#define EVP_CIPHER_CTX_free
static grpc_status_code gsec_aes_gcm_aead_crypter_tag_length(const gsec_aead_crypter *crypter, size_t *tag_length, char **error_details)
static void aes_gcm_format_errors(const char *error_msg, char **error_details)
static void aes_gcm_mask_nonce(uint8_t *dst, const uint8_t *nonce, const uint8_t *mask)
const size_t kAes128GcmKeyLength
const size_t kAesGcmTagLength
grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:40