cswift.h 9.9 KB

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