sureware.h revision 160814
1160814Ssimon/*
2160814Ssimon* Written by Corinne Dive-Reclus(cdive@baltimore.com)
3160814Ssimon*
4160814Ssimon* Copyright@2001 Baltimore Technologies Ltd.
5160814Ssimon*																								*
6160814Ssimon*		THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND																			*
7160814Ssimon*		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE					*
8160814Ssimon*		IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE				*
9160814Ssimon*		ARE DISCLAIMED.  IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE						*
10160814Ssimon*		FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL				*
11160814Ssimon*		DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS					*
12160814Ssimon*		OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)					*
13160814Ssimon*		HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT				*
14160814Ssimon*		LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY				*
15160814Ssimon*		OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF					*
16160814Ssimon*		SUCH DAMAGE.																			*
17160814Ssimon*
18160814Ssimon*
19160814Ssimon*/
20160814Ssimon#ifdef WIN32
21160814Ssimon#define SW_EXPORT	__declspec ( dllexport )
22160814Ssimon#else
23160814Ssimon#define SW_EXPORT
24160814Ssimon#endif
25160814Ssimon
26160814Ssimon/*
27160814Ssimon*	List of exposed SureWare errors
28160814Ssimon*/
29160814Ssimon#define SUREWAREHOOK_ERROR_FAILED		-1
30160814Ssimon#define SUREWAREHOOK_ERROR_FALLBACK		-2
31160814Ssimon#define SUREWAREHOOK_ERROR_UNIT_FAILURE -3
32160814Ssimon#define SUREWAREHOOK_ERROR_DATA_SIZE -4
33160814Ssimon#define SUREWAREHOOK_ERROR_INVALID_PAD -5
34160814Ssimon/*
35160814Ssimon* -----------------WARNING-----------------------------------
36160814Ssimon* In all the following functions:
37160814Ssimon* msg is a string with at least 24 bytes free.
38160814Ssimon* A 24 bytes string will be concatenated to the existing content of msg.
39160814Ssimon*/
40160814Ssimon/*
41160814Ssimon*	SureWare Initialisation function
42160814Ssimon*	in param threadsafe, if !=0, thread safe enabled
43160814Ssimon*	return SureWareHOOK_ERROR_UNIT_FAILURE if failure, 1 if success
44160814Ssimon*/
45160814Ssimontypedef int SureWareHook_Init_t(char*const msg,int threadsafe);
46160814Ssimonextern SW_EXPORT SureWareHook_Init_t SureWareHook_Init;
47160814Ssimon/*
48160814Ssimon*	SureWare Finish function
49160814Ssimon*/
50160814Ssimontypedef void SureWareHook_Finish_t(void);
51160814Ssimonextern SW_EXPORT SureWareHook_Finish_t SureWareHook_Finish;
52160814Ssimon/*
53160814Ssimon*	 PRE_CONDITION:
54160814Ssimon*		DO NOT CALL ANY OF THE FOLLOWING FUNCTIONS IN CASE OF INIT FAILURE
55160814Ssimon*/
56160814Ssimon/*
57160814Ssimon*	SureWare RAND Bytes function
58160814Ssimon*	In case of failure, the content of buf is unpredictable.
59160814Ssimon*	return 1 if success
60160814Ssimon*			SureWareHOOK_ERROR_FALLBACK if function not available in hardware
61160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
62160814Ssimon*			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
63160814Ssimon*			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
64160814Ssimon*
65160814Ssimon*	in/out param buf : a num bytes long buffer where random bytes will be put
66160814Ssimon*	in param num : the number of bytes into buf
67160814Ssimon*/
68160814Ssimontypedef int SureWareHook_Rand_Bytes_t(char*const msg,unsigned char *buf, int num);
69160814Ssimonextern SW_EXPORT SureWareHook_Rand_Bytes_t SureWareHook_Rand_Bytes;
70160814Ssimon
71160814Ssimon/*
72160814Ssimon*	SureWare RAND Seed function
73160814Ssimon*	Adds some seed to the Hardware Random Number Generator
74160814Ssimon*	return 1 if success
75160814Ssimon*			SureWareHOOK_ERROR_FALLBACK if function not available in hardware
76160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
77160814Ssimon*			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
78160814Ssimon*			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
79160814Ssimon*
80160814Ssimon*	in param buf : the seed to add into the HRNG
81160814Ssimon*	in param num : the number of bytes into buf
82160814Ssimon*/
83160814Ssimontypedef int SureWareHook_Rand_Seed_t(char*const msg,const void *buf, int num);
84160814Ssimonextern SW_EXPORT SureWareHook_Rand_Seed_t SureWareHook_Rand_Seed;
85160814Ssimon
86160814Ssimon/*
87160814Ssimon*	SureWare Load Private Key function
88160814Ssimon*	return 1 if success
89160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
90160814Ssimon*	No hardware is contact for this function.
91160814Ssimon*
92160814Ssimon*	in param key_id :the name of the private protected key file without the extension
93160814Ssimon						".sws"
94160814Ssimon*	out param hptr : a pointer to a buffer allocated by SureWare_Hook
95160814Ssimon*	out param num: the effective key length in bytes
96160814Ssimon*	out param keytype: 1 if RSA 2 if DSA
97160814Ssimon*/
98160814Ssimontypedef int SureWareHook_Load_Privkey_t(char*const msg,const char *key_id,char **hptr,unsigned long *num,char *keytype);
99160814Ssimonextern SW_EXPORT SureWareHook_Load_Privkey_t SureWareHook_Load_Privkey;
100160814Ssimon
101160814Ssimon/*
102160814Ssimon*	SureWare Info Public Key function
103160814Ssimon*	return 1 if success
104160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
105160814Ssimon*	No hardware is contact for this function.
106160814Ssimon*
107160814Ssimon*	in param key_id :the name of the private protected key file without the extension
108160814Ssimon						".swp"
109160814Ssimon*	out param hptr : a pointer to a buffer allocated by SureWare_Hook
110160814Ssimon*	out param num: the effective key length in bytes
111160814Ssimon*	out param keytype: 1 if RSA 2 if DSA
112160814Ssimon*/
113160814Ssimontypedef int SureWareHook_Info_Pubkey_t(char*const msg,const char *key_id,unsigned long *num,
114160814Ssimon										char *keytype);
115160814Ssimonextern SW_EXPORT SureWareHook_Info_Pubkey_t SureWareHook_Info_Pubkey;
116160814Ssimon
117160814Ssimon/*
118160814Ssimon*	SureWare Load Public Key function
119160814Ssimon*	return 1 if success
120160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
121160814Ssimon*	No hardware is contact for this function.
122160814Ssimon*
123160814Ssimon*	in param key_id :the name of the public protected key file without the extension
124160814Ssimon						".swp"
125160814Ssimon*	in param num : the bytes size of n and e
126160814Ssimon*	out param n: where to write modulus in bn format
127160814Ssimon*	out param e: where to write exponent in bn format
128160814Ssimon*/
129160814Ssimontypedef int SureWareHook_Load_Rsa_Pubkey_t(char*const msg,const char *key_id,unsigned long num,
130160814Ssimon										unsigned long *n, unsigned long *e);
131160814Ssimonextern SW_EXPORT SureWareHook_Load_Rsa_Pubkey_t SureWareHook_Load_Rsa_Pubkey;
132160814Ssimon
133160814Ssimon/*
134160814Ssimon*	SureWare Load DSA Public Key function
135160814Ssimon*	return 1 if success
136160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
137160814Ssimon*	No hardware is contact for this function.
138160814Ssimon*
139160814Ssimon*	in param key_id :the name of the public protected key file without the extension
140160814Ssimon						".swp"
141160814Ssimon*	in param num : the bytes size of n and e
142160814Ssimon*	out param pub: where to write pub key in bn format
143160814Ssimon*	out param p: where to write prime in bn format
144160814Ssimon*	out param q: where to write sunprime (length 20 bytes) in bn format
145160814Ssimon*	out param g: where to write base in bn format
146160814Ssimon*/
147160814Ssimontypedef int SureWareHook_Load_Dsa_Pubkey_t(char*const msg,const char *key_id,unsigned long num,
148160814Ssimon										unsigned long *pub, unsigned long *p,unsigned long*q,
149160814Ssimon										unsigned long *g);
150160814Ssimonextern SW_EXPORT SureWareHook_Load_Dsa_Pubkey_t SureWareHook_Load_Dsa_Pubkey;
151160814Ssimon
152160814Ssimon/*
153160814Ssimon*	SureWare Free function
154160814Ssimon*	Destroy the key into the hardware if destroy==1
155160814Ssimon*/
156160814Ssimontypedef void SureWareHook_Free_t(char *p,int destroy);
157160814Ssimonextern SW_EXPORT SureWareHook_Free_t SureWareHook_Free;
158160814Ssimon
159160814Ssimon#define SUREWARE_PKCS1_PAD 1
160160814Ssimon#define SUREWARE_ISO9796_PAD 2
161160814Ssimon#define SUREWARE_NO_PAD 0
162160814Ssimon/*
163160814Ssimon* SureWare RSA Private Decryption
164160814Ssimon* return 1 if success
165160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
166160814Ssimon*			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
167160814Ssimon*			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
168160814Ssimon*
169160814Ssimon*	in param flen : byte size of from and to
170160814Ssimon*	in param from : encrypted data buffer, should be a not-null valid pointer
171160814Ssimon*	out param tlen: byte size of decrypted data, if error, unexpected value
172160814Ssimon*	out param to : decrypted data buffer, should be a not-null valid pointer
173160814Ssimon*   in param prsa: a protected key pointer, should be a not-null valid pointer
174160814Ssimon*   int padding: padding id as follow
175160814Ssimon*					SUREWARE_PKCS1_PAD
176160814Ssimon*					SUREWARE_NO_PAD
177160814Ssimon*
178160814Ssimon*/
179160814Ssimontypedef int SureWareHook_Rsa_Priv_Dec_t(char*const msg,int flen,unsigned char *from,
180160814Ssimon										int *tlen,unsigned char *to,
181160814Ssimon										char *prsa,int padding);
182160814Ssimonextern SW_EXPORT SureWareHook_Rsa_Priv_Dec_t SureWareHook_Rsa_Priv_Dec;
183160814Ssimon/*
184160814Ssimon* SureWare RSA Signature
185160814Ssimon* return 1 if success
186160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
187160814Ssimon*			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
188160814Ssimon*			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
189160814Ssimon*
190160814Ssimon*	in param flen : byte size of from and to
191160814Ssimon*	in param from : encrypted data buffer, should be a not-null valid pointer
192160814Ssimon*	out param tlen: byte size of decrypted data, if error, unexpected value
193160814Ssimon*	out param to : decrypted data buffer, should be a not-null valid pointer
194160814Ssimon*   in param prsa: a protected key pointer, should be a not-null valid pointer
195160814Ssimon*   int padding: padding id as follow
196160814Ssimon*					SUREWARE_PKCS1_PAD
197160814Ssimon*					SUREWARE_ISO9796_PAD
198160814Ssimon*
199160814Ssimon*/
200160814Ssimontypedef int SureWareHook_Rsa_Sign_t(char*const msg,int flen,unsigned char *from,
201160814Ssimon										int *tlen,unsigned char *to,
202160814Ssimon										char *prsa,int padding);
203160814Ssimonextern SW_EXPORT SureWareHook_Rsa_Sign_t SureWareHook_Rsa_Sign;
204160814Ssimon/*
205160814Ssimon* SureWare DSA Signature
206160814Ssimon* return 1 if success
207160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
208160814Ssimon*			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
209160814Ssimon*			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
210160814Ssimon*
211160814Ssimon*	in param flen : byte size of from and to
212160814Ssimon*	in param from : encrypted data buffer, should be a not-null valid pointer
213160814Ssimon*	out param to : decrypted data buffer, should be a 40bytes valid pointer
214160814Ssimon*   in param pdsa: a protected key pointer, should be a not-null valid pointer
215160814Ssimon*
216160814Ssimon*/
217160814Ssimontypedef int SureWareHook_Dsa_Sign_t(char*const msg,int flen,const unsigned char *from,
218160814Ssimon										unsigned long *r,unsigned long *s,char *pdsa);
219160814Ssimonextern SW_EXPORT SureWareHook_Dsa_Sign_t SureWareHook_Dsa_Sign;
220160814Ssimon
221160814Ssimon
222160814Ssimon/*
223160814Ssimon* SureWare Mod Exp
224160814Ssimon* return 1 if success
225160814Ssimon*			SureWareHOOK_ERROR_FAILED if error while processing
226160814Ssimon*			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
227160814Ssimon*			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
228160814Ssimon*
229160814Ssimon*	mod and res are mlen bytes long.
230160814Ssimon*	exp is elen bytes long
231160814Ssimon*	data is dlen bytes long
232160814Ssimon*	mlen,elen and dlen are all multiple of sizeof(unsigned long)
233160814Ssimon*/
234160814Ssimontypedef int SureWareHook_Mod_Exp_t(char*const msg,int mlen,const unsigned long *mod,
235160814Ssimon									int elen,const unsigned long *exponent,
236160814Ssimon									int dlen,unsigned long *data,
237160814Ssimon									unsigned long *res);
238160814Ssimonextern SW_EXPORT SureWareHook_Mod_Exp_t SureWareHook_Mod_Exp;
239160814Ssimon
240