From 267253470a11846001374add822cff791ce38085 Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Wed, 28 Jan 2015 15:52:57 -0800 Subject: [PATCH] Align pointers by hand. This avoids having Windows be different and is also easier for testing because it's a simple matter to unalign the pointer if needed. Change-Id: I32cfa5834e3fe4f16304a25092b9c71946d4744d Reviewed-on: https://boringssl-review.googlesource.com/3131 Reviewed-by: Adam Langley --- tool/speed.cc | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/tool/speed.cc b/tool/speed.cc index cfffcb05..11747b1a 100644 --- a/tool/speed.cc +++ b/tool/speed.cc @@ -169,26 +169,15 @@ struct free_functor { } }; -#if defined(OPENSSL_WINDOWS) -uint8_t *AllocAligned(size_t size) { - void *ptr = malloc(size); - if (ptr == NULL) { - abort(); - } - return static_cast(ptr); +static uint8_t *align(uint8_t *in, unsigned alignment) { + return reinterpret_cast( + (reinterpret_cast(in) + alignment) & ~(alignment - 1)); } -#else -uint8_t *AllocAligned(size_t size) { - void *ptr; - if (posix_memalign(&ptr, 64, size)) { - abort(); - } - return static_cast(ptr); -} -#endif static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name, size_t chunk_len, size_t ad_len) { + static const unsigned kAlignment = 16; + EVP_AEAD_CTX ctx; const size_t key_len = EVP_AEAD_key_length(aead); const size_t nonce_len = EVP_AEAD_nonce_length(aead); @@ -198,14 +187,18 @@ static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name, memset(key.get(), 0, key_len); std::unique_ptr nonce(new uint8_t[nonce_len]); memset(nonce.get(), 0, nonce_len); - std::unique_ptr> in(AllocAligned(chunk_len)); - memset(in.get(), 0, chunk_len); - std::unique_ptr> out( - AllocAligned(chunk_len + overhead_len)); - memset(out.get(), 0, chunk_len + overhead_len); + std::unique_ptr> in_storage( + new uint8_t[chunk_len + kAlignment]); + std::unique_ptr> out_storage( + new uint8_t[chunk_len + overhead_len + kAlignment]); std::unique_ptr ad(new uint8_t[ad_len]); memset(ad.get(), 0, ad_len); + uint8_t *const in = align(in_storage.get(), kAlignment); + memset(in, 0, chunk_len); + uint8_t *const out = align(out_storage.get(), kAlignment); + memset(out, 0, chunk_len + overhead_len); + if (!EVP_AEAD_CTX_init(&ctx, aead, key.get(), key_len, EVP_AEAD_DEFAULT_TAG_LENGTH, NULL)) { fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n"); @@ -214,13 +207,13 @@ static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name, } TimeResults results; - if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, ad_len, &in, - &out, &ctx, &nonce, &ad]() -> bool { + if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, ad_len, in, + out, &ctx, &nonce, &ad]() -> bool { size_t out_len; return EVP_AEAD_CTX_seal( - &ctx, out.get(), &out_len, chunk_len + overhead_len, nonce.get(), - nonce_len, in.get(), chunk_len, ad.get(), ad_len); + &ctx, out, &out_len, chunk_len + overhead_len, nonce.get(), + nonce_len, in, chunk_len, ad.get(), ad_len); })) { fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n"); BIO_print_errors_fp(stderr);