]> git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/utilLib/common.h
Security-57740.31.2.tar.gz
[apple/security.git] / SecurityTests / cspxutils / utilLib / common.h
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
17 extern "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
33 void * appMalloc (CSSM_SIZE size, void *allocRef);
34 void appFree (void *mem_ptr, void *allocRef);
35 void * appRealloc (void *ptr, CSSM_SIZE size, void *allocRef);
36 void * 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 */
50 extern CSSM_DB_HANDLE commonDb;
51
52 /*
53 * Init CSSM; returns CSSM_FALSE on error. Reusable.
54 */
55 extern CSSM_BOOL cssmStartup();
56
57 /* various flavors of "start up the CSP with optional DB open" */
58 CSSM_CSP_HANDLE cspStartup(); // bare bones CSP
59 CSSM_CSP_HANDLE cspDbStartup( // bare bones CSP, DB open
60 CSSM_DB_HANDLE *dbHandPtr);
61 CSSM_DL_HANDLE dlStartup();
62 CSSM_CSP_HANDLE cspDlDbStartup( // one size fits all
63 CSSM_BOOL bareCsp, // true ==> CSP, false ==> CSP/DL
64 CSSM_DB_HANDLE *dbHandPtr); // optional
65 CSSM_RETURN cspShutdown(
66 CSSM_CSP_HANDLE cspHand,
67 CSSM_BOOL bareCsp); // true ==> CSP, false ==> CSP/DL
68 CSSM_RETURN dbDelete(
69 CSSM_DL_HANDLE dlHand, // from dlStartup()
70 const char *dbName);
71 CSSM_DB_HANDLE dbStartup(
72 CSSM_DL_HANDLE dlHand, // from dlStartup()
73 const char *dbName);
74 CSSM_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
82 extern void intToBytes(unsigned i, unsigned char *buf);
83 void shortToBytes(unsigned short s, unsigned char *buf);
84 unsigned bytesToInt(const unsigned char *buf);
85 unsigned short bytesToShort(const unsigned char *buf);
86
87 /* specify either 32-bit integer or a pointer as an added attribute value */
88 typedef enum {
89 CAT_Uint32,
90 CAT_Ptr
91 } ContextAttrType;
92
93 CSSM_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);
100 void printError(const char *op, CSSM_RETURN err);
101 CSSM_RETURN appSetupCssmData(
102 CSSM_DATA_PTR data,
103 uint32 numBytes);
104 void appFreeCssmData(CSSM_DATA_PTR data,
105 CSSM_BOOL freeStruct);
106 CSSM_RETURN appCopyCssmData(const CSSM_DATA *src,
107 CSSM_DATA_PTR dst);
108 /* copy raw data to a CSSM_DATAm mallocing dst. */
109 CSSM_RETURN appCopyData(const void *src,
110 uint32 len,
111 CSSM_DATA_PTR dst);
112
113 /* returns CSSM_TRUE on success, else CSSM_FALSE */
114 CSSM_BOOL appCompareCssmData(const CSSM_DATA *d1,
115 const CSSM_DATA *d2);
116
117 const char *cssmErrToStr(CSSM_RETURN err);
118
119 /*
120 * Calculate random data size, fill dataPool with that many random bytes.
121 */
122 typedef enum {
123 DT_Random,
124 DT_Increment,
125 DT_Zero,
126 DT_ASCII
127 } dataType;
128
129 unsigned genData(unsigned char *dataPool,
130 unsigned minExp,
131 unsigned maxExp,
132 dataType type);
133 void simpleGenData(CSSM_DATA_PTR dbuf, unsigned minBufSize, unsigned maxBufSize);
134 unsigned genRand(unsigned min, unsigned max);
135 extern void appGetRandomBytes(void *buf, unsigned len);
136
137 void dumpBuffer(
138 const char *bufName, // optional
139 unsigned char *buf,
140 unsigned len);
141
142 int testError(CSSM_BOOL quiet);
143
144 void 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