📄 init.c
字号:
/* Shut down all of the kernel modules */
endAllocation();
endAttributeACL();
endCertMgmtACL();
endInternalMsgs();
endKeymgmtACL();
endMechanismACL();
endMessageACL();
endObjects();
endObjectAltAccess();
endSemaphores();
endSendMessage();
/* At this point all kernel services have been shut down */
assert( krnlData->shutdownLevel >= SHUTDOWN_LEVEL_MUTEXES );
/* Turn off the lights on the way out. Note that the kernel data-
clearing operation leaves the shutdown level set to handle any
threads that may still be active */
CLEAR_KERNEL_DATA();
krnlData->shutdownLevel = SHUTDOWN_LEVEL_ALL;
MUTEX_UNLOCK( initialisation );
#ifdef STATIC_INIT
/* We're shutting down, destroy the initialisation lock */
MUTEX_DESTROY( initialisation );
memset( krnlData, 0, sizeof( KERNEL_DATA ) );
#endif /* STATIC_INIT */
return( CRYPT_OK );
}
/* Indicate to a cryptlib-internal worker thread that the kernel is shutting
down and the thread should exit as quickly as possible. We don't protect
this check with a mutex since it can be called after the kernel mutexes
have been destroyed. This lack of mutex protection for the flag isn't a
serious problem, it's checked at regular intervals by worker threads so
if the thread misses the flag update it'll bve caught at the next check */
BOOLEAN krnlIsExiting( void )
{
return( krnlData->shutdownLevel >= SHUTDOWN_LEVEL_THREADS );
}
/****************************************************************************
* *
* Extended Self-test Functions *
* *
****************************************************************************/
/* Self-test code for several general crypto algorithms that are used
internally all over cryptlib: MD5, SHA-1, and 3DES (and by extension
DES) */
#define DES_BLOCKSIZE 8
#if defined( INC_ALL )
#include "des.h"
#include "testdes.h"
#elif defined( INC_CHILD )
#include "../crypt/des.h"
#include "../crypt/testdes.h"
#else
#include "crypt/des.h"
#include "crypt/testdes.h"
#endif /* Compiler-specific includes */
static BOOLEAN des3TestLoop( const DES_TEST *testData, int iterations )
{
BYTE temp[ DES_BLOCKSIZE ];
BYTE key1[ DES_SCHEDULE_SZ ], key2[ DES_SCHEDULE_SZ ], key3[ DES_SCHEDULE_SZ ];
int i;
for( i = 0; i < iterations; i++ )
{
memcpy( temp, testData[ i ].plaintext, DES_BLOCKSIZE );
des_set_key_unchecked( ( C_Block * ) testData[ i ].key,
*( ( Key_schedule * ) key1 ) );
des_set_key_unchecked( ( C_Block * ) testData[ i ].key,
*( ( Key_schedule * ) key2 ) );
des_set_key_unchecked( ( C_Block * ) testData[ i ].key,
*( ( Key_schedule * ) key3 ) );
des_ecb3_encrypt( ( C_Block * ) temp, ( C_Block * ) temp,
*( ( Key_schedule * ) key1 ),
*( ( Key_schedule * ) key2 ),
*( ( Key_schedule * ) key3 ), DES_ENCRYPT );
if( memcmp( testData[ i ].ciphertext, temp, DES_BLOCKSIZE ) )
return( FALSE );
}
return( TRUE );
}
static BOOLEAN testGeneralAlgorithms( void )
{
static const FAR_BSS struct {
const char *data;
const int length;
const BYTE hashValue[ 16 ];
} md5Vectors[] = { /* RFC 1321 MD5 test vectors */
{ "", 0,
{ 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E } },
{ "a", 1,
{ 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 } },
{ "abc", 3,
{ 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 } },
{ "message digest", 14,
{ 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 } },
{ "abcdefghijklmnopqrstuvwxyz", 26,
{ 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B } },
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62,
{ 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F } },
{ "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 80,
{ 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A } },
{ NULL, 0, { 0 } }
};
static const FAR_BSS struct {
const char *data;
const int length;
const BYTE hashValue[ 20 ];
} sha1Vectors[] = { /* FIPS 180-1 SHA-1 test vectors */
{ "abc", 3,
{ 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C,
0x9C, 0xD0, 0xD8, 0x9D } },
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
{ 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
0xE5, 0x46, 0x70, 0xF1 } },
{ NULL, 0, { 0 } }
};
HASHFUNCTION hashFunction;
BYTE hashValue[ CRYPT_MAX_HASHSIZE ];
int hashSize, i;
/* Test the MD5 code against the values given in RFC 1321 */
getHashParameters( CRYPT_ALGO_MD5, &hashFunction, &hashSize );
if( hashFunction == NULL || hashSize != 16 )
return( FALSE );
for( i = 0; md5Vectors[ i ].data != NULL; i++ )
{
hashFunction( NULL, hashValue, ( BYTE * ) md5Vectors[ i ].data,
md5Vectors[ i ].length, HASH_ALL );
if( memcmp( hashValue, md5Vectors[ i ].hashValue, 16 ) )
return( FALSE );
}
/* Test the SHA-1 code against the values given in FIPS 180-1 */
getHashParameters( CRYPT_ALGO_SHA, &hashFunction, &hashSize );
if( hashFunction == NULL || hashSize != 20 )
return( FALSE );
for( i = 0; sha1Vectors[ i ].data != NULL; i++ )
{
hashFunction( NULL, hashValue, ( BYTE * ) sha1Vectors[ i ].data,
sha1Vectors[ i ].length, HASH_ALL );
if( memcmp( hashValue, sha1Vectors[ i ].hashValue, 20 ) )
return( FALSE );
}
/* Test the 3DES code against the values given in NIST Special Pub.800-20,
1999, which are actually the same as NBS Special Pub.500-20, 1980 since
they require that K1 = K2 = K3 */
if( !des3TestLoop( testIP, sizeof( testIP ) / sizeof( DES_TEST ) ) || \
!des3TestLoop( testVP, sizeof( testVP ) / sizeof( DES_TEST ) ) || \
!des3TestLoop( testKP, sizeof( testKP ) / sizeof( DES_TEST ) ) || \
!des3TestLoop( testDP, sizeof( testDP ) / sizeof( DES_TEST ) ) || \
!des3TestLoop( testSB, sizeof( testSB ) / sizeof( DES_TEST ) ) )
return( FALSE );
return( TRUE );
}
/* Test the kernel mechanisms to make sure that everything's working as
expected. This performs the following tests:
General:
Object creation.
Access checks:
Inability to access internal object or attribute via external message.
Inability to perform an internal-only action externally, ability to
perform an internal-only action internally
Attribute checks:
Attribute range checking for numeric, string, boolean, and time
attributes.
Inability to write a read-only attribute, read a write-only attribute,
or delete a non-deletable attribute.
Object state checks:
Ability to perform standard operation on object, ability to transition a
state = low object to state = high.
Inability to perform state = high operation on state = low object,
inability to perform state = low operation on state = high object.
Object property checks:
Ability to use an object with a finite usage count, inability to
increment the count, ability to decrement the count, inability to
exceed the usage count.
Ability to lock an object, inability to change security parameters once
it's locked */
static BOOLEAN testKernelMechanisms( void )
{
MESSAGE_CREATEOBJECT_INFO createInfo;
RESOURCE_DATA msgData;
CRYPT_CONTEXT cryptHandle;
static const BYTE key[] = { 0x10, 0x46, 0x91, 0x34, 0x89, 0x98, 0x01, 0x31 };
BYTE buffer[ 128 ];
time_t timeVal;
int value, status;
/* Verify object creation */
setMessageCreateObjectInfo( &createInfo, CRYPT_ALGO_DES );
status = krnlSendMessage( SYSTEM_OBJECT_HANDLE, IMESSAGE_DEV_CREATEOBJECT,
&createInfo, OBJECT_TYPE_CONTEXT );
if( cryptStatusError( status ) )
return( FALSE );
cryptHandle = createInfo.cryptHandle;
/* Verify the inability to access an internal object or attribute using
an external message. The attribute access will be stopped by the
object access check before it even gets to the attribute access check,
so we also re-do the check further on when the object is made
externally visible to verify the attribute-level checks as well */
if( krnlSendMessage( cryptHandle, MESSAGE_GETATTRIBUTE, &value,
CRYPT_CTXINFO_ALGO ) != CRYPT_ARGERROR_OBJECT || \
krnlSendMessage( cryptHandle, MESSAGE_GETATTRIBUTE, &value,
CRYPT_IATTRIBUTE_TYPE ) != CRYPT_ARGERROR_VALUE )
{
krnlSendNotifier( cryptHandle, IMESSAGE_DECREFCOUNT );
return( FALSE );
}
/* Verify the ability to perform standard operations and the inability
to perform a state = high operation on a state = low object */
setMessageData( &msgData, ( void * ) key, 8 );
memset( buffer, 0, 16 );
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE_S, &msgData,
CRYPT_CTXINFO_IV ) != CRYPT_OK || \
krnlSendMessage( cryptHandle, IMESSAGE_CTX_ENCRYPT,
buffer, 8 ) != CRYPT_ERROR_NOTINITED )
{
krnlSendNotifier( cryptHandle, IMESSAGE_DECREFCOUNT );
return( FALSE );
}
/* Verify the functioning of kernel range checking, phase 1: Numeric
values */
status = CRYPT_OK;
value = -10; /* Below (negative) */
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE, &value,
CRYPT_CTXINFO_KEYING_ITERATIONS ) != CRYPT_ARGERROR_NUM1 )
status = CRYPT_ERROR;
value = 0; /* Lower bound fencepost error */
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE, &value,
CRYPT_CTXINFO_KEYING_ITERATIONS ) != CRYPT_ARGERROR_NUM1 )
status = CRYPT_ERROR;
value = 1; /* Lower bound */
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE, &value,
CRYPT_CTXINFO_KEYING_ITERATIONS ) != CRYPT_OK )
status = CRYPT_ERROR;
value = 10000; /* Mid-range */
krnlSendMessage( cryptHandle, IMESSAGE_DELETEATTRIBUTE, NULL,
CRYPT_CTXINFO_KEYING_ITERATIONS );
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE, &value,
CRYPT_CTXINFO_KEYING_ITERATIONS ) != CRYPT_OK )
status = CRYPT_ERROR;
value = 20000; /* Upper bound */
krnlSendMessage( cryptHandle, IMESSAGE_DELETEATTRIBUTE, NULL,
CRYPT_CTXINFO_KEYING_ITERATIONS );
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE, &value,
CRYPT_CTXINFO_KEYING_ITERATIONS ) != CRYPT_OK )
status = CRYPT_ERROR;
value = 20001; /* Upper bound fencepost error */
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE, &value,
CRYPT_CTXINFO_KEYING_ITERATIONS ) != CRYPT_ARGERROR_NUM1 )
status = CRYPT_ERROR;
value = 32767; /* High */
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE, &value,
CRYPT_CTXINFO_KEYING_ITERATIONS ) != CRYPT_ARGERROR_NUM1 )
status = CRYPT_ERROR;
if( cryptStatusError( status ) )
{
krnlSendNotifier( cryptHandle, IMESSAGE_DECREFCOUNT );
return( FALSE );
}
/* Verify the functioning of kernel range checking, phase 2: String
values. We have to disable the more outrageous out-of-bounds values
in the debug version since they'll cause the debug kernel to throw an
exception if it sees them */
status = CRYPT_OK;
memset( buffer, '*', CRYPT_MAX_HASHSIZE + 1 );
#ifdef NDEBUG
/* Below (negative) */
setMessageData( &msgData, buffer, -10 );
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE_S, &msgData,
CRYPT_CTXINFO_KEYING_SALT ) != CRYPT_ARGERROR_NUM1 )
status = CRYPT_ERROR;
#endif /* NDEBUG */
/* Lower bound fencepost error */
setMessageData( &msgData, buffer, 7 );
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE_S, &msgData,
CRYPT_CTXINFO_KEYING_SALT ) != CRYPT_ARGERROR_NUM1 )
status = CRYPT_ERROR;
/* Lower bound */
setMessageData( &msgData, buffer, 8 );
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE_S, &msgData,
CRYPT_CTXINFO_KEYING_SALT ) != CRYPT_OK )
status = CRYPT_ERROR;
/* Mid-range */
setMessageData( &msgData, buffer, CRYPT_MAX_HASHSIZE / 2 );
krnlSendMessage( cryptHandle, IMESSAGE_DELETEATTRIBUTE, NULL,
CRYPT_CTXINFO_KEYING_SALT );
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE_S, &msgData,
CRYPT_CTXINFO_KEYING_SALT ) != CRYPT_OK )
status = CRYPT_ERROR;
/* Upper bound */
setMessageData( &msgData, buffer, CRYPT_MAX_HASHSIZE );
krnlSendMessage( cryptHandle, IMESSAGE_DELETEATTRIBUTE, NULL,
CRYPT_CTXINFO_KEYING_SALT );
if( krnlSendMessage( cryptHandle, IMESSAGE_SETATTRIBUTE_S, &msgData,
CRYPT_CTXINFO_KEYING_SALT ) != CRYPT_OK )
status = CRYPT_ERROR;
/* Upper bound fencepost error */
setMessageData( &msgData, buffer, CRYPT_MAX_HASHSIZE + 1 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -