| 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 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/tsi/alts/crypt/gsec.h" |
| 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 |
|
| 38 |
grpc_status_code gsec_aead_crypter_encrypt( |
| 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); |
| 57 |
return GRPC_STATUS_INVALID_ARGUMENT; |
| 58 |
} |
| 59 |
|
| 60 |
grpc_status_code gsec_aead_crypter_encrypt_iovec( |
| 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); |
| 75 |
return GRPC_STATUS_INVALID_ARGUMENT; |
| 76 |
} |
| 77 |
|
| 78 |
grpc_status_code gsec_aead_crypter_decrypt( |
| 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); |
| 95 |
return GRPC_STATUS_INVALID_ARGUMENT; |
| 96 |
} |
| 97 |
|
| 98 |
grpc_status_code gsec_aead_crypter_decrypt_iovec( |
| 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); |
| 113 |
return GRPC_STATUS_INVALID_ARGUMENT; |
| 114 |
} |
| 115 |
|
| 116 |
grpc_status_code gsec_aead_crypter_max_ciphertext_and_tag_length( |
| 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); |
| 127 |
return GRPC_STATUS_INVALID_ARGUMENT; |
| 128 |
} |
| 129 |
|
| 130 |
grpc_status_code gsec_aead_crypter_max_plaintext_length( |
| 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); |
| 141 |
return GRPC_STATUS_INVALID_ARGUMENT; |
| 142 |
} |
| 143 |
|
| 144 |
grpc_status_code gsec_aead_crypter_nonce_length( |
| 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); |
| 154 |
return GRPC_STATUS_INVALID_ARGUMENT; |
| 155 |
} |
| 156 |
|
| 157 |
grpc_status_code gsec_aead_crypter_key_length(const gsec_aead_crypter* crypter, |
| 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); |
| 167 |
return GRPC_STATUS_INVALID_ARGUMENT; |
| 168 |
} |
| 169 |
|
| 170 |
grpc_status_code gsec_aead_crypter_tag_length(const gsec_aead_crypter* crypter, |
| 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); |
| 180 |
return GRPC_STATUS_INVALID_ARGUMENT; |
| 181 |
} |
| 182 |
|
| 183 |
void gsec_aead_crypter_destroy(gsec_aead_crypter* crypter) { |
| 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 |
} |