aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/SHA2_native.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2019-11-04 15:55:43 +0100
committerJay Berkenbilt <ejb@ql.org>2019-11-09 14:18:02 +0100
commitbb427bd11774f47f553257cdc0693f77b559654d (patch)
tree379100926074a82f81f24d0d6cbe41631b71cc50 /libqpdf/SHA2_native.cc
parenteadc222ff9087c8dd41a7afced717a65802e849c (diff)
downloadqpdf-bb427bd11774f47f553257cdc0693f77b559654d.tar.zst
SHA2: switch to pluggable crypto
Diffstat (limited to 'libqpdf/SHA2_native.cc')
-rw-r--r--libqpdf/SHA2_native.cc131
1 files changed, 35 insertions, 96 deletions
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 <qpdf/Pl_SHA2.hh>
+#include <qpdf/SHA2_native.hh>
#include <stdexcept>
#include <cstdio>
#include <qpdf/PointerHolder.hh>
#include <qpdf/QUtil.hh>
-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());
-}