diff options
author | Jack Lloyd <[email protected]> | 2018-11-17 14:54:21 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-11-17 22:50:26 -0500 |
commit | 432d31546eb335bb96c6b2a8c58f0168266387ec (patch) | |
tree | 2397468c243906a08eea08b9376a9922346e6856 /src/lib | |
parent | 6b3a827fb493ad884c7443092ba8f9967636b3c8 (diff) |
Avoid calling memset, memcpy within library code
Prefer using wrappers in mem_utils for this.
Current exception is where memcpy is being used to convert between
two different types, since copy_mem requires input and output
pointers have the same type. There should be a new function to
handle conversion-via-memcpy operation.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/hash/streebog/streebog.cpp | 4 | ||||
-rw-r--r-- | src/lib/prov/bearssl/bearssl_hash.cpp | 2 | ||||
-rw-r--r-- | src/lib/prov/commoncrypto/commoncrypto_mode.cpp | 4 | ||||
-rw-r--r-- | src/lib/prov/commoncrypto/commoncrypto_utils.cpp | 2 | ||||
-rw-r--r-- | src/lib/prov/openssl/openssl_mode.cpp | 4 | ||||
-rw-r--r-- | src/lib/prov/openssl/openssl_rsa.cpp | 2 | ||||
-rw-r--r-- | src/lib/prov/pkcs11/p11_mechanism.h | 4 | ||||
-rw-r--r-- | src/lib/prov/tpm/tpm.cpp | 2 | ||||
-rw-r--r-- | src/lib/pubkey/ed25519/ed25519.cpp | 4 | ||||
-rw-r--r-- | src/lib/pubkey/ed25519/ed25519_fe.h | 8 | ||||
-rw-r--r-- | src/lib/utils/socket/socket.cpp | 2 |
11 files changed, 20 insertions, 18 deletions
diff --git a/src/lib/hash/streebog/streebog.cpp b/src/lib/hash/streebog/streebog.cpp index cd67256cb..8bb56ccc9 100644 --- a/src/lib/hash/streebog/streebog.cpp +++ b/src/lib/hash/streebog/streebog.cpp @@ -35,9 +35,10 @@ static inline void addm(const uint8_t* m, uint64_t* h) } } -inline void lps(uint64_t* block) +inline void lps(uint64_t block[8]) { uint8_t r[64]; + // FIXME std::memcpy(r, block, 64); for(int i = 0; i < 8; ++i) @@ -171,6 +172,7 @@ void Streebog::final_result(uint8_t output[]) compress(m_buffer.data(), true); compress(reinterpret_cast<const uint8_t*>(m_S.data()), true); + // FIXME std::memcpy(output, &m_h[8 - output_length() / 8], output_length()); clear(); } diff --git a/src/lib/prov/bearssl/bearssl_hash.cpp b/src/lib/prov/bearssl/bearssl_hash.cpp index 564935624..2b837bcf5 100644 --- a/src/lib/prov/bearssl/bearssl_hash.cpp +++ b/src/lib/prov/bearssl/bearssl_hash.cpp @@ -37,7 +37,7 @@ class BearSSL_HashFunction final : public HashFunction std::unique_ptr<HashFunction> copy_state() const override { std::unique_ptr<BearSSL_HashFunction> copy(new BearSSL_HashFunction(m_ctx.vtable, m_name)); - memcpy(©->m_ctx, &m_ctx, sizeof(m_ctx)); + std::memcpy(©->m_ctx, &m_ctx, sizeof(m_ctx)); return std::move(copy); } diff --git a/src/lib/prov/commoncrypto/commoncrypto_mode.cpp b/src/lib/prov/commoncrypto/commoncrypto_mode.cpp index a3a27637a..cb30b6802 100644 --- a/src/lib/prov/commoncrypto/commoncrypto_mode.cpp +++ b/src/lib/prov/commoncrypto/commoncrypto_mode.cpp @@ -111,7 +111,7 @@ size_t CommonCrypto_Cipher_Mode::process(uint8_t msg[], size_t msg_len) { throw CommonCrypto_Error("CCCryptorUpdate", status); } - memcpy(msg, out.data(), outl); + copy_mem(msg, out.data(), outl); return outl; } @@ -143,7 +143,7 @@ void CommonCrypto_Cipher_Mode::finish(secure_vector<uint8_t>& buffer, { buffer.resize(new_len); } - memcpy(buffer.data() - offset + written, out.data(), outl); + copy_mem(buffer.data() - offset + written, out.data(), outl); written += outl; } diff --git a/src/lib/prov/commoncrypto/commoncrypto_utils.cpp b/src/lib/prov/commoncrypto/commoncrypto_utils.cpp index 9ec9c30ee..97da49496 100644 --- a/src/lib/prov/commoncrypto/commoncrypto_utils.cpp +++ b/src/lib/prov/commoncrypto/commoncrypto_utils.cpp @@ -178,7 +178,7 @@ void commoncrypto_adjust_key_size(const uint8_t key[], size_t length, full_key.resize(length * repeat); for(int i=0; i<repeat; i++) { - memcpy(full_key.data() + i * length, key, length); + copy_mem(full_key.data() + i * length, key, length); } } else if(opts.algo == kCCAlgorithm3DES && length == 16) diff --git a/src/lib/prov/openssl/openssl_mode.cpp b/src/lib/prov/openssl/openssl_mode.cpp index 5636d008f..d1983949d 100644 --- a/src/lib/prov/openssl/openssl_mode.cpp +++ b/src/lib/prov/openssl/openssl_mode.cpp @@ -117,7 +117,7 @@ size_t OpenSSL_Cipher_Mode::process(uint8_t msg[], size_t msg_len) if(!EVP_CipherUpdate(m_cipher, out.data(), &outl, msg, msg_len)) throw OpenSSL_Error("EVP_CipherUpdate"); - memcpy(msg, out.data(), outl); + copy_mem(msg, out.data(), outl); return outl; } @@ -137,7 +137,7 @@ void OpenSSL_Cipher_Mode::finish(secure_vector<uint8_t>& buffer, if(!EVP_CipherFinal_ex(m_cipher, out.data(), &outl)) throw OpenSSL_Error("EVP_CipherFinal_ex"); - memcpy(buf + written, out.data(), outl); + copy_mem(buf + written, out.data(), outl); written += outl; buffer.resize(offset + written); } diff --git a/src/lib/prov/openssl/openssl_rsa.cpp b/src/lib/prov/openssl/openssl_rsa.cpp index 98c0734a8..4e9068ce4 100644 --- a/src/lib/prov/openssl/openssl_rsa.cpp +++ b/src/lib/prov/openssl/openssl_rsa.cpp @@ -302,7 +302,7 @@ make_openssl_rsa_private_key(RandomNumberGenerator& rng, size_t rsa_bits) throw OpenSSL_Error("i2d_RSAPrivateKey"); const secure_vector<uint8_t> keydata(der, der + bytes); - memset(der, 0, bytes); + secure_scrub_memory(der, bytes); std::free(der); return std::unique_ptr<Botan::RSA_PrivateKey> (new RSA_PrivateKey(AlgorithmIdentifier(), keydata)); diff --git a/src/lib/prov/pkcs11/p11_mechanism.h b/src/lib/prov/pkcs11/p11_mechanism.h index 035d1e69a..8947372fd 100644 --- a/src/lib/prov/pkcs11/p11_mechanism.h +++ b/src/lib/prov/pkcs11/p11_mechanism.h @@ -92,12 +92,12 @@ class MechanismWrapper final return m_padding_size; } - /// Holds the mechanism parameters for OEAP, PSS and ECDH + /// Holds the mechanism parameters for OAEP, PSS and ECDH union MechanismParameters { MechanismParameters() { - std::memset(this, 0, sizeof(MechanismParameters)); + clear_mem(this, 1); } RsaPkcsOaepParams oaep_params; diff --git a/src/lib/prov/tpm/tpm.cpp b/src/lib/prov/tpm/tpm.cpp index 4847b0df6..ffbc220d6 100644 --- a/src/lib/prov/tpm/tpm.cpp +++ b/src/lib/prov/tpm/tpm.cpp @@ -191,7 +191,7 @@ void TPM_Context::gen_random(uint8_t out[], size_t out_len) { BYTE* mem; TSPI_CHECK_SUCCESS(::Tspi_TPM_GetRandom(m_tpm, out_len, &mem)); - std::memcpy(out, mem, out_len); + copy_mem(out, reinterpret_cast<const uint8_t*>(mem), out_len); TSPI_CHECK_SUCCESS(::Tspi_Context_FreeMemory(m_ctx, mem)); } diff --git a/src/lib/pubkey/ed25519/ed25519.cpp b/src/lib/pubkey/ed25519/ed25519.cpp index d959df709..b75028041 100644 --- a/src/lib/pubkey/ed25519/ed25519.cpp +++ b/src/lib/pubkey/ed25519/ed25519.cpp @@ -29,8 +29,8 @@ void ed25519_gen_keypair(uint8_t* pk, uint8_t* sk, const uint8_t seed[32]) ge_scalarmult_base(pk, az); // todo copy_mem - memmove(sk, seed, 32); - memmove(sk + 32, pk, 32); + copy_mem(sk, seed, 32); + copy_mem(sk + 32, pk, 32); } void ed25519_sign(uint8_t sig[64], diff --git a/src/lib/pubkey/ed25519/ed25519_fe.h b/src/lib/pubkey/ed25519/ed25519_fe.h index af92af46a..bcdc36a5e 100644 --- a/src/lib/pubkey/ed25519/ed25519_fe.h +++ b/src/lib/pubkey/ed25519/ed25519_fe.h @@ -30,16 +30,16 @@ class FE_25519 FE_25519(int init = 0) { if(init != 0 && init != 1) - { throw Invalid_Argument("Invalid FE_25519 initial value"); } - memset(m_fe, 0, 10 * sizeof(int32_t)); + throw Invalid_Argument("Invalid FE_25519 initial value"); + clear_mem(m_fe, 10); m_fe[0] = init; } FE_25519(std::initializer_list<int32_t> x) { if(x.size() != 10) - { throw Invalid_Argument("Invalid FE_25519 initializer list"); } - memcpy(m_fe, x.begin(), 10 * sizeof(int32_t)); + throw Invalid_Argument("Invalid FE_25519 initializer list"); + copy_mem(m_fe, x.begin(), 10); } FE_25519(int64_t h0, int64_t h1, int64_t h2, int64_t h3, int64_t h4, diff --git a/src/lib/utils/socket/socket.cpp b/src/lib/utils/socket/socket.cpp index 1caeb2413..39ffc7951 100644 --- a/src/lib/utils/socket/socket.cpp +++ b/src/lib/utils/socket/socket.cpp @@ -207,7 +207,7 @@ class BSD_Socket final : public OS::Socket m_socket = invalid_socket(); addrinfo hints; - ::memset(&hints, 0, sizeof(addrinfo)); + clear_mem(&hints, 1); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; addrinfo* res; |