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