1/* Copyright (c) 1997,2003,2005-2006,2008 Apple Inc. 2 * 3 * common.h - Common CSP test code 4 * 5 * Revision History 6 * ---------------- 7 * 12 Aug 1997 Doug Mitchell at Apple 8 * Created. 9 */ 10 11#ifndef _UTIL_LIB_COMMON_H_ 12#define _UTIL_LIB_COMMON_H_ 13 14#include <Security/cssm.h> 15 16#ifdef __cplusplus 17extern "C" { 18#endif 19 20#undef COMMON_CSSM_MEMORY 21#define COMMON_CSSM_MEMORY 0 22 23#if COMMON_CSSM_MEMORY 24#define CSSM_MALLOC(size) CSSM_Malloc(size) 25#define CSSM_FREE(ptr) CSSM_Free(ptr) 26#define CSSM_CALLOC(num, size) CSSM_Calloc(num, size) 27#define CSSM_REALLOC(ptr, newSize) CSSM_Realloc(ptr, newSize) 28/* used in cspwrap when allocating memory on app's behalf */ 29#define appMalloc(size, allocRef) CSSM_Malloc(size) 30 31#else /* !COMMON_CSSM_MEMORY */ 32 33void * appMalloc (CSSM_SIZE size, void *allocRef); 34void appFree (void *mem_ptr, void *allocRef); 35void * appRealloc (void *ptr, CSSM_SIZE size, void *allocRef); 36void * appCalloc (uint32 num, CSSM_SIZE size, void *allocRef); 37 38#define CSSM_MALLOC(size) appMalloc(size, NULL) 39#define CSSM_FREE(ptr) appFree(ptr, NULL) 40#define CSSM_CALLOC(num, size) appCalloc(num, size, NULL) 41#define CSSM_REALLOC(ptr, newSize) appRealloc(ptr, newSize, NULL) 42 43#endif /* COMMON_CSSM_MEMORY */ 44 45/* 46 * As of 23 March 1999, there is no longer a "default DB" available for 47 * generating keys. This is the standard DB handle created when 48 * calling cspStartup(). 49 */ 50extern CSSM_DB_HANDLE commonDb; 51 52/* 53 * Init CSSM; returns CSSM_FALSE on error. Reusable. 54 */ 55extern CSSM_BOOL cssmStartup(); 56 57/* various flavors of "start up the CSP with optional DB open" */ 58CSSM_CSP_HANDLE cspStartup(); // bare bones CSP 59CSSM_CSP_HANDLE cspDbStartup( // bare bones CSP, DB open 60 CSSM_DB_HANDLE *dbHandPtr); 61CSSM_DL_HANDLE dlStartup(); 62CSSM_CSP_HANDLE cspDlDbStartup( // one size fits all 63 CSSM_BOOL bareCsp, // true ==> CSP, false ==> CSP/DL 64 CSSM_DB_HANDLE *dbHandPtr); // optional 65CSSM_RETURN cspShutdown( 66 CSSM_CSP_HANDLE cspHand, 67 CSSM_BOOL bareCsp); // true ==> CSP, false ==> CSP/DL 68CSSM_RETURN dbDelete( 69 CSSM_DL_HANDLE dlHand, // from dlStartup() 70 const char *dbName); 71CSSM_DB_HANDLE dbStartup( 72 CSSM_DL_HANDLE dlHand, // from dlStartup() 73 const char *dbName); 74CSSM_RETURN dbCreateOpen( 75 CSSM_DL_HANDLE dlHand, // from dlStartup() 76 const char *dbName, 77 CSSM_BOOL doCreate, // if false, must already exist 78 CSSM_BOOL deleteExist, 79 const char *pwd, // optional 80 CSSM_DB_HANDLE *dbHand); 81 82extern void intToBytes(unsigned i, unsigned char *buf); 83void shortToBytes(unsigned short s, unsigned char *buf); 84unsigned bytesToInt(const unsigned char *buf); 85unsigned short bytesToShort(const unsigned char *buf); 86 87/* specify either 32-bit integer or a pointer as an added attribute value */ 88typedef enum { 89 CAT_Uint32, 90 CAT_Ptr 91} ContextAttrType; 92 93CSSM_RETURN AddContextAttribute(CSSM_CC_HANDLE CCHandle, 94 uint32 AttributeType, 95 uint32 AttributeLength, 96 ContextAttrType attrType, 97 /* specify exactly one of these */ 98 const void *AttributePtr, 99 uint32 attributeInt); 100void printError(const char *op, CSSM_RETURN err); 101CSSM_RETURN appSetupCssmData( 102 CSSM_DATA_PTR data, 103 uint32 numBytes); 104void appFreeCssmData(CSSM_DATA_PTR data, 105 CSSM_BOOL freeStruct); 106CSSM_RETURN appCopyCssmData(const CSSM_DATA *src, 107 CSSM_DATA_PTR dst); 108/* copy raw data to a CSSM_DATAm mallocing dst. */ 109CSSM_RETURN appCopyData(const void *src, 110 uint32 len, 111 CSSM_DATA_PTR dst); 112 113/* returns CSSM_TRUE on success, else CSSM_FALSE */ 114CSSM_BOOL appCompareCssmData(const CSSM_DATA *d1, 115 const CSSM_DATA *d2); 116 117const char *cssmErrToStr(CSSM_RETURN err); 118 119/* 120 * Calculate random data size, fill dataPool with that many random bytes. 121 */ 122typedef enum { 123 DT_Random, 124 DT_Increment, 125 DT_Zero, 126 DT_ASCII 127} dataType; 128 129unsigned genData(unsigned char *dataPool, 130 unsigned minExp, 131 unsigned maxExp, 132 dataType type); 133void simpleGenData(CSSM_DATA_PTR dbuf, unsigned minBufSize, unsigned maxBufSize); 134unsigned genRand(unsigned min, unsigned max); 135extern void appGetRandomBytes(void *buf, unsigned len); 136 137void dumpBuffer( 138 const char *bufName, // optional 139 unsigned char *buf, 140 unsigned len); 141 142int testError(CSSM_BOOL quiet); 143 144void testStartBanner( 145 const char *testName, 146 int argc, 147 char **argv); 148 149#ifdef __cplusplus 150} 151 152#endif 153#endif /* _UTIL_LIB_COMMON_H_*/ 154 155 156