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 "crypto/aead_openssl.h" | 5 #include "crypto/aead_openssl.h" |
6 | 6 |
7 #if defined(USE_OPENSSL) | 7 #if defined(USE_OPENSSL) |
8 | 8 |
9 #include <openssl/aes.h> | 9 #include <openssl/aes.h> |
10 #include <openssl/evp.h> | 10 #include <openssl/evp.h> |
| 11 #include <stddef.h> |
| 12 #include <stdint.h> |
11 #include <string> | 13 #include <string> |
12 | 14 |
13 #include "base/basictypes.h" | |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "crypto/openssl_util.h" | 16 #include "crypto/openssl_util.h" |
16 | 17 |
17 namespace crypto { | 18 namespace crypto { |
18 | 19 |
19 Aead::Aead(AeadAlgorithm algorithm) : key_(nullptr) { | 20 Aead::Aead(AeadAlgorithm algorithm) : key_(nullptr) { |
20 EnsureOpenSSLInit(); | 21 EnsureOpenSSLInit(); |
21 switch (algorithm) { | 22 switch (algorithm) { |
22 case AES_128_CTR_HMAC_SHA256: | 23 case AES_128_CTR_HMAC_SHA256: |
23 aead_ = EVP_aead_aes_128_ctr_hmac_sha256(); | 24 aead_ = EVP_aead_aes_128_ctr_hmac_sha256(); |
(...skipping 12 matching lines...) Expand all Loading... |
36 | 37 |
37 bool Aead::Seal(const base::StringPiece& plaintext, | 38 bool Aead::Seal(const base::StringPiece& plaintext, |
38 const base::StringPiece& nonce, | 39 const base::StringPiece& nonce, |
39 const base::StringPiece& additional_data, | 40 const base::StringPiece& additional_data, |
40 std::string* ciphertext) const { | 41 std::string* ciphertext) const { |
41 DCHECK(key_); | 42 DCHECK(key_); |
42 DCHECK_EQ(NonceLength(), nonce.size()); | 43 DCHECK_EQ(NonceLength(), nonce.size()); |
43 EVP_AEAD_CTX ctx; | 44 EVP_AEAD_CTX ctx; |
44 | 45 |
45 if (!EVP_AEAD_CTX_init(&ctx, aead_, | 46 if (!EVP_AEAD_CTX_init(&ctx, aead_, |
46 reinterpret_cast<const uint8*>(key_->data()), | 47 reinterpret_cast<const uint8_t*>(key_->data()), |
47 key_->size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) { | 48 key_->size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) { |
48 return false; | 49 return false; |
49 } | 50 } |
50 | 51 |
51 std::string result; | 52 std::string result; |
52 const size_t max_output_length = | 53 const size_t max_output_length = |
53 EVP_AEAD_max_overhead(aead_) + plaintext.size(); | 54 EVP_AEAD_max_overhead(aead_) + plaintext.size(); |
54 size_t output_length; | 55 size_t output_length; |
55 uint8* out_ptr = | 56 uint8_t* out_ptr = reinterpret_cast<uint8_t*>( |
56 reinterpret_cast<uint8*>(base::WriteInto(&result, max_output_length + 1)); | 57 base::WriteInto(&result, max_output_length + 1)); |
57 | 58 |
58 if (!EVP_AEAD_CTX_seal( | 59 if (!EVP_AEAD_CTX_seal( |
59 &ctx, out_ptr, &output_length, max_output_length, | 60 &ctx, out_ptr, &output_length, max_output_length, |
60 reinterpret_cast<const uint8*>(nonce.data()), nonce.size(), | 61 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), |
61 reinterpret_cast<const uint8*>(plaintext.data()), plaintext.size(), | 62 reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size(), |
62 reinterpret_cast<const uint8*>(additional_data.data()), | 63 reinterpret_cast<const uint8_t*>(additional_data.data()), |
63 additional_data.size())) { | 64 additional_data.size())) { |
64 EVP_AEAD_CTX_cleanup(&ctx); | 65 EVP_AEAD_CTX_cleanup(&ctx); |
65 return false; | 66 return false; |
66 } | 67 } |
67 | 68 |
68 DCHECK_LE(output_length, max_output_length); | 69 DCHECK_LE(output_length, max_output_length); |
69 result.resize(output_length); | 70 result.resize(output_length); |
70 | 71 |
71 ciphertext->swap(result); | 72 ciphertext->swap(result); |
72 EVP_AEAD_CTX_cleanup(&ctx); | 73 EVP_AEAD_CTX_cleanup(&ctx); |
73 | 74 |
74 return true; | 75 return true; |
75 } | 76 } |
76 | 77 |
77 bool Aead::Open(const base::StringPiece& ciphertext, | 78 bool Aead::Open(const base::StringPiece& ciphertext, |
78 const base::StringPiece& nonce, | 79 const base::StringPiece& nonce, |
79 const base::StringPiece& additional_data, | 80 const base::StringPiece& additional_data, |
80 std::string* plaintext) const { | 81 std::string* plaintext) const { |
81 DCHECK(key_); | 82 DCHECK(key_); |
82 EVP_AEAD_CTX ctx; | 83 EVP_AEAD_CTX ctx; |
83 | 84 |
84 if (!EVP_AEAD_CTX_init(&ctx, aead_, | 85 if (!EVP_AEAD_CTX_init(&ctx, aead_, |
85 reinterpret_cast<const uint8*>(key_->data()), | 86 reinterpret_cast<const uint8_t*>(key_->data()), |
86 key_->size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) { | 87 key_->size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) { |
87 return false; | 88 return false; |
88 } | 89 } |
89 | 90 |
90 std::string result; | 91 std::string result; |
91 const size_t max_output_length = ciphertext.size(); | 92 const size_t max_output_length = ciphertext.size(); |
92 size_t output_length; | 93 size_t output_length; |
93 uint8* out_ptr = | 94 uint8_t* out_ptr = reinterpret_cast<uint8_t*>( |
94 reinterpret_cast<uint8*>(base::WriteInto(&result, max_output_length + 1)); | 95 base::WriteInto(&result, max_output_length + 1)); |
95 | 96 |
96 if (!EVP_AEAD_CTX_open( | 97 if (!EVP_AEAD_CTX_open( |
97 &ctx, out_ptr, &output_length, max_output_length, | 98 &ctx, out_ptr, &output_length, max_output_length, |
98 reinterpret_cast<const uint8*>(nonce.data()), nonce.size(), | 99 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), |
99 reinterpret_cast<const uint8*>(ciphertext.data()), ciphertext.size(), | 100 reinterpret_cast<const uint8_t*>(ciphertext.data()), |
100 reinterpret_cast<const uint8*>(additional_data.data()), | 101 ciphertext.size(), |
| 102 reinterpret_cast<const uint8_t*>(additional_data.data()), |
101 additional_data.size())) { | 103 additional_data.size())) { |
102 EVP_AEAD_CTX_cleanup(&ctx); | 104 EVP_AEAD_CTX_cleanup(&ctx); |
103 return false; | 105 return false; |
104 } | 106 } |
105 | 107 |
106 DCHECK_LE(output_length, max_output_length); | 108 DCHECK_LE(output_length, max_output_length); |
107 result.resize(output_length); | 109 result.resize(output_length); |
108 | 110 |
109 plaintext->swap(result); | 111 plaintext->swap(result); |
110 EVP_AEAD_CTX_cleanup(&ctx); | 112 EVP_AEAD_CTX_cleanup(&ctx); |
111 | 113 |
112 return true; | 114 return true; |
113 } | 115 } |
114 | 116 |
115 size_t Aead::KeyLength() const { | 117 size_t Aead::KeyLength() const { |
116 return EVP_AEAD_key_length(aead_); | 118 return EVP_AEAD_key_length(aead_); |
117 } | 119 } |
118 | 120 |
119 size_t Aead::NonceLength() const { | 121 size_t Aead::NonceLength() const { |
120 return EVP_AEAD_nonce_length(aead_); | 122 return EVP_AEAD_nonce_length(aead_); |
121 } | 123 } |
122 | 124 |
123 } // namespace | 125 } // namespace |
124 | 126 |
125 #endif | 127 #endif |
OLD | NEW |