home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / CAPI.H next >
Text File  |  1996-10-10  |  30KB  |  777 lines

  1. #ifndef _CAPI_DEFINED
  2.  
  3. #define _CAPI_DEFINED
  4.  
  5. /* Fixup for Windows support.  We need to include windows.h for various types
  6.    and prototypes needed for DLL's */
  7.  
  8. #if defined( _WINDOWS ) || defined( WIN32 ) || defined( _WIN32 )
  9.   #define WIN32_LEAN_AND_MEAN        /* Skip RPC, OLE, Multimedia, etc */
  10.   #include <windows.h>
  11. #endif /* _WINDOWS || WIN32 || _WIN32 */
  12.  
  13. /* Machine-dependant types to allow use in special library types such as
  14.    DLL's.  Under Win32 we need to use the dllimport and dllexport directives 
  15.    for the DLL version of the library, so we define the type used for 
  16.    functions depending on whether we're being included via crypt.h or not */
  17.  
  18. #if ( defined( WIN32 ) || defined( _WIN32 ) )
  19.   #define CPTR    *                    /* General pointer */
  20.   #ifdef _CRYPT_DEFINED
  21.     #define CRET    __declspec( dllexport ) int    /* DLL export return value */
  22.   #else
  23.     #define CRET    __declspec( dllimport ) int    /* DLL import return value */
  24.   #endif /* CRYPT_DEFINED */
  25. #elif defined( _WINDOWS )
  26.   #define CPTR    FAR *                /* DLL pointer */
  27.   #define CRET    int FAR PASCAL _export    /* DLL return value */
  28. #else
  29.   #define CPTR    *                    /* General pointer */
  30.   #define CRET    int                    /* General return value */
  31. #endif /* _WINDOWS && !( WIN32 || _WIN32 ) */
  32.  
  33. /****************************************************************************
  34. *                                                                            *
  35. *                        Encryption Algorithm Types/Modes                    *
  36. *                                                                            *
  37. ****************************************************************************/
  38.  
  39. /* The encryption algorithms we can use */
  40.  
  41. typedef enum {
  42.     /* No encryption */
  43.     CRYPT_ALGO_NONE,                /* No encryption */
  44.  
  45.     /* Conventional encryption */
  46.     CRYPT_ALGO_DES,                    /* DES */
  47.     CRYPT_ALGO_3DES,                /* Triple DES */
  48.     CRYPT_ALGO_IDEA,                /* IDEA */
  49.     CRYPT_ALGO_MDCSHS,                /* MDC/SHS */
  50.     CRYPT_ALGO_RC2,                    /* RC2 */
  51.     CRYPT_ALGO_RC4,                    /* RC4 */
  52.     CRYPT_ALGO_RC5,                    /* RC5 */
  53.     CRYPT_ALGO_SAFER,                /* SAFER/SAFER-SK */
  54.     CRYPT_ALGO_BLOWFISH,            /* Blowfish/Blowfish-SK */
  55.     CRYPT_ALGO_GOST,                /* GOST 28147 */
  56.     CRYPT_ALGO_SKIPJACK,            /* It's only a matter of time... */
  57.  
  58.     /* Public-key encryption */
  59.     CRYPT_ALGO_DH = 100,            /* Diffie-Hellman */
  60.     CRYPT_ALGO_RSA,                    /* RSA */
  61.     CRYPT_ALGO_DSA,                    /* DSA */
  62.  
  63.     /* Hash/MAC algorithms */
  64.     CRYPT_ALGO_MD2 = 200,            /* MD2 */
  65.     CRYPT_ALGO_MD4,                    /* MD4 */
  66.     CRYPT_ALGO_MD5,                    /* MD5 */
  67.     CRYPT_ALGO_SHA,                    /* SHA/SHA1 */
  68.     CRYPT_ALGO_RIPEMD160,            /* RIPE-MD 160 */
  69.  
  70.     CRYPT_ALGO_LAST,                /* Last possible crypt algo value */
  71.  
  72.     /* In order that we can scan through a range of algorithms with
  73.        cryptAlgoAvailable(), we define the following boundary points for each
  74.        algorithm class */
  75.     CRYPT_ALGO_FIRST_CONVENTIONAL = CRYPT_ALGO_DES,
  76.     CRYPT_ALGO_LAST_CONVENTIONAL = CRYPT_ALGO_DH - 1,
  77.     CRYPT_ALGO_FIRST_PKC = CRYPT_ALGO_DH,
  78.     CRYPT_ALGO_LAST_PKC = CRYPT_ALGO_MD2 - 1,
  79.     CRYPT_ALGO_FIRST_HASH = CRYPT_ALGO_MD2,
  80.     CRYPT_ALGO_LAST_HASH = CRYPT_ALGO_MD2 + 99    /* End of hash algo.range */
  81.     } CRYPT_ALGO;
  82.  
  83. /* The encryption modes we can use */
  84.  
  85. typedef enum {
  86.     /* No encryption */
  87.     CRYPT_MODE_NONE,                /* No encryption */
  88.  
  89.     /* Stream cipher modes */
  90.     CRYPT_MODE_STREAM,                /* Stream cipher */
  91.  
  92.     /* Block cipher modes */
  93.     CRYPT_MODE_ECB,                    /* ECB */
  94.     CRYPT_MODE_CBC,                    /* CBC */
  95.     CRYPT_MODE_CFB,                    /* CFB */
  96.     CRYPT_MODE_OFB,                    /* OFB */
  97.     CRYPT_MODE_PCBC,                /* PCBC */
  98.     CRYPT_MODE_COUNTER,                /* KSG/counter */
  99.  
  100.     /* Public-key cipher modes */
  101.     CRYPT_MODE_PKC = 100,            /* PKC */
  102.  
  103.     CRYPT_MODE_LAST,                /* Last possible crypt mode value */
  104.  
  105.     /* In order that we can scan through a range of modes with
  106.        cryptAlgoModeAvailable(), we define the following boundary points for
  107.        the conventional encryption modes */
  108.     CRYPT_MODE_FIRST_CONVENTIONAL = CRYPT_MODE_STREAM,
  109.     CRYPT_MODE_LAST_CONVENTIONAL = CRYPT_MODE_PKC - 1
  110.     } CRYPT_MODE;
  111.  
  112. /* The encryption object types we recognise */
  113.  
  114. typedef enum {
  115.     CRYPT_OBJECT_NONE,                /* No object type */
  116.     CRYPT_OBJECT_ENCRYPTED_KEY,        /* Conventional-encrypted key */
  117.     CRYPT_OBJECT_PKCENCRYPTED_KEY,    /* PKC-encrypted key */
  118.     CRYPT_OBJECT_SIGNATURE,            /* Signature */
  119.     CRYPT_OBJECT_ENCRYPTED_DATA,    /* Encrypted data */
  120.     CRYPT_OBJECT_COMPRESSED_DATA,    /* Compressed data */
  121.     CRYPT_OBJECT_SIGNED_DATA,        /* Signed data */
  122.     CRYPT_OBJECT_RAW_DATA,            /* Raw data */
  123.     CRYPT_OBJECT_NONDATA,            /* Non-data (used for padding) */
  124.     CRYPT_OBJECT_LAST                /* Last possible object type */
  125.     } CRYPT_OBJECT_TYPE;
  126.  
  127. /* The keyset types */
  128.  
  129. typedef enum {
  130.     CRYPT_KEYSET_NONE,                /* No keyset type */
  131.     CRYPT_KEYSET_PGP,                /* PGP key ring */
  132.     CRYPT_KEYSET_X509,                /* X.509/SET key certificate */
  133.     CRYPT_KEYSET_LAST                /* Last possible keyset type */
  134.     } CRYPT_KEYSET_TYPE;
  135.  
  136. /* The certificate information types */
  137.  
  138. typedef enum {
  139.     /* No information */
  140.     CRYPT_CERTINFO_NONE,            /* No certificate information */
  141.  
  142.     /* General certificate information */
  143.     CRYPT_CERTINFO_SERIALNUMBER,    /* serialNumber */
  144.     CRYPT_CERTINFO_VALIDITY,        /* validity */
  145.     CRYPT_CERTINFO_UNIQUEIDENTIFIER,/* uniqueIdentifier (deprecated) */
  146.  
  147.     /* X.520 Distinguished Name components */
  148.     CRYPT_CERTINFO_COUNTRYNAME = 100,    /* countryName */
  149.     CRYPT_CERTINFO_ORGANIZATIONNAME,    /* organizationName */
  150.     CRYPT_CERTINFO_ORGANIZATIONALUNITNAME,    /* organizationalUnitName */
  151.     CRYPT_CERTINFO_COMMONNAME,        /* commonName */
  152.  
  153.     /* X.509v3 GeneralName extensions */
  154.     CRYPT_CERTINFO_OTHERNAME = 200,    /* OTHER-NAME */
  155.     CRYPT_CERTINFO_RFC822NAME,        /* rfc822Name */
  156.     CRYPT_CERTINFO_DNSNAME,            /* dNSName */
  157.     CRYPT_CERTINFO_X400ADDRESS,        /* x400Address */
  158.     CRYPT_CERTINFO_DIRECTORYNAME,    /* directoryName */
  159.     CRYPT_CERTINFO_EDIPARTYNAME,    /* ediPartyName */
  160.     CRYPT_CERTINFO_UNIFORMRESOURCEIDENTIFIER,    /* uniformResourceIdentifier */
  161.     CRYPT_CERTINFO_IPADDRESS,        /* iPAddress */
  162.     CRYPT_CERTINFO_REGISTEREDID,    /* registeredID */
  163.  
  164.     /* SET extensions */
  165.     CRYPT_CERTINFO_ACCOUNTALIAS = 300,    /* accountAlias */
  166.     CRYPT_CERTINFO_HASHEDROOTKEY,    /* hashedRootKey */
  167.     CRYPT_CERTINFO_CERTIFICATETYPE,    /* certificateType */
  168.     CRYPT_CERTINFO_MERCHANTDATA        /* merchantData */
  169.     } CRYPT_CERTINFO_TYPE;
  170.  
  171. /****************************************************************************
  172. *                                                                            *
  173. *                    Library-Wide Constants and Definitions                    *
  174. *                                                                            *
  175. ****************************************************************************/
  176.  
  177. /* The maximum user key size - 2048 bits */
  178.  
  179. #define CRYPT_MAX_KEYSIZE        256
  180.  
  181. /* The maximum IV size - 64 bits */
  182.  
  183. #define CRYPT_MAX_IVSIZE        8
  184.  
  185. /* The maximum public-key component size - 4096 bits */
  186.  
  187. #define CRYPT_MAX_PKCSIZE        512
  188.  
  189. /* The maximum hash size - 256 bits */
  190.  
  191. #define CRYPT_MAX_HASHSIZE        64
  192.  
  193. /* The maximum size of a text string */
  194.  
  195. #define CRYPT_MAX_TEXTSIZE        128
  196.  
  197. /* The maximum size of a key ID */
  198.  
  199. #define CRYPT_MAX_KEYIDSIZE        80
  200.  
  201. /* The maximum speed ratio for an encryption algorithm */
  202.  
  203. #define CRYPT_MAX_SPEED            1000
  204.  
  205. /* A magic value indicating that the default setting for this parameter
  206.    should be used */
  207.  
  208. #define CRYPT_USE_DEFAULT        -1
  209.  
  210. /* A magic value for unused parameters */
  211.  
  212. #define CRYPT_UNUSED            -2
  213.  
  214. /* The endianness of the external components of the PKC key */
  215.  
  216. #define CRYPT_COMPONENTS_BIGENDIAN        0
  217. #define CRYPT_COMPONENTS_LITTLENDIAN    1
  218.  
  219. /* Whether the PKC key is a public or private key */
  220.  
  221. #define CRYPT_KEYTYPE_PRIVATE    0
  222. #define CRYPT_KEYTYPE_PUBLIC    1
  223.  
  224. /* The type of information polling to perform to get random seed information */
  225.  
  226. #define CRYPT_RANDOM_FASTPOLL    -1
  227. #define CRYPT_RANDOM_SLOWPOLL    -2
  228.  
  229. /* The keyset access modes */
  230.  
  231. #define CRYPT_ACCESS_READ        1
  232. #define CRYPT_ACCESS_WRITE        2
  233. #define CRYPT_ACCESS_CREATE        4
  234.  
  235. /* Special userID's for getFirst()/getNext() functionality in keysets */
  236.  
  237. #define CRYPT_KEYSET_GETFIRST    ( ( void CPTR ) -10 )
  238. #define CRYPT_KEYSET_GETNEXT    ( ( void CPTR ) -11 )
  239.  
  240. /* Macros to convert to and from the bit counts used for some encryption
  241.    parameters */
  242.  
  243. #define bitsToBytes(bits)    ( ( ( bits ) + 7 ) >> 3 )
  244. #define bytesToBits(bytes)    ( ( bytes ) << 3 )
  245.  
  246. /* The encryption context, public/private key collection, certificate, and
  247.    envelope.  These are opaque data types used internally by the library.
  248.    Although declared as a char *, they don't point to anything and should not
  249.    be treated as a pointer (the use of char * rather than the more logical
  250.    void * allows for better type checking by the compiler.
  251.    "The way is void" - Musashi) */
  252.  
  253. typedef char CPTR CRYPT_CONTEXT;
  254. typedef char CPTR CRYPT_KEYSET;
  255. typedef char CPTR CRYPT_CERT;
  256. typedef char CPTR CRYPT_ENVELOPE;
  257.  
  258. /****************************************************************************
  259. *                                                                            *
  260. *                            Encryption Data Structures                        *
  261. *                                                                            *
  262. ****************************************************************************/
  263.  
  264. /* Results returned from the encryption capability query */
  265.  
  266. typedef struct {
  267.     /* The algorithm, encryption mode, and algorithm and mode names */
  268.     CRYPT_ALGO cryptAlgo;            /* The encryption algorithm */
  269.     CRYPT_MODE cryptMode;            /* The encryption mode */
  270.     char *algoName;                    /* The algorithm name */
  271.     char *modeName;                    /* The mode name */
  272.  
  273.     /* The algorithm parameters */
  274.     int blockSize;                    /* The basic block size of the algorithm */
  275.     int minKeySize;                    /* Minimum key size in bytes */
  276.     int keySize;                    /* Recommended key size in bytes */
  277.     int maxKeySize;                    /* Maximum key size in bytes */
  278.     int minIVsize;                    /* Minimum IV size in bytes */
  279.     int ivSize;                        /* Recommended IV size in bytes */
  280.     int maxIVsize;                    /* Maximum IV size in bytes */
  281.  
  282.     /* Various algorithm characteristics */
  283.     int speed;                        /* Speed relative to block copy */
  284.  
  285.     /* Miscellaneous information (only used by some algorithms) */
  286.     unsigned char hashValue[ CRYPT_MAX_HASHSIZE ];
  287.                                     /* Hash algoriithm hash value */
  288.     unsigned char keyID[ CRYPT_MAX_KEYIDSIZE ];    /* PKC key ID */
  289.     int keyIDsize;                    /* PKC key ID length */
  290.     long controlVector;                /* Conventional key control vector */
  291.     } CRYPT_QUERY_INFO;
  292.  
  293. /* Results returned from the encryption object query */
  294.  
  295. typedef struct {
  296.     /* The object type and size information */
  297.     CRYPT_OBJECT_TYPE type;            /* The object type */
  298.     long size;                        /* The object size */
  299.     int headerSize;                    /* Size of the objects header */
  300.     long payloadSize;                /* Size of the objects payload */
  301.  
  302.     /* The encryption algorithm and mode for EncryptedKey, PKCEncryptedKey,
  303.        Signature, and SignedData objects */
  304.     CRYPT_ALGO cryptAlgo;            /* The encryption algorithm */
  305.     CRYPT_MODE cryptMode;            /* The encryption mode */
  306.  
  307.     /* The key ID for PKCEncryptedKey and Signature objects */
  308.     unsigned char keyID[ CRYPT_MAX_KEYIDSIZE ];    /* PKC key ID */
  309.     unsigned char keyIDsize;        /* PKC key ID length */
  310.  
  311.     /* The key derivation algorithm and iteration count for EncryptedKey
  312.        objects */
  313.     CRYPT_ALGO keySetupAlgo;        /* Key setup algorithm */
  314.     int keySetupIterations;            /* Key setup iteration count */
  315.  
  316.     /* The control vector and algorithm-specific information for
  317.        EncryptedKey objects.  The algorithm-specific information can be
  318.        passed directly to cryptCreateContextEx() for any algorithm (even
  319.        those which would normally use cryptCreateContext()) */
  320.     long controlVector;                /* Conventional key control vector */
  321.     void *cryptContextExInfo;        /* Algo-specific information */
  322.  
  323.     /* The cookie for EncryptedKey, PKCEncryptedKey, EncryptedData, and
  324.        SignedData objects */
  325.     unsigned char cookie[ CRYPT_MAX_HASHSIZE ];
  326.     int cookieSize;
  327.  
  328.     /* The IV for EncryptedData objects */
  329.     unsigned char iv[ CRYPT_MAX_IVSIZE ];    /* IV */
  330.     int ivSize;                        /* Length of the IV */
  331.     } CRYPT_OBJECT_INFO;
  332.  
  333. /* Extra algorithm-specific information for the hash algorithms, stored
  334.    within a crypt context.  Set the parameter values to CRYPT_USE_DEFAULT
  335.    to use the default values for this algorithm */
  336.  
  337. typedef struct {
  338.     int isSHA;                    /* Whether to use SHA rather than SHA1 */
  339.     } CRYPT_INFO_SHA;
  340.  
  341. /* Extra algorithm-specific information for the conventional encryption
  342.    algorithms, stored within a crypt context.  Set the parameter values to
  343.    CRYPT_USE_DEFAULT to use the default values for this algorithm */
  344.  
  345. typedef struct {
  346.     int isDESX;                    /* Whether to use the DESX variant */
  347.     } CRYPT_INFO_DES;
  348.  
  349. typedef struct {
  350.     int isThreeKey;                /* Whether to use three-key variant */
  351.     } CRYPT_INFO_3DES;
  352.  
  353. typedef struct {
  354.     int keySetupIterations;        /* No.iterations for user key setup */
  355.     } CRYPT_INFO_MDCSHS;
  356.  
  357. typedef struct {
  358.     int rounds;                    /* Number of encryption rounds */
  359.     } CRYPT_INFO_RC5;
  360.  
  361. typedef struct {
  362.     int useSaferSK;                /* Whether to use strengthened-key version */
  363.     int rounds;                    /* Number of encryption rounds */
  364.     } CRYPT_INFO_SAFER;
  365.  
  366. typedef struct {
  367.     int useBlowfishSK;            /* Whether to use strengthened-key version */
  368.     int keySetupIterations;        /* No.iterations for SK user key setup */
  369.     } CRYPT_INFO_BLOWFISH;
  370.  
  371. /* Key information for the public-key encryption algorithms.  These fields
  372.    are not accessed directly, but can be manipulated with the init/set/get/
  373.    destroyComponents() macros */
  374.  
  375. typedef struct {
  376.     /* Status information */
  377.     int endianness;                /* Endianness of integer strings */
  378.     int isPublicKey;            /* Whether this is a public or private key */
  379.  
  380.     /* Public components */
  381.     unsigned char p[ CRYPT_MAX_PKCSIZE ];    /* Prime */
  382.     int pLen;                    /* Length of prime in bits */
  383.     unsigned char g[ CRYPT_MAX_PKCSIZE ];    /* Base */
  384.     int gLen;                    /* Length of base in bits */
  385.     } CRYPT_PKCINFO_DH;
  386.  
  387. typedef struct {
  388.     /* Status information */
  389.     int endianness;                /* Endianness of integer strings */
  390.     int isPublicKey;            /* Whether this is a public or private key */
  391.  
  392.     /* Public components */
  393.     unsigned char n[ CRYPT_MAX_PKCSIZE ];    /* Modulus */
  394.     int nLen;                    /* Length of modulus in bits */
  395.     unsigned char e[ CRYPT_MAX_PKCSIZE ];    /* Public exponent */
  396.     int eLen;                    /* Length of public exponent in bits */
  397.  
  398.     /* Private components */
  399.     unsigned char d[ CRYPT_MAX_PKCSIZE ];    /* Private exponent */
  400.     int dLen;                    /* Length of private exponent in bits */
  401.     unsigned char p[ CRYPT_MAX_PKCSIZE ];    /* Prime factor 1 */
  402.     int pLen;                    /* Length of prime factor 1 in bits */
  403.     unsigned char q[ CRYPT_MAX_PKCSIZE ];    /* Prime factor 2 */
  404.     int qLen;                    /* Length of prime factor 2 in bits */
  405.     unsigned char u[ CRYPT_MAX_PKCSIZE ];    /* Mult.inverse of q, mod p */
  406.     int uLen;                    /* Length of private exponent in bits */
  407.     unsigned char e1[ CRYPT_MAX_PKCSIZE ];    /* Private exponent 1 (PKCS) */
  408.     int e1Len;                    /* Length of private exponent in bits */
  409.     unsigned char e2[ CRYPT_MAX_PKCSIZE ];    /* Private exponent 2 (PKCS) */
  410.     int e2Len;                    /* Length of private exponent in bits */
  411.     } CRYPT_PKCINFO_RSA;
  412.  
  413. typedef struct {
  414.     /* Status information */
  415.     int endianness;                /* Endianness of integer strings */
  416.     int isPublicKey;            /* Whether this is a public or private key */
  417.  
  418.     /* Public components */
  419.     unsigned char p[ CRYPT_MAX_PKCSIZE ];    /* Prime modulus */
  420.     int pLen;                    /* Length of prime modulus in bits */
  421.     unsigned char q[ CRYPT_MAX_PKCSIZE ];    /* Prime divisor */
  422.     int qLen;                    /* Length of prime divisor in bits */
  423.     unsigned char g[ CRYPT_MAX_PKCSIZE ];    /* h^( ( p - 1 ) / q ) mod p */
  424.     int gLen;                    /* Length of g in bits */
  425.     unsigned char x[ CRYPT_MAX_PKCSIZE ];    /* Public random integer */
  426.     int xLen;                    /* Length of public integer in bits */
  427.  
  428.     /* Private components */
  429.     unsigned char y[ CRYPT_MAX_PKCSIZE ];    /* Private random integer */
  430.     int yLen;                    /* Length of private integer in bits */
  431.     } CRYPT_PKCINFO_DSA;
  432.  
  433. /* Macros to initialise and destroy the structure which stores the components
  434.    of a public key */
  435.  
  436. #define cryptInitComponents( componentInfo, componentEndianness, componentKeyType ) \
  437.     { memset( componentInfo, 0, sizeof( *componentInfo ) ); \
  438.       componentInfo##->endianness = componentEndianness; \
  439.       componentInfo##->isPublicKey = componentKeyType; }
  440.  
  441. #define cryptDestroyComponents( componentInfo ) \
  442.     memset( componentInfo, 0, sizeof( *componentInfo ) )
  443.  
  444. /* Macros to set and get a component of the public key */
  445.  
  446. #define cryptSetComponent( destination, source, length ) \
  447.     { memcpy( destination, source, bitsToBytes( length ) ); \
  448.       destination##Len = length; }
  449.  
  450. #define cryptGetComponent( destination, source, length ) \
  451.     { memcpy( source, destination, bitsToBytes( destination##Len ) ); \
  452.       length = destination##Len; }
  453.  
  454. /****************************************************************************
  455. *                                                                            *
  456. *                        Crypt IOCTL Structures and Constants                *
  457. *                                                                            *
  458. ****************************************************************************/
  459.  
  460. /* The various IOCTL types */
  461.  
  462. typedef enum {
  463.     CRYPT_IOCTL_NONE,                /* Null IOCTL */
  464.     CRYPT_IOCTL_MEMORY,                /* Control page locking */
  465.     CRYPT_IOCTL_KEYCOOKIE,            /* Control key cookie export */
  466.     CRYPT_IOCTL_SIGCOOKIE,            /* Control signature cookie export */
  467.     CRYPT_IOCTL_PKCPADDING,            /* Control PKC encryption padding type */
  468.     CRYPT_IOCTL_KEYSETNAMES,        /* Control keyset table names */
  469.     CRYPT_IOCTL_LAST                /* Last possible IOCTL type */
  470.     } CRYPT_IOCTL;
  471.  
  472. /* The data structure and constants for CRYPT_IOCTL_MEMORY */
  473.  
  474. enum { CRYPT_MEMORY_NOLOCK, CRYPT_MEMORY_LOCK, CRYPT_MEMORY_FORCELOCK };
  475.  
  476. typedef struct {
  477.     int memoryLockType;                /* What to do with memory blocks */
  478.     } CRYPT_IOCTLINFO_MEMORY;
  479.  
  480. /* The data structure for CRYPT_IOCTL_KEYCOOKIE and CRYPT_IOCTL_SIGCOOKIE */
  481.  
  482. typedef struct {
  483.     int exportCookie;                /* Whether to export cookies or not */
  484.     } CRYPT_IOCTLINFO_COOKIE;
  485.  
  486. /* The data structure for CRYPT_IOCTL_PKCPADDING */
  487.  
  488. typedef struct {
  489.     int useOAEP;                    /* Whether to use optimal asymmetric
  490.                                        encryption padding */
  491.     } CRYPT_IOCTLINFO_PKCPADDING;
  492.  
  493. /* The data structure for CRYPT_IOCTL_KEYSETNAMES */
  494.  
  495. typedef struct {
  496.     char *userID;                    /* Name of user ID table */
  497.     char *keyID;                    /* Name of key ID table */
  498.     char *publicKey;                /* Name of public key table */
  499.     char *privateKey;                /* Name of private key table */
  500.     } CRYPT_IOCTLINFO_KEYSETNAMES;
  501.  
  502. /****************************************************************************
  503. *                                                                            *
  504. *                                Status Codes                                *
  505. *                                                                            *
  506. ****************************************************************************/
  507.  
  508. /* No error in function call */
  509.  
  510. #define CRYPT_OK            0        /* No error */
  511.  
  512. /* Internal errors */
  513.  
  514. #define CRYPT_ERROR            -1        /* Nonspecific error */
  515. #define CRYPT_SELFTEST        -2        /* Failed self-test */
  516.  
  517. /* Error in parameters passed to function */
  518.  
  519. #define CRYPT_BADPARM        -10        /* Generic bad argument to function */
  520. #define CRYPT_BADPARM1        -11        /* Bad argument, parameter 1 */
  521. #define CRYPT_BADPARM2        -12        /* Bad argument, parameter 2 */
  522. #define CRYPT_BADPARM3        -13        /* Bad argument, parameter 3 */
  523. #define CRYPT_BADPARM4        -14        /* Bad argument, parameter 4 */
  524. #define CRYPT_BADPARM5        -15        /* Bad argument, parameter 5 */
  525. #define CRYPT_BADPARM6        -16        /* Bad argument, parameter 6 */
  526. #define CRYPT_BADPARM7        -17        /* Bad argument, parameter 7 */
  527. #define CRYPT_BADPARM8        -18        /* Bad argument, parameter 8 */
  528. #define CRYPT_BADPARM9        -19        /* Bad argument, parameter 9 */
  529. #define CRYPT_BADPARM10        -20        /* Bad argument, parameter 10 */
  530. #define CRYPT_BADPARM11        -21        /* Bad argument, parameter 11 */
  531. #define CRYPT_BADPARM12        -22        /* Bad argument, parameter 12 */
  532. #define CRYPT_BADPARM13        -23        /* Bad argument, parameter 13 */
  533. #define CRYPT_BADPARM14        -24        /* Bad argument, parameter 14 */
  534. #define CRYPT_BADPARM15        -25        /* Bad argument, parameter 15 */
  535.  
  536. /* Errors due to insufficient resources */
  537.  
  538. #define CRYPT_NOMEM            -50        /* Out of memory */
  539. #define CRYPT_NOTINITED        -51        /* Data has not been initialised */
  540. #define CRYPT_INITED        -52        /* Data has already been initialised */
  541. #define CRYPT_NOALGO        -53        /* Algorithm unavailable */
  542. #define CRYPT_NOMODE        -54        /* Encryption mode unavailable */
  543. #define CRYPT_NOKEY            -55        /* Key not initialised */
  544. #define CRYPT_NOIV            -56        /* IV not initialised */
  545. #define CRYPT_NOLOCK        -57        /* Unable to lock pages in memory */
  546. #define CRYPT_NORANDOM        -58        /* No reliable random data available */
  547.  
  548. /* CAPI security violations */
  549.  
  550. #define CRYPT_NOTAVAIL        -100    /* Operation not available for this algo/mode */
  551. #define CRYPT_KEYPERM        -101    /* No key permissions for this operation */
  552. #define CRYPT_WRONGKEY        -102    /* Incorrect key used to decrypt data */
  553. #define CRYPT_INCOMPLETE    -103    /* Operation incomplete/still in progress */
  554. #define CRYPT_COMPLETE        -104    /* Operation complete/can't continue */
  555. #define CRYPT_ORPHAN        -105    /* Encryption contexts remained allocated */
  556.  
  557. /* High-level function errors */
  558.  
  559. #define CRYPT_DATASIZE        -150    /* Too much data supplied to function */
  560. #define CRYPT_PKCCRYPT        -151    /* PKC en/decryption failed */
  561. #define CRYPT_BADDATA        -152    /* Bad data format in object */
  562. #define CRYPT_BADSIG        -153    /* Bad signature on data */
  563.  
  564. /* Key collection errors */
  565.  
  566. #define CRYPT_KEYSET_OPEN        -200    /* Cannot open key set */
  567. #define CRYPT_KEYSET_NOTFOUND    -201    /* Key or key info not found in key set */
  568. #define CRYPT_KEYSET_DUPLICATE    -202    /* Key already present in key set */
  569. #define CRYPT_KEYSET_UPDATE        -203    /* Cannot update key set */
  570.  
  571. /* Certificate errors */
  572.  
  573. #define CRYPT_CERT_NONE        -250    /* No errors defined yet */
  574.  
  575. /* Macros to examine return values */
  576.  
  577. #define cryptStatusError( status )    ( ( status ) < CRYPT_OK )
  578. #define cryptStatusOK( status )        ( ( status ) == CRYPT_OK )
  579. #define cryptIsParamError( status )    ( ( status ) >= CRYPT_BADPARM && \
  580.                                       ( status ) < CRYPT_NOMEM )
  581. #define cryptIsResourceError( status ) \
  582.                                     ( ( status ) >= CRYPT_NOMEM && \
  583.                                       ( status ) < CRYPT_NOTAVAIL )
  584. #define cryptIsSecurityError( status ) \
  585.                                     ( ( status ) >= CRYPT_NOTAVAIL && \
  586.                                       ( status ) < CRYPT_DATASIZE )
  587. #define cryptIsHighlevelError( status ) \
  588.                                     ( ( status ) >= CRYPT_DATASIZE && \
  589.                                       ( status ) < CRYPT_KEYSET_ORPHAN )
  590. #define cryptIsKeysetError( status ) \
  591.                                     ( ( status ) >= CRYPT_KEYSET_OPEN && \
  592.                                       ( status ) < CRYPT_CERT_NONE )
  593. #define cryptIsCertError( status ) \
  594.                                     ( ( status ) >= CRYPT_CERT_NONE && \
  595.                                       ( status ) <= CRYPT_CERT_NONE )
  596.  
  597. /****************************************************************************
  598. *                                                                            *
  599. *                                Encryption Functions                        *
  600. *                                                                            *
  601. ****************************************************************************/
  602.  
  603. /* Initialise and shut down the encryption library */
  604.  
  605. CRET cryptInit( void );
  606. CRET cryptEnd( void );
  607.  
  608. /* Query the capabilities of the encryption library */
  609.  
  610. CRET cryptModeAvailable( const CRYPT_ALGO cryptAlgo, \
  611.                          const CRYPT_MODE cryptMode );
  612. CRET cryptAlgoAvailable( const CRYPT_ALGO cryptAlgo );
  613. CRET cryptQueryAlgoMode( const CRYPT_ALGO cryptAlgo, \
  614.                          const CRYPT_MODE cryptMode, \
  615.                          CRYPT_QUERY_INFO CPTR cryptQueryInfo );
  616. CRET cryptQueryContext( const CRYPT_CONTEXT cryptContext, \
  617.                         CRYPT_QUERY_INFO CPTR cryptQueryInfo );
  618.  
  619. /* Initialise and destroy an encryption context */
  620.  
  621. CRET cryptCreateContextEx( CRYPT_CONTEXT CPTR cryptContext, \
  622.                            const CRYPT_ALGO cryptAlgo, \
  623.                            const CRYPT_MODE cryptMode, \
  624.                            const void CPTR cryptContextEx );
  625. CRET cryptDestroyContext( CRYPT_CONTEXT cryptContext );
  626.  
  627. #define cryptCreateContext( cryptContext, cryptAlgo, cryptMode ) \
  628.         cryptCreateContextEx( cryptContext, cryptAlgo, cryptMode, \
  629.                               ( void * ) CRYPT_UNUSED )
  630.  
  631. /* Load a user key into an encryption context, add random data to the random
  632.    pool, generate a session key from the random pool into a context, or
  633.    derive an encryption key from a variable-length user key */
  634.  
  635. CRET cryptLoadContext( CRYPT_CONTEXT cryptContext, void CPTR key, \
  636.                        const int keyLength );
  637. CRET cryptAddRandom( void CPTR randomData, int randomDataLength );
  638. CRET cryptGenerateContextEx( CRYPT_CONTEXT cryptContext, const int keyLength );
  639. CRET cryptDeriveKeyEx( CRYPT_CONTEXT cryptContext, const void CPTR userKey, \
  640.                        const int userKeyLength, const CRYPT_ALGO algorithm, \
  641.                        const int iterations );
  642.  
  643. #define cryptGenerateContext( cryptContext ) \
  644.         cryptGenerateContextEx( cryptContext, CRYPT_USE_DEFAULT )
  645. #define cryptDeriveKey( cryptContext, userKey, userKeyLength ) \
  646.         cryptDeriveKeyEx( cryptContext, userKey, userKeyLength, \
  647.                           CRYPT_USE_DEFAULT, CRYPT_USE_DEFAULT )
  648.  
  649. /* Load/retrieve an IV into/from an encryption context */
  650.  
  651. CRET cryptLoadIV( CRYPT_CONTEXT cryptContext, const void CPTR iv, \
  652.                   const int length );
  653. CRET cryptRetrieveIV( CRYPT_CONTEXT cryptContext, void CPTR iv );
  654.  
  655. /* Encrypt/decrypt/hash a block of memory */
  656.  
  657. CRET cryptEncrypt( CRYPT_CONTEXT cryptContext, void CPTR buffer, \
  658.                    const int length );
  659. CRET cryptDecrypt( CRYPT_CONTEXT cryptContext, void CPTR buffer, \
  660.                    const int length );
  661.  
  662. /* Crypt library IOCTL */
  663.  
  664. CRET cryptIoctl( CRYPT_IOCTL ioctlCode, void CPTR ioctlInformation,
  665.                  CRYPT_CONTEXT cryptContext );
  666.  
  667. /****************************************************************************
  668. *                                                                            *
  669. *                                Key Managment Functions                        *
  670. *                                                                            *
  671. ****************************************************************************/
  672.  
  673. /* Open and close a keyset */
  674.  
  675. CRET cryptKeysetOpenEx( CRYPT_KEYSET CPTR keySet, const char CPTR name, \
  676.                         const int accessMode, const char CPTR user, \
  677.                         const char CPTR password );
  678. CRET cryptExtKeysetOpen( CRYPT_KEYSET CPTR keySet, const char CPTR name, \
  679.                          const CRYPT_KEYSET_TYPE type );
  680. CRET cryptKeysetClose( CRYPT_KEYSET keySet );
  681.  
  682. #define cryptKeysetOpen( keySet, name, accessMode ) \
  683.         cryptKeysetOpenEx( keySet, name, accessMode, \
  684.                            ( char CPTR ) CRYPT_UNUSED, ( char CPTR ) CRYPT_UNUSED )
  685.  
  686. /* Get a key from a keyset */
  687.  
  688. CRET cryptGetKeyEx( CRYPT_KEYSET keySet, CRYPT_CONTEXT CPTR cryptContext, \
  689.                     const void CPTR userID, const void CPTR password );
  690. CRET cryptGetKeyFromObjectEx( CRYPT_KEYSET keySet, CRYPT_CONTEXT CPTR cryptContext, \
  691.                               const void CPTR object, const void CPTR password );
  692.  
  693. #define cryptGetKey( keySet, cryptContext, userID ) \
  694.         cryptGetKeyEx( keySet, cryptContext, userID, ( void CPTR ) CRYPT_UNUSED )
  695. #define cryptGetKeyFromObject( keySet, cryptContext, object ) \
  696.         cryptGetKeyFromObjectEx( keySet, cryptContext, object, \
  697.                                  ( void CPTR ) CRYPT_UNUSED )
  698.  
  699. /****************************************************************************
  700. *                                                                            *
  701. *                            Certificate Managment Functions                    *
  702. *                                                                            *
  703. ****************************************************************************/
  704.  
  705. /* Code to implement these functions is not shipped with cryptlib 2.00 since
  706.    the details are still undergoing revisions */
  707.  
  708. /* Create/destroy a certificate */
  709.  
  710. CRET cryptCreateCert( CRYPT_CERT CPTR certificate,
  711.                       const CRYPT_CONTEXT cryptContext );
  712. CRET cryptDestroyCert( CRYPT_CERT certificate );
  713.  
  714. /* Get/add/delete certificate components */
  715.  
  716. CRET cryptGetCertComponent( CRYPT_CERT certificate,
  717.                             const CRYPT_CERTINFO_TYPE certInfoType,
  718.                             void CPTR certInfo );
  719. CRET cryptAddCertComponent( CRYPT_CERT certificate,
  720.                             const CRYPT_CERTINFO_TYPE certInfoType,
  721.                             const void CPTR certInfo );
  722. CRET cryptDeleteCertComponent( CRYPT_CERT certificate,
  723.                                const CRYPT_CERTINFO_TYPE certInfoType,
  724.                                const void CPTR certInfo );
  725.  
  726. /* Sign a certificate */
  727.  
  728. CRET cryptSignCert( CRYPT_CERT certificate, CRYPT_CONTEXT pkcContext );
  729.  
  730. /* Import/export a certificate */
  731.  
  732. CRET cryptImportCert( const void CPTR certObject, CRYPT_CERT CPTR certificate );
  733. CRET cryptExportCert( void CPTR certObject, int CPTR certObjectLength,
  734.                       CRYPT_CERT certificate );
  735.  
  736. /****************************************************************************
  737. *                                                                            *
  738. *                                High-Level Functions                        *
  739. *                                                                            *
  740. ****************************************************************************/
  741.  
  742. /* Import and export a PKC-encrypted session key object */
  743.  
  744. CRET cryptExportKey( void CPTR encryptedKey, int CPTR encryptedKeyLength,
  745.                      const CRYPT_CONTEXT pkcContext,
  746.                      const CRYPT_CONTEXT cryptContext );
  747. CRET cryptImportKey( void CPTR encryptedKey, const CRYPT_CONTEXT pkcContext,
  748.                      CRYPT_CONTEXT CPTR cryptContext );
  749.  
  750. /* Create and check a digital signature object */
  751.  
  752. CRET cryptCreateSignature( void CPTR signature, int CPTR signatureLength,
  753.                            const CRYPT_CONTEXT pkcContext,
  754.                            const CRYPT_CONTEXT hashContext );
  755. CRET cryptCheckSignature( const void CPTR signature,
  756.                           const CRYPT_CONTEXT pkcContext,
  757.                           const CRYPT_CONTEXT hashContext );
  758.  
  759. /* Export and import a general data object and query an object */
  760.  
  761. CRET cryptExportObjectEx( void CPTR object, int CPTR objectLength,
  762.                           CRYPT_OBJECT_TYPE objectType, const long dataLength,
  763.                           const CRYPT_CONTEXT cryptContext );
  764. CRET cryptImportObjectEx( const void CPTR object, int CPTR payloadStart,
  765.                           long CPTR payloadLength,
  766.                           CRYPT_CONTEXT CPTR cryptContext );
  767. CRET cryptQueryObject( const void CPTR object,
  768.                        CRYPT_OBJECT_INFO CPTR cryptObjectInfo );
  769.  
  770. #define cryptExportObject( object, objectLength, objectType, dataLength ) \
  771.         cryptExportObjectEx( object, objectLength, objectType, dataLength, \
  772.                              ( CRYPT_CONTEXT ) CRYPT_UNUSED )
  773. #define cryptImportObject( object, payloadStart, payloadLength ) \
  774.         cryptImportObjectEx( object, payloadStart, payloadLength, \
  775.                              ( CRYPT_CONTEXT CPTR ) CRYPT_UNUSED )
  776. #endif /* _CAPI_DEFINED */
  777.