Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: crypto/aead_openssl.cc

Issue 1083493003: Encrypt certificate reports before uploading to HTTP URLs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: make nonce/key length static functions on Aead Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "crypto/aead_openssl.h"
6
7 #if defined(USE_OPENSSL)
8
9 #include <openssl/aes.h>
10 #include <openssl/evp.h>
11 #include <string>
12
13 #include "base/strings/string_util.h"
14 #include "crypto/openssl_util.h"
15 #include "crypto/symmetric_key.h"
16
17 namespace crypto {
18
19 Aead::Aead(const std::string* key) : key_(key) {
20 EnsureOpenSSLInit();
21 DCHECK_EQ(KeyLength(), key->size());
22 }
23
24 Aead::~Aead() {
25 }
26
27 bool Aead::Seal(const base::StringPiece& plaintext,
28 const base::StringPiece& nonce,
29 std::string* ciphertext) {
30 DCHECK_EQ(NonceLength(), nonce.size());
31 const EVP_AEAD* aead = EVP_aead_aes_128_ctr_hmac_sha256();
32 EVP_AEAD_CTX ctx;
33
34 if (!EVP_AEAD_CTX_init(&ctx, aead,
35 reinterpret_cast<const uint8*>(key_->data()),
36 key_->size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) {
37 return false;
38 }
39
40 std::string result;
41 const size_t max_output_length =
42 EVP_AEAD_max_overhead(aead) + plaintext.size();
43 size_t output_length;
44 uint8* out_ptr =
45 reinterpret_cast<uint8*>(WriteInto(&result, max_output_length + 1));
46
47 if (!EVP_AEAD_CTX_seal(&ctx, out_ptr, &output_length, max_output_length,
48 reinterpret_cast<const uint8*>(nonce.data()),
49 nonce.size(),
50 reinterpret_cast<const uint8*>(plaintext.data()),
51 plaintext.size(), nullptr, 0)) {
52 EVP_AEAD_CTX_cleanup(&ctx);
53 return false;
54 }
55
56 DCHECK_LE(output_length, max_output_length);
57 result.resize(output_length);
58
59 ciphertext->swap(result);
60 EVP_AEAD_CTX_cleanup(&ctx);
61
62 return true;
63 }
64
65 bool Aead::Open(const base::StringPiece& ciphertext,
66 const base::StringPiece& nonce,
67 std::string* plaintext) {
68 const EVP_AEAD* aead = EVP_aead_aes_128_ctr_hmac_sha256();
69 EVP_AEAD_CTX ctx;
70
71 if (!EVP_AEAD_CTX_init(&ctx, aead,
72 reinterpret_cast<const uint8*>(key_->data()),
73 key_->size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) {
74 return false;
75 }
76
77 std::string result;
78 const size_t max_output_length = ciphertext.size();
79 size_t output_length;
80 uint8* out_ptr =
81 reinterpret_cast<uint8*>(WriteInto(&result, max_output_length + 1));
82
83 if (!EVP_AEAD_CTX_open(&ctx, out_ptr, &output_length, max_output_length,
84 reinterpret_cast<const uint8*>(nonce.data()),
85 nonce.size(),
86 reinterpret_cast<const uint8*>(ciphertext.data()),
87 ciphertext.size(), nullptr, 0)) {
88 EVP_AEAD_CTX_cleanup(&ctx);
89 return false;
90 }
91
92 DCHECK_LE(output_length, max_output_length);
93 result.resize(output_length);
94
95 plaintext->swap(result);
96 EVP_AEAD_CTX_cleanup(&ctx);
97
98 return true;
99 }
100
101 size_t Aead::KeyLength() {
102 return EVP_AEAD_key_length(EVP_aead_aes_128_ctr_hmac_sha256());
103 }
104
105 size_t Aead::NonceLength() {
106 return EVP_AEAD_nonce_length(EVP_aead_aes_128_ctr_hmac_sha256());
107 }
108
109 } // namespace
110
111 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698