1296341Sdelphij/*-
2296341Sdelphij * Written by Corinne Dive-Reclus(cdive@baltimore.com)
3296341Sdelphij *
4296341Sdelphij * Copyright@2001 Baltimore Technologies Ltd.
5296341Sdelphij *
6296341Sdelphij * THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND
7296341Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8296341Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9296341Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE
10296341Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11296341Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12296341Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13296341Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14296341Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
15296341Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
16296341Sdelphij * SUCH DAMAGE.
17296341Sdelphij */
18296341Sdelphij
19160814Ssimon#ifdef WIN32
20296341Sdelphij# define SW_EXPORT       __declspec ( dllexport )
21160814Ssimon#else
22296341Sdelphij# define SW_EXPORT
23160814Ssimon#endif
24160814Ssimon
25160814Ssimon/*
26296341Sdelphij *       List of exposed SureWare errors
27296341Sdelphij */
28296341Sdelphij#define SUREWAREHOOK_ERROR_FAILED               -1
29296341Sdelphij#define SUREWAREHOOK_ERROR_FALLBACK             -2
30160814Ssimon#define SUREWAREHOOK_ERROR_UNIT_FAILURE -3
31160814Ssimon#define SUREWAREHOOK_ERROR_DATA_SIZE -4
32160814Ssimon#define SUREWAREHOOK_ERROR_INVALID_PAD -5
33296341Sdelphij/*-
34160814Ssimon* -----------------WARNING-----------------------------------
35160814Ssimon* In all the following functions:
36160814Ssimon* msg is a string with at least 24 bytes free.
37296341Sdelphij* A 24 bytes string will be concatenated to the existing content of msg.
38160814Ssimon*/
39296341Sdelphij/*-
40296341Sdelphij*       SureWare Initialisation function
41296341Sdelphij*       in param threadsafe, if !=0, thread safe enabled
42296341Sdelphij*       return SureWareHOOK_ERROR_UNIT_FAILURE if failure, 1 if success
43160814Ssimon*/
44296341Sdelphijtypedef int SureWareHook_Init_t(char *const msg, int threadsafe);
45160814Ssimonextern SW_EXPORT SureWareHook_Init_t SureWareHook_Init;
46296341Sdelphij/*-
47296341Sdelphij*       SureWare Finish function
48160814Ssimon*/
49160814Ssimontypedef void SureWareHook_Finish_t(void);
50160814Ssimonextern SW_EXPORT SureWareHook_Finish_t SureWareHook_Finish;
51296341Sdelphij/*-
52296341Sdelphij*        PRE_CONDITION:
53296341Sdelphij*               DO NOT CALL ANY OF THE FOLLOWING FUNCTIONS IN CASE OF INIT FAILURE
54160814Ssimon*/
55296341Sdelphij/*-
56296341Sdelphij*       SureWare RAND Bytes function
57296341Sdelphij*       In case of failure, the content of buf is unpredictable.
58296341Sdelphij*       return 1 if success
59296341Sdelphij*                       SureWareHOOK_ERROR_FALLBACK if function not available in hardware
60296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
61296341Sdelphij*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
62296341Sdelphij*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
63160814Ssimon*
64296341Sdelphij*       in/out param buf : a num bytes long buffer where random bytes will be put
65296341Sdelphij*       in param num : the number of bytes into buf
66160814Ssimon*/
67296341Sdelphijtypedef int SureWareHook_Rand_Bytes_t(char *const msg, unsigned char *buf,
68296341Sdelphij                                      int num);
69160814Ssimonextern SW_EXPORT SureWareHook_Rand_Bytes_t SureWareHook_Rand_Bytes;
70160814Ssimon
71296341Sdelphij/*-
72296341Sdelphij*       SureWare RAND Seed function
73296341Sdelphij*       Adds some seed to the Hardware Random Number Generator
74296341Sdelphij*       return 1 if success
75296341Sdelphij*                       SureWareHOOK_ERROR_FALLBACK if function not available in hardware
76296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
77296341Sdelphij*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
78296341Sdelphij*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
79160814Ssimon*
80296341Sdelphij*       in param buf : the seed to add into the HRNG
81296341Sdelphij*       in param num : the number of bytes into buf
82160814Ssimon*/
83296341Sdelphijtypedef int SureWareHook_Rand_Seed_t(char *const msg, const void *buf,
84296341Sdelphij                                     int num);
85160814Ssimonextern SW_EXPORT SureWareHook_Rand_Seed_t SureWareHook_Rand_Seed;
86160814Ssimon
87296341Sdelphij/*-
88296341Sdelphij*       SureWare Load Private Key function
89296341Sdelphij*       return 1 if success
90296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
91296341Sdelphij*       No hardware is contact for this function.
92160814Ssimon*
93296341Sdelphij*       in param key_id :the name of the private protected key file without the extension
94296341Sdelphij                                                ".sws"
95296341Sdelphij*       out param hptr : a pointer to a buffer allocated by SureWare_Hook
96296341Sdelphij*       out param num: the effective key length in bytes
97296341Sdelphij*       out param keytype: 1 if RSA 2 if DSA
98160814Ssimon*/
99296341Sdelphijtypedef int SureWareHook_Load_Privkey_t(char *const msg, const char *key_id,
100296341Sdelphij                                        char **hptr, unsigned long *num,
101296341Sdelphij                                        char *keytype);
102160814Ssimonextern SW_EXPORT SureWareHook_Load_Privkey_t SureWareHook_Load_Privkey;
103160814Ssimon
104296341Sdelphij/*-
105296341Sdelphij*       SureWare Info Public Key function
106296341Sdelphij*       return 1 if success
107296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
108296341Sdelphij*       No hardware is contact for this function.
109160814Ssimon*
110296341Sdelphij*       in param key_id :the name of the private protected key file without the extension
111296341Sdelphij                                                ".swp"
112296341Sdelphij*       out param hptr : a pointer to a buffer allocated by SureWare_Hook
113296341Sdelphij*       out param num: the effective key length in bytes
114296341Sdelphij*       out param keytype: 1 if RSA 2 if DSA
115160814Ssimon*/
116296341Sdelphijtypedef int SureWareHook_Info_Pubkey_t(char *const msg, const char *key_id,
117296341Sdelphij                                       unsigned long *num, char *keytype);
118160814Ssimonextern SW_EXPORT SureWareHook_Info_Pubkey_t SureWareHook_Info_Pubkey;
119160814Ssimon
120296341Sdelphij/*-
121296341Sdelphij*       SureWare Load Public Key function
122296341Sdelphij*       return 1 if success
123296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
124296341Sdelphij*       No hardware is contact for this function.
125160814Ssimon*
126296341Sdelphij*       in param key_id :the name of the public protected key file without the extension
127296341Sdelphij                                                ".swp"
128296341Sdelphij*       in param num : the bytes size of n and e
129296341Sdelphij*       out param n: where to write modulus in bn format
130296341Sdelphij*       out param e: where to write exponent in bn format
131160814Ssimon*/
132296341Sdelphijtypedef int SureWareHook_Load_Rsa_Pubkey_t(char *const msg,
133296341Sdelphij                                           const char *key_id,
134296341Sdelphij                                           unsigned long num,
135296341Sdelphij                                           unsigned long *n,
136296341Sdelphij                                           unsigned long *e);
137160814Ssimonextern SW_EXPORT SureWareHook_Load_Rsa_Pubkey_t SureWareHook_Load_Rsa_Pubkey;
138160814Ssimon
139296341Sdelphij/*-
140296341Sdelphij*       SureWare Load DSA Public Key function
141296341Sdelphij*       return 1 if success
142296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
143296341Sdelphij*       No hardware is contact for this function.
144160814Ssimon*
145296341Sdelphij*       in param key_id :the name of the public protected key file without the extension
146296341Sdelphij                                                ".swp"
147296341Sdelphij*       in param num : the bytes size of n and e
148296341Sdelphij*       out param pub: where to write pub key in bn format
149296341Sdelphij*       out param p: where to write prime in bn format
150296341Sdelphij*       out param q: where to write sunprime (length 20 bytes) in bn format
151296341Sdelphij*       out param g: where to write base in bn format
152160814Ssimon*/
153296341Sdelphijtypedef int SureWareHook_Load_Dsa_Pubkey_t(char *const msg,
154296341Sdelphij                                           const char *key_id,
155296341Sdelphij                                           unsigned long num,
156296341Sdelphij                                           unsigned long *pub,
157296341Sdelphij                                           unsigned long *p, unsigned long *q,
158296341Sdelphij                                           unsigned long *g);
159160814Ssimonextern SW_EXPORT SureWareHook_Load_Dsa_Pubkey_t SureWareHook_Load_Dsa_Pubkey;
160160814Ssimon
161296341Sdelphij/*-
162296341Sdelphij*       SureWare Free function
163296341Sdelphij*       Destroy the key into the hardware if destroy==1
164160814Ssimon*/
165296341Sdelphijtypedef void SureWareHook_Free_t(char *p, int destroy);
166160814Ssimonextern SW_EXPORT SureWareHook_Free_t SureWareHook_Free;
167160814Ssimon
168160814Ssimon#define SUREWARE_PKCS1_PAD 1
169160814Ssimon#define SUREWARE_ISO9796_PAD 2
170160814Ssimon#define SUREWARE_NO_PAD 0
171296341Sdelphij/*-
172160814Ssimon* SureWare RSA Private Decryption
173160814Ssimon* return 1 if success
174296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
175296341Sdelphij*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
176296341Sdelphij*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
177160814Ssimon*
178296341Sdelphij*       in param flen : byte size of from and to
179296341Sdelphij*       in param from : encrypted data buffer, should be a not-null valid pointer
180296341Sdelphij*       out param tlen: byte size of decrypted data, if error, unexpected value
181296341Sdelphij*       out param to : decrypted data buffer, should be a not-null valid pointer
182160814Ssimon*   in param prsa: a protected key pointer, should be a not-null valid pointer
183160814Ssimon*   int padding: padding id as follow
184296341Sdelphij*                                       SUREWARE_PKCS1_PAD
185296341Sdelphij*                                       SUREWARE_NO_PAD
186160814Ssimon*
187160814Ssimon*/
188296341Sdelphijtypedef int SureWareHook_Rsa_Priv_Dec_t(char *const msg, int flen,
189296341Sdelphij                                        unsigned char *from, int *tlen,
190296341Sdelphij                                        unsigned char *to, char *prsa,
191296341Sdelphij                                        int padding);
192160814Ssimonextern SW_EXPORT SureWareHook_Rsa_Priv_Dec_t SureWareHook_Rsa_Priv_Dec;
193296341Sdelphij/*-
194160814Ssimon* SureWare RSA Signature
195160814Ssimon* return 1 if success
196296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
197296341Sdelphij*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
198296341Sdelphij*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
199160814Ssimon*
200296341Sdelphij*       in param flen : byte size of from and to
201296341Sdelphij*       in param from : encrypted data buffer, should be a not-null valid pointer
202296341Sdelphij*       out param tlen: byte size of decrypted data, if error, unexpected value
203296341Sdelphij*       out param to : decrypted data buffer, should be a not-null valid pointer
204160814Ssimon*   in param prsa: a protected key pointer, should be a not-null valid pointer
205160814Ssimon*   int padding: padding id as follow
206296341Sdelphij*                                       SUREWARE_PKCS1_PAD
207296341Sdelphij*                                       SUREWARE_ISO9796_PAD
208160814Ssimon*
209160814Ssimon*/
210296341Sdelphijtypedef int SureWareHook_Rsa_Sign_t(char *const msg, int flen,
211296341Sdelphij                                    unsigned char *from, int *tlen,
212296341Sdelphij                                    unsigned char *to, char *prsa,
213296341Sdelphij                                    int padding);
214160814Ssimonextern SW_EXPORT SureWareHook_Rsa_Sign_t SureWareHook_Rsa_Sign;
215296341Sdelphij/*-
216160814Ssimon* SureWare DSA Signature
217160814Ssimon* return 1 if success
218296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
219296341Sdelphij*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
220296341Sdelphij*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
221160814Ssimon*
222296341Sdelphij*       in param flen : byte size of from and to
223296341Sdelphij*       in param from : encrypted data buffer, should be a not-null valid pointer
224296341Sdelphij*       out param to : decrypted data buffer, should be a 40bytes valid pointer
225160814Ssimon*   in param pdsa: a protected key pointer, should be a not-null valid pointer
226160814Ssimon*
227160814Ssimon*/
228296341Sdelphijtypedef int SureWareHook_Dsa_Sign_t(char *const msg, int flen,
229296341Sdelphij                                    const unsigned char *from,
230296341Sdelphij                                    unsigned long *r, unsigned long *s,
231296341Sdelphij                                    char *pdsa);
232160814Ssimonextern SW_EXPORT SureWareHook_Dsa_Sign_t SureWareHook_Dsa_Sign;
233160814Ssimon
234296341Sdelphij/*-
235160814Ssimon* SureWare Mod Exp
236160814Ssimon* return 1 if success
237296341Sdelphij*                       SureWareHOOK_ERROR_FAILED if error while processing
238296341Sdelphij*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
239296341Sdelphij*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
240160814Ssimon*
241296341Sdelphij*       mod and res are mlen bytes long.
242296341Sdelphij*       exp is elen bytes long
243296341Sdelphij*       data is dlen bytes long
244296341Sdelphij*       mlen,elen and dlen are all multiple of sizeof(unsigned long)
245160814Ssimon*/
246296341Sdelphijtypedef int SureWareHook_Mod_Exp_t(char *const msg, int mlen,
247296341Sdelphij                                   const unsigned long *mod, int elen,
248296341Sdelphij                                   const unsigned long *exponent, int dlen,
249296341Sdelphij                                   unsigned long *data, unsigned long *res);
250160814Ssimonextern SW_EXPORT SureWareHook_Mod_Exp_t SureWareHook_Mod_Exp;
251