nigori_test_utils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SYNC_NIGORI_NIGORI_TEST_UTILS_H_
  5. #define COMPONENTS_SYNC_NIGORI_NIGORI_TEST_UTILS_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "components/sync/engine/nigori/key_derivation_params.h"
  10. #include "components/sync/engine/nigori/nigori.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace sync_pb {
  13. class BookmarkSpecifics;
  14. class NigoriSpecifics;
  15. class EntitySpecifics;
  16. } // namespace sync_pb
  17. namespace syncer {
  18. class Cryptographer;
  19. struct KeyParamsForTesting {
  20. KeyDerivationParams derivation_params;
  21. std::string password;
  22. };
  23. // Creates KeyParamsForTesting, where |derivation_params| is Pbkdf2
  24. // KeyDerivationParams and |password| is base64 encoded |raw_key|.
  25. KeyParamsForTesting KeystoreKeyParamsForTesting(
  26. const std::vector<uint8_t>& raw_key);
  27. // Creates KeyParamsForTesting, where |derivation_params| is Pbkdf2
  28. // KeyDerivationParams and |password| is base64 encoded |raw_key|.
  29. KeyParamsForTesting TrustedVaultKeyParamsForTesting(
  30. const std::vector<uint8_t>& raw_key);
  31. // Creates KeyParamsForTesting, where |derivation_params| is Pbdf2
  32. // KeyDerivationParams and |password| is |passphrase|.
  33. KeyParamsForTesting Pbkdf2PassphraseKeyParamsForTesting(
  34. const std::string& passphrase);
  35. // Creates KeyParamsForTesting, where |derivation_params| is Scrypt
  36. // KeyDerivationParams with constant salt and |password| is |passphrase|.
  37. KeyParamsForTesting ScryptPassphraseKeyParamsForTesting(
  38. const std::string& passphrase);
  39. // Builds NigoriSpecifics with following fields:
  40. // 1. encryption_keybag contains all keys derived from |keybag_keys_params|
  41. // and encrypted with a key derived from |keybag_decryptor_params|.
  42. // 2. keystore_decryptor_token contains the key derived from
  43. // |keybag_decryptor_params| and encrypted with a key derived from
  44. // |keystore_key_params|.
  45. // 3. passphrase_type is KEYSTORE_PASSHPRASE.
  46. // 4. Other fields are default.
  47. // |keybag_keys_params| must be non-empty.
  48. sync_pb::NigoriSpecifics BuildKeystoreNigoriSpecifics(
  49. const std::vector<KeyParamsForTesting>& keybag_keys_params,
  50. const KeyParamsForTesting& keystore_decryptor_params,
  51. const KeyParamsForTesting& keystore_key_params);
  52. // Builds NigoriSpecifics with following fields:
  53. // 1. encryption_keybag contains keys derived from |trusted_vault_keys| and
  54. // encrypted with key derived from last of them.
  55. // 2. passphrase_type is TRUSTED_VAULT_PASSPHRASE.
  56. // 3. keybag_is_frozen set to true.
  57. sync_pb::NigoriSpecifics BuildTrustedVaultNigoriSpecifics(
  58. const std::vector<std::vector<uint8_t>>& trusted_vault_keys);
  59. // Creates a NigoriSpecifics that describes encryption using a custom
  60. // passphrase with the given |passphrase_key_params|. If |old_key_params| is
  61. // presented, |encryption_keybag| will also contain keys derived from it.
  62. sync_pb::NigoriSpecifics BuildCustomPassphraseNigoriSpecifics(
  63. const KeyParamsForTesting& passphrase_key_params,
  64. const absl::optional<KeyParamsForTesting>& old_key_params = absl::nullopt);
  65. // Initializes KeyDerivationParams as described in a given |nigori|. This
  66. // function will fail the test (using ADD_FAILURE/EXPECT) if the |nigori| is
  67. // not a custom passphrase one.
  68. KeyDerivationParams InitCustomPassphraseKeyDerivationParamsFromNigori(
  69. const sync_pb::NigoriSpecifics& nigori);
  70. // Given a |nigori| with CUSTOM_PASSPHRASE passphrase type, initializes the
  71. // Cryptographer with the key described in it. Since the key inside the Nigori
  72. // is encrypted (by design), the provided |passphrase| will be used to
  73. // decrypt it. This function will fail the test (using ADD_FAILURE/EXPECT) if
  74. // the |nigori| is not a custom passphrase one, or if the key cannot be
  75. // decrypted.
  76. std::unique_ptr<Cryptographer> InitCustomPassphraseCryptographerFromNigori(
  77. const sync_pb::NigoriSpecifics& nigori,
  78. const std::string& passphrase);
  79. // Returns an EntitySpecifics containing encrypted data corresponding to the
  80. // provided BookmarkSpecifics and encrypted using the given |key_params|.
  81. sync_pb::EntitySpecifics GetEncryptedBookmarkEntitySpecifics(
  82. const sync_pb::BookmarkSpecifics& specifics,
  83. const KeyParamsForTesting& key_params);
  84. } // namespace syncer
  85. #endif // COMPONENTS_SYNC_NIGORI_NIGORI_TEST_UTILS_H_