Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers into modules, and delete rossubsys.

This commit is contained in:
Colin Finck 2017-10-03 07:45:34 +00:00
parent b94e2d8ca0
commit c2c66aff7d
24198 changed files with 0 additions and 37285 deletions

View file

@ -0,0 +1,13 @@
list(APPEND SOURCE
aes.c
des.c
md4.c
md5.c
mvAesAlg.c
rc4.c
sha1.c
util.c)
add_library(cryptlib ${SOURCE})
add_dependencies(cryptlib xdk)

View file

@ -0,0 +1,29 @@
This libbrary implements the following algorithms:
MD4
---
- files: md4.c, md4.h
- Implements: MD4Init, MD4Update, MD4Final
MD5
---
- files: md5.c, md5.h
- Implements: MD5Init, MD5Update, MD5Final
RC4
---
- files: rc4.c, rc4.h
- Implements: rc4_init, rc4_crypt
SHA1
----
- files: sha1.c, sha1.h
- Implements: A_SHAInit, A_SHAUpdate, A_SHAFinal
AES
---
- files: mvAesAlg.c, mvAesAlg.h, mvOs.h, mvAesBoxes.dat
- Taken from: http://enduser.subsignal.org/~trondah/tree/target/linux/generic/files/crypto/ocf/kirkwood/cesa/AES/
- Original reference implementation: https://github.com/briandfoy/crypt-rijndael/tree/master/rijndael-vals/reference%20implementation
- Implements: rijndaelEncrypt128, rijndaelDecrypt128

1266
sdk/lib/cryptlib/aes.c Normal file

File diff suppressed because it is too large Load diff

1495
sdk/lib/cryptlib/des.c Normal file

File diff suppressed because it is too large Load diff

246
sdk/lib/cryptlib/md4.c Normal file
View file

@ -0,0 +1,246 @@
/*
* Copyright (C) 2001 Nikos Mavroyanopoulos
* Copyright (C) 2004 Hans Leidekker
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* This code implements the MD4 message-digest algorithm.
* It is based on code in the public domain written by Colin
* Plumb in 1993. The algorithm is due to Ron Rivest.
*
* Equivalent code is available from RSA Data Security, Inc.
* This code has been tested against that, and is equivalent,
* except that you don't need to include two pages of legalese
* with every copy.
*
* To compute the message digest of a chunk of bytes, declare an
* MD4_CTX structure, pass it to MD4Init, call MD4Update as
* needed on buffers full of bytes, and then call MD4Final, which
* will fill a supplied 16-byte array with the digest.
*/
#include "md4.h"
#include "util.h"
static void MD4Transform( unsigned int buf[4], unsigned int const in[16] );
/*
* Start MD4 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
VOID NTAPI MD4Init( MD4_CTX *ctx )
{
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
ctx->buf[3] = 0x10325476;
ctx->i[0] = ctx->i[1] = 0;
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
VOID NTAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len )
{
register unsigned int t;
/* Update bitcount */
t = ctx->i[0];
if ((ctx->i[0] = t + (len << 3)) < t)
ctx->i[1]++; /* Carry from low to high */
ctx->i[1] += len >> 29;
t = (t >> 3) & 0x3f;
/* Handle any leading odd-sized chunks */
if (t)
{
unsigned char *p = (unsigned char *)ctx->in + t;
t = 64 - t;
if (len < t)
{
memcpy( p, buf, len );
return;
}
memcpy( p, buf, t );
byteReverse( ctx->in, 16 );
MD4Transform( ctx->buf, (unsigned int *)ctx->in );
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
while (len >= 64)
{
memcpy( ctx->in, buf, 64 );
byteReverse( ctx->in, 16 );
MD4Transform( ctx->buf, (unsigned int *)ctx->in );
buf += 64;
len -= 64;
}
/* Handle any remaining bytes of data. */
memcpy( ctx->in, buf, len );
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
VOID NTAPI MD4Final( MD4_CTX *ctx )
{
unsigned int count;
unsigned char *p;
/* Compute number of bytes mod 64 */
count = (ctx->i[0] >> 3) & 0x3F;
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
p = ctx->in + count;
*p++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
count = 64 - 1 - count;
/* Pad out to 56 mod 64 */
if (count < 8)
{
/* Two lots of padding: Pad the first block to 64 bytes */
memset( p, 0, count );
byteReverse( ctx->in, 16 );
MD4Transform( ctx->buf, (unsigned int *)ctx->in );
/* Now fill the next block with 56 bytes */
memset( ctx->in, 0, 56 );
}
else
{
/* Pad block to 56 bytes */
memset( p, 0, count - 8 );
}
byteReverse( ctx->in, 14 );
/* Append length in bits and transform */
((unsigned int *)ctx->in)[14] = ctx->i[0];
((unsigned int *)ctx->in)[15] = ctx->i[1];
MD4Transform( ctx->buf, (unsigned int *)ctx->in );
byteReverse( (unsigned char *)ctx->buf, 4 );
memcpy( ctx->digest, ctx->buf, 16 );
}
/* The three core functions */
#define rotl32(x,n) (((x) << ((unsigned int)(n))) | ((x) >> (32 - (unsigned int)(n))))
#define F( x, y, z ) (((x) & (y)) | ((~x) & (z)))
#define G( x, y, z ) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
#define H( x, y, z ) ((x) ^ (y) ^ (z))
#define FF( a, b, c, d, x, s ) { \
(a) += F( (b), (c), (d) ) + (x); \
(a) = rotl32( (a), (s) ); \
}
#define GG( a, b, c, d, x, s ) { \
(a) += G( (b), (c), (d) ) + (x) + (unsigned int)0x5a827999; \
(a) = rotl32( (a), (s) ); \
}
#define HH( a, b, c, d, x, s ) { \
(a) += H( (b), (c), (d) ) + (x) + (unsigned int)0x6ed9eba1; \
(a) = rotl32( (a), (s) ); \
}
/*
* The core of the MD4 algorithm
*/
static void MD4Transform( unsigned int buf[4], const unsigned int in[16] )
{
register unsigned int a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
FF( a, b, c, d, in[0], 3 );
FF( d, a, b, c, in[1], 7 );
FF( c, d, a, b, in[2], 11 );
FF( b, c, d, a, in[3], 19 );
FF( a, b, c, d, in[4], 3 );
FF( d, a, b, c, in[5], 7 );
FF( c, d, a, b, in[6], 11 );
FF( b, c, d, a, in[7], 19 );
FF( a, b, c, d, in[8], 3 );
FF( d, a, b, c, in[9], 7 );
FF( c, d, a, b, in[10], 11 );
FF( b, c, d, a, in[11], 19 );
FF( a, b, c, d, in[12], 3 );
FF( d, a, b, c, in[13], 7 );
FF( c, d, a, b, in[14], 11 );
FF( b, c, d, a, in[15], 19 );
GG( a, b, c, d, in[0], 3 );
GG( d, a, b, c, in[4], 5 );
GG( c, d, a, b, in[8], 9 );
GG( b, c, d, a, in[12], 13 );
GG( a, b, c, d, in[1], 3 );
GG( d, a, b, c, in[5], 5 );
GG( c, d, a, b, in[9], 9 );
GG( b, c, d, a, in[13], 13 );
GG( a, b, c, d, in[2], 3 );
GG( d, a, b, c, in[6], 5 );
GG( c, d, a, b, in[10], 9 );
GG( b, c, d, a, in[14], 13 );
GG( a, b, c, d, in[3], 3 );
GG( d, a, b, c, in[7], 5 );
GG( c, d, a, b, in[11], 9 );
GG( b, c, d, a, in[15], 13 );
HH( a, b, c, d, in[0], 3 );
HH( d, a, b, c, in[8], 9 );
HH( c, d, a, b, in[4], 11 );
HH( b, c, d, a, in[12], 15 );
HH( a, b, c, d, in[2], 3 );
HH( d, a, b, c, in[10], 9 );
HH( c, d, a, b, in[6], 11 );
HH( b, c, d, a, in[14], 15 );
HH( a, b, c, d, in[1], 3 );
HH( d, a, b, c, in[9], 9 );
HH( c, d, a, b, in[5], 11 );
HH( b, c, d, a, in[13], 15 );
HH( a, b, c, d, in[3], 3 );
HH( d, a, b, c, in[11], 9 );
HH( c, d, a, b, in[7], 11 );
HH( b, c, d, a, in[15], 15 );
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}

19
sdk/lib/cryptlib/md4.h Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include <ntdef.h>
typedef struct
{
unsigned int buf[4];
unsigned int i[2];
unsigned char in[64];
unsigned char digest[16];
} MD4_CTX;
VOID NTAPI MD4Init( MD4_CTX *ctx );
VOID NTAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len );
VOID NTAPI MD4Final( MD4_CTX *ctx );

256
sdk/lib/cryptlib/md5.c Normal file
View file

@ -0,0 +1,256 @@
/*
* Copyright (C) 2001 Nikos Mavroyanopoulos
* Copyright (C) 2004 Hans Leidekker
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* This code implements the MD5 message-digest algorithm.
* It is based on code in the public domain written by Colin
* Plumb in 1993. The algorithm is due to Ron Rivest.
*
* Equivalent code is available from RSA Data Security, Inc.
* This code has been tested against that, and is equivalent,
* except that you don't need to include two pages of legalese
* with every copy.
*
* To compute the message digest of a chunk of bytes, declare an
* MD5_CTX structure, pass it to MD5Init, call MD5Update as
* needed on buffers full of bytes, and then call MD5Final, which
* will fill a supplied 16-byte array with the digest.
*/
#include "md5.h"
#include "util.h"
static void MD5Transform( unsigned int buf[4], const unsigned int in[16] );
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
VOID NTAPI MD5Init( MD5_CTX *ctx )
{
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
ctx->buf[3] = 0x10325476;
ctx->i[0] = ctx->i[1] = 0;
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
VOID NTAPI MD5Update( MD5_CTX *ctx, const unsigned char *buf, unsigned int len )
{
register unsigned int t;
/* Update bitcount */
t = ctx->i[0];
if ((ctx->i[0] = t + (len << 3)) < t)
ctx->i[1]++; /* Carry from low to high */
ctx->i[1] += len >> 29;
t = (t >> 3) & 0x3f;
/* Handle any leading odd-sized chunks */
if (t)
{
unsigned char *p = (unsigned char *)ctx->in + t;
t = 64 - t;
if (len < t)
{
memcpy( p, buf, len );
return;
}
memcpy( p, buf, t );
byteReverse( ctx->in, 16 );
MD5Transform( ctx->buf, (unsigned int *)ctx->in );
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
while (len >= 64)
{
memcpy( ctx->in, buf, 64 );
byteReverse( ctx->in, 16 );
MD5Transform( ctx->buf, (unsigned int *)ctx->in );
buf += 64;
len -= 64;
}
/* Handle any remaining bytes of data. */
memcpy( ctx->in, buf, len );
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
VOID NTAPI MD5Final( MD5_CTX *ctx )
{
unsigned int count;
unsigned char *p;
/* Compute number of bytes mod 64 */
count = (ctx->i[0] >> 3) & 0x3F;
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
p = ctx->in + count;
*p++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
count = 64 - 1 - count;
/* Pad out to 56 mod 64 */
if (count < 8)
{
/* Two lots of padding: Pad the first block to 64 bytes */
memset( p, 0, count );
byteReverse( ctx->in, 16 );
MD5Transform( ctx->buf, (unsigned int *)ctx->in );
/* Now fill the next block with 56 bytes */
memset( ctx->in, 0, 56 );
}
else
{
/* Pad block to 56 bytes */
memset( p, 0, count - 8 );
}
byteReverse( ctx->in, 14 );
/* Append length in bits and transform */
((unsigned int *)ctx->in)[14] = ctx->i[0];
((unsigned int *)ctx->in)[15] = ctx->i[1];
MD5Transform( ctx->buf, (unsigned int *)ctx->in );
byteReverse( (unsigned char *)ctx->buf, 4 );
memcpy( ctx->digest, ctx->buf, 16 );
}
/* The four core functions - F1 is optimized somewhat */
/* #define F1( x, y, z ) (x & y | ~x & z) */
#define F1( x, y, z ) (z ^ (x & (y ^ z)))
#define F2( x, y, z ) F1( z, x, y )
#define F3( x, y, z ) (x ^ y ^ z)
#define F4( x, y, z ) (y ^ (x | ~z))
/* This is the central step in the MD5 algorithm. */
#define MD5STEP( f, w, x, y, z, data, s ) \
( w += f( x, y, z ) + data, w = w << s | w >> (32 - s), w += x )
/*
* The core of the MD5 algorithm, this alters an existing MD5 hash to
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
static void MD5Transform( unsigned int buf[4], const unsigned int in[16] )
{
register unsigned int a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
MD5STEP( F1, a, b, c, d, in[0] + 0xd76aa478, 7 );
MD5STEP( F1, d, a, b, c, in[1] + 0xe8c7b756, 12 );
MD5STEP( F1, c, d, a, b, in[2] + 0x242070db, 17 );
MD5STEP( F1, b, c, d, a, in[3] + 0xc1bdceee, 22 );
MD5STEP( F1, a, b, c, d, in[4] + 0xf57c0faf, 7 );
MD5STEP( F1, d, a, b, c, in[5] + 0x4787c62a, 12 );
MD5STEP( F1, c, d, a, b, in[6] + 0xa8304613, 17 );
MD5STEP( F1, b, c, d, a, in[7] + 0xfd469501, 22 );
MD5STEP( F1, a, b, c, d, in[8] + 0x698098d8, 7 );
MD5STEP( F1, d, a, b, c, in[9] + 0x8b44f7af, 12 );
MD5STEP( F1, c, d, a, b, in[10] + 0xffff5bb1, 17 );
MD5STEP( F1, b, c, d, a, in[11] + 0x895cd7be, 22 );
MD5STEP( F1, a, b, c, d, in[12] + 0x6b901122, 7 );
MD5STEP( F1, d, a, b, c, in[13] + 0xfd987193, 12 );
MD5STEP( F1, c, d, a, b, in[14] + 0xa679438e, 17 );
MD5STEP( F1, b, c, d, a, in[15] + 0x49b40821, 22 );
MD5STEP( F2, a, b, c, d, in[1] + 0xf61e2562, 5 );
MD5STEP( F2, d, a, b, c, in[6] + 0xc040b340, 9 );
MD5STEP( F2, c, d, a, b, in[11] + 0x265e5a51, 14 );
MD5STEP( F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20 );
MD5STEP( F2, a, b, c, d, in[5] + 0xd62f105d, 5 );
MD5STEP( F2, d, a, b, c, in[10] + 0x02441453, 9 );
MD5STEP( F2, c, d, a, b, in[15] + 0xd8a1e681, 14 );
MD5STEP( F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20 );
MD5STEP( F2, a, b, c, d, in[9] + 0x21e1cde6, 5 );
MD5STEP( F2, d, a, b, c, in[14] + 0xc33707d6, 9 );
MD5STEP( F2, c, d, a, b, in[3] + 0xf4d50d87, 14 );
MD5STEP( F2, b, c, d, a, in[8] + 0x455a14ed, 20 );
MD5STEP( F2, a, b, c, d, in[13] + 0xa9e3e905, 5 );
MD5STEP( F2, d, a, b, c, in[2] + 0xfcefa3f8, 9 );
MD5STEP( F2, c, d, a, b, in[7] + 0x676f02d9, 14 );
MD5STEP( F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20 );
MD5STEP( F3, a, b, c, d, in[5] + 0xfffa3942, 4 );
MD5STEP( F3, d, a, b, c, in[8] + 0x8771f681, 11 );
MD5STEP( F3, c, d, a, b, in[11] + 0x6d9d6122, 16 );
MD5STEP( F3, b, c, d, a, in[14] + 0xfde5380c, 23 );
MD5STEP( F3, a, b, c, d, in[1] + 0xa4beea44, 4 );
MD5STEP( F3, d, a, b, c, in[4] + 0x4bdecfa9, 11 );
MD5STEP( F3, c, d, a, b, in[7] + 0xf6bb4b60, 16 );
MD5STEP( F3, b, c, d, a, in[10] + 0xbebfbc70, 23 );
MD5STEP( F3, a, b, c, d, in[13] + 0x289b7ec6, 4 );
MD5STEP( F3, d, a, b, c, in[0] + 0xeaa127fa, 11 );
MD5STEP( F3, c, d, a, b, in[3] + 0xd4ef3085, 16 );
MD5STEP( F3, b, c, d, a, in[6] + 0x04881d05, 23 );
MD5STEP( F3, a, b, c, d, in[9] + 0xd9d4d039, 4 );
MD5STEP( F3, d, a, b, c, in[12] + 0xe6db99e5, 11 );
MD5STEP( F3, c, d, a, b, in[15] + 0x1fa27cf8, 16 );
MD5STEP( F3, b, c, d, a, in[2] + 0xc4ac5665, 23 );
MD5STEP( F4, a, b, c, d, in[0] + 0xf4292244, 6 );
MD5STEP( F4, d, a, b, c, in[7] + 0x432aff97, 10 );
MD5STEP( F4, c, d, a, b, in[14] + 0xab9423a7, 15 );
MD5STEP( F4, b, c, d, a, in[5] + 0xfc93a039, 21 );
MD5STEP( F4, a, b, c, d, in[12] + 0x655b59c3, 6 );
MD5STEP( F4, d, a, b, c, in[3] + 0x8f0ccc92, 10 );
MD5STEP( F4, c, d, a, b, in[10] + 0xffeff47d, 15 );
MD5STEP( F4, b, c, d, a, in[1] + 0x85845dd1, 21 );
MD5STEP( F4, a, b, c, d, in[8] + 0x6fa87e4f, 6 );
MD5STEP( F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10 );
MD5STEP( F4, c, d, a, b, in[6] + 0xa3014314, 15 );
MD5STEP( F4, b, c, d, a, in[13] + 0x4e0811a1, 21 );
MD5STEP( F4, a, b, c, d, in[4] + 0xf7537e82, 6 );
MD5STEP( F4, d, a, b, c, in[11] + 0xbd3af235, 10 );
MD5STEP( F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15 );
MD5STEP( F4, b, c, d, a, in[9] + 0xeb86d391, 21 );
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}

19
sdk/lib/cryptlib/md5.h Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include <ntdef.h>
typedef struct
{
unsigned int i[2];
unsigned int buf[4];
unsigned char in[64];
unsigned char digest[16];
} MD5_CTX;
VOID NTAPI MD5Init( MD5_CTX *ctx );
VOID NTAPI MD5Update( MD5_CTX *ctx, const unsigned char *buf, unsigned int len );
VOID NTAPI MD5Final( MD5_CTX *ctx );

317
sdk/lib/cryptlib/mvAesAlg.c Normal file
View file

@ -0,0 +1,317 @@
/* rijndael-alg-ref.c v2.0 August '99
* Reference ANSI C code
* authors: Paulo Barreto
* Vincent Rijmen, K.U.Leuven
*
* This code is placed in the public domain.
*/
#include "mvOs.h"
#include "mvAesAlg.h"
#include "mvAesBoxes.dat"
MV_U8 mul1(MV_U8 aa, MV_U8 bb);
void KeyAddition(MV_U8 a[4][MAXBC], MV_U8 rk[4][MAXBC], MV_U8 BC);
void ShiftRow128Enc(MV_U8 a[4][MAXBC]);
void ShiftRow128Dec(MV_U8 a[4][MAXBC]);
void Substitution(MV_U8 a[4][MAXBC], MV_U8 box[256]);
void MixColumn(MV_U8 a[4][MAXBC], MV_U8 rk[4][MAXBC]);
void InvMixColumn(MV_U8 a[4][MAXBC]);
#define mul(aa, bb) (mask[bb] & Alogtable[aa + Logtable[bb]])
MV_U8 mul1(MV_U8 aa, MV_U8 bb)
{
return mask[bb] & Alogtable[aa + Logtable[bb]];
}
void KeyAddition(MV_U8 a[4][MAXBC], MV_U8 rk[4][MAXBC], MV_U8 BC)
{
/* Exor corresponding text input and round key input bytes
*/
((MV_U32*)(&(a[0][0])))[0] ^= ((MV_U32*)(&(rk[0][0])))[0];
((MV_U32*)(&(a[1][0])))[0] ^= ((MV_U32*)(&(rk[1][0])))[0];
((MV_U32*)(&(a[2][0])))[0] ^= ((MV_U32*)(&(rk[2][0])))[0];
((MV_U32*)(&(a[3][0])))[0] ^= ((MV_U32*)(&(rk[3][0])))[0];
}
void ShiftRow128Enc(MV_U8 a[4][MAXBC]) {
/* Row 0 remains unchanged
* The other three rows are shifted a variable amount
*/
MV_U8 tmp[MAXBC];
tmp[0] = a[1][1];
tmp[1] = a[1][2];
tmp[2] = a[1][3];
tmp[3] = a[1][0];
((MV_U32*)(&(a[1][0])))[0] = ((MV_U32*)(&(tmp[0])))[0];
/*
a[1][0] = tmp[0];
a[1][1] = tmp[1];
a[1][2] = tmp[2];
a[1][3] = tmp[3];
*/
tmp[0] = a[2][2];
tmp[1] = a[2][3];
tmp[2] = a[2][0];
tmp[3] = a[2][1];
((MV_U32*)(&(a[2][0])))[0] = ((MV_U32*)(&(tmp[0])))[0];
/*
a[2][0] = tmp[0];
a[2][1] = tmp[1];
a[2][2] = tmp[2];
a[2][3] = tmp[3];
*/
tmp[0] = a[3][3];
tmp[1] = a[3][0];
tmp[2] = a[3][1];
tmp[3] = a[3][2];
((MV_U32*)(&(a[3][0])))[0] = ((MV_U32*)(&(tmp[0])))[0];
/*
a[3][0] = tmp[0];
a[3][1] = tmp[1];
a[3][2] = tmp[2];
a[3][3] = tmp[3];
*/
}
void ShiftRow128Dec(MV_U8 a[4][MAXBC]) {
/* Row 0 remains unchanged
* The other three rows are shifted a variable amount
*/
MV_U8 tmp[MAXBC];
tmp[0] = a[1][3];
tmp[1] = a[1][0];
tmp[2] = a[1][1];
tmp[3] = a[1][2];
((MV_U32*)(&(a[1][0])))[0] = ((MV_U32*)(&(tmp[0])))[0];
/*
a[1][0] = tmp[0];
a[1][1] = tmp[1];
a[1][2] = tmp[2];
a[1][3] = tmp[3];
*/
tmp[0] = a[2][2];
tmp[1] = a[2][3];
tmp[2] = a[2][0];
tmp[3] = a[2][1];
((MV_U32*)(&(a[2][0])))[0] = ((MV_U32*)(&(tmp[0])))[0];
/*
a[2][0] = tmp[0];
a[2][1] = tmp[1];
a[2][2] = tmp[2];
a[2][3] = tmp[3];
*/
tmp[0] = a[3][1];
tmp[1] = a[3][2];
tmp[2] = a[3][3];
tmp[3] = a[3][0];
((MV_U32*)(&(a[3][0])))[0] = ((MV_U32*)(&(tmp[0])))[0];
/*
a[3][0] = tmp[0];
a[3][1] = tmp[1];
a[3][2] = tmp[2];
a[3][3] = tmp[3];
*/
}
void Substitution(MV_U8 a[4][MAXBC], MV_U8 box[256]) {
/* Replace every byte of the input by the byte at that place
* in the nonlinear S-box
*/
int i, j;
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++) a[i][j] = box[a[i][j]] ;
}
void MixColumn(MV_U8 a[4][MAXBC], MV_U8 rk[4][MAXBC]) {
/* Mix the four bytes of every column in a linear way
*/
MV_U8 b[4][MAXBC];
int i, j;
for(j = 0; j < 4; j++){
b[0][j] = mul(25,a[0][j]) ^ mul(1,a[1][j]) ^ a[2][j] ^ a[3][j];
b[1][j] = mul(25,a[1][j]) ^ mul(1,a[2][j]) ^ a[3][j] ^ a[0][j];
b[2][j] = mul(25,a[2][j]) ^ mul(1,a[3][j]) ^ a[0][j] ^ a[1][j];
b[3][j] = mul(25,a[3][j]) ^ mul(1,a[0][j]) ^ a[1][j] ^ a[2][j];
}
for(i = 0; i < 4; i++)
/*for(j = 0; j < BC; j++) a[i][j] = b[i][j];*/
((MV_U32*)(&(a[i][0])))[0] = ((MV_U32*)(&(b[i][0])))[0] ^ ((MV_U32*)(&(rk[i][0])))[0];
}
void InvMixColumn(MV_U8 a[4][MAXBC]) {
/* Mix the four bytes of every column in a linear way
* This is the opposite operation of Mixcolumn
*/
MV_U8 b[4][MAXBC];
int i, j;
for(j = 0; j < 4; j++){
b[0][j] = mul(223,a[0][j]) ^ mul(104,a[1][j]) ^ mul(238,a[2][j]) ^ mul(199,a[3][j]);
b[1][j] = mul(223,a[1][j]) ^ mul(104,a[2][j]) ^ mul(238,a[3][j]) ^ mul(199,a[0][j]);
b[2][j] = mul(223,a[2][j]) ^ mul(104,a[3][j]) ^ mul(238,a[0][j]) ^ mul(199,a[1][j]);
b[3][j] = mul(223,a[3][j]) ^ mul(104,a[0][j]) ^ mul(238,a[1][j]) ^ mul(199,a[2][j]);
}
for(i = 0; i < 4; i++)
/*for(j = 0; j < BC; j++) a[i][j] = b[i][j];*/
((MV_U32*)(&(a[i][0])))[0] = ((MV_U32*)(&(b[i][0])))[0];
}
int rijndaelKeySched (MV_U8 k[4][MAXKC], int keyBits, int blockBits, MV_U8 W[MAXROUNDS+1][4][MAXBC])
{
/* Calculate the necessary round keys
* The number of calculations depends on keyBits and blockBits
*/
int KC, BC, ROUNDS;
int i, j, t, rconpointer = 0;
MV_U8 tk[4][MAXKC];
switch (keyBits) {
case 128: KC = 4; break;
case 192: KC = 6; break;
case 256: KC = 8; break;
default : return (-1);
}
switch (blockBits) {
case 128: BC = 4; break;
case 192: BC = 6; break;
case 256: BC = 8; break;
default : return (-2);
}
switch (keyBits >= blockBits ? keyBits : blockBits) {
case 128: ROUNDS = 10; break;
case 192: ROUNDS = 12; break;
case 256: ROUNDS = 14; break;
default : return (-3); /* this cannot happen */
}
for(j = 0; j < KC; j++)
for(i = 0; i < 4; i++)
tk[i][j] = k[i][j];
t = 0;
/* copy values into round key array */
for(j = 0; (j < KC) && (t < (ROUNDS+1)*BC); j++, t++)
for(i = 0; i < 4; i++) W[t / BC][i][t % BC] = tk[i][j];
while (t < (ROUNDS+1)*BC) { /* while not enough round key material calculated */
/* calculate new values */
for(i = 0; i < 4; i++)
tk[i][0] ^= S[tk[(i+1)%4][KC-1]];
tk[0][0] ^= rcon[rconpointer++];
if (KC != 8)
for(j = 1; j < KC; j++)
for(i = 0; i < 4; i++) tk[i][j] ^= tk[i][j-1];
else {
for(j = 1; j < KC/2; j++)
for(i = 0; i < 4; i++) tk[i][j] ^= tk[i][j-1];
for(i = 0; i < 4; i++) tk[i][KC/2] ^= S[tk[i][KC/2 - 1]];
for(j = KC/2 + 1; j < KC; j++)
for(i = 0; i < 4; i++) tk[i][j] ^= tk[i][j-1];
}
/* copy values into round key array */
for(j = 0; (j < KC) && (t < (ROUNDS+1)*BC); j++, t++)
for(i = 0; i < 4; i++) W[t / BC][i][t % BC] = tk[i][j];
}
return 0;
}
int rijndaelEncrypt128(MV_U8 a[4][MAXBC], MV_U8 rk[MAXROUNDS+1][4][MAXBC], int rounds)
{
/* Encryption of one block.
*/
int r, BC, ROUNDS;
BC = 4;
ROUNDS = rounds;
/* begin with a key addition
*/
KeyAddition(a,rk[0],BC);
/* ROUNDS-1 ordinary rounds
*/
for(r = 1; r < ROUNDS; r++) {
Substitution(a,S);
ShiftRow128Enc(a);
MixColumn(a, rk[r]);
/*KeyAddition(a,rk[r],BC);*/
}
/* Last round is special: there is no MixColumn
*/
Substitution(a,S);
ShiftRow128Enc(a);
KeyAddition(a,rk[ROUNDS],BC);
return 0;
}
int rijndaelDecrypt128(MV_U8 a[4][MAXBC], MV_U8 rk[MAXROUNDS+1][4][MAXBC], int rounds)
{
int r, BC, ROUNDS;
BC = 4;
ROUNDS = rounds;
/* To decrypt: apply the inverse operations of the encrypt routine,
* in opposite order
*
* (KeyAddition is an involution: it 's equal to its inverse)
* (the inverse of Substitution with table S is Substitution with the inverse table of S)
* (the inverse of Shiftrow is Shiftrow over a suitable distance)
*/
/* First the special round:
* without InvMixColumn
* with extra KeyAddition
*/
KeyAddition(a,rk[ROUNDS],BC);
ShiftRow128Dec(a);
Substitution(a,Si);
/* ROUNDS-1 ordinary rounds
*/
for(r = ROUNDS-1; r > 0; r--) {
KeyAddition(a,rk[r],BC);
InvMixColumn(a);
ShiftRow128Dec(a);
Substitution(a,Si);
}
/* End with the extra key addition
*/
KeyAddition(a,rk[0],BC);
return 0;
}

View file

@ -0,0 +1,19 @@
/* rijndael-alg-ref.h v2.0 August '99
* Reference ANSI C code
* authors: Paulo Barreto
* Vincent Rijmen, K.U.Leuven
*/
#ifndef __RIJNDAEL_ALG_H
#define __RIJNDAEL_ALG_H
#define MAXBC (128/32)
#define MAXKC (256/32)
#define MAXROUNDS 14
int rijndaelKeySched (MV_U8 k[4][MAXKC], int keyBits, int blockBits, MV_U8 rk[MAXROUNDS+1][4][MAXBC]);
int rijndaelEncrypt128(MV_U8 a[4][MAXBC], MV_U8 rk[MAXROUNDS+1][4][MAXBC], int rounds);
int rijndaelDecrypt128(MV_U8 a[4][MAXBC], MV_U8 rk[MAXROUNDS+1][4][MAXBC], int rounds);
#endif /* __RIJNDAEL_ALG_H */

View file

@ -0,0 +1,123 @@
static MV_U8 mask[256] = {
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
static MV_U8 Logtable[256] = {
0, 0, 25, 1, 50, 2, 26, 198, 75, 199, 27, 104, 51, 238, 223, 3,
100, 4, 224, 14, 52, 141, 129, 239, 76, 113, 8, 200, 248, 105, 28, 193,
125, 194, 29, 181, 249, 185, 39, 106, 77, 228, 166, 114, 154, 201, 9, 120,
101, 47, 138, 5, 33, 15, 225, 36, 18, 240, 130, 69, 53, 147, 218, 142,
150, 143, 219, 189, 54, 208, 206, 148, 19, 92, 210, 241, 64, 70, 131, 56,
102, 221, 253, 48, 191, 6, 139, 98, 179, 37, 226, 152, 34, 136, 145, 16,
126, 110, 72, 195, 163, 182, 30, 66, 58, 107, 40, 84, 250, 133, 61, 186,
43, 121, 10, 21, 155, 159, 94, 202, 78, 212, 172, 229, 243, 115, 167, 87,
175, 88, 168, 80, 244, 234, 214, 116, 79, 174, 233, 213, 231, 230, 173, 232,
44, 215, 117, 122, 235, 22, 11, 245, 89, 203, 95, 176, 156, 169, 81, 160,
127, 12, 246, 111, 23, 196, 73, 236, 216, 67, 31, 45, 164, 118, 123, 183,
204, 187, 62, 90, 251, 96, 177, 134, 59, 82, 161, 108, 170, 85, 41, 157,
151, 178, 135, 144, 97, 190, 220, 252, 188, 149, 207, 205, 55, 63, 91, 209,
83, 57, 132, 60, 65, 162, 109, 71, 20, 42, 158, 93, 86, 242, 211, 171,
68, 17, 146, 217, 35, 32, 46, 137, 180, 124, 184, 38, 119, 153, 227, 165,
103, 74, 237, 222, 197, 49, 254, 24, 13, 99, 140, 128, 192, 247, 112, 7,
};
static MV_U8 Alogtable[512] = {
1, 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53,
95, 225, 56, 72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170,
229, 52, 92, 228, 55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49,
83, 245, 4, 12, 20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178, 205,
76, 212, 103, 169, 224, 59, 77, 215, 98, 166, 241, 8, 24, 40, 120, 136,
131, 158, 185, 208, 107, 189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154,
181, 196, 87, 249, 16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163,
254, 25, 43, 125, 135, 146, 173, 236, 47, 113, 147, 174, 233, 32, 96, 160,
251, 22, 58, 78, 210, 109, 183, 194, 93, 231, 50, 86, 250, 21, 63, 65,
195, 94, 226, 61, 71, 201, 64, 192, 91, 237, 44, 116, 156, 191, 218, 117,
159, 186, 213, 100, 172, 239, 42, 126, 130, 157, 188, 223, 122, 142, 137, 128,
155, 182, 193, 88, 232, 35, 101, 175, 234, 37, 111, 177, 200, 67, 197, 84,
252, 31, 33, 99, 165, 244, 7, 9, 27, 45, 119, 153, 176, 203, 70, 202,
69, 207, 74, 222, 121, 139, 134, 145, 168, 227, 62, 66, 198, 81, 243, 14,
18, 54, 90, 238, 41, 123, 141, 140, 143, 138, 133, 148, 167, 242, 13, 23,
57, 75, 221, 124, 132, 151, 162, 253, 28, 36, 108, 180, 199, 82, 246, 1,
3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53,
95, 225, 56, 72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170,
229, 52, 92, 228, 55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49,
83, 245, 4, 12, 20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178, 205,
76, 212, 103, 169, 224, 59, 77, 215, 98, 166, 241, 8, 24, 40, 120, 136,
131, 158, 185, 208, 107, 189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154,
181, 196, 87, 249, 16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163,
254, 25, 43, 125, 135, 146, 173, 236, 47, 113, 147, 174, 233, 32, 96, 160,
251, 22, 58, 78, 210, 109, 183, 194, 93, 231, 50, 86, 250, 21, 63, 65,
195, 94, 226, 61, 71, 201, 64, 192, 91, 237, 44, 116, 156, 191, 218, 117,
159, 186, 213, 100, 172, 239, 42, 126, 130, 157, 188, 223, 122, 142, 137, 128,
155, 182, 193, 88, 232, 35, 101, 175, 234, 37, 111, 177, 200, 67, 197, 84,
252, 31, 33, 99, 165, 244, 7, 9, 27, 45, 119, 153, 176, 203, 70, 202,
69, 207, 74, 222, 121, 139, 134, 145, 168, 227, 62, 66, 198, 81, 243, 14,
18, 54, 90, 238, 41, 123, 141, 140, 143, 138, 133, 148, 167, 242, 13, 23,
57, 75, 221, 124, 132, 151, 162, 253, 28, 36, 108, 180, 199, 82, 246, 1,
};
static MV_U8 S[256] = {
99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118,
202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192,
183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21,
4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117,
9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132,
83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207,
208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168,
81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210,
205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115,
96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219,
224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121,
231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8,
186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138,
112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158,
225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22,
};
static MV_U8 Si[256] = {
82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, 251,
124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233, 203,
84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195, 78,
8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209, 37,
114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, 204, 93, 101, 182, 146,
108, 112, 72, 80, 253, 237, 185, 218, 94, 21, 70, 87, 167, 141, 157, 132,
144, 216, 171, 0, 140, 188, 211, 10, 247, 228, 88, 5, 184, 179, 69, 6,
208, 44, 30, 143, 202, 63, 15, 2, 193, 175, 189, 3, 1, 19, 138, 107,
58, 145, 17, 65, 79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230, 115,
150, 172, 116, 34, 231, 173, 53, 133, 226, 249, 55, 232, 28, 117, 223, 110,
71, 241, 26, 113, 29, 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27,
252, 86, 62, 75, 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244,
31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95,
96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239,
160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97,
23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125,
};
/*
static MV_U8 iG[4][4] = {
{0x0e, 0x09, 0x0d, 0x0b},
{0x0b, 0x0e, 0x09, 0x0d},
{0x0d, 0x0b, 0x0e, 0x09},
{0x09, 0x0d, 0x0b, 0x0e},
};
*/
static MV_U32 rcon[30] = {
0x01,0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, };

7
sdk/lib/cryptlib/mvOs.h Normal file
View file

@ -0,0 +1,7 @@
#pragma once
typedef unsigned char MV_U8;
typedef unsigned short MV_U16;
typedef unsigned long MV_U32;

67
sdk/lib/cryptlib/rc4.c Normal file
View file

@ -0,0 +1,67 @@
/*
* Copyright 2006 Mike McCormack
*
* based on arc4.cpp - written and placed in the public domain by Wei Dai
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* http://cryptopp.sourceforge.net/docs/ref521/arc4_8cpp-source.html */
#include "rc4.h"
void rc4_init(RC4_CONTEXT *a4i, const unsigned char *key, unsigned int keyLen)
{
unsigned int keyIndex = 0, stateIndex = 0;
unsigned int i, a;
a4i->x = a4i->y = 0;
for (i=0; i<256; i++)
a4i->state[i] = i;
for (i=0; i<256; i++)
{
a = a4i->state[i];
stateIndex += key[keyIndex] + a;
stateIndex &= 0xff;
a4i->state[i] = a4i->state[stateIndex];
a4i->state[stateIndex] = a;
if (++keyIndex >= keyLen)
keyIndex = 0;
}
}
void rc4_crypt(RC4_CONTEXT *a4i, unsigned char *inoutString, unsigned int length)
{
unsigned char *const s=a4i->state;
unsigned int x = a4i->x;
unsigned int y = a4i->y;
unsigned int a, b;
while(length--)
{
x = (x+1) & 0xff;
a = s[x];
y = (y+a) & 0xff;
b = s[y];
s[x] = b;
s[y] = a;
*inoutString++ ^= s[(a+b) & 0xff];
}
a4i->x = x;
a4i->y = y;
}

13
sdk/lib/cryptlib/rc4.h Normal file
View file

@ -0,0 +1,13 @@
#pragma once
typedef struct _RC4_CONTEXT
{
unsigned char state[256];
unsigned char x, y;
} RC4_CONTEXT;
void rc4_init(RC4_CONTEXT *a4i, const unsigned char *key, unsigned int keyLen);
void rc4_crypt(RC4_CONTEXT *a4i, unsigned char *inoutString, unsigned int length);

198
sdk/lib/cryptlib/sha1.c Normal file
View file

@ -0,0 +1,198 @@
/*
* Copyright 2004 Filip Navara
* Based on public domain SHA code by Steve Reid <steve@edmweb.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "sha1.h"
/* SHA1 Helper Macros */
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
/* FIXME: This definition of DWORD2BE is little endian specific! */
#define DWORD2BE(x) (((x) >> 24) & 0xff) | (((x) >> 8) & 0xff00) | (((x) << 8) & 0xff0000) | (((x) << 24) & 0xff000000);
/* FIXME: This definition of blk0 is little endian specific! */
#define blk0(i) (Block[i] = (rol(Block[i],24)&0xFF00FF00)|(rol(Block[i],8)&0x00FF00FF))
#define blk1(i) (Block[i&15] = rol(Block[(i+13)&15]^Block[(i+8)&15]^Block[(i+2)&15]^Block[i&15],1))
#define f1(x,y,z) (z^(x&(y^z)))
#define f2(x,y,z) (x^y^z)
#define f3(x,y,z) ((x&y)|(z&(x|y)))
#define f4(x,y,z) (x^y^z)
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
#define R0(v,w,x,y,z,i) z+=f1(w,x,y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R1(v,w,x,y,z,i) z+=f1(w,x,y)+blk1(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R2(v,w,x,y,z,i) z+=f2(w,x,y)+blk1(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
#define R3(v,w,x,y,z,i) z+=f3(w,x,y)+blk1(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
#define R4(v,w,x,y,z,i) z+=f4(w,x,y)+blk1(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
/* Hash a single 512-bit block. This is the core of the algorithm. */
static void SHA1Transform(ULONG State[5], UCHAR Buffer[64])
{
ULONG a, b, c, d, e;
ULONG *Block;
Block = (ULONG*)Buffer;
/* Copy Context->State[] to working variables */
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 variables back into Context->State[] */
State[0] += a;
State[1] += b;
State[2] += c;
State[3] += d;
State[4] += e;
/* Wipe variables */
a = b = c = d = e = 0;
}
/******************************************************************************
* A_SHAInit [ADVAPI32.@]
*
* Initialize a SHA context structure.
*
* PARAMS
* Context [O] SHA context
*
* RETURNS
* Nothing
*/
VOID NTAPI
A_SHAInit(PSHA_CTX 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] =
Context->Count[1] = 0;
}
/******************************************************************************
* A_SHAUpdate [ADVAPI32.@]
*
* Update a SHA context with a hashed data from supplied buffer.
*
* PARAMS
* Context [O] SHA context
* Buffer [I] hashed data
* BufferSize [I] hashed data size
*
* RETURNS
* Nothing
*/
VOID NTAPI
A_SHAUpdate(PSHA_CTX Context, const unsigned char *Buffer, ULONG BufferSize)
{
ULONG BufferContentSize;
BufferContentSize = Context->Count[1] & 63;
Context->Count[1] += BufferSize;
if (Context->Count[1] < BufferSize)
Context->Count[0]++;
Context->Count[0] += (BufferSize >> 29);
if (BufferContentSize + BufferSize < 64)
{
memcpy(&Context->Buffer[BufferContentSize], Buffer,
BufferSize);
}
else
{
while (BufferContentSize + BufferSize >= 64)
{
memcpy(Context->Buffer + BufferContentSize, Buffer,
64 - BufferContentSize);
Buffer += 64 - BufferContentSize;
BufferSize -= 64 - BufferContentSize;
SHA1Transform(Context->State, Context->Buffer);
BufferContentSize = 0;
}
memcpy(Context->Buffer + BufferContentSize, Buffer, BufferSize);
}
}
/******************************************************************************
* A_SHAFinal [ADVAPI32.@]
*
* Finalize SHA context and return the resulting hash.
*
* PARAMS
* Context [I/O] SHA context
* Result [O] resulting hash
*
* RETURNS
* Nothing
*/
VOID NTAPI
A_SHAFinal(PSHA_CTX Context, PULONG Result)
{
INT Pad, Index;
UCHAR Buffer[72];
ULONG *Count;
ULONG BufferContentSize, LengthHi, LengthLo;
BufferContentSize = Context->Count[1] & 63;
if (BufferContentSize >= 56)
Pad = 56 + 64 - BufferContentSize;
else
Pad = 56 - BufferContentSize;
LengthHi = (Context->Count[0] << 3) | (Context->Count[1] >> (32 - 3));
LengthLo = (Context->Count[1] << 3);
memset(Buffer + 1, 0, Pad - 1);
Buffer[0] = 0x80;
Count = (ULONG*)(Buffer + Pad);
Count[0] = DWORD2BE(LengthHi);
Count[1] = DWORD2BE(LengthLo);
A_SHAUpdate(Context, Buffer, Pad + 8);
for (Index = 0; Index < 5; Index++)
Result[Index] = DWORD2BE(Context->State[Index]);
A_SHAInit(Context);
}

32
sdk/lib/cryptlib/sha1.h Normal file
View file

@ -0,0 +1,32 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <ntdef.h>
/* SHA Context Structure Declaration */
typedef struct
{
ULONG Unknown[6];
ULONG State[5];
ULONG Count[2];
UCHAR Buffer[64];
} SHA_CTX, *PSHA_CTX;
VOID NTAPI
A_SHAInit(PSHA_CTX Context);
VOID NTAPI
A_SHAUpdate(PSHA_CTX Context, const unsigned char *Buffer, ULONG BufferSize);
VOID NTAPI
A_SHAFinal(PSHA_CTX Context, PULONG Result);
#ifdef __cplusplus
}
#endif

469
sdk/lib/cryptlib/tomcrypt.h Normal file
View file

@ -0,0 +1,469 @@
/*
* dlls/rsaenh/tomcrypt.h
* Function prototypes, type definitions and constant definitions
* for LibTomCrypt code.
*
* Copyright 2004 Michael Jung
* Based on public domain code by Tom St Denis (tomstdenis@iahu.ca)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* This file contains code from the LibTomCrypt cryptographic
* library written by Tom St Denis (tomstdenis@iahu.ca). LibTomCrypt
* is in the public domain. The code in this file is tailored to
* special requirements. Take a look at http://libtomcrypt.org for the
* original version.
*/
#ifndef __WINE_TOMCRYPT_H_
#define __WINE_TOMCRYPT_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <limits.h>
#include <basetsd.h>
/* error codes [will be expanded in future releases] */
enum {
CRYPT_OK=0, /* Result OK */
CRYPT_ERROR, /* Generic Error */
CRYPT_NOP, /* Not a failure but no operation was performed */
CRYPT_INVALID_KEYSIZE, /* Invalid key size given */
CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */
CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */
CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */
CRYPT_INVALID_PACKET, /* Invalid input packet given */
CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */
CRYPT_INVALID_CIPHER, /* Invalid cipher specified */
CRYPT_INVALID_HASH, /* Invalid hash specified */
CRYPT_INVALID_PRNG, /* Invalid PRNG specified */
CRYPT_MEM, /* Out of memory */
CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */
CRYPT_INVALID_ARG, /* Generic invalid argument */
CRYPT_FILE_NOTFOUND, /* File Not Found */
CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */
CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */
CRYPT_PK_DUP, /* Duplicate key already in key ring */
CRYPT_PK_NOT_FOUND, /* Key not found in keyring */
CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */
CRYPT_INVALID_PRIME_SIZE/* Invalid size of prime requested */
};
#define CONST64(a,b) ((((ULONG64)(a)) << 32) | (b))
typedef ULONG64 ulong64;
/* this is the "32-bit at least" data type
* Re-define it to suit your platform but it must be at least 32-bits
*/
typedef ULONG32 ulong32;
/* ---- HELPER MACROS ---- */
#define STORE32H(x, y) \
{ (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
(y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
#define LOAD32H(x, y) \
{ x = ((unsigned long)((y)[0] & 255)<<24) | \
((unsigned long)((y)[1] & 255)<<16) | \
((unsigned long)((y)[2] & 255)<<8) | \
((unsigned long)((y)[3] & 255)); }
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC)
static inline unsigned ROR(unsigned word, int i)
{
__asm__("rorl %%cl,%0"
:"=r" (word)
:"0" (word),"c" (i));
return word;
}
#else
/* rotates the hard way */
#define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
#endif
#undef MIN
#define MIN(x, y) ( ((x)<(y))?(x):(y) )
#define byte(x, n) (((x) >> (8 * (n))) & 255)
typedef struct tag_rc2_key {
unsigned xkey[64];
} rc2_key;
typedef struct tag_des_key {
ulong32 ek[32], dk[32];
} des_key;
typedef struct tag_des3_key {
ulong32 ek[3][32], dk[3][32];
} des3_key;
typedef struct tag_aes_key {
ulong32 eK[64], dK[64];
int Nr;
} aes_key;
int rc2_setup(const unsigned char *key, int keylen, int bits, int num_rounds, rc2_key *skey);
void rc2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, rc2_key *key);
void rc2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, rc2_key *key);
int des_setup(const unsigned char *key, int keylen, int num_rounds, des_key *skey);
void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const des_key *key);
void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const des_key *key);
int des3_setup(const unsigned char *key, int keylen, int num_rounds, des3_key *skey);
void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const des3_key *key);
void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const des3_key *key);
int aes_setup(const unsigned char *key, int keylen, int rounds, aes_key *skey);
void aes_ecb_encrypt(const unsigned char *pt, unsigned char *ct, aes_key *skey);
void aes_ecb_decrypt(const unsigned char *ct, unsigned char *pt, aes_key *skey);
typedef struct tag_md2_state {
unsigned char chksum[16], X[48], buf[16];
unsigned long curlen;
} md2_state;
int md2_init(md2_state * md);
int md2_process(md2_state * md, const unsigned char *buf, unsigned long len);
int md2_done(md2_state * md, unsigned char *hash);
struct rc4_prng {
int x, y;
unsigned char buf[256];
};
typedef union Prng_state {
struct rc4_prng rc4;
} prng_state;
int rc4_start(prng_state *prng);
int rc4_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
int rc4_ready(prng_state *prng);
unsigned long rc4_read(unsigned char *buf, unsigned long len, prng_state *prng);
/* some default configurations.
*
* A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
* A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
*
* At the very least a mp_digit must be able to hold 7 bits
* [any size beyond that is ok provided it doesn't overflow the data type]
*/
typedef unsigned long mp_digit;
typedef ulong64 mp_word;
#define DIGIT_BIT 28
#define MP_DIGIT_BIT DIGIT_BIT
#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
#define MP_DIGIT_MAX MP_MASK
/* equalities */
#define MP_LT -1 /* less than */
#define MP_EQ 0 /* equal to */
#define MP_GT 1 /* greater than */
#define MP_ZPOS 0 /* positive integer */
#define MP_NEG 1 /* negative */
#define MP_OKAY 0 /* ok result */
#define MP_MEM -2 /* out of mem */
#define MP_VAL -3 /* invalid input */
#define MP_RANGE MP_VAL
#define MP_YES 1 /* yes response */
#define MP_NO 0 /* no response */
/* Primality generation flags */
#define LTM_PRIME_BBS 0x0001 /* BBS style prime */
#define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
#define LTM_PRIME_2MSB_OFF 0x0004 /* force 2nd MSB to 0 */
#define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
typedef int mp_err;
/* define this to use lower memory usage routines (exptmods mostly) */
/* #define MP_LOW_MEM */
#define MP_PREC 64 /* default digits of precision */
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
#define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
/* the infamous mp_int structure */
typedef struct {
int used, alloc, sign;
mp_digit *dp;
} mp_int;
/* callback for mp_prime_random, should fill dst with random bytes and return how many read [up to len] */
typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
#define DIGIT(m,k) ((m)->dp[(k)])
/* error code to char* string */
char *mp_error_to_string(int code);
/* init a null terminated series of arguments */
int mp_init_multi(mp_int *mp, ...);
/* clear a null terminated series of arguments */
void mp_clear_multi(mp_int *mp, ...);
/* shrink ram required for a bignum */
int mp_shrink(mp_int *a);
/* ---> Basic Manipulations <--- */
#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
#define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
#define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
/* set a 32-bit const */
int mp_set_int(mp_int *a, unsigned long b);
/* get a 32-bit value */
unsigned long mp_get_int(const mp_int * a);
/* initialize and set a digit */
int mp_init_set (mp_int * a, mp_digit b);
/* initialize and set 32-bit value */
int mp_init_set_int (mp_int * a, unsigned long b);
/* copy, b = a */
int mp_copy(const mp_int *a, mp_int *b);
/* inits and copies, a = b */
int mp_init_copy(mp_int *a, const mp_int *b);
/* ---> digit manipulation <--- */
/* I Love Earth! */
/* makes a pseudo-random int of a given size */
int mp_rand(mp_int *a, int digits);
/* ---> binary operations <--- */
/* c = a XOR b */
int mp_xor(mp_int *a, mp_int *b, mp_int *c);
/* c = a OR b */
int mp_or(mp_int *a, mp_int *b, mp_int *c);
/* c = a AND b */
int mp_and(mp_int *a, mp_int *b, mp_int *c);
/* ---> Basic arithmetic <--- */
/* b = -a */
int mp_neg(mp_int *a, mp_int *b);
/* compare a to b */
int mp_cmp(const mp_int *a, const mp_int *b);
/* c = a + b */
int mp_add(mp_int *a, mp_int *b, mp_int *c);
/* c = a - b */
int mp_sub(mp_int *a, mp_int *b, mp_int *c);
/* c = a * b */
int mp_mul(const mp_int *a, const mp_int *b, mp_int *c);
/* c = a mod b, 0 <= c < b */
int mp_mod(const mp_int *a, mp_int *b, mp_int *c);
/* ---> single digit functions <--- */
/* compare against a single digit */
int mp_cmp_d(const mp_int *a, mp_digit b);
/* c = a - b */
int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
/* a/3 => 3c + d == a */
int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
/* c = a**b */
int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
/* ---> number theory <--- */
/* d = a + b (mod c) */
int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
/* d = a - b (mod c) */
int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
/* d = a * b (mod c) */
int mp_mulmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
/* c = 1/a (mod b) */
int mp_invmod(const mp_int *a, mp_int *b, mp_int *c);
/* c = (a, b) */
int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c);
/* produces value such that U1*a + U2*b = U3 */
int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
/* c = [a, b] or (a*b)/(a, b) */
int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c);
/* finds one of the b'th root of a, such that |c|**b <= |a|
*
* returns error if a < 0 and b is even
*/
int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
/* special sqrt algo */
int mp_sqrt(mp_int *arg, mp_int *ret);
/* is number a square? */
int mp_is_square(mp_int *arg, int *ret);
/* computes the jacobi c = (a | n) (or Legendre if b is prime) */
int mp_jacobi(mp_int *a, mp_int *n, int *c);
/* returns 1 if a is a valid DR modulus */
int mp_dr_is_modulus(mp_int *a);
/* returns true if a can be reduced with mp_reduce_2k */
int mp_reduce_is_2k(mp_int *a);
/* d = a**b (mod c) */
int mp_exptmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
/* ---> Primes <--- */
/* number of primes */
#define PRIME_SIZE 256
/* performs one Fermat test of "a" using base "b".
* Sets result to 0 if composite or 1 if probable prime
*/
int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
/* This gives [for a given bit size] the number of trials required
* such that Miller-Rabin gives a prob of failure lower than 2^-96
*/
int mp_prime_rabin_miller_trials(int size);
/* finds the next prime after the number "a" using "t" trials
* of Miller-Rabin.
*
* bbs_style = 1 means the prime must be congruent to 3 mod 4
*/
int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
/* makes a truly random prime of a given size (bytes),
* call with bbs = 1 if you want it to be congruent to 3 mod 4
*
* You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
* have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
* so it can be NULL
*
* The prime generated will be larger than 2^(8*size).
*/
#define mp_prime_random(a, t, size, bbs, cb, dat) mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
/* makes a truly random prime of a given size (bits),
*
* Flags are as follows:
*
* LTM_PRIME_BBS - make prime congruent to 3 mod 4
* LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
* LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
* LTM_PRIME_2MSB_ON - make the 2nd highest bit one
*
* You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
* have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
* so it can be NULL
*
*/
int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
/* ---> radix conversion <--- */
int mp_count_bits(const mp_int *a);
int mp_unsigned_bin_size(const mp_int *a);
int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c);
int mp_to_unsigned_bin(const mp_int *a, unsigned char *b);
int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
int mp_to_signed_bin(mp_int *a, unsigned char *b);
int mp_read_radix(mp_int *a, char *str, int radix);
int mp_toradix(mp_int *a, char *str, int radix);
int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
int mp_radix_size(mp_int *a, int radix, int *size);
int mp_fread(mp_int *a, int radix, FILE *stream);
int mp_fwrite(mp_int *a, int radix, FILE *stream);
#define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
#define mp_raw_size(mp) mp_signed_bin_size(mp)
#define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
#define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
#define mp_mag_size(mp) mp_unsigned_bin_size(mp)
#define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
#define mp_tobinary(M, S) mp_toradix((M), (S), 2)
#define mp_tooctal(M, S) mp_toradix((M), (S), 8)
#define mp_todecimal(M, S) mp_toradix((M), (S), 10)
#define mp_tohex(M, S) mp_toradix((M), (S), 16)
extern const char *mp_s_rmap;
#define PK_PRIVATE 0 /* PK private keys */
#define PK_PUBLIC 1 /* PK public keys */
/* Min and Max RSA key sizes (in bits) */
#define MIN_RSA_SIZE 384
#define MAX_RSA_SIZE 16384
typedef struct Rsa_key {
int type;
mp_int e, d, N, p, q, qP, dP, dQ;
} rsa_key;
int rsa_make_key(int size, long e, rsa_key *key);
int rsa_exptmod(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, int which,
rsa_key *key);
void rsa_free(rsa_key *key);
#endif /* __WINE_TOMCRYPT_H_ */

26
sdk/lib/cryptlib/util.c Normal file
View file

@ -0,0 +1,26 @@
#if !defined(_MSC_VER) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#include <stdlib.h>
#include <intrin.h>
void
byteReverse(unsigned char *buf, unsigned longs)
{
unsigned int t;
do
{
#if 0
t = (unsigned int)((unsigned)buf[3] << 8 | buf[2]) << 16 |
((unsigned)buf[1] << 8 | buf[0]);
#else
t = _byteswap_ulong(*(unsigned int *)buf);
#endif
*(unsigned int *)buf = t;
buf += 4;
} while (--longs);
}
#endif // !defined(_MSC_VER) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)

9
sdk/lib/cryptlib/util.h Normal file
View file

@ -0,0 +1,9 @@
#pragma once
#if defined(_MSC_VER) || (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#define byteReverse(buf, long)((void)(buf, long))
#else
void
byteReverse(unsigned char *buf, unsigned longs);
#endif