| 1 | #cxx . . $ `<file>/mlspp/lib/hpke/src/aead_cipher.cpp`/include "aead_cipher.h"cxx . . $ `<file>/mlspp/lib/hpke/src/aead_cipher.h`/ |
| 2 | #include "openssl_common.h"cxx . . $ `<file>/mlspp/lib/hpke/src/openssl_common.h`/ |
| 3 | |
| 4 | #include <openssl/evp.h> |
| 5 | |
| 6 | #if WITH_BORINGSSL |
| 7 | #include <openssl/aead.h> |
| 8 | #endif |
| 9 | |
| 10 | namespace mlsppcxx . . $ mlspp/::hpkecxx . . $ mlspp/hpke/ { |
| 11 | |
| 12 | /// |
| 13 | /// ExportOnlyCipher |
| 14 | /// |
| 15 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 16 | ExportOnlyCiphercxx . . $ mlspp/hpke/ExportOnlyCipher#::sealcxx . . $ mlspp/hpke/ExportOnlyCipher#seal(a2160eb270d8417e).(const bytescxx . . $ mlspp/bytes_ns/bytes#& /* key */, |
| 17 | const bytescxx . . $ mlspp/bytes_ns/bytes#& /* nonce */, |
| 18 | const bytescxx . . $ mlspp/bytes_ns/bytes#& /* aad */, |
| 19 | const bytescxx . . $ mlspp/bytes_ns/bytes#& /* pt */) const |
| 20 | { |
| 21 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("seal() on export-only context"); |
| 22 | } |
| 23 | |
| 24 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#> |
| 25 | ExportOnlyCiphercxx . . $ mlspp/hpke/ExportOnlyCipher#::opencxx . . $ mlspp/hpke/ExportOnlyCipher#open(f7542dab5ac51900).(const bytescxx . . $ mlspp/bytes_ns/bytes#& /* key */, |
| 26 | const bytescxx . . $ mlspp/bytes_ns/bytes#& /* nonce */, |
| 27 | const bytescxx . . $ mlspp/bytes_ns/bytes#& /* aad */, |
| 28 | const bytescxx . . $ mlspp/bytes_ns/bytes#& /* ct */) const |
| 29 | { |
| 30 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("open() on export-only context"); |
| 31 | } |
| 32 | |
| 33 | ExportOnlyCiphercxx . . $ mlspp/hpke/ExportOnlyCipher#::ExportOnlyCiphercxx . . $ mlspp/hpke/ExportOnlyCipher#ExportOnlyCipher(49f6e7a06ebc5aa8).() |
| 34 | : AEADcxx . . $ mlspp/hpke/AEAD#(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::export_onlycxx . . $ mlspp/hpke/AEAD#ID#export_only., 0, 0) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | /// |
| 39 | /// AEADCipher |
| 40 | /// |
| 41 | AEADCiphercxx . . $ mlspp/hpke/AEADCipher# |
| 42 | make_aeadcxx . . $ mlspp/hpke/make_aead(127a9c1e385b6779).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID# cipher_in) |
| 43 | { |
| 44 | return {cxx . . $ mlspp/hpke/AEADCipher#AEADCipher(68316fa464f72640). cipher_in }; |
| 45 | } |
| 46 | |
| 47 | template<> |
| 48 | const AEADCiphercxx . . $ mlspp/hpke/AEADCipher#& |
| 49 | AEADCiphercxx . . $ mlspp/hpke/AEADCipher#::getcxx . . $ mlspp/hpke/AEADCipher#get(857c9916fe512820).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.>() |
| 50 | { |
| 51 | static const auto instance = make_aeadcxx . . $ mlspp/hpke/make_aead(127a9c1e385b6779).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.); |
| 52 | return instance; |
| 53 | } |
| 54 | |
| 55 | template<> |
| 56 | const AEADCiphercxx . . $ mlspp/hpke/AEADCipher#& |
| 57 | AEADCiphercxx . . $ mlspp/hpke/AEADCipher#::getcxx . . $ mlspp/hpke/AEADCipher#get(857c9916fe512820).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.>() |
| 58 | { |
| 59 | static const auto instance = make_aeadcxx . . $ mlspp/hpke/make_aead(127a9c1e385b6779).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.); |
| 60 | return instance; |
| 61 | } |
| 62 | |
| 63 | template<> |
| 64 | const AEADCiphercxx . . $ mlspp/hpke/AEADCipher#& |
| 65 | AEADCiphercxx . . $ mlspp/hpke/AEADCipher#::getcxx . . $ mlspp/hpke/AEADCipher#get(857c9916fe512820).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.>() |
| 66 | { |
| 67 | static const auto instance = make_aeadcxx . . $ mlspp/hpke/make_aead(127a9c1e385b6779).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.); |
| 68 | return instance; |
| 69 | } |
| 70 | |
| 71 | static size_t |
| 72 | cipher_key_sizecxx . . $ mlspp/hpke/cipher_key_size(eb60e4a5b054dd4e).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID# cipher) |
| 73 | { |
| 74 | switch (cipher) { |
| 75 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.: |
| 76 | return 16; |
| 77 | |
| 78 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.: |
| 79 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.: |
| 80 | return 32; |
| 81 | |
| 82 | default: |
| 83 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static size_t |
| 88 | cipher_nonce_sizecxx . . $ mlspp/hpke/cipher_nonce_size(eb60e4a5b054dd4e).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID# cipher) |
| 89 | { |
| 90 | switch (cipher) { |
| 91 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.: |
| 92 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.: |
| 93 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.: |
| 94 | return 12; |
| 95 | |
| 96 | default: |
| 97 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static size_t |
| 102 | cipher_tag_sizecxx . . $ mlspp/hpke/cipher_tag_size(eb60e4a5b054dd4e).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID# cipher) |
| 103 | { |
| 104 | switch (cipher) { |
| 105 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.: |
| 106 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.: |
| 107 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.: |
| 108 | return 16; |
| 109 | |
| 110 | default: |
| 111 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | #if WITH_BORINGSSL |
| 116 | static const EVP_AEAD* |
| 117 | boringssl_cipher(AEAD::ID cipher) |
| 118 | { |
| 119 | switch (cipher) { |
| 120 | case AEAD::ID::AES_128_GCM: |
| 121 | return EVP_aead_aes_128_gcm(); |
| 122 | |
| 123 | case AEAD::ID::AES_256_GCM: |
| 124 | return EVP_aead_aes_256_gcm(); |
| 125 | |
| 126 | case AEAD::ID::CHACHA20_POLY1305: |
| 127 | return EVP_aead_chacha20_poly1305(); |
| 128 | |
| 129 | default: |
| 130 | throw std::runtime_error("Unsupported algorithm"); |
| 131 | } |
| 132 | } |
| 133 | #else |
| 134 | static const EVP_CIPHER* |
| 135 | openssl_ciphercxx . . $ mlspp/hpke/openssl_cipher(eeaa53c2a40a6d14).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID# cipher) |
| 136 | { |
| 137 | switch (cipher) { |
| 138 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.: |
| 139 | return EVP_aes_128_gcm(); |
| 140 | |
| 141 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.: |
| 142 | return EVP_aes_256_gcm(); |
| 143 | |
| 144 | case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.: |
| 145 | return EVP_chacha20_poly1305(); |
| 146 | |
| 147 | default: |
| 148 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 149 | } |
| 150 | } |
| 151 | #endif // WITH_BORINGSSL |
| 152 | |
| 153 | AEADCiphercxx . . $ mlspp/hpke/AEADCipher#::AEADCiphercxx . . $ mlspp/hpke/AEADCipher#AEADCipher(68316fa464f72640).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID# id_in) |
| 154 | : AEADcxx . . $ mlspp/hpke/AEAD#(id_in, cipher_key_sizecxx . . $ mlspp/hpke/cipher_key_size(eb60e4a5b054dd4e).(id_in), cipher_nonce_sizecxx . . $ mlspp/hpke/cipher_nonce_size(eb60e4a5b054dd4e).(id_in)) |
| 155 | , tag_sizecxx . . $ mlspp/hpke/AEADCipher#tag_size.(cipher_tag_sizecxx . . $ mlspp/hpke/cipher_tag_size(eb60e4a5b054dd4e).(idcxx . . $ mlspp/hpke/AEAD#id.)) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 160 | AEADCiphercxx . . $ mlspp/hpke/AEADCipher#::sealcxx . . $ mlspp/hpke/AEADCipher#seal(a2160eb270d8417e).(const bytescxx . . $ mlspp/bytes_ns/bytes#& key, |
| 161 | const bytescxx . . $ mlspp/bytes_ns/bytes#& nonce, |
| 162 | const bytescxx . . $ mlspp/bytes_ns/bytes#& aad, |
| 163 | const bytescxx . . $ mlspp/bytes_ns/bytes#& pt) const |
| 164 | { |
| 165 | #if WITH_BORINGSSL |
| 166 | auto ctx = make_typed_unique( |
| 167 | EVP_AEAD_CTX_new(boringssl_cipher(id), key.data(), key.size(), tag_size)); |
| 168 | if (ctx == nullptr) { |
| 169 | throw openssl_error(); |
| 170 | } |
| 171 | |
| 172 | auto ct = bytes(pt.size() + tag_size); |
| 173 | auto out_len = ct.size(); |
| 174 | if (1 != EVP_AEAD_CTX_seal(ctx.get(), |
| 175 | ct.data(), |
| 176 | &out_len, |
| 177 | ct.size(), |
| 178 | nonce.data(), |
| 179 | nonce.size(), |
| 180 | pt.data(), |
| 181 | pt.size(), |
| 182 | aad.data(), |
| 183 | aad.size())) { |
| 184 | throw openssl_error(); |
| 185 | } |
| 186 | |
| 187 | return ct; |
| 188 | #else |
| 189 | auto ctx = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_CIPHER_CTX_new()); |
| 190 | if (ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 191 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 192 | } |
| 193 | |
| 194 | const auto* cipher = openssl_ciphercxx . . $ mlspp/hpke/openssl_cipher(eeaa53c2a40a6d14).(idcxx . . $ mlspp/hpke/AEAD#id.); |
| 195 | if (1 != EVP_EncryptInit(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), cipher, key.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), nonce.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).())) { |
| 196 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 197 | } |
| 198 | |
| 199 | int outlen = 0; |
| 200 | if (!aad.emptycxx . . $ mlspp/bytes_ns/bytes#empty(50ce9a9e25b4a850).()) { |
| 201 | if (1 != EVP_EncryptUpdate(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 202 | nullptr, |
| 203 | &outlen, |
| 204 | aad.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), |
| 205 | static_cast<int>(aad.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()))) { |
| 206 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | bytescxx . . $ mlspp/bytes_ns/bytes# ctcxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(pt.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()); |
| 211 | if (1 != EVP_EncryptUpdate(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 212 | ct.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), |
| 213 | &outlen, |
| 214 | pt.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), |
| 215 | static_cast<int>(pt.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()))) { |
| 216 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 217 | } |
| 218 | |
| 219 | // Providing nullptr as an argument is safe here because this |
| 220 | // function never writes with GCM; it only computes the tag |
| 221 | if (1 != EVP_EncryptFinal(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr, &outlen)) { |
| 222 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 223 | } |
| 224 | |
| 225 | bytescxx . . $ mlspp/bytes_ns/bytes# tagcxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(tag_sizecxx . . $ mlspp/hpke/AEADCipher#tag_size.); |
| 226 | if (1 != EVP_CIPHER_CTX_ctrl(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 227 | EVP_CTRL_GCM_GET_TAGcxx . . $ `<external>/2497497914425334111/evp.h:393:9`!, |
| 228 | static_cast<int>(tag_sizecxx . . $ mlspp/hpke/AEADCipher#tag_size.), |
| 229 | tag.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).())) { |
| 230 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 231 | } |
| 232 | |
| 233 | ct +=cxx . . $ mlspp/bytes_ns/bytes#`operator+=`(89adb9997e4a49c). tag; |
| 234 | return ctcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 235 | #endif // WITH_BORINGSSL |
| 236 | } |
| 237 | |
| 238 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#> |
| 239 | AEADCiphercxx . . $ mlspp/hpke/AEADCipher#::opencxx . . $ mlspp/hpke/AEADCipher#open(f7542dab5ac51900).(const bytescxx . . $ mlspp/bytes_ns/bytes#& key, |
| 240 | const bytescxx . . $ mlspp/bytes_ns/bytes#& nonce, |
| 241 | const bytescxx . . $ mlspp/bytes_ns/bytes#& aad, |
| 242 | const bytescxx . . $ mlspp/bytes_ns/bytes#& ct) const |
| 243 | { |
| 244 | if (ct.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).() < tag_sizecxx . . $ mlspp/hpke/AEADCipher#tag_size.) { |
| 245 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("AEAD ciphertext smaller than tag size"); |
| 246 | } |
| 247 | |
| 248 | #if WITH_BORINGSSL |
| 249 | auto ctx = make_typed_unique(EVP_AEAD_CTX_new( |
| 250 | boringssl_cipher(id), key.data(), key.size(), cipher_tag_size(id))); |
| 251 | if (ctx == nullptr) { |
| 252 | throw openssl_error(); |
| 253 | } |
| 254 | |
| 255 | auto pt = bytes(ct.size() - tag_size); |
| 256 | auto out_len = pt.size(); |
| 257 | if (1 != EVP_AEAD_CTX_open(ctx.get(), |
| 258 | pt.data(), |
| 259 | &out_len, |
| 260 | pt.size(), |
| 261 | nonce.data(), |
| 262 | nonce.size(), |
| 263 | ct.data(), |
| 264 | ct.size(), |
| 265 | aad.data(), |
| 266 | aad.size())) { |
| 267 | throw openssl_error(); |
| 268 | } |
| 269 | |
| 270 | return pt; |
| 271 | #else |
| 272 | auto ctx = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_CIPHER_CTX_new()); |
| 273 | if (ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 274 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 275 | } |
| 276 | |
| 277 | const auto* cipher = openssl_ciphercxx . . $ mlspp/hpke/openssl_cipher(eeaa53c2a40a6d14).(idcxx . . $ mlspp/hpke/AEAD#id.); |
| 278 | if (1 != EVP_DecryptInit(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), cipher, key.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), nonce.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).())) { |
| 279 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 280 | } |
| 281 | |
| 282 | auto inner_ct_size = ct.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).() - tag_sizecxx . . $ mlspp/hpke/AEADCipher#tag_size.; |
| 283 | auto tag = ct.slicecxx . . $ mlspp/bytes_ns/bytes#slice(ef0b1008b63e00ff).(inner_ct_size, ct.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()); |
| 284 | if (1 != EVP_CIPHER_CTX_ctrl(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 285 | EVP_CTRL_GCM_SET_TAGcxx . . $ `<external>/2497497914425334111/evp.h:394:9`!, |
| 286 | static_cast<int>(tag_sizecxx . . $ mlspp/hpke/AEADCipher#tag_size.), |
| 287 | tag.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).())) { |
| 288 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 289 | } |
| 290 | |
| 291 | int out_size = 0; |
| 292 | if (!aad.emptycxx . . $ mlspp/bytes_ns/bytes#empty(50ce9a9e25b4a850).()) { |
| 293 | if (1 != EVP_DecryptUpdate(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 294 | nullptr, |
| 295 | &out_size, |
| 296 | aad.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), |
| 297 | static_cast<int>(aad.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()))) { |
| 298 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | bytescxx . . $ mlspp/bytes_ns/bytes# ptcxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(inner_ct_size); |
| 303 | if (1 != EVP_DecryptUpdate(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 304 | pt.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), |
| 305 | &out_size, |
| 306 | ct.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), |
| 307 | static_cast<int>(inner_ct_size))) { |
| 308 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 309 | } |
| 310 | |
| 311 | // Providing nullptr as an argument is safe here because this |
| 312 | // function never writes with GCM; it only verifies the tag |
| 313 | if (1 != EVP_DecryptFinal(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr, &out_size)) { |
| 314 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("AEAD authentication failure"); |
| 315 | } |
| 316 | |
| 317 | return ptcxx . . $ std/optional#optional(e3f0d6382bd52f24).; |
| 318 | #endif // WITH_BORINGSSL |
| 319 | } |
| 320 | |
| 321 | } // namespace mlspp::hpke |