File: nigori_test_utils.h

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 (137 lines) | stat: -rw-r--r-- 6,027 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
// 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.

#ifndef COMPONENTS_SYNC_TEST_NIGORI_TEST_UTILS_H_
#define COMPONENTS_SYNC_TEST_NIGORI_TEST_UTILS_H_

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/time/time.h"
#include "components/sync/engine/nigori/cross_user_sharing_public_key.h"
#include "components/sync/engine/nigori/key_derivation_params.h"
#include "components/sync/engine/nigori/nigori.h"
#include "components/sync/nigori/cross_user_sharing_keys.h"

namespace sync_pb {

class BookmarkSpecifics;
class NigoriSpecifics;
class EntitySpecifics;

}  // namespace sync_pb

namespace syncer {

class Cryptographer;

struct KeyParamsForTesting {
  KeyDerivationParams derivation_params;
  std::string password;
};

// Creates KeyParamsForTesting, where `derivation_params` is Pbkdf2
// KeyDerivationParams and `password` is base64 encoded `raw_key`.
KeyParamsForTesting KeystoreKeyParamsForTesting(
    const std::vector<uint8_t>& raw_key);

// Creates KeyParamsForTesting, where `derivation_params` is Pbkdf2
// KeyDerivationParams and `password` is base64 encoded `raw_key`.
KeyParamsForTesting TrustedVaultKeyParamsForTesting(
    const std::vector<uint8_t>& raw_key);

// Creates KeyParamsForTesting, where `derivation_params` is Pbdf2
// KeyDerivationParams and `password` is `passphrase`.
KeyParamsForTesting Pbkdf2PassphraseKeyParamsForTesting(
    const std::string& passphrase);

// Creates KeyParamsForTesting, where `derivation_params` is Scrypt
// KeyDerivationParams with constant salt and `password` is `passphrase`.
KeyParamsForTesting ScryptPassphraseKeyParamsForTesting(
    const std::string& passphrase);

// Builds NigoriSpecifics with following fields:
// 1. encryption_keybag contains all keys derived from `keybag_keys_params`
// and encrypted with a key derived from `keystore_decryptor_params`.
// 2. keystore_decryptor_token contains the key derived from
// `keystore_decryptor_params` and encrypted with a key derived from
// `keystore_key_params`.
// 3. passphrase_type is KEYSTORE_PASSHPRASE.
// 4. Other fields are default.
// `keybag_keys_params` must be non-empty.
// `cross_user_sharing_keys` can be empty and contains cross user sharing keys.
sync_pb::NigoriSpecifics BuildKeystoreNigoriSpecifics(
    const std::vector<KeyParamsForTesting>& keybag_keys_params,
    const KeyParamsForTesting& keystore_decryptor_params,
    const KeyParamsForTesting& keystore_key_params,
    const CrossUserSharingKeys& cross_user_sharing_keys =
        CrossUserSharingKeys::CreateEmpty());

// Builds NigoriSpecifics with following fields:
// 1. encryption_keybag contains all keys derived from `keybag_keys_params`
// and encrypted with a key derived from `keystore_decryptor_params`.
// 2. keystore_decryptor_token contains the key derived from
// `keystore_decryptor_params` and encrypted with a key derived from
// `keystore_key_params`.
// 3. passphrase_type is KEYSTORE_PASSHPRASE.
// 4. Other fields are default.
// `keybag_keys_params` must be non-empty.
// `cross_user_sharing_keys` can be empty and contains cross user sharing keys.
// `cross_user_sharing_public_key` is the public to register.
// `cross_user_sharing_public_key_version` is the associated version of the
// public key to register.
sync_pb::NigoriSpecifics BuildKeystoreNigoriSpecificsWithCrossUserSharingKeys(
    const std::vector<KeyParamsForTesting>& keybag_keys_params,
    const KeyParamsForTesting& keystore_decryptor_params,
    const KeyParamsForTesting& keystore_key_params,
    const CrossUserSharingKeys& cross_user_sharing_keys,
    const CrossUserSharingPublicKey& cross_user_sharing_public_key,
    const uint32_t cross_user_sharing_public_key_version);

// Builds NigoriSpecifics with following fields:
// 1. encryption_keybag contains keys derived from `trusted_vault_keys` and
// encrypted with key derived from last of them.
// 2. passphrase_type is TRUSTED_VAULT_PASSPHRASE.
// 3. keybag_is_frozen set to true.
//
// `migration_time` allows the caller to specify a trusted vault migration time
// as represented in `TrustedVaultDebugInfo`.
sync_pb::NigoriSpecifics BuildTrustedVaultNigoriSpecifics(
    const std::vector<std::vector<uint8_t>>& trusted_vault_keys,
    base::Time migration_time = base::Time::UnixEpoch());

// Creates a NigoriSpecifics that describes encryption using a custom
// passphrase with the given `passphrase_key_params`. If `old_key_params` is
// presented, `encryption_keybag` will also contain keys derived from it.
sync_pb::NigoriSpecifics BuildCustomPassphraseNigoriSpecifics(
    const KeyParamsForTesting& passphrase_key_params,
    const std::optional<KeyParamsForTesting>& old_key_params = std::nullopt);

// Initializes KeyDerivationParams as described in a given `nigori`. This
// function will fail the test (using ADD_FAILURE/EXPECT) if the `nigori` is
// not a custom passphrase one.
KeyDerivationParams InitCustomPassphraseKeyDerivationParamsFromNigori(
    const sync_pb::NigoriSpecifics& nigori);

// Given a `nigori` with CUSTOM_PASSPHRASE passphrase type, initializes the
// Cryptographer with the key described in it. Since the key inside the Nigori
// is encrypted (by design), the provided `passphrase` will be used to
// decrypt it. This function will fail the test (using ADD_FAILURE/EXPECT) if
// the `nigori` is not a custom passphrase one, or if the key cannot be
// decrypted.
std::unique_ptr<Cryptographer> InitCustomPassphraseCryptographerFromNigori(
    const sync_pb::NigoriSpecifics& nigori,
    const std::string& passphrase);

// Returns an EntitySpecifics containing encrypted data corresponding to the
// provided BookmarkSpecifics and encrypted using the given `key_params`.
sync_pb::EntitySpecifics GetEncryptedBookmarkEntitySpecifics(
    const sync_pb::BookmarkSpecifics& specifics,
    const KeyParamsForTesting& key_params);

}  // namespace syncer

#endif  // COMPONENTS_SYNC_TEST_NIGORI_TEST_UTILS_H_