diff --git a/reactos/dll/win32/advapi32/CMakeLists.txt b/reactos/dll/win32/advapi32/CMakeLists.txt index 50e74231036..db953400474 100644 --- a/reactos/dll/win32/advapi32/CMakeLists.txt +++ b/reactos/dll/win32/advapi32/CMakeLists.txt @@ -8,6 +8,7 @@ add_definitions(-D_WIN32_WINNT=0x600) include_directories( ${REACTOS_SOURCE_DIR}/include/reactos/idl + ${REACTOS_SOURCE_DIR}/lib/cryptlib ${CMAKE_CURRENT_BINARY_DIR}) add_rpc_files(client @@ -17,12 +18,8 @@ add_rpc_files(client list(APPEND SOURCE crypt/crypt.c - crypt/crypt_arc4.c crypt/crypt_des.c crypt/crypt_lmhash.c - crypt/crypt_md4.c - crypt/crypt_md5.c - crypt/crypt_sha.c misc/dllmain.c misc/efs.c misc/hwprofiles.c @@ -56,7 +53,7 @@ list(APPEND SOURCE add_library(advapi32 SHARED ${SOURCE}) set_module_type(advapi32 win32dll UNICODE) -target_link_libraries(advapi32 wine ${PSEH_LIB}) +target_link_libraries(advapi32 cryptlib wine ${PSEH_LIB}) add_delay_importlibs(advapi32 rpcrt4) add_importlibs(advapi32 secur32 msvcrt kernel32 ntdll) diff --git a/reactos/dll/win32/advapi32/crypt/crypt.c b/reactos/dll/win32/advapi32/crypt/crypt.c index 81b37c891e5..5d6266b3003 100644 --- a/reactos/dll/win32/advapi32/crypt/crypt.c +++ b/reactos/dll/win32/advapi32/crypt/crypt.c @@ -27,23 +27,6 @@ #include WINE_DEFAULT_DEBUG_CHANNEL(crypt); - -/* - * Note: this code is harmless on little-endian machines. - */ -VOID byteReverse(unsigned char *buf, unsigned longs) -{ - unsigned int t; - - do - { - t = (unsigned int)((unsigned)buf[3] << 8 | buf[2]) << 16 | - ((unsigned)buf[1] << 8 | buf[0]); - *(unsigned int *)buf = t; - buf += 4; - } while (--longs); -} - static HWND crypt_hWindow; #define CRYPT_Alloc(size) (LocalAlloc(LMEM_ZEROINIT, size)) @@ -323,7 +306,7 @@ static void CRYPT_CreateMachineGuid(void) * dwProvType [I] Crypto provider type to get a handle. * dwFlags [I] flags for the operation * - * RETURNS + * RETURNS * TRUE on success, FALSE on failure. */ BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer, @@ -347,7 +330,7 @@ BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer, SetLastError(NTE_BAD_PROV_TYPE); return FALSE; } - + if (!phProv) { SetLastError(ERROR_INVALID_PARAMETER); @@ -583,7 +566,7 @@ BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer, */ BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags) { - PCRYPTPROV pProv = (PCRYPTPROV)hProv; + PCRYPTPROV pProv = (PCRYPTPROV)hProv; TRACE("(0x%lx, %p, %08x)\n", hProv, pdwReserved, dwFlags); @@ -636,7 +619,7 @@ BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, DWORD dwFlags) } pProv->refcount--; - if (pProv->refcount <= 0) + if (pProv->refcount <= 0) { ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags); pProv->dwMagic = 0; @@ -1144,10 +1127,10 @@ BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved, { DWORD numkeys; WCHAR *provNameW; - + RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName, NULL, NULL, NULL, NULL, NULL, NULL); - + if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR)))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); @@ -1169,7 +1152,7 @@ BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved, DWORD size = sizeof(DWORD); DWORD result; HKEY subkey; - + result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR)); if (result) { @@ -1313,7 +1296,7 @@ BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved, *pdwProvType += (*(--ch) - '0') * 10; *pdwProvType += (*(--ch) - '0') * 100; CRYPT_Free(keyname); - + result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName); if (result) { @@ -1366,7 +1349,7 @@ BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved, /****************************************************************************** * CryptExportKey (ADVAPI32.@) - * + * * Exports a cryptographic key from a CSP. * * PARAMS @@ -1507,8 +1490,8 @@ BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved, return FALSE; } CRYPT_Free(keyname); - - result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName); + + result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName); RegCloseKey(hKey); if (result) @@ -1517,10 +1500,10 @@ BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved, SetLastError(NTE_PROV_TYPE_ENTRY_BAD); else SetLastError(result); - + return FALSE; } - + return TRUE; } @@ -1768,7 +1751,7 @@ BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen * * Compute the cryptographic hash of a session key object. * - * PARAMS + * PARAMS * hHash [I] Handle to the hash object. * hKey [I] Handle to the key to be hashed. * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN. @@ -1883,7 +1866,7 @@ BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescript PCRYPTHASH hash = (PCRYPTHASH)hHash; PCRYPTPROV prov; - TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n", + TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n", hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen); if (!hash) @@ -1914,7 +1897,7 @@ BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescripti LPWSTR wsDescription; BOOL result; - TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n", + TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n", hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen); CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1); @@ -2075,7 +2058,7 @@ BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *p return FALSE; } CRYPT_Free(keyname); - + if (dwFlags & CRYPT_DELETE_DEFAULT) { RegDeleteValueW(hTypeKey, nameW); @@ -2097,7 +2080,7 @@ BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *p return FALSE; } CRYPT_Free(keyname); - + if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName, (strlenW(pszProvName) + 1)*sizeof(WCHAR))) { @@ -2105,7 +2088,7 @@ BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *p RegCloseKey(hProvKey); return FALSE; } - + RegCloseKey(hProvKey); } RegCloseKey(hTypeKey); @@ -2201,7 +2184,7 @@ BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, CONST BYTE *pbDa * RETURNS * Success: TRUE * Failure: FALSE - * + * * NOTES * Because of security flaws sDescription should not be used and should thus be * NULL. It is supported only for compatibility with Microsoft's Cryptographic @@ -2224,7 +2207,7 @@ BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, CONST BYTE *pbSignature, DW SetLastError(ERROR_INVALID_PARAMETER); return FALSE; } - + prov = hash->pProvider; return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen, key->hPrivate, sDescription, dwFlags); @@ -2316,8 +2299,8 @@ BOOLEAN WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen) FIXME("couldn't open /dev/urandom\n"); SetLastError(NTE_FAIL); return FALSE; -} - +} + /* These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory, in crypt32.dll. @@ -2332,10 +2315,10 @@ BOOLEAN WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen) * memory [I/O] Pointer to memory to encrypt. * length [I] Length of region to encrypt in bytes. * flags [I] Control whether other processes are able to decrypt the memory. - * RTL_ENCRYPT_OPTION_SAME_PROCESS - * RTL_ENCRYPT_OPTION_CROSS_PROCESS + * RTL_ENCRYPT_OPTION_SAME_PROCESS + * RTL_ENCRYPT_OPTION_CROSS_PROCESS * RTL_ENCRYPT_OPTION_SAME_LOGON - * + * * RETURNS * Success: STATUS_SUCCESS * Failure: NTSTATUS error code diff --git a/reactos/dll/win32/advapi32/crypt/crypt.h b/reactos/dll/win32/advapi32/crypt/crypt.h index a3e4f0a14ca..937e30b6e69 100644 --- a/reactos/dll/win32/advapi32/crypt/crypt.h +++ b/reactos/dll/win32/advapi32/crypt/crypt.h @@ -91,30 +91,12 @@ extern unsigned char *CRYPT_DEShash( unsigned char *dst, const unsigned char *ke extern unsigned char *CRYPT_DESunhash( unsigned char *dst, const unsigned char *key, const unsigned char *src ) DECLSPEC_HIDDEN; -void byteReverse(unsigned char *buf, unsigned longs); struct ustring { DWORD Length; DWORD MaximumLength; unsigned char *Buffer; }; -typedef struct { - unsigned int buf[4]; - unsigned int i[2]; - unsigned char in[64]; - unsigned char digest[16]; -} MD4_CTX; - -typedef struct tag_arc4_info { - unsigned char state[256]; - unsigned char x, y; -} arc4_info; - -VOID WINAPI MD4Init( MD4_CTX *ctx ); -VOID WINAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len ); -VOID WINAPI MD4Final(MD4_CTX *ctx); -void arc4_init(arc4_info *a4i, const BYTE *key, unsigned int keyLen); -void arc4_ProcessString(arc4_info *a4i, BYTE *inoutString, unsigned int length); NTSTATUS WINAPI SystemFunction032(struct ustring *data, const struct ustring *key); #endif /* __WINE_CRYPT_H_ */ diff --git a/reactos/dll/win32/advapi32/misc/sysfunc.c b/reactos/dll/win32/advapi32/misc/sysfunc.c index d105b64ea54..b8fc700b076 100644 --- a/reactos/dll/win32/advapi32/misc/sysfunc.c +++ b/reactos/dll/win32/advapi32/misc/sysfunc.c @@ -12,6 +12,9 @@ */ #include +#include +#include +#include static const unsigned char CRYPT_LMhash_Magic[8] = { 'K', 'G', 'S', '!', '@', '#', '$', '%' }; @@ -502,10 +505,10 @@ WINAPI SystemFunction030(LPCVOID b1, LPCVOID b2) NTSTATUS WINAPI SystemFunction032(struct ustring *data, const struct ustring *key) { - arc4_info a4i; + RC4_CONTEXT a4i; - arc4_init(&a4i, key->Buffer, key->Length); - arc4_ProcessString(&a4i, data->Buffer, data->Length); + rc4_init(&a4i, key->Buffer, key->Length); + rc4_crypt(&a4i, data->Buffer, data->Length); return STATUS_SUCCESS; } @@ -626,10 +629,10 @@ SystemFunction036(PVOID pbBuffer, ULONG dwLen) * memory [I/O] Pointer to memory to encrypt. * length [I] Length of region to encrypt in bytes. * flags [I] Control whether other processes are able to decrypt the memory. - * RTL_ENCRYPT_OPTION_SAME_PROCESS - * RTL_ENCRYPT_OPTION_CROSS_PROCESS + * RTL_ENCRYPT_OPTION_SAME_PROCESS + * RTL_ENCRYPT_OPTION_CROSS_PROCESS * RTL_ENCRYPT_OPTION_SAME_LOGON - * + * * RETURNS * Success: STATUS_SUCCESS * Failure: NTSTATUS error code diff --git a/reactos/lib/CMakeLists.txt b/reactos/lib/CMakeLists.txt index 49944594571..8918d671094 100644 --- a/reactos/lib/CMakeLists.txt +++ b/reactos/lib/CMakeLists.txt @@ -7,6 +7,7 @@ if(CMAKE_CROSSCOMPILING) add_subdirectory(3rdparty) add_subdirectory(atl) add_subdirectory(cportlib) +add_subdirectory(cryptlib) #add_subdirectory(dnslib) Nothing links to this lib. add_subdirectory(drivers) add_subdirectory(epsapi) diff --git a/reactos/lib/cryptlib/CMakeLists.txt b/reactos/lib/cryptlib/CMakeLists.txt new file mode 100644 index 00000000000..015d13bebf4 --- /dev/null +++ b/reactos/lib/cryptlib/CMakeLists.txt @@ -0,0 +1,9 @@ + +add_library(cryptlib + md4.c + md5.c + mvAesAlg.c + rc4.c + sha1.c + util.c +) diff --git a/reactos/lib/cryptlib/README.txt b/reactos/lib/cryptlib/README.txt new file mode 100644 index 00000000000..631e8761237 --- /dev/null +++ b/reactos/lib/cryptlib/README.txt @@ -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 diff --git a/reactos/dll/win32/advapi32/crypt/crypt_md4.c b/reactos/lib/cryptlib/md4.c similarity index 80% rename from reactos/dll/win32/advapi32/crypt/crypt_md4.c rename to reactos/lib/cryptlib/md4.c index 9726a425c3d..a553a20eb4e 100644 --- a/reactos/dll/win32/advapi32/crypt/crypt_md4.c +++ b/reactos/lib/cryptlib/md4.c @@ -33,7 +33,8 @@ * will fill a supplied 16-byte array with the digest. */ -#include +#include "md4.h" +#include "util.h" static void MD4Transform( unsigned int buf[4], unsigned int const in[16] ); @@ -41,7 +42,7 @@ 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 WINAPI MD4Init( MD4_CTX *ctx ) +VOID NTAPI MD4Init( MD4_CTX *ctx ) { ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; @@ -55,7 +56,7 @@ VOID WINAPI MD4Init( MD4_CTX *ctx ) * Update context to reflect the concatenation of another buffer full * of bytes. */ -VOID WINAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len ) +VOID NTAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len ) { register unsigned int t; @@ -106,10 +107,10 @@ VOID WINAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len } /* - * Final wrapup - pad to 64-byte boundary with the bit pattern + * Final wrapup - pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ -VOID WINAPI MD4Final( MD4_CTX *ctx ) +VOID NTAPI MD4Final( MD4_CTX *ctx ) { unsigned int count; unsigned char *p; @@ -243,58 +244,3 @@ static void MD4Transform( unsigned int buf[4], const unsigned int in[16] ) buf[3] += d; } -#ifndef __REACTOS__ -/****************************************************************************** - * SystemFunction007 [ADVAPI32.@] - * - * MD4 hash a unicode string - * - * PARAMS - * string [I] the string to hash - * output [O] the md4 hash of the string (16 bytes) - * - * RETURNS - * Success: STATUS_SUCCESS - * Failure: STATUS_UNSUCCESSFUL - * - */ -NTSTATUS WINAPI SystemFunction007(const UNICODE_STRING *string, LPBYTE hash) -{ - MD4_CTX ctx; - - MD4Init( &ctx ); - MD4Update( &ctx, (const BYTE *)string->Buffer, string->Length ); - MD4Final( &ctx ); - memcpy( hash, ctx.digest, 0x10 ); - - return STATUS_SUCCESS; -} - -/****************************************************************************** - * SystemFunction010 [ADVAPI32.@] - * SystemFunction011 [ADVAPI32.@] - * - * MD4 hashes 16 bytes of data - * - * PARAMS - * unknown [] seems to have no effect on the output - * data [I] pointer to data to hash (16 bytes) - * output [O] the md4 hash of the data (16 bytes) - * - * RETURNS - * Success: STATUS_SUCCESS - * Failure: STATUS_UNSUCCESSFUL - * - */ -NTSTATUS WINAPI SystemFunction010(LPVOID unknown, const BYTE *data, LPBYTE hash) -{ - MD4_CTX ctx; - - MD4Init( &ctx ); - MD4Update( &ctx, data, 0x10 ); - MD4Final( &ctx ); - memcpy( hash, ctx.digest, 0x10 ); - - return STATUS_SUCCESS; -} -#endif /* !__REACTOS__ */ diff --git a/reactos/lib/cryptlib/md4.h b/reactos/lib/cryptlib/md4.h new file mode 100644 index 00000000000..9611ccabece --- /dev/null +++ b/reactos/lib/cryptlib/md4.h @@ -0,0 +1,19 @@ + +#pragma once + +#include + +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 ); + diff --git a/reactos/dll/win32/advapi32/crypt/crypt_md5.c b/reactos/lib/cryptlib/md5.c similarity index 95% rename from reactos/dll/win32/advapi32/crypt/crypt_md5.c rename to reactos/lib/cryptlib/md5.c index 52648f786a1..a847997172a 100644 --- a/reactos/dll/win32/advapi32/crypt/crypt_md5.c +++ b/reactos/lib/cryptlib/md5.c @@ -33,15 +33,8 @@ * will fill a supplied 16-byte array with the digest. */ -#include - -typedef struct -{ - unsigned int i[2]; - unsigned int buf[4]; - unsigned char in[64]; - unsigned char digest[16]; -} MD5_CTX; +#include "md5.h" +#include "util.h" static void MD5Transform( unsigned int buf[4], const unsigned int in[16] ); @@ -49,7 +42,7 @@ 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 WINAPI MD5Init( MD5_CTX *ctx ) +VOID NTAPI MD5Init( MD5_CTX *ctx ) { ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; @@ -63,7 +56,7 @@ VOID WINAPI MD5Init( MD5_CTX *ctx ) * Update context to reflect the concatenation of another buffer full * of bytes. */ -VOID WINAPI MD5Update( MD5_CTX *ctx, const unsigned char *buf, unsigned int len ) +VOID NTAPI MD5Update( MD5_CTX *ctx, const unsigned char *buf, unsigned int len ) { register unsigned int t; @@ -114,10 +107,10 @@ VOID WINAPI MD5Update( MD5_CTX *ctx, const unsigned char *buf, unsigned int len } /* - * Final wrapup - pad to 64-byte boundary with the bit pattern + * Final wrapup - pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ -VOID WINAPI MD5Final( MD5_CTX *ctx ) +VOID NTAPI MD5Final( MD5_CTX *ctx ) { unsigned int count; unsigned char *p; @@ -260,3 +253,4 @@ static void MD5Transform( unsigned int buf[4], const unsigned int in[16] ) buf[2] += c; buf[3] += d; } + diff --git a/reactos/lib/cryptlib/md5.h b/reactos/lib/cryptlib/md5.h new file mode 100644 index 00000000000..57ee7164769 --- /dev/null +++ b/reactos/lib/cryptlib/md5.h @@ -0,0 +1,19 @@ + +#pragma once + +#include + +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 ); + diff --git a/reactos/lib/cryptlib/mvAesAlg.c b/reactos/lib/cryptlib/mvAesAlg.c new file mode 100644 index 00000000000..f71201e381f --- /dev/null +++ b/reactos/lib/cryptlib/mvAesAlg.c @@ -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; +} + diff --git a/reactos/lib/cryptlib/mvAesAlg.h b/reactos/lib/cryptlib/mvAesAlg.h new file mode 100644 index 00000000000..ec81e403fac --- /dev/null +++ b/reactos/lib/cryptlib/mvAesAlg.h @@ -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 */ diff --git a/reactos/lib/cryptlib/mvAesBoxes.dat b/reactos/lib/cryptlib/mvAesBoxes.dat new file mode 100644 index 00000000000..4011b187b43 --- /dev/null +++ b/reactos/lib/cryptlib/mvAesBoxes.dat @@ -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, }; diff --git a/reactos/lib/cryptlib/mvOs.h b/reactos/lib/cryptlib/mvOs.h new file mode 100644 index 00000000000..da6a573f446 --- /dev/null +++ b/reactos/lib/cryptlib/mvOs.h @@ -0,0 +1,7 @@ + +#pragma once + +typedef unsigned char MV_U8; +typedef unsigned short MV_U16; +typedef unsigned long MV_U32; + diff --git a/reactos/dll/win32/advapi32/crypt/crypt_arc4.c b/reactos/lib/cryptlib/rc4.c similarity index 66% rename from reactos/dll/win32/advapi32/crypt/crypt_arc4.c rename to reactos/lib/cryptlib/rc4.c index f38783bf673..906dd18130c 100644 --- a/reactos/dll/win32/advapi32/crypt/crypt_arc4.c +++ b/reactos/lib/cryptlib/rc4.c @@ -20,9 +20,9 @@ /* http://cryptopp.sourceforge.net/docs/ref521/arc4_8cpp-source.html */ -#include +#include "rc4.h" -void arc4_init(arc4_info *a4i, const BYTE *key, unsigned int keyLen) +void rc4_init(RC4_CONTEXT *a4i, const unsigned char *key, unsigned int keyLen) { unsigned int keyIndex = 0, stateIndex = 0; unsigned int i, a; @@ -44,9 +44,9 @@ void arc4_init(arc4_info *a4i, const BYTE *key, unsigned int keyLen) } } -void arc4_ProcessString(arc4_info *a4i, BYTE *inoutString, unsigned int length) +void rc4_crypt(RC4_CONTEXT *a4i, unsigned char *inoutString, unsigned int length) { - BYTE *const s=a4i->state; + unsigned char *const s=a4i->state; unsigned int x = a4i->x; unsigned int y = a4i->y; unsigned int a, b; @@ -65,31 +65,3 @@ void arc4_ProcessString(arc4_info *a4i, BYTE *inoutString, unsigned int length) a4i->x = x; a4i->y = y; } - -#ifndef __REACTOS__ -/****************************************************************************** - * SystemFunction032 [ADVAPI32.@] - * - * Encrypts a string data using ARC4 - * - * PARAMS - * data [I/O] data to encrypt - * key [I] key data - * - * RETURNS - * Success: STATUS_SUCCESS - * Failure: STATUS_UNSUCCESSFUL - * - * NOTES - * see http://web.it.kth.se/~rom/ntsec.html#crypto-strongavail - */ -NTSTATUS WINAPI SystemFunction032(struct ustring *data, const struct ustring *key) -{ - arc4_info a4i; - - arc4_init(&a4i, key->Buffer, key->Length); - arc4_ProcessString(&a4i, data->Buffer, data->Length); - - return STATUS_SUCCESS; -} -#endif diff --git a/reactos/lib/cryptlib/rc4.h b/reactos/lib/cryptlib/rc4.h new file mode 100644 index 00000000000..964881cbdba --- /dev/null +++ b/reactos/lib/cryptlib/rc4.h @@ -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); + diff --git a/reactos/dll/win32/advapi32/crypt/crypt_sha.c b/reactos/lib/cryptlib/sha1.c similarity index 92% rename from reactos/dll/win32/advapi32/crypt/crypt_sha.c rename to reactos/lib/cryptlib/sha1.c index b677c426ea8..ba9cd7ba488 100644 --- a/reactos/dll/win32/advapi32/crypt/crypt_sha.c +++ b/reactos/lib/cryptlib/sha1.c @@ -17,16 +17,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -#include - -/* SHA Context Structure Declaration */ - -typedef struct { - ULONG Unknown[6]; - ULONG State[5]; - ULONG Count[2]; - UCHAR Buffer[64]; -} SHA_CTX, *PSHA_CTX; +#include "sha1.h" /* SHA1 Helper Macros */ @@ -107,7 +98,7 @@ static void SHA1Transform(ULONG State[5], UCHAR Buffer[64]) * RETURNS * Nothing */ -VOID WINAPI +VOID NTAPI A_SHAInit(PSHA_CTX Context) { /* SHA1 initialization constants */ @@ -133,8 +124,8 @@ A_SHAInit(PSHA_CTX Context) * RETURNS * Nothing */ -VOID WINAPI -A_SHAUpdate(PSHA_CTX Context, const unsigned char *Buffer, UINT BufferSize) +VOID NTAPI +A_SHAUpdate(PSHA_CTX Context, const unsigned char *Buffer, ULONG BufferSize) { ULONG BufferContentSize; @@ -146,21 +137,21 @@ A_SHAUpdate(PSHA_CTX Context, const unsigned char *Buffer, UINT BufferSize) if (BufferContentSize + BufferSize < 64) { - RtlCopyMemory(&Context->Buffer[BufferContentSize], Buffer, + memcpy(&Context->Buffer[BufferContentSize], Buffer, BufferSize); } else { while (BufferContentSize + BufferSize >= 64) { - RtlCopyMemory(Context->Buffer + BufferContentSize, Buffer, + memcpy(Context->Buffer + BufferContentSize, Buffer, 64 - BufferContentSize); Buffer += 64 - BufferContentSize; BufferSize -= 64 - BufferContentSize; SHA1Transform(Context->State, Context->Buffer); BufferContentSize = 0; } - RtlCopyMemory(Context->Buffer + BufferContentSize, Buffer, BufferSize); + memcpy(Context->Buffer + BufferContentSize, Buffer, BufferSize); } } @@ -176,7 +167,7 @@ A_SHAUpdate(PSHA_CTX Context, const unsigned char *Buffer, UINT BufferSize) * RETURNS * Nothing */ -VOID WINAPI +VOID NTAPI A_SHAFinal(PSHA_CTX Context, PULONG Result) { INT Pad, Index; @@ -193,7 +184,7 @@ A_SHAFinal(PSHA_CTX Context, PULONG Result) LengthHi = (Context->Count[0] << 3) | (Context->Count[1] >> (32 - 3)); LengthLo = (Context->Count[1] << 3); - RtlZeroMemory(Buffer + 1, Pad - 1); + memset(Buffer + 1, 0, Pad - 1); Buffer[0] = 0x80; Count = (ULONG*)(Buffer + Pad); Count[0] = DWORD2BE(LengthHi); diff --git a/reactos/lib/cryptlib/sha1.h b/reactos/lib/cryptlib/sha1.h new file mode 100644 index 00000000000..e9e094efd7f --- /dev/null +++ b/reactos/lib/cryptlib/sha1.h @@ -0,0 +1,24 @@ + +#pragma once + +#include + +/* 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); + + diff --git a/reactos/lib/cryptlib/util.c b/reactos/lib/cryptlib/util.c new file mode 100644 index 00000000000..9858ec78528 --- /dev/null +++ b/reactos/lib/cryptlib/util.c @@ -0,0 +1,26 @@ + + +#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#include +#include + +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 // (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) + diff --git a/reactos/lib/cryptlib/util.h b/reactos/lib/cryptlib/util.h new file mode 100644 index 00000000000..f2e2cf526af --- /dev/null +++ b/reactos/lib/cryptlib/util.h @@ -0,0 +1,10 @@ + +#pragma once + + +#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) +#define byteReverse(buf, long)((void)(buf, long)) +#else +void +byteReverse(unsigned char *buf, unsigned longs); +#endif