gsec.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include <grpc/support/alloc.h>
27 
28 static const char vtable_error_msg[] =
29  "crypter or crypter->vtable has not been initialized properly";
30 
31 static void maybe_copy_error_msg(const char* src, char** dst) {
32  if (dst != nullptr && src != nullptr) {
33  *dst = static_cast<char*>(gpr_malloc(strlen(src) + 1));
34  memcpy(*dst, src, strlen(src) + 1);
35  }
36 }
37 
39  gsec_aead_crypter* crypter, const uint8_t* nonce, size_t nonce_length,
40  const uint8_t* aad, size_t aad_length, const uint8_t* plaintext,
41  size_t plaintext_length, uint8_t* ciphertext_and_tag,
42  size_t ciphertext_and_tag_length, size_t* bytes_written,
43  char** error_details) {
44  if (crypter != nullptr && crypter->vtable != nullptr &&
45  crypter->vtable->encrypt_iovec != nullptr) {
46  struct iovec aad_vec = {const_cast<uint8_t*>(aad), aad_length};
47  struct iovec plaintext_vec = {const_cast<uint8_t*>(plaintext),
48  plaintext_length};
49  struct iovec ciphertext_vec = {ciphertext_and_tag,
50  ciphertext_and_tag_length};
51  return crypter->vtable->encrypt_iovec(
52  crypter, nonce, nonce_length, &aad_vec, 1, &plaintext_vec, 1,
53  ciphertext_vec, bytes_written, error_details);
54  }
55  /* An error occurred. */
56  maybe_copy_error_msg(vtable_error_msg, error_details);
58 }
59 
61  gsec_aead_crypter* crypter, const uint8_t* nonce, size_t nonce_length,
62  const struct iovec* aad_vec, size_t aad_vec_length,
63  const struct iovec* plaintext_vec, size_t plaintext_vec_length,
64  struct iovec ciphertext_vec, size_t* ciphertext_bytes_written,
65  char** error_details) {
66  if (crypter != nullptr && crypter->vtable != nullptr &&
67  crypter->vtable->encrypt_iovec != nullptr) {
68  return crypter->vtable->encrypt_iovec(
69  crypter, nonce, nonce_length, aad_vec, aad_vec_length, plaintext_vec,
70  plaintext_vec_length, ciphertext_vec, ciphertext_bytes_written,
71  error_details);
72  }
73  /* An error occurred. */
74  maybe_copy_error_msg(vtable_error_msg, error_details);
76 }
77 
79  gsec_aead_crypter* crypter, const uint8_t* nonce, size_t nonce_length,
80  const uint8_t* aad, size_t aad_length, const uint8_t* ciphertext_and_tag,
81  size_t ciphertext_and_tag_length, uint8_t* plaintext,
82  size_t plaintext_length, size_t* bytes_written, char** error_details) {
83  if (crypter != nullptr && crypter->vtable != nullptr &&
84  crypter->vtable->decrypt_iovec != nullptr) {
85  struct iovec aad_vec = {const_cast<uint8_t*>(aad), aad_length};
86  struct iovec ciphertext_vec = {const_cast<uint8_t*>(ciphertext_and_tag),
87  ciphertext_and_tag_length};
88  struct iovec plaintext_vec = {plaintext, plaintext_length};
89  return crypter->vtable->decrypt_iovec(
90  crypter, nonce, nonce_length, &aad_vec, 1, &ciphertext_vec, 1,
91  plaintext_vec, bytes_written, error_details);
92  }
93  /* An error occurred. */
94  maybe_copy_error_msg(vtable_error_msg, error_details);
96 }
97 
99  gsec_aead_crypter* crypter, const uint8_t* nonce, size_t nonce_length,
100  const struct iovec* aad_vec, size_t aad_vec_length,
101  const struct iovec* ciphertext_vec, size_t ciphertext_vec_length,
102  struct iovec plaintext_vec, size_t* plaintext_bytes_written,
103  char** error_details) {
104  if (crypter != nullptr && crypter->vtable != nullptr &&
105  crypter->vtable->encrypt_iovec != nullptr) {
106  return crypter->vtable->decrypt_iovec(
107  crypter, nonce, nonce_length, aad_vec, aad_vec_length, ciphertext_vec,
108  ciphertext_vec_length, plaintext_vec, plaintext_bytes_written,
109  error_details);
110  }
111  /* An error occurred. */
112  maybe_copy_error_msg(vtable_error_msg, error_details);
114 }
115 
117  const gsec_aead_crypter* crypter, size_t plaintext_length,
118  size_t* max_ciphertext_and_tag_length_to_return, char** error_details) {
119  if (crypter != nullptr && crypter->vtable != nullptr &&
120  crypter->vtable->max_ciphertext_and_tag_length != nullptr) {
121  return crypter->vtable->max_ciphertext_and_tag_length(
122  crypter, plaintext_length, max_ciphertext_and_tag_length_to_return,
123  error_details);
124  }
125  /* An error occurred. */
126  maybe_copy_error_msg(vtable_error_msg, error_details);
128 }
129 
131  const gsec_aead_crypter* crypter, size_t ciphertext_and_tag_length,
132  size_t* max_plaintext_length_to_return, char** error_details) {
133  if (crypter != nullptr && crypter->vtable != nullptr &&
134  crypter->vtable->max_plaintext_length != nullptr) {
135  return crypter->vtable->max_plaintext_length(
136  crypter, ciphertext_and_tag_length, max_plaintext_length_to_return,
137  error_details);
138  }
139  /* An error occurred. */
140  maybe_copy_error_msg(vtable_error_msg, error_details);
142 }
143 
145  const gsec_aead_crypter* crypter, size_t* nonce_length_to_return,
146  char** error_details) {
147  if (crypter != nullptr && crypter->vtable != nullptr &&
148  crypter->vtable->nonce_length != nullptr) {
149  return crypter->vtable->nonce_length(crypter, nonce_length_to_return,
150  error_details);
151  }
152  /* An error occurred. */
153  maybe_copy_error_msg(vtable_error_msg, error_details);
155 }
156 
158  size_t* key_length_to_return,
159  char** error_details) {
160  if (crypter != nullptr && crypter->vtable != nullptr &&
161  crypter->vtable->key_length != nullptr) {
162  return crypter->vtable->key_length(crypter, key_length_to_return,
163  error_details);
164  }
165  /* An error occurred */
166  maybe_copy_error_msg(vtable_error_msg, error_details);
168 }
169 
171  size_t* tag_length_to_return,
172  char** error_details) {
173  if (crypter != nullptr && crypter->vtable != nullptr &&
174  crypter->vtable->tag_length != nullptr) {
175  return crypter->vtable->tag_length(crypter, tag_length_to_return,
176  error_details);
177  }
178  /* An error occurred. */
179  maybe_copy_error_msg(vtable_error_msg, error_details);
181 }
182 
184  if (crypter != nullptr) {
185  if (crypter->vtable != nullptr && crypter->vtable->destruct != nullptr) {
186  crypter->vtable->destruct(crypter);
187  }
188  gpr_free(crypter);
189  }
190 }
gsec_aead_crypter_vtable::decrypt_iovec
grpc_status_code(* 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)
Definition: gsec.h:153
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
gsec_aead_crypter_vtable::max_ciphertext_and_tag_length
grpc_status_code(* 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)
Definition: gsec.h:159
gsec_aead_crypter_vtable::tag_length
grpc_status_code(* tag_length)(const gsec_aead_crypter *crypter, size_t *tag_length_to_return, char **error_details)
Definition: gsec.h:171
gsec_aead_crypter_vtable::max_plaintext_length
grpc_status_code(* max_plaintext_length)(const gsec_aead_crypter *crypter, size_t ciphertext_and_tag_length, size_t *max_plaintext_length_to_return, char **error_details)
Definition: gsec.h:162
gsec_aead_crypter_vtable::destruct
void(* destruct)(gsec_aead_crypter *crypter)
Definition: gsec.h:174
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gsec_aead_crypter_vtable::nonce_length
grpc_status_code(* nonce_length)(const gsec_aead_crypter *crypter, size_t *nonce_length_to_return, char **error_details)
Definition: gsec.h:165
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
gsec_aead_crypter_key_length
grpc_status_code gsec_aead_crypter_key_length(const gsec_aead_crypter *crypter, size_t *key_length_to_return, char **error_details)
Definition: gsec.cc:157
gsec_aead_crypter_decrypt_iovec
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)
Definition: gsec.cc:98
GRPC_STATUS_INVALID_ARGUMENT
@ GRPC_STATUS_INVALID_ARGUMENT
Definition: include/grpc/impl/codegen/status.h:46
gsec_aead_crypter_tag_length
grpc_status_code gsec_aead_crypter_tag_length(const gsec_aead_crypter *crypter, size_t *tag_length_to_return, char **error_details)
Definition: gsec.cc:170
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
vtable_error_msg
static const char vtable_error_msg[]
Definition: gsec.cc:28
gsec.h
gsec_aead_crypter_vtable::key_length
grpc_status_code(* key_length)(const gsec_aead_crypter *crypter, size_t *key_length_to_return, char **error_details)
Definition: gsec.h:168
gsec_aead_crypter_max_ciphertext_and_tag_length
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)
Definition: gsec.cc:116
maybe_copy_error_msg
static void maybe_copy_error_msg(const char *src, char **dst)
Definition: gsec.cc:31
iovec
Definition: gsec.h:33
gsec_aead_crypter_nonce_length
grpc_status_code gsec_aead_crypter_nonce_length(const gsec_aead_crypter *crypter, size_t *nonce_length_to_return, char **error_details)
Definition: gsec.cc:144
gsec_aead_crypter::vtable
const struct gsec_aead_crypter_vtable * vtable
Definition: gsec.h:179
alloc.h
gsec_aead_crypter_encrypt
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)
Definition: gsec.cc:38
gsec_aead_crypter
Definition: gsec.h:178
plaintext
const char * plaintext
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil_unittest.cc:85
bytes_written
static size_t bytes_written
Definition: test-ipc-heavy-traffic-deadlock-bug.c:46
gsec_aead_crypter_destroy
void gsec_aead_crypter_destroy(gsec_aead_crypter *crypter)
Definition: gsec.cc:183
gsec_aead_crypter_max_plaintext_length
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)
Definition: gsec.cc:130
gsec_aead_crypter_vtable::encrypt_iovec
grpc_status_code(* 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)
Definition: gsec.h:147
gsec_aead_crypter_decrypt
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)
Definition: gsec.cc:78
gsec_aead_crypter_encrypt_iovec
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)
Definition: gsec.cc:60
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:48