Initial commit. Version 1.0.0. Contains
- MD5 - SHA1 - SHA256 - SHA512 - RC4
This commit is contained in:
315
lib/LibMd5.c
Normal file
315
lib/LibMd5.c
Normal file
@@ -0,0 +1,315 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibMd5
|
||||
//
|
||||
// Implementation of MD5 hash function. Originally written by Alexander Peslyak. Modified by WaterJuice retaining
|
||||
// Public Domain license.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "LibMd5.h"
|
||||
#include <memory.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// INTERNAL FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// F, G, H, I
|
||||
//
|
||||
// The basic MD5 functions. F and G are optimized compared to their RFC 1321 definitions for architectures that lack
|
||||
// an AND-NOT instruction, just like in Colin Plumb's implementation.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#define F( x, y, z ) ( (z) ^ ((x) & ((y) ^ (z))) )
|
||||
#define G( x, y, z ) ( (y) ^ ((z) & ((x) ^ (y))) )
|
||||
#define H( x, y, z ) ( (x) ^ (y) ^ (z) )
|
||||
#define I( x, y, z ) ( (y) ^ ((x) | ~(z)) )
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// STEP
|
||||
//
|
||||
// The MD5 transformation for all four rounds.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#define STEP( f, a, b, c, d, x, t, s ) \
|
||||
(a) += f((b), (c), (d)) + (x) + (t); \
|
||||
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
|
||||
(a) += (b);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// SET, GET
|
||||
//
|
||||
// SET reads 4 input bytes in little-endian byte order and stores them in a properly aligned word in host byte order.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#define SET(n) (*(uint32_t *)&ptr[(n) * 4])
|
||||
#define GET(n) SET(n)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TransformFunction
|
||||
//
|
||||
// This processes one or more 64-byte data blocks, but does NOT update the bit counters. There are no alignment
|
||||
// requirements.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
static
|
||||
void*
|
||||
TransformFunction
|
||||
(
|
||||
Md5Context* ctx,
|
||||
void* data,
|
||||
uintmax_t size
|
||||
)
|
||||
{
|
||||
uint8_t* ptr;
|
||||
uint32_t a;
|
||||
uint32_t b;
|
||||
uint32_t c;
|
||||
uint32_t d;
|
||||
uint32_t saved_a;
|
||||
uint32_t saved_b;
|
||||
uint32_t saved_c;
|
||||
uint32_t saved_d;
|
||||
|
||||
ptr = (uint8_t*)data;
|
||||
|
||||
a = ctx->a;
|
||||
b = ctx->b;
|
||||
c = ctx->c;
|
||||
d = ctx->d;
|
||||
|
||||
do
|
||||
{
|
||||
saved_a = a;
|
||||
saved_b = b;
|
||||
saved_c = c;
|
||||
saved_d = d;
|
||||
|
||||
// Round 1
|
||||
STEP( F, a, b, c, d, SET(0), 0xd76aa478, 7 )
|
||||
STEP( F, d, a, b, c, SET(1), 0xe8c7b756, 12 )
|
||||
STEP( F, c, d, a, b, SET(2), 0x242070db, 17 )
|
||||
STEP( F, b, c, d, a, SET(3), 0xc1bdceee, 22 )
|
||||
STEP( F, a, b, c, d, SET(4), 0xf57c0faf, 7 )
|
||||
STEP( F, d, a, b, c, SET(5), 0x4787c62a, 12 )
|
||||
STEP( F, c, d, a, b, SET(6), 0xa8304613, 17 )
|
||||
STEP( F, b, c, d, a, SET(7), 0xfd469501, 22 )
|
||||
STEP( F, a, b, c, d, SET(8 ), 0x698098d8, 7 )
|
||||
STEP( F, d, a, b, c, SET(9 ), 0x8b44f7af, 12 )
|
||||
STEP( F, c, d, a, b, SET(10 ), 0xffff5bb1, 17 )
|
||||
STEP( F, b, c, d, a, SET(11 ), 0x895cd7be, 22 )
|
||||
STEP( F, a, b, c, d, SET(12 ), 0x6b901122, 7 )
|
||||
STEP( F, d, a, b, c, SET(13 ), 0xfd987193, 12 )
|
||||
STEP( F, c, d, a, b, SET(14 ), 0xa679438e, 17 )
|
||||
STEP( F, b, c, d, a, SET(15 ), 0x49b40821, 22 )
|
||||
|
||||
// Round 2
|
||||
STEP( G, a, b, c, d, GET(1), 0xf61e2562, 5 )
|
||||
STEP( G, d, a, b, c, GET(6), 0xc040b340, 9 )
|
||||
STEP( G, c, d, a, b, GET(11), 0x265e5a51, 14 )
|
||||
STEP( G, b, c, d, a, GET(0), 0xe9b6c7aa, 20 )
|
||||
STEP( G, a, b, c, d, GET(5), 0xd62f105d, 5 )
|
||||
STEP( G, d, a, b, c, GET(10), 0x02441453, 9 )
|
||||
STEP( G, c, d, a, b, GET(15), 0xd8a1e681, 14 )
|
||||
STEP( G, b, c, d, a, GET(4), 0xe7d3fbc8, 20 )
|
||||
STEP( G, a, b, c, d, GET(9), 0x21e1cde6, 5 )
|
||||
STEP( G, d, a, b, c, GET(14), 0xc33707d6, 9 )
|
||||
STEP( G, c, d, a, b, GET(3), 0xf4d50d87, 14 )
|
||||
STEP( G, b, c, d, a, GET(8), 0x455a14ed, 20 )
|
||||
STEP( G, a, b, c, d, GET(13), 0xa9e3e905, 5 )
|
||||
STEP( G, d, a, b, c, GET(2), 0xfcefa3f8, 9 )
|
||||
STEP( G, c, d, a, b, GET(7), 0x676f02d9, 14 )
|
||||
STEP( G, b, c, d, a, GET(12), 0x8d2a4c8a, 20 )
|
||||
|
||||
// Round 3
|
||||
STEP( H, a, b, c, d, GET(5), 0xfffa3942, 4 )
|
||||
STEP( H, d, a, b, c, GET(8), 0x8771f681, 11 )
|
||||
STEP( H, c, d, a, b, GET(11), 0x6d9d6122, 16 )
|
||||
STEP( H, b, c, d, a, GET(14), 0xfde5380c, 23 )
|
||||
STEP( H, a, b, c, d, GET(1), 0xa4beea44, 4 )
|
||||
STEP( H, d, a, b, c, GET(4), 0x4bdecfa9, 11 )
|
||||
STEP( H, c, d, a, b, GET(7), 0xf6bb4b60, 16 )
|
||||
STEP( H, b, c, d, a, GET(10), 0xbebfbc70, 23 )
|
||||
STEP( H, a, b, c, d, GET(13), 0x289b7ec6, 4 )
|
||||
STEP( H, d, a, b, c, GET(0), 0xeaa127fa, 11 )
|
||||
STEP( H, c, d, a, b, GET(3), 0xd4ef3085, 16 )
|
||||
STEP( H, b, c, d, a, GET(6), 0x04881d05, 23 )
|
||||
STEP( H, a, b, c, d, GET(9), 0xd9d4d039, 4 )
|
||||
STEP( H, d, a, b, c, GET(12), 0xe6db99e5, 11 )
|
||||
STEP( H, c, d, a, b, GET(15), 0x1fa27cf8, 16 )
|
||||
STEP( H, b, c, d, a, GET(2), 0xc4ac5665, 23 )
|
||||
|
||||
// Round 4
|
||||
STEP( I, a, b, c, d, GET(0), 0xf4292244, 6 )
|
||||
STEP( I, d, a, b, c, GET(7), 0x432aff97, 10 )
|
||||
STEP( I, c, d, a, b, GET(14), 0xab9423a7, 15 )
|
||||
STEP( I, b, c, d, a, GET(5), 0xfc93a039, 21 )
|
||||
STEP( I, a, b, c, d, GET(12), 0x655b59c3, 6 )
|
||||
STEP( I, d, a, b, c, GET(3), 0x8f0ccc92, 10 )
|
||||
STEP( I, c, d, a, b, GET(10), 0xffeff47d, 15 )
|
||||
STEP( I, b, c, d, a, GET(1), 0x85845dd1, 21 )
|
||||
STEP( I, a, b, c, d, GET(8), 0x6fa87e4f, 6 )
|
||||
STEP( I, d, a, b, c, GET(15), 0xfe2ce6e0, 10 )
|
||||
STEP( I, c, d, a, b, GET(6), 0xa3014314, 15 )
|
||||
STEP( I, b, c, d, a, GET(13), 0x4e0811a1, 21 )
|
||||
STEP( I, a, b, c, d, GET(4), 0xf7537e82, 6 )
|
||||
STEP( I, d, a, b, c, GET(11), 0xbd3af235, 10 )
|
||||
STEP( I, c, d, a, b, GET(2), 0x2ad7d2bb, 15 )
|
||||
STEP( I, b, c, d, a, GET(9), 0xeb86d391, 21 )
|
||||
|
||||
a += saved_a;
|
||||
b += saved_b;
|
||||
c += saved_c;
|
||||
d += saved_d;
|
||||
|
||||
ptr += 64;
|
||||
} while( size -= 64 );
|
||||
|
||||
ctx->a = a;
|
||||
ctx->b = b;
|
||||
ctx->c = c;
|
||||
ctx->d = d;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// EXPORTED FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Md5Initialise
|
||||
//
|
||||
// Initialises an MD5 Context. Use this to initialise/reset a context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Md5Initialise
|
||||
(
|
||||
Md5Context* Context
|
||||
)
|
||||
{
|
||||
Context->a = 0x67452301;
|
||||
Context->b = 0xefcdab89;
|
||||
Context->c = 0x98badcfe;
|
||||
Context->d = 0x10325476;
|
||||
|
||||
Context->lo = 0;
|
||||
Context->hi = 0;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Md5Update
|
||||
//
|
||||
// Adds data to the MD5 context. This will process the data and update the internal state of the context. Keep on
|
||||
// calling this function until all the data has been added. Then call Md5Finalise to calculate the hash.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Md5Update
|
||||
(
|
||||
Md5Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t BufferSize
|
||||
)
|
||||
{
|
||||
uint32_t saved_lo;
|
||||
uint32_t used;
|
||||
uint32_t free;
|
||||
|
||||
saved_lo = Context->lo;
|
||||
if( (Context->lo = (saved_lo + BufferSize) & 0x1fffffff) < saved_lo )
|
||||
{
|
||||
Context->hi++;
|
||||
}
|
||||
Context->hi += (uint32_t)( BufferSize >> 29 );
|
||||
|
||||
used = saved_lo & 0x3f;
|
||||
|
||||
if( used )
|
||||
{
|
||||
free = 64 - used;
|
||||
|
||||
if( BufferSize < free )
|
||||
{
|
||||
memcpy( &Context->buffer[used], Buffer, BufferSize );
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy( &Context->buffer[used], Buffer, free );
|
||||
Buffer = (uint8_t*)Buffer + free;
|
||||
BufferSize -= free;
|
||||
TransformFunction(Context, Context->buffer, 64);
|
||||
}
|
||||
|
||||
if( BufferSize >= 64 )
|
||||
{
|
||||
Buffer = TransformFunction( Context, Buffer, BufferSize & ~(unsigned long)0x3f );
|
||||
BufferSize &= 0x3f;
|
||||
}
|
||||
|
||||
memcpy( Context->buffer, Buffer, BufferSize );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Md5Finalise
|
||||
//
|
||||
// Performs the final calculation of the hash and returns the digest (16 byte buffer containing 128bit hash). After
|
||||
// calling this, Md5Initialised must be used to reuse the context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Md5Finalise
|
||||
(
|
||||
Md5Context* Context,
|
||||
MD5_HASH* Digest
|
||||
)
|
||||
{
|
||||
uint32_t used;
|
||||
uint32_t free;
|
||||
|
||||
used = Context->lo & 0x3f;
|
||||
|
||||
Context->buffer[used++] = 0x80;
|
||||
|
||||
free = 64 - used;
|
||||
|
||||
if(free < 8)
|
||||
{
|
||||
memset( &Context->buffer[used], 0, free );
|
||||
TransformFunction( Context, Context->buffer, 64 );
|
||||
used = 0;
|
||||
free = 64;
|
||||
}
|
||||
|
||||
memset( &Context->buffer[used], 0, free - 8 );
|
||||
|
||||
Context->lo <<= 3;
|
||||
Context->buffer[56] = (uint8_t)( Context->lo );
|
||||
Context->buffer[57] = (uint8_t)( Context->lo >> 8 );
|
||||
Context->buffer[58] = (uint8_t)( Context->lo >> 16 );
|
||||
Context->buffer[59] = (uint8_t)( Context->lo >> 24 );
|
||||
Context->buffer[60] = (uint8_t)( Context->hi );
|
||||
Context->buffer[61] = (uint8_t)( Context->hi >> 8 );
|
||||
Context->buffer[62] = (uint8_t)( Context->hi >> 16 );
|
||||
Context->buffer[63] = (uint8_t)( Context->hi >> 24 );
|
||||
|
||||
TransformFunction( Context, Context->buffer, 64 );
|
||||
|
||||
Digest->bytes[0] = (uint8_t)( Context->a );
|
||||
Digest->bytes[1] = (uint8_t)( Context->a >> 8 );
|
||||
Digest->bytes[2] = (uint8_t)( Context->a >> 16 );
|
||||
Digest->bytes[3] = (uint8_t)( Context->a >> 24 );
|
||||
Digest->bytes[4] = (uint8_t)( Context->b );
|
||||
Digest->bytes[5] = (uint8_t)( Context->b >> 8 );
|
||||
Digest->bytes[6] = (uint8_t)( Context->b >> 16 );
|
||||
Digest->bytes[7] = (uint8_t)( Context->b >> 24 );
|
||||
Digest->bytes[8] = (uint8_t)( Context->c );
|
||||
Digest->bytes[9] = (uint8_t)( Context->c >> 8 );
|
||||
Digest->bytes[10] = (uint8_t)( Context->c >> 16 );
|
||||
Digest->bytes[11] = (uint8_t)( Context->c >> 24 );
|
||||
Digest->bytes[12] = (uint8_t)( Context->d );
|
||||
Digest->bytes[13] = (uint8_t)( Context->d >> 8 );
|
||||
Digest->bytes[14] = (uint8_t)( Context->d >> 16 );
|
||||
Digest->bytes[15] = (uint8_t)( Context->d >> 24 );
|
||||
}
|
||||
|
||||
89
lib/LibMd5.h
Normal file
89
lib/LibMd5.h
Normal file
@@ -0,0 +1,89 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibMd5
|
||||
//
|
||||
// Implementation of MD5 hash function. Originally written by Alexander Peslyak. Modified by WaterJuice retaining
|
||||
// Public Domain license.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _LibMd5_h_
|
||||
#define _LibMd5_h_
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TYPES
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Md5Context - This must be initialised using Md5Initialised. Do not modify the contents of this structure directly.
|
||||
typedef struct
|
||||
{
|
||||
uint32_t lo;
|
||||
uint32_t hi;
|
||||
uint32_t a;
|
||||
uint32_t b;
|
||||
uint32_t c;
|
||||
uint32_t d;
|
||||
uint8_t buffer[64];
|
||||
uint32_t block[16];
|
||||
} Md5Context;
|
||||
|
||||
#define MD5_HASH_SIZE ( 128 / 8 )
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t bytes [MD5_HASH_SIZE];
|
||||
} MD5_HASH;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Md5Initialise
|
||||
//
|
||||
// Initialises an MD5 Context. Use this to initialise/reset a context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Md5Initialise
|
||||
(
|
||||
Md5Context* Context
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Md5Update
|
||||
//
|
||||
// Adds data to the MD5 context. This will process the data and update the internal state of the context. Keep on
|
||||
// calling this function until all the data has been added. Then call Md5Finalise to calculate the hash.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Md5Update
|
||||
(
|
||||
Md5Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t BufferSize
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Md5Finalise
|
||||
//
|
||||
// Performs the final calculation of the hash and returns the digest (16 byte buffer containing 128bit hash). After
|
||||
// calling this, Md5Initialised must be used to reuse the context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Md5Finalise
|
||||
(
|
||||
Md5Context* Context,
|
||||
MD5_HASH* Digest
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#endif //_LibMd5_h_
|
||||
|
||||
129
lib/LibRc4.c
Normal file
129
lib/LibRc4.c
Normal file
@@ -0,0 +1,129 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibRC4
|
||||
//
|
||||
// An implementation of RC4 stream cipher
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "LibRc4.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// INTERNAL FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define SwapBytes( Value1, Value2 ) \
|
||||
{ \
|
||||
uint8_t temp = Value1; \
|
||||
Value1 = Value2; \
|
||||
Value2 = temp; \
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Rc4Initialise
|
||||
//
|
||||
// Initialises an RC4 cipher and discards the specified number of first bytes.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Rc4Initialise
|
||||
(
|
||||
Rc4Context* Context,
|
||||
void* Key,
|
||||
uint32_t KeySize,
|
||||
uint32_t DropN
|
||||
)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t j;
|
||||
uint32_t n;
|
||||
|
||||
// Setup key schedule
|
||||
for( i=0; i<256; i++ )
|
||||
{
|
||||
Context->S[i] = (uint8_t)i;
|
||||
}
|
||||
|
||||
j = 0;
|
||||
for( i=0; i<256; i++ )
|
||||
{
|
||||
j = ( j + Context->S[i] + ((uint8_t*)Key)[i % KeySize] ) % 256;
|
||||
SwapBytes( Context->S[i], Context->S[j] );
|
||||
}
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
|
||||
// Drop first bytes (if requested)
|
||||
for( n=0; n<DropN; n++ )
|
||||
{
|
||||
i = ( i + 1 ) % 256;
|
||||
j = ( j + Context->S[i] ) % 256;
|
||||
SwapBytes( Context->S[i], Context->S[j] );
|
||||
}
|
||||
|
||||
Context->i = i;
|
||||
Context->j = j;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Rc4Output
|
||||
//
|
||||
// Outputs the requested number of bytes from the RC4 stream
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Rc4Output
|
||||
(
|
||||
Rc4Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t Size
|
||||
)
|
||||
{
|
||||
uint32_t n;
|
||||
|
||||
for( n=0; n<Size; n++ )
|
||||
{
|
||||
Context->i = ( Context->i + 1 ) % 256;
|
||||
Context->j = ( Context->j + Context->S[Context->i] ) % 256;
|
||||
SwapBytes( Context->S[Context->i], Context->S[Context->j] );
|
||||
|
||||
((uint8_t*)Buffer)[n] = Context->S[ (Context->S[Context->i] + Context->S[Context->j]) % 256 ];
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Rc4Xor
|
||||
//
|
||||
// XORs the RC4 stream with an input buffer and puts the results in an output buffer. This is used for encrypting
|
||||
// and decrypting data. InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Rc4Xor
|
||||
(
|
||||
Rc4Context* Context,
|
||||
void* InBuffer,
|
||||
void* OutBuffer,
|
||||
uint32_t Size
|
||||
)
|
||||
{
|
||||
uint32_t n;
|
||||
|
||||
for( n=0; n<Size; n++ )
|
||||
{
|
||||
Context->i = ( Context->i + 1 ) % 256;
|
||||
Context->j = ( Context->j + Context->S[Context->i] ) % 256;
|
||||
SwapBytes( Context->S[Context->i], Context->S[Context->j] );
|
||||
|
||||
((uint8_t*)OutBuffer)[n] = ((uint8_t*)InBuffer)[n]
|
||||
^ ( Context->S[ (Context->S[Context->i] + Context->S[Context->j]) % 256 ] );
|
||||
}
|
||||
}
|
||||
|
||||
78
lib/LibRc4.h
Normal file
78
lib/LibRc4.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibRC4
|
||||
//
|
||||
// An implementation of RC4 stream cipher
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _LibRc4_h_
|
||||
#define _LibRc4_h_
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TYPES
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Rc4Context - This must be initialised using Rc4Initialised. Do not modify the contents of this structure directly.
|
||||
typedef struct
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t j;
|
||||
uint8_t S[256];
|
||||
} Rc4Context;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Rc4Initialise
|
||||
//
|
||||
// Initialises an RC4 cipher and discards the specified number of first bytes.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Rc4Initialise
|
||||
(
|
||||
Rc4Context* Context,
|
||||
void* Key,
|
||||
uint32_t KeySize,
|
||||
uint32_t DropN
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Rc4Output
|
||||
//
|
||||
// Outputs the requested number of bytes from the RC4 stream
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Rc4Output
|
||||
(
|
||||
Rc4Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t Size
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Rc4Xor
|
||||
//
|
||||
// XORs the RC4 stream with an input buffer and puts the results in an output buffer. This is used for encrypting
|
||||
// and decrypting data. InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Rc4Xor
|
||||
(
|
||||
Rc4Context* Context,
|
||||
void* InBuffer,
|
||||
void* OutBuffer,
|
||||
uint32_t Size
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#endif //_LibRc4_h_
|
||||
|
||||
212
lib/LibSha1.c
Normal file
212
lib/LibSha1.c
Normal file
@@ -0,0 +1,212 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibSha1
|
||||
//
|
||||
// Implementation of SHA1 hash function.
|
||||
// Original author: Steve Reid <sreid@sea-to-sky.net>
|
||||
// Contributions by: James H. Brown <jbrown@burgoyne.com>, Saul Kravitz <Saul.Kravitz@celera.com>,
|
||||
// and Ralph Giles <giles@ghostscript.com>
|
||||
// Modified by WaterJuice retaining Public Domain license.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "LibSha1.h"
|
||||
#include <memory.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TYPES
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
typedef union
|
||||
{
|
||||
uint8_t c [64];
|
||||
uint32_t l [16];
|
||||
} CHAR64LONG16;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// INTERNAL FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
||||
|
||||
// blk0() and blk() perform the initial expand.
|
||||
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|
||||
|(rol(block->l[i],8)&0x00FF00FF))
|
||||
|
||||
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
|
||||
^block->l[(i+2)&15]^block->l[i&15],1))
|
||||
|
||||
// (R0+R1), R2, R3, R4 are the different operations used in SHA1
|
||||
#define R0(v,w,x,y,z,i) z += ((w&(x^y))^y) + blk0(i)+ 0x5A827999 + rol(v,5); w=rol(w,30);
|
||||
#define R1(v,w,x,y,z,i) z += ((w&(x^y))^y) + blk(i) + 0x5A827999 + rol(v,5); w=rol(w,30);
|
||||
#define R2(v,w,x,y,z,i) z += (w^x^y) + blk(i) + 0x6ED9EBA1 + rol(v,5); w=rol(w,30);
|
||||
#define R3(v,w,x,y,z,i) z += (((w|x)&y)|(w&x)) + blk(i) + 0x8F1BBCDC + rol(v,5); w=rol(w,30);
|
||||
#define R4(v,w,x,y,z,i) z += (w^x^y) + blk(i) + 0xCA62C1D6 + rol(v,5); w=rol(w,30);
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TransformFunction
|
||||
//
|
||||
// Hash a single 512-bit block. This is the core of the algorithm
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
static
|
||||
void
|
||||
TransformFunction
|
||||
(
|
||||
uint32_t state[5],
|
||||
const uint8_t buffer[64]
|
||||
)
|
||||
{
|
||||
uint32_t a;
|
||||
uint32_t b;
|
||||
uint32_t c;
|
||||
uint32_t d;
|
||||
uint32_t e;
|
||||
uint8_t workspace[64];
|
||||
CHAR64LONG16* block = (CHAR64LONG16*) workspace;
|
||||
|
||||
memcpy( block, buffer, 64 );
|
||||
|
||||
// Copy context->state[] to working vars
|
||||
a = state[0];
|
||||
b = state[1];
|
||||
c = state[2];
|
||||
d = state[3];
|
||||
e = state[4];
|
||||
|
||||
// 4 rounds of 20 operations each. Loop unrolled.
|
||||
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
|
||||
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
|
||||
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
|
||||
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
|
||||
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
|
||||
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
|
||||
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
|
||||
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
|
||||
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
|
||||
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
|
||||
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
|
||||
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
|
||||
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
|
||||
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
|
||||
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
|
||||
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
|
||||
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
|
||||
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
|
||||
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
|
||||
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
|
||||
|
||||
// Add the working vars back into context.state[]
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
state[4] += e;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha1Initialise
|
||||
//
|
||||
// Initialises an SHA1 Context. Use this to initialise/reset a context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha1Initialise
|
||||
(
|
||||
Sha1Context* Context
|
||||
)
|
||||
{
|
||||
// SHA1 initialization constants
|
||||
Context->State[0] = 0x67452301;
|
||||
Context->State[1] = 0xEFCDAB89;
|
||||
Context->State[2] = 0x98BADCFE;
|
||||
Context->State[3] = 0x10325476;
|
||||
Context->State[4] = 0xC3D2E1F0;
|
||||
Context->Count[0] = 0;
|
||||
Context->Count[1] = 0;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha1Update
|
||||
//
|
||||
// Adds data to the SHA1 context. This will process the data and update the internal state of the context. Keep on
|
||||
// calling this function until all the data has been added. Then call Sha1Finalise to calculate the hash.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha1Update
|
||||
(
|
||||
Sha1Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t BufferSize
|
||||
)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t j;
|
||||
|
||||
j = (Context->Count[0] >> 3) & 63;
|
||||
if( (Context->Count[0] += BufferSize << 3) < (BufferSize << 3) )
|
||||
{
|
||||
Context->Count[1]++;
|
||||
}
|
||||
|
||||
Context->Count[1] += (BufferSize >> 29);
|
||||
if( (j + BufferSize) > 63 )
|
||||
{
|
||||
i = 64 - j;
|
||||
memcpy( &Context->Buffer[j], Buffer, i );
|
||||
TransformFunction(Context->State, Context->Buffer);
|
||||
for( ; i + 63 < BufferSize; i += 64 )
|
||||
{
|
||||
TransformFunction(Context->State, (uint8_t*)Buffer + i);
|
||||
}
|
||||
j = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0;
|
||||
}
|
||||
|
||||
memcpy( &Context->Buffer[j], &((uint8_t*)Buffer)[i], BufferSize - i );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha1Finalise
|
||||
//
|
||||
// Performs the final calculation of the hash and returns the digest (20 byte buffer containing 160bit hash). After
|
||||
// calling this, Sha1Initialised must be used to reuse the context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha1Finalise
|
||||
(
|
||||
Sha1Context* Context,
|
||||
SHA1_HASH* Digest
|
||||
)
|
||||
{
|
||||
uint32_t i;
|
||||
uint8_t finalcount[8];
|
||||
|
||||
for( i=0; i<8; i++ )
|
||||
{
|
||||
finalcount[i] = (unsigned char)((Context->Count[(i >= 4 ? 0 : 1)]
|
||||
>> ((3-(i & 3)) * 8) ) & 255); // Endian independent
|
||||
}
|
||||
Sha1Update( Context, (uint8_t*)"\x80", 1 );
|
||||
while( (Context->Count[0] & 504) != 448 )
|
||||
{
|
||||
Sha1Update( Context, (uint8_t*)"\0", 1 );
|
||||
}
|
||||
|
||||
Sha1Update( Context, finalcount, 8 ); // Should cause a Sha1TransformFunction()
|
||||
for( i=0; i<SHA1_HASH_SIZE; i++ )
|
||||
{
|
||||
Digest->bytes[i] = (uint8_t)((Context->State[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
|
||||
}
|
||||
}
|
||||
|
||||
86
lib/LibSha1.h
Normal file
86
lib/LibSha1.h
Normal file
@@ -0,0 +1,86 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibSha1
|
||||
//
|
||||
// Implementation of SHA1 hash function.
|
||||
// Original author: Steve Reid <sreid@sea-to-sky.net>
|
||||
// Contributions by: James H. Brown <jbrown@burgoyne.com>, Saul Kravitz <Saul.Kravitz@celera.com>,
|
||||
// and Ralph Giles <giles@ghostscript.com>
|
||||
// Modified by WaterJuice retaining Public Domain license.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _LibSha1_h_
|
||||
#define _LibSha1_h_
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TYPES
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Sha1Context - This must be initialised using Sha1Initialised. Do not modify the contents of this structure directly.
|
||||
typedef struct
|
||||
{
|
||||
uint32_t State[5];
|
||||
uint32_t Count[2];
|
||||
uint8_t Buffer[64];
|
||||
} Sha1Context;
|
||||
|
||||
#define SHA1_HASH_SIZE ( 160 / 8 )
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t bytes [SHA1_HASH_SIZE];
|
||||
} SHA1_HASH;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha1Initialise
|
||||
//
|
||||
// Initialises an SHA1 Context. Use this to initialise/reset a context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha1Initialise
|
||||
(
|
||||
Sha1Context* Context
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha1Update
|
||||
//
|
||||
// Adds data to the SHA1 context. This will process the data and update the internal state of the context. Keep on
|
||||
// calling this function until all the data has been added. Then call Sha1Finalise to calculate the hash.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha1Update
|
||||
(
|
||||
Sha1Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t BufferSize
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha1Finalise
|
||||
//
|
||||
// Performs the final calculation of the hash and returns the digest (20 byte buffer containing 160bit hash). After
|
||||
// calling this, Sha1Initialised must be used to reuse the context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha1Finalise
|
||||
(
|
||||
Sha1Context* Context,
|
||||
SHA1_HASH* Digest
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#endif //_LibSha1_h_
|
||||
|
||||
273
lib/LibSha256.c
Normal file
273
lib/LibSha256.c
Normal file
@@ -0,0 +1,273 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibSha256
|
||||
//
|
||||
// Implementation of SHA256 hash function.
|
||||
// Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
// Modified by WaterJuice retaining Public Domain license.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "LibSha256.h"
|
||||
#include <memory.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// MACROS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
|
||||
|
||||
#define MIN(x, y) ( ((x)<(y))?(x):(y) )
|
||||
|
||||
#define STORE32H(x, y) \
|
||||
{ (y)[0] = (uint8_t)(((x)>>24)&255); (y)[1] = (uint8_t)(((x)>>16)&255); \
|
||||
(y)[2] = (uint8_t)(((x)>>8)&255); (y)[3] = (uint8_t)((x)&255); }
|
||||
|
||||
#define LOAD32H(x, y) \
|
||||
{ x = ((uint32_t)((y)[0] & 255)<<24) | \
|
||||
((uint32_t)((y)[1] & 255)<<16) | \
|
||||
((uint32_t)((y)[2] & 255)<<8) | \
|
||||
((uint32_t)((y)[3] & 255)); }
|
||||
|
||||
#define STORE64H(x, y) \
|
||||
{ (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \
|
||||
(y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \
|
||||
(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
|
||||
(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// CONSTANTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// The K array
|
||||
static const uint32_t K[64] = {
|
||||
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
|
||||
0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
|
||||
0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
|
||||
0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
|
||||
0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
|
||||
0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
|
||||
0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
|
||||
0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
|
||||
0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
|
||||
0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
|
||||
0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
|
||||
0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
|
||||
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
|
||||
};
|
||||
|
||||
#define BLOCK_SIZE 64
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// INTERNAL FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Various logical functions
|
||||
#define Ch( x, y, z ) (z ^ (x & (y ^ z)))
|
||||
#define Maj( x, y, z ) (((x | y) & z) | (x & y))
|
||||
#define S( x, n ) ror((x),(n))
|
||||
#define R( x, n ) (((x)&0xFFFFFFFFUL)>>(n))
|
||||
#define Sigma0( x ) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
|
||||
#define Sigma1( x ) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
|
||||
#define Gamma0( x ) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
|
||||
#define Gamma1( x ) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
|
||||
|
||||
#define Sha256Round( a, b, c, d, e, f, g, h, i ) \
|
||||
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
|
||||
t1 = Sigma0(a) + Maj(a, b, c); \
|
||||
d += t0; \
|
||||
h = t0 + t1;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TransformFunction
|
||||
//
|
||||
// Compress 512-bits
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
static
|
||||
void
|
||||
TransformFunction
|
||||
(
|
||||
Sha256Context* Context,
|
||||
uint8_t* Buffer
|
||||
)
|
||||
{
|
||||
uint32_t S[8];
|
||||
uint32_t W[64];
|
||||
uint32_t t0;
|
||||
uint32_t t1;
|
||||
uint32_t t;
|
||||
int i;
|
||||
|
||||
// Copy state into S
|
||||
for( i=0; i<8; i++ )
|
||||
{
|
||||
S[i] = Context->state[i];
|
||||
}
|
||||
|
||||
// Copy the state into 512-bits into W[0..15]
|
||||
for( i=0; i<16; i++ )
|
||||
{
|
||||
LOAD32H( W[i], Buffer + (4*i) );
|
||||
}
|
||||
|
||||
// Fill W[16..63]
|
||||
for( i=16; i<64; i++ )
|
||||
{
|
||||
W[i] = Gamma1( W[i-2]) + W[i-7] + Gamma0( W[i-15] ) + W[i-16];
|
||||
}
|
||||
|
||||
// Compress
|
||||
for( i=0; i<64; i++ )
|
||||
{
|
||||
Sha256Round( S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i );
|
||||
t = S[7];
|
||||
S[7] = S[6];
|
||||
S[6] = S[5];
|
||||
S[5] = S[4];
|
||||
S[4] = S[3];
|
||||
S[3] = S[2];
|
||||
S[2] = S[1];
|
||||
S[1] = S[0];
|
||||
S[0] = t;
|
||||
}
|
||||
|
||||
// Feedback
|
||||
for( i=0; i<8; i++ )
|
||||
{
|
||||
Context->state[i] = Context->state[i] + S[i];
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha256Initialise
|
||||
//
|
||||
// Initialises a SHA256 Context. Use this to initialise/reset a context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void Sha256Initialise
|
||||
(
|
||||
Sha256Context* Context
|
||||
)
|
||||
{
|
||||
Context->curlen = 0;
|
||||
Context->length = 0;
|
||||
Context->state[0] = 0x6A09E667UL;
|
||||
Context->state[1] = 0xBB67AE85UL;
|
||||
Context->state[2] = 0x3C6EF372UL;
|
||||
Context->state[3] = 0xA54FF53AUL;
|
||||
Context->state[4] = 0x510E527FUL;
|
||||
Context->state[5] = 0x9B05688CUL;
|
||||
Context->state[6] = 0x1F83D9ABUL;
|
||||
Context->state[7] = 0x5BE0CD19UL;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha256Update
|
||||
//
|
||||
// Adds data to the SHA256 context. This will process the data and update the internal state of the context. Keep on
|
||||
// calling this function until all the data has been added. Then call Sha256Finalise to calculate the hash.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void Sha256Update
|
||||
(
|
||||
Sha256Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t BufferSize
|
||||
)
|
||||
{
|
||||
uint32_t n
|
||||
;
|
||||
if( Context->curlen > sizeof(Context->buf) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while( BufferSize > 0 )
|
||||
{
|
||||
if( Context->curlen == 0 && BufferSize >= BLOCK_SIZE )
|
||||
{
|
||||
TransformFunction( Context, (uint8_t*)Buffer );
|
||||
Context->length += BLOCK_SIZE * 8;
|
||||
Buffer = (uint8_t*)Buffer + BLOCK_SIZE;
|
||||
BufferSize -= BLOCK_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = MIN( BufferSize, (BLOCK_SIZE - Context->curlen) );
|
||||
memcpy( Context->buf + Context->curlen, Buffer, (size_t)n );
|
||||
Context->curlen += n;
|
||||
Buffer = (uint8_t*)Buffer + n;
|
||||
BufferSize -= n;
|
||||
if( Context->curlen == BLOCK_SIZE )
|
||||
{
|
||||
TransformFunction( Context, Context->buf );
|
||||
Context->length += 8*BLOCK_SIZE;
|
||||
Context->curlen = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha256Finalise
|
||||
//
|
||||
// Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash). After
|
||||
// calling this, Sha256Initialised must be used to reuse the context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha256Finalise
|
||||
(
|
||||
Sha256Context* Context,
|
||||
SHA256_HASH* Digest
|
||||
)
|
||||
{
|
||||
int i;
|
||||
|
||||
if( Context->curlen >= sizeof(Context->buf) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Increase the length of the message
|
||||
Context->length += Context->curlen * 8;
|
||||
|
||||
// Append the '1' bit
|
||||
Context->buf[Context->curlen++] = (uint8_t)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( Context->curlen > 56 )
|
||||
{
|
||||
while( Context->curlen < 64 )
|
||||
{
|
||||
Context->buf[Context->curlen++] = (uint8_t)0;
|
||||
}
|
||||
TransformFunction(Context, Context->buf);
|
||||
Context->curlen = 0;
|
||||
}
|
||||
|
||||
// Pad up to 56 bytes of zeroes
|
||||
while( Context->curlen < 56 )
|
||||
{
|
||||
Context->buf[Context->curlen++] = (uint8_t)0;
|
||||
}
|
||||
|
||||
// Store length
|
||||
STORE64H( Context->length, Context->buf+56 );
|
||||
TransformFunction( Context, Context->buf );
|
||||
|
||||
// Copy output
|
||||
for( i=0; i<8; i++ )
|
||||
{
|
||||
STORE32H( Context->state[i], Digest->bytes+(4*i) );
|
||||
}
|
||||
}
|
||||
|
||||
78
lib/LibSha256.h
Normal file
78
lib/LibSha256.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibSha256
|
||||
//
|
||||
// Implementation of SHA256 hash function.
|
||||
// Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
// Modified by WaterJuice retaining Public Domain license.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _LibSha256_h_
|
||||
#define _LibSha256_h_
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint64_t length;
|
||||
uint32_t state[8];
|
||||
uint32_t curlen;
|
||||
uint8_t buf[64];
|
||||
} Sha256Context;
|
||||
|
||||
#define SHA256_HASH_SIZE ( 256 / 8 )
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t bytes [SHA256_HASH_SIZE];
|
||||
} SHA256_HASH;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha256Initialise
|
||||
//
|
||||
// Initialises a SHA256 Context. Use this to initialise/reset a context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void Sha256Initialise
|
||||
(
|
||||
Sha256Context* Context
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha256Update
|
||||
//
|
||||
// Adds data to the SHA256 context. This will process the data and update the internal state of the context. Keep on
|
||||
// calling this function until all the data has been added. Then call Sha256Finalise to calculate the hash.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void Sha256Update
|
||||
(
|
||||
Sha256Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t BufferSize
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha256Finalise
|
||||
//
|
||||
// Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash). After
|
||||
// calling this, Sha256Initialised must be used to reuse the context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha256Finalise
|
||||
(
|
||||
Sha256Context* Context,
|
||||
SHA256_HASH* Digest
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#endif //_LibSha256_h_
|
||||
|
||||
277
lib/LibSha512.c
Normal file
277
lib/LibSha512.c
Normal file
@@ -0,0 +1,277 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibSha512
|
||||
//
|
||||
// Implementation of SHA512 hash function.
|
||||
// Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
// Modified by WaterJuice retaining Public Domain license.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "LibSha512.h"
|
||||
#include <memory.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// MACROS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define ROR64( value, bits ) (((value) >> (bits)) | ((value) << (64 - (bits))))
|
||||
|
||||
#define MIN( x, y ) ( ((x)<(y))?(x):(y) )
|
||||
|
||||
#define LOAD64H( x, y ) \
|
||||
{ x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \
|
||||
(((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \
|
||||
(((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \
|
||||
(((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); }
|
||||
|
||||
#define STORE64H( x, y ) \
|
||||
{ (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \
|
||||
(y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \
|
||||
(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
|
||||
(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// CONSTANTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// The K array
|
||||
static const uint64_t K[80] = {
|
||||
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
|
||||
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
|
||||
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
|
||||
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
|
||||
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
|
||||
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
|
||||
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
|
||||
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
|
||||
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
|
||||
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
|
||||
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
|
||||
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
|
||||
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
|
||||
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
|
||||
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
|
||||
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
|
||||
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
|
||||
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
|
||||
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
|
||||
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
|
||||
};
|
||||
|
||||
#define BLOCK_SIZE 128
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// INTERNAL FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Various logical functions
|
||||
#define Ch( x, y, z ) (z ^ (x & (y ^ z)))
|
||||
#define Maj(x, y, z ) (((x | y) & z) | (x & y))
|
||||
#define S( x, n ) ROR64( x, n )
|
||||
#define R( x, n ) (((x)&0xFFFFFFFFFFFFFFFFULL)>>((uint64_t)n))
|
||||
#define Sigma0( x ) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
|
||||
#define Sigma1( x ) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
|
||||
#define Gamma0( x ) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
|
||||
#define Gamma1( x ) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
|
||||
|
||||
#define Sha512Round( a, b, c, d, e, f, g, h, i ) \
|
||||
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
|
||||
t1 = Sigma0(a) + Maj(a, b, c); \
|
||||
d += t0; \
|
||||
h = t0 + t1;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TransformFunction
|
||||
//
|
||||
// Compress 1024-bits
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
static
|
||||
void
|
||||
TransformFunction
|
||||
(
|
||||
Sha512Context* Context,
|
||||
uint8_t* Buffer
|
||||
)
|
||||
{
|
||||
uint64_t S[8];
|
||||
uint64_t W[80];
|
||||
uint64_t t0;
|
||||
uint64_t t1;
|
||||
int i;
|
||||
|
||||
// Copy state into S
|
||||
for( i=0; i<8; i++ )
|
||||
{
|
||||
S[i] = Context->state[i];
|
||||
}
|
||||
|
||||
// Copy the state into 1024-bits into W[0..15]
|
||||
for( i=0; i<16; i++ )
|
||||
{
|
||||
LOAD64H(W[i], Buffer + (8*i));
|
||||
}
|
||||
|
||||
// Fill W[16..79]
|
||||
for( i=16; i<80; i++ )
|
||||
{
|
||||
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
|
||||
}
|
||||
|
||||
// Compress
|
||||
for( i=0; i<80; i+=8 )
|
||||
{
|
||||
Sha512Round(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
|
||||
Sha512Round(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
|
||||
Sha512Round(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
|
||||
Sha512Round(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
|
||||
Sha512Round(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
|
||||
Sha512Round(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
|
||||
Sha512Round(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
|
||||
Sha512Round(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
|
||||
}
|
||||
|
||||
// Feedback
|
||||
for( i=0; i<8; i++ )
|
||||
{
|
||||
Context->state[i] = Context->state[i] + S[i];
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha512Initialise
|
||||
//
|
||||
// Initialises a SHA512 Context. Use this to initialise/reset a context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha512Initialise
|
||||
(
|
||||
Sha512Context* Context
|
||||
)
|
||||
{
|
||||
Context->curlen = 0;
|
||||
Context->length = 0;
|
||||
Context->state[0] = 0x6a09e667f3bcc908ULL;
|
||||
Context->state[1] = 0xbb67ae8584caa73bULL;
|
||||
Context->state[2] = 0x3c6ef372fe94f82bULL;
|
||||
Context->state[3] = 0xa54ff53a5f1d36f1ULL;
|
||||
Context->state[4] = 0x510e527fade682d1ULL;
|
||||
Context->state[5] = 0x9b05688c2b3e6c1fULL;
|
||||
Context->state[6] = 0x1f83d9abfb41bd6bULL;
|
||||
Context->state[7] = 0x5be0cd19137e2179ULL;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha512Update
|
||||
//
|
||||
// Adds data to the SHA512 context. This will process the data and update the internal state of the context. Keep on
|
||||
// calling this function until all the data has been added. Then call Sha512Finalise to calculate the hash.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha512Update
|
||||
(
|
||||
Sha512Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t BufferSize
|
||||
)
|
||||
{
|
||||
uint32_t n;
|
||||
|
||||
if( Context->curlen > sizeof(Context->buf) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while( BufferSize > 0 )
|
||||
{
|
||||
if( Context->curlen == 0 && BufferSize >= BLOCK_SIZE )
|
||||
{
|
||||
TransformFunction( Context, (uint8_t *)Buffer );
|
||||
Context->length += BLOCK_SIZE * 8;
|
||||
Buffer = (uint8_t*)Buffer + BLOCK_SIZE;
|
||||
BufferSize -= BLOCK_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = MIN( BufferSize, (BLOCK_SIZE - Context->curlen) );
|
||||
memcpy( Context->buf + Context->curlen, Buffer, (size_t)n );
|
||||
Context->curlen += n;
|
||||
Buffer = (uint8_t*)Buffer + n;
|
||||
BufferSize -= n;
|
||||
if( Context->curlen == BLOCK_SIZE )
|
||||
{
|
||||
TransformFunction( Context, Context->buf );
|
||||
Context->length += 8*BLOCK_SIZE;
|
||||
Context->curlen = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha512Finalise
|
||||
//
|
||||
// Performs the final calculation of the hash and returns the digest (64 byte buffer containing 512bit hash). After
|
||||
// calling this, Sha512Initialised must be used to reuse the context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha512Finalise
|
||||
(
|
||||
Sha512Context* Context,
|
||||
SHA512_HASH* Digest
|
||||
)
|
||||
{
|
||||
int i;
|
||||
|
||||
if( Context->curlen >= sizeof(Context->buf) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Increase the length of the message
|
||||
Context->length += Context->curlen * 8ULL;
|
||||
|
||||
// Append the '1' bit
|
||||
Context->buf[Context->curlen++] = (uint8_t)0x80;
|
||||
|
||||
// If the length is currently above 112 bytes we append zeros
|
||||
// then compress. Then we can fall back to padding zeros and length
|
||||
// encoding like normal.
|
||||
if( Context->curlen > 112 )
|
||||
{
|
||||
while( Context->curlen < 128 )
|
||||
{
|
||||
Context->buf[Context->curlen++] = (uint8_t)0;
|
||||
}
|
||||
TransformFunction( Context, Context->buf );
|
||||
Context->curlen = 0;
|
||||
}
|
||||
|
||||
// Pad up to 120 bytes of zeroes
|
||||
// note: that from 112 to 120 is the 64 MSB of the length. We assume that you won't hash
|
||||
// > 2^64 bits of data... :-)
|
||||
while( Context->curlen < 120 )
|
||||
{
|
||||
Context->buf[Context->curlen++] = (uint8_t)0;
|
||||
}
|
||||
|
||||
// Store length
|
||||
STORE64H( Context->length, Context->buf+120 );
|
||||
TransformFunction( Context, Context->buf );
|
||||
|
||||
// Copy output
|
||||
for( i=0; i<8; i++ )
|
||||
{
|
||||
STORE64H( Context->state[i], Digest->bytes+(8*i) );
|
||||
}
|
||||
}
|
||||
|
||||
78
lib/LibSha512.h
Normal file
78
lib/LibSha512.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// LibSha512
|
||||
//
|
||||
// Implementation of SHA512 hash function.
|
||||
// Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
// Modified by WaterJuice retaining Public Domain license.
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _LibSha512_h_
|
||||
#define _LibSha512_h_
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint64_t length;
|
||||
uint64_t state[8];
|
||||
uint32_t curlen;
|
||||
uint8_t buf[128];
|
||||
} Sha512Context;
|
||||
|
||||
#define SHA512_HASH_SIZE ( 512 / 8 )
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t bytes [SHA512_HASH_SIZE];
|
||||
} SHA512_HASH;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC FUNCTIONS
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha512Initialise
|
||||
//
|
||||
// Initialises a SHA512 Context. Use this to initialise/reset a context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void Sha512Initialise
|
||||
(
|
||||
Sha512Context* Context
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha512Update
|
||||
//
|
||||
// Adds data to the SHA512 context. This will process the data and update the internal state of the context. Keep on
|
||||
// calling this function until all the data has been added. Then call Sha512Finalise to calculate the hash.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void Sha512Update
|
||||
(
|
||||
Sha512Context* Context,
|
||||
void* Buffer,
|
||||
uint32_t BufferSize
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Sha512Finalise
|
||||
//
|
||||
// Performs the final calculation of the hash and returns the digest (64 byte buffer containing 512bit hash). After
|
||||
// calling this, Sha512Initialised must be used to reuse the context.
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
Sha512Finalise
|
||||
(
|
||||
Sha512Context* Context,
|
||||
SHA512_HASH* Digest
|
||||
);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#endif //_LibSha512_h_
|
||||
|
||||
Reference in New Issue
Block a user