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

Side by Side Diff: components/gcm_driver/crypto/p256_key_util.cc

Issue 1910903002: Remove *_openssl files for gcm_driver (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update comment. Created 4 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
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 "components/gcm_driver/crypto/p256_key_util.h" 5 #include "components/gcm_driver/crypto/p256_key_util.h"
6 6
7 #include <openssl/ec.h>
8 #include <openssl/ecdh.h>
9 #include <openssl/evp.h>
7 #include <stddef.h> 10 #include <stddef.h>
8 #include <stdint.h> 11 #include <stdint.h>
9 12
10 #include <memory> 13 #include <memory>
11 #include <vector> 14 #include <vector>
12 15
13 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/strings/string_util.h"
14 #include "crypto/ec_private_key.h" 18 #include "crypto/ec_private_key.h"
19 #include "crypto/scoped_openssl_types.h"
15 20
16 namespace gcm { 21 namespace gcm {
17 22
18 namespace { 23 namespace {
19 24
20 // The first byte in an uncompressed P-256 point per SEC1 2.3.3. 25 // The first byte in an uncompressed P-256 point per SEC1 2.3.3.
21 const char kUncompressedPointForm = 0x04; 26 const char kUncompressedPointForm = 0x04;
22 27
23 // A P-256 field element consists of 32 bytes. 28 // A P-256 field element consists of 32 bytes.
24 const size_t kFieldBytes = 32; 29 const size_t kFieldBytes = 32;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 public_key_x509.size()); 86 public_key_x509.size());
82 87
83 // Concatenate the leading 0x04 byte and the two uncompressed points. 88 // Concatenate the leading 0x04 byte and the two uncompressed points.
84 out_public_key->reserve(kUncompressedPointBytes); 89 out_public_key->reserve(kUncompressedPointBytes);
85 out_public_key->push_back(kUncompressedPointForm); 90 out_public_key->push_back(kUncompressedPointForm);
86 out_public_key->append(candidate_public_key); 91 out_public_key->append(candidate_public_key);
87 92
88 return true; 93 return true;
89 } 94 }
90 95
96 bool ComputeSharedP256Secret(const base::StringPiece& private_key,
97 const base::StringPiece& public_key_x509,
98 const base::StringPiece& peer_public_key,
99 std::string* out_shared_secret) {
100 DCHECK(out_shared_secret);
101
102 std::unique_ptr<crypto::ECPrivateKey> local_key_pair(
103 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
104 "" /* no password */,
105 std::vector<uint8_t>(private_key.data(),
106 private_key.data() + private_key.size()),
107 std::vector<uint8_t>(
108 public_key_x509.data(),
109 public_key_x509.data() + public_key_x509.size())));
110
111 if (!local_key_pair) {
112 DLOG(ERROR) << "Unable to create the local key pair.";
113 return false;
114 }
115
116 crypto::ScopedEC_KEY ec_private_key(
117 EVP_PKEY_get1_EC_KEY(local_key_pair->key()));
118
119 if (!ec_private_key || !EC_KEY_check_key(ec_private_key.get())) {
120 DLOG(ERROR) << "The private key is invalid.";
121 return false;
122 }
123
124 crypto::ScopedEC_POINT point(
125 EC_POINT_new(EC_KEY_get0_group(ec_private_key.get())));
126
127 if (!point ||
128 !EC_POINT_oct2point(EC_KEY_get0_group(ec_private_key.get()), point.get(),
129 reinterpret_cast<const uint8_t*>(
130 peer_public_key.data()),
131 peer_public_key.size(), nullptr)) {
132 DLOG(ERROR) << "Can't convert peer public value to curve point.";
133 return false;
134 }
135
136 uint8_t result[kFieldBytes];
137 if (ECDH_compute_key(result, sizeof(result), point.get(),
138 ec_private_key.get(), nullptr) != sizeof(result)) {
139 DLOG(ERROR) << "Unable to compute the ECDH shared secret.";
140 return false;
141 }
142
143 out_shared_secret->assign(reinterpret_cast<char*>(result), sizeof(result));
144 return true;
145 }
146
91 } // namespace gcm 147 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698