alts_seal_privacy_integrity_crypter.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 
21 #include <grpc/support/alloc.h>
22 
26 
27 static void maybe_copy_error_msg(const char* src, char** dst) {
28  if (dst != nullptr && src != nullptr) {
29  *dst = static_cast<char*>(gpr_malloc(strlen(src) + 1));
30  memcpy(*dst, src, strlen(src) + 1);
31  }
32 }
33 
34 /* Perform input santity check for a seal operation. */
35 static grpc_status_code seal_check(alts_crypter* c, const unsigned char* data,
36  size_t data_allocated_size, size_t data_size,
37  size_t* output_size, char** error_details) {
38  /* Do common input sanity check. */
40  reinterpret_cast<const alts_record_protocol_crypter*>(c), data,
41  output_size, error_details);
42  if (status != GRPC_STATUS_OK) return status;
43  /* Do seal-specific check. */
44  size_t num_overhead_bytes =
45  alts_crypter_num_overhead_bytes(reinterpret_cast<const alts_crypter*>(c));
46  if (data_size == 0) {
47  const char error_msg[] = "data_size is zero.";
48  maybe_copy_error_msg(error_msg, error_details);
50  }
51  if (data_size + num_overhead_bytes > data_allocated_size) {
52  const char error_msg[] =
53  "data_allocated_size is smaller than sum of data_size and "
54  "num_overhead_bytes.";
55  maybe_copy_error_msg(error_msg, error_details);
57  }
58  return GRPC_STATUS_OK;
59 }
60 
62  alts_crypter* c, unsigned char* data, size_t data_allocated_size,
63  size_t data_size, size_t* output_size, char** error_details) {
64  grpc_status_code status = seal_check(c, data, data_allocated_size, data_size,
65  output_size, error_details);
66  if (status != GRPC_STATUS_OK) {
67  return status;
68  }
69  /* Do AEAD encryption. */
70  alts_record_protocol_crypter* rp_crypter =
71  reinterpret_cast<alts_record_protocol_crypter*>(c);
73  rp_crypter->crypter, alts_counter_get_counter(rp_crypter->ctr),
74  alts_counter_get_size(rp_crypter->ctr), nullptr /* aad */,
75  0 /* aad_length */, data, data_size, data, data_allocated_size,
76  output_size, error_details);
77  if (status != GRPC_STATUS_OK) {
78  return status;
79  }
80  /* Increment the crypter counter. */
81  return increment_counter(rp_crypter, error_details);
82 }
83 
84 static const alts_crypter_vtable vtable = {
87 
89  size_t overflow_size,
90  alts_crypter** crypter,
91  char** error_details) {
92  if (crypter == nullptr) {
93  const char error_msg[] = "crypter is nullptr.";
94  maybe_copy_error_msg(error_msg, error_details);
96  }
97  alts_record_protocol_crypter* rp_crypter =
98  alts_crypter_create_common(gc, !is_client, overflow_size, error_details);
99  if (rp_crypter == nullptr) {
101  }
102  rp_crypter->base.vtable = &vtable;
103  *crypter = &rp_crypter->base;
104  return GRPC_STATUS_OK;
105 }
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
alts_seal_crypter_create
grpc_status_code alts_seal_crypter_create(gsec_aead_crypter *gc, bool is_client, size_t overflow_size, alts_crypter **crypter, char **error_details)
Definition: alts_seal_privacy_integrity_crypter.cc:88
alts_counter_get_counter
unsigned char * alts_counter_get_counter(alts_counter *crypter_counter)
Definition: alts_counter.cc:106
alts_record_protocol_crypter_common.h
alts_counter.h
alts_seal_crypter_process_in_place
static grpc_status_code alts_seal_crypter_process_in_place(alts_crypter *c, unsigned char *data, size_t data_allocated_size, size_t data_size, size_t *output_size, char **error_details)
Definition: alts_seal_privacy_integrity_crypter.cc:61
alts_crypter_create_common
alts_record_protocol_crypter * alts_crypter_create_common(gsec_aead_crypter *crypter, bool is_client, size_t overflow_size, char **error_details)
Definition: alts_record_protocol_crypter_common.cc:90
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
alts_record_protocol_crypter
Definition: alts_record_protocol_crypter_common.h:38
alts_crypter_vtable
Definition: alts_crypter.h:124
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
status
absl::Status status
Definition: rls.cc:251
alts_crypter.h
alts_crypter_num_overhead_bytes
size_t alts_crypter_num_overhead_bytes(const alts_crypter *crypter)
Definition: alts_crypter.cc:50
GRPC_STATUS_INVALID_ARGUMENT
@ GRPC_STATUS_INVALID_ARGUMENT
Definition: include/grpc/impl/codegen/status.h:46
alts_counter_get_size
size_t alts_counter_get_size(alts_counter *crypter_counter)
Definition: alts_counter.cc:99
alts_record_protocol_crypter::crypter
gsec_aead_crypter * crypter
Definition: alts_record_protocol_crypter_common.h:40
increment_counter
grpc_status_code increment_counter(alts_record_protocol_crypter *rp_crypter, char **error_details)
Definition: alts_record_protocol_crypter_common.cc:48
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
alts_record_protocol_crypter_destruct
void alts_record_protocol_crypter_destruct(alts_crypter *c)
Definition: alts_record_protocol_crypter_common.cc:81
alts_record_protocol_crypter_num_overhead_bytes
size_t alts_record_protocol_crypter_num_overhead_bytes(const alts_crypter *c)
Definition: alts_record_protocol_crypter_common.cc:66
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
alts_crypter
Definition: alts_crypter.h:135
gc
void gc(uv_timer_t *handle)
Definition: libuv/docs/code/ref-timer/main.c:9
alts_record_protocol_crypter::base
alts_crypter base
Definition: alts_record_protocol_crypter_common.h:39
alloc.h
alts_crypter::vtable
const alts_crypter_vtable * vtable
Definition: alts_crypter.h:136
maybe_copy_error_msg
static void maybe_copy_error_msg(const char *src, char **dst)
Definition: alts_seal_privacy_integrity_crypter.cc:27
vtable
static const alts_crypter_vtable vtable
Definition: alts_seal_privacy_integrity_crypter.cc:84
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
alts_record_protocol_crypter::ctr
alts_counter * ctr
Definition: alts_record_protocol_crypter_common.h:41
GRPC_STATUS_FAILED_PRECONDITION
@ GRPC_STATUS_FAILED_PRECONDITION
Definition: include/grpc/impl/codegen/status.h:97
gsec_aead_crypter
Definition: gsec.h:178
seal_check
static grpc_status_code seal_check(alts_crypter *c, const unsigned char *data, size_t data_allocated_size, size_t data_size, size_t *output_size, char **error_details)
Definition: alts_seal_privacy_integrity_crypter.cc:35
port_platform.h
input_sanity_check
grpc_status_code input_sanity_check(const alts_record_protocol_crypter *rp_crypter, const unsigned char *data, size_t *output_size, char **error_details)
Definition: alts_record_protocol_crypter_common.cc:32


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:41