cryptop.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. cryptop.h
  5. Abstract:
  6. This header contains internal definitions for the Cryptographic Library.
  7. Author:
  8. Evan Green 13-Jan-2015
  9. --*/
  10. //
  11. // ------------------------------------------------------------------- Includes
  12. //
  13. #define CRYPTO_API __DLLEXPORT
  14. #define RTL_API __DLLPROTECTED
  15. #include <minoca/lib/types.h>
  16. #include <minoca/lib/status.h>
  17. #include <minoca/lib/rtl.h>
  18. #include <minoca/lib/crypto.h>
  19. //
  20. // ---------------------------------------------------------------- Definitions
  21. //
  22. #define BIG_INTEGER_RADIX 0x100000000ULL
  23. //
  24. // Define the modulo indices.
  25. //
  26. #define BIG_INTEGER_M_OFFSET 0
  27. #define BIG_INTEGER_P_OFFSET 1
  28. #define BIG_INTEGER_Q_OFFSET 2
  29. //
  30. // ------------------------------------------------------ Data Type Definitions
  31. //
  32. //
  33. // -------------------------------------------------------------------- Globals
  34. //
  35. //
  36. // -------------------------------------------------------- Function Prototypes
  37. //
  38. //
  39. // Big integer functions
  40. //
  41. KSTATUS
  42. CypBiInitializeContext (
  43. PBIG_INTEGER_CONTEXT Context
  44. );
  45. /*++
  46. Routine Description:
  47. This routine initializes a big integer context.
  48. Arguments:
  49. Context - Supplies a pointer to the context to initialize.
  50. Return Value:
  51. STATUS_SUCCESS on success.
  52. STATUS_INVALID_PARAMETER if the context was not partially filled
  53. in correctly.
  54. STATUS_INSUFFICIENT_RESOURCES if an allocation failed.
  55. --*/
  56. VOID
  57. CypBiDestroyContext (
  58. PBIG_INTEGER_CONTEXT Context
  59. );
  60. /*++
  61. Routine Description:
  62. This routine destroys a big integer context.
  63. Arguments:
  64. Context - Supplies a pointer to the context to tear down.
  65. Return Value:
  66. None.
  67. --*/
  68. VOID
  69. CypBiClearCache (
  70. PBIG_INTEGER_CONTEXT Context
  71. );
  72. /*++
  73. Routine Description:
  74. This routine destroys all big integers on the free list for the given
  75. context.
  76. Arguments:
  77. Context - Supplies a pointer to the context to clear.
  78. for.
  79. Return Value:
  80. None.
  81. --*/
  82. KSTATUS
  83. CypBiCalculateModuli (
  84. PBIG_INTEGER_CONTEXT Context,
  85. PBIG_INTEGER Value,
  86. INTN ModOffset
  87. );
  88. /*++
  89. Routine Description:
  90. This routine performs some pre-calculations used in modulo reduction
  91. optimizations.
  92. Arguments:
  93. Context - Supplies a pointer to the big integer context.
  94. Value - Supplies a pointer to the modulus that will be used. This value
  95. will be made permanent.
  96. ModOffset - Supplies an offset to the moduli that can be used: the standard
  97. moduli, or the primes p and q.
  98. Return Value:
  99. STATUS_SUCCESS on success.
  100. STATUS_INSUFFICIENT_RESOURCES if an allocation failed.
  101. --*/
  102. VOID
  103. CypBiReleaseModuli (
  104. PBIG_INTEGER_CONTEXT Context,
  105. INTN ModOffset
  106. );
  107. /*++
  108. Routine Description:
  109. This routine frees memory associated with moduli for the given offset.
  110. Arguments:
  111. Context - Supplies a pointer to the big integer context.
  112. ModOffset - Supplies the index of the moduli to free: the standard
  113. moduli, or the primes p and q.
  114. Return Value:
  115. None.
  116. --*/
  117. PBIG_INTEGER
  118. CypBiExponentiateModulo (
  119. PBIG_INTEGER_CONTEXT Context,
  120. PBIG_INTEGER Value,
  121. PBIG_INTEGER Exponent
  122. );
  123. /*++
  124. Routine Description:
  125. This routine performs exponentiation, modulo a value.
  126. Arguments:
  127. Context - Supplies a pointer to the big integer context.
  128. Value - Supplies a pointer to the value to reduce. A reference on this
  129. value will be released on success.
  130. Exponent - Supplies the exponent to raise the value to. A reference on this
  131. value will be released on success.
  132. Return Value:
  133. Returns a pointer to the exponentiated value on success.
  134. NULL on allocation failure.
  135. --*/
  136. PBIG_INTEGER
  137. CypBiChineseRemainderTheorem (
  138. PBIG_INTEGER_CONTEXT Context,
  139. PBIG_INTEGER Value,
  140. PBIG_INTEGER DpValue,
  141. PBIG_INTEGER DqValue,
  142. PBIG_INTEGER PValue,
  143. PBIG_INTEGER QValue,
  144. PBIG_INTEGER QInverse
  145. );
  146. /*++
  147. Routine Description:
  148. This routine uses the Chinese Remainder Theorem as an aide to quickly
  149. decrypting RSA values.
  150. Arguments:
  151. Context - Supplies a pointer to the big integer context.
  152. Value - Supplies a pointer to the value to perform the exponentiation on. A
  153. reference on this value will be released on success.
  154. DpValue - Supplies a pointer to the dP value. A reference on this will be
  155. released on success.
  156. DqValue - Supplies a pointer to the dQ value. A reference on this will be
  157. released on success.
  158. PValue - Supplies a pointer to the p prime. A reference on this value will
  159. be released on success.
  160. QValue - Supplies a pointer to the q prime. A reference on this value will
  161. be released on success.
  162. QInverse - Supplies a pointer to the Q inverse. A reference on this will be
  163. released on success.
  164. Return Value:
  165. Returns a pointer to the result of the Chinese Remainder Theorem on success.
  166. NULL on allocation failure.
  167. --*/
  168. PBIG_INTEGER
  169. CypBiImport (
  170. PBIG_INTEGER_CONTEXT Context,
  171. PVOID Data,
  172. UINTN Size
  173. );
  174. /*++
  175. Routine Description:
  176. This routine creates a big integer from a set of raw binary bytes.
  177. Arguments:
  178. Context - Supplies a pointer to the big integer context.
  179. Data - Supplies a pointer to the data to import.
  180. Size - Supplies the number of bytes in the data.
  181. Return Value:
  182. Returns a pointer to the newly created value on success.
  183. NULL on allocation failure.
  184. --*/
  185. KSTATUS
  186. CypBiExport (
  187. PBIG_INTEGER_CONTEXT Context,
  188. PBIG_INTEGER Value,
  189. PVOID Data,
  190. UINTN Size
  191. );
  192. /*++
  193. Routine Description:
  194. This routine exports a big integer to a byte stream.
  195. Arguments:
  196. Context - Supplies a pointer to the big integer context.
  197. Value - Supplies a pointer to the big integer to export. A reference is
  198. released on this value by this function on success.
  199. Data - Supplies a pointer to the data buffer.
  200. Size - Supplies the number of bytes in the data buffer.
  201. Return Value:
  202. STATUS_SUCCESS on success.
  203. STATUS_BUFFER_TOO_SMALL if the given buffer was not big enough to hold the
  204. entire integer.
  205. --*/
  206. VOID
  207. CypBiDebugPrint (
  208. PBIG_INTEGER Value
  209. );
  210. /*++
  211. Routine Description:
  212. This routine debug prints the contents of a big integer.
  213. Arguments:
  214. Value - Supplies a pointer to the value to print.
  215. Return Value:
  216. None.
  217. --*/
  218. VOID
  219. CypBiAddReference (
  220. PBIG_INTEGER Integer
  221. );
  222. /*++
  223. Routine Description:
  224. This routine adds a reference to the given big integer.
  225. Arguments:
  226. Integer - Supplies a pointer to the big integer.
  227. Return Value:
  228. None.
  229. --*/
  230. VOID
  231. CypBiReleaseReference (
  232. PBIG_INTEGER_CONTEXT Context,
  233. PBIG_INTEGER Integer
  234. );
  235. /*++
  236. Routine Description:
  237. This routine releases resources associated with a big integer.
  238. Arguments:
  239. Context - Supplies a pointer to the context that owns the integer.
  240. Integer - Supplies a pointer to the big integer.
  241. ComponentCount - Supplies the number of components to allocate
  242. for.
  243. Return Value:
  244. None.
  245. --*/
  246. VOID
  247. CypBiMakePermanent (
  248. PBIG_INTEGER Integer
  249. );
  250. /*++
  251. Routine Description:
  252. This routine makes a big integer "permanent", causing add and release
  253. references to be ignored.
  254. Arguments:
  255. Integer - Supplies a pointer to the big integer.
  256. Return Value:
  257. None.
  258. --*/
  259. VOID
  260. CypBiMakeNonPermanent (
  261. PBIG_INTEGER Integer
  262. );
  263. /*++
  264. Routine Description:
  265. This routine undoes the effects of making a big integer permanent,
  266. instead giving it a reference count of 1.
  267. Arguments:
  268. Integer - Supplies a pointer to the big integer.
  269. Return Value:
  270. None.
  271. --*/
  272. KSTATUS
  273. CypAsn1AddPrivateKey (
  274. PRSA_CONTEXT RsaContext,
  275. PVOID PemData,
  276. UINTN PemDataSize
  277. );
  278. /*++
  279. Routine Description:
  280. This routine extracts a private key given ASN.1 data.
  281. Arguments:
  282. RsaContext - Supplies a pointer to an initialized RSA context where the
  283. private key should be placed.
  284. PemData - Supplies a pointer to the ASN.1 data, which must have already
  285. been Base64 decoded and decrypted if necessary prior to calling this
  286. function.
  287. PemDataSize - Supplies the size of the ASN data in bytes.
  288. Return Value:
  289. Status code.
  290. --*/