From 21c56b9074fbfdacba112a51ae3b54edfa243e59 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 15 Aug 2013 14:21:16 +0000 Subject: custom base64 & sha1 libs removed from MRA git-svn-id: http://svn.miranda-ng.org/main/trunk@5702 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/MRA/src/Sdk/Base64.h | 380 ----------------------------- protocols/MRA/src/Sdk/SHA1.h | 530 ++--------------------------------------- 2 files changed, 14 insertions(+), 896 deletions(-) delete mode 100644 protocols/MRA/src/Sdk/Base64.h (limited to 'protocols/MRA/src/Sdk') diff --git a/protocols/MRA/src/Sdk/Base64.h b/protocols/MRA/src/Sdk/Base64.h deleted file mode 100644 index d3cce03e9a..0000000000 --- a/protocols/MRA/src/Sdk/Base64.h +++ /dev/null @@ -1,380 +0,0 @@ -/* - * Copyright (c) 2003 Rozhuk Ivan - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - */ - - -#if !defined(AFX_BASE64__H__INCLUDED_) -#define AFX_BASE64__H__INCLUDED_ - - - -#if _MSC_VER > 1000 -#pragma once -#endif // _MSC_VER > 1000 - - - -//typedef unsigned char BYTE; -// -// BASE64 coding: -// 214 46 138 -// 11010100 00101110 10001010 -// ! ! ! -// ---------->>> convert 3 8bit to 4 6bit -// 110101 000010 111010 001010 -// 53 2 58 10 -// this numbers is offset in array coding below... -// - //01234567890123456789012345 -static BYTE *pbtCodingTableBASE64=(BYTE*)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; //52...63 -static BYTE btDeCodingTableBASE64[256]={64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,52,53,54,55,56,57,58,59,60,61,64,64,64,64,64,64,64,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64}; - - - - -__inline void BASE64CopyUnSafe(LPCVOID lpcSource,LPCVOID lpcDestination,size_t dwSize,size_t *pdwCopyedSize) -{// копируем только б64 символы - LPBYTE lpbSource,lpbDestination; - - lpbSource=(LPBYTE)lpcSource; - lpbDestination=(LPBYTE)lpcDestination; - while(dwSize--) - { - if ((*lpbSource)>32 && (*lpbSource)<128) (*lpbDestination++)=(*lpbSource); - lpbSource++; - } - if (pdwCopyedSize) (*pdwCopyedSize)=((size_t)lpbDestination-(size_t)lpcDestination); -} - - - -__inline DWORD BASE64EncodeUnSafe(LPCVOID lpcSource,size_t dwSourceSize,LPCVOID lpcDestination,size_t dwDestinationSize,size_t *pdwEncodedSize) -{// BASE64 кодирование - DWORD dwRetErrorCode; - size_t dwEncodedSize=((dwSourceSize*4+11)/12*4+1);//(((dwSourceSize+2)/3)*4); - - if ((dwDestinationSize>2]; // c1 - *(lpbtDestination++)=pbtCodingTableBASE64[(((*lpbtSource)<<4)&060) | ((lpbtSource[1]>>4)&017)]; // c2 - *(lpbtDestination++)=pbtCodingTableBASE64[((lpbtSource[1]<<2)&074) | ((lpbtSource[2]>>6)&03)]; // c3 - *(lpbtDestination++)=pbtCodingTableBASE64[lpbtSource[2] & 077]; // c4 - lpbtSource+=3; - } - - // If dwSourceSize was not a multiple of 3, then we have encoded too many characters. Adjust appropriately. - if(i==(dwSourceSize+1)) - {// There were only 2 bytes in that last group - lpbtDestination[-1]='='; - }else - if(i==(dwSourceSize+2)) - {// There was only 1 byte in that last group - lpbtDestination[-1]='='; - lpbtDestination[-2]='='; - } - - (*lpbtDestination)=0; - dwEncodedSize=(lpbtDestination-((LPBYTE)lpcDestination)); - -#else - __asm{ - push ebx // сохраняем регистр - push edi // сохраняем регистр - push esi // сохраняем регистр - - mov ebx,pbtCodingTableBASE64// ebx = адрес таблицы перекодировки - mov ecx,dwSourceSize // ecx = длинна входного буффера - mov edi,lpcDestination // edi = адрес выходного буффера - mov esi,lpcSource // esi = указатель на входной буффер - cld - jmp short read_loop_cmp - - //////////Code function/////////////////////////////////////////// - // функция кодировки - // eax - входящий буффер, используется только 3 байта //in buff, 3 byte used - // eax - выходящий буффер, используется 4 байта //out buff, 4 byte used - code_func: - // третья версия функции разложения, - // меняем местами два крайних байта - bswap eax - rol eax,6 - shl al,2 - ror eax,10 - shr ax,2 - shr al,2 - xlat - rol eax,8 - xlat - rol eax,8 - xlat - rol eax,8 - xlat - rol eax,8 - bswap eax// 188-235 - ret - ////////////////////////////////////////////////////////////////// - - /////////////Read & converting cycle////////////////////////////// - read_loop: - lodsd // читаем 4 байта - dec esi // используем только 3 - and eax,0x00FFFFFF - //==================================================== - // третья версия функции разложения, - // меняем местами два крайних байта - bswap eax - rol eax,6 - shl al,2 - ror eax,10 - shr ax,2 - shr al,2 - xlat - rol eax,8 - xlat - rol eax,8 - xlat - rol eax,8 - xlat - rol eax,8 - bswap eax - //==================================================== - stosd - sub ecx,3 - - read_loop_cmp: - cmp ecx,3 // проверяем, чтобы длинна была как минимум 4 байта - jg short read_loop // если длинна 3 и более байт, то продолжаем дальше - - ///////////////////////////////////////////////////////////////// - xor eax,eax // обнуляем регистр - cmp ecx,3 // сравниваем длинну с 3 - je short tree_byte_tail// если длинна 3 байта, то переходим к функции обрабатывающей остаток такой длинны - cmp ecx,2 // сравниваем длинну с 2 - je short two_byte_tail // если длинна 2 байта, то переходим к функции обрабатывающей остаток такой длинны - // иначе, длинна остатка равна 1 - //////////tail 1 byte//////////////////////////////////////////// - mov al,byte ptr [esi] // читаем 1 байт - call code_func // преобразуем - and eax,0x0000FFFF // обнуляем последние два байта - or eax,0x3D3D0000 // записываем в последние два байта 61("=") - jmp short end_tail_handler // - - //////////tail 2 byte//////////////////////////////////////////// - two_byte_tail: - mov ax,word ptr [esi] // читаем 2 байта - call code_func // преобразуем - and eax,0x00FFFFFF // обнуляем последний байт - or eax,0x3D000000 // записываем в последний байт 61("=") - jmp short end_tail_handler // - - //////////tail 3 byte//////////////////////////////////////////// - tree_byte_tail: - lodsw - ror eax,16 - mov al,byte ptr [esi] // читаем 1 байт - rol eax,16 - call code_func // преобразуем - - end_tail_handler: - stosd // записываем 4 байта, уже закодированных - - sub edi,lpcDestination // вычисляем колличество байт, записанных в выходной буффер - mov dwEncodedSize,edi // записываем колличество байт в возвращаемую переменную - pop esi // восстанавливаем содержимое регистра - pop edi // восстанавливаем содержимое регистра - pop ebx // восстанавливаем содержимое регистра - } -#endif - dwRetErrorCode=NO_ERROR; - }else{ - dwRetErrorCode=ERROR_INVALID_HANDLE; - } - } - if (pdwEncodedSize) (*pdwEncodedSize)=dwEncodedSize; - -return(dwRetErrorCode); -} - - -__inline DWORD BASE64Encode(LPCVOID lpcSource,size_t dwSourceSize,LPCVOID lpcDestination,size_t dwDestinationSize,size_t *pdwEncodedSize) -{// BASE64 кодирование - DWORD dwRetErrorCode; - - __try - { - dwRetErrorCode=BASE64EncodeUnSafe(lpcSource,dwSourceSize,lpcDestination,dwDestinationSize,pdwEncodedSize); - }__except(EXCEPTION_EXECUTE_HANDLER){ - dwRetErrorCode=ERROR_INVALID_ADDRESS; - } -return(dwRetErrorCode); -} - - - -__inline DWORD BASE64DecodeUnSafe(LPCVOID lpcSource,size_t dwSourceSize,LPCVOID lpcDestination,size_t dwDestinationSize,size_t *pdwDecodedSize) -{// BASE64 декодирование - DWORD dwRetErrorCode; - size_t dwDecodedSize=((dwSourceSize>>2)*3);// ((dwSourceSize/4)*3); - - if ((dwDestinationSize3) - { -#ifdef _WIN64 - LPBYTE lpbtSource=(LPBYTE)lpcSource,lpbtDestination=(LPBYTE)lpcDestination; - - for(size_t i=0;i> 4); - *(lpbtDestination++)=(unsigned char) (btDeCodingTableBASE64[lpbtSource[1]] << 4 | btDeCodingTableBASE64[lpbtSource[2]] >> 2); - *(lpbtDestination++)=(unsigned char) (btDeCodingTableBASE64[lpbtSource[2]] << 6 | btDeCodingTableBASE64[lpbtSource[3]]); - lpbtSource+=4; - } - - dwDecodedSize=(lpbtDestination-((LPBYTE)lpcDestination)); - if ((*((BYTE*)lpcSource+(dwSourceSize-1)))=='=') dwDecodedSize--; - if ((*((BYTE*)lpcSource+(dwSourceSize-2)))=='=') dwDecodedSize--; - - -#else - __asm{ - push ebx // сохраняем регистр - push edi // сохраняем регистр - push esi // сохраняем регистр - - mov ebx,offset btDeCodingTableBASE64// ebx = адрес таблицы перекодировки - mov ecx,dwSourceSize // ecx = длинна входного буффера - mov edi,lpcDestination // edi = адрес выходного буффера - mov esi,lpcSource // esi = указатель на входной буффер - cld - - read_loop: - lodsd // читаем 4 байта - //===============bit conversion==================================== - // eax - входящий буффер, используется только 4 байта //in buff, 4 byte used - // eax - выходящий буффер, используется только 3 байта //out buff, 3 byte used - // третья версия функции сложения бит - bswap eax - - ror eax,8 - xlat - - ror eax,8 - xlat - - ror eax,8 - xlat - - ror eax,8 - xlat - - shl al,2 - shl ax,2 - rol eax,10 - shr al,2 - ror eax,6 - bswap eax - mov edx,eax - //234-250 - //=============================================================== - stosd - dec edi - sub ecx,4 - cmp ecx,3 - jg short read_loop - - sub edi,lpcDestination // вычисляем колличество байт, записанных в выходной буффер - mov dwDecodedSize,edi // записываем колличество байт в возвращаемую переменную - pop esi // восстанавливаем содержимое регистра - pop edi // восстанавливаем содержимое регистра - pop ebx // восстанавливаем содержимое регистра - } - - if ((*((BYTE*)lpcSource+(dwSourceSize-1)))=='=') dwDecodedSize--; - if ((*((BYTE*)lpcSource+(dwSourceSize-2)))=='=') dwDecodedSize--; -#endif - dwRetErrorCode=NO_ERROR; - }else{// во входном буффере слишком мало данных - dwRetErrorCode=ERROR_INSUFFICIENT_BUFFER; - } - }else{ - dwRetErrorCode=ERROR_INVALID_HANDLE; - } - } - - if (pdwDecodedSize) (*pdwDecodedSize)=dwDecodedSize; -return(dwRetErrorCode); -} - - -__inline DWORD BASE64Decode(LPCVOID lpcSource,size_t dwSourceSize,LPCVOID lpcDestination,size_t dwDestinationSize,size_t *pdwDecodedSize) -{// BASE64 декодирование - DWORD dwRetErrorCode; - - __try - { - dwRetErrorCode=BASE64DecodeUnSafe(lpcSource,dwSourceSize,lpcDestination,dwDestinationSize,pdwDecodedSize); - }__except(EXCEPTION_EXECUTE_HANDLER){ - dwRetErrorCode=ERROR_INVALID_ADDRESS; - } -return(dwRetErrorCode); -} - - -__inline DWORD BASE64DecodeFormated(LPCVOID lpcSource,size_t dwSourceSize,LPCVOID lpcDestination,size_t dwDestinationSize,size_t *pdwDecodedSize) -{// BASE64 декодирование и игнорирование форматирования - DWORD dwRetErrorCode; - - if (dwSourceSize<=dwDestinationSize) - { - BASE64CopyUnSafe(lpcSource,lpcDestination,dwSourceSize,&dwSourceSize); - dwRetErrorCode=BASE64DecodeUnSafe(lpcDestination,dwSourceSize,lpcDestination,dwDestinationSize,pdwDecodedSize); - }else{// во входном буффере слишком мало данных - dwRetErrorCode=ERROR_INSUFFICIENT_BUFFER; - } - -return(dwRetErrorCode); -} - - - -#endif // !defined(AFX_BASE64__H__INCLUDED_) diff --git a/protocols/MRA/src/Sdk/SHA1.h b/protocols/MRA/src/Sdk/SHA1.h index f7d4643d32..61f6c58399 100644 --- a/protocols/MRA/src/Sdk/SHA1.h +++ b/protocols/MRA/src/Sdk/SHA1.h @@ -25,7 +25,6 @@ * */ - #if !defined(AFX__SHA1_H__INCLUDED_) #define AFX__SHA1_H__INCLUDED_ @@ -33,92 +32,20 @@ #pragma once #endif // _MSC_VER > 1000 +//////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////// +//////////////////////////////RFC 2104////////////////////////////// +//////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////// +void hmac_sha1(BYTE *text,size_t text_len,BYTE *key,size_t key_len,BYTE *digest); -// see -// RFC 3174 - SHA1 -// RFC 2104 - HMAC -// RFC 2617 - CvtHex - - - -#define SHA1HashSize 20 -#define SHA1HashHexSize 40 - -/* This structure will hold context information for the SHA-1 hashing operation */ -typedef struct SHA1Context -{ - DWORD Intermediate_Hash[SHA1HashSize/4];/* Message Digest */ - ULARGE_INTEGER Length; /* Message length in bits */ - BYTE Message_Block[64]; /* 512-bit message blocks */ - BOOL Computed; /* Is the digest computed? */ -} SHA1Context; - - -#ifdef UNICODE - #define SHA1HMACGetString SHA1HMACGetStringW - #define SHA1GetStringDigest SHA1GetStringDigestW - #define SHA1CvtString SHA1CvtStringW -#else - #define SHA1HMACGetString SHA1HMACGetStringA - #define SHA1GetStringDigest SHA1GetStringDigestA - #define SHA1CvtString SHA1CvtStringA -#endif - - -#ifndef SHA1_MAX_SPEED - #ifdef SecureZeroMemory - #define SHA1SecureZeroMemory SecureZeroMemory - #else - #define SHA1SecureZeroMemory bzero - #endif -#else - #define SHA1SecureZeroMemory -#endif - - -/* - * Description: - * This file implements the Secure Hashing Algorithm 1 as - * defined in FIPS PUB 180-1 published April 17, 1995. - * - * The SHA-1, produces a 160-bit message digest for a given - * data stream. It should take about 2**n steps to find a - * message with the same digest as a given message and - * 2**(n/2) to find any two messages with the same digest, - * when n is the digest size in bits. Therefore, this - * algorithm can serve as a means of providing a - * "fingerprint" for a message. - * - * Portability Issues: - * SHA-1 is defined in terms of 32-bit "words". This code - * uses (included via "sha1.h" to define 32 and 8 - * bit unsigned integer types. If your C compiler does not - * support 32 bit unsigned integers, this code is not - * appropriate. - * - * Caveats: - * SHA-1 is designed to work with messages less than 2^64 bits - * long. Although SHA-1 allows a message digest to be generated - * for messages of any number of bits less than 2^64, this - * implementation only works with messages with a length that is - * a multiple of the size of an 8-bit character. - * - */ - - - -/* Define the SHA1 circular left shift macro */ -#define SHA1CircularShift(bits,word) (((word) << (bits)) | ((word) >> (32-(bits)))) - -/* Local Function Prototyptes */ -//void SHA1PadMessage(SHA1Context *); -//void SHA1ProcessMessageBlock(SHA1Context *); - +//////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////// __inline DWORD BSWAP(DWORD dwIn) { -return((((dwIn<<8) & 0x00ff0000) | (dwIn<<24) | ((dwIn>>8) & 0x0000ff00) | (dwIn>>24))); + return((((dwIn<<8) & 0x00ff0000) | (dwIn<<24) | ((dwIn>>8) & 0x0000ff00) | (dwIn>>24))); } __inline void CopyMemoryReverseDWORD(LPCVOID lpcDestination,LPCVOID lpcSource,size_t dwSize) @@ -152,445 +79,16 @@ __inline void CopyMemoryReverseDWORD(LPCVOID lpcDestination,LPCVOID lpcSource,si #endif } - - -/* -* SHA1ProcessMessageBlock -* -* Description: -* This function will process the next 512 bits of the message -* stored in the Message_Block array. -* -* Parameters: -* None. -* -* Returns: -* Nothing. -* -* Comments: -* Many of the variable names in this code, especially the -* single character names, were used because those were the -* names used in the publication. -* -* -*/ -__inline void SHA1ProcessMessageBlock(SHA1Context *context,BYTE *Message_Block) -{ - /* Constants defined in SHA-1 */ - const DWORD K[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6}; - DWORD t; /* Loop counter */ - DWORD temp; /* Temporary word value */ - DWORD W[80]; /* Word sequence */ - DWORD A=context->Intermediate_Hash[0],/* Word buffers */ - B=context->Intermediate_Hash[1], - C=context->Intermediate_Hash[2], - D=context->Intermediate_Hash[3], - E=context->Intermediate_Hash[4]; - - /* Initialize the first 16 words in the array W */ - CopyMemoryReverseDWORD(W,Message_Block,64); - - for(t=16;t<80;t++) - { - W[t]=SHA1CircularShift(1,W[t-3]^W[t-8]^W[t-14]^W[t-16]); - } - - for(t=0;t<20;t++) - { - temp=SHA1CircularShift(5,A) + ((B&C) | ((~B)&D)) + E + W[t] + K[0]; - E=D; - D=C; - C=SHA1CircularShift(30,B); - B=A; - A=temp; - } - - for(t=20;t<40;t++) - { - temp=SHA1CircularShift(5,A) + (B^C^D) + E + W[t] + K[1]; - E=D; - D=C; - C=SHA1CircularShift(30,B); - B=A; - A=temp; - } - - for(t=40;t<60;t++) - { - temp=SHA1CircularShift(5,A) + ((B&C) | (B&D) | (C&D)) + E + W[t] + K[2]; - E=D; - D=C; - C=SHA1CircularShift(30,B); - B=A; - A=temp; - } - - for(t=60;t<80;t++) - { - temp=SHA1CircularShift(5,A) + (B^C^D) + E + W[t] + K[3]; - E=D; - D=C; - C=SHA1CircularShift(30,B); - B=A; - A=temp; - } - - context->Intermediate_Hash[0]+=A; - context->Intermediate_Hash[1]+=B; - context->Intermediate_Hash[2]+=C; - context->Intermediate_Hash[3]+=D; - context->Intermediate_Hash[4]+=E; - - /* Zeroize sensitive information.*/ - SHA1SecureZeroMemory(W,sizeof(W)); -} - - -/* -* SHA1PadMessage -* -* Description: -* According to the standard, the message must be padded to an even -* 512 bits. The first padding bit must be a '1'. The last 64 -* bits represent the length of the original message. All bits in -* between should be 0. This function will pad the message -* according to those rules by filling the Message_Block array -* accordingly. It will also call the ProcessMessageBlock function -* provided appropriately. When it returns, it can be assumed that -* the message digest has been computed. -* -* Parameters: -* context: [in/out] -* The context to pad -* ProcessMessageBlock: [in] -* The appropriate SHA*ProcessMessageBlock function -* Returns: -* Nothing. -* -*/ -__inline void SHA1PadMessage(SHA1Context *context) -{ - /* - * Check to see if the current message block is too small to hold - * the initial padding bits and length. If so, we will pad the - * block, process it, and then continue padding into a second - * block. - */ - size_t Message_Block_Index=(size_t)((context->Length.LowPart>>3) & 0x3F); - context->Message_Block[Message_Block_Index++]=0x80; - if (Message_Block_Index>56) - { - memset(&context->Message_Block[Message_Block_Index], 0, (64-Message_Block_Index)); - SHA1ProcessMessageBlock(context,context->Message_Block); - memset(&context->Message_Block, 0, 56); - }else{ - memset(&context->Message_Block[Message_Block_Index], 0, (56-Message_Block_Index)); - } - - /* Store the message length as the last 8 octets */ - context->Message_Block[56]=(BYTE)(context->Length.HighPart>>24); - context->Message_Block[57]=(BYTE)(context->Length.HighPart>>16); - context->Message_Block[58]=(BYTE)(context->Length.HighPart>>8); - context->Message_Block[59]=(BYTE)(context->Length.HighPart); - context->Message_Block[60]=(BYTE)(context->Length.LowPart>>24); - context->Message_Block[61]=(BYTE)(context->Length.LowPart>>16); - context->Message_Block[62]=(BYTE)(context->Length.LowPart>>8); - context->Message_Block[63]=(BYTE)(context->Length.LowPart); - - SHA1ProcessMessageBlock(context,context->Message_Block); -} - - - -/* -* SHA1Reset -* -* Description: -* This function will initialize the SHA1Context in preparation -* for computing a new SHA1 message digest. -* -* Parameters: -* context: [in/out] -* The context to reset. -* -* Returns: -* sha Error Code. -* -*/ -__inline DWORD SHA1Reset(SHA1Context *context) -{ - context->Intermediate_Hash[0]=0x67452301; - context->Intermediate_Hash[1]=0xEFCDAB89; - context->Intermediate_Hash[2]=0x98BADCFE; - context->Intermediate_Hash[3]=0x10325476; - context->Intermediate_Hash[4]=0xC3D2E1F0; - context->Length.QuadPart=0; - context->Computed=FALSE; - -return(NO_ERROR); -} - -/* -* SHA1Result -* -* Description: -* This function will return the 160-bit message digest into the -* Message_Digest array provided by the caller. -* NOTE: The first octet of hash is stored in the 0th element, -* the last octet of hash in the 19th element. -* -* Parameters: -* context: [in/out] -* The context to use to calculate the SHA-1 hash. -* Message_Digest: [out] -* Where the digest is returned. -* -* Returns: -* sha Error Code. -* -*/ -__inline DWORD SHA1Result(SHA1Context *context,BYTE *Message_Digest) -{ - if (context->Computed==FALSE) - { - SHA1PadMessage(context); - SHA1SecureZeroMemory(context->Message_Block,64);/* message may be sensitive, clear it out */ - context->Length.QuadPart=0; /* and clear length */ - context->Computed=TRUE; - } - - CopyMemoryReverseDWORD(Message_Digest,context->Intermediate_Hash,SHA1HashSize); - -return(NO_ERROR); -} - -/* -* SHA1Input -* -* Description: -* This function accepts an array of octets as the next portion -* of the message. -* -* Parameters: -* context: [in/out] -* The SHA context to update -* message_array: [in] -* An array of characters representing the next portion of -* the message. -* length: [in] -* The length of the message in message_array -* -* Returns: -* sha Error Code. -* -*/ -__inline DWORD SHA1Input(SHA1Context *context,const BYTE *message_array,size_t length) -{ - if (context->Computed==TRUE) return(ERROR_INVALID_HANDLE_STATE); - - if ((context->Length.QuadPart+(length<<3))>=(length<<3)) - { - size_t i,Message_Block_Index,partLen; - /* Compute number of bytes mod 64 */ - Message_Block_Index=(size_t)((context->Length.LowPart>>3) & 0x3F); - /* Update number of bits */ - context->Length.QuadPart+=(((ULONGLONG)length)<<3); - partLen=(64-Message_Block_Index); - /* Transform as many times as possible.*/ - if (length>=partLen) - { - memmove(&context->Message_Block[Message_Block_Index],message_array,partLen); - SHA1ProcessMessageBlock(context,context->Message_Block); - for (i=partLen;(i+63)Message_Block[Message_Block_Index],&message_array[i],(length-i)); - }else{ - return(RPC_S_STRING_TOO_LONG);/* Message is too long */ - } -return(NO_ERROR); -} -//////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////// -//////////////////////////////RFC 2104////////////////////////////// -//////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////// -__inline void hmac_sha1(BYTE *text,size_t text_len,BYTE *key,size_t key_len,BYTE *digest) -{ -//BYTE* text; /* pointer to data stream */ -//int text_len; /* length of data stream */ -//BYTE* key; /* pointer to authentication key */ -//int key_len; /* length of authentication key */ -//HASH digest; /* caller digest to be filled in */ - SHA1Context context; - BYTE k_ipad[65]; /* inner padding - key XORd with ipad */ - BYTE k_opad[65]; /* outer padding - key XORd with opad */ - BYTE tk[SHA1HashSize]; - /* if key is longer than 64 bytes reset it to key=SHA1(key) */ - if (key_len>64) - { - SHA1Context tctx; - - SHA1Reset(&tctx); - SHA1Input(&tctx,key,key_len); - SHA1Result(&tctx,(BYTE*)&tk); - - key=tk; - key_len=SHA1HashSize; - } - - /* - * the HMAC_SHA1 transform looks like: - * - * SHA1(K XOR opad, SHA1(K XOR ipad, text)) - * - * where K is an n byte key - * ipad is the byte 0x36 repeated 64 times - * opad is the byte 0x5c repeated 64 times - * and text is the data being protected - */ - - /* start out by storing key in pads */ - memmove(&k_ipad,key,key_len); - memmove(&k_opad,key,key_len); - memset(&k_ipad[key_len], 0, (sizeof(k_ipad)-key_len)); - memset(&k_opad[key_len], 0 , (sizeof(k_opad)-key_len)); - - /* XOR key with ipad and opad values */ - for (size_t i=0;i<(64/sizeof(ULONGLONG));i++) - { - ((ULONGLONG*)k_ipad)[i]^=0x3636363636363636; - ((ULONGLONG*)k_opad)[i]^=0x5C5C5C5C5C5C5C5C; - } - /* perform inner SHA1 */ - SHA1Reset(&context); /* init context for 1st pass */ - SHA1Input(&context,k_ipad,64); /* start with inner pad */ - SHA1Input(&context,text,text_len); /* then text of datagram */ - SHA1Result(&context,digest); /* finish up 1st pass */ - /* perform outer SHA1 */ - SHA1Reset(&context); /* init context for 2nd pass */ - SHA1Input(&context,k_opad,64); /* start with outer pad */ - SHA1Input(&context,(BYTE*)digest,SHA1HashSize); /* then results of 1st hash */ - SHA1Result(&context,digest); /* finish up 2nd pass */ - - SHA1SecureZeroMemory(k_ipad,sizeof(k_ipad)); - SHA1SecureZeroMemory(k_opad,sizeof(k_opad)); - SHA1SecureZeroMemory(tk,sizeof(tk)); -} -//////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////// -//////////////////////////////RFC 2617////////////////////////////// -//////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////// -__inline void SHA1CvtHex(BYTE *Bin,BYTE *Hex) -{ - BYTE j; - - for (size_t i=0;i>4)&0xf; - if(j<=9) - { - Hex[(i*2)]=(j+'0'); - }else{ - Hex[(i*2)]=(j+'a'-10); - } - - j=Bin[i]&0xf; - if(j<=9) - { - Hex[(i*2+1)]=(j+'0'); - }else{ - Hex[(i*2+1)]=(j+'a'-10); - } - }; - Hex[SHA1HashHexSize]=0; -}; - -//////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////// -__inline void SHA1CvtStringA(BYTE *digest,LPSTR lpszDigest) -{ - SHA1CvtHex(digest,(BYTE*)lpszDigest); -}; - - -__inline void SHA1CvtStringW(BYTE *digest,LPWSTR lpszDigest) -{ - size_t i,p=0; - for (i=0;i