79 lines
2.4 KiB
C++
79 lines
2.4 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
// Copyright 2019 Google LLC.
|
|
//
|
|
// This software is licensed under the terms defined in the Widevine Master
|
|
// License Agreement. For a copy of this agreement, please contact
|
|
// widevine-licensing@google.com.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Description:
|
|
// Implementation of fake EC key generator. Returns precomputed test keys.
|
|
|
|
#include "common/fake_ec_key_source.h"
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include "glog/logging.h"
|
|
#include "common/ec_key.h"
|
|
#include "common/ec_test_keys.h"
|
|
|
|
namespace widevine {
|
|
|
|
FakeECKeySource::FakeECKeySource() {
|
|
ECTestKeys ec_test_keys;
|
|
CHECK(SetKey(ECPrivateKey::SECP256R1,
|
|
ec_test_keys.private_test_key_1_secp256r1(),
|
|
ec_test_keys.public_test_key_1_secp256r1()));
|
|
|
|
CHECK(SetKey(ECPrivateKey::SECP384R1,
|
|
ec_test_keys.private_test_key_1_secp384r1(),
|
|
ec_test_keys.public_test_key_1_secp384r1()));
|
|
|
|
CHECK(SetKey(ECPrivateKey::SECP521R1,
|
|
ec_test_keys.private_test_key_1_secp521r1(),
|
|
ec_test_keys.public_test_key_1_secp521r1()));
|
|
}
|
|
|
|
bool FakeECKeySource::SetKey(ECPrivateKey::EllipticCurve curve,
|
|
const std::string& private_key,
|
|
const std::string& public_key) {
|
|
std::unique_ptr<ECPrivateKey> pri_key = ECPrivateKey::Create(private_key);
|
|
std::unique_ptr<ECPublicKey> pub_key = ECPublicKey::Create(public_key);
|
|
if (pub_key == nullptr || pri_key == nullptr) {
|
|
LOG(ERROR) << "public key or private key was null.";
|
|
return false;
|
|
}
|
|
if (!pri_key->MatchesPublicKey(*pub_key)) {
|
|
LOG(ERROR) << "Public and private keys do not match.";
|
|
return false;
|
|
}
|
|
if (pri_key->Curve() != curve) {
|
|
LOG(ERROR) << "Curve does not match specified curve.";
|
|
return false;
|
|
}
|
|
|
|
test_keys_[curve] = std::make_pair(private_key, public_key);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool FakeECKeySource::GetECKey(ECPrivateKey::EllipticCurve curve,
|
|
std::string* private_key,
|
|
std::string* public_key) {
|
|
CHECK(private_key);
|
|
CHECK(public_key);
|
|
|
|
const auto keys = test_keys_.find(curve);
|
|
if (keys == test_keys_.end()) {
|
|
return false;
|
|
}
|
|
|
|
std::tie(*private_key, *public_key) = keys->second;
|
|
|
|
return true;
|
|
}
|
|
|
|
} // namespace widevine
|