sureware.h 11 KB

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