diff options
author | Jack Lloyd <[email protected]> | 2018-11-17 16:40:24 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-11-17 22:50:26 -0500 |
commit | 71ecbd15c47b136c4a7c712775d86435fa4fc453 (patch) | |
tree | 630e196dac5001b9cb7103740e3239f0fb7cd1ec | |
parent | 432d31546eb335bb96c6b2a8c58f0168266387ec (diff) |
Add typecast_copy
Wraps memcpy in the cases where we really are doing a type conversion
using memcpy
-rw-r--r-- | src/lib/prov/tpm/tpm.cpp | 4 | ||||
-rw-r--r-- | src/lib/utils/loadstor.h | 34 | ||||
-rw-r--r-- | src/lib/utils/mem_ops.h | 40 |
3 files changed, 48 insertions, 30 deletions
diff --git a/src/lib/prov/tpm/tpm.cpp b/src/lib/prov/tpm/tpm.cpp index ffbc220d6..5516d41a0 100644 --- a/src/lib/prov/tpm/tpm.cpp +++ b/src/lib/prov/tpm/tpm.cpp @@ -113,7 +113,7 @@ TSS_UUID to_tss_uuid(const UUID& uuid) static_assert(sizeof(TSS_UUID) == 16, "Expected size of packed UUID"); TSS_UUID tss_uuid; - std::memcpy(&tss_uuid, uuid.binary_value().data(), 16); + typecast_copy(tss_uuid, uuid.binary_value().data()); return tss_uuid; } @@ -122,7 +122,7 @@ UUID from_tss_uuid(const TSS_UUID& tss_uuid) static_assert(sizeof(TSS_UUID) == 16, "Expected size of packed UUID"); std::vector<uint8_t> mem(16); - std::memcpy(mem.data(), &tss_uuid, 16); + typecast_copy(mem.data(), tss_uuid); UUID uuid(std::move(mem)); return uuid; } diff --git a/src/lib/utils/loadstor.h b/src/lib/utils/loadstor.h index 2fb1543b0..579d05deb 100644 --- a/src/lib/utils/loadstor.h +++ b/src/lib/utils/loadstor.h @@ -140,7 +140,7 @@ inline uint16_t load_be<uint16_t>(const uint8_t in[], size_t off) #if defined(BOTAN_ENDIAN_N2B) uint16_t x; - std::memcpy(&x, in, sizeof(x)); + typecast_copy(x, in); return BOTAN_ENDIAN_N2B(x); #else return make_uint16(in[0], in[1]); @@ -160,7 +160,7 @@ inline uint16_t load_le<uint16_t>(const uint8_t in[], size_t off) #if defined(BOTAN_ENDIAN_N2L) uint16_t x; - std::memcpy(&x, in, sizeof(x)); + typecast_copy(x, in); return BOTAN_ENDIAN_N2L(x); #else return make_uint16(in[1], in[0]); @@ -179,7 +179,7 @@ inline uint32_t load_be<uint32_t>(const uint8_t in[], size_t off) in += off * sizeof(uint32_t); #if defined(BOTAN_ENDIAN_N2B) uint32_t x; - std::memcpy(&x, in, sizeof(x)); + typecast_copy(x, in); return BOTAN_ENDIAN_N2B(x); #else return make_uint32(in[0], in[1], in[2], in[3]); @@ -198,7 +198,7 @@ inline uint32_t load_le<uint32_t>(const uint8_t in[], size_t off) in += off * sizeof(uint32_t); #if defined(BOTAN_ENDIAN_N2L) uint32_t x; - std::memcpy(&x, in, sizeof(x)); + typecast_copy(x, in); return BOTAN_ENDIAN_N2L(x); #else return make_uint32(in[3], in[2], in[1], in[0]); @@ -217,7 +217,7 @@ inline uint64_t load_be<uint64_t>(const uint8_t in[], size_t off) in += off * sizeof(uint64_t); #if defined(BOTAN_ENDIAN_N2B) uint64_t x; - std::memcpy(&x, in, sizeof(x)); + typecast_copy(x, in); return BOTAN_ENDIAN_N2B(x); #else return make_uint64(in[0], in[1], in[2], in[3], @@ -237,7 +237,7 @@ inline uint64_t load_le<uint64_t>(const uint8_t in[], size_t off) in += off * sizeof(uint64_t); #if defined(BOTAN_ENDIAN_N2L) uint64_t x; - std::memcpy(&x, in, sizeof(x)); + typecast_copy(x, in); return BOTAN_ENDIAN_N2L(x); #else return make_uint64(in[7], in[6], in[5], in[4], @@ -317,9 +317,11 @@ inline void load_le(T out[], if(count > 0) { #if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN) - std::memcpy(out, in, sizeof(T)*count); + typecast_copy(out, in, count); + #elif defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN) - std::memcpy(out, in, sizeof(T)*count); + typecast_copy(out, in, count); + const size_t blocks = count - (count % 4); const size_t left = count - blocks; @@ -407,10 +409,10 @@ inline void load_be(T out[], if(count > 0) { #if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN) - std::memcpy(out, in, sizeof(T)*count); + typecast_copy(out, in, count); #elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN) - std::memcpy(out, in, sizeof(T)*count); + typecast_copy(out, in, count); const size_t blocks = count - (count % 4); const size_t left = count - blocks; @@ -435,7 +437,7 @@ inline void store_be(uint16_t in, uint8_t out[2]) { #if defined(BOTAN_ENDIAN_N2B) uint16_t o = BOTAN_ENDIAN_N2B(in); - std::memcpy(out, &o, sizeof(o)); + typecast_copy(out, o); #else out[0] = get_byte(0, in); out[1] = get_byte(1, in); @@ -451,7 +453,7 @@ inline void store_le(uint16_t in, uint8_t out[2]) { #if defined(BOTAN_ENDIAN_N2L) uint16_t o = BOTAN_ENDIAN_N2L(in); - std::memcpy(out, &o, sizeof(o)); + typecast_copy(out, o); #else out[0] = get_byte(1, in); out[1] = get_byte(0, in); @@ -467,7 +469,7 @@ inline void store_be(uint32_t in, uint8_t out[4]) { #if defined(BOTAN_ENDIAN_B2N) uint32_t o = BOTAN_ENDIAN_B2N(in); - std::memcpy(out, &o, sizeof(o)); + typecast_copy(out, o); #else out[0] = get_byte(0, in); out[1] = get_byte(1, in); @@ -485,7 +487,7 @@ inline void store_le(uint32_t in, uint8_t out[4]) { #if defined(BOTAN_ENDIAN_L2N) uint32_t o = BOTAN_ENDIAN_L2N(in); - std::memcpy(out, &o, sizeof(o)); + typecast_copy(out, o); #else out[0] = get_byte(3, in); out[1] = get_byte(2, in); @@ -503,7 +505,7 @@ inline void store_be(uint64_t in, uint8_t out[8]) { #if defined(BOTAN_ENDIAN_B2N) uint64_t o = BOTAN_ENDIAN_B2N(in); - std::memcpy(out, &o, sizeof(o)); + typecast_copy(out, o); #else out[0] = get_byte(0, in); out[1] = get_byte(1, in); @@ -525,7 +527,7 @@ inline void store_le(uint64_t in, uint8_t out[8]) { #if defined(BOTAN_ENDIAN_L2N) uint64_t o = BOTAN_ENDIAN_L2N(in); - std::memcpy(out, &o, sizeof(o)); + typecast_copy(out, o); #else out[0] = get_byte(7, in); out[1] = get_byte(6, in); diff --git a/src/lib/utils/mem_ops.h b/src/lib/utils/mem_ops.h index c59c02d5a..f0599c8b2 100644 --- a/src/lib/utils/mem_ops.h +++ b/src/lib/utils/mem_ops.h @@ -113,6 +113,21 @@ template<typename T> inline void copy_mem(T* out, const T* in, size_t n) } } +template<typename T> inline void typecast_copy(uint8_t out[], T in) + { + std::memcpy(out, &in, sizeof(T)); + } + +template<typename T> inline void typecast_copy(T& out, const uint8_t in[]) + { + std::memcpy(&out, in, sizeof(T)); + } + +template<typename T> inline void typecast_copy(T out[], const uint8_t in[], size_t N) + { + std::memcpy(out, in, sizeof(T)*N); + } + /** * Set memory to a fixed value * @param ptr a pointer to an array @@ -178,15 +193,16 @@ inline void xor_buf(uint8_t out[], while(length >= 16) { uint64_t x0, x1, y0, y1; - std::memcpy(&x0, in, 8); - std::memcpy(&x1, in + 8, 8); - std::memcpy(&y0, out, 8); - std::memcpy(&y1, out + 8, 8); + + typecast_copy(x0, in); + typecast_copy(x1, in + 8); + typecast_copy(y0, out); + typecast_copy(y1, out + 8); y0 ^= x0; y1 ^= x1; - std::memcpy(out, &y0, 8); - std::memcpy(out + 8, &y1, 8); + typecast_copy(out, y0); + typecast_copy(out + 8, y1); out += 16; in += 16; length -= 16; } @@ -214,15 +230,15 @@ inline void xor_buf(uint8_t out[], while(length >= 16) { uint64_t x0, x1, y0, y1; - std::memcpy(&x0, in, 8); - std::memcpy(&x1, in + 8, 8); - std::memcpy(&y0, in2, 8); - std::memcpy(&y1, in2 + 8, 8); + typecast_copy(x0, in); + typecast_copy(x1, in + 8); + typecast_copy(y0, in2); + typecast_copy(y1, in2 + 8); x0 ^= y0; x1 ^= y1; - std::memcpy(out, &x0, 8); - std::memcpy(out + 8, &x1, 8); + typecast_copy(out, x0); + typecast_copy(out + 8, x1); out += 16; in += 16; in2 += 16; length -= 16; } |