p256_key_util.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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
  3. // found in the LICENSE file.
  4. #include "components/gcm_driver/crypto/p256_key_util.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/logging.h"
  10. #include "base/strings/string_util.h"
  11. #include "crypto/ec_private_key.h"
  12. #include "third_party/boringssl/src/include/openssl/ec.h"
  13. #include "third_party/boringssl/src/include/openssl/ecdh.h"
  14. #include "third_party/boringssl/src/include/openssl/evp.h"
  15. namespace gcm {
  16. namespace {
  17. // A P-256 field element consists of 32 bytes.
  18. const size_t kFieldBytes = 32;
  19. // A P-256 point in uncompressed form consists of 0x04 (to denote that the point
  20. // is uncompressed per SEC1 2.3.3) followed by two, 32-byte field elements.
  21. const size_t kUncompressedPointBytes = 1 + 2 * kFieldBytes;
  22. } // namespace
  23. bool GetRawPublicKey(const crypto::ECPrivateKey& key, std::string* public_key) {
  24. DCHECK(public_key);
  25. std::string candidate_public_key;
  26. // ECPrivateKey::ExportRawPublicKey() returns the EC point in the uncompressed
  27. // point format.
  28. if (!key.ExportRawPublicKey(&candidate_public_key) ||
  29. candidate_public_key.size() != kUncompressedPointBytes) {
  30. DLOG(ERROR) << "Unable to export the public key.";
  31. return false;
  32. }
  33. public_key->erase();
  34. public_key->reserve(kUncompressedPointBytes);
  35. public_key->append(candidate_public_key);
  36. return true;
  37. }
  38. // TODO(peter): Get rid of this once all key management code has been updated
  39. // to use ECPrivateKey instead of std::string.
  40. bool GetRawPrivateKey(const crypto::ECPrivateKey& key,
  41. std::string* private_key) {
  42. DCHECK(private_key);
  43. std::vector<uint8_t> private_key_vector;
  44. if (!key.ExportPrivateKey(&private_key_vector))
  45. return false;
  46. private_key->assign(private_key_vector.begin(), private_key_vector.end());
  47. return true;
  48. }
  49. bool ComputeSharedP256Secret(crypto::ECPrivateKey& key,
  50. const base::StringPiece& peer_public_key,
  51. std::string* out_shared_secret) {
  52. DCHECK(out_shared_secret);
  53. EC_KEY* ec_private_key = EVP_PKEY_get0_EC_KEY(key.key());
  54. if (!ec_private_key || !EC_KEY_check_key(ec_private_key)) {
  55. DLOG(ERROR) << "The private key is invalid.";
  56. return false;
  57. }
  58. bssl::UniquePtr<EC_POINT> point(
  59. EC_POINT_new(EC_KEY_get0_group(ec_private_key)));
  60. if (!point || !EC_POINT_oct2point(
  61. EC_KEY_get0_group(ec_private_key), point.get(),
  62. reinterpret_cast<const uint8_t*>(peer_public_key.data()),
  63. peer_public_key.size(), nullptr)) {
  64. DLOG(ERROR) << "Can't convert peer public value to curve point.";
  65. return false;
  66. }
  67. uint8_t result[kFieldBytes];
  68. if (ECDH_compute_key(result, sizeof(result), point.get(), ec_private_key,
  69. nullptr) != sizeof(result)) {
  70. DLOG(ERROR) << "Unable to compute the ECDH shared secret.";
  71. return false;
  72. }
  73. out_shared_secret->assign(reinterpret_cast<char*>(result), sizeof(result));
  74. return true;
  75. }
  76. } // namespace gcm