Skip to content

Commit

Permalink
Switch to standard integer types in crypto/.
Browse files Browse the repository at this point in the history
BUG=138542
TBR=rsleevi@chromium.org
NOPRESUBMIT=true

Review URL: https://codereview.chromium.org/1539353003

Cr-Commit-Position: refs/heads/master@{#366460}
  • Loading branch information
avi authored and Commit bot committed Dec 21, 2015
1 parent 6411415 commit dd373b8
Show file tree
Hide file tree
Showing 82 changed files with 1,051 additions and 1,010 deletions.
28 changes: 15 additions & 13 deletions crypto/aead_openssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

#include <openssl/aes.h>
#include <openssl/evp.h>
#include <stddef.h>
#include <stdint.h>
#include <string>

#include "base/basictypes.h"
#include "base/strings/string_util.h"
#include "crypto/openssl_util.h"

Expand Down Expand Up @@ -43,7 +44,7 @@ bool Aead::Seal(const base::StringPiece& plaintext,
EVP_AEAD_CTX ctx;

if (!EVP_AEAD_CTX_init(&ctx, aead_,
reinterpret_cast<const uint8*>(key_->data()),
reinterpret_cast<const uint8_t*>(key_->data()),
key_->size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) {
return false;
}
Expand All @@ -52,14 +53,14 @@ bool Aead::Seal(const base::StringPiece& plaintext,
const size_t max_output_length =
EVP_AEAD_max_overhead(aead_) + plaintext.size();
size_t output_length;
uint8* out_ptr =
reinterpret_cast<uint8*>(base::WriteInto(&result, max_output_length + 1));
uint8_t* out_ptr = reinterpret_cast<uint8_t*>(
base::WriteInto(&result, max_output_length + 1));

if (!EVP_AEAD_CTX_seal(
&ctx, out_ptr, &output_length, max_output_length,
reinterpret_cast<const uint8*>(nonce.data()), nonce.size(),
reinterpret_cast<const uint8*>(plaintext.data()), plaintext.size(),
reinterpret_cast<const uint8*>(additional_data.data()),
reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(),
reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size(),
reinterpret_cast<const uint8_t*>(additional_data.data()),
additional_data.size())) {
EVP_AEAD_CTX_cleanup(&ctx);
return false;
Expand All @@ -82,22 +83,23 @@ bool Aead::Open(const base::StringPiece& ciphertext,
EVP_AEAD_CTX ctx;

if (!EVP_AEAD_CTX_init(&ctx, aead_,
reinterpret_cast<const uint8*>(key_->data()),
reinterpret_cast<const uint8_t*>(key_->data()),
key_->size(), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) {
return false;
}

std::string result;
const size_t max_output_length = ciphertext.size();
size_t output_length;
uint8* out_ptr =
reinterpret_cast<uint8*>(base::WriteInto(&result, max_output_length + 1));
uint8_t* out_ptr = reinterpret_cast<uint8_t*>(
base::WriteInto(&result, max_output_length + 1));

if (!EVP_AEAD_CTX_open(
&ctx, out_ptr, &output_length, max_output_length,
reinterpret_cast<const uint8*>(nonce.data()), nonce.size(),
reinterpret_cast<const uint8*>(ciphertext.data()), ciphertext.size(),
reinterpret_cast<const uint8*>(additional_data.data()),
reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(),
reinterpret_cast<const uint8_t*>(ciphertext.data()),
ciphertext.size(),
reinterpret_cast<const uint8_t*>(additional_data.data()),
additional_data.size())) {
EVP_AEAD_CTX_cleanup(&ctx);
return false;
Expand Down
2 changes: 2 additions & 0 deletions crypto/aead_openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef CRYPTO_AEAD_H_
#define CRYPTO_AEAD_H_

#include <stddef.h>

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

Expand Down
3 changes: 2 additions & 1 deletion crypto/apple_keychain.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

#include <Security/Security.h>

#include "base/basictypes.h"
#include "base/macros.h"
#include "build/build_config.h"
#include "crypto/crypto_export.h"

#if defined (OS_IOS)
Expand Down
4 changes: 3 additions & 1 deletion crypto/capi_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#include "crypto/capi_util.h"

#include "base/basictypes.h"
#include <stddef.h>

#include "base/macros.h"
#include "base/memory/singleton.h"
#include "base/synchronization/lock.h"

Expand Down
1 change: 1 addition & 0 deletions crypto/capi_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define CRYPTO_CAPI_UTIL_H_

#include <windows.h>
#include <stddef.h>

#include "crypto/crypto_export.h"
#include "crypto/wincrypt_shim.h"
Expand Down
3 changes: 2 additions & 1 deletion crypto/cssm_init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "crypto/cssm_init.h"

#include <Security/SecBase.h>
#include <stdint.h>

#include "base/logging.h"
#include "base/mac/scoped_cftyperef.h"
Expand Down Expand Up @@ -32,7 +33,7 @@ void* CSSMRealloc(void* ptr, CSSM_SIZE size, void* alloc_ref) {
return realloc(ptr, size);
}

void* CSSMCalloc(uint32 num, CSSM_SIZE size, void* alloc_ref) {
void* CSSMCalloc(uint32_t num, CSSM_SIZE size, void* alloc_ref) {
return calloc(num, size);
}

Expand Down
2 changes: 1 addition & 1 deletion crypto/cssm_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <Security/cssm.h>

#include "base/basictypes.h"
#include "base/macros.h"
#include "crypto/crypto_export.h"

namespace crypto {
Expand Down
2 changes: 2 additions & 0 deletions crypto/curve25519_nss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "crypto/curve25519.h"

#include <stdint.h>

#include "crypto/secure_util.h"

// Curve25519 is specified in terms of byte strings, not numbers, so all
Expand Down
1 change: 1 addition & 0 deletions crypto/curve25519_openssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "crypto/curve25519.h"

#include <openssl/curve25519.h>
#include <stdint.h>

namespace crypto {

Expand Down
2 changes: 2 additions & 0 deletions crypto/curve25519_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "crypto/curve25519.h"

#include <stdint.h>

#include <string>

#include "crypto/random.h"
Expand Down
19 changes: 11 additions & 8 deletions crypto/ec_private_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#ifndef CRYPTO_EC_PRIVATE_KEY_H_
#define CRYPTO_EC_PRIVATE_KEY_H_

#include <stddef.h>
#include <stdint.h>

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/macros.h"
#include "build/build_config.h"
#include "crypto/crypto_export.h"

Expand Down Expand Up @@ -45,8 +48,8 @@ class CRYPTO_EXPORT ECPrivateKey {
// Returns NULL if initialization fails.
static ECPrivateKey* CreateFromEncryptedPrivateKeyInfo(
const std::string& password,
const std::vector<uint8>& encrypted_private_key_info,
const std::vector<uint8>& subject_public_key_info);
const std::vector<uint8_t>& encrypted_private_key_info,
const std::vector<uint8_t>& subject_public_key_info);

#if !defined(USE_OPENSSL)
// Imports the key pair into |slot| and returns in |public_key| and |key|.
Expand All @@ -56,7 +59,7 @@ class CRYPTO_EXPORT ECPrivateKey {
static bool ImportFromEncryptedPrivateKeyInfo(
PK11SlotInfo* slot,
const std::string& password,
const uint8* encrypted_private_key_info,
const uint8_t* encrypted_private_key_info,
size_t encrypted_private_key_info_len,
CERTSubjectPublicKeyInfo* decoded_spki,
bool permanent,
Expand All @@ -82,18 +85,18 @@ class CRYPTO_EXPORT ECPrivateKey {
// of 1000 iterations, on modern systems a larger value may be preferrable.
bool ExportEncryptedPrivateKey(const std::string& password,
int iterations,
std::vector<uint8>* output);
std::vector<uint8_t>* output);

// Exports the public key to an X.509 SubjectPublicKeyInfo block.
bool ExportPublicKey(std::vector<uint8>* output);
bool ExportPublicKey(std::vector<uint8_t>* output);

// Exports the public key as an EC point in the uncompressed point format.
bool ExportRawPublicKey(std::string* output);

// Exports private key data for testing. The format of data stored into output
// doesn't matter other than that it is consistent for the same key.
bool ExportValue(std::vector<uint8>* output);
bool ExportECParams(std::vector<uint8>* output);
bool ExportValue(std::vector<uint8_t>* output);
bool ExportECParams(std::vector<uint8_t>* output);

private:
// Constructor is private. Use one of the Create*() methods above instead.
Expand Down
23 changes: 12 additions & 11 deletions crypto/ec_private_key_nss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extern "C" {
#include <keyhi.h>
#include <pk11pub.h>
#include <secmod.h>
#include <stddef.h>
#include <stdint.h>

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
Expand All @@ -27,7 +29,7 @@ namespace {
// Copied from rsa_private_key_nss.cc.
static bool ReadAttribute(SECKEYPrivateKey* key,
CK_ATTRIBUTE_TYPE type,
std::vector<uint8>* output) {
std::vector<uint8_t>* output) {
SECItem item;
SECStatus rv;
rv = PK11_ReadRawAttribute(PK11_TypePrivKey, key, type, &item);
Expand Down Expand Up @@ -102,8 +104,8 @@ ECPrivateKey* ECPrivateKey::Create() {
// static
ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
const std::string& password,
const std::vector<uint8>& encrypted_private_key_info,
const std::vector<uint8>& subject_public_key_info) {
const std::vector<uint8_t>& encrypted_private_key_info,
const std::vector<uint8_t>& subject_public_key_info) {
EnsureNSSInit();

ScopedPK11Slot slot(PK11_GetInternalSlot());
Expand Down Expand Up @@ -149,7 +151,7 @@ ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
bool ECPrivateKey::ImportFromEncryptedPrivateKeyInfo(
PK11SlotInfo* slot,
const std::string& password,
const uint8* encrypted_private_key_info,
const uint8_t* encrypted_private_key_info,
size_t encrypted_private_key_info_len,
CERTSubjectPublicKeyInfo* decoded_spki,
bool permanent,
Expand Down Expand Up @@ -237,10 +239,9 @@ ECPrivateKey* ECPrivateKey::Copy() const {
return copy.release();
}

bool ECPrivateKey::ExportEncryptedPrivateKey(
const std::string& password,
int iterations,
std::vector<uint8>* output) {
bool ECPrivateKey::ExportEncryptedPrivateKey(const std::string& password,
int iterations,
std::vector<uint8_t>* output) {
// We export as an EncryptedPrivateKeyInfo bundle instead of a plain PKCS #8
// PrivateKeyInfo because PK11_ImportDERPrivateKeyInfoAndReturnKey doesn't
// support EC keys.
Expand Down Expand Up @@ -282,7 +283,7 @@ bool ECPrivateKey::ExportEncryptedPrivateKey(
return true;
}

bool ECPrivateKey::ExportPublicKey(std::vector<uint8>* output) {
bool ECPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) {
ScopedSECItem der_pubkey(
SECKEY_EncodeDERSubjectPublicKeyInfo(public_key_));
if (!der_pubkey.get()) {
Expand Down Expand Up @@ -310,11 +311,11 @@ bool ECPrivateKey::ExportRawPublicKey(std::string* output) {
return true;
}

bool ECPrivateKey::ExportValue(std::vector<uint8>* output) {
bool ECPrivateKey::ExportValue(std::vector<uint8_t>* output) {
return ReadAttribute(key_, CKA_VALUE, output);
}

bool ECPrivateKey::ExportECParams(std::vector<uint8>* output) {
bool ECPrivateKey::ExportECParams(std::vector<uint8_t>* output) {
return ReadAttribute(key_, CKA_EC_PARAMS, output);
}

Expand Down
27 changes: 14 additions & 13 deletions crypto/ec_private_key_openssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <openssl/evp.h>
#include <openssl/pkcs12.h>
#include <openssl/x509.h>
#include <stddef.h>
#include <stdint.h>

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
Expand All @@ -32,7 +34,7 @@ using ScopedX509_SIG = ScopedOpenSSL<X509_SIG, X509_SIG_free>;
// Helper to export |key| into |output| via the specified ExportBioFunction.
bool ExportKeyWithBio(const void* key,
ExportBioFunction export_fn,
std::vector<uint8>* output) {
std::vector<uint8_t>* output) {
if (!key)
return false;

Expand Down Expand Up @@ -62,7 +64,7 @@ typedef int (*ExportDataFunction)(const void* key, unsigned char** data);
// Helper to export |key| into |output| via the specified export function.
bool ExportKey(const void* key,
ExportDataFunction export_fn,
std::vector<uint8>* output) {
std::vector<uint8_t>* output) {
if (!key)
return false;

Expand Down Expand Up @@ -112,8 +114,8 @@ ECPrivateKey* ECPrivateKey::Create() {
// static
ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
const std::string& password,
const std::vector<uint8>& encrypted_private_key_info,
const std::vector<uint8>& subject_public_key_info) {
const std::vector<uint8_t>& encrypted_private_key_info,
const std::vector<uint8_t>& subject_public_key_info) {
// NOTE: The |subject_public_key_info| can be ignored here, it is only
// useful for the NSS implementation (which uses the public key's SHA1
// as a lookup key when storing the private one in its store).
Expand Down Expand Up @@ -157,10 +159,9 @@ ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
return result.release();
}

bool ECPrivateKey::ExportEncryptedPrivateKey(
const std::string& password,
int iterations,
std::vector<uint8>* output) {
bool ECPrivateKey::ExportEncryptedPrivateKey(const std::string& password,
int iterations,
std::vector<uint8_t>* output) {
OpenSSLErrStackTracer err_tracer(FROM_HERE);
// Convert into a PKCS#8 object.
ScopedPKCS8_PRIV_KEY_INFO pkcs8(EVP_PKEY2PKCS8(key_));
Expand Down Expand Up @@ -189,7 +190,7 @@ bool ECPrivateKey::ExportEncryptedPrivateKey(
output);
}

bool ECPrivateKey::ExportPublicKey(std::vector<uint8>* output) {
bool ECPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) {
OpenSSLErrStackTracer err_tracer(FROM_HERE);
return ExportKeyWithBio(
key_, reinterpret_cast<ExportBioFunction>(i2d_PUBKEY_bio), output);
Expand All @@ -205,8 +206,8 @@ bool ECPrivateKey::ExportRawPublicKey(std::string* output) {
if (len != kExpectedKeyLength)
return false;

uint8 buf[kExpectedKeyLength];
uint8* derp = buf;
uint8_t buf[kExpectedKeyLength];
uint8_t* derp = buf;
len = i2d_PublicKey(key_, &derp);
if (len != kExpectedKeyLength)
return false;
Expand All @@ -215,15 +216,15 @@ bool ECPrivateKey::ExportRawPublicKey(std::string* output) {
return true;
}

bool ECPrivateKey::ExportValue(std::vector<uint8>* output) {
bool ECPrivateKey::ExportValue(std::vector<uint8_t>* output) {
OpenSSLErrStackTracer err_tracer(FROM_HERE);
ScopedEC_KEY ec_key(EVP_PKEY_get1_EC_KEY(key_));
return ExportKey(ec_key.get(),
reinterpret_cast<ExportDataFunction>(i2d_ECPrivateKey),
output);
}

bool ECPrivateKey::ExportECParams(std::vector<uint8>* output) {
bool ECPrivateKey::ExportECParams(std::vector<uint8_t>* output) {
OpenSSLErrStackTracer err_tracer(FROM_HERE);
ScopedEC_KEY ec_key(EVP_PKEY_get1_EC_KEY(key_));
return ExportKey(ec_key.get(),
Expand Down
Loading

0 comments on commit dd373b8

Please sign in to comment.