sureware.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * List of exposed SureWare errors
  27. */
  28. #define SUREWAREHOOK_ERROR_FAILED -1
  29. #define SUREWAREHOOK_ERROR_FALLBACK -2
  30. #define SUREWAREHOOK_ERROR_UNIT_FAILURE -3
  31. #define SUREWAREHOOK_ERROR_DATA_SIZE -4
  32. #define SUREWAREHOOK_ERROR_INVALID_PAD -5
  33. /*
  34. * -----------------WARNING-----------------------------------
  35. * In all the following functions:
  36. * msg is a string with at least 24 bytes free.
  37. * A 24 bytes string will be concatenated to the existing content of msg.
  38. */
  39. /*
  40. * SureWare Initialisation function
  41. * in param threadsafe, if !=0, thread safe enabled
  42. * return SureWareHOOK_ERROR_UNIT_FAILURE if failure, 1 if success
  43. */
  44. typedef int SureWareHook_Init_t(char*const msg,int threadsafe);
  45. extern SW_EXPORT SureWareHook_Init_t SureWareHook_Init;
  46. /*
  47. * SureWare Finish function
  48. */
  49. typedef void SureWareHook_Finish_t(void);
  50. extern SW_EXPORT SureWareHook_Finish_t SureWareHook_Finish;
  51. /*
  52. * PRE_CONDITION:
  53. * DO NOT CALL ANY OF THE FOLLOWING FUNCTIONS IN CASE OF INIT FAILURE
  54. */
  55. /*
  56. * SureWare RAND Bytes function
  57. * In case of failure, the content of buf is unpredictable.
  58. * return 1 if success
  59. * SureWareHOOK_ERROR_FALLBACK if function not available in hardware
  60. * SureWareHOOK_ERROR_FAILED if error while processing
  61. * SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
  62. * SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
  63. *
  64. * in/out param buf : a num bytes long buffer where random bytes will be put
  65. * in param num : the number of bytes into buf
  66. */
  67. typedef int SureWareHook_Rand_Bytes_t(char*const msg,unsigned char *buf, int num);
  68. extern SW_EXPORT SureWareHook_Rand_Bytes_t SureWareHook_Rand_Bytes;
  69. /*
  70. * SureWare RAND Seed function
  71. * Adds some seed to the Hardware Random Number Generator
  72. * return 1 if success
  73. * SureWareHOOK_ERROR_FALLBACK if function not available in hardware
  74. * SureWareHOOK_ERROR_FAILED if error while processing
  75. * SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
  76. * SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
  77. *
  78. * in param buf : the seed to add into the HRNG
  79. * in param num : the number of bytes into buf
  80. */
  81. typedef int SureWareHook_Rand_Seed_t(char*const msg,const void *buf, int num);
  82. extern SW_EXPORT SureWareHook_Rand_Seed_t SureWareHook_Rand_Seed;
  83. /*
  84. * SureWare Load Private Key function
  85. * return 1 if success
  86. * SureWareHOOK_ERROR_FAILED if error while processing
  87. * No hardware is contact for this function.
  88. *
  89. * in param key_id :the name of the private protected key file without the extension
  90. ".sws"
  91. * out param hptr : a pointer to a buffer allocated by SureWare_Hook
  92. * out param num: the effective key length in bytes
  93. * out param keytype: 1 if RSA 2 if DSA
  94. */
  95. typedef int SureWareHook_Load_Privkey_t(char*const msg,const char *key_id,char **hptr,unsigned long *num,char *keytype);
  96. extern SW_EXPORT SureWareHook_Load_Privkey_t SureWareHook_Load_Privkey;
  97. /*
  98. * SureWare Info Public Key function
  99. * return 1 if success
  100. * SureWareHOOK_ERROR_FAILED if error while processing
  101. * No hardware is contact for this function.
  102. *
  103. * in param key_id :the name of the private protected key file without the extension
  104. ".swp"
  105. * out param hptr : a pointer to a buffer allocated by SureWare_Hook
  106. * out param num: the effective key length in bytes
  107. * out param keytype: 1 if RSA 2 if DSA
  108. */
  109. typedef int SureWareHook_Info_Pubkey_t(char*const msg,const char *key_id,unsigned long *num,
  110. char *keytype);
  111. extern SW_EXPORT SureWareHook_Info_Pubkey_t SureWareHook_Info_Pubkey;
  112. /*
  113. * SureWare Load Public Key function
  114. * return 1 if success
  115. * SureWareHOOK_ERROR_FAILED if error while processing
  116. * No hardware is contact for this function.
  117. *
  118. * in param key_id :the name of the public protected key file without the extension
  119. ".swp"
  120. * in param num : the bytes size of n and e
  121. * out param n: where to write modulus in bn format
  122. * out param e: where to write exponent in bn format
  123. */
  124. typedef int SureWareHook_Load_Rsa_Pubkey_t(char*const msg,const char *key_id,unsigned long num,
  125. unsigned long *n, unsigned long *e);
  126. extern SW_EXPORT SureWareHook_Load_Rsa_Pubkey_t SureWareHook_Load_Rsa_Pubkey;
  127. /*
  128. * SureWare Load DSA Public Key function
  129. * return 1 if success
  130. * SureWareHOOK_ERROR_FAILED if error while processing
  131. * No hardware is contact for this function.
  132. *
  133. * in param key_id :the name of the public protected key file without the extension
  134. ".swp"
  135. * in param num : the bytes size of n and e
  136. * out param pub: where to write pub key in bn format
  137. * out param p: where to write prime in bn format
  138. * out param q: where to write sunprime (length 20 bytes) in bn format
  139. * out param g: where to write base in bn format
  140. */
  141. typedef int SureWareHook_Load_Dsa_Pubkey_t(char*const msg,const char *key_id,unsigned long num,
  142. unsigned long *pub, unsigned long *p,unsigned long*q,
  143. unsigned long *g);
  144. extern SW_EXPORT SureWareHook_Load_Dsa_Pubkey_t SureWareHook_Load_Dsa_Pubkey;
  145. /*
  146. * SureWare Free function
  147. * Destroy the key into the hardware if destroy==1
  148. */
  149. typedef void SureWareHook_Free_t(char *p,int destroy);
  150. extern SW_EXPORT SureWareHook_Free_t SureWareHook_Free;
  151. #define SUREWARE_PKCS1_PAD 1
  152. #define SUREWARE_ISO9796_PAD 2
  153. #define SUREWARE_NO_PAD 0
  154. /*
  155. * SureWare RSA Private Decryption
  156. * return 1 if success
  157. * SureWareHOOK_ERROR_FAILED if error while processing
  158. * SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
  159. * SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
  160. *
  161. * in param flen : byte size of from and to
  162. * in param from : encrypted data buffer, should be a not-null valid pointer
  163. * out param tlen: byte size of decrypted data, if error, unexpected value
  164. * out param to : decrypted data buffer, should be a not-null valid pointer
  165. * in param prsa: a protected key pointer, should be a not-null valid pointer
  166. * int padding: padding id as follow
  167. * SUREWARE_PKCS1_PAD
  168. * SUREWARE_NO_PAD
  169. *
  170. */
  171. typedef int SureWareHook_Rsa_Priv_Dec_t(char*const msg,int flen,unsigned char *from,
  172. int *tlen,unsigned char *to,
  173. char *prsa,int padding);
  174. extern SW_EXPORT SureWareHook_Rsa_Priv_Dec_t SureWareHook_Rsa_Priv_Dec;
  175. /*
  176. * SureWare RSA Signature
  177. * return 1 if success
  178. * SureWareHOOK_ERROR_FAILED if error while processing
  179. * SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
  180. * SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
  181. *
  182. * in param flen : byte size of from and to
  183. * in param from : encrypted data buffer, should be a not-null valid pointer
  184. * out param tlen: byte size of decrypted data, if error, unexpected value
  185. * out param to : decrypted data buffer, should be a not-null valid pointer
  186. * in param prsa: a protected key pointer, should be a not-null valid pointer
  187. * int padding: padding id as follow
  188. * SUREWARE_PKCS1_PAD
  189. * SUREWARE_ISO9796_PAD
  190. *
  191. */
  192. typedef int SureWareHook_Rsa_Sign_t(char*const msg,int flen,unsigned char *from,
  193. int *tlen,unsigned char *to,
  194. char *prsa,int padding);
  195. extern SW_EXPORT SureWareHook_Rsa_Sign_t SureWareHook_Rsa_Sign;
  196. /*
  197. * SureWare DSA Signature
  198. * return 1 if success
  199. * SureWareHOOK_ERROR_FAILED if error while processing
  200. * SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
  201. * SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
  202. *
  203. * in param flen : byte size of from and to
  204. * in param from : encrypted data buffer, should be a not-null valid pointer
  205. * out param to : decrypted data buffer, should be a 40bytes valid pointer
  206. * in param pdsa: a protected key pointer, should be a not-null valid pointer
  207. *
  208. */
  209. typedef int SureWareHook_Dsa_Sign_t(char*const msg,int flen,const unsigned char *from,
  210. unsigned long *r,unsigned long *s,char *pdsa);
  211. extern SW_EXPORT SureWareHook_Dsa_Sign_t SureWareHook_Dsa_Sign;
  212. /*
  213. * SureWare Mod Exp
  214. * return 1 if success
  215. * SureWareHOOK_ERROR_FAILED if error while processing
  216. * SureWareHOOK_ERROR_UNIT_FAILURE if hardware failure
  217. * SUREWAREHOOK_ERROR_DATA_SIZE wrong size for buf
  218. *
  219. * mod and res are mlen bytes long.
  220. * exp is elen bytes long
  221. * data is dlen bytes long
  222. * mlen,elen and dlen are all multiple of sizeof(unsigned long)
  223. */
  224. typedef int SureWareHook_Mod_Exp_t(char*const msg,int mlen,const unsigned long *mod,
  225. int elen,const unsigned long *exponent,
  226. int dlen,unsigned long *data,
  227. unsigned long *res);
  228. extern SW_EXPORT SureWareHook_Mod_Exp_t SureWareHook_Mod_Exp;