OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/gcm_driver/crypto/gcm_message_cryptographer.h" | 5 #include "components/gcm_driver/crypto/gcm_message_cryptographer.h" |
6 | 6 |
7 #include <openssl/aead.h> | 7 #include <openssl/aead.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/numerics/safe_math.h" | 10 #include "base/numerics/safe_math.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 const base::StringPiece& nonce, | 30 const base::StringPiece& nonce, |
31 std::string* output) const { | 31 std::string* output) const { |
32 DCHECK(output); | 32 DCHECK(output); |
33 | 33 |
34 const EVP_AEAD* aead = EVP_aead_aes_128_gcm(); | 34 const EVP_AEAD* aead = EVP_aead_aes_128_gcm(); |
35 | 35 |
36 EVP_AEAD_CTX context; | 36 EVP_AEAD_CTX context; |
37 if (!EVP_AEAD_CTX_init(&context, aead, | 37 if (!EVP_AEAD_CTX_init(&context, aead, |
38 reinterpret_cast<const uint8_t*>(key.data()), | 38 reinterpret_cast<const uint8_t*>(key.data()), |
39 key.size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) { | 39 key.size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) { |
| 40 LOG(INFO) << "--b1"; |
40 return false; | 41 return false; |
41 } | 42 } |
42 | 43 |
43 base::CheckedNumeric<size_t> maximum_output_length(input.size()); | 44 base::CheckedNumeric<size_t> maximum_output_length(input.size()); |
44 if (mode == ENCRYPT) | 45 if (mode == ENCRYPT) |
45 maximum_output_length += kAuthenticationTagBytes; | 46 maximum_output_length += kAuthenticationTagBytes; |
46 | 47 |
47 // WriteInto requires the buffer to finish with a NULL-byte. | 48 // WriteInto requires the buffer to finish with a NULL-byte. |
48 maximum_output_length += 1; | 49 maximum_output_length += 1; |
49 | 50 |
50 size_t output_length = 0; | 51 size_t output_length = 0; |
51 uint8_t* raw_output = reinterpret_cast<uint8_t*>( | 52 uint8_t* raw_output = reinterpret_cast<uint8_t*>( |
52 base::WriteInto(output, maximum_output_length.ValueOrDie())); | 53 base::WriteInto(output, maximum_output_length.ValueOrDie())); |
53 | 54 |
54 EVP_AEAD_CTX_TransformFunction* transform_function = | 55 EVP_AEAD_CTX_TransformFunction* transform_function = |
55 mode == ENCRYPT ? EVP_AEAD_CTX_seal : EVP_AEAD_CTX_open; | 56 mode == ENCRYPT ? EVP_AEAD_CTX_seal : EVP_AEAD_CTX_open; |
56 | 57 |
57 if (!transform_function( | 58 if (!transform_function( |
58 &context, raw_output, &output_length, output->size(), | 59 &context, raw_output, &output_length, output->size(), |
59 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), | 60 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), |
60 reinterpret_cast<const uint8_t*>(input.data()), input.size(), | 61 reinterpret_cast<const uint8_t*>(input.data()), input.size(), |
61 nullptr, 0)) { | 62 nullptr, 0)) { |
| 63 LOG(INFO) << "--b2"; |
62 EVP_AEAD_CTX_cleanup(&context); | 64 EVP_AEAD_CTX_cleanup(&context); |
63 return false; | 65 return false; |
64 } | 66 } |
65 | 67 |
66 EVP_AEAD_CTX_cleanup(&context); | 68 EVP_AEAD_CTX_cleanup(&context); |
67 | 69 |
68 base::CheckedNumeric<size_t> expected_output_length(input.size()); | 70 base::CheckedNumeric<size_t> expected_output_length(input.size()); |
69 if (mode == ENCRYPT) | 71 if (mode == ENCRYPT) |
70 expected_output_length += kAuthenticationTagBytes; | 72 expected_output_length += kAuthenticationTagBytes; |
71 else | 73 else |
72 expected_output_length -= kAuthenticationTagBytes; | 74 expected_output_length -= kAuthenticationTagBytes; |
73 | 75 |
74 DCHECK_EQ(expected_output_length.ValueOrDie(), output_length); | 76 DCHECK_EQ(expected_output_length.ValueOrDie(), output_length); |
75 | 77 |
76 output->resize(output_length); | 78 output->resize(output_length); |
77 return true; | 79 return true; |
78 } | 80 } |
79 | 81 |
80 } // namespace gcm | 82 } // namespace gcm |
OLD | NEW |