34
34
#include " openssl/evp.h"
35
35
#include " tink/aead.h"
36
36
#include " tink/internal/aes_util.h"
37
+ #include " tink/internal/call_with_core_dump_protection.h"
37
38
#include " tink/internal/fips_utils.h"
38
39
#include " tink/internal/util.h"
39
40
#include " tink/subtle/random.h"
@@ -160,12 +161,15 @@ crypto::tink::util::StatusOr<std::unique_ptr<Aead>> AesEaxBoringSsl::New(
160
161
return util::Status (absl::StatusCode::kInvalidArgument ,
161
162
" Invalid nonce size" );
162
163
}
163
- auto aeskey_or = InitAesKey (key);
164
- if (!aeskey_or.ok ()) {
165
- return aeskey_or.status ();
166
- }
167
- return {absl::WrapUnique (
168
- new AesEaxBoringSsl (std::move (aeskey_or).value (), nonce_size_in_bytes))};
164
+ return internal::CallWithCoreDumpProtection (
165
+ [&]() -> util::StatusOr<std::unique_ptr<Aead>> {
166
+ auto aeskey_or = InitAesKey (key);
167
+ if (!aeskey_or.ok ()) {
168
+ return aeskey_or.status ();
169
+ }
170
+ return absl::WrapUnique (new AesEaxBoringSsl (
171
+ std::move (aeskey_or).value (), nonce_size_in_bytes));
172
+ });
169
173
}
170
174
171
175
AesEaxBoringSsl::Block AesEaxBoringSsl::Pad (
@@ -224,7 +228,7 @@ AesEaxBoringSsl::Block AesEaxBoringSsl::Omac(absl::Span<const uint8_t> data,
224
228
}
225
229
226
230
util::Status AesEaxBoringSsl::CtrCrypt (const Block& N, absl::string_view in,
227
- absl::Span<char > out) const {
231
+ absl::Span<char > out) const {
228
232
// Make a copy of N, since BoringSsl changes ctr.
229
233
uint8_t ctr[kBlockSize ];
230
234
std::copy_n (N.begin (), kBlockSize , ctr);
@@ -241,21 +245,26 @@ crypto::tink::util::StatusOr<std::string> AesEaxBoringSsl::Encrypt(
241
245
size_t ciphertext_size = plaintext.size () + nonce_size_ + kTagSize ;
242
246
std::string ciphertext;
243
247
ResizeStringUninitialized (&ciphertext, ciphertext_size);
244
- const std::string nonce = Random::GetRandomBytes (nonce_size_);
245
- const Block N = Omac (nonce, 0 );
246
- const Block H = Omac (associated_data, 1 );
247
- uint8_t * ct_start = reinterpret_cast <uint8_t *>(&ciphertext[nonce_size_]);
248
- util::Status res =
249
- CtrCrypt (N, plaintext, absl::MakeSpan (ciphertext).subspan (nonce_size_));
250
- if (!res.ok ()) {
251
- return res;
252
- }
253
- Block mac = Omac (absl::MakeSpan (ct_start, plaintext.size ()), 2 );
254
- XorBlock (N.data (), &mac);
255
- XorBlock (H.data (), &mac);
256
- absl::c_copy (nonce, ciphertext.begin ());
257
- std::copy_n (mac.begin (), kTagSize , &ciphertext[ciphertext_size - kTagSize ]);
258
- return ciphertext;
248
+ return internal::CallWithCoreDumpProtection (
249
+ [&]() -> util::StatusOr<std::string> {
250
+ const std::string nonce = Random::GetRandomBytes (nonce_size_);
251
+ const Block N = Omac (nonce, 0 );
252
+ const Block H = Omac (associated_data, 1 );
253
+ uint8_t * ct_start =
254
+ reinterpret_cast <uint8_t *>(&ciphertext[nonce_size_]);
255
+ util::Status res = CtrCrypt (
256
+ N, plaintext, absl::MakeSpan (ciphertext).subspan (nonce_size_));
257
+ if (!res.ok ()) {
258
+ return res;
259
+ }
260
+ Block mac = Omac (absl::MakeSpan (ct_start, plaintext.size ()), 2 );
261
+ XorBlock (N.data (), &mac);
262
+ XorBlock (H.data (), &mac);
263
+ absl::c_copy (nonce, ciphertext.begin ());
264
+ std::copy_n (mac.begin (), kTagSize ,
265
+ &ciphertext[ciphertext_size - kTagSize ]);
266
+ return ciphertext;
267
+ });
259
268
}
260
269
261
270
crypto::tink::util::StatusOr<std::string> AesEaxBoringSsl::Decrypt (
@@ -273,22 +282,26 @@ crypto::tink::util::StatusOr<std::string> AesEaxBoringSsl::Decrypt(
273
282
absl::string_view nonce = ciphertext.substr (0 , nonce_size_);
274
283
absl::string_view encrypted = ciphertext.substr (nonce_size_, out_size);
275
284
absl::string_view tag = ciphertext.substr (ct_size - kTagSize , kTagSize );
276
- const Block N = Omac (nonce, 0 );
277
- const Block H = Omac (associated_data, 1 );
278
- Block mac = Omac (encrypted, 2 );
279
- XorBlock (N.data (), &mac);
280
- XorBlock (H.data (), &mac);
281
- const uint8_t * sig = reinterpret_cast <const uint8_t *>(tag.data ());
282
- if (!EqualBlocks (mac.data (), sig)) {
283
- return util::Status (absl::StatusCode::kInvalidArgument , " Tag mismatch" );
284
- }
285
- std::string plaintext;
286
- ResizeStringUninitialized (&plaintext, out_size);
287
- util::Status res = CtrCrypt (N, encrypted, absl::MakeSpan (plaintext));
288
- if (!res.ok ()) {
289
- return res;
290
- }
291
- return plaintext;
285
+ return internal::CallWithCoreDumpProtection (
286
+ [&]() -> util::StatusOr<std::string> {
287
+ const Block N = Omac (nonce, 0 );
288
+ const Block H = Omac (associated_data, 1 );
289
+ Block mac = Omac (encrypted, 2 );
290
+ XorBlock (N.data (), &mac);
291
+ XorBlock (H.data (), &mac);
292
+ const uint8_t * sig = reinterpret_cast <const uint8_t *>(tag.data ());
293
+ if (!EqualBlocks (mac.data (), sig)) {
294
+ return util::Status (absl::StatusCode::kInvalidArgument ,
295
+ " Tag mismatch" );
296
+ }
297
+ std::string plaintext;
298
+ ResizeStringUninitialized (&plaintext, out_size);
299
+ util::Status res = CtrCrypt (N, encrypted, absl::MakeSpan (plaintext));
300
+ if (!res.ok ()) {
301
+ return res;
302
+ }
303
+ return plaintext;
304
+ });
292
305
}
293
306
294
307
} // namespace subtle
0 commit comments