symmetric_key.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (c) 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.
  4. #ifndef CRYPTO_SYMMETRIC_KEY_H_
  5. #define CRYPTO_SYMMETRIC_KEY_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include "build/build_config.h"
  10. #include "crypto/crypto_export.h"
  11. namespace crypto {
  12. // Wraps a platform-specific symmetric key and allows it to be held in a
  13. // scoped_ptr.
  14. class CRYPTO_EXPORT SymmetricKey {
  15. public:
  16. // Defines the algorithm that a key will be used with. See also
  17. // classs Encrptor.
  18. enum Algorithm {
  19. AES,
  20. HMAC_SHA1,
  21. };
  22. SymmetricKey(const SymmetricKey&) = delete;
  23. SymmetricKey& operator=(const SymmetricKey&) = delete;
  24. virtual ~SymmetricKey();
  25. // Generates a random key suitable to be used with |algorithm| and of
  26. // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8.
  27. // The caller is responsible for deleting the returned SymmetricKey.
  28. static std::unique_ptr<SymmetricKey> GenerateRandomKey(
  29. Algorithm algorithm,
  30. size_t key_size_in_bits);
  31. // Derives a key from the supplied password and salt using PBKDF2, suitable
  32. // for use with specified |algorithm|. Note |algorithm| is not the algorithm
  33. // used to derive the key from the password. |key_size_in_bits| must be a
  34. // multiple of 8. The caller is responsible for deleting the returned
  35. // SymmetricKey.
  36. static std::unique_ptr<SymmetricKey> DeriveKeyFromPasswordUsingPbkdf2(
  37. Algorithm algorithm,
  38. const std::string& password,
  39. const std::string& salt,
  40. size_t iterations,
  41. size_t key_size_in_bits);
  42. // Derives a key from the supplied password and salt using scrypt, suitable
  43. // for use with specified |algorithm|. Note |algorithm| is not the algorithm
  44. // used to derive the key from the password. |cost_parameter|, |block_size|,
  45. // and |parallelization_parameter| correspond to the parameters |N|, |r|, and
  46. // |p| from the scrypt specification (see RFC 7914). |key_size_in_bits| must
  47. // be a multiple of 8. The caller is responsible for deleting the returned
  48. // SymmetricKey.
  49. static std::unique_ptr<SymmetricKey> DeriveKeyFromPasswordUsingScrypt(
  50. Algorithm algorithm,
  51. const std::string& password,
  52. const std::string& salt,
  53. size_t cost_parameter,
  54. size_t block_size,
  55. size_t parallelization_parameter,
  56. size_t max_memory_bytes,
  57. size_t key_size_in_bits);
  58. // Imports an array of key bytes in |raw_key|. This key may have been
  59. // generated by GenerateRandomKey or DeriveKeyFromPassword{Pbkdf2,Scrypt} and
  60. // exported with key(). The key must be of suitable size for use with
  61. // |algorithm|. The caller owns the returned SymmetricKey.
  62. static std::unique_ptr<SymmetricKey> Import(Algorithm algorithm,
  63. const std::string& raw_key);
  64. // Returns the raw platform specific key data.
  65. const std::string& key() const { return key_; }
  66. private:
  67. SymmetricKey();
  68. std::string key_;
  69. };
  70. } // namespace crypto
  71. #endif // CRYPTO_SYMMETRIC_KEY_H_