hkdf.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (c) 2013 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 "crypto/hkdf.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/check.h"
  9. #include "crypto/hmac.h"
  10. #include "third_party/boringssl/src/include/openssl/digest.h"
  11. #include "third_party/boringssl/src/include/openssl/hkdf.h"
  12. namespace crypto {
  13. std::string HkdfSha256(base::StringPiece secret,
  14. base::StringPiece salt,
  15. base::StringPiece info,
  16. size_t derived_key_size) {
  17. std::string key;
  18. key.resize(derived_key_size);
  19. int result = ::HKDF(
  20. reinterpret_cast<uint8_t*>(&key[0]), derived_key_size, EVP_sha256(),
  21. reinterpret_cast<const uint8_t*>(secret.data()), secret.size(),
  22. reinterpret_cast<const uint8_t*>(salt.data()), salt.size(),
  23. reinterpret_cast<const uint8_t*>(info.data()), info.size());
  24. DCHECK(result);
  25. return key;
  26. }
  27. std::vector<uint8_t> HkdfSha256(base::span<const uint8_t> secret,
  28. base::span<const uint8_t> salt,
  29. base::span<const uint8_t> info,
  30. size_t derived_key_size) {
  31. std::vector<uint8_t> ret;
  32. ret.resize(derived_key_size);
  33. int result =
  34. ::HKDF(ret.data(), derived_key_size, EVP_sha256(), secret.data(),
  35. secret.size(), salt.data(), salt.size(), info.data(), info.size());
  36. DCHECK(result);
  37. return ret;
  38. }
  39. } // namespace crypto