From f8eac2fafb58f58c6186afb7b4a2e6cebb16ffcc Mon Sep 17 00:00:00 2001 From: Erwin Nindl Date: Wed, 12 Dec 2007 16:55:28 +0000 Subject: codechanges in sha1authalgo --- authAlgo.cpp | 12 ++++++++++-- authAlgo.h | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/authAlgo.cpp b/authAlgo.cpp index 328a42f..cc345ca 100644 --- a/authAlgo.cpp +++ b/authAlgo.cpp @@ -31,6 +31,7 @@ #include "authAlgo.h" #include "log.h" #include "buffer.h" +#include "threadUtils.hpp" #include @@ -45,6 +46,7 @@ const char* Sha1AuthAlgo::MIN_GCRYPT_VERSION = "1.2.3"; // HMAC_SHA1 Sha1AuthAlgo::Sha1AuthAlgo() : ctx_(NULL) { + Lock lock(mutex_); gcry_error_t err; // No other library has already initialized libgcrypt. if( !gcry_control(GCRYCTL_ANY_INITIALIZATION_P) ) @@ -69,12 +71,14 @@ Sha1AuthAlgo::Sha1AuthAlgo() : ctx_(NULL) Sha1AuthAlgo::~Sha1AuthAlgo() { + Lock lock(mutex_); gcry_md_close( ctx_ ); cLog.msg(Log::PRIO_DEBUG) << "Sha1AuthAlgo::~Sha1AuthAlgo: closed hmac handler"; } void Sha1AuthAlgo::setKey(Buffer key) { + Lock lock(mutex_); gcry_error_t err; err = gcry_md_setkey( ctx_, key.getBuf(), key.getLength() ); if( err ) @@ -84,12 +88,16 @@ void Sha1AuthAlgo::setKey(Buffer key) AuthTag Sha1AuthAlgo::calc(const Buffer& buf) { + Lock lock(mutex_); // gcry_error_t err; - Buffer hmac; //80bit + Buffer hmac(10); // 10byte + gcry_mpi_t tmp = gcry_mpi_new(160); // 20byte gcry_md_write( ctx_, static_cast(buf).getBuf(), buf.getLength() ); gcry_md_final( ctx_ ); - hmac = Buffer(gcry_md_read( ctx_, 0 ), 10); + gcry_mpi_scan( &tmp, GCRYMPI_FMT_STD, gcry_md_read(ctx_, 0), 20, NULL ); + gcry_mpi_clear_highbit( tmp, 81 ); // truncate hmac from 20byte to 10byte + gcry_mpi_print( GCRYMPI_FMT_STD, hmac, hmac.getLength(), NULL, tmp ); return hmac; } diff --git a/authAlgo.h b/authAlgo.h index 1a6d431..45e2fc4 100644 --- a/authAlgo.h +++ b/authAlgo.h @@ -34,6 +34,7 @@ #include "authTag.h" #include "datatypes.h" #include "buffer.h" +#include "threadUtils.hpp" #include @@ -64,6 +65,7 @@ public: protected: static const char* MIN_GCRYPT_VERSION; gcry_md_hd_t ctx_; + Mutex mutex_; }; #endif -- cgit v1.2.3