1280304Sjkim/*-
2280304Sjkim * Written by Corinne Dive-Reclus(cdive@baltimore.com)
3280304Sjkim *
4280304Sjkim * Copyright@2001 Baltimore Technologies Ltd.
5280304Sjkim *
6280304Sjkim * THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND
7280304Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8280304Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9280304Sjkim * ARE DISCLAIMED.  IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE
10280304Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11280304Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12280304Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13280304Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14280304Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
15280304Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
16280304Sjkim * SUCH DAMAGE.
17280304Sjkim */
18280304Sjkim
19160814Ssimon#ifdef WIN32
20280304Sjkim# define SW_EXPORT       __declspec ( dllexport )
21160814Ssimon#else
22280304Sjkim# define SW_EXPORT
23160814Ssimon#endif
24160814Ssimon
25160814Ssimon/*
26280304Sjkim *       List of exposed SureWare errors
27280304Sjkim */
28280304Sjkim#define SUREWAREHOOK_ERROR_FAILED               -1
29280304Sjkim#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
33280304Sjkim/*-
34160814Ssimon* -----------------WARNING-----------------------------------
35160814Ssimon* In all the following functions:
36160814Ssimon* msg is a string with at least 24 bytes free.
37280304Sjkim* A 24 bytes string will be concatenated to the existing content of msg.
38160814Ssimon*/
39280304Sjkim/*-
40280304Sjkim*       SureWare Initialisation function
41280304Sjkim*       in param threadsafe, if !=0, thread safe enabled
42280304Sjkim*       return SureWareHOOK_ERROR_UNIT_FAILURE if failure, 1 if success
43160814Ssimon*/
44280304Sjkimtypedef int SureWareHook_Init_t(char *const msg, int threadsafe);
45160814Ssimonextern SW_EXPORT SureWareHook_Init_t SureWareHook_Init;
46280304Sjkim/*-
47280304Sjkim*       SureWare Finish function
48160814Ssimon*/
49160814Ssimontypedef void SureWareHook_Finish_t(void);
50160814Ssimonextern SW_EXPORT SureWareHook_Finish_t SureWareHook_Finish;
51280304Sjkim/*-
52280304Sjkim*        PRE_CONDITION:
53280304Sjkim*               DO NOT CALL ANY OF THE FOLLOWING FUNCTIONS IN CASE OF INIT FAILURE
54160814Ssimon*/
55280304Sjkim/*-
56280304Sjkim*       SureWare RAND Bytes function
57280304Sjkim*       In case of failure, the content of buf is unpredictable.
58280304Sjkim*       return 1 if success
59280304Sjkim*                       SureWareHOOK_ERROR_FALLBACK if function not available in hardware
60280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
61280304Sjkim*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
62280304Sjkim*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
63160814Ssimon*
64280304Sjkim*       in/out param buf : a num bytes long buffer where random bytes will be put
65280304Sjkim*       in param num : the number of bytes into buf
66160814Ssimon*/
67280304Sjkimtypedef int SureWareHook_Rand_Bytes_t(char *const msg, unsigned char *buf,
68280304Sjkim                                      int num);
69160814Ssimonextern SW_EXPORT SureWareHook_Rand_Bytes_t SureWareHook_Rand_Bytes;
70160814Ssimon
71280304Sjkim/*-
72280304Sjkim*       SureWare RAND Seed function
73280304Sjkim*       Adds some seed to the Hardware Random Number Generator
74280304Sjkim*       return 1 if success
75280304Sjkim*                       SureWareHOOK_ERROR_FALLBACK if function not available in hardware
76280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
77280304Sjkim*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
78280304Sjkim*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
79160814Ssimon*
80280304Sjkim*       in param buf : the seed to add into the HRNG
81280304Sjkim*       in param num : the number of bytes into buf
82160814Ssimon*/
83280304Sjkimtypedef int SureWareHook_Rand_Seed_t(char *const msg, const void *buf,
84280304Sjkim                                     int num);
85160814Ssimonextern SW_EXPORT SureWareHook_Rand_Seed_t SureWareHook_Rand_Seed;
86160814Ssimon
87280304Sjkim/*-
88280304Sjkim*       SureWare Load Private Key function
89280304Sjkim*       return 1 if success
90280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
91280304Sjkim*       No hardware is contact for this function.
92160814Ssimon*
93280304Sjkim*       in param key_id :the name of the private protected key file without the extension
94280304Sjkim                                                ".sws"
95280304Sjkim*       out param hptr : a pointer to a buffer allocated by SureWare_Hook
96280304Sjkim*       out param num: the effective key length in bytes
97280304Sjkim*       out param keytype: 1 if RSA 2 if DSA
98160814Ssimon*/
99280304Sjkimtypedef int SureWareHook_Load_Privkey_t(char *const msg, const char *key_id,
100280304Sjkim                                        char **hptr, unsigned long *num,
101280304Sjkim                                        char *keytype);
102160814Ssimonextern SW_EXPORT SureWareHook_Load_Privkey_t SureWareHook_Load_Privkey;
103160814Ssimon
104280304Sjkim/*-
105280304Sjkim*       SureWare Info Public Key function
106280304Sjkim*       return 1 if success
107280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
108280304Sjkim*       No hardware is contact for this function.
109160814Ssimon*
110280304Sjkim*       in param key_id :the name of the private protected key file without the extension
111280304Sjkim                                                ".swp"
112280304Sjkim*       out param hptr : a pointer to a buffer allocated by SureWare_Hook
113280304Sjkim*       out param num: the effective key length in bytes
114280304Sjkim*       out param keytype: 1 if RSA 2 if DSA
115160814Ssimon*/
116280304Sjkimtypedef int SureWareHook_Info_Pubkey_t(char *const msg, const char *key_id,
117280304Sjkim                                       unsigned long *num, char *keytype);
118160814Ssimonextern SW_EXPORT SureWareHook_Info_Pubkey_t SureWareHook_Info_Pubkey;
119160814Ssimon
120280304Sjkim/*-
121280304Sjkim*       SureWare Load Public Key function
122280304Sjkim*       return 1 if success
123280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
124280304Sjkim*       No hardware is contact for this function.
125160814Ssimon*
126280304Sjkim*       in param key_id :the name of the public protected key file without the extension
127280304Sjkim                                                ".swp"
128280304Sjkim*       in param num : the bytes size of n and e
129280304Sjkim*       out param n: where to write modulus in bn format
130280304Sjkim*       out param e: where to write exponent in bn format
131160814Ssimon*/
132280304Sjkimtypedef int SureWareHook_Load_Rsa_Pubkey_t(char *const msg,
133280304Sjkim                                           const char *key_id,
134280304Sjkim                                           unsigned long num,
135280304Sjkim                                           unsigned long *n,
136280304Sjkim                                           unsigned long *e);
137160814Ssimonextern SW_EXPORT SureWareHook_Load_Rsa_Pubkey_t SureWareHook_Load_Rsa_Pubkey;
138160814Ssimon
139280304Sjkim/*-
140280304Sjkim*       SureWare Load DSA Public Key function
141280304Sjkim*       return 1 if success
142280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
143280304Sjkim*       No hardware is contact for this function.
144160814Ssimon*
145280304Sjkim*       in param key_id :the name of the public protected key file without the extension
146280304Sjkim                                                ".swp"
147280304Sjkim*       in param num : the bytes size of n and e
148280304Sjkim*       out param pub: where to write pub key in bn format
149280304Sjkim*       out param p: where to write prime in bn format
150280304Sjkim*       out param q: where to write sunprime (length 20 bytes) in bn format
151280304Sjkim*       out param g: where to write base in bn format
152160814Ssimon*/
153280304Sjkimtypedef int SureWareHook_Load_Dsa_Pubkey_t(char *const msg,
154280304Sjkim                                           const char *key_id,
155280304Sjkim                                           unsigned long num,
156280304Sjkim                                           unsigned long *pub,
157280304Sjkim                                           unsigned long *p, unsigned long *q,
158280304Sjkim                                           unsigned long *g);
159160814Ssimonextern SW_EXPORT SureWareHook_Load_Dsa_Pubkey_t SureWareHook_Load_Dsa_Pubkey;
160160814Ssimon
161280304Sjkim/*-
162280304Sjkim*       SureWare Free function
163280304Sjkim*       Destroy the key into the hardware if destroy==1
164160814Ssimon*/
165280304Sjkimtypedef 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
171280304Sjkim/*-
172160814Ssimon* SureWare RSA Private Decryption
173160814Ssimon* return 1 if success
174280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
175280304Sjkim*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
176280304Sjkim*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
177160814Ssimon*
178280304Sjkim*       in param flen : byte size of from and to
179280304Sjkim*       in param from : encrypted data buffer, should be a not-null valid pointer
180280304Sjkim*       out param tlen: byte size of decrypted data, if error, unexpected value
181280304Sjkim*       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
184280304Sjkim*                                       SUREWARE_PKCS1_PAD
185280304Sjkim*                                       SUREWARE_NO_PAD
186160814Ssimon*
187160814Ssimon*/
188280304Sjkimtypedef int SureWareHook_Rsa_Priv_Dec_t(char *const msg, int flen,
189280304Sjkim                                        unsigned char *from, int *tlen,
190280304Sjkim                                        unsigned char *to, char *prsa,
191280304Sjkim                                        int padding);
192160814Ssimonextern SW_EXPORT SureWareHook_Rsa_Priv_Dec_t SureWareHook_Rsa_Priv_Dec;
193280304Sjkim/*-
194160814Ssimon* SureWare RSA Signature
195160814Ssimon* return 1 if success
196280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
197280304Sjkim*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
198280304Sjkim*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
199160814Ssimon*
200280304Sjkim*       in param flen : byte size of from and to
201280304Sjkim*       in param from : encrypted data buffer, should be a not-null valid pointer
202280304Sjkim*       out param tlen: byte size of decrypted data, if error, unexpected value
203280304Sjkim*       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
206280304Sjkim*                                       SUREWARE_PKCS1_PAD
207280304Sjkim*                                       SUREWARE_ISO9796_PAD
208160814Ssimon*
209160814Ssimon*/
210280304Sjkimtypedef int SureWareHook_Rsa_Sign_t(char *const msg, int flen,
211280304Sjkim                                    unsigned char *from, int *tlen,
212280304Sjkim                                    unsigned char *to, char *prsa,
213280304Sjkim                                    int padding);
214160814Ssimonextern SW_EXPORT SureWareHook_Rsa_Sign_t SureWareHook_Rsa_Sign;
215280304Sjkim/*-
216160814Ssimon* SureWare DSA Signature
217160814Ssimon* return 1 if success
218280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
219280304Sjkim*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
220280304Sjkim*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
221160814Ssimon*
222280304Sjkim*       in param flen : byte size of from and to
223280304Sjkim*       in param from : encrypted data buffer, should be a not-null valid pointer
224280304Sjkim*       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*/
228280304Sjkimtypedef int SureWareHook_Dsa_Sign_t(char *const msg, int flen,
229280304Sjkim                                    const unsigned char *from,
230280304Sjkim                                    unsigned long *r, unsigned long *s,
231280304Sjkim                                    char *pdsa);
232160814Ssimonextern SW_EXPORT SureWareHook_Dsa_Sign_t SureWareHook_Dsa_Sign;
233160814Ssimon
234280304Sjkim/*-
235160814Ssimon* SureWare Mod Exp
236160814Ssimon* return 1 if success
237280304Sjkim*                       SureWareHOOK_ERROR_FAILED if error while processing
238280304Sjkim*                       SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
239280304Sjkim*                       SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
240160814Ssimon*
241280304Sjkim*       mod and res are mlen bytes long.
242280304Sjkim*       exp is elen bytes long
243280304Sjkim*       data is dlen bytes long
244280304Sjkim*       mlen,elen and dlen are all multiple of sizeof(unsigned long)
245160814Ssimon*/
246280304Sjkimtypedef int SureWareHook_Mod_Exp_t(char *const msg, int mlen,
247280304Sjkim                                   const unsigned long *mod, int elen,
248280304Sjkim                                   const unsigned long *exponent, int dlen,
249280304Sjkim                                   unsigned long *data, unsigned long *res);
250160814Ssimonextern SW_EXPORT SureWareHook_Mod_Exp_t SureWareHook_Mod_Exp;
251