| 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 <openssl/ec.h> | 7 #include <openssl/ec.h> |
| 8 #include <openssl/ecdh.h> | 8 #include <openssl/ecdh.h> |
| 9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "crypto/ec_private_key.h" | 18 #include "crypto/ec_private_key.h" |
| 19 #include "crypto/scoped_openssl_types.h" | |
| 20 | 19 |
| 21 namespace gcm { | 20 namespace gcm { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 // The first byte in an uncompressed P-256 point per SEC1 2.3.3. | 24 // The first byte in an uncompressed P-256 point per SEC1 2.3.3. |
| 26 const char kUncompressedPointForm = 0x04; | 25 const char kUncompressedPointForm = 0x04; |
| 27 | 26 |
| 28 // A P-256 field element consists of 32 bytes. | 27 // A P-256 field element consists of 32 bytes. |
| 29 const size_t kFieldBytes = 32; | 28 const size_t kFieldBytes = 32; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 private_key.data() + private_key.size()), | 105 private_key.data() + private_key.size()), |
| 107 std::vector<uint8_t>( | 106 std::vector<uint8_t>( |
| 108 public_key_x509.data(), | 107 public_key_x509.data(), |
| 109 public_key_x509.data() + public_key_x509.size()))); | 108 public_key_x509.data() + public_key_x509.size()))); |
| 110 | 109 |
| 111 if (!local_key_pair) { | 110 if (!local_key_pair) { |
| 112 DLOG(ERROR) << "Unable to create the local key pair."; | 111 DLOG(ERROR) << "Unable to create the local key pair."; |
| 113 return false; | 112 return false; |
| 114 } | 113 } |
| 115 | 114 |
| 116 crypto::ScopedEC_KEY ec_private_key( | 115 EC_KEY* ec_private_key = EVP_PKEY_get0_EC_KEY(local_key_pair->key()); |
| 117 EVP_PKEY_get1_EC_KEY(local_key_pair->key())); | 116 if (!ec_private_key || !EC_KEY_check_key(ec_private_key)) { |
| 118 | |
| 119 if (!ec_private_key || !EC_KEY_check_key(ec_private_key.get())) { | |
| 120 DLOG(ERROR) << "The private key is invalid."; | 117 DLOG(ERROR) << "The private key is invalid."; |
| 121 return false; | 118 return false; |
| 122 } | 119 } |
| 123 | 120 |
| 124 crypto::ScopedEC_POINT point( | 121 bssl::UniquePtr<EC_POINT> point( |
| 125 EC_POINT_new(EC_KEY_get0_group(ec_private_key.get()))); | 122 EC_POINT_new(EC_KEY_get0_group(ec_private_key))); |
| 126 | 123 |
| 127 if (!point || | 124 if (!point || |
| 128 !EC_POINT_oct2point(EC_KEY_get0_group(ec_private_key.get()), point.get(), | 125 !EC_POINT_oct2point( |
| 129 reinterpret_cast<const uint8_t*>( | 126 EC_KEY_get0_group(ec_private_key), point.get(), |
| 130 peer_public_key.data()), | 127 reinterpret_cast<const uint8_t*>(peer_public_key.data()), |
| 131 peer_public_key.size(), nullptr)) { | 128 peer_public_key.size(), nullptr)) { |
| 132 DLOG(ERROR) << "Can't convert peer public value to curve point."; | 129 DLOG(ERROR) << "Can't convert peer public value to curve point."; |
| 133 return false; | 130 return false; |
| 134 } | 131 } |
| 135 | 132 |
| 136 uint8_t result[kFieldBytes]; | 133 uint8_t result[kFieldBytes]; |
| 137 if (ECDH_compute_key(result, sizeof(result), point.get(), | 134 if (ECDH_compute_key(result, sizeof(result), point.get(), ec_private_key, |
| 138 ec_private_key.get(), nullptr) != sizeof(result)) { | 135 nullptr) != sizeof(result)) { |
| 139 DLOG(ERROR) << "Unable to compute the ECDH shared secret."; | 136 DLOG(ERROR) << "Unable to compute the ECDH shared secret."; |
| 140 return false; | 137 return false; |
| 141 } | 138 } |
| 142 | 139 |
| 143 out_shared_secret->assign(reinterpret_cast<char*>(result), sizeof(result)); | 140 out_shared_secret->assign(reinterpret_cast<char*>(result), sizeof(result)); |
| 144 return true; | 141 return true; |
| 145 } | 142 } |
| 146 | 143 |
| 147 } // namespace gcm | 144 } // namespace gcm |
| OLD | NEW |