Ignore PureCryptTables.h - it isn't important. There isn't anything in there that could be causing the problem as it's just a load of constants.
Code: Select all
///////////////////////////////////////////////////////////////////////////////
// PureCrypt v1.0a //
///////////////////////////////////////////////////////////////////////////////
// Contains library functions and library test routine. //
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <windows.h>
#include "PureCrypt.h"
#include "PureCryptTables.h"
// PureCrypt API //////////////////////////////////////////////////////////////
// Needs to be given a 20 byte buffer for the result
PBINT PB_SHA1Hash(uchar8 *buffer, ulong32 length, uchar8 *result)
{
SHA1 md;
// Have we got input?
if (buffer && *buffer && result && *result) {
if (sha1_init(&md) != TRUE)
return FALSE;
if (sha1_process(&md, buffer, length) != TRUE)
return FALSE;
if (sha1_done(&md, result) != TRUE)
return FALSE;
return TRUE;
} else {
return FALSE;
}
}
// Debugger function interface ////////////////////////////////////////////////
// --- All command debug equivalent (name + _DEBUG)
// No more '_stdcall' as the arguments aren't unstacked..
PBDEBUG PB_SHA1Hash_DEBUG(uchar8 *buffer, ulong32 length, uchar8 *result)
{
if (length == 0)
SendDebuggerError("0 length input !");
}
// SHA1 Functions /////////////////////////////////////////////////////////////
int sha1_compress(SHA1 *md, uchar8 *buf)
{
ulong32 a,b,c,d,e,W[80],i;
/* copy the state into 512-bits into W[0..15] */
for (i = 0; i < 16; i++) {
LOAD32H(W[i], buf + (4*i));
}
/* copy state */
a = md->state[0];
b = md->state[1];
c = md->state[2];
d = md->state[3];
e = md->state[4];
/* expand it */
for (i = 16; i < 80; i++) {
W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
}
/* compress */
#ifdef F0
#undef F0
#endif
#define F0(x,y,z) (z ^ (x & (y ^ z)))
#ifdef FF0
#undef FF0
#endif
#define FF0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
/* round one */
for (i = 0; i < 20; ) {
FF0(a,b,c,d,e,i++);
FF0(e,a,b,c,d,i++);
FF0(d,e,a,b,c,i++);
FF0(c,d,e,a,b,i++);
FF0(b,c,d,e,a,i++);
}
#ifdef F1
#undef F1
#endif
#define F1(x,y,z) (x ^ y ^ z)
#ifdef FF1
#undef FF1
#endif
#define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
/* round two */
for (; i < 40; ) {
FF1(a,b,c,d,e,i++);
FF1(e,a,b,c,d,i++);
FF1(d,e,a,b,c,i++);
FF1(c,d,e,a,b,i++);
FF1(b,c,d,e,a,i++);
}
#ifdef F2
#undef F2
#endif
#define F2(x,y,z) ((x & y) | (z & (x | y)))
#ifdef FF2
#undef FF2
#endif
#define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
/* round three */
for (; i < 60; ) {
FF2(a,b,c,d,e,i++);
FF2(e,a,b,c,d,i++);
FF2(d,e,a,b,c,i++);
FF2(c,d,e,a,b,i++);
FF2(b,c,d,e,a,i++);
}
#ifdef F3
#undef F3
#endif
#define F3(x,y,z) (x ^ y ^ z)
#ifdef FF3
#undef FF3
#endif
#define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
/* round four */
for (; i < 80; ) {
FF3(a,b,c,d,e,i++);
FF3(e,a,b,c,d,i++);
FF3(d,e,a,b,c,i++);
FF3(c,d,e,a,b,i++);
FF3(b,c,d,e,a,i++);
}
/* store */
md->state[0] = md->state[0] + a;
md->state[1] = md->state[1] + b;
md->state[2] = md->state[2] + c;
md->state[3] = md->state[3] + d;
md->state[4] = md->state[4] + e;
return TRUE;
}
int sha1_init(SHA1 *md)
{
int i;
if (md == NULL) return FALSE;
md->state[0] = 0x67452301UL;
md->state[1] = 0xefcdab89UL;
md->state[2] = 0x98badcfeUL;
md->state[3] = 0x10325476UL;
md->state[4] = 0xc3d2e1f0UL;
md->curlen = 0;
md->length = 0;
for (i = 0; i < 64; i++)
md->buf[i] = ' ';
return TRUE;
}
int sha1_process(SHA1 *md, uchar8 *in, ulong32 inlen)
{
ulong32 n;
int blocksize = 64;
// Check to see we have been passed valid arguments
if (md == NULL || in == NULL) {
return FALSE;
}
if (md->curlen > sizeof(md->buf)) {
return FALSE;
}
// Process block
while (inlen > 0) {
if (md->curlen == 0 && inlen >= blocksize) {
if ((sha1_compress(md, (unsigned char *)in)) != TRUE) {
return FALSE;
}
md->length += blocksize * 8;
in += blocksize;
inlen -= blocksize;
} else {
n = MIN(inlen, (blocksize - md->curlen));
memcpy(md->buf + md->curlen, in, (size_t)n);
md->curlen += n;
in += n;
inlen -= n;
if (md->curlen == blocksize) {
if ((sha1_compress(md, md->buf)) != TRUE) {
return FALSE;
}
md->length += 8*blocksize;
md->curlen = 0;
}
}
}
return TRUE;
}
int sha1_done(SHA1 *md, uchar8 *out)
{
int i;
if (md == NULL || out == NULL) {
return FALSE;
}
if (md->curlen >= sizeof(md->buf)) {
return FALSE;
}
/* increase the length of the message */
md->length += md->curlen * 8;
/* append the '1' bit */
md->buf[md->curlen++] = (unsigned char)0x80;
/* if the length is currently above 56 bytes we append zeros
* then compress. Then we can fall back to padding zeros and length
* encoding like normal.
*/
if (md->curlen > 56) {
while (md->curlen < 64) {
md->buf[md->curlen++] = (unsigned char)0;
}
sha1_compress(md, md->buf);
md->curlen = 0;
}
/* pad upto 56 bytes of zeroes */
while (md->curlen < 56) {
md->buf[md->curlen++] = (unsigned char)0;
}
/* store length */
STORE64H(md->length, md->buf+56);
sha1_compress(md, md->buf);
/* copy output */
for (i = 0; i < 5; i++) {
STORE32H(md->state[i], out+(4*i));
}
return TRUE;
}
// Examples of how to interface with PureBASIC ////////////////////////////////
/*
// String return example ------------------------------------------------------------------
PBSTRING PB_StringMultiply(char *String, int NbTimes)
{
char *ResultString = PB_StringBase;
int k;
if (String && *String) // It's always good for safety and perfomance to exclude null or empty strings
{
int StringLength = strlen(String);
for (k=0; k<NbTimes; k++)
{
strcpy(PB_StringBase, String);
PB_StringBase += StringLength; // Increase the output string buffer
}
}
*PB_StringBase = 0; // Finally write the zero ending string. PB_StringBase contains the result string
return ResultString; // Returns the start of the buffer, as it has been passed
}
// Float return example --------------------------------------------------------------------
PBFLOAT PB_FloatReturn()
{
return 15.21;
}
*/
// Test library ///////////////////////////////////////////////////////////////
#ifndef PURELIBRARY
// Not done yet
int main()
{
PB_MessageBox(0, "Titre", "Text", 0);
return 0;
}
#endif