From ef25649c3d6a535706d1039aba128e1b22341f91 Mon Sep 17 00:00:00 2001 From: Matt Emmerton Date: Wed, 29 Jun 2011 20:22:02 +0200 Subject: Make private sha2 functions static (freebsd which can be considered the "official" upstream has applied it) --- apt-pkg/contrib/sha2_internal.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/sha2_internal.cc b/apt-pkg/contrib/sha2_internal.cc index 10b82dec4..565db2f91 100644 --- a/apt-pkg/contrib/sha2_internal.cc +++ b/apt-pkg/contrib/sha2_internal.cc @@ -219,9 +219,9 @@ typedef u_int64_t sha2_word64; /* Exactly 8 bytes */ * library -- they are intended for private internal visibility/use * only. */ -void SHA512_Last(SHA512_CTX*); -void SHA256_Transform(SHA256_CTX*, const sha2_word32*); -void SHA512_Transform(SHA512_CTX*, const sha2_word64*); +static void SHA512_Last(SHA512_CTX*); +static void SHA256_Transform(SHA256_CTX*, const sha2_word32*); +static void SHA512_Transform(SHA512_CTX*, const sha2_word64*); /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ @@ -379,7 +379,7 @@ void SHA256_Init(SHA256_CTX* context) { (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ j++ -void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) { +static void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) { sha2_word32 a, b, c, d, e, f, g, h, s0, s1; sha2_word32 T1, *W256; int j; @@ -437,7 +437,7 @@ void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) { #else /* SHA2_UNROLL_TRANSFORM */ -void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) { +static void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) { sha2_word32 a, b, c, d, e, f, g, h, s0, s1; sha2_word32 T1, T2, *W256; int j; @@ -706,7 +706,7 @@ void SHA512_Init(SHA512_CTX* context) { (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \ j++ -void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) { +static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) { sha2_word64 a, b, c, d, e, f, g, h, s0, s1; sha2_word64 T1, *W512 = (sha2_word64*)context->buffer; int j; @@ -761,7 +761,7 @@ void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) { #else /* SHA2_UNROLL_TRANSFORM */ -void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) { +static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) { sha2_word64 a, b, c, d, e, f, g, h, s0, s1; sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer; int j; @@ -887,7 +887,7 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) { usedspace = freespace = 0; } -void SHA512_Last(SHA512_CTX* context) { +static void SHA512_Last(SHA512_CTX* context) { unsigned int usedspace; usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; -- cgit v1.2.3 From c31c1dded85ee1e88231a041aac7e507f2ed426c Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 13 Jul 2011 16:37:15 +0200 Subject: move implementation of checksums around by abstracting even more --- apt-pkg/contrib/hashsum.cc | 28 +++++++++++++++++++++++++ apt-pkg/contrib/hashsum_template.h | 20 ++++++++++++++++++ apt-pkg/contrib/md5.cc | 23 -------------------- apt-pkg/contrib/md5.h | 16 ++++++-------- apt-pkg/contrib/sha1.cc | 23 -------------------- apt-pkg/contrib/sha1.h | 11 +++------- apt-pkg/contrib/sha2.cc | 43 -------------------------------------- apt-pkg/contrib/sha2.h | 27 +++++++----------------- 8 files changed, 65 insertions(+), 126 deletions(-) create mode 100644 apt-pkg/contrib/hashsum.cc delete mode 100644 apt-pkg/contrib/sha2.cc (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/hashsum.cc b/apt-pkg/contrib/hashsum.cc new file mode 100644 index 000000000..b97eaf831 --- /dev/null +++ b/apt-pkg/contrib/hashsum.cc @@ -0,0 +1,28 @@ +// Cryptographic API Base + +#include +#include "hashsum_template.h" + +// Summation::AddFD - Add content of file into the checksum /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool SummationImplementation::AddFD(int const Fd, unsigned long Size) { + unsigned char Buf[64 * 64]; + int Res = 0; + int ToEOF = (Size == 0); + unsigned long n = sizeof(Buf); + if (!ToEOF) + n = std::min(Size, n); + while (Size != 0 || ToEOF) + { + Res = read(Fd, Buf, n); + if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read + return false; + if (ToEOF && Res == 0) // EOF + break; + Size -= Res; + Add(Buf,Res); + } + return true; +} + /*}}}*/ diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h index 7667baf92..2847f3308 100644 --- a/apt-pkg/contrib/hashsum_template.h +++ b/apt-pkg/contrib/hashsum_template.h @@ -84,4 +84,24 @@ class HashSumValue } }; +class SummationImplementation +{ + public: + virtual bool Add(const unsigned char *inbuf, unsigned long inlen) = 0; + inline bool Add(const char *inbuf, unsigned long const inlen) + { return Add((unsigned char *)inbuf, inlen); }; + + inline bool Add(const unsigned char *Data) + { return Add(Data, strlen((const char *)Data)); }; + inline bool Add(const char *Data) + { return Add((const unsigned char *)Data, strlen((const char *)Data)); }; + + inline bool Add(const unsigned char *Beg, const unsigned char *End) + { return Add(Beg, End - Beg); }; + inline bool Add(const char *Beg, const char *End) + { return Add((const unsigned char *)Beg, End - Beg); }; + + bool AddFD(int Fd, unsigned long Size); +}; + #endif diff --git a/apt-pkg/contrib/md5.cc b/apt-pkg/contrib/md5.cc index 6820d3951..65e20e9bb 100644 --- a/apt-pkg/contrib/md5.cc +++ b/apt-pkg/contrib/md5.cc @@ -231,29 +231,6 @@ bool MD5Summation::Add(const unsigned char *data,unsigned long len) return true; } /*}}}*/ -// MD5Summation::AddFD - Add the contents of a FD to the hash /*{{{*/ -// --------------------------------------------------------------------- -/* */ -bool MD5Summation::AddFD(int Fd,unsigned long Size) -{ - unsigned char Buf[64*64]; - int Res = 0; - int ToEOF = (Size == 0); - while (Size != 0 || ToEOF) - { - unsigned n = sizeof(Buf); - if (!ToEOF) n = min(Size,(unsigned long)n); - Res = read(Fd,Buf,n); - if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read - return false; - if (ToEOF && Res == 0) // EOF - break; - Size -= Res; - Add(Buf,Res); - } - return true; -} - /*}}}*/ // MD5Summation::Result - Returns the value of the sum /*{{{*/ // --------------------------------------------------------------------- /* Because this must add in the last bytes of the series it prevents anyone diff --git a/apt-pkg/contrib/md5.h b/apt-pkg/contrib/md5.h index 9cc88cfbe..e76428325 100644 --- a/apt-pkg/contrib/md5.h +++ b/apt-pkg/contrib/md5.h @@ -34,26 +34,22 @@ using std::min; #include "hashsum_template.h" -class MD5Summation; - typedef HashSumValue<128> MD5SumValue; -class MD5Summation +class MD5Summation : public SummationImplementation { uint32_t Buf[4]; unsigned char Bytes[2*4]; unsigned char In[16*4]; bool Done; - + public: - bool Add(const unsigned char *Data,unsigned long Size); - inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));}; - bool AddFD(int Fd,unsigned long Size); - inline bool Add(const unsigned char *Beg,const unsigned char *End) - {return Add(Beg,End-Beg);}; + bool Add(const unsigned char *inbuf, unsigned long inlen); + using SummationImplementation::Add; + MD5SumValue Result(); - + MD5Summation(); }; diff --git a/apt-pkg/contrib/sha1.cc b/apt-pkg/contrib/sha1.cc index 9a6725ef3..4b0552102 100644 --- a/apt-pkg/contrib/sha1.cc +++ b/apt-pkg/contrib/sha1.cc @@ -273,26 +273,3 @@ bool SHA1Summation::Add(const unsigned char *data,unsigned long len) return true; } /*}}}*/ -// SHA1Summation::AddFD - Add content of file into the checksum /*{{{*/ -// --------------------------------------------------------------------- -/* */ -bool SHA1Summation::AddFD(int Fd,unsigned long Size) -{ - unsigned char Buf[64 * 64]; - int Res = 0; - int ToEOF = (Size == 0); - while (Size != 0 || ToEOF) - { - unsigned n = sizeof(Buf); - if (!ToEOF) n = min(Size,(unsigned long)n); - Res = read(Fd,Buf,n); - if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read - return false; - if (ToEOF && Res == 0) // EOF - break; - Size -= Res; - Add(Buf,Res); - } - return true; -} - /*}}}*/ diff --git a/apt-pkg/contrib/sha1.h b/apt-pkg/contrib/sha1.h index e7683fa7b..2701fc67e 100644 --- a/apt-pkg/contrib/sha1.h +++ b/apt-pkg/contrib/sha1.h @@ -23,11 +23,9 @@ using std::min; #include "hashsum_template.h" -class SHA1Summation; - typedef HashSumValue<160> SHA1SumValue; -class SHA1Summation +class SHA1Summation : public SummationImplementation { /* assumes 64-bit alignment just in case */ unsigned char Buffer[64] __attribute__((aligned(8))); @@ -36,12 +34,9 @@ class SHA1Summation bool Done; public: + bool Add(const unsigned char *inbuf, unsigned long inlen); + using SummationImplementation::Add; - bool Add(const unsigned char *inbuf,unsigned long inlen); - inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));}; - bool AddFD(int Fd,unsigned long Size); - inline bool Add(const unsigned char *Beg,const unsigned char *End) - {return Add(Beg,End-Beg);}; SHA1SumValue Result(); SHA1Summation(); diff --git a/apt-pkg/contrib/sha2.cc b/apt-pkg/contrib/sha2.cc deleted file mode 100644 index 4604d3167..000000000 --- a/apt-pkg/contrib/sha2.cc +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Cryptographic API. {{{ - * - * SHA-512, as specified in - * http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) - * any later version. - * - */ /*}}}*/ - -#ifdef __GNUG__ -#pragma implementation "apt-pkg/sha2.h" -#endif - -#include -#include - -// SHA2Summation::AddFD - Add content of file into the checksum /*{{{*/ -// --------------------------------------------------------------------- -/* */ -bool SHA2SummationBase::AddFD(int Fd,unsigned long Size){ - unsigned char Buf[64 * 64]; - int Res = 0; - int ToEOF = (Size == 0); - while (Size != 0 || ToEOF) - { - unsigned n = sizeof(Buf); - if (!ToEOF) n = min(Size,(unsigned long)n); - Res = read(Fd,Buf,n); - if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read - return false; - if (ToEOF && Res == 0) // EOF - break; - Size -= Res; - Add(Buf,Res); - } - return true; -} - /*}}}*/ - diff --git a/apt-pkg/contrib/sha2.h b/apt-pkg/contrib/sha2.h index bd5472527..386225889 100644 --- a/apt-pkg/contrib/sha2.h +++ b/apt-pkg/contrib/sha2.h @@ -22,31 +22,16 @@ #include "sha2_internal.h" #include "hashsum_template.h" -using std::string; -using std::min; - -class SHA512Summation; -class SHA256Summation; - typedef HashSumValue<512> SHA512SumValue; typedef HashSumValue<256> SHA256SumValue; -class SHA2SummationBase +class SHA2SummationBase : public SummationImplementation { protected: bool Done; public: - virtual bool Add(const unsigned char *inbuf,unsigned long inlen) = 0; - virtual bool AddFD(int Fd,unsigned long Size); + bool Add(const unsigned char *inbuf, unsigned long len) = 0; - inline bool Add(const char *Data) - { - return Add((unsigned char *)Data,strlen(Data)); - }; - inline bool Add(const unsigned char *Beg,const unsigned char *End) - { - return Add(Beg,End-Beg); - }; void Result(); }; @@ -56,13 +41,15 @@ class SHA256Summation : public SHA2SummationBase unsigned char Sum[32]; public: - virtual bool Add(const unsigned char *inbuf, unsigned long len) + bool Add(const unsigned char *inbuf, unsigned long len) { if (Done) return false; SHA256_Update(&ctx, inbuf, len); return true; }; + using SummationImplementation::Add; + SHA256SumValue Result() { if (!Done) { @@ -86,13 +73,15 @@ class SHA512Summation : public SHA2SummationBase unsigned char Sum[64]; public: - virtual bool Add(const unsigned char *inbuf, unsigned long len) + bool Add(const unsigned char *inbuf, unsigned long len) { if (Done) return false; SHA512_Update(&ctx, inbuf, len); return true; }; + using SummationImplementation::Add; + SHA512SumValue Result() { if (!Done) { -- cgit v1.2.3 From 1dab797ca6dc0357474675a0f132c962dee4a2c2 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 13 Jul 2011 23:10:38 +0200 Subject: enable Hashes::AddFD() to skip creation of certain hashes --- apt-pkg/contrib/hashes.cc | 19 ++++++++++++------- apt-pkg/contrib/hashes.h | 5 ++++- apt-pkg/contrib/hashsum.cc | 5 ++--- apt-pkg/contrib/hashsum_template.h | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index 66ae33146..d217747df 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -107,7 +107,8 @@ string HashString::toStr() const // Hashes::AddFD - Add the contents of the FD /*{{{*/ // --------------------------------------------------------------------- /* */ -bool Hashes::AddFD(int Fd,unsigned long Size) +bool Hashes::AddFD(int const Fd,unsigned long Size, bool const addMD5, + bool const addSHA1, bool const addSHA256, bool const addSHA512) { unsigned char Buf[64*64]; int Res = 0; @@ -118,14 +119,18 @@ bool Hashes::AddFD(int Fd,unsigned long Size) if (!ToEOF) n = min(Size,(unsigned long)n); Res = read(Fd,Buf,n); if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read - return false; + return false; if (ToEOF && Res == 0) // EOF - break; + break; Size -= Res; - MD5.Add(Buf,Res); - SHA1.Add(Buf,Res); - SHA256.Add(Buf,Res); - SHA512.Add(Buf,Res); + if (addMD5 == true) + MD5.Add(Buf,Res); + if (addSHA1 == true) + SHA1.Add(Buf,Res); + if (addSHA256 == true) + SHA256.Add(Buf,Res); + if (addSHA512 == true) + SHA512.Add(Buf,Res); } return true; } diff --git a/apt-pkg/contrib/hashes.h b/apt-pkg/contrib/hashes.h index 4b6a08b1f..e702fcca2 100644 --- a/apt-pkg/contrib/hashes.h +++ b/apt-pkg/contrib/hashes.h @@ -67,7 +67,10 @@ class Hashes return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size); }; inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));}; - bool AddFD(int Fd,unsigned long Size); + inline bool AddFD(int const Fd,unsigned long Size = 0) + { return AddFD(Fd, Size, true, true, true, true); }; + bool AddFD(int const Fd, unsigned long Size, bool const addMD5, + bool const addSHA1, bool const addSHA256, bool const addSHA512); inline bool Add(const unsigned char *Beg,const unsigned char *End) {return Add(Beg,End-Beg);}; }; diff --git a/apt-pkg/contrib/hashsum.cc b/apt-pkg/contrib/hashsum.cc index b97eaf831..728747d7a 100644 --- a/apt-pkg/contrib/hashsum.cc +++ b/apt-pkg/contrib/hashsum.cc @@ -10,11 +10,10 @@ bool SummationImplementation::AddFD(int const Fd, unsigned long Size) { unsigned char Buf[64 * 64]; int Res = 0; int ToEOF = (Size == 0); - unsigned long n = sizeof(Buf); - if (!ToEOF) - n = std::min(Size, n); while (Size != 0 || ToEOF) { + unsigned n = sizeof(Buf); + if (!ToEOF) n = min(Size,(unsigned long)n); Res = read(Fd, Buf, n); if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read return false; diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h index 2847f3308..85d94c2af 100644 --- a/apt-pkg/contrib/hashsum_template.h +++ b/apt-pkg/contrib/hashsum_template.h @@ -101,7 +101,7 @@ class SummationImplementation inline bool Add(const char *Beg, const char *End) { return Add((const unsigned char *)Beg, End - Beg); }; - bool AddFD(int Fd, unsigned long Size); + bool AddFD(int Fd, unsigned long Size = 0); }; #endif -- cgit v1.2.3 From 2dcf7b8f9b9e037901339ddef7d94a6a2bab90db Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 14 Jul 2011 01:44:35 +0200 Subject: fix sha512 calculation in Hashes::VerifyFiles() --- apt-pkg/contrib/hashes.cc | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index d217747df..4407574fa 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -53,31 +53,30 @@ HashString::HashString(string StringedHash) /*{{{*/ /*}}}*/ bool HashString::VerifyFile(string filename) const /*{{{*/ { - FileFd fd; - MD5Summation MD5; - SHA1Summation SHA1; - SHA256Summation SHA256; - SHA256Summation SHA512; string fileHash; FileFd Fd(filename, FileFd::ReadOnly); - if(Type == "MD5Sum") + if(Type == "MD5Sum") { + MD5Summation MD5; MD5.AddFD(Fd.Fd(), Fd.Size()); fileHash = (string)MD5.Result(); - } + } else if (Type == "SHA1") { + SHA1Summation SHA1; SHA1.AddFD(Fd.Fd(), Fd.Size()); fileHash = (string)SHA1.Result(); - } - else if (Type == "SHA256") + } + else if (Type == "SHA256") { + SHA256Summation SHA256; SHA256.AddFD(Fd.Fd(), Fd.Size()); fileHash = (string)SHA256.Result(); } - else if (Type == "SHA512") + else if (Type == "SHA512") { + SHA512Summation SHA512; SHA512.AddFD(Fd.Fd(), Fd.Size()); fileHash = (string)SHA512.Result(); } -- cgit v1.2.3