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

Side by Side Diff: net/quic/core/crypto/p256_key_exchange.cc

Issue 2740453006: Add QuicStringPiece which is actually StringPiece. (Closed)
Patch Set: fix compile error and rebase Created 3 years, 9 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
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/core/crypto/p256_key_exchange.h" 5 #include "net/quic/core/crypto/p256_key_exchange.h"
6 6
7 #include <cstdint> 7 #include <cstdint>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 11
12 #include "net/quic/platform/api/quic_logging.h" 12 #include "net/quic/platform/api/quic_logging.h"
13 #include "third_party/boringssl/src/include/openssl/ec.h" 13 #include "third_party/boringssl/src/include/openssl/ec.h"
14 #include "third_party/boringssl/src/include/openssl/ecdh.h" 14 #include "third_party/boringssl/src/include/openssl/ecdh.h"
15 #include "third_party/boringssl/src/include/openssl/err.h" 15 #include "third_party/boringssl/src/include/openssl/err.h"
16 #include "third_party/boringssl/src/include/openssl/evp.h" 16 #include "third_party/boringssl/src/include/openssl/evp.h"
17 17
18 using base::StringPiece;
19 using std::string; 18 using std::string;
20 19
21 namespace net { 20 namespace net {
22 21
23 P256KeyExchange::P256KeyExchange(bssl::UniquePtr<EC_KEY> private_key, 22 P256KeyExchange::P256KeyExchange(bssl::UniquePtr<EC_KEY> private_key,
24 const uint8_t* public_key) 23 const uint8_t* public_key)
25 : private_key_(std::move(private_key)) { 24 : private_key_(std::move(private_key)) {
26 memcpy(public_key_, public_key, sizeof(public_key_)); 25 memcpy(public_key_, public_key, sizeof(public_key_));
27 } 26 }
28 27
29 P256KeyExchange::~P256KeyExchange() {} 28 P256KeyExchange::~P256KeyExchange() {}
30 29
31 // static 30 // static
32 P256KeyExchange* P256KeyExchange::New(StringPiece key) { 31 P256KeyExchange* P256KeyExchange::New(QuicStringPiece key) {
33 if (key.empty()) { 32 if (key.empty()) {
34 QUIC_DLOG(INFO) << "Private key is empty"; 33 QUIC_DLOG(INFO) << "Private key is empty";
35 return nullptr; 34 return nullptr;
36 } 35 }
37 36
38 const uint8_t* keyp = reinterpret_cast<const uint8_t*>(key.data()); 37 const uint8_t* keyp = reinterpret_cast<const uint8_t*>(key.data());
39 bssl::UniquePtr<EC_KEY> private_key( 38 bssl::UniquePtr<EC_KEY> private_key(
40 d2i_ECPrivateKey(nullptr, &keyp, key.size())); 39 d2i_ECPrivateKey(nullptr, &keyp, key.size()));
41 if (!private_key.get() || !EC_KEY_check_key(private_key.get())) { 40 if (!private_key.get() || !EC_KEY_check_key(private_key.get())) {
42 QUIC_DLOG(INFO) << "Private key is invalid."; 41 QUIC_DLOG(INFO) << "Private key is invalid.";
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 } 75 }
77 return string(reinterpret_cast<char*>(private_key.get()), key_len); 76 return string(reinterpret_cast<char*>(private_key.get()), key_len);
78 } 77 }
79 78
80 KeyExchange* P256KeyExchange::NewKeyPair(QuicRandom* /*rand*/) const { 79 KeyExchange* P256KeyExchange::NewKeyPair(QuicRandom* /*rand*/) const {
81 // TODO(agl): avoid the serialisation/deserialisation in this function. 80 // TODO(agl): avoid the serialisation/deserialisation in this function.
82 const string private_value = NewPrivateKey(); 81 const string private_value = NewPrivateKey();
83 return P256KeyExchange::New(private_value); 82 return P256KeyExchange::New(private_value);
84 } 83 }
85 84
86 bool P256KeyExchange::CalculateSharedKey(StringPiece peer_public_value, 85 bool P256KeyExchange::CalculateSharedKey(QuicStringPiece peer_public_value,
87 string* out_result) const { 86 string* out_result) const {
88 if (peer_public_value.size() != kUncompressedP256PointBytes) { 87 if (peer_public_value.size() != kUncompressedP256PointBytes) {
89 QUIC_DLOG(INFO) << "Peer public value is invalid"; 88 QUIC_DLOG(INFO) << "Peer public value is invalid";
90 return false; 89 return false;
91 } 90 }
92 91
93 bssl::UniquePtr<EC_POINT> point( 92 bssl::UniquePtr<EC_POINT> point(
94 EC_POINT_new(EC_KEY_get0_group(private_key_.get()))); 93 EC_POINT_new(EC_KEY_get0_group(private_key_.get())));
95 if (!point.get() || 94 if (!point.get() ||
96 !EC_POINT_oct2point(/* also test if point is on curve */ 95 !EC_POINT_oct2point(/* also test if point is on curve */
97 EC_KEY_get0_group(private_key_.get()), point.get(), 96 EC_KEY_get0_group(private_key_.get()), point.get(),
98 reinterpret_cast<const uint8_t*>( 97 reinterpret_cast<const uint8_t*>(
99 peer_public_value.data()), 98 peer_public_value.data()),
100 peer_public_value.size(), nullptr)) { 99 peer_public_value.size(), nullptr)) {
101 QUIC_DLOG(INFO) << "Can't convert peer public value to curve point."; 100 QUIC_DLOG(INFO) << "Can't convert peer public value to curve point.";
102 return false; 101 return false;
103 } 102 }
104 103
105 uint8_t result[kP256FieldBytes]; 104 uint8_t result[kP256FieldBytes];
106 if (ECDH_compute_key(result, sizeof(result), point.get(), private_key_.get(), 105 if (ECDH_compute_key(result, sizeof(result), point.get(), private_key_.get(),
107 nullptr) != sizeof(result)) { 106 nullptr) != sizeof(result)) {
108 QUIC_DLOG(INFO) << "Can't compute ECDH shared key."; 107 QUIC_DLOG(INFO) << "Can't compute ECDH shared key.";
109 return false; 108 return false;
110 } 109 }
111 110
112 out_result->assign(reinterpret_cast<char*>(result), sizeof(result)); 111 out_result->assign(reinterpret_cast<char*>(result), sizeof(result));
113 return true; 112 return true;
114 } 113 }
115 114
116 StringPiece P256KeyExchange::public_value() const { 115 QuicStringPiece P256KeyExchange::public_value() const {
117 return StringPiece(reinterpret_cast<const char*>(public_key_), 116 return QuicStringPiece(reinterpret_cast<const char*>(public_key_),
118 sizeof(public_key_)); 117 sizeof(public_key_));
119 } 118 }
120 119
121 QuicTag P256KeyExchange::tag() const { 120 QuicTag P256KeyExchange::tag() const {
122 return kP256; 121 return kP256;
123 } 122 }
124 123
125 } // namespace net 124 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/core/crypto/p256_key_exchange.h ('k') | net/quic/core/crypto/p256_key_exchange_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698