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

Side by Side Diff: net/quic/crypto/crypto_utils.cc

Issue 423333002: Implement QUIC key extraction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Pass a size_t constant as a size_t argument. Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "net/quic/crypto/crypto_utils.h" 5 #include "net/quic/crypto/crypto_utils.h"
6 6
7 #include "crypto/hkdf.h" 7 #include "crypto/hkdf.h"
8 #include "net/base/net_util.h" 8 #include "net/base/net_util.h"
9 #include "net/quic/crypto/crypto_handshake.h" 9 #include "net/quic/crypto/crypto_handshake.h"
10 #include "net/quic/crypto/crypto_protocol.h" 10 #include "net/quic/crypto/crypto_protocol.h"
11 #include "net/quic/crypto/quic_decrypter.h" 11 #include "net/quic/crypto/quic_decrypter.h"
12 #include "net/quic/crypto/quic_encrypter.h" 12 #include "net/quic/crypto/quic_encrypter.h"
13 #include "net/quic/crypto/quic_random.h" 13 #include "net/quic/crypto/quic_random.h"
14 #include "net/quic/quic_time.h" 14 #include "net/quic/quic_time.h"
15 #include "url/url_canon.h" 15 #include "url/url_canon.h"
16 16
17 using base::StringPiece; 17 using base::StringPiece;
18 using std::numeric_limits;
18 using std::string; 19 using std::string;
19 20
20 namespace net { 21 namespace net {
21 22
22 // static 23 // static
23 void CryptoUtils::GenerateNonce(QuicWallTime now, 24 void CryptoUtils::GenerateNonce(QuicWallTime now,
24 QuicRandom* random_generator, 25 QuicRandom* random_generator,
25 StringPiece orbit, 26 StringPiece orbit,
26 string* nonce) { 27 string* nonce) {
27 // a 4-byte timestamp + 28 random bytes. 28 // a 4-byte timestamp + 28 random bytes.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 return host; 77 return host;
77 } 78 }
78 79
79 // static 80 // static
80 bool CryptoUtils::DeriveKeys(StringPiece premaster_secret, 81 bool CryptoUtils::DeriveKeys(StringPiece premaster_secret,
81 QuicTag aead, 82 QuicTag aead,
82 StringPiece client_nonce, 83 StringPiece client_nonce,
83 StringPiece server_nonce, 84 StringPiece server_nonce,
84 const string& hkdf_input, 85 const string& hkdf_input,
85 Perspective perspective, 86 Perspective perspective,
86 CrypterPair* out) { 87 CrypterPair* crypters,
87 out->encrypter.reset(QuicEncrypter::Create(aead)); 88 string* subkey_secret) {
88 out->decrypter.reset(QuicDecrypter::Create(aead)); 89 crypters->encrypter.reset(QuicEncrypter::Create(aead));
89 size_t key_bytes = out->encrypter->GetKeySize(); 90 crypters->decrypter.reset(QuicDecrypter::Create(aead));
90 size_t nonce_prefix_bytes = out->encrypter->GetNoncePrefixSize(); 91 size_t key_bytes = crypters->encrypter->GetKeySize();
92 size_t nonce_prefix_bytes = crypters->encrypter->GetNoncePrefixSize();
93 size_t subkey_secret_bytes =
94 subkey_secret == NULL ? 0 : premaster_secret.length();
91 95
92 StringPiece nonce = client_nonce; 96 StringPiece nonce = client_nonce;
93 string nonce_storage; 97 string nonce_storage;
94 if (!server_nonce.empty()) { 98 if (!server_nonce.empty()) {
95 nonce_storage = client_nonce.as_string() + server_nonce.as_string(); 99 nonce_storage = client_nonce.as_string() + server_nonce.as_string();
96 nonce = nonce_storage; 100 nonce = nonce_storage;
97 } 101 }
98 102
99 crypto::HKDF hkdf(premaster_secret, nonce, hkdf_input, key_bytes, 103 crypto::HKDF hkdf(premaster_secret, nonce, hkdf_input, key_bytes,
100 nonce_prefix_bytes); 104 nonce_prefix_bytes, subkey_secret_bytes);
101 if (perspective == SERVER) { 105 if (perspective == SERVER) {
102 if (!out->encrypter->SetKey(hkdf.server_write_key()) || 106 if (!crypters->encrypter->SetKey(hkdf.server_write_key()) ||
103 !out->encrypter->SetNoncePrefix(hkdf.server_write_iv()) || 107 !crypters->encrypter->SetNoncePrefix(hkdf.server_write_iv()) ||
104 !out->decrypter->SetKey(hkdf.client_write_key()) || 108 !crypters->decrypter->SetKey(hkdf.client_write_key()) ||
105 !out->decrypter->SetNoncePrefix(hkdf.client_write_iv())) { 109 !crypters->decrypter->SetNoncePrefix(hkdf.client_write_iv())) {
106 return false; 110 return false;
107 } 111 }
108 } else { 112 } else {
109 if (!out->encrypter->SetKey(hkdf.client_write_key()) || 113 if (!crypters->encrypter->SetKey(hkdf.client_write_key()) ||
110 !out->encrypter->SetNoncePrefix(hkdf.client_write_iv()) || 114 !crypters->encrypter->SetNoncePrefix(hkdf.client_write_iv()) ||
111 !out->decrypter->SetKey(hkdf.server_write_key()) || 115 !crypters->decrypter->SetKey(hkdf.server_write_key()) ||
112 !out->decrypter->SetNoncePrefix(hkdf.server_write_iv())) { 116 !crypters->decrypter->SetNoncePrefix(hkdf.server_write_iv())) {
113 return false; 117 return false;
114 } 118 }
115 } 119 }
120 if (subkey_secret != NULL) {
121 hkdf.subkey_secret().CopyToString(subkey_secret);
122 }
116 123
117 return true; 124 return true;
118 } 125 }
119 126
127 // static
128 bool CryptoUtils::ExportKeyingMaterial(StringPiece subkey_secret,
129 StringPiece label,
130 StringPiece context,
131 size_t result_len,
132 string* result) {
133 for (size_t i = 0; i < label.length(); i++) {
134 if (label[i] == '\0') {
135 LOG(ERROR) << "ExportKeyingMaterial label may not contain NULs";
136 return false;
137 }
138 }
139 // Create HKDF info input: null-terminated label + length-prefixed context
140 if (context.length() >= numeric_limits<uint32>::max()) {
141 LOG(ERROR) << "Context value longer than 2^32";
142 return false;
143 }
144 uint32 context_length = static_cast<uint32>(context.length());
145 string info = label.as_string();
146 info.push_back('\0');
147 info.append(reinterpret_cast<char*>(&context_length), sizeof(context_length));
148 info.append(context.data(), context.length());
149
150 crypto::HKDF hkdf(subkey_secret,
151 StringPiece() /* no salt */,
152 info,
153 result_len,
154 0 /* no fixed IV */,
155 0 /* no subkey secret */);
156 hkdf.client_write_key().CopyToString(result);
157 return true;
158 }
159
120 } // namespace net 160 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698