OLD | NEW |
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/quic/crypto/crypto_handshake.h" | 8 #include "net/quic/crypto/crypto_handshake.h" |
9 #include "net/quic/crypto/crypto_protocol.h" | 9 #include "net/quic/crypto/crypto_protocol.h" |
10 #include "net/quic/crypto/quic_decrypter.h" | 10 #include "net/quic/crypto/quic_decrypter.h" |
11 #include "net/quic/crypto/quic_encrypter.h" | 11 #include "net/quic/crypto/quic_encrypter.h" |
12 #include "net/quic/crypto/quic_random.h" | 12 #include "net/quic/crypto/quic_random.h" |
13 #include "net/quic/quic_time.h" | 13 #include "net/quic/quic_time.h" |
14 | 14 |
15 using base::StringPiece; | 15 using base::StringPiece; |
16 using std::string; | 16 using std::string; |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 | 19 |
20 // static | 20 // static |
21 bool CryptoUtils::FindMutualTag(const CryptoTagVector& our_tags_vector, | 21 bool CryptoUtils::FindMutualTag(const QuicTagVector& our_tags_vector, |
22 const CryptoTag* their_tags, | 22 const QuicTag* their_tags, |
23 size_t num_their_tags, | 23 size_t num_their_tags, |
24 Priority priority, | 24 Priority priority, |
25 CryptoTag* out_result, | 25 QuicTag* out_result, |
26 size_t* out_index) { | 26 size_t* out_index) { |
27 if (our_tags_vector.empty()) { | 27 if (our_tags_vector.empty()) { |
28 return false; | 28 return false; |
29 } | 29 } |
30 const size_t num_our_tags = our_tags_vector.size(); | 30 const size_t num_our_tags = our_tags_vector.size(); |
31 const CryptoTag* our_tags = &our_tags_vector[0]; | 31 const QuicTag* our_tags = &our_tags_vector[0]; |
32 | 32 |
33 size_t num_priority_tags, num_inferior_tags; | 33 size_t num_priority_tags, num_inferior_tags; |
34 const CryptoTag* priority_tags; | 34 const QuicTag* priority_tags; |
35 const CryptoTag* inferior_tags; | 35 const QuicTag* inferior_tags; |
36 if (priority == LOCAL_PRIORITY) { | 36 if (priority == LOCAL_PRIORITY) { |
37 num_priority_tags = num_our_tags; | 37 num_priority_tags = num_our_tags; |
38 priority_tags = our_tags; | 38 priority_tags = our_tags; |
39 num_inferior_tags = num_their_tags; | 39 num_inferior_tags = num_their_tags; |
40 inferior_tags = their_tags; | 40 inferior_tags = their_tags; |
41 } else { | 41 } else { |
42 num_priority_tags = num_their_tags; | 42 num_priority_tags = num_their_tags; |
43 priority_tags = their_tags; | 43 priority_tags = their_tags; |
44 num_inferior_tags = num_our_tags; | 44 num_inferior_tags = num_our_tags; |
45 inferior_tags = our_tags; | 45 inferior_tags = our_tags; |
(...skipping 11 matching lines...) Expand all Loading... |
57 } | 57 } |
58 } | 58 } |
59 return true; | 59 return true; |
60 } | 60 } |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 return false; | 64 return false; |
65 } | 65 } |
66 | 66 |
67 void CryptoUtils::GenerateNonce(QuicTime::Delta now, | 67 void CryptoUtils::GenerateNonce(QuicWallTime now, |
68 QuicRandom* random_generator, | 68 QuicRandom* random_generator, |
69 StringPiece orbit, | 69 StringPiece orbit, |
70 string* nonce) { | 70 string* nonce) { |
71 // a 4-byte timestamp + 28 random bytes. | 71 // a 4-byte timestamp + 28 random bytes. |
72 nonce->reserve(kNonceSize); | 72 nonce->reserve(kNonceSize); |
73 nonce->resize(kNonceSize); | 73 nonce->resize(kNonceSize); |
74 uint32 gmt_unix_time = now.ToSeconds(); | 74 uint32 gmt_unix_time = now.ToUNIXSeconds(); |
75 // The time in the nonce must be encoded in big-endian because the | 75 // The time in the nonce must be encoded in big-endian because the |
76 // strike-register depends on the nonces being ordered by time. | 76 // strike-register depends on the nonces being ordered by time. |
77 (*nonce)[0] = static_cast<char>(gmt_unix_time >> 24); | 77 (*nonce)[0] = static_cast<char>(gmt_unix_time >> 24); |
78 (*nonce)[1] = static_cast<char>(gmt_unix_time >> 16); | 78 (*nonce)[1] = static_cast<char>(gmt_unix_time >> 16); |
79 (*nonce)[2] = static_cast<char>(gmt_unix_time >> 8); | 79 (*nonce)[2] = static_cast<char>(gmt_unix_time >> 8); |
80 (*nonce)[3] = static_cast<char>(gmt_unix_time); | 80 (*nonce)[3] = static_cast<char>(gmt_unix_time); |
81 | 81 |
82 size_t bytes_written = sizeof(gmt_unix_time); | 82 size_t bytes_written = sizeof(gmt_unix_time); |
83 if (orbit.size() == 8) { | 83 if (orbit.size() == 8) { |
84 memcpy(&(*nonce)[bytes_written], orbit.data(), orbit.size()); | 84 memcpy(&(*nonce)[bytes_written], orbit.data(), orbit.size()); |
85 bytes_written += orbit.size(); | 85 bytes_written += orbit.size(); |
86 } | 86 } |
87 random_generator->RandBytes(&(*nonce)[bytes_written], | 87 random_generator->RandBytes(&(*nonce)[bytes_written], |
88 kNonceSize - bytes_written); | 88 kNonceSize - bytes_written); |
89 } | 89 } |
90 | 90 |
91 void CryptoUtils::DeriveKeys(QuicCryptoNegotiatedParameters* params, | 91 void CryptoUtils::DeriveKeys(StringPiece premaster_secret, |
| 92 QuicTag aead, |
92 StringPiece client_nonce, | 93 StringPiece client_nonce, |
| 94 StringPiece server_nonce, |
93 const string& hkdf_input, | 95 const string& hkdf_input, |
94 Perspective perspective) { | 96 Perspective perspective, |
95 params->encrypter.reset(QuicEncrypter::Create(params->aead)); | 97 CrypterPair* out) { |
96 params->decrypter.reset(QuicDecrypter::Create(params->aead)); | 98 out->encrypter.reset(QuicEncrypter::Create(aead)); |
97 size_t key_bytes = params->encrypter->GetKeySize(); | 99 out->decrypter.reset(QuicDecrypter::Create(aead)); |
98 size_t nonce_prefix_bytes = params->encrypter->GetNoncePrefixSize(); | 100 size_t key_bytes = out->encrypter->GetKeySize(); |
| 101 size_t nonce_prefix_bytes = out->encrypter->GetNoncePrefixSize(); |
99 | 102 |
100 StringPiece nonce = client_nonce; | 103 StringPiece nonce = client_nonce; |
101 string nonce_storage; | 104 string nonce_storage; |
102 if (!params->server_nonce.empty()) { | 105 if (!server_nonce.empty()) { |
103 nonce_storage = client_nonce.as_string() + params->server_nonce; | 106 nonce_storage = client_nonce.as_string() + server_nonce.as_string(); |
104 nonce = nonce_storage; | 107 nonce = nonce_storage; |
105 } | 108 } |
106 | 109 |
107 crypto::HKDF hkdf(params->premaster_secret, nonce, | 110 crypto::HKDF hkdf(premaster_secret, nonce, hkdf_input, key_bytes, |
108 hkdf_input, key_bytes, nonce_prefix_bytes); | 111 nonce_prefix_bytes); |
109 if (perspective == SERVER) { | 112 if (perspective == SERVER) { |
110 params->encrypter->SetKey(hkdf.server_write_key()); | 113 out->encrypter->SetKey(hkdf.server_write_key()); |
111 params->encrypter->SetNoncePrefix(hkdf.server_write_iv()); | 114 out->encrypter->SetNoncePrefix(hkdf.server_write_iv()); |
112 params->decrypter->SetKey(hkdf.client_write_key()); | 115 out->decrypter->SetKey(hkdf.client_write_key()); |
113 params->decrypter->SetNoncePrefix(hkdf.client_write_iv()); | 116 out->decrypter->SetNoncePrefix(hkdf.client_write_iv()); |
114 } else { | 117 } else { |
115 params->encrypter->SetKey(hkdf.client_write_key()); | 118 out->encrypter->SetKey(hkdf.client_write_key()); |
116 params->encrypter->SetNoncePrefix(hkdf.client_write_iv()); | 119 out->encrypter->SetNoncePrefix(hkdf.client_write_iv()); |
117 params->decrypter->SetKey(hkdf.server_write_key()); | 120 out->decrypter->SetKey(hkdf.server_write_key()); |
118 params->decrypter->SetNoncePrefix(hkdf.server_write_iv()); | 121 out->decrypter->SetNoncePrefix(hkdf.server_write_iv()); |
119 } | 122 } |
120 } | 123 } |
121 | 124 |
122 } // namespace net | 125 } // namespace net |
OLD | NEW |