0

crypto: add some more byte-orientated versions.

This change adds byte (as opposed to std::string) based functions to
save callers some copies and casts.

Change-Id: Ib3306c6abf14c8ff849009e1752284bc693da44a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1776793
Commit-Queue: Adam Langley <agl@chromium.org>
Reviewed-by: David Benjamin <davidben@chromium.org>
Cr-Commit-Position: refs/heads/master@{#692159}
This commit is contained in:
Adam Langley
2019-08-30 19:21:06 +00:00
committed by Commit Bot
parent dbb915dcf3
commit fc2c6b5934
4 changed files with 37 additions and 4 deletions

@ -31,4 +31,17 @@ std::string HkdfSha256(base::StringPiece secret,
return key;
}
std::vector<uint8_t> HkdfSha256(base::span<const uint8_t> secret,
base::span<const uint8_t> salt,
base::span<const uint8_t> info,
size_t derived_key_size) {
std::vector<uint8_t> ret;
ret.resize(derived_key_size);
int result =
::HKDF(ret.data(), derived_key_size, EVP_sha256(), secret.data(),
secret.size(), salt.data(), salt.size(), info.data(), info.size());
DCHECK(result);
return ret;
}
} // namespace crypto

@ -9,6 +9,7 @@
#include <string>
#include "base/containers/span.h"
#include "base/strings/string_piece.h"
#include "crypto/crypto_export.h"
@ -20,6 +21,12 @@ std::string HkdfSha256(base::StringPiece secret,
base::StringPiece info,
size_t derived_key_size);
CRYPTO_EXPORT
std::vector<uint8_t> HkdfSha256(base::span<const uint8_t> secret,
base::span<const uint8_t> salt,
base::span<const uint8_t> info,
size_t derived_key_size);
} // namespace crypto
#endif // CRYPTO_HKDF_H_

@ -10,9 +10,16 @@
#include "base/stl_util.h"
#include "crypto/secure_hash.h"
#include "third_party/boringssl/src/include/openssl/sha.h"
namespace crypto {
std::array<uint8_t, kSHA256Length> SHA256Hash(base::span<const uint8_t> input) {
std::array<uint8_t, kSHA256Length> digest;
::SHA256(input.data(), input.size(), digest.data());
return digest;
}
void SHA256HashString(base::StringPiece str, void* output, size_t len) {
std::unique_ptr<SecureHash> ctx(SecureHash::Create(SecureHash::SHA256));
ctx->Update(str.data(), str.length());

@ -7,8 +7,10 @@
#include <stddef.h>
#include <array>
#include <string>
#include "base/containers/span.h"
#include "base/strings/string_piece.h"
#include "crypto/crypto_export.h"
@ -20,6 +22,14 @@ namespace crypto {
static const size_t kSHA256Length = 32; // Length in bytes of a SHA-256 hash.
// Computes the SHA-256 hash of |input|.
CRYPTO_EXPORT std::array<uint8_t, kSHA256Length> SHA256Hash(
base::span<const uint8_t> input);
// Convenience version of the above that returns the result in a 32-byte
// string.
CRYPTO_EXPORT std::string SHA256HashString(base::StringPiece str);
// Computes the SHA-256 hash of the input string 'str' and stores the first
// 'len' bytes of the hash in the output buffer 'output'. If 'len' > 32,
// only 32 bytes (the full hash) are stored in the 'output' buffer.
@ -27,10 +37,6 @@ CRYPTO_EXPORT void SHA256HashString(base::StringPiece str,
void* output,
size_t len);
// Convenience version of the above that returns the result in a 32-byte
// string.
CRYPTO_EXPORT std::string SHA256HashString(base::StringPiece str);
} // namespace crypto
#endif // CRYPTO_SHA2_H_