From bb427bd11774f47f553257cdc0693f77b559654d Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Mon, 4 Nov 2019 09:55:43 -0500 Subject: SHA2: switch to pluggable crypto --- libqpdf/SHA2_native.cc | 131 +++++++++++++------------------------------------ 1 file changed, 35 insertions(+), 96 deletions(-) (limited to 'libqpdf/SHA2_native.cc') diff --git a/libqpdf/SHA2_native.cc b/libqpdf/SHA2_native.cc index 17eff7e3..35c2bb05 100644 --- a/libqpdf/SHA2_native.cc +++ b/libqpdf/SHA2_native.cc @@ -1,93 +1,59 @@ -#include +#include #include #include #include #include -Pl_SHA2::Pl_SHA2(int bits, Pipeline* next) : - Pipeline("sha2", next), - in_progress(false), - bits(0) + +SHA2_native::SHA2_native(int bits) : + bits(bits) { - if (bits) + switch (bits) { - resetBits(bits); + case 256: + sph_sha256_init(&this->ctx256); + break; + case 384: + sph_sha384_init(&this->ctx384); + break; + case 512: + sph_sha512_init(&this->ctx512); + break; + default: + badBits(); + break; } } -Pl_SHA2::~Pl_SHA2() -{ -} - void -Pl_SHA2::badBits() +SHA2_native::badBits() { - throw std::logic_error("Pl_SHA2 has unexpected value for bits"); + throw std::logic_error("SHA2_native has bits != 256, 384, or 512"); } void -Pl_SHA2::write(unsigned char* buf, size_t len) +SHA2_native::update(unsigned char const* buf, size_t len) { - if (! this->in_progress) - { - switch (bits) - { - case 256: - sph_sha256_init(&this->ctx256); - break; - case 384: - sph_sha384_init(&this->ctx384); - break; - case 512: - sph_sha512_init(&this->ctx512); - break; - default: - badBits(); - break; - } - this->in_progress = true; - } - - // Write in chunks in case len is too big to fit in an int. - // Assume int is at least 32 bits. - static size_t const max_bytes = 1 << 30; - size_t bytes_left = len; - unsigned char* data = buf; - while (bytes_left > 0) - { - size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left); - switch (bits) - { - case 256: - sph_sha256(&this->ctx256, data, bytes); - break; - case 384: - sph_sha384(&this->ctx384, data, bytes); - break; - case 512: - sph_sha512(&this->ctx512, data, bytes); - break; - default: - badBits(); - break; - } - bytes_left -= bytes; - data += bytes; - } - - if (this->getNext(true)) + switch (bits) { - this->getNext()->write(buf, len); + case 256: + sph_sha256(&this->ctx256, buf, len); + break; + case 384: + sph_sha384(&this->ctx384, buf, len); + break; + case 512: + sph_sha512(&this->ctx512, buf, len); + break; + default: + badBits(); + break; } } void -Pl_SHA2::finish() +SHA2_native::finalize() { - if (this->getNext(true)) - { - this->getNext()->finish(); - } switch (bits) { case 256: @@ -103,26 +69,10 @@ Pl_SHA2::finish() badBits(); break; } - this->in_progress = false; -} - -void -Pl_SHA2::resetBits(int bits) -{ - if (this->in_progress) - { - throw std::logic_error( - "bit reset requested for in-progress SHA2 Pipeline"); - } - if (! ((bits == 256) || (bits == 384) || (bits == 512))) - { - throw std::logic_error("Pl_SHA2 called with bits != 256, 384, or 512"); - } - this->bits = bits; } std::string -Pl_SHA2::getRawDigest() +SHA2_native::getRawDigest() { std::string result; switch (bits) @@ -145,14 +95,3 @@ Pl_SHA2::getRawDigest() } return result; } - -std::string -Pl_SHA2::getHexDigest() -{ - if (this->in_progress) - { - throw std::logic_error( - "digest requested for in-progress SHA2 Pipeline"); - } - return QUtil::hex_encode(getRawDigest()); -} -- cgit v1.2.3-70-g09d2