Merge topic 'crypto-hash'

0599c5f sha2: Zero entire SHA_CTX structure during cleanup
0a6705c sha2: Suppress -Wcast-align warning from Clang
24b1feb sha2: Cast safe conversions to smaller integer types
1ec3fa0 Merge branch 'update-KWIML' into crypto-hash
6495b59 cmCryptoHash: Add virtual destructor
8302608 Fix CMake.File hash test for CRLF checkouts
9fb1a9c sha2: Wrap long lines in third-party declarations
b0853b5 Disable file() and string() hash commands during bootstrap
9da8340 sha2: Suppress Borland warnings in third-party code
23b3df7 sha2: Use KWIML fixed-size integer constant macros
2e9c26c Add string(MD5) and string(SHA*) commands to compute hashes
293a7f4 cmCryptoHash: Provide factory "New" method
46ab056 sha2: Use "static const" instead of "const static" declarations
38771d3 Add file(SHA*) commands to compute cryptographic hashes
73efd4a sha2: Build as part of CMakeLib
c1856a3 sha2: Use KWIML fixed-size integer types and endian-ness
fcc3ce5 Merge branch 'import-sha2' into crypto-hash
042f796 Add file(MD5) command to compute cryptographic hash
ed7cef5 Factor Compute(File|String)MD5 into cmCryptoHash helper
8251b20 Import sha2 implementation 1.1 from Aaron D. Gifford
9912c41 Import sha2 implementation 1.0 from Aaron D. Gifford
This commit is contained in:
David Cole 2011-11-22 16:24:21 -05:00 committed by CMake Topic Stage
commit d9df0cca66
36 changed files with 2202 additions and 51 deletions

2
Source/.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
# Preserve upstream indentation style.
cm_sha2.* whitespace=indent-with-non-tab

View File

@ -129,6 +129,8 @@ SET(SRCS
cmComputeLinkInformation.h cmComputeLinkInformation.h
cmComputeTargetDepends.h cmComputeTargetDepends.h
cmComputeTargetDepends.cxx cmComputeTargetDepends.cxx
cmCryptoHash.cxx
cmCryptoHash.h
cmCustomCommand.cxx cmCustomCommand.cxx
cmCustomCommand.h cmCustomCommand.h
cmCustomCommandGenerator.cxx cmCustomCommandGenerator.cxx
@ -259,6 +261,8 @@ SET(SRCS
cmakewizard.cxx cmakewizard.cxx
cmakewizard.h cmakewizard.h
cm_sha2.h
cm_sha2.c
cm_utf8.h cm_utf8.h
cm_utf8.c cm_utf8.c
) )

130
Source/cmCryptoHash.cxx Normal file
View File

@ -0,0 +1,130 @@
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmCryptoHash.h"
#include <cmsys/MD5.h>
#include "cm_sha2.h"
//----------------------------------------------------------------------------
cmsys::auto_ptr<cmCryptoHash> cmCryptoHash::New(const char* algo)
{
if(strcmp(algo,"MD5") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashMD5); }
else if(strcmp(algo,"SHA1") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA1); }
else if(strcmp(algo,"SHA224") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA224); }
else if(strcmp(algo,"SHA256") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA256); }
else if(strcmp(algo,"SHA384") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA384); }
else if(strcmp(algo,"SHA512") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA512); }
else
{ return cmsys::auto_ptr<cmCryptoHash>(0); }
}
//----------------------------------------------------------------------------
std::string cmCryptoHash::HashString(const char* input)
{
this->Initialize();
this->Append(reinterpret_cast<unsigned char const*>(input),
static_cast<int>(strlen(input)));
return this->Finalize();
}
//----------------------------------------------------------------------------
std::string cmCryptoHash::HashFile(const char* file)
{
std::ifstream fin(file, std::ios::in | cmsys_ios_binary);
if(!fin)
{
return "";
}
this->Initialize();
// Should be efficient enough on most system:
const int bufferSize = 4096;
char buffer[bufferSize];
unsigned char const* buffer_uc =
reinterpret_cast<unsigned char const*>(buffer);
// This copy loop is very sensitive on certain platforms with
// slightly broken stream libraries (like HPUX). Normally, it is
// incorrect to not check the error condition on the fin.read()
// before using the data, but the fin.gcount() will be zero if an
// error occurred. Therefore, the loop should be safe everywhere.
while(fin)
{
fin.read(buffer, bufferSize);
if(int gcount = static_cast<int>(fin.gcount()))
{
this->Append(buffer_uc, gcount);
}
}
if(fin.eof())
{
return this->Finalize();
}
return "";
}
//----------------------------------------------------------------------------
cmCryptoHashMD5::cmCryptoHashMD5(): MD5(cmsysMD5_New())
{
}
//----------------------------------------------------------------------------
cmCryptoHashMD5::~cmCryptoHashMD5()
{
cmsysMD5_Delete(this->MD5);
}
//----------------------------------------------------------------------------
void cmCryptoHashMD5::Initialize()
{
cmsysMD5_Initialize(this->MD5);
}
//----------------------------------------------------------------------------
void cmCryptoHashMD5::Append(unsigned char const* buf, int sz)
{
cmsysMD5_Append(this->MD5, buf, sz);
}
//----------------------------------------------------------------------------
std::string cmCryptoHashMD5::Finalize()
{
char md5out[32];
cmsysMD5_FinalizeHex(this->MD5, md5out);
return std::string(md5out, 32);
}
#define cmCryptoHash_SHA_CLASS_IMPL(SHA) \
cmCryptoHash##SHA::cmCryptoHash##SHA(): SHA(new SHA_CTX) {} \
cmCryptoHash##SHA::~cmCryptoHash##SHA() { delete this->SHA; } \
void cmCryptoHash##SHA::Initialize() { SHA##_Init(this->SHA); } \
void cmCryptoHash##SHA::Append(unsigned char const* buf, int sz) \
{ SHA##_Update(this->SHA, buf, sz); } \
std::string cmCryptoHash##SHA::Finalize() \
{ \
char out[SHA##_DIGEST_STRING_LENGTH]; \
SHA##_End(this->SHA, out); \
return std::string(out, SHA##_DIGEST_STRING_LENGTH-1); \
}
cmCryptoHash_SHA_CLASS_IMPL(SHA1)
cmCryptoHash_SHA_CLASS_IMPL(SHA224)
cmCryptoHash_SHA_CLASS_IMPL(SHA256)
cmCryptoHash_SHA_CLASS_IMPL(SHA384)
cmCryptoHash_SHA_CLASS_IMPL(SHA512)

65
Source/cmCryptoHash.h Normal file
View File

@ -0,0 +1,65 @@
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#ifndef cmCryptoHash_h
#define cmCryptoHash_h
#include "cmStandardIncludes.h"
#include <cmsys/auto_ptr.hxx>
class cmCryptoHash
{
public:
virtual ~cmCryptoHash() {}
static cmsys::auto_ptr<cmCryptoHash> New(const char* algo);
std::string HashString(const char* input);
std::string HashFile(const char* file);
protected:
virtual void Initialize()=0;
virtual void Append(unsigned char const*, int)=0;
virtual std::string Finalize()=0;
};
class cmCryptoHashMD5: public cmCryptoHash
{
struct cmsysMD5_s* MD5;
public:
cmCryptoHashMD5();
~cmCryptoHashMD5();
protected:
virtual void Initialize();
virtual void Append(unsigned char const* buf, int sz);
virtual std::string Finalize();
};
#define cmCryptoHash_SHA_CLASS_DECL(SHA) \
class cmCryptoHash##SHA: public cmCryptoHash \
{ \
union _SHA_CTX* SHA; \
public: \
cmCryptoHash##SHA(); \
~cmCryptoHash##SHA(); \
protected: \
virtual void Initialize(); \
virtual void Append(unsigned char const* buf, int sz); \
virtual std::string Finalize(); \
}
cmCryptoHash_SHA_CLASS_DECL(SHA1);
cmCryptoHash_SHA_CLASS_DECL(SHA224);
cmCryptoHash_SHA_CLASS_DECL(SHA256);
cmCryptoHash_SHA_CLASS_DECL(SHA384);
cmCryptoHash_SHA_CLASS_DECL(SHA512);
#undef cmCryptoHash_SHA_CLASS_DECL
#endif

View File

@ -13,6 +13,7 @@
#include "cmake.h" #include "cmake.h"
#include "cmHexFileConverter.h" #include "cmHexFileConverter.h"
#include "cmFileTimeComparison.h" #include "cmFileTimeComparison.h"
#include "cmCryptoHash.h"
#if defined(CMAKE_BUILD_WITH_CMAKE) #if defined(CMAKE_BUILD_WITH_CMAKE)
#include "cm_curl.h" #include "cm_curl.h"
@ -22,6 +23,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <cmsys/auto_ptr.hxx>
#include <cmsys/Directory.hxx> #include <cmsys/Directory.hxx>
#include <cmsys/Glob.hxx> #include <cmsys/Glob.hxx>
#include <cmsys/RegularExpression.hxx> #include <cmsys/RegularExpression.hxx>
@ -83,6 +85,15 @@ bool cmFileCommand
{ {
return this->HandleReadCommand(args); return this->HandleReadCommand(args);
} }
else if ( subCommand == "MD5" ||
subCommand == "SHA1" ||
subCommand == "SHA224" ||
subCommand == "SHA256" ||
subCommand == "SHA384" ||
subCommand == "SHA512" )
{
return this->HandleHashCommand(args);
}
else if ( subCommand == "STRINGS" ) else if ( subCommand == "STRINGS" )
{ {
return this->HandleStringsCommand(args); return this->HandleStringsCommand(args);
@ -338,6 +349,41 @@ bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
return true; return true;
} }
//----------------------------------------------------------------------------
bool cmFileCommand::HandleHashCommand(std::vector<std::string> const& args)
{
#if defined(CMAKE_BUILD_WITH_CMAKE)
if(args.size() != 3)
{
cmOStringStream e;
e << args[0] << " requires a file name and output variable";
this->SetError(e.str().c_str());
return false;
}
cmsys::auto_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
if(hash.get())
{
std::string out = hash->HashFile(args[1].c_str());
if(!out.empty())
{
this->Makefile->AddDefinition(args[2].c_str(), out.c_str());
return true;
}
cmOStringStream e;
e << args[0] << " failed to read file \"" << args[1] << "\": "
<< cmSystemTools::GetLastSystemError();
this->SetError(e.str().c_str());
}
return false;
#else
cmOStringStream e;
e << args[0] << " not available during bootstrap";
this->SetError(e.str().c_str());
return false;
#endif
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args) bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
{ {

View File

@ -65,6 +65,7 @@ public:
" file(WRITE filename \"message to write\"... )\n" " file(WRITE filename \"message to write\"... )\n"
" file(APPEND filename \"message to write\"... )\n" " file(APPEND filename \"message to write\"... )\n"
" file(READ filename variable [LIMIT numBytes] [OFFSET offset] [HEX])\n" " file(READ filename variable [LIMIT numBytes] [OFFSET offset] [HEX])\n"
" file(<MD5|SHA1|SHA224|SHA256|SHA384|SHA512> filename variable)\n"
" file(STRINGS filename variable [LIMIT_COUNT num]\n" " file(STRINGS filename variable [LIMIT_COUNT num]\n"
" [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]\n" " [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]\n"
" [LENGTH_MINIMUM numBytes] [LENGTH_MAXIMUM numBytes]\n" " [LENGTH_MINIMUM numBytes] [LENGTH_MAXIMUM numBytes]\n"
@ -94,6 +95,8 @@ public:
"variable. It will start at the given offset and read up to numBytes. " "variable. It will start at the given offset and read up to numBytes. "
"If the argument HEX is given, the binary data will be converted to " "If the argument HEX is given, the binary data will be converted to "
"hexadecimal representation and this will be stored in the variable.\n" "hexadecimal representation and this will be stored in the variable.\n"
"MD5, SHA1, SHA224, SHA256, SHA384, and SHA512 "
"will compute a cryptographic hash of the content of a file.\n"
"STRINGS will parse a list of ASCII strings from a file and " "STRINGS will parse a list of ASCII strings from a file and "
"store it in a variable. Binary data in the file are ignored. Carriage " "store it in a variable. Binary data in the file are ignored. Carriage "
"return (CR) characters are ignored. It works also for Intel Hex and " "return (CR) characters are ignored. It works also for Intel Hex and "
@ -227,6 +230,7 @@ protected:
bool HandleRemove(std::vector<std::string> const& args, bool recurse); bool HandleRemove(std::vector<std::string> const& args, bool recurse);
bool HandleWriteCommand(std::vector<std::string> const& args, bool append); bool HandleWriteCommand(std::vector<std::string> const& args, bool append);
bool HandleReadCommand(std::vector<std::string> const& args); bool HandleReadCommand(std::vector<std::string> const& args);
bool HandleHashCommand(std::vector<std::string> const& args);
bool HandleStringsCommand(std::vector<std::string> const& args); bool HandleStringsCommand(std::vector<std::string> const& args);
bool HandleGlobCommand(std::vector<std::string> const& args, bool recurse); bool HandleGlobCommand(std::vector<std::string> const& args, bool recurse);
bool HandleMakeDirectoryCommand(std::vector<std::string> const& args); bool HandleMakeDirectoryCommand(std::vector<std::string> const& args);

View File

@ -10,6 +10,8 @@
See the License for more information. See the License for more information.
============================================================================*/ ============================================================================*/
#include "cmStringCommand.h" #include "cmStringCommand.h"
#include "cmCryptoHash.h"
#include <cmsys/RegularExpression.hxx> #include <cmsys/RegularExpression.hxx>
#include <cmsys/SystemTools.hxx> #include <cmsys/SystemTools.hxx>
@ -36,6 +38,15 @@ bool cmStringCommand
{ {
return this->HandleReplaceCommand(args); return this->HandleReplaceCommand(args);
} }
else if ( subCommand == "MD5" ||
subCommand == "SHA1" ||
subCommand == "SHA224" ||
subCommand == "SHA256" ||
subCommand == "SHA384" ||
subCommand == "SHA512" )
{
return this->HandleHashCommand(args);
}
else if(subCommand == "TOLOWER") else if(subCommand == "TOLOWER")
{ {
return this->HandleToUpperLowerCommand(args, false); return this->HandleToUpperLowerCommand(args, false);
@ -82,6 +93,34 @@ bool cmStringCommand
return false; return false;
} }
//----------------------------------------------------------------------------
bool cmStringCommand::HandleHashCommand(std::vector<std::string> const& args)
{
#if defined(CMAKE_BUILD_WITH_CMAKE)
if(args.size() != 3)
{
cmOStringStream e;
e << args[0] << " requires an output variable and an input string";
this->SetError(e.str().c_str());
return false;
}
cmsys::auto_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
if(hash.get())
{
std::string out = hash->HashString(args[2].c_str());
this->Makefile->AddDefinition(args[1].c_str(), out.c_str());
return true;
}
return false;
#else
cmOStringStream e;
e << args[0] << " not available during bootstrap";
this->SetError(e.str().c_str());
return false;
#endif
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
bool cmStringCommand::HandleToUpperLowerCommand( bool cmStringCommand::HandleToUpperLowerCommand(
std::vector<std::string> const& args, bool toUpper) std::vector<std::string> const& args, bool toUpper)

View File

@ -76,6 +76,8 @@ public:
" string(REPLACE <match_string>\n" " string(REPLACE <match_string>\n"
" <replace_string> <output variable>\n" " <replace_string> <output variable>\n"
" <input> [<input>...])\n" " <input> [<input>...])\n"
" string(<MD5|SHA1|SHA224|SHA256|SHA384|SHA512>\n"
" <output variable> <input>)\n"
" string(COMPARE EQUAL <string1> <string2> <output variable>)\n" " string(COMPARE EQUAL <string1> <string2> <output variable>)\n"
" string(COMPARE NOTEQUAL <string1> <string2> <output variable>)\n" " string(COMPARE NOTEQUAL <string1> <string2> <output variable>)\n"
" string(COMPARE LESS <string1> <string2> <output variable>)\n" " string(COMPARE LESS <string1> <string2> <output variable>)\n"
@ -103,6 +105,8 @@ public:
"backslash through argument parsing.\n" "backslash through argument parsing.\n"
"REPLACE will replace all occurrences of match_string in the input with " "REPLACE will replace all occurrences of match_string in the input with "
"replace_string and store the result in the output.\n" "replace_string and store the result in the output.\n"
"MD5, SHA1, SHA224, SHA256, SHA384, and SHA512 "
"will compute a cryptographic hash of the input string.\n"
"COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and " "COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
"store true or false in the output variable.\n" "store true or false in the output variable.\n"
"ASCII will convert all numbers into corresponding ASCII characters.\n" "ASCII will convert all numbers into corresponding ASCII characters.\n"
@ -150,6 +154,7 @@ protected:
bool RegexMatch(std::vector<std::string> const& args); bool RegexMatch(std::vector<std::string> const& args);
bool RegexMatchAll(std::vector<std::string> const& args); bool RegexMatchAll(std::vector<std::string> const& args);
bool RegexReplace(std::vector<std::string> const& args); bool RegexReplace(std::vector<std::string> const& args);
bool HandleHashCommand(std::vector<std::string> const& args);
bool HandleToUpperLowerCommand(std::vector<std::string> const& args, bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
bool toUpper); bool toUpper);
bool HandleCompareCommand(std::vector<std::string> const& args); bool HandleCompareCommand(std::vector<std::string> const& args);

View File

@ -54,7 +54,7 @@
#if defined(CMAKE_BUILD_WITH_CMAKE) #if defined(CMAKE_BUILD_WITH_CMAKE)
# include <memory> // auto_ptr # include <memory> // auto_ptr
# include <fcntl.h> # include <fcntl.h>
# include <cmsys/MD5.h> # include "cmCryptoHash.h"
#endif #endif
#if defined(CMAKE_USE_ELF_PARSER) #if defined(CMAKE_USE_ELF_PARSER)
@ -1197,48 +1197,10 @@ bool cmSystemTools::RenameFile(const char* oldname, const char* newname)
bool cmSystemTools::ComputeFileMD5(const char* source, char* md5out) bool cmSystemTools::ComputeFileMD5(const char* source, char* md5out)
{ {
#if defined(CMAKE_BUILD_WITH_CMAKE) #if defined(CMAKE_BUILD_WITH_CMAKE)
if(!cmSystemTools::FileExists(source)) cmCryptoHashMD5 md5;
{ std::string str = md5.HashFile(source);
return false; strncpy(md5out, str.c_str(), 32);
} return !str.empty();
// Open files
#if defined(_WIN32) || defined(__CYGWIN__)
cmsys_ios::ifstream fin(source, cmsys_ios::ios::binary | cmsys_ios::ios::in);
#else
cmsys_ios::ifstream fin(source);
#endif
if(!fin)
{
return false;
}
cmsysMD5* md5 = cmsysMD5_New();
cmsysMD5_Initialize(md5);
// Should be efficient enough on most system:
const int bufferSize = 4096;
char buffer[bufferSize];
unsigned char const* buffer_uc =
reinterpret_cast<unsigned char const*>(buffer);
// This copy loop is very sensitive on certain platforms with
// slightly broken stream libraries (like HPUX). Normally, it is
// incorrect to not check the error condition on the fin.read()
// before using the data, but the fin.gcount() will be zero if an
// error occurred. Therefore, the loop should be safe everywhere.
while(fin)
{
fin.read(buffer, bufferSize);
if(int gcount = static_cast<int>(fin.gcount()))
{
cmsysMD5_Append(md5, buffer_uc, gcount);
}
}
cmsysMD5_FinalizeHex(md5, md5out);
cmsysMD5_Delete(md5);
fin.close();
return true;
#else #else
(void)source; (void)source;
(void)md5out; (void)md5out;
@ -1250,13 +1212,8 @@ bool cmSystemTools::ComputeFileMD5(const char* source, char* md5out)
std::string cmSystemTools::ComputeStringMD5(const char* input) std::string cmSystemTools::ComputeStringMD5(const char* input)
{ {
#if defined(CMAKE_BUILD_WITH_CMAKE) #if defined(CMAKE_BUILD_WITH_CMAKE)
char md5out[32]; cmCryptoHashMD5 md5;
cmsysMD5* md5 = cmsysMD5_New(); return md5.HashString(input);
cmsysMD5_Initialize(md5);
cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
cmsysMD5_FinalizeHex(md5, md5out);
cmsysMD5_Delete(md5);
return std::string(md5out, 32);
#else #else
(void)input; (void)input;
cmSystemTools::Message("md5sum not supported in bootstrapping mode","Error"); cmSystemTools::Message("md5sum not supported in bootstrapping mode","Error");

1613
Source/cm_sha2.c Normal file

File diff suppressed because it is too large Load Diff

140
Source/cm_sha2.h Normal file
View File

@ -0,0 +1,140 @@
/*
* FILE: sha2.h
* AUTHOR: Aaron D. Gifford
* http://www.aarongifford.com/computers/sha.html
*
* Copyright (c) 2000-2003, 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.4 2004/01/07 19:06:18 adg Exp $
*/
#ifndef __SHA2_H__
#define __SHA2_H__
#include "cm_sha2_mangle.h"
/* CMake modification: use integer types from cmIML. */
#include "cmIML/INT.h"
typedef cmIML_INT_uint8_t cm_sha2_uint8_t;
typedef cmIML_INT_uint32_t cm_sha2_uint32_t;
typedef cmIML_INT_uint64_t cm_sha2_uint64_t;
#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>
/*** SHA-224/256/384/512 Various Length Definitions *******************/
/* Digest lengths for SHA-1/224/256/384/512 */
#define SHA1_DIGEST_LENGTH 20
#define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
#define SHA224_DIGEST_LENGTH 28
#define SHA224_DIGEST_STRING_LENGTH (SHA224_DIGEST_LENGTH * 2 + 1)
#define SHA256_DIGEST_LENGTH 32
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
#define SHA384_DIGEST_LENGTH 48
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
#define SHA512_DIGEST_LENGTH 64
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
/*** SHA-224/256/384/512 Context Structures ***************************/
typedef union _SHA_CTX {
/* SHA-1 uses this part of the union: */
struct {
cm_sha2_uint32_t state[5];
cm_sha2_uint64_t bitcount;
cm_sha2_uint8_t buffer[64];
} s1;
/* SHA-224 and SHA-256 use this part of the union: */
struct {
cm_sha2_uint32_t state[8];
cm_sha2_uint64_t bitcount;
cm_sha2_uint8_t buffer[64];
} s256;
/* SHA-384 and SHA-512 use this part of the union: */
struct {
cm_sha2_uint64_t state[8];
cm_sha2_uint64_t bitcount[2];
cm_sha2_uint8_t buffer[128];
} s512;
} SHA_CTX;
/*** SHA-256/384/512 Function Prototypes ******************************/
void SHA1_Init(SHA_CTX*);
void SHA1_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA1_Final(cm_sha2_uint8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
char* SHA1_End(SHA_CTX*, char[SHA1_DIGEST_STRING_LENGTH]);
char* SHA1_Data(const cm_sha2_uint8_t*, size_t,
char[SHA1_DIGEST_STRING_LENGTH]);
void SHA224_Init(SHA_CTX*);
void SHA224_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA224_Final(cm_sha2_uint8_t[SHA224_DIGEST_LENGTH], SHA_CTX*);
char* SHA224_End(SHA_CTX*, char[SHA224_DIGEST_STRING_LENGTH]);
char* SHA224_Data(const cm_sha2_uint8_t*, size_t,
char[SHA224_DIGEST_STRING_LENGTH]);
void SHA256_Init(SHA_CTX*);
void SHA256_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA256_Final(cm_sha2_uint8_t[SHA256_DIGEST_LENGTH], SHA_CTX*);
char* SHA256_End(SHA_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const cm_sha2_uint8_t*, size_t,
char[SHA256_DIGEST_STRING_LENGTH]);
void SHA384_Init(SHA_CTX*);
void SHA384_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA384_Final(cm_sha2_uint8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);
char* SHA384_End(SHA_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const cm_sha2_uint8_t*, size_t,
char[SHA384_DIGEST_STRING_LENGTH]);
void SHA512_Init(SHA_CTX*);
void SHA512_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA512_Final(cm_sha2_uint8_t[SHA512_DIGEST_LENGTH], SHA_CTX*);
char* SHA512_End(SHA_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const cm_sha2_uint8_t*, size_t,
char[SHA512_DIGEST_STRING_LENGTH]);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __SHA2_H__ */

51
Source/cm_sha2_mangle.h Normal file
View File

@ -0,0 +1,51 @@
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2011 Kitware, Inc., Insight Software Consortium
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#ifndef cm_sha2_mangle_h
#define cm_sha2_mangle_h
/* Mangle sha2 symbol names to avoid possible conflict with
implementations in other libraries to which CMake links. */
#define SHA1_Data cmSHA1_Data
#define SHA1_End cmSHA1_End
#define SHA1_Final cmSHA1_Final
#define SHA1_Init cmSHA1_Init
#define SHA1_Internal_Transform cmSHA1_Internal_Transform
#define SHA1_Update cmSHA1_Update
#define SHA224_Data cmSHA224_Data
#define SHA224_End cmSHA224_End
#define SHA224_Final cmSHA224_Final
#define SHA224_Init cmSHA224_Init
#define SHA224_Internal_Transform cmSHA224_Internal_Transform
#define SHA224_Update cmSHA224_Update
#define SHA256_Data cmSHA256_Data
#define SHA256_End cmSHA256_End
#define SHA256_Final cmSHA256_Final
#define SHA256_Init cmSHA256_Init
#define SHA256_Internal_Init cmSHA256_Internal_Init
#define SHA256_Internal_Last cmSHA256_Internal_Last
#define SHA256_Internal_Transform cmSHA256_Internal_Transform
#define SHA256_Update cmSHA256_Update
#define SHA384_Data cmSHA384_Data
#define SHA384_End cmSHA384_End
#define SHA384_Final cmSHA384_Final
#define SHA384_Init cmSHA384_Init
#define SHA384_Update cmSHA384_Update
#define SHA512_Data cmSHA512_Data
#define SHA512_End cmSHA512_End
#define SHA512_Final cmSHA512_Final
#define SHA512_Init cmSHA512_Init
#define SHA512_Internal_Init cmSHA512_Internal_Init
#define SHA512_Internal_Last cmSHA512_Internal_Last
#define SHA512_Internal_Transform cmSHA512_Internal_Transform
#define SHA512_Update cmSHA512_Update
#endif

1
Tests/CMakeTests/.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
File-HASH-Input.txt crlf=input

View File

@ -12,7 +12,7 @@ function(check_cmake_test prefix)
) )
string(REGEX REPLACE "\n" "\n out> " out " out> ${stdout}") string(REGEX REPLACE "\n" "\n out> " out " out> ${stdout}")
string(REGEX REPLACE "\n" "\n err> " err " err> ${stderr}") string(REGEX REPLACE "\n" "\n err> " err " err> ${stderr}")
if(NOT "${result}" STREQUAL ${${test}-RESULT}) if(NOT "${result}" STREQUAL "${${test}-RESULT}")
message(FATAL_ERROR message(FATAL_ERROR
"Test ${test} result is [${result}], not [${${test}-RESULT}].\n" "Test ${test} result is [${result}], not [${${test}-RESULT}].\n"
"Test ${test} output:\n" "Test ${test} output:\n"

View File

@ -0,0 +1 @@
sample input string

View File

@ -0,0 +1 @@
file(MD5)

View File

@ -0,0 +1 @@
file(MD5 ${CMAKE_CURRENT_LIST_DIR}/File-HASH-Input.txt)

View File

@ -0,0 +1 @@
file(MD5 ${CMAKE_CURRENT_LIST_DIR}/File-HASH-Input.txt md5 extra_arg)

View File

@ -0,0 +1 @@
file(MD5 ${CMAKE_CURRENT_LIST_DIR}/DoesNotExist.cmake md5)

View File

@ -0,0 +1,2 @@
file(MD5 ${CMAKE_CURRENT_LIST_DIR}/File-HASH-Input.txt md5)
message("${md5}")

View File

@ -0,0 +1,2 @@
file(SHA1 ${CMAKE_CURRENT_LIST_DIR}/File-HASH-Input.txt sha1)
message("${sha1}")

View File

@ -0,0 +1,2 @@
file(SHA224 ${CMAKE_CURRENT_LIST_DIR}/File-HASH-Input.txt sha224)
message("${sha224}")

View File

@ -0,0 +1,2 @@
file(SHA256 ${CMAKE_CURRENT_LIST_DIR}/File-HASH-Input.txt sha256)
message("${sha256}")

View File

@ -0,0 +1,2 @@
file(SHA384 ${CMAKE_CURRENT_LIST_DIR}/File-HASH-Input.txt sha384)
message("${sha384}")

View File

@ -0,0 +1,2 @@
file(SHA512 ${CMAKE_CURRENT_LIST_DIR}/File-HASH-Input.txt sha512)
message("${sha512}")

View File

@ -12,6 +12,26 @@ set(Copy-NoDest-RESULT 1)
set(Copy-NoDest-STDERR "given no DESTINATION") set(Copy-NoDest-STDERR "given no DESTINATION")
set(Copy-NoFile-RESULT 1) set(Copy-NoFile-RESULT 1)
set(Copy-NoFile-STDERR "COPY cannot find.*/does_not_exist\\.txt") set(Copy-NoFile-STDERR "COPY cannot find.*/does_not_exist\\.txt")
set(MD5-NoFile-RESULT 1)
set(MD5-NoFile-STDERR "file MD5 failed to read file")
set(MD5-BadArg1-RESULT 1)
set(MD5-BadArg1-STDERR "file must be called with at least two arguments")
set(MD5-BadArg2-RESULT 1)
set(MD5-BadArg2-STDERR "file MD5 requires a file name and output variable")
set(MD5-BadArg4-RESULT 1)
set(MD5-BadArg4-STDERR "file MD5 requires a file name and output variable")
set(MD5-Works-RESULT 0)
set(MD5-Works-STDERR "10d20ddb981a6202b84aa1ce1cb7fce3")
set(SHA1-Works-RESULT 0)
set(SHA1-Works-STDERR "83f093e04289b21a9415f408ad50be8b57ad2f34")
set(SHA224-Works-RESULT 0)
set(SHA224-Works-STDERR "e995a7789922c4ef9279d94e763c8375934180a51baa7147bc48edf7")
set(SHA256-Works-RESULT 0)
set(SHA256-Works-STDERR "d1c5915d8b71150726a1eef75a29ec6bea8fd1bef6b7299ef8048760b0402025")
set(SHA384-Works-RESULT 0)
set(SHA384-Works-STDERR "1de9560b4e030e02051ea408200ffc55d70c97ac64ebf822461a5c786f495c36df43259b14483bc8d364f0106f4971ee")
set(SHA512-Works-RESULT 0)
set(SHA512-Works-STDERR "3982a1b4e651768bec70ab1fb97045cb7a659f4ba7203d501c52ab2e803071f9d5fd272022df15f27727fc67f8cd022e710e29010b2a9c0b467c111e2f6abf51")
include("@CMAKE_CURRENT_SOURCE_DIR@/CheckCMakeTest.cmake") include("@CMAKE_CURRENT_SOURCE_DIR@/CheckCMakeTest.cmake")
check_cmake_test(File check_cmake_test(File
@ -22,6 +42,16 @@ check_cmake_test(File
Copy-LateArg Copy-LateArg
Copy-NoDest Copy-NoDest
Copy-NoFile Copy-NoFile
MD5-NoFile
MD5-BadArg1
MD5-BadArg2
MD5-BadArg4
MD5-Works
SHA1-Works
SHA224-Works
SHA256-Works
SHA384-Works
SHA512-Works
) )
# Also execute each test listed in FileTestScript.cmake: # Also execute each test listed in FileTestScript.cmake:

View File

@ -0,0 +1 @@
string(MD5)

View File

@ -0,0 +1 @@
string(MD5 md5)

View File

@ -0,0 +1 @@
string(MD5 md5 input extra_arg)

View File

@ -0,0 +1,2 @@
string(MD5 md5 "sample input string\n")
message("${md5}")

View File

@ -0,0 +1,2 @@
string(SHA1 sha1 "sample input string\n")
message("${sha1}")

View File

@ -0,0 +1,2 @@
string(SHA224 sha224 "sample input string\n")
message("${sha224}")

View File

@ -0,0 +1,2 @@
string(SHA256 sha256 "sample input string\n")
message("${sha256}")

View File

@ -0,0 +1,2 @@
string(SHA384 sha384 "sample input string\n")
message("${sha384}")

View File

@ -0,0 +1,2 @@
string(SHA512 sha512 "sample input string\n")
message("${sha512}")

View File

@ -1,3 +1,35 @@
set(MD5-BadArg1-RESULT 1)
set(MD5-BadArg1-STDERR "string MD5 requires an output variable")
set(MD5-BadArg2-RESULT 1)
set(MD5-BadArg2-STDERR "string MD5 requires an output variable and an input string")
set(MD5-BadArg4-RESULT 1)
set(MD5-BadArg4-STDERR "string MD5 requires an output variable and an input string")
set(MD5-Works-RESULT 0)
set(MD5-Works-STDERR "10d20ddb981a6202b84aa1ce1cb7fce3")
set(SHA1-Works-RESULT 0)
set(SHA1-Works-STDERR "83f093e04289b21a9415f408ad50be8b57ad2f34")
set(SHA224-Works-RESULT 0)
set(SHA224-Works-STDERR "e995a7789922c4ef9279d94e763c8375934180a51baa7147bc48edf7")
set(SHA256-Works-RESULT 0)
set(SHA256-Works-STDERR "d1c5915d8b71150726a1eef75a29ec6bea8fd1bef6b7299ef8048760b0402025")
set(SHA384-Works-RESULT 0)
set(SHA384-Works-STDERR "1de9560b4e030e02051ea408200ffc55d70c97ac64ebf822461a5c786f495c36df43259b14483bc8d364f0106f4971ee")
set(SHA512-Works-RESULT 0)
set(SHA512-Works-STDERR "3982a1b4e651768bec70ab1fb97045cb7a659f4ba7203d501c52ab2e803071f9d5fd272022df15f27727fc67f8cd022e710e29010b2a9c0b467c111e2f6abf51")
include("@CMAKE_CURRENT_SOURCE_DIR@/CheckCMakeTest.cmake")
check_cmake_test(String
MD5-BadArg1
MD5-BadArg2
MD5-BadArg4
MD5-Works
SHA1-Works
SHA224-Works
SHA256-Works
SHA384-Works
SHA512-Works
)
# Execute each test listed in StringTestScript.cmake: # Execute each test listed in StringTestScript.cmake:
# #
set(scriptname "@CMAKE_CURRENT_SOURCE_DIR@/StringTestScript.cmake") set(scriptname "@CMAKE_CURRENT_SOURCE_DIR@/StringTestScript.cmake")