passphrase_enums.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2016 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. #include "components/sync/base/passphrase_enums.h"
  5. #include "base/check_op.h"
  6. #include "base/notreached.h"
  7. namespace syncer {
  8. bool IsExplicitPassphrase(PassphraseType type) {
  9. switch (type) {
  10. case PassphraseType::kImplicitPassphrase:
  11. case PassphraseType::kKeystorePassphrase:
  12. case PassphraseType::kTrustedVaultPassphrase:
  13. return false;
  14. case PassphraseType::kFrozenImplicitPassphrase:
  15. case PassphraseType::kCustomPassphrase:
  16. return true;
  17. }
  18. NOTREACHED();
  19. return false;
  20. }
  21. sync_pb::NigoriSpecifics::PassphraseType ProtoPassphraseInt32ToProtoEnum(
  22. ::google::protobuf::int32 type) {
  23. return sync_pb::NigoriSpecifics::PassphraseType_IsValid(type)
  24. ? static_cast<sync_pb::NigoriSpecifics::PassphraseType>(type)
  25. : sync_pb::NigoriSpecifics::UNKNOWN;
  26. }
  27. absl::optional<PassphraseType> ProtoPassphraseInt32ToEnum(
  28. ::google::protobuf::int32 type) {
  29. switch (ProtoPassphraseInt32ToProtoEnum(type)) {
  30. case sync_pb::NigoriSpecifics::IMPLICIT_PASSPHRASE:
  31. return PassphraseType::kImplicitPassphrase;
  32. case sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE:
  33. return PassphraseType::kKeystorePassphrase;
  34. case sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE:
  35. return PassphraseType::kCustomPassphrase;
  36. case sync_pb::NigoriSpecifics::FROZEN_IMPLICIT_PASSPHRASE:
  37. return PassphraseType::kFrozenImplicitPassphrase;
  38. case sync_pb::NigoriSpecifics::TRUSTED_VAULT_PASSPHRASE:
  39. return PassphraseType::kTrustedVaultPassphrase;
  40. case sync_pb::NigoriSpecifics::UNKNOWN:
  41. // This must be an unknown value coming from future versions or a field
  42. // actually being populated with UNKNOWN (which is a protocol violation).
  43. break;
  44. }
  45. return absl::nullopt;
  46. }
  47. sync_pb::NigoriSpecifics::PassphraseType EnumPassphraseTypeToProto(
  48. PassphraseType type) {
  49. switch (type) {
  50. case PassphraseType::kImplicitPassphrase:
  51. return sync_pb::NigoriSpecifics::IMPLICIT_PASSPHRASE;
  52. case PassphraseType::kKeystorePassphrase:
  53. return sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE;
  54. case PassphraseType::kCustomPassphrase:
  55. return sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE;
  56. case PassphraseType::kFrozenImplicitPassphrase:
  57. return sync_pb::NigoriSpecifics::FROZEN_IMPLICIT_PASSPHRASE;
  58. case PassphraseType::kTrustedVaultPassphrase:
  59. return sync_pb::NigoriSpecifics::TRUSTED_VAULT_PASSPHRASE;
  60. }
  61. NOTREACHED();
  62. return sync_pb::NigoriSpecifics::IMPLICIT_PASSPHRASE;
  63. }
  64. KeyDerivationMethod ProtoKeyDerivationMethodToEnum(
  65. ::google::protobuf::int32 method) {
  66. DCHECK_GE(method, 0);
  67. switch (method) {
  68. case sync_pb::NigoriSpecifics::UNSPECIFIED:
  69. // This is the default value; it comes from an old client (<M70) which
  70. // does not know about this field. These old clients all use PBKDF2.
  71. return KeyDerivationMethod::PBKDF2_HMAC_SHA1_1003;
  72. case sync_pb::NigoriSpecifics::PBKDF2_HMAC_SHA1_1003:
  73. return KeyDerivationMethod::PBKDF2_HMAC_SHA1_1003;
  74. case sync_pb::NigoriSpecifics::SCRYPT_8192_8_11:
  75. return KeyDerivationMethod::SCRYPT_8192_8_11;
  76. }
  77. // We do not know about this value. It is likely a method added in a newer
  78. // version of Chrome.
  79. return KeyDerivationMethod::UNSUPPORTED;
  80. }
  81. sync_pb::NigoriSpecifics::KeyDerivationMethod EnumKeyDerivationMethodToProto(
  82. KeyDerivationMethod method) {
  83. switch (method) {
  84. case KeyDerivationMethod::PBKDF2_HMAC_SHA1_1003:
  85. return sync_pb::NigoriSpecifics::PBKDF2_HMAC_SHA1_1003;
  86. case KeyDerivationMethod::SCRYPT_8192_8_11:
  87. return sync_pb::NigoriSpecifics::SCRYPT_8192_8_11;
  88. case KeyDerivationMethod::UNSUPPORTED:
  89. // This value does not have a counterpart in the protocol proto enum,
  90. // because it is just a client side abstraction.
  91. break;
  92. }
  93. NOTREACHED();
  94. return sync_pb::NigoriSpecifics::UNSPECIFIED;
  95. }
  96. } // namespace syncer