📄 crypt.h
字号:
a real stream cipher rather than just a block cipher run in a stream
mode) */
#define isStreamCipher( algorithm ) ( ( algorithm ) == CRYPT_ALGO_RC4 )
/* A macro to check whether an algorithm is regarded as being (relatively)
insecure or not. This is used by some of the higher-level internal
routines that normally use the default algorithm set in the configuration
database if nothing else is explicitly specified, but that specifically
check for the weaker algorithms and use something stronger instead if a
weak algorithm is specified. This is done both for luser-proofing and to
avoid possible problems from a trojan patching the configuration
database */
#define isWeakCryptAlgo( algorithm ) ( ( algorithm ) == CRYPT_ALGO_DES || \
( algorithm ) == CRYPT_ALGO_RC4 )
/* Macros to check whether a PKC algorithm is useful for a certain purpose or
requires special-case handling */
#define isSigAlgo( algorithm ) \
( ( algorithm ) == CRYPT_ALGO_RSA || ( algorithm ) == CRYPT_ALGO_DSA || \
( algorithm ) == CRYPT_ALGO_ELGAMAL )
#define isCryptAlgo( algorithm ) \
( ( algorithm ) == CRYPT_ALGO_RSA || ( algorithm ) == CRYPT_ALGO_ELGAMAL )
#define isKeyxAlgo( algorithm ) \
( ( algorithm ) == CRYPT_ALGO_DH || ( algorithm ) == CRYPT_ALGO_KEA )
#define isDlpAlgo( algorithm ) \
( ( algorithm ) == CRYPT_ALGO_DSA || ( algorithm ) == CRYPT_ALGO_ELGAMAL || \
( algorithm ) == CRYPT_ALGO_DH || ( algorithm ) == CRYPT_ALGO_KEA )
/* Check the validity of a pointer passed to a cryptlib function. Usually
the best that we can do is check that it's not null, but some OSes allow
for better checking than this, for example that it points to a block of
readable or writeable memory. Under Windows IsBadReadPtr() will always
succeed if the size is 0, so we have to add a separate check to make sure
that it's non-NULL */
#if defined( __WIN32__ ) || defined( __WINCE__ )
#define isReadPtr( ptr, size ) ( ( ptr ) != NULL && ( size ) > 0 && \
!IsBadReadPtr( ( ptr ), ( size ) ) )
#define isWritePtr( ptr, size ) ( ( ptr ) != NULL && ( size ) > 0 && \
!IsBadWritePtr( ( ptr ), ( size ) ) )
#else
#define isReadPtr( ptr, size ) ( ( ptr ) != NULL && ( size ) > 0 )
#define isWritePtr( ptr, size ) ( ( ptr ) != NULL && ( size ) > 0 )
#endif /* Pointer check macros */
/* Clear/set object error information */
#define clearErrorInfo( objectInfoPtr ) \
{ \
( objectInfoPtr )->errorLocus = CRYPT_ATTRIBUTE_NONE; \
( objectInfoPtr )->errorType = CRYPT_OK; \
}
#define setErrorInfo( objectInfoPtr, locus, type ) \
{ \
( objectInfoPtr )->errorLocus = locus; \
( objectInfoPtr )->errorType = type; \
}
/****************************************************************************
* *
* Internal API Functions *
* *
****************************************************************************/
/* Pull in the internal API function definitions and prototypes */
#if defined( INC_ALL )
#include "int_api.h"
#else
#include "misc/int_api.h"
#endif /* Compiler-specific includes */
/****************************************************************************
* *
* Debugging Functions *
* *
****************************************************************************/
/* When we encounter an internal consistency check failure, we usually want
to display some sort of message telling the user that something has gone
catastrophically wrong, however people probably don't want klaxons going
off when there's a problem in production code so we only enable it in
debug versions. The command-line makefile by default builds release
versions, so in practice the warn-the-user action is only taken under
Windows unless the user explicitly enables the use of assertions */
#if defined( __WINCE__ ) && _WIN32_WCE < 400
#if 0
/* Older WinCE environments don't support assert() because there's no
console and no other support for it in the runtime (the documentation
claims there's at least an _ASSERT available, but this isn't present
in many systems such as PocketPC), so we use it if it's available and
otherwise kludge it using NKDbgPrintfW() */
#ifndef _ASSERTE
#ifdef NDEBUG
#define _ASSERTE( x )
#else
#define _ASSERTE( expr ) ( void )( ( expr ) || ( NKDbgPrintfW( #expr ) ) )
#endif /* Debug vs. non-debug builds */
#endif /* _ASSERTE available */
#define assert( expr ) _ASSERTE( expr )
#else
/* Older WinCE environments don't support assert() because there's no
console and no other support for it in the runtime (the documentation
claims there's at least an _ASSERT available, but this isn't present
in many systems such as PocketPC), so we build our own assert() from
DEBUGMSG(). Note that (in theory) the version check isn't reliable
since we should be checking for the development environment version
rather than the target OS version, however in practice compiler/SDK
version == OS version unless you seriously enjoy pain, and in any case
it's not really possible to differentiate between eVC++ 3.0 and 4.0 -
the SHx, MIPS, and ARM compilers at least report 120{1|2} for 3.0 and
1200 for 3.0, but the x86 compiler reports 1200 for both 3.0 and 4.0
even though it's a different build, 0.8168 vs. 0.8807 */
#ifdef NDEBUG
#define assert( x )
#else
#define assert( x ) \
DEBUGMSG( !( x ), ( TEXT( "Assert failed in %s line %d: %s" ), TEXT( __FILE__ ), __LINE__, TEXT( #x ) ) )
#endif /* Debug vs. non-debug builds */
#endif /* 0 */
#else
#include <assert.h>
#endif /* Systems without assert() */
#define NOTREACHED 0 /* Force an assertion failure via assert( NOTREACHED ) */
/* The following macro outputs an I-am-here to stdout, useful when tracing
errors in code without debug symbols available */
#define DEBUG_INFO() printf( "%4d %s.\n", __LINE__, __FILE__ );
/* The following macros can be used to enable dumping of PDUs to disk and to
create a hex dump of the first n bytes of a buffer, along with the length
and a checksum of the entire buffer. As a safeguard, these only work in
the Win32 debug version to prevent them from being accidentally enabled in
any release version */
#if defined( __WIN32__ ) && !defined( NDEBUG )
#define DEBUG_DUMP( name, data, length ) \
{ \
FILE *filePtr; \
char fileName[ 1024 ]; \
\
GetTempPath( 512, fileName ); \
strcat( fileName, name ); \
strcat( fileName, ".der" ); \
\
filePtr = fopen( fileName, "wb" ); \
if( filePtr != NULL ) \
{ \
if( length > 0 ) \
fwrite( data, 1, length, filePtr ); \
fclose( filePtr ); \
} \
}
#define DEBUG_DUMP_CERT( name, cert ) \
{ \
RESOURCE_DATA msgData; \
FILE *filePtr; \
char fileName[ 1024 ]; \
BYTE certData[ 2048 ]; \
\
GetTempPath( 512, fileName ); \
strcat( fileName, name ); \
strcat( fileName, ".der" ); \
\
filePtr = fopen( fileName, "wb" ); \
if( filePtr != NULL ) \
{ \
setMessageData( &msgData, certData, 2048 ); \
status = krnlSendMessage( cert, IMESSAGE_CRT_EXPORT, &msgData, \
CRYPT_CERTFORMAT_CERTIFICATE ); \
if( cryptStatusOK( status ) ) \
fwrite( msgData.data, 1, msgData.length, filePtr ); \
fclose( filePtr ); \
} \
}
#define DEBUG_DUMPHEX( dumpBuf, dumpLen ) \
{ \
const int maxLen = min( dumpLen, 19 ); \
int i; \
\
printf( "%4d %04X ", dumpLen, checksumData( dumpBuf, dumpLen ) ); \
for( i = 0; i < maxLen; i++ ) \
printf( "%02X ", ( ( BYTE * ) dumpBuf )[ i ] ); \
for( i = 0; i < maxLen; i++ ) \
{ \
const BYTE ch = ( ( BYTE * ) dumpBuf )[ i ]; \
\
putchar( isprint( ch ) ? ch : '.' ); \
} \
putchar( '\n' ); \
}
#else
#define DEBUG_DUMP( name, data, length )
#define DEBUG_DUMP_CERT( name, data, length )
#define DEBUG_DUMPHEX( dumpBuf, dumpLen )
#endif /* Win32 debug */
/* In order to debug memory usage, we can define CONFIG_DEBUG_MALLOC to dump
memory usage diagnostics to stdout (this would usually be done in the
makefile). Without this, the debug malloc just becomes a standard malloc.
Note that crypt/osconfig.h contains its own debug-malloc() handling for
the OpenSSL-derived code enabled via USE_BN_DEBUG_MALLOC in osconfig.h,
and zlib also has its own allocation code (which isn't instrumented for
diagnostic purposes).
In addition in order to control on-demand allocation of buffers for
larger-than-normal data items, we can define CONFIG_NO_DYNALLOC to
disable this allocation. This is useful in memory-constrained
environments where we can't afford to grab chunks of memory at random */
#ifdef CONFIG_DEBUG_MALLOC
#undef clAlloc
#undef clFree
#define clAlloc( string, size ) \
clAllocFn( __FILE__, ( string ), __LINE__, ( size ) )
#define clFree( string, memblock ) \
clFreeFn( __FILE__, ( string ), __LINE__, ( memblock ) )
void *clAllocFn( const char *fileName, const char *fnName,
const int lineNo, size_t size );
void clFreeFn( const char *fileName, const char *fnName,
const int lineNo, void *memblock );
#else
#define clAlloc( string, size ) malloc( size )
#define clFree( string, memblock ) free( memblock )
#endif /* !CONFIG_DEBUG_MALLOC */
#ifdef CONFIG_NO_DYNALLOC
#define clDynAlloc( string, size ) NULL
#else
#define clDynAlloc( string, size ) clAlloc( string, size )
#endif /* CONFIG_NO_DYNALLOC */
#endif /* _CRYPT_DEFINED */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -