67 lines
2.2 KiB
C++
67 lines
2.2 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
// Copyright 2018 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.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "common/signature_util.h"
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include "util/status.h"
|
|
#include "common/aes_cbc_util.h"
|
|
#include "common/rsa_key.h"
|
|
#include "common/sha_util.h"
|
|
|
|
namespace widevine {
|
|
namespace signature_util {
|
|
|
|
util::Status GenerateAesSignature(const std::string& message, const std::string& aes_key,
|
|
const std::string& aes_iv, std::string* signature) {
|
|
if (signature == nullptr) {
|
|
return util::Status(util::error::INVALID_ARGUMENT, "signature is nullptr");
|
|
}
|
|
std::string hash = Sha1_Hash(message);
|
|
if (hash.empty()) {
|
|
return util::Status(util::error::INTERNAL, "Computed hash is empty");
|
|
}
|
|
std::string sig = crypto_util::EncryptAesCbc(aes_key, aes_iv, hash);
|
|
if (sig.empty()) {
|
|
return util::Status(util::error::INTERNAL,
|
|
"Computed AES signature is empty");
|
|
}
|
|
*signature = sig;
|
|
return util::OkStatus();
|
|
}
|
|
|
|
util::Status GenerateRsaSignature(const std::string& message,
|
|
const std::string& private_key,
|
|
std::string* signature) {
|
|
if (signature == nullptr) {
|
|
return util::Status(util::error::INVALID_ARGUMENT, "signature is nullptr");
|
|
}
|
|
std::unique_ptr<RsaPrivateKey> rsa_private_key(
|
|
RsaPrivateKey::Create(private_key));
|
|
if (rsa_private_key == nullptr) {
|
|
return util::Status(util::error::INTERNAL,
|
|
"Failed to construct a RsaPrivateKey");
|
|
}
|
|
std::string sig;
|
|
if (!rsa_private_key->GenerateSignature(message, &sig)) {
|
|
return util::Status(util::error::INTERNAL,
|
|
"Failed to generate a RSA signature");
|
|
}
|
|
if (sig.empty()) {
|
|
return util::Status(util::error::INTERNAL,
|
|
"Computed RSA signature is empty");
|
|
}
|
|
*signature = sig;
|
|
return util::OkStatus();
|
|
}
|
|
|
|
} // namespace signature_util
|
|
} // namespace widevine
|