📄 crypt.h
字号:
#include "os_spec.h"
#else
#include "misc/os_spec.h"
#endif /* Compiler-specific includes */
/****************************************************************************
* *
* Config Options *
* *
****************************************************************************/
/* Pull in the cryptlib initialisation options file, which contains the
various USE_xxx defines that enable different cryptlib features. Note
that this *must* be included after os_spec.h, which performs OS detection
used by config.h to enable/disable various code features */
#if defined( INC_ALL )
#include "config.h"
#else
#include "misc/config.h"
#endif /* Compiler-specific includes */
/****************************************************************************
* *
* Kernel Interface *
* *
****************************************************************************/
/* Pull in the cryptlib kernel interface defines */
#include "cryptkrn.h"
/****************************************************************************
* *
* Portability Defines *
* *
****************************************************************************/
/* Read/write values as 16- and 32-bit big-endian data, required for a
variety of non-ASN.1 data formats */
#define mgetWord( memPtr ) \
( ( ( unsigned int ) memPtr[ 0 ] << 8 ) | \
( unsigned int ) memPtr[ 1 ] ); \
memPtr += 2
#define mputWord( memPtr, data ) \
memPtr[ 0 ] = ( BYTE ) ( ( ( data ) >> 8 ) & 0xFF ); \
memPtr[ 1 ] = ( BYTE ) ( ( data ) & 0xFF ); \
memPtr += 2
#define mgetLong( memPtr ) \
( ( ( unsigned long ) memPtr[ 0 ] << 24 ) | \
( ( unsigned long ) memPtr[ 1 ] << 16 ) | \
( ( unsigned long ) memPtr[ 2 ] << 8 ) | \
( unsigned long ) memPtr[ 3 ] ); \
memPtr += 4
#define mputLong( memPtr, data ) \
memPtr[ 0 ] = ( BYTE ) ( ( ( data ) >> 24 ) & 0xFF ); \
memPtr[ 1 ] = ( BYTE ) ( ( ( data ) >> 16 ) & 0xFF ); \
memPtr[ 2 ] = ( BYTE ) ( ( ( data ) >> 8 ) & 0xFF ); \
memPtr[ 3 ] = ( BYTE ) ( ( data ) & 0xFF ); \
memPtr += 4
/****************************************************************************
* *
* Data Size and Crypto-related Constants *
* *
****************************************************************************/
/* Pull in the data-size and crypt-related constants */
#if defined( INC_ALL )
#include "consts.h"
#else
#include "misc/consts.h"
#endif /* Compiler-specific includes */
/****************************************************************************
* *
* Data Structures *
* *
****************************************************************************/
/* Information on exported key/signature data. This is an extended version
of the data returned by the externally-visible cryptQueryObject() routine */
typedef struct {
/* Object format and status information */
CRYPT_FORMAT_TYPE formatType; /* Object format type */
CRYPT_OBJECT_TYPE type; /* Object type */
long size; /* Object size */
int version; /* Object format version */
/* The encryption algorithm and mode */
CRYPT_ALGO_TYPE cryptAlgo; /* The encryption algorithm */
CRYPT_MODE_TYPE cryptMode; /* The encryption mode */
/* The key ID for public key objects */
BYTE keyID[ CRYPT_MAX_HASHSIZE ]; /* PKC key ID */
int keyIDlength;
/* The IV for conventionally encrypted data */
BYTE iv[ CRYPT_MAX_IVSIZE ]; /* IV */
int ivLength;
/* The key derivation algorithm and iteration count for conventionally
encrypted keys */
CRYPT_ALGO_TYPE keySetupAlgo; /* Key setup algorithm */
int keySetupIterations; /* Key setup iteration count */
BYTE salt[ CRYPT_MAX_HASHSIZE ];/* Key setup salt */
int saltLength;
/* The hash algorithm for signatures */
CRYPT_ALGO_TYPE hashAlgo; /* Hash algorithm */
/* The start and length of the payload data */
void *dataStart; /* Start of payload data */
int dataLength;
/* The start and length of the issuerAndSerialNumber and attributes for
CMS objects */
void *iAndSStart; /* Start of issuerAndSerialNumber */
int iAndSLength;
void *attributeStart; /* Start of attributes */
int attributeLength;
void *unauthAttributeStart; /* Start of unauthenticated attributes */
int unauthAttributeLength;
} QUERY_INFO;
/* DLP algorithms require composite parameters when en/decrypting and
signing/sig checking, so we can't just pass in a single buffer full of
data as we can with RSA. In addition the data length changes, for
example for a DSA sig we pass in a 20-byte hash and get back a ~50-byte
sig, for sig.checking we pass in a 20-byte hash and ~50-byte sig and get
back nothing. Because of this we have to use the following structure to
pass data to the DLP-based PKCs */
typedef struct {
const BYTE *inParam1, *inParam2; /* Input parameters */
BYTE *outParam; /* Output parameter */
int inLen1, inLen2, outLen; /* Parameter lengths */
CRYPT_FORMAT_TYPE formatType; /* Paramter format type */
} DLP_PARAMS;
#define setDLPParams( dlpDataPtr, dataIn, dataInLen, dataOut, dataOutLen ) \
{ \
memset( ( dlpDataPtr ), 0, sizeof( DLP_PARAMS ) ); \
( dlpDataPtr )->formatType = CRYPT_FORMAT_CRYPTLIB; \
( dlpDataPtr )->inParam1 = ( dataIn ); \
( dlpDataPtr )->inLen1 = ( dataInLen ); \
( dlpDataPtr )->outParam = ( dataOut ); \
( dlpDataPtr )->outLen = ( dataOutLen ); \
}
/* When calling key agreement functions we have to pass a mass of cruft
around instead of the usual flat data (even more than the generic DLP
parameter information) for which we use the following structure. The
public value is the public key value used for the agreement process,
typically y = g^x mod p for DH-like mechanisms. The ukm is the user
keying material, typically something which is mixed into the DH process
to make the new key unique. The wrapped key is the output (originator)/
input(recipient) to the keyagreement process. The session key context
contains a context into which the derived key is loaded. Typical
examples of use are:
PKCS #3: publicValue = y
Fortezza: publicValue = y, ukm = Ra, wrappedKey = TEK-wrapped MEK
S/MIME: publicValue = y, ukm = 512-bit nonce, wrappedKey = g^x mod p
SSH, SSL: publicValue = y, wrappedKey = x */
typedef struct {
BYTE publicValue[ CRYPT_MAX_PKCSIZE + 8 ];
int publicValueLen; /* Public key value */
#ifdef USE_FORTEZZA
BYTE ukm[ CRYPT_MAX_PKCSIZE + 8 ];
int ukmLen; /* User keying material */
CRYPT_CONTEXT sessionKeyContext;/* Context for derived key */
#endif /* USE_FORTEZZA */
BYTE wrappedKey[ CRYPT_MAX_PKCSIZE + 8 ];
int wrappedKeyLen; /* Wrapped key */
} KEYAGREE_PARAMS;
/****************************************************************************
* *
* Useful General Macros *
* *
****************************************************************************/
/* Reasonably reliable way to get rid of unused argument warnings in a
compiler-independant manner */
#define UNUSED( arg ) ( ( arg ) = ( arg ) )
/* Although min() and max() aren't in the ANSI standard, most stdlib.h's have
them anyway for historical reasons. Just in case they're not defined
there by some pedantic compiler (some versions of Borland C do this), we
define them here */
#ifndef max
#define max( a, b ) ( ( ( a ) > ( b ) ) ? ( ( int ) ( a ) ) : \
( ( int ) ( b ) ) )
#endif /* !max */
#ifndef min
#define min( a, b ) ( ( ( a ) < ( b ) ) ? ( ( int ) ( a ) ) : \
( ( int ) ( b ) ) )
#endif /* !min */
/* Macros to convert to and from the bit counts used for some encryption
parameters */
#define bitsToBytes( bits ) ( ( ( bits ) + 7 ) >> 3 )
#define bytesToBits( bytes ) ( ( bytes ) << 3 )
/* Macro to round a value up to the nearest multiple of a second value,
with the second value being a power of 2 */
#define roundUp( size, roundSize ) \
( ( ( size ) + ( ( roundSize ) - 1 ) ) & ~( ( roundSize ) - 1 ) )
/* A macro to clear sensitive data from memory. This is somewhat easier to
use than calling memset with the second parameter set to 0 all the time,
and makes it obvious where sensitive data is being erased */
#define zeroise( memory, size ) memset( memory, 0, size )
/* A macro to check that a value is a possibly valid handle. This doesn't
check that the handle refers to a valid object, merely that the value is
in the range for valid handles. The full function isValidHandle() used
in the kernel does check that the handle refers to a valid object, being
more than just a range check */
#define isHandleRangeValid( handle ) \
( ( handle ) > NO_SYSTEM_OBJECTS - 1 && ( handle ) < MAX_OBJECTS )
/* A macro to check whether an encryption mode needs an IV or not */
#define needsIV( mode ) ( ( mode ) == CRYPT_MODE_CBC || \
( mode ) == CRYPT_MODE_CFB || \
( mode ) == CRYPT_MODE_OFB )
/* A macro to check whether an algorithm is a pure stream cipher (that is,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -