| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/gcm_driver/crypto/gcm_message_cryptographer.h" | |
| 6 | |
| 7 #include <openssl/aead.h> | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/numerics/safe_math.h" | |
| 13 #include "base/strings/string_util.h" | |
| 14 | |
| 15 namespace gcm { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // The BoringSSL functions used to seal (encrypt) and open (decrypt) a payload | |
| 20 // follow the same prototype, declared as follows. | |
| 21 using EVP_AEAD_CTX_TransformFunction = | |
| 22 int(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, | |
| 23 size_t max_out_len, const uint8_t *nonce, size_t nonce_len, | |
| 24 const uint8_t *in, size_t in_len, const uint8_t *ad, size_t ad_len); | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 bool GCMMessageCryptographer::EncryptDecryptRecordInternal( | |
| 29 Mode mode, | |
| 30 const base::StringPiece& input, | |
| 31 const base::StringPiece& key, | |
| 32 const base::StringPiece& nonce, | |
| 33 std::string* output) const { | |
| 34 DCHECK(output); | |
| 35 | |
| 36 const EVP_AEAD* aead = EVP_aead_aes_128_gcm(); | |
| 37 | |
| 38 EVP_AEAD_CTX context; | |
| 39 if (!EVP_AEAD_CTX_init(&context, aead, | |
| 40 reinterpret_cast<const uint8_t*>(key.data()), | |
| 41 key.size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) { | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 base::CheckedNumeric<size_t> maximum_output_length(input.size()); | |
| 46 if (mode == ENCRYPT) | |
| 47 maximum_output_length += kAuthenticationTagBytes; | |
| 48 | |
| 49 // WriteInto requires the buffer to finish with a NULL-byte. | |
| 50 maximum_output_length += 1; | |
| 51 | |
| 52 size_t output_length = 0; | |
| 53 uint8_t* raw_output = reinterpret_cast<uint8_t*>( | |
| 54 base::WriteInto(output, maximum_output_length.ValueOrDie())); | |
| 55 | |
| 56 EVP_AEAD_CTX_TransformFunction* transform_function = | |
| 57 mode == ENCRYPT ? EVP_AEAD_CTX_seal : EVP_AEAD_CTX_open; | |
| 58 | |
| 59 if (!transform_function( | |
| 60 &context, raw_output, &output_length, output->size(), | |
| 61 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), | |
| 62 reinterpret_cast<const uint8_t*>(input.data()), input.size(), | |
| 63 nullptr, 0)) { | |
| 64 EVP_AEAD_CTX_cleanup(&context); | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 EVP_AEAD_CTX_cleanup(&context); | |
| 69 | |
| 70 base::CheckedNumeric<size_t> expected_output_length(input.size()); | |
| 71 if (mode == ENCRYPT) | |
| 72 expected_output_length += kAuthenticationTagBytes; | |
| 73 else | |
| 74 expected_output_length -= kAuthenticationTagBytes; | |
| 75 | |
| 76 DCHECK_EQ(expected_output_length.ValueOrDie(), output_length); | |
| 77 | |
| 78 output->resize(output_length); | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 } // namespace gcm | |
| OLD | NEW |