Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / sync / util / nigori.h
blobcd67bf9ec47a4aba00b2ed1ad730fc58b1f79918
1 // Copyright 2012 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.
5 #ifndef SYNC_UTIL_NIGORI_H_
6 #define SYNC_UTIL_NIGORI_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "sync/base/sync_export.h"
13 namespace crypto {
14 class SymmetricKey;
15 } // namespace crypto
17 namespace syncer {
19 // A (partial) implementation of Nigori, a protocol to securely store secrets in
20 // the cloud. This implementation does not support server authentication or
21 // assisted key derivation.
23 // To store secrets securely, use the |Permute| method to derive a lookup name
24 // for your secret (basically a map key), and |Encrypt| and |Decrypt| to store
25 // and retrieve the secret.
27 // TODO: Link to doc.
28 class SYNC_EXPORT Nigori {
29 public:
30 enum Type {
31 Password = 1,
34 Nigori();
35 virtual ~Nigori();
37 // Initialize the client with the given |hostname|, |username| and |password|.
38 bool InitByDerivation(const std::string& hostname,
39 const std::string& username,
40 const std::string& password);
42 // Initialize the client by importing the given keys instead of deriving new
43 // ones.
44 bool InitByImport(const std::string& user_key,
45 const std::string& encryption_key,
46 const std::string& mac_key);
48 // Derives a secure lookup name from |type| and |name|. If |hostname|,
49 // |username| and |password| are kept constant, a given |type| and |name| pair
50 // always yields the same |permuted| value. Note that |permuted| will be
51 // Base64 encoded.
52 bool Permute(Type type, const std::string& name, std::string* permuted) const;
54 // Encrypts |value|. Note that on success, |encrypted| will be Base64
55 // encoded.
56 bool Encrypt(const std::string& value, std::string* encrypted) const;
58 // Decrypts |value| into |decrypted|. It is assumed that |value| is Base64
59 // encoded.
60 bool Decrypt(const std::string& value, std::string* decrypted) const;
62 // Exports the raw derived keys.
63 bool ExportKeys(std::string* user_key,
64 std::string* encryption_key,
65 std::string* mac_key) const;
67 static const char kSaltSalt[]; // The salt used to derive the user salt.
68 static const size_t kSaltKeySizeInBits = 128;
69 static const size_t kDerivedKeySizeInBits = 128;
70 static const size_t kIvSize = 16;
71 static const size_t kHashSize = 32;
73 static const size_t kSaltIterations = 1001;
74 static const size_t kUserIterations = 1002;
75 static const size_t kEncryptionIterations = 1003;
76 static const size_t kSigningIterations = 1004;
78 private:
79 scoped_ptr<crypto::SymmetricKey> user_key_;
80 scoped_ptr<crypto::SymmetricKey> encryption_key_;
81 scoped_ptr<crypto::SymmetricKey> mac_key_;
84 } // namespace syncer
86 #endif // SYNC_UTIL_NIGORI_H_