[ActiveLock-Development] CVS: activelock2/src modActiveLock.bas,1.8,1.9
Brought to you by:
ialkan
From: Thanh H. T. <th...@us...> - 2003-08-04 03:58:04
|
Update of /cvsroot/activelock/activelock2/src In directory sc8-pr-cvs1:/tmp/cvs-serv13771 Modified Files: modActiveLock.bas Log Message: VBdox'ed this module Index: modActiveLock.bas =================================================================== RCS file: /cvsroot/activelock/activelock2/src/modActiveLock.bas,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- modActiveLock.bas 3 Aug 2003 08:33:35 -0000 1.8 +++ modActiveLock.bas 4 Aug 2003 03:58:00 -0000 1.9 @@ -42,54 +42,117 @@ ' / MODULE TO DO LIST / ' /////////////////////////////////////////////////////////////////////// ' -' [ ] Nothing to do :) -' +' @bug rsa_encrypt() sometimes causes crash. This is related a bug in +' rsa_generate(). Sometimes, rsa_generate() generates a bad keyset, and +' this causes the encryption routines to crash. + ' /////////////////////////////////////////////////////////////////////// ' / MODULE CHANGE LOG / ' /////////////////////////////////////////////////////////////////////// -' +' @history +' <pre> ' 07.07.03 - mcrute - Updated the header comments for this file. ' 07.30.03 - th2tran - New routines to do MD5 hashes of TypeLibs. ' 08.02.03 - th2tran - wizzardme2000 found a gaping security hole with using md5_hash(). -' So now I'm using CRC checksums and MapFileAndCheckSum() API call +' So now I'm using CRC checksums and MapFileAndCheckSum() API call ' instead. This approach was suggested by Peter Young (vbclassicforever) ' in the forum and mailing list a while back. -' +' 08.02.03 - th2tran - VBdox'd this module. +' </pre> + ' /////////////////////////////////////////////////////////////////////// ' / MODULE CODE BEGINS BELOW THIS LINE / ' /////////////////////////////////////////////////////////////////////// - Option Private Module Option Explicit Option Base 0 +'' +' RSA Key Structure +' @param bits Key length in bits +' @param data Key data Type RSAKey ' 36-byte structure bits As Long -' bytes As Long -' modulus As Integer -' exponent As Integer -' private_exponent As Integer -' p As Integer -' q As Integer -' iqmp As Integer -' comment As String data(32) As Byte End Type '' -' ALCrypto.Dll Declares +' Generates an RSA key set +' @param ptrKey RSA key structure +' @param bits key length in bits +' @param pfn TBD +' @param pfnparam TBD Public Declare Function rsa_generate Lib "ALCrypto" (ptrKey As RSAKey, ByVal bits As Long, ByVal pfn As Long, ByVal pfnparam As Long) As Long + +'' +' Returns the public key blob fro the specified key. +' @param ptrKey RSA key structure +' @param blob [Output] Key bob to be returned +' @param blobLen Length of the key blob, in bytes Public Declare Function rsa_public_key_blob Lib "ALCrypto" (ptrKey As RSAKey, ByVal blob As String, blobLen As Long) As Long + +'' +' Returns the private key blob fro the specified key. +' @param ptrKey RSA key structure +' @param blob [Output] Key bob to be returned +' @param blobLen Length of the key blob, in bytes Public Declare Function rsa_private_key_blob Lib "ALCrypto" (ptrKey As RSAKey, ByVal blob As String, blobLen As Long) As Long + +'' +' Creates a new RSAKey from the specified key blobs. +' @param pub_blob Public key blob +' @param pub_len Length of public key blob, in bytes +' @param priv_blob Private key blob +' @param priv_len Length of private key blob, in bytes +' @param ptrKey [out] RSA key to be returned. +' Public Declare Function rsa_createkey Lib "ALCrypto" (ByVal pub_blob As String, ByVal pub_len As Long, ByVal priv_blob As String, ByVal priv_len As Long, ptrKey As RSAKey) As Long +'' +' Release memory allocated by rsa_createkey() to store the key. +' @param ptrKey RSA key Public Declare Function rsa_freekey Lib "ALCrypto" (ptrKey As RSAKey) As Long -' CryptType = 0 for public; 1 for private +'' +' RSA encrypts the data. +' @param CryptType CryptType = 0 for public; 1 for private +' @param Data Data to be encrypted +' @param dLen [in/out] Length of data, in bytes. This parameter will contain length of encrypted data when returned. +' @param ptrKey Key to be used for encryption +' Public Declare Function rsa_encrypt Lib "ALCrypto" (ByVal CryptType As Long, ByVal data As String, dLen As Long, ptrKey As RSAKey) As Long + +'' +' RSA decrypts the data. +' @param CryptType CryptType = 0 for public; 1 for private +' @param Data Data to be encrypted +' @param dLen [in/out] Length of data, in bytes. This parameter will contain length of encrypted data when returned. +' @param ptrKey Key to be used for encryption +' Public Declare Function rsa_decrypt Lib "ALCrypto" (ByVal CryptType As Long, ByVal data As String, dLen As Long, ptrKey As RSAKey) As Long + +'' +' Signs the data using the specified RSA private key. +' @param ptrKey Key to be used for signing +' @param data Data to be signed +' @param dLen Data length +' @param sig [out] Signature +' @param sLen Signature length Public Declare Function rsa_sign Lib "ALCrypto" (ByRef ptrKey As RSAKey, ByVal data As String, ByVal dLen As Long, ByVal sig As String, ByRef sLen As Long) As Long + +'' +' Verifies an RSA signature. +' @param ptrKey Key to be used for signing +' @param sig [out] Signature +' @param sLen Signature length +' @param data Data with which to verify +' @param dLen Data length Public Declare Function rsa_verifysig Lib "ALCrypto" (ByRef ptrKey As RSAKey, ByVal sig As String, ByVal sLen As Long, ByVal data As String, ByVal dLen As Long) As Long +'' +' Computers an MD5 hash from the data. +' @param inData Data to be hashed +' @param nDataLen Length of inData +' @param outData [out] 32-byte Computed hash code Public Declare Function md5_hash Lib "ALCrypto" (ByVal inData As String, ByVal nDataLen As Long, ByVal outData As String) As Long Type PhaseType @@ -131,92 +194,9 @@ Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long Private Declare Function MapFileAndCheckSum Lib "imagehlp" Alias "MapFileAndCheckSumA" (ByVal FileName As String, HeaderSum As Long, CheckSum As Long) As Long -Public Sub Init() - If fInit Then Exit Sub - 'TODO: Add module init code here - fInit = True -End Sub - -Public Static Sub GetWord(LongValue As Long, CryptBuffer() As Byte, Offset As Long) -On Local Error Resume Next -' Call CopyMem(LongValue, CryptBuffer(Offset), 4) - - Dim bb(0 To 3) As Byte - - bb(3) = CryptBuffer(Offset) - bb(2) = CryptBuffer(Offset + 1) - bb(1) = CryptBuffer(Offset + 2) - bb(0) = CryptBuffer(Offset + 3) - Call CopyMem(LongValue, bb(0), 4) - -End Sub - -Public Static Sub PutWord(LongValue As Long, CryptBuffer() As Byte, Offset As Long) -On Local Error Resume Next -' Call CopyMem(CryptBuffer(Offset), LongValue, 4) - - Dim bb(0 To 3) As Byte - - Call CopyMem(bb(0), LongValue, 4) - CryptBuffer(Offset) = bb(3) - CryptBuffer(Offset + 1) = bb(2) - CryptBuffer(Offset + 2) = bb(1) - CryptBuffer(Offset + 3) = bb(0) - -End Sub -Public Static Function UnsignedAdd(ByVal Data1 As Long, Data2 As Long) As Long - - Dim x1(0 To 3) As Byte - Dim x2(0 To 3) As Byte - Dim xx(0 To 3) As Byte - Dim Rest As Long - Dim Value As Long - Dim a As Long - - Call CopyMem(x1(0), Data1, 4) - Call CopyMem(x2(0), Data2, 4) - - Rest = 0 - For a = 0 To 3 - Value = CLng(x1(a)) + CLng(x2(a)) + Rest - xx(a) = Value And 255 - Rest = Value \ 256 - Next - - Call CopyMem(UnsignedAdd, xx(0), 4) - -End Function -Public Function UnsignedDel(Data1 As Long, Data2 As Long) As Long - - Dim x1(0 To 3) As Byte - Dim x2(0 To 3) As Byte - Dim xx(0 To 3) As Byte - Dim Rest As Long - Dim Value As Long - Dim a As Long - - Call CopyMem(x1(0), Data1, 4) - Call CopyMem(x2(0), Data2, 4) - Call CopyMem(xx(0), UnsignedDel, 4) - - For a = 0 To 3 - Value = CLng(x1(a)) - CLng(x2(a)) - Rest - If (Value < 0) Then - Value = Value + 256 - Rest = 1 - Else - Rest = 0 - End If - xx(a) = Value - Next - - Call CopyMem(UnsignedDel, xx(0), 4) - -End Function - '' ' Trims Null characters from the string. -' +' @param startstr String to be trimmed Public Function TrimNulls(startstr As String) As String Dim pos As Integer pos = InStr(startstr, Chr$(0)) @@ -230,6 +210,8 @@ '' ' Reads a binary file into the sData buffer. ' Returns the number of bytes read. +' @param sPath Path to the file to be read +' @param sData Output parameter contains the data that has been read ' Public Function ReadFile(ByVal sPath As String, ByRef sData As String) As Long Dim hFile As Integer @@ -252,12 +234,20 @@ Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext End Function +'' +' [INTERNAL] Call-back routine used by ALCrypto.dll during key generation process. +' @param param TBD +' @param action Action being performed +' @param phase Current phase +' @param iprogress Percent complete +' Public Sub CryptoProgressUpdate(ByVal param As Long, ByVal action As Long, ByVal phase As Long, ByVal iprogress As Long) Debug.Print "Progress Update received " & param & ", action: " & action & ", iprogress: " & iprogress End Sub '' ' Computes an MD5 hash of the type library containing the object. +' @param obj COM object used to determine the file path to the type library ' Public Function MD5HashTypeLib(obj As IUnknown) As String Dim strDllPath As String @@ -278,6 +268,7 @@ '' ' Performs CRC checksum on the type library containing the object. +' @param obj COM object used to determine the file path to the type library ' Public Function CRCCheckSumTypeLib(obj As IUnknown) As Long Dim strDllPath As String @@ -290,6 +281,7 @@ '' ' Computes an MD5 hash of the specified file. +' @param strPath File path ' Public Function MD5HashFile(ByVal strPath As String) As String Debug.Print "Hashing file " & strPath @@ -304,7 +296,8 @@ End Function '' -' Check if we're running inside the VB6 IDE +' Check if we're running inside the VB6 IDE +' Public Function IsRunningInIde() As Boolean Dim strFileName As String Dim lngCount As Long |