cswift.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Attribution notice: Rainbow have generously allowed me to reproduce
  2. * the necessary definitions here from their API. This means the support
  3. * can build independently of whether application builders have the
  4. * API or hardware. This will allow developers to easily produce software
  5. * that has latent hardware support for any users that have accelertors
  6. * installed, without the developers themselves needing anything extra.
  7. *
  8. * I have only clipped the parts from the CryptoSwift header files that
  9. * are (or seem) relevant to the CryptoSwift support code. This is
  10. * simply to keep the file sizes reasonable.
  11. * [Geoff]
  12. */
  13. /* NB: These type widths do *not* seem right in general, in particular
  14. * they're not terribly friendly to 64-bit architectures (unsigned long)
  15. * will be 64-bit on IA-64 for a start. I'm leaving these alone as they
  16. * agree with Rainbow's API and this will only be called into question
  17. * on platforms with Rainbow support anyway! ;-) */
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif /* __cplusplus */
  21. typedef long SW_STATUS; /* status */
  22. typedef unsigned char SW_BYTE; /* 8 bit byte */
  23. typedef unsigned short SW_U16; /* 16 bit number */
  24. #if defined(_IRIX)
  25. #include <sgidefs.h>
  26. typedef __uint32_t SW_U32;
  27. #else
  28. typedef unsigned long SW_U32; /* 32 bit integer */
  29. #endif
  30. #if defined(OPENSSL_SYS_WIN32)
  31. typedef struct _SW_U64 {
  32. SW_U32 low32;
  33. SW_U32 high32;
  34. } SW_U64; /* 64 bit integer */
  35. #elif defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
  36. typedef longlong SW_U64
  37. #else /* Unix variants */
  38. typedef struct _SW_U64 {
  39. SW_U32 low32;
  40. SW_U32 high32;
  41. } SW_U64; /* 64 bit integer */
  42. #endif
  43. /* status codes */
  44. #define SW_OK (0L)
  45. #define SW_ERR_BASE (-10000L)
  46. #define SW_ERR_NO_CARD (SW_ERR_BASE-1) /* The Card is not present */
  47. #define SW_ERR_CARD_NOT_READY (SW_ERR_BASE-2) /* The card has not powered */
  48. /* up yet */
  49. #define SW_ERR_TIME_OUT (SW_ERR_BASE-3) /* Execution of a command */
  50. /* time out */
  51. #define SW_ERR_NO_EXECUTE (SW_ERR_BASE-4) /* The Card failed to */
  52. /* execute the command */
  53. #define SW_ERR_INPUT_NULL_PTR (SW_ERR_BASE-5) /* a required pointer is */
  54. /* NULL */
  55. #define SW_ERR_INPUT_SIZE (SW_ERR_BASE-6) /* size is invalid, too */
  56. /* small, too large. */
  57. #define SW_ERR_INVALID_HANDLE (SW_ERR_BASE-7) /* Invalid SW_ACC_CONTEXT */
  58. /* handle */
  59. #define SW_ERR_PENDING (SW_ERR_BASE-8) /* A request is already out- */
  60. /* standing at this */
  61. /* context handle */
  62. #define SW_ERR_AVAILABLE (SW_ERR_BASE-9) /* A result is available. */
  63. #define SW_ERR_NO_PENDING (SW_ERR_BASE-10)/* No request is pending. */
  64. #define SW_ERR_NO_MEMORY (SW_ERR_BASE-11)/* Not enough memory */
  65. #define SW_ERR_BAD_ALGORITHM (SW_ERR_BASE-12)/* Invalid algorithm type */
  66. /* in SW_PARAM structure */
  67. #define SW_ERR_MISSING_KEY (SW_ERR_BASE-13)/* No key is associated with */
  68. /* context. */
  69. /* swAttachKeyParam() is */
  70. /* not called. */
  71. #define SW_ERR_KEY_CMD_MISMATCH \
  72. (SW_ERR_BASE-14)/* Cannot perform requested */
  73. /* SW_COMMAND_CODE since */
  74. /* key attached via */
  75. /* swAttachKeyParam() */
  76. /* cannot be used for this*/
  77. /* SW_COMMAND_CODE. */
  78. #define SW_ERR_NOT_IMPLEMENTED \
  79. (SW_ERR_BASE-15)/* Not implemented */
  80. #define SW_ERR_BAD_COMMAND (SW_ERR_BASE-16)/* Bad command code */
  81. #define SW_ERR_BAD_ITEM_SIZE (SW_ERR_BASE-17)/* too small or too large in */
  82. /* the "initems" or */
  83. /* "outitems". */
  84. #define SW_ERR_BAD_ACCNUM (SW_ERR_BASE-18)/* Bad accelerator number */
  85. #define SW_ERR_SELFTEST_FAIL (SW_ERR_BASE-19)/* At least one of the self */
  86. /* test fail, look at the */
  87. /* selfTestBitmap in */
  88. /* SW_ACCELERATOR_INFO for*/
  89. /* details. */
  90. #define SW_ERR_MISALIGN (SW_ERR_BASE-20)/* Certain alogrithms require*/
  91. /* key materials aligned */
  92. /* in certain order, e.g. */
  93. /* 128 bit for CRT */
  94. #define SW_ERR_OUTPUT_NULL_PTR \
  95. (SW_ERR_BASE-21)/* a required pointer is */
  96. /* NULL */
  97. #define SW_ERR_OUTPUT_SIZE \
  98. (SW_ERR_BASE-22)/* size is invalid, too */
  99. /* small, too large. */
  100. #define SW_ERR_FIRMWARE_CHECKSUM \
  101. (SW_ERR_BASE-23)/* firmware checksum mismatch*/
  102. /* download failed. */
  103. #define SW_ERR_UNKNOWN_FIRMWARE \
  104. (SW_ERR_BASE-24)/* unknown firmware error */
  105. #define SW_ERR_INTERRUPT (SW_ERR_BASE-25)/* request is abort when */
  106. /* it's waiting to be */
  107. /* completed. */
  108. #define SW_ERR_NVWRITE_FAIL (SW_ERR_BASE-26)/* error in writing to Non- */
  109. /* volatile memory */
  110. #define SW_ERR_NVWRITE_RANGE (SW_ERR_BASE-27)/* out of range error in */
  111. /* writing to NV memory */
  112. #define SW_ERR_RNG_ERROR (SW_ERR_BASE-28)/* Random Number Generation */
  113. /* failure */
  114. #define SW_ERR_DSS_FAILURE (SW_ERR_BASE-29)/* DSS Sign or Verify failure*/
  115. #define SW_ERR_MODEXP_FAILURE (SW_ERR_BASE-30)/* Failure in various math */
  116. /* calculations */
  117. #define SW_ERR_ONBOARD_MEMORY (SW_ERR_BASE-31)/* Error in accessing on - */
  118. /* board memory */
  119. #define SW_ERR_FIRMWARE_VERSION \
  120. (SW_ERR_BASE-32)/* Wrong version in firmware */
  121. /* update */
  122. #define SW_ERR_ZERO_WORKING_ACCELERATOR \
  123. (SW_ERR_BASE-44)/* All accelerators are bad */
  124. /* algorithm type */
  125. #define SW_ALG_CRT 1
  126. #define SW_ALG_EXP 2
  127. #define SW_ALG_DSA 3
  128. #define SW_ALG_NVDATA 4
  129. /* command code */
  130. #define SW_CMD_MODEXP_CRT 1 /* perform Modular Exponentiation using */
  131. /* Chinese Remainder Theorem (CRT) */
  132. #define SW_CMD_MODEXP 2 /* perform Modular Exponentiation */
  133. #define SW_CMD_DSS_SIGN 3 /* perform DSS sign */
  134. #define SW_CMD_DSS_VERIFY 4 /* perform DSS verify */
  135. #define SW_CMD_RAND 5 /* perform random number generation */
  136. #define SW_CMD_NVREAD 6 /* perform read to nonvolatile RAM */
  137. #define SW_CMD_NVWRITE 7 /* perform write to nonvolatile RAM */
  138. typedef SW_U32 SW_ALGTYPE; /* alogrithm type */
  139. typedef SW_U32 SW_STATE; /* state */
  140. typedef SW_U32 SW_COMMAND_CODE; /* command code */
  141. typedef SW_U32 SW_COMMAND_BITMAP[4]; /* bitmap */
  142. typedef struct _SW_LARGENUMBER {
  143. SW_U32 nbytes; /* number of bytes in the buffer "value" */
  144. SW_BYTE* value; /* the large integer as a string of */
  145. /* bytes in network (big endian) order */
  146. } SW_LARGENUMBER;
  147. #if defined(OPENSSL_SYS_WIN32)
  148. #include <windows.h>
  149. typedef HANDLE SW_OSHANDLE; /* handle to kernel object */
  150. #define SW_OS_INVALID_HANDLE INVALID_HANDLE_VALUE
  151. #define SW_CALLCONV _stdcall
  152. #elif defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
  153. /* async callback mechanisms */
  154. /* swiftCallbackLevel */
  155. #define SW_MAC_CALLBACK_LEVEL_NO 0
  156. #define SW_MAC_CALLBACK_LEVEL_HARDWARE 1 /* from the hardware ISR */
  157. #define SW_MAC_CALLBACK_LEVEL_SECONDARY 2 /* as secondary ISR */
  158. typedef int SW_MAC_CALLBACK_LEVEL;
  159. typedef int SW_OSHANDLE;
  160. #define SW_OS_INVALID_HANDLE (-1)
  161. #define SW_CALLCONV
  162. #else /* Unix variants */
  163. typedef int SW_OSHANDLE; /* handle to driver */
  164. #define SW_OS_INVALID_HANDLE (-1)
  165. #define SW_CALLCONV
  166. #endif
  167. typedef struct _SW_CRT {
  168. SW_LARGENUMBER p; /* prime number p */
  169. SW_LARGENUMBER q; /* prime number q */
  170. SW_LARGENUMBER dmp1; /* exponent1 */
  171. SW_LARGENUMBER dmq1; /* exponent2 */
  172. SW_LARGENUMBER iqmp; /* CRT coefficient */
  173. } SW_CRT;
  174. typedef struct _SW_EXP {
  175. SW_LARGENUMBER modulus; /* modulus */
  176. SW_LARGENUMBER exponent;/* exponent */
  177. } SW_EXP;
  178. typedef struct _SW_DSA {
  179. SW_LARGENUMBER p; /* */
  180. SW_LARGENUMBER q; /* */
  181. SW_LARGENUMBER g; /* */
  182. SW_LARGENUMBER key; /* private/public key */
  183. } SW_DSA;
  184. typedef struct _SW_NVDATA {
  185. SW_U32 accnum; /* accelerator board number */
  186. SW_U32 offset; /* offset in byte */
  187. } SW_NVDATA;
  188. typedef struct _SW_PARAM {
  189. SW_ALGTYPE type; /* type of the alogrithm */
  190. union {
  191. SW_CRT crt;
  192. SW_EXP exp;
  193. SW_DSA dsa;
  194. SW_NVDATA nvdata;
  195. } up;
  196. } SW_PARAM;
  197. typedef SW_U32 SW_CONTEXT_HANDLE; /* opaque context handle */
  198. /* Now the OpenSSL bits, these function types are the for the function
  199. * pointers that will bound into the Rainbow shared libraries. */
  200. typedef SW_STATUS SW_CALLCONV t_swAcquireAccContext(SW_CONTEXT_HANDLE *hac);
  201. typedef SW_STATUS SW_CALLCONV t_swAttachKeyParam(SW_CONTEXT_HANDLE hac,
  202. SW_PARAM *key_params);
  203. typedef SW_STATUS SW_CALLCONV t_swSimpleRequest(SW_CONTEXT_HANDLE hac,
  204. SW_COMMAND_CODE cmd,
  205. SW_LARGENUMBER pin[],
  206. SW_U32 pin_count,
  207. SW_LARGENUMBER pout[],
  208. SW_U32 pout_count);
  209. typedef SW_STATUS SW_CALLCONV t_swReleaseAccContext(SW_CONTEXT_HANDLE hac);
  210. #ifdef __cplusplus
  211. }
  212. #endif /* __cplusplus */