summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib
diff options
context:
space:
mode:
authorMichael Vogt <michael.vogt@ubuntu.com>2011-07-15 09:42:37 +0200
committerMichael Vogt <michael.vogt@ubuntu.com>2011-07-15 09:42:37 +0200
commit35555c1826da5df9f2c06a74f2e91be843ad8142 (patch)
tree9127cb9461a2de987748600c08682f7051ba12b7 /apt-pkg/contrib
parentcec8c646c79e40d928464b08a6f2e3ea62bb1611 (diff)
parent4b42f43bed369817398b6c8d538f08e5bf6dff76 (diff)
merged from donkult
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r--apt-pkg/contrib/hashes.cc38
-rw-r--r--apt-pkg/contrib/hashes.h5
-rw-r--r--apt-pkg/contrib/hashsum.cc27
-rw-r--r--apt-pkg/contrib/hashsum_template.h20
-rw-r--r--apt-pkg/contrib/md5.cc23
-rw-r--r--apt-pkg/contrib/md5.h16
-rw-r--r--apt-pkg/contrib/sha1.cc23
-rw-r--r--apt-pkg/contrib/sha1.h11
-rw-r--r--apt-pkg/contrib/sha2.cc43
-rw-r--r--apt-pkg/contrib/sha2.h27
-rw-r--r--apt-pkg/contrib/sha2_internal.cc16
11 files changed, 97 insertions, 152 deletions
diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc
index 66ae33146..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();
}
@@ -107,7 +106,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 +118,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
new file mode 100644
index 000000000..728747d7a
--- /dev/null
+++ b/apt-pkg/contrib/hashsum.cc
@@ -0,0 +1,27 @@
+// Cryptographic API Base
+
+#include <unistd.h>
+#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);
+ 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/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h
index 7667baf92..85d94c2af 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 = 0);
+};
+
#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 <apt-pkg/sha2.h>
-#include <apt-pkg/strutl.h>
-
-// 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) {
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;