sureware.h revision 280304
1164410Ssyrinx/*-
2164410Ssyrinx * Written by Corinne Dive-Reclus(cdive@baltimore.com)
3164410Ssyrinx *
4164410Ssyrinx * Copyright@2001 Baltimore Technologies Ltd.
5164410Ssyrinx *
6164410Ssyrinx * THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND
7164410Ssyrinx * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8164410Ssyrinx * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9164410Ssyrinx * ARE DISCLAIMED.  IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE
10164410Ssyrinx * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11164410Ssyrinx * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12164410Ssyrinx * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13164410Ssyrinx * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14164410Ssyrinx * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
15164410Ssyrinx * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
16164410Ssyrinx * SUCH DAMAGE.
17164410Ssyrinx */
18164410Ssyrinx
19164410Ssyrinx#ifdef WIN32
20164410Ssyrinx# define SW_EXPORT       __declspec ( dllexport )
21164410Ssyrinx#else
22164410Ssyrinx# define SW_EXPORT
23164410Ssyrinx#endif
24164410Ssyrinx
25164410Ssyrinx/*
26164410Ssyrinx *       List of exposed SureWare errors
27164410Ssyrinx */
28164410Ssyrinx#define SUREWAREHOOK_ERROR_FAILED               -1
29164410Ssyrinx#define SUREWAREHOOK_ERROR_FALLBACK             -2
30164410Ssyrinx#define SUREWAREHOOK_ERROR_UNIT_FAILURE -3
31164410Ssyrinx#define SUREWAREHOOK_ERROR_DATA_SIZE -4
32164410Ssyrinx#define SUREWAREHOOK_ERROR_INVALID_PAD -5
33164410Ssyrinx/*-
34164410Ssyrinx* -----------------WARNING-----------------------------------
35164410Ssyrinx* In all the following functions:
36164410Ssyrinx* msg is a string with at least 24 bytes free.
37164410Ssyrinx* A 24 bytes string will be concatenated to the existing content of msg.
38164410Ssyrinx*/
39164410Ssyrinx/*-
40164410Ssyrinx*       SureWare Initialisation function
41164410Ssyrinx*       in param threadsafe, if !=0, thread safe enabled
42164410Ssyrinx*       return SureWareHOOK_ERROR_UNIT_FAILURE if failure, 1 if success
43164410Ssyrinx*/
44164410Ssyrinxtypedef int SureWareHook_Init_t(char *const msg, int threadsafe);
45164410Ssyrinxextern SW_EXPORT SureWareHook_Init_t SureWareHook_Init;
46164410Ssyrinx/*-
47164410Ssyrinx*       SureWare Finish function
48164410Ssyrinx*/
49164410Ssyrinxtypedef void SureWareHook_Finish_t(void);
50164410Ssyrinxextern SW_EXPORT SureWareHook_Finish_t SureWareHook_Finish;
51164410Ssyrinx/*-
52164410Ssyrinx*        PRE_CONDITION:
53164410Ssyrinx*               DO NOT CALL ANY OF THE FOLLOWING FUNCTIONS IN CASE OF INIT FAILURE
54164410Ssyrinx*/
55164410Ssyrinx/*-
56164410Ssyrinx*       SureWare RAND Bytes function
57164410Ssyrinx*       In case of failure, the content of buf is unpredictable.
58164410Ssyrinx*       return 1 if success
59164410Ssyrinx*                       SureWareHOOK_ERROR_FALLBACK if function not available in hardware
60164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
61164410Ssyrinx*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
62164410Ssyrinx*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
63164410Ssyrinx*
64164410Ssyrinx*       in/out param buf : a num bytes long buffer where random bytes will be put
65164410Ssyrinx*       in param num : the number of bytes into buf
66164410Ssyrinx*/
67164410Ssyrinxtypedef int SureWareHook_Rand_Bytes_t(char *const msg, unsigned char *buf,
68164410Ssyrinx                                      int num);
69164410Ssyrinxextern SW_EXPORT SureWareHook_Rand_Bytes_t SureWareHook_Rand_Bytes;
70164410Ssyrinx
71164410Ssyrinx/*-
72164410Ssyrinx*       SureWare RAND Seed function
73164410Ssyrinx*       Adds some seed to the Hardware Random Number Generator
74164410Ssyrinx*       return 1 if success
75164410Ssyrinx*                       SureWareHOOK_ERROR_FALLBACK if function not available in hardware
76164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
77164410Ssyrinx*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
78164410Ssyrinx*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
79164410Ssyrinx*
80164410Ssyrinx*       in param buf : the seed to add into the HRNG
81164410Ssyrinx*       in param num : the number of bytes into buf
82164410Ssyrinx*/
83164410Ssyrinxtypedef int SureWareHook_Rand_Seed_t(char *const msg, const void *buf,
84164410Ssyrinx                                     int num);
85164410Ssyrinxextern SW_EXPORT SureWareHook_Rand_Seed_t SureWareHook_Rand_Seed;
86164410Ssyrinx
87164410Ssyrinx/*-
88164410Ssyrinx*       SureWare Load Private Key function
89164410Ssyrinx*       return 1 if success
90164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
91164410Ssyrinx*       No hardware is contact for this function.
92164410Ssyrinx*
93164410Ssyrinx*       in param key_id :the name of the private protected key file without the extension
94164410Ssyrinx                                                ".sws"
95164410Ssyrinx*       out param hptr : a pointer to a buffer allocated by SureWare_Hook
96164410Ssyrinx*       out param num: the effective key length in bytes
97164410Ssyrinx*       out param keytype: 1 if RSA 2 if DSA
98164410Ssyrinx*/
99164410Ssyrinxtypedef int SureWareHook_Load_Privkey_t(char *const msg, const char *key_id,
100164410Ssyrinx                                        char **hptr, unsigned long *num,
101164410Ssyrinx                                        char *keytype);
102164410Ssyrinxextern SW_EXPORT SureWareHook_Load_Privkey_t SureWareHook_Load_Privkey;
103164410Ssyrinx
104164410Ssyrinx/*-
105164410Ssyrinx*       SureWare Info Public Key function
106164410Ssyrinx*       return 1 if success
107164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
108164410Ssyrinx*       No hardware is contact for this function.
109164410Ssyrinx*
110164410Ssyrinx*       in param key_id :the name of the private protected key file without the extension
111164410Ssyrinx                                                ".swp"
112164410Ssyrinx*       out param hptr : a pointer to a buffer allocated by SureWare_Hook
113164410Ssyrinx*       out param num: the effective key length in bytes
114164410Ssyrinx*       out param keytype: 1 if RSA 2 if DSA
115164410Ssyrinx*/
116164410Ssyrinxtypedef int SureWareHook_Info_Pubkey_t(char *const msg, const char *key_id,
117164410Ssyrinx                                       unsigned long *num, char *keytype);
118164410Ssyrinxextern SW_EXPORT SureWareHook_Info_Pubkey_t SureWareHook_Info_Pubkey;
119164410Ssyrinx
120164410Ssyrinx/*-
121164410Ssyrinx*       SureWare Load Public Key function
122164410Ssyrinx*       return 1 if success
123164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
124164410Ssyrinx*       No hardware is contact for this function.
125164410Ssyrinx*
126164410Ssyrinx*       in param key_id :the name of the public protected key file without the extension
127164410Ssyrinx                                                ".swp"
128164410Ssyrinx*       in param num : the bytes size of n and e
129164410Ssyrinx*       out param n: where to write modulus in bn format
130164410Ssyrinx*       out param e: where to write exponent in bn format
131164410Ssyrinx*/
132164410Ssyrinxtypedef int SureWareHook_Load_Rsa_Pubkey_t(char *const msg,
133164410Ssyrinx                                           const char *key_id,
134164410Ssyrinx                                           unsigned long num,
135164410Ssyrinx                                           unsigned long *n,
136164410Ssyrinx                                           unsigned long *e);
137164410Ssyrinxextern SW_EXPORT SureWareHook_Load_Rsa_Pubkey_t SureWareHook_Load_Rsa_Pubkey;
138164410Ssyrinx
139164410Ssyrinx/*-
140164410Ssyrinx*       SureWare Load DSA Public Key function
141164410Ssyrinx*       return 1 if success
142164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
143164410Ssyrinx*       No hardware is contact for this function.
144164410Ssyrinx*
145164410Ssyrinx*       in param key_id :the name of the public protected key file without the extension
146164410Ssyrinx                                                ".swp"
147164410Ssyrinx*       in param num : the bytes size of n and e
148164410Ssyrinx*       out param pub: where to write pub key in bn format
149164410Ssyrinx*       out param p: where to write prime in bn format
150164410Ssyrinx*       out param q: where to write sunprime (length 20 bytes) in bn format
151164410Ssyrinx*       out param g: where to write base in bn format
152164410Ssyrinx*/
153164410Ssyrinxtypedef int SureWareHook_Load_Dsa_Pubkey_t(char *const msg,
154164410Ssyrinx                                           const char *key_id,
155164410Ssyrinx                                           unsigned long num,
156164410Ssyrinx                                           unsigned long *pub,
157164410Ssyrinx                                           unsigned long *p, unsigned long *q,
158164410Ssyrinx                                           unsigned long *g);
159164410Ssyrinxextern SW_EXPORT SureWareHook_Load_Dsa_Pubkey_t SureWareHook_Load_Dsa_Pubkey;
160164410Ssyrinx
161164410Ssyrinx/*-
162164410Ssyrinx*       SureWare Free function
163164410Ssyrinx*       Destroy the key into the hardware if destroy==1
164164410Ssyrinx*/
165164410Ssyrinxtypedef void SureWareHook_Free_t(char *p, int destroy);
166164410Ssyrinxextern SW_EXPORT SureWareHook_Free_t SureWareHook_Free;
167164410Ssyrinx
168164410Ssyrinx#define SUREWARE_PKCS1_PAD 1
169164410Ssyrinx#define SUREWARE_ISO9796_PAD 2
170164410Ssyrinx#define SUREWARE_NO_PAD 0
171164410Ssyrinx/*-
172164410Ssyrinx* SureWare RSA Private Decryption
173164410Ssyrinx* return 1 if success
174164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
175164410Ssyrinx*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
176164410Ssyrinx*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
177164410Ssyrinx*
178164410Ssyrinx*       in param flen : byte size of from and to
179164410Ssyrinx*       in param from : encrypted data buffer, should be a not-null valid pointer
180164410Ssyrinx*       out param tlen: byte size of decrypted data, if error, unexpected value
181164410Ssyrinx*       out param to : decrypted data buffer, should be a not-null valid pointer
182164410Ssyrinx*   in param prsa: a protected key pointer, should be a not-null valid pointer
183164410Ssyrinx*   int padding: padding id as follow
184164410Ssyrinx*                                       SUREWARE_PKCS1_PAD
185164410Ssyrinx*                                       SUREWARE_NO_PAD
186164410Ssyrinx*
187164410Ssyrinx*/
188164410Ssyrinxtypedef int SureWareHook_Rsa_Priv_Dec_t(char *const msg, int flen,
189164410Ssyrinx                                        unsigned char *from, int *tlen,
190164410Ssyrinx                                        unsigned char *to, char *prsa,
191164410Ssyrinx                                        int padding);
192164410Ssyrinxextern SW_EXPORT SureWareHook_Rsa_Priv_Dec_t SureWareHook_Rsa_Priv_Dec;
193164410Ssyrinx/*-
194164410Ssyrinx* SureWare RSA Signature
195164410Ssyrinx* return 1 if success
196164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
197164410Ssyrinx*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
198164410Ssyrinx*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
199164410Ssyrinx*
200164410Ssyrinx*       in param flen : byte size of from and to
201164410Ssyrinx*       in param from : encrypted data buffer, should be a not-null valid pointer
202164410Ssyrinx*       out param tlen: byte size of decrypted data, if error, unexpected value
203164410Ssyrinx*       out param to : decrypted data buffer, should be a not-null valid pointer
204164410Ssyrinx*   in param prsa: a protected key pointer, should be a not-null valid pointer
205164410Ssyrinx*   int padding: padding id as follow
206164410Ssyrinx*                                       SUREWARE_PKCS1_PAD
207164410Ssyrinx*                                       SUREWARE_ISO9796_PAD
208164410Ssyrinx*
209164410Ssyrinx*/
210164410Ssyrinxtypedef int SureWareHook_Rsa_Sign_t(char *const msg, int flen,
211164410Ssyrinx                                    unsigned char *from, int *tlen,
212164410Ssyrinx                                    unsigned char *to, char *prsa,
213164410Ssyrinx                                    int padding);
214164410Ssyrinxextern SW_EXPORT SureWareHook_Rsa_Sign_t SureWareHook_Rsa_Sign;
215164410Ssyrinx/*-
216164410Ssyrinx* SureWare DSA Signature
217164410Ssyrinx* return 1 if success
218164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
219164410Ssyrinx*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
220164410Ssyrinx*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
221164410Ssyrinx*
222164410Ssyrinx*       in param flen : byte size of from and to
223164410Ssyrinx*       in param from : encrypted data buffer, should be a not-null valid pointer
224164410Ssyrinx*       out param to : decrypted data buffer, should be a 40bytes valid pointer
225164410Ssyrinx*   in param pdsa: a protected key pointer, should be a not-null valid pointer
226164410Ssyrinx*
227164410Ssyrinx*/
228164410Ssyrinxtypedef int SureWareHook_Dsa_Sign_t(char *const msg, int flen,
229164410Ssyrinx                                    const unsigned char *from,
230164410Ssyrinx                                    unsigned long *r, unsigned long *s,
231164410Ssyrinx                                    char *pdsa);
232164410Ssyrinxextern SW_EXPORT SureWareHook_Dsa_Sign_t SureWareHook_Dsa_Sign;
233164410Ssyrinx
234164410Ssyrinx/*-
235164410Ssyrinx* SureWare Mod Exp
236164410Ssyrinx* return 1 if success
237164410Ssyrinx*                       SureWareHOOK_ERROR_FAILED if error while processing
238164410Ssyrinx*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
239164410Ssyrinx*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
240164410Ssyrinx*
241164410Ssyrinx*       mod and res are mlen bytes long.
242164410Ssyrinx*       exp is elen bytes long
243164410Ssyrinx*       data is dlen bytes long
244164410Ssyrinx*       mlen,elen and dlen are all multiple of sizeof(unsigned long)
245164410Ssyrinx*/
246164410Ssyrinxtypedef int SureWareHook_Mod_Exp_t(char *const msg, int mlen,
247164410Ssyrinx                                   const unsigned long *mod, int elen,
248164410Ssyrinx                                   const unsigned long *exponent, int dlen,
249164410Ssyrinx                                   unsigned long *data, unsigned long *res);
250164410Ssyrinxextern SW_EXPORT SureWareHook_Mod_Exp_t SureWareHook_Mod_Exp;
251164410Ssyrinx