Skip to content
Permalink
Browse files

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}
  • Loading branch information
Adam Langley Commit Bot
Adam Langley authored and Commit Bot committed Aug 30, 2019
1 parent dbb915d commit fc2c6b5934aa3bf865afd1f3e28e060fefc8071e
Showing with 37 additions and 4 deletions.
  1. +13 −0 crypto/hkdf.cc
  2. +7 −0 crypto/hkdf.h
  3. +7 −0 crypto/sha2.cc
  4. +10 −4 crypto/sha2.h
@@ -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,17 +22,21 @@ 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.
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_

0 comments on commit fc2c6b5

Please sign in to comment.
You can’t perform that action at this time.