1 /*
2 * Written by Corinne Dive-Reclus(cdive@baltimore.com)
3 *
4 * Copyright@2001 Baltimore Technologies Ltd.
5 *																								*
6 *		THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND																			*
7 *		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE					*
8 *		IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE				*
9 *		ARE DISCLAIMED.  IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE						*
10 *		FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL				*
11 *		DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS					*
12 *		OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)					*
13 *		HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT				*
14 *		LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY				*
15 *		OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF					*
16 *		SUCH DAMAGE.																			*
17 *
18 *
19 */
20 #ifdef WIN32
21 #define SW_EXPORT	__declspec ( dllexport )
22 #else
23 #define SW_EXPORT
24 #endif
25 
26 /*
27 *	List of exposed SureWare errors
28 */
29 #define SUREWAREHOOK_ERROR_FAILED		-1
30 #define SUREWAREHOOK_ERROR_FALLBACK		-2
31 #define SUREWAREHOOK_ERROR_UNIT_FAILURE -3
32 #define SUREWAREHOOK_ERROR_DATA_SIZE -4
33 #define SUREWAREHOOK_ERROR_INVALID_PAD -5
34 /*
35 * -----------------WARNING-----------------------------------
36 * In all the following functions:
37 * msg is a string with at least 24 bytes free.
38 * A 24 bytes string will be concatenated to the existing content of msg.
39 */
40 /*
41 *	SureWare Initialisation function
42 *	in param threadsafe, if !=0, thread safe enabled
43 *	return SureWareHOOK_ERROR_UNIT_FAILURE if failure, 1 if success
44 */
45 typedef int SureWareHook_Init_t(char*const msg,int threadsafe);
46 extern SW_EXPORT SureWareHook_Init_t SureWareHook_Init;
47 /*
48 *	SureWare Finish function
49 */
50 typedef void SureWareHook_Finish_t(void);
51 extern SW_EXPORT SureWareHook_Finish_t SureWareHook_Finish;
52 /*
53 *	 PRE_CONDITION:
54 *		DO NOT CALL ANY OF THE FOLLOWING FUNCTIONS IN CASE OF INIT FAILURE
55 */
56 /*
57 *	SureWare RAND Bytes function
58 *	In case of failure, the content of buf is unpredictable.
59 *	return 1 if success
60 *			SureWareHOOK_ERROR_FALLBACK if function not available in hardware
61 *			SureWareHOOK_ERROR_FAILED if error while processing
62 *			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
63 *			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
64 *
65 *	in/out param buf : a num bytes long buffer where random bytes will be put
66 *	in param num : the number of bytes into buf
67 */
68 typedef int SureWareHook_Rand_Bytes_t(char*const msg,unsigned char *buf, int num);
69 extern SW_EXPORT SureWareHook_Rand_Bytes_t SureWareHook_Rand_Bytes;
70 
71 /*
72 *	SureWare RAND Seed function
73 *	Adds some seed to the Hardware Random Number Generator
74 *	return 1 if success
75 *			SureWareHOOK_ERROR_FALLBACK if function not available in hardware
76 *			SureWareHOOK_ERROR_FAILED if error while processing
77 *			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
78 *			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
79 *
80 *	in param buf : the seed to add into the HRNG
81 *	in param num : the number of bytes into buf
82 */
83 typedef int SureWareHook_Rand_Seed_t(char*const msg,const void *buf, int num);
84 extern SW_EXPORT SureWareHook_Rand_Seed_t SureWareHook_Rand_Seed;
85 
86 /*
87 *	SureWare Load Private Key function
88 *	return 1 if success
89 *			SureWareHOOK_ERROR_FAILED if error while processing
90 *	No hardware is contact for this function.
91 *
92 *	in param key_id :the name of the private protected key file without the extension
93 						".sws"
94 *	out param hptr : a pointer to a buffer allocated by SureWare_Hook
95 *	out param num: the effective key length in bytes
96 *	out param keytype: 1 if RSA 2 if DSA
97 */
98 typedef int SureWareHook_Load_Privkey_t(char*const msg,const char *key_id,char **hptr,unsigned long *num,char *keytype);
99 extern SW_EXPORT SureWareHook_Load_Privkey_t SureWareHook_Load_Privkey;
100 
101 /*
102 *	SureWare Info Public Key function
103 *	return 1 if success
104 *			SureWareHOOK_ERROR_FAILED if error while processing
105 *	No hardware is contact for this function.
106 *
107 *	in param key_id :the name of the private protected key file without the extension
108 						".swp"
109 *	out param hptr : a pointer to a buffer allocated by SureWare_Hook
110 *	out param num: the effective key length in bytes
111 *	out param keytype: 1 if RSA 2 if DSA
112 */
113 typedef int SureWareHook_Info_Pubkey_t(char*const msg,const char *key_id,unsigned long *num,
114 										char *keytype);
115 extern SW_EXPORT SureWareHook_Info_Pubkey_t SureWareHook_Info_Pubkey;
116 
117 /*
118 *	SureWare Load Public Key function
119 *	return 1 if success
120 *			SureWareHOOK_ERROR_FAILED if error while processing
121 *	No hardware is contact for this function.
122 *
123 *	in param key_id :the name of the public protected key file without the extension
124 						".swp"
125 *	in param num : the bytes size of n and e
126 *	out param n: where to write modulus in bn format
127 *	out param e: where to write exponent in bn format
128 */
129 typedef int SureWareHook_Load_Rsa_Pubkey_t(char*const msg,const char *key_id,unsigned long num,
130 										unsigned long *n, unsigned long *e);
131 extern SW_EXPORT SureWareHook_Load_Rsa_Pubkey_t SureWareHook_Load_Rsa_Pubkey;
132 
133 /*
134 *	SureWare Load DSA Public Key function
135 *	return 1 if success
136 *			SureWareHOOK_ERROR_FAILED if error while processing
137 *	No hardware is contact for this function.
138 *
139 *	in param key_id :the name of the public protected key file without the extension
140 						".swp"
141 *	in param num : the bytes size of n and e
142 *	out param pub: where to write pub key in bn format
143 *	out param p: where to write prime in bn format
144 *	out param q: where to write sunprime (length 20 bytes) in bn format
145 *	out param g: where to write base in bn format
146 */
147 typedef int SureWareHook_Load_Dsa_Pubkey_t(char*const msg,const char *key_id,unsigned long num,
148 										unsigned long *pub, unsigned long *p,unsigned long*q,
149 										unsigned long *g);
150 extern SW_EXPORT SureWareHook_Load_Dsa_Pubkey_t SureWareHook_Load_Dsa_Pubkey;
151 
152 /*
153 *	SureWare Free function
154 *	Destroy the key into the hardware if destroy==1
155 */
156 typedef void SureWareHook_Free_t(char *p,int destroy);
157 extern SW_EXPORT SureWareHook_Free_t SureWareHook_Free;
158 
159 #define SUREWARE_PKCS1_PAD 1
160 #define SUREWARE_ISO9796_PAD 2
161 #define SUREWARE_NO_PAD 0
162 /*
163 * SureWare RSA Private Decryption
164 * return 1 if success
165 *			SureWareHOOK_ERROR_FAILED if error while processing
166 *			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
167 *			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
168 *
169 *	in param flen : byte size of from and to
170 *	in param from : encrypted data buffer, should be a not-null valid pointer
171 *	out param tlen: byte size of decrypted data, if error, unexpected value
172 *	out param to : decrypted data buffer, should be a not-null valid pointer
173 *   in param prsa: a protected key pointer, should be a not-null valid pointer
174 *   int padding: padding id as follow
175 *					SUREWARE_PKCS1_PAD
176 *					SUREWARE_NO_PAD
177 *
178 */
179 typedef int SureWareHook_Rsa_Priv_Dec_t(char*const msg,int flen,unsigned char *from,
180 										int *tlen,unsigned char *to,
181 										char *prsa,int padding);
182 extern SW_EXPORT SureWareHook_Rsa_Priv_Dec_t SureWareHook_Rsa_Priv_Dec;
183 /*
184 * SureWare RSA Signature
185 * return 1 if success
186 *			SureWareHOOK_ERROR_FAILED if error while processing
187 *			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
188 *			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
189 *
190 *	in param flen : byte size of from and to
191 *	in param from : encrypted data buffer, should be a not-null valid pointer
192 *	out param tlen: byte size of decrypted data, if error, unexpected value
193 *	out param to : decrypted data buffer, should be a not-null valid pointer
194 *   in param prsa: a protected key pointer, should be a not-null valid pointer
195 *   int padding: padding id as follow
196 *					SUREWARE_PKCS1_PAD
197 *					SUREWARE_ISO9796_PAD
198 *
199 */
200 typedef int SureWareHook_Rsa_Sign_t(char*const msg,int flen,unsigned char *from,
201 										int *tlen,unsigned char *to,
202 										char *prsa,int padding);
203 extern SW_EXPORT SureWareHook_Rsa_Sign_t SureWareHook_Rsa_Sign;
204 /*
205 * SureWare DSA Signature
206 * return 1 if success
207 *			SureWareHOOK_ERROR_FAILED if error while processing
208 *			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
209 *			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
210 *
211 *	in param flen : byte size of from and to
212 *	in param from : encrypted data buffer, should be a not-null valid pointer
213 *	out param to : decrypted data buffer, should be a 40bytes valid pointer
214 *   in param pdsa: a protected key pointer, should be a not-null valid pointer
215 *
216 */
217 typedef int SureWareHook_Dsa_Sign_t(char*const msg,int flen,const unsigned char *from,
218 										unsigned long *r,unsigned long *s,char *pdsa);
219 extern SW_EXPORT SureWareHook_Dsa_Sign_t SureWareHook_Dsa_Sign;
220 
221 
222 /*
223 * SureWare Mod Exp
224 * return 1 if success
225 *			SureWareHOOK_ERROR_FAILED if error while processing
226 *			SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
227 *			SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
228 *
229 *	mod and res are mlen bytes long.
230 *	exp is elen bytes long
231 *	data is dlen bytes long
232 *	mlen,elen and dlen are all multiple of sizeof(unsigned long)
233 */
234 typedef int SureWareHook_Mod_Exp_t(char*const msg,int mlen,const unsigned long *mod,
235 									int elen,const unsigned long *exponent,
236 									int dlen,unsigned long *data,
237 									unsigned long *res);
238 extern SW_EXPORT SureWareHook_Mod_Exp_t SureWareHook_Mod_Exp;
239 
240