Skip to content

Commit

Permalink
Add back call to EVP_CipherFinal_ex in decryption phase.
Browse files Browse the repository at this point in the history
Few other miscellaneous cleanups. Looks lke we're still tripping over some
undefined behaviour, I see crashes and/or corruption that changes depending
on compiler (gcc/clang) and optimization.
  • Loading branch information
stixpjr committed Nov 15, 2018
1 parent 46d6122 commit c672aa2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ if (USE_CLANG)
set(CXX clang++)
endif(USE_CLANG)

set(CMAKE_C_FLAGS_DEBUG -g)
set(CMAKE_CXX_FLAGS_DEBUG -g)
set(CMAKE_C_FLAGS_DEBUG "-g -Wall -O0")
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -O0")
set(CMAKE_C_FLAGS_RELEASE "-Wall -O2")
set(CMAKE_CXX_FLAGS_RELEASE "-Wall -O2")
set(CMAKE_BUILD_TYPE Release)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ sh$ cmake ..
sh$ make
```

## Options

Some options have been implememented that affect the build, which may be passed
to cmake via, eg:

```cmake -DCMAKE_BUILD_TYPE=(Debug|Release)```

* CMAKE_BUILD_TYPE=(Release|Debug)
* USE_CLANG=(ON|OFF)

## TODO:

* Use git submodules for:
Expand Down
26 changes: 22 additions & 4 deletions cpu_check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,6 @@ void Worker::Run() {
for (uint32_t i = 0; i < hashers.size(); i++) {
if (exiting) break;
hashes[i] = hashers[i].func(src_buf);
// LOG(DEBUG) << "Hash " << hashers[i].name << ": " << hashes[i] << ".";
// XXX
}
LOG(DEBUG) << "tid " << tid_ << " initial hashes done.";
if (exiting) break;
Expand Down Expand Up @@ -389,7 +387,7 @@ void Worker::Run() {
unsigned char gmac[14];
FillBufferRandomData(&ivec);

int enc_len, enc_unused_len;
int enc_len = 0, enc_unused_len = 0;
EVP_CipherInit_ex(cipher_ctx, EVP_aes_256_gcm(), NULL, key,
(unsigned char *)ivec.data(), 1);

Expand All @@ -411,6 +409,13 @@ void Worker::Run() {
dst_buf.erase();
continue;
}
enc_len += enc_unused_len;
if (enc_len != (int) dst_buf.size()) {
LOG(ERROR) << "tid " << tid_ << " encrypt length mismatch: " << enc_len << " vs " << dst_buf.size();
EVP_CIPHER_CTX_cleanup(cipher_ctx);
dst_buf.erase();
continue;
}
if (EVP_CIPHER_CTX_ctrl(cipher_ctx, EVP_CTRL_GCM_GET_TAG, 16, gmac) != 1) {
LOG(ERROR) << "tid " << tid_ << " EVP_CTRL_GCM_GET_TAG failed.";
errorCount++;
Expand Down Expand Up @@ -463,7 +468,7 @@ void Worker::Run() {

// Decrypt.

int dec_len;
int dec_len = 0, dec_extra_len = 0;
EVP_CipherInit_ex(cipher_ctx, EVP_aes_256_gcm(), NULL, key,
(unsigned char *)ivec.data(), 0);
dst_buf.resize(src_buf.size());
Expand All @@ -484,7 +489,20 @@ void Worker::Run() {
dst_buf.erase();
continue;
}
if (EVP_CipherFinal_ex(cipher_ctx, (unsigned char *)&dst_buf[dec_len], &dec_extra_len) != 1) {
LOG(ERROR) << "tid " << tid_ << " decrypt EVP_CipherFinal_ex failed.";
errorCount++;
EVP_CIPHER_CTX_cleanup(cipher_ctx);
dst_buf.erase();
continue;
}
dec_len += dec_extra_len;
EVP_CIPHER_CTX_cleanup(cipher_ctx);
if (dec_len != (int) dst_buf.size()) {
LOG(ERROR) << "tid " << tid_ << " decrypt length mismatch: " << dec_len << " vs " << dst_buf.size();
dst_buf.erase();
continue;
}
std::swap(src_buf, dst_buf);
dst_buf.clear();
LOG(DEBUG) << "tid " << tid_ << " dencrypt done.";
Expand Down
2 changes: 1 addition & 1 deletion crc32c_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main(int argc, char **argv) {
for (int i = 0; i < 100; i++) {
size_t len = size_dist(rndeng);
buf.resize(len);
for (int j = 0; j < len; j++) {
for (size_t j = 0; j < len; j++) {
buf[j] = d_dist(rndeng);
}
uint32_t crc_hw = crc32c_hw(buf.data(), len);
Expand Down

0 comments on commit c672aa2

Please sign in to comment.