Browse Source

add sha512 interface based on sha2 by aaron gifford

tags/debian/0.9.0
Michael Vogt 10 years ago
parent
commit
54ce88fd26
6 changed files with 1483 additions and 8 deletions
  1. +1065
    -0
      apt-pkg/contrib/sha2.cc
  2. +197
    -0
      apt-pkg/contrib/sha2.h
  3. +128
    -0
      apt-pkg/contrib/sha512.cc
  4. +68
    -0
      apt-pkg/contrib/sha512.h
  5. +6
    -2
      apt-pkg/makefile
  6. +19
    -6
      test/hash.cc

+ 1065
- 0
apt-pkg/contrib/sha2.cc
File diff suppressed because it is too large
View File


+ 197
- 0
apt-pkg/contrib/sha2.h View File

@@ -0,0 +1,197 @@
/*
* FILE: sha2.h
* AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
*
* Copyright (c) 2000-2001, Aaron D. Gifford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
*/

#ifndef __SHA2_H__
#define __SHA2_H__

#ifdef __cplusplus
extern "C" {
#endif


/*
* Import u_intXX_t size_t type definitions from system headers. You
* may need to change this, or define these things yourself in this
* file.
*/
#include <sys/types.h>

#ifdef SHA2_USE_INTTYPES_H

#include <inttypes.h>

#endif /* SHA2_USE_INTTYPES_H */


/*** SHA-256/384/512 Various Length Definitions ***********************/
#define SHA256_BLOCK_LENGTH 64
#define SHA256_DIGEST_LENGTH 32
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
#define SHA384_BLOCK_LENGTH 128
#define SHA384_DIGEST_LENGTH 48
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
#define SHA512_BLOCK_LENGTH 128
#define SHA512_DIGEST_LENGTH 64
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)


/*** SHA-256/384/512 Context Structures *******************************/
/* NOTE: If your architecture does not define either u_intXX_t types or
* uintXX_t (from inttypes.h), you may need to define things by hand
* for your system:
*/
#if 0
typedef unsigned char u_int8_t; /* 1-byte (8-bits) */
typedef unsigned int u_int32_t; /* 4-bytes (32-bits) */
typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */
#endif
/*
* Most BSD systems already define u_intXX_t types, as does Linux.
* Some systems, however, like Compaq's Tru64 Unix instead can use
* uintXX_t types defined by very recent ANSI C standards and included
* in the file:
*
* #include <inttypes.h>
*
* If you choose to use <inttypes.h> then please define:
*
* #define SHA2_USE_INTTYPES_H
*
* Or on the command line during compile:
*
* cc -DSHA2_USE_INTTYPES_H ...
*/
#ifdef SHA2_USE_INTTYPES_H

typedef struct _SHA256_CTX {
uint32_t state[8];
uint64_t bitcount;
uint8_t buffer[SHA256_BLOCK_LENGTH];
} SHA256_CTX;
typedef struct _SHA512_CTX {
uint64_t state[8];
uint64_t bitcount[2];
uint8_t buffer[SHA512_BLOCK_LENGTH];
} SHA512_CTX;

#else /* SHA2_USE_INTTYPES_H */

typedef struct _SHA256_CTX {
u_int32_t state[8];
u_int64_t bitcount;
u_int8_t buffer[SHA256_BLOCK_LENGTH];
} SHA256_CTX;
typedef struct _SHA512_CTX {
u_int64_t state[8];
u_int64_t bitcount[2];
u_int8_t buffer[SHA512_BLOCK_LENGTH];
} SHA512_CTX;

#endif /* SHA2_USE_INTTYPES_H */

typedef SHA512_CTX SHA384_CTX;


/*** SHA-256/384/512 Function Prototypes ******************************/
#ifndef NOPROTO
#ifdef SHA2_USE_INTTYPES_H

void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);

void SHA384_Init(SHA384_CTX*);
void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);

void SHA512_Init(SHA512_CTX*);
void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);

#else /* SHA2_USE_INTTYPES_H */

void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t);
void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);

void SHA384_Init(SHA384_CTX*);
void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t);
void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);

void SHA512_Init(SHA512_CTX*);
void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t);
void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);

#endif /* SHA2_USE_INTTYPES_H */

#else /* NOPROTO */

void SHA256_Init();
void SHA256_Update();
void SHA256_Final();
char* SHA256_End();
char* SHA256_Data();

void SHA384_Init();
void SHA384_Update();
void SHA384_Final();
char* SHA384_End();
char* SHA384_Data();

void SHA512_Init();
void SHA512_Update();
void SHA512_Final();
char* SHA512_End();
char* SHA512_Data();

#endif /* NOPROTO */

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __SHA2_H__ */


+ 128
- 0
apt-pkg/contrib/sha512.cc View File

@@ -0,0 +1,128 @@
/*
* 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/sha512.h"
#endif

#include <apt-pkg/sha512.h>
#include <apt-pkg/sha2.h>
#include <apt-pkg/strutl.h>

SHA512Summation::SHA512Summation() /*{{{*/
{
SHA512_Init(&ctx);
Done = false;
}
/*}}}*/
bool SHA512Summation::Add(const unsigned char *inbuf,unsigned long len) /*{{{*/
{
if (Done)
return false;
SHA512_Update(&ctx, inbuf, len);
return true;
}
/*}}}*/
SHA512SumValue SHA512Summation::Result() /*{{{*/
{
if (!Done) {
SHA512_Final(Sum, &ctx);
Done = true;
}

SHA512SumValue res;
res.Set(Sum);
return res;
}
/*}}}*/
// SHA512SumValue::SHA512SumValue - Constructs the sum from a string /*{{{*/
// ---------------------------------------------------------------------
/* The string form of a SHA512 is a 64 character hex number */
SHA512SumValue::SHA512SumValue(string Str)
{
memset(Sum,0,sizeof(Sum));
Set(Str);
}
/*}}}*/
// SHA512SumValue::SHA512SumValue - Default constructor /*{{{*/
// ---------------------------------------------------------------------
/* Sets the value to 0 */
SHA512SumValue::SHA512SumValue()
{
memset(Sum,0,sizeof(Sum));
}
/*}}}*/
// SHA512SumValue::Set - Set the sum from a string /*{{{*/
// ---------------------------------------------------------------------
/* Converts the hex string into a set of chars */
bool SHA512SumValue::Set(string Str)
{
return Hex2Num(Str,Sum,sizeof(Sum));
}
/*}}}*/
// SHA512SumValue::Value - Convert the number into a string /*{{{*/
// ---------------------------------------------------------------------
/* Converts the set of chars into a hex string in lower case */
string SHA512SumValue::Value() const
{
char Conv[16] =
{ '0','1','2','3','4','5','6','7','8','9','a','b',
'c','d','e','f'
};
char Result[129];
Result[128] = 0;

// Convert each char into two letters
int J = 0;
int I = 0;
for (; I != 128; J++,I += 2)
{
Result[I] = Conv[Sum[J] >> 4];
Result[I + 1] = Conv[Sum[J] & 0xF];
}

return string(Result);
}
/*}}}*/
// SHA512SumValue::operator == - Comparator /*{{{*/
// ---------------------------------------------------------------------
/* Call memcmp on the buffer */
bool SHA512SumValue::operator == (const SHA512SumValue & rhs) const
{
return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
}
/*}}}*/
// SHA512Summation::AddFD - Add content of file into the checksum /*{{{*/
// ---------------------------------------------------------------------
/* */
bool SHA512Summation::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;
}
/*}}}*/


+ 68
- 0
apt-pkg/contrib/sha512.h View File

@@ -0,0 +1,68 @@
// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
// $Id: sha512.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
/* ######################################################################

SHA512SumValue - Storage for a SHA-512 hash.
SHA512Summation - SHA-512 Secure Hash Algorithm.
This is a C++ interface to a set of SHA512Sum functions, that mirrors
the equivalent MD5 & SHA1 classes.

##################################################################### */
/*}}}*/
#ifndef APTPKG_SHA512_H
#define APTPKG_SHA512_H

#include <string>
#include <cstring>
#include <algorithm>
#include <stdint.h>

#include "sha2.h"

using std::string;
using std::min;

class SHA512Summation;

class SHA512SumValue
{
friend class SHA512Summation;
unsigned char Sum[64];
public:

// Accessors
bool operator ==(const SHA512SumValue &rhs) const;
string Value() const;
inline void Value(unsigned char S[64])
{for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
inline operator string() const {return Value();};
bool Set(string Str);
inline void Set(unsigned char S[64])
{for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};

SHA512SumValue(string Str);
SHA512SumValue();
};

class SHA512Summation
{
SHA512_CTX ctx;
unsigned char Sum[64];
bool Done;

public:

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);};
SHA512SumValue Result();
SHA512Summation();
};

#endif

+ 6
- 2
apt-pkg/makefile View File

@@ -20,11 +20,15 @@ APT_DOMAIN:=libapt-pkg$(LIBAPTPKG_MAJOR)
# Source code for the contributed non-core things
SOURCE = contrib/mmap.cc contrib/error.cc contrib/strutl.cc \
contrib/configuration.cc contrib/progress.cc contrib/cmndline.cc \
contrib/md5.cc contrib/sha1.cc contrib/sha256.cc contrib/hashes.cc \
contrib/md5.cc contrib/sha1.cc contrib/sha256.cc contrib/sha2.cc \
contrib/sha512.cc \
contrib/hashes.cc \
contrib/cdromutl.cc contrib/crc-16.cc contrib/netrc.cc \
contrib/fileutl.cc
HEADERS = mmap.h error.h configuration.h fileutl.h cmndline.h netrc.h\
md5.h crc-16.h cdromutl.h strutl.h sptr.h sha1.h sha256.h hashes.h \
md5.h crc-16.h cdromutl.h strutl.h sptr.h sha1.h sha256.h sha2.h \
sha512.h\
hashes.h \
macros.h weakptr.h

# Source code for the core main library


+ 19
- 6
test/hash.cc View File

@@ -1,6 +1,7 @@
#include <apt-pkg/md5.h>
#include <apt-pkg/sha1.h>
#include <apt-pkg/sha256.h>
#include <apt-pkg/sha512.h>
#include <apt-pkg/strutl.h>
#include <iostream>

@@ -10,9 +11,17 @@ template <class T> void Test(const char *In,const char *Out)
{
T Sum;
Sum.Add(In);
cout << Sum.Result().Value() << endl;
if (stringcasecmp(Sum.Result().Value(),Out) != 0)

cout << "expected: '" << Out << "'" << endl;
cout << "got : '" << Sum.Result().Value() << "'" << endl;
cout << "got : '" << Sum.Result().Value() << "'" << endl;
cout << "got : '" << Sum.Result().Value() << "'" << endl;
if (stringcasecmp(Sum.Result().Value(), Out) != 0) {
cout << "FAIL" << endl << endl;
abort();
} else {
cout << "PASS" << endl << endl;
}
}

template <class T> void TestMill(const char *Out)
@@ -34,9 +43,8 @@ template <class T> void TestMill(const char *Out)
Count = 0;
}
}
cout << Sum.Result().Value() << endl;
if (stringcasecmp(Sum.Result().Value(),Out) != 0)

if (stringcasecmp(Sum.Result().Value(), Out) != 0)
abort();
}

@@ -62,8 +70,13 @@ int main()
// SHA-256, From FIPS 180-2
Test<SHA256Summation>("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");

// SHA-512, From
Test<SHA512Summation>(
"abc",
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
return 0;
}



Loading…
Cancel
Save