Skip to content

Commit

Permalink
Replace base::StringPiece with std::string_view in //crypto
Browse files Browse the repository at this point in the history
While I'm here, use the new, more direct EC_GROUP APIs in
crypto/p224_spake.cc.

Bug: 691162
Change-Id: I45b25bd265aa5a10504c953fdbd4e6f689ed38f2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4826810
Auto-Submit: David Benjamin <davidben@chromium.org>
Reviewed-by: Bob Beck <bbe@chromium.org>
Commit-Queue: David Benjamin <davidben@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1191583}
  • Loading branch information
davidben authored and Chromium LUCI CQ committed Sep 1, 2023
1 parent 51aa012 commit 3bfdc52
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 108 deletions.
14 changes: 7 additions & 7 deletions crypto/aead.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void Aead::Init(base::span<const uint8_t> key) {
key_ = key;
}

static base::span<const uint8_t> ToSpan(base::StringPiece sp) {
static base::span<const uint8_t> ToSpan(std::string_view sp) {
return base::as_bytes(base::make_span(sp));
}

Expand All @@ -66,9 +66,9 @@ std::vector<uint8_t> Aead::Seal(
return ret;
}

bool Aead::Seal(base::StringPiece plaintext,
base::StringPiece nonce,
base::StringPiece additional_data,
bool Aead::Seal(std::string_view plaintext,
std::string_view nonce,
std::string_view additional_data,
std::string* ciphertext) const {
const size_t max_output_length =
EVP_AEAD_max_overhead(aead_) + plaintext.size();
Expand Down Expand Up @@ -105,9 +105,9 @@ absl::optional<std::vector<uint8_t>> Aead::Open(
return ret;
}

bool Aead::Open(base::StringPiece ciphertext,
base::StringPiece nonce,
base::StringPiece additional_data,
bool Aead::Open(std::string_view ciphertext,
std::string_view nonce,
std::string_view additional_data,
std::string* plaintext) const {
const size_t max_output_length = ciphertext.size();
CHECK(max_output_length + 1 > max_output_length);
Expand Down
16 changes: 8 additions & 8 deletions crypto/aead.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include <stdint.h>

#include <string>
#include <string_view>
#include <vector>

#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_piece.h"
#include "crypto/crypto_export.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

Expand All @@ -23,7 +23,7 @@ namespace crypto {

// This class exposes the AES-128-CTR-HMAC-SHA256 and AES_256_GCM AEAD. Note
// that there are two versions of most methods: an historical version based
// around |StringPiece| and a more modern version that takes |base::span|.
// around |std::string_view| and a more modern version that takes |base::span|.
// Prefer the latter in new code.
class CRYPTO_EXPORT Aead {
public:
Expand Down Expand Up @@ -51,19 +51,19 @@ class CRYPTO_EXPORT Aead {
base::span<const uint8_t> nonce,
base::span<const uint8_t> additional_data) const;

bool Seal(base::StringPiece plaintext,
base::StringPiece nonce,
base::StringPiece additional_data,
bool Seal(std::string_view plaintext,
std::string_view nonce,
std::string_view additional_data,
std::string* ciphertext) const;

absl::optional<std::vector<uint8_t>> Open(
base::span<const uint8_t> ciphertext,
base::span<const uint8_t> nonce,
base::span<const uint8_t> additional_data) const;

bool Open(base::StringPiece ciphertext,
base::StringPiece nonce,
base::StringPiece additional_data,
bool Open(std::string_view ciphertext,
std::string_view nonce,
std::string_view additional_data,
std::string* plaintext) const;

size_t KeyLength() const;
Expand Down
6 changes: 4 additions & 2 deletions crypto/chaps_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <secmod.h>
#include <secmodt.h>

#include <string_view>

#include "base/logging.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/memory/stack_allocated.h"
Expand Down Expand Up @@ -114,8 +116,8 @@ bool IsSlotProvidedByChaps(PK11SlotInfo* slot) {
return false;

SECMODModule* pk11_module = PK11_GetModule(slot);
return pk11_module && base::StringPiece(pk11_module->commonName) ==
base::StringPiece(kChapsModuleName);
return pk11_module && std::string_view(pk11_module->commonName) ==
std::string_view(kChapsModuleName);
}

} // namespace crypto
10 changes: 5 additions & 5 deletions crypto/encryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Encryptor::Encryptor() : key_(nullptr), mode_(CBC) {}

Encryptor::~Encryptor() = default;

bool Encryptor::Init(const SymmetricKey* key, Mode mode, base::StringPiece iv) {
bool Encryptor::Init(const SymmetricKey* key, Mode mode, std::string_view iv) {
return Init(key, mode, base::as_bytes(base::make_span(iv)));
}

Expand All @@ -63,7 +63,7 @@ bool Encryptor::Init(const SymmetricKey* key,
return true;
}

bool Encryptor::Encrypt(base::StringPiece plaintext, std::string* ciphertext) {
bool Encryptor::Encrypt(std::string_view plaintext, std::string* ciphertext) {
return CryptString(/*do_encrypt=*/true, plaintext, ciphertext);
}

Expand All @@ -72,7 +72,7 @@ bool Encryptor::Encrypt(base::span<const uint8_t> plaintext,
return CryptBytes(/*do_encrypt=*/true, plaintext, ciphertext);
}

bool Encryptor::Decrypt(base::StringPiece ciphertext, std::string* plaintext) {
bool Encryptor::Decrypt(std::string_view ciphertext, std::string* plaintext) {
return CryptString(/*do_encrypt=*/false, ciphertext, plaintext);
}

Expand All @@ -81,7 +81,7 @@ bool Encryptor::Decrypt(base::span<const uint8_t> ciphertext,
return CryptBytes(/*do_encrypt=*/false, ciphertext, plaintext);
}

bool Encryptor::SetCounter(base::StringPiece counter) {
bool Encryptor::SetCounter(std::string_view counter) {
return SetCounter(base::as_bytes(base::make_span(counter)));
}

Expand All @@ -96,7 +96,7 @@ bool Encryptor::SetCounter(base::span<const uint8_t> counter) {
}

bool Encryptor::CryptString(bool do_encrypt,
base::StringPiece input,
std::string_view input,
std::string* output) {
std::string result(MaxOutput(do_encrypt, input.size()), '\0');
absl::optional<size_t> len =
Expand Down
14 changes: 7 additions & 7 deletions crypto/encryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

#include <memory>
#include <string>
#include <string_view>

#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_piece.h"
#include "build/build_config.h"
#include "crypto/crypto_export.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
Expand All @@ -24,7 +24,7 @@ class SymmetricKey;

// This class implements encryption without authentication, which is usually
// unsafe. Prefer crypto::Aead for new code. If using this class, prefer the
// base::span and std::vector overloads over the base::StringPiece and
// base::span and std::vector overloads over the std::string_view and
// std::string overloads.
class CRYPTO_EXPORT Encryptor {
public:
Expand All @@ -41,12 +41,12 @@ class CRYPTO_EXPORT Encryptor {
//
// If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be
// empty.
bool Init(const SymmetricKey* key, Mode mode, base::StringPiece iv);
bool Init(const SymmetricKey* key, Mode mode, std::string_view iv);
bool Init(const SymmetricKey* key, Mode mode, base::span<const uint8_t> iv);

// Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if
// the mode is CBC.
bool Encrypt(base::StringPiece plaintext, std::string* ciphertext);
bool Encrypt(std::string_view plaintext, std::string* ciphertext);
bool Encrypt(base::span<const uint8_t> plaintext,
std::vector<uint8_t>* ciphertext);

Expand All @@ -59,15 +59,15 @@ class CRYPTO_EXPORT Encryptor {
// must either authenticate the ciphertext before decrypting it, or take
// care to not report decryption failure. Otherwise it could inadvertently
// be used as a padding oracle to attack the cryptosystem.
bool Decrypt(base::StringPiece ciphertext, std::string* plaintext);
bool Decrypt(std::string_view ciphertext, std::string* plaintext);
bool Decrypt(base::span<const uint8_t> ciphertext,
std::vector<uint8_t>* plaintext);

// Sets the counter value when in CTR mode. Currently only 128-bits
// counter value is supported.
//
// Returns true only if update was successful.
bool SetCounter(base::StringPiece counter);
bool SetCounter(std::string_view counter);
bool SetCounter(base::span<const uint8_t> counter);

// TODO(albertb): Support streaming encryption.
Expand All @@ -77,7 +77,7 @@ class CRYPTO_EXPORT Encryptor {
Mode mode_;

bool CryptString(bool do_encrypt,
base::StringPiece input,
std::string_view input,
std::string* output);
bool CryptBytes(bool do_encrypt,
base::span<const uint8_t> input,
Expand Down
12 changes: 6 additions & 6 deletions crypto/encryptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ void TestAESCTREncrypt(
crypto::Encryptor encryptor;
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, ""));

base::StringPiece init_counter_str(
reinterpret_cast<const char*>(init_counter), init_counter_size);
base::StringPiece plaintext_str(
reinterpret_cast<const char*>(plaintext), plaintext_size);
std::string_view init_counter_str(reinterpret_cast<const char*>(init_counter),
init_counter_size);
std::string_view plaintext_str(reinterpret_cast<const char*>(plaintext),
plaintext_size);

EXPECT_TRUE(encryptor.SetCounter(init_counter_str));
std::string encrypted;
Expand Down Expand Up @@ -253,7 +253,7 @@ void TestAESCTRMultipleDecrypt(
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, ""));

// Counter is set only once.
EXPECT_TRUE(encryptor.SetCounter(base::StringPiece(
EXPECT_TRUE(encryptor.SetCounter(std::string_view(
reinterpret_cast<const char*>(init_counter), init_counter_size)));

std::string ciphertext_str(reinterpret_cast<const char*>(ciphertext),
Expand Down Expand Up @@ -569,5 +569,5 @@ TEST(EncryptorTest, CipherTextNotMultipleOfBlockSize) {

std::string plaintext;
EXPECT_FALSE(
encryptor.Decrypt(base::StringPiece(ciphertext.get(), 1), &plaintext));
encryptor.Decrypt(std::string_view(ciphertext.get(), 1), &plaintext));
}
6 changes: 3 additions & 3 deletions crypto/hkdf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

namespace crypto {

std::string HkdfSha256(base::StringPiece secret,
base::StringPiece salt,
base::StringPiece info,
std::string HkdfSha256(std::string_view secret,
std::string_view salt,
std::string_view info,
size_t derived_key_size) {
std::string key;
key.resize(derived_key_size);
Expand Down
8 changes: 4 additions & 4 deletions crypto/hkdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
#include <stddef.h>

#include <string>
#include <string_view>

#include "base/containers/span.h"
#include "base/strings/string_piece.h"
#include "crypto/crypto_export.h"

namespace crypto {

CRYPTO_EXPORT
std::string HkdfSha256(base::StringPiece secret,
base::StringPiece salt,
base::StringPiece info,
std::string HkdfSha256(std::string_view secret,
std::string_view salt,
std::string_view info,
size_t derived_key_size);

CRYPTO_EXPORT
Expand Down
8 changes: 4 additions & 4 deletions crypto/hmac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool HMAC::Init(const SymmetricKey* key) {
return Init(key->key());
}

bool HMAC::Sign(base::StringPiece data,
bool HMAC::Sign(std::string_view data,
unsigned char* digest,
size_t digest_length) const {
return Sign(base::as_bytes(base::make_span(data)),
Expand All @@ -76,7 +76,7 @@ bool HMAC::Sign(base::span<const uint8_t> data,
nullptr);
}

bool HMAC::Verify(base::StringPiece data, base::StringPiece digest) const {
bool HMAC::Verify(std::string_view data, std::string_view digest) const {
return Verify(base::as_bytes(base::make_span(data)),
base::as_bytes(base::make_span(digest)));
}
Expand All @@ -88,8 +88,8 @@ bool HMAC::Verify(base::span<const uint8_t> data,
return VerifyTruncated(data, digest);
}

bool HMAC::VerifyTruncated(base::StringPiece data,
base::StringPiece digest) const {
bool HMAC::VerifyTruncated(std::string_view data,
std::string_view digest) const {
return VerifyTruncated(base::as_bytes(base::make_span(data)),
base::as_bytes(base::make_span(digest)));
}
Expand Down
16 changes: 8 additions & 8 deletions crypto/hmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
// Utility class for calculating the HMAC for a given message. We currently only
// support SHA-1 and SHA-256 for the hash algorithm, but this can be extended
// easily. Prefer the base::span and std::vector overloads over the
// base::StringPiece and std::string overloads.
// std::string_view and std::string overloads.

#ifndef CRYPTO_HMAC_H_
#define CRYPTO_HMAC_H_

#include <stddef.h>

#include <memory>
#include <string_view>
#include <vector>

#include "base/containers/span.h"
#include "base/strings/string_piece.h"
#include "crypto/crypto_export.h"

namespace crypto {
Expand Down Expand Up @@ -62,7 +62,7 @@ class CRYPTO_EXPORT HMAC {

// Initializes this instance using |key|. Call Init only once. It returns
// false on the second or later calls.
[[nodiscard]] bool Init(base::StringPiece key) {
[[nodiscard]] bool Init(std::string_view key) {
return Init(base::as_bytes(base::make_span(key)));
}

Expand All @@ -77,7 +77,7 @@ class CRYPTO_EXPORT HMAC {
// returned in |digest|, which has |digest_length| bytes of storage available.
// If |digest_length| is smaller than DigestLength(), the output will be
// truncated. If it is larger, this method will fail.
[[nodiscard]] bool Sign(base::StringPiece data,
[[nodiscard]] bool Sign(std::string_view data,
unsigned char* digest,
size_t digest_length) const;
[[nodiscard]] bool Sign(base::span<const uint8_t> data,
Expand All @@ -90,15 +90,15 @@ class CRYPTO_EXPORT HMAC {
// comparisons may result in side-channel disclosures, such as timing, that
// undermine the cryptographic integrity. |digest| must be exactly
// |DigestLength()| bytes long.
[[nodiscard]] bool Verify(base::StringPiece data,
base::StringPiece digest) const;
[[nodiscard]] bool Verify(std::string_view data,
std::string_view digest) const;
[[nodiscard]] bool Verify(base::span<const uint8_t> data,
base::span<const uint8_t> digest) const;

// Verifies a truncated HMAC, behaving identical to Verify(), except
// that |digest| is allowed to be smaller than |DigestLength()|.
[[nodiscard]] bool VerifyTruncated(base::StringPiece data,
base::StringPiece digest) const;
[[nodiscard]] bool VerifyTruncated(std::string_view data,
std::string_view digest) const;
[[nodiscard]] bool VerifyTruncated(base::span<const uint8_t> data,
base::span<const uint8_t> digest) const;

Expand Down
Loading

0 comments on commit 3bfdc52

Please sign in to comment.