File: fake_cryptographer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (140 lines) | stat: -rw-r--r-- 4,498 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2021 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/sync/test/fake_cryptographer.h"

#include <algorithm>
#include <iterator>

#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "components/sync/protocol/encryption.pb.h"

namespace syncer {

namespace {

const char kSeparator[] = "@@";

}  // namespace

// static
std::unique_ptr<FakeCryptographer> FakeCryptographer::FromSingleDefaultKey(
    const std::string& key_name) {
  auto cryptographer = std::make_unique<FakeCryptographer>();
  cryptographer->AddEncryptionKey(key_name);
  cryptographer->SelectDefaultEncryptionKey(key_name);
  return cryptographer;
}

FakeCryptographer::FakeCryptographer() = default;

FakeCryptographer::~FakeCryptographer() = default;

void FakeCryptographer::AddEncryptionKey(const std::string& key_name) {
  DCHECK(!key_name.empty());
  known_key_names_.insert(key_name);
}

void FakeCryptographer::SelectDefaultEncryptionKey(
    const std::string& key_name) {
  DCHECK(known_key_names_.contains(key_name));
  default_key_name_ = key_name;
}

void FakeCryptographer::ClearDefaultEncryptionKey() {
  default_key_name_.clear();
}

bool FakeCryptographer::CanEncrypt() const {
  return !default_key_name_.empty();
}

bool FakeCryptographer::CanDecrypt(
    const sync_pb::EncryptedData& encrypted) const {
  return known_key_names_.contains(encrypted.key_name());
}

std::string FakeCryptographer::GetDefaultEncryptionKeyName() const {
  return default_key_name_;
}

bool FakeCryptographer::EncryptString(const std::string& decrypted,
                                      sync_pb::EncryptedData* encrypted) const {
  if (default_key_name_.empty()) {
    return false;
  }
  *encrypted->mutable_key_name() = default_key_name_;
  *encrypted->mutable_blob() =
      base::StrCat({default_key_name_, kSeparator, decrypted});
  return true;
}

bool FakeCryptographer::DecryptToString(const sync_pb::EncryptedData& encrypted,
                                        std::string* decrypted) const {
  auto key_iter = std::ranges::find(known_key_names_, encrypted.key_name());
  if (key_iter == known_key_names_.end()) {
    return false;
  }

  std::string key_name_and_separator = base::StrCat({*key_iter, kSeparator});
  if (!base::StartsWith(encrypted.blob(), key_name_and_separator)) {
    // Corrupted blob.
    return false;
  }

  *decrypted =
      std::string(encrypted.blob().begin() + key_name_and_separator.size(),
                  encrypted.blob().end());
  return true;
}

const CrossUserSharingPublicPrivateKeyPair&
FakeCryptographer::GetCrossUserSharingKeyPair(uint32_t version) const {
  return cross_user_sharing_key_pair_;
}

std::optional<std::vector<uint8_t>>
FakeCryptographer::AuthEncryptForCrossUserSharing(
    base::span<const uint8_t> plaintext,
    base::span<const uint8_t> recipient_public_key) const {
  // Just join two parts of the data. Note that sender's private key is omitted
  // here for simplicity.
  std::vector<uint8_t> result;
  result.reserve(plaintext.size() + recipient_public_key.size());
  std::ranges::copy(recipient_public_key, std::back_inserter(result));
  std::ranges::copy(plaintext, std::back_inserter(result));
  return result;
}

std::optional<std::vector<uint8_t>>
FakeCryptographer::AuthDecryptForCrossUserSharing(
    base::span<const uint8_t> encrypted_data,
    base::span<const uint8_t> sender_public_key,
    const uint32_t recipient_key_version) const {
  // `encrypted_data` is expected to contain receiver's public key as a prefix,
  // and the actual data. `sender_public_key` is not used for simplicity. In
  // real case, sender's private key is used during encryption and sender's
  // public key during decryption.
  if (encrypted_data.size() <
      cross_user_sharing_key_pair_.GetRawPublicKey().size()) {
    return std::nullopt;
  }

  // Verify that the prefix contains an expected public key.
  if (std::ranges::equal(
          cross_user_sharing_key_pair_.GetRawPublicKey().begin(),
          cross_user_sharing_key_pair_.GetRawPublicKey().end(),
          encrypted_data.begin(),
          encrypted_data.begin() +
              cross_user_sharing_key_pair_.GetRawPublicKey().size())) {
    return std::nullopt;
  }
  return std::vector<uint8_t>(
      encrypted_data.begin() +
          cross_user_sharing_key_pair_.GetRawPublicKey().size(),
      encrypted_data.end());
}

}  // namespace syncer