chromium/components/trusted_vault/securebox.cc

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/trusted_vault/securebox.h"

#include <algorithm>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/check_op.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/ranges/algorithm.h"
#include "crypto/hkdf.h"
#include "crypto/openssl_util.h"
#include "crypto/random.h"
#include "third_party/boringssl/src/include/openssl/aead.h"
#include "third_party/boringssl/src/include/openssl/bn.h"
#include "third_party/boringssl/src/include/openssl/ec.h"
#include "third_party/boringssl/src/include/openssl/ecdh.h"
#include "third_party/boringssl/src/include/openssl/nid.h"

namespace trusted_vault {

namespace {

const size_t kP256FieldBytes =;
const size_t kAES128KeyLength =;
const size_t kNonceLength =;
const size_t kTagLength =;
const size_t kECPrivateKeyLength =;
const size_t kECPointLength =;
const size_t kVersionLength =;
const uint8_t kSecureBoxVersion[] =;
const uint8_t kHkdfSalt[] =;
const char kHkdfInfoWithPublicKey[] =;
const char kHkdfInfoWithoutPublicKey[] =;

// Returns bytes representation of |str| (without trailing \0).
base::span<const uint8_t> StringToBytes(std::string_view str) {}

// Concatenates spans in |bytes_spans|.
std::vector<uint8_t> ConcatBytes(
    const std::vector<base::span<const uint8_t>>& bytes_spans) {}

// Creates public EC_KEY from |public_key_bytes|. |public_key_bytes| must be
// a X9.62 formatted NIST P-256 point.
bssl::UniquePtr<EC_KEY> ECPublicKeyFromBytes(
    base::span<const uint8_t> public_key_bytes,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

// Writes |key| point into |output| using X9.62 format.
std::vector<uint8_t> ECPublicKeyToBytes(
    const EC_KEY* key,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

bssl::UniquePtr<EC_KEY> GenerateECKey(
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

// Computes a 16-byte shared AES-GCM secret. If |private_key| is not null, first
// computes the EC-DH secret. Appends the |shared_secret|, and computes HKDF of
// that. |public_key| and |private_key| might be null, but if either of them is
// not null, other must be not null as well. |shared_secret| may be empty.
std::vector<uint8_t> SecureBoxComputeSecret(
    const EC_KEY* private_key,
    const EC_POINT* public_key,
    base::span<const uint8_t> shared_secret,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

// This function implements AES-GCM, using AES-128, a 96-bit nonce, and 128-bit
// tag.
std::vector<uint8_t> SecureBoxAesGcmEncrypt(
    base::span<const uint8_t> secret_key,
    base::span<const uint8_t> nonce,
    base::span<const uint8_t> plaintext,
    base::span<const uint8_t> associated_data,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

// Decrypts using AES-GCM.
std::optional<std::vector<uint8_t>> SecureBoxAesGcmDecrypt(
    base::span<const uint8_t> secret_key,
    base::span<const uint8_t> nonce,
    base::span<const uint8_t> ciphertext,
    base::span<const uint8_t> associated_data,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

// Creates NIST P-256 EC_KEY given NIST P-256 point multiplier in padded
// big-endian format. Returns nullptr if P-256 key can't be derived using
// |key_bytes| or its format is incorrect.
bssl::UniquePtr<EC_KEY> ImportECPrivateKey(
    base::span<const uint8_t> key_bytes,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

// |our_key_pair| and |their_public_key| might be null, but if either of them is
// not null, other must be not null as well. |shared_secret|, |header| and
// |payload| may be empty.
std::vector<uint8_t> SecureBoxEncryptImpl(
    const EC_KEY* our_key_pair,
    const EC_POINT* their_public_key,
    base::span<const uint8_t> shared_secret,
    base::span<const uint8_t> header,
    base::span<const uint8_t> payload,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

// |our_private_key| may be null. |shared_secret|, |header| and |payload| may be
// empty. Returns nullopt if decryption failed.
std::optional<std::vector<uint8_t>> SecureBoxDecryptImpl(
    const EC_KEY* our_private_key,
    base::span<const uint8_t> shared_secret,
    base::span<const uint8_t> header,
    base::span<const uint8_t> encrypted_payload) {}

}  // namespace

std::vector<uint8_t> SecureBoxSymmetricEncrypt(
    base::span<const uint8_t> shared_secret,
    base::span<const uint8_t> header,
    base::span<const uint8_t> payload) {}

std::optional<std::vector<uint8_t>> SecureBoxSymmetricDecrypt(
    base::span<const uint8_t> shared_secret,
    base::span<const uint8_t> header,
    base::span<const uint8_t> encrypted_payload) {}

// static
std::unique_ptr<SecureBoxPublicKey> SecureBoxPublicKey::CreateByImport(
    base::span<const uint8_t> key_bytes) {}

// static
std::unique_ptr<SecureBoxPublicKey> SecureBoxPublicKey::CreateInternal(
    bssl::UniquePtr<EC_KEY> key,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

SecureBoxPublicKey::SecureBoxPublicKey(
    bssl::UniquePtr<EC_KEY> key,
    const crypto::OpenSSLErrStackTracer& err_tracer)
    :{}

SecureBoxPublicKey::~SecureBoxPublicKey() = default;

std::vector<uint8_t> SecureBoxPublicKey::ExportToBytes() const {}

std::vector<uint8_t> SecureBoxPublicKey::Encrypt(
    base::span<const uint8_t> shared_secret,
    base::span<const uint8_t> header,
    base::span<const uint8_t> payload) const {}

// static
std::unique_ptr<SecureBoxPrivateKey> SecureBoxPrivateKey::CreateByImport(
    base::span<const uint8_t> key_bytes) {}

// static
std::unique_ptr<SecureBoxPrivateKey> SecureBoxPrivateKey::CreateInternal(
    bssl::UniquePtr<EC_KEY> key,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

SecureBoxPrivateKey::SecureBoxPrivateKey(
    bssl::UniquePtr<EC_KEY> key,
    const crypto::OpenSSLErrStackTracer& error_tracer)
    :{}

SecureBoxPrivateKey::~SecureBoxPrivateKey() = default;

std::vector<uint8_t> SecureBoxPrivateKey::ExportToBytes() const {}

std::optional<std::vector<uint8_t>> SecureBoxPrivateKey::Decrypt(
    base::span<const uint8_t> shared_secret,
    base::span<const uint8_t> header,
    base::span<const uint8_t> encrypted_payload) const {}

// static
std::unique_ptr<SecureBoxKeyPair> SecureBoxKeyPair::GenerateRandom() {}

// static
std::unique_ptr<SecureBoxKeyPair> SecureBoxKeyPair::CreateByPrivateKeyImport(
    base::span<const uint8_t> private_key_bytes) {}

SecureBoxKeyPair::SecureBoxKeyPair(
    bssl::UniquePtr<EC_KEY> private_ec_key,
    const crypto::OpenSSLErrStackTracer& err_tracer) {}

SecureBoxKeyPair::~SecureBoxKeyPair() = default;

}  // namespace trusted_vault