Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

426 lines
13 KiB

  1. #ifndef __RSA_H__
  2. #define __RSA_H__
  3. #ifndef RSA32API
  4. #define RSA32API __stdcall
  5. #endif
  6. /* rsa.h
  7. *
  8. * RSA library functions.
  9. *
  10. * Copyright (C) RSA Data Security, Inc. created 1990. This is an
  11. * unpublished work protected as such under copyright law. This work
  12. * contains proprietary, confidential, and trade secret information of
  13. * RSA Data Security, Inc. Use, disclosure or reproduction without the
  14. * express written authorization of RSA Data Security, Inc. is
  15. * prohibited.
  16. *
  17. */
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define RSA1 ((DWORD)'R'+((DWORD)'S'<<8)+((DWORD)'A'<<16)+((DWORD)'1'<<24))
  22. #define RSA2 ((DWORD)'R'+((DWORD)'S'<<8)+((DWORD)'A'<<16)+((DWORD)'2'<<24))
  23. // Key header structures.
  24. //
  25. // These structs define the fixed data at the beginning of an RSA key.
  26. // They are followed by a variable length of data, sized by the stlen
  27. // field.
  28. typedef struct {
  29. DWORD magic; /* Should always be RSA1 */
  30. DWORD keylen; // size of modulus buffer
  31. DWORD bitlen; // # of bits in modulus
  32. DWORD datalen; // max number of bytes to be encoded
  33. DWORD pubexp; //public exponent
  34. } BSAFE_PUB_KEY, FAR *LPBSAFE_PUB_KEY;
  35. typedef struct {
  36. DWORD magic; /* Should always be RSA2 */
  37. DWORD keylen; // size of modulus buffer
  38. DWORD bitlen; // bit size of key
  39. DWORD datalen; // max number of bytes to be encoded
  40. DWORD pubexp; // public exponent
  41. } BSAFE_PRV_KEY, FAR *LPBSAFE_PRV_KEY;
  42. typedef struct {
  43. BYTE *modulus;
  44. BYTE *prvexp;
  45. BYTE *prime1;
  46. BYTE *prime2;
  47. BYTE *exp1;
  48. BYTE *exp2;
  49. BYTE *coef;
  50. BYTE *invmod;
  51. BYTE *invpr1;
  52. BYTE *invpr2;
  53. } BSAFE_KEY_PARTS, FAR *LPBSAFE_KEY_PARTS;
  54. typedef const BYTE far *cLPBYTE; // const LPBYTE resolves wrong
  55. // Structure for passing info into BSafe calls (currently this is used for
  56. // passing in a callback function pointer for random number generation and
  57. // information needed by the RNG, may eventually support exponentiation
  58. // offload.
  59. //
  60. typedef struct {
  61. void *pRNGInfo; // dat
  62. void *pFuncRNG; // Function pointer for RNG callback
  63. // callback prototype is
  64. // void pFuncRNG(
  65. // IN void *pRNGInfo,
  66. // IN OUT unsigned char **ppbRandSeed, // initial seed value (ignored if already set)
  67. // IN unsigned long *pcbRandSeed,
  68. // IN OUT unsigned char *pbBuffer,
  69. // IN unsigned long dwLength
  70. // );
  71. } BSAFE_OTHER_INFO;
  72. /* BSafeEncPublic
  73. *
  74. * BSafeEncPublic(key, part_in, part_out)
  75. *
  76. * RSA encrypt a buffer of size key->keylen, filled with data of size
  77. * key->datalen with the public key pointed to by key, returning the
  78. * encrypted data in part_out.
  79. *
  80. * Parameters
  81. *
  82. * LPBSAFE_PUB_KEY key - points to a public key in BSAFE_KEY
  83. * format.
  84. *
  85. * LPBYTE part_in - points to a BYTE array of size key->keylen
  86. * holding the data to be encrypted. The
  87. * data in the buffer should be no larger
  88. * than key->datalen. All other bytes should
  89. * be zero.
  90. *
  91. * LPBYTE part_out - points to a BYTE array of size keylen
  92. * to receive the encrypted data.
  93. *
  94. * Returns
  95. *
  96. * TRUE - encryption succeeded.
  97. * FALSE - encryption failed.
  98. *
  99. */
  100. BOOL
  101. RSA32API
  102. BSafeEncPublic(
  103. const LPBSAFE_PUB_KEY key,
  104. cLPBYTE part_in,
  105. LPBYTE part_out
  106. );
  107. /* BSafeDecPrivate
  108. *
  109. * BSafeDecPrivate(key, part_in, part_out)
  110. *
  111. * RSA decrypt a buffer of size keylen, containing key->datalen bytes
  112. * of data with the private key pointed to by key, returning the
  113. * decrypted data in part_out.
  114. *
  115. * Parameters
  116. *
  117. * LPBSAFE_PRV_KEY key - points to a private key in BSAFE_KEY
  118. * format.
  119. *
  120. * LPBYTE part_in - points to a BYTE array of size key->keylen
  121. * holding the data to be decrypted. The data
  122. * in the buffer should be no longer than
  123. * key->datalen. All other bytes should be zero.
  124. *
  125. * LPBYTE part_out - points to a BYTE array of size GRAINSIZE
  126. * to receive the decrypted data.
  127. *
  128. * Returns
  129. *
  130. * TRUE - decryption succeeded.
  131. * FALSE - decryption failed.
  132. *
  133. */
  134. BOOL
  135. RSA32API
  136. BSafeDecPrivate(
  137. const LPBSAFE_PRV_KEY key,
  138. cLPBYTE part_in,
  139. LPBYTE part_out
  140. );
  141. /* BSafeMakeKeyPair
  142. *
  143. * BSafeMakeKeyPair(public_key, private_key, bits)
  144. *
  145. * Generate an RSA key pair.
  146. *
  147. * Parameters
  148. *
  149. * LPBSAFE_PUB_KEY public_key - points to the memory to recieve
  150. * the public key. This pointer must
  151. * point to at least the number of bytes
  152. * specified as the public key size by
  153. * BSafeComputeKeySizes.
  154. *
  155. * LPBSAFE_PRV_KEY private_key - points to the memory to recieve
  156. * the private key. This pointer must
  157. * point to at least the number of bytes
  158. * specified as the private key size
  159. * by BSafeComputeKeySizes.
  160. *
  161. * DWORD bits - length of the requested key in bits.
  162. * This value must be even and greater than 63
  163. *
  164. * Returns
  165. *
  166. * TRUE - keys were successfully generated
  167. * FALSE - not enough memory to generate keys
  168. *
  169. */
  170. BOOL
  171. RSA32API
  172. BSafeMakeKeyPair(
  173. LPBSAFE_PUB_KEY public_key,
  174. LPBSAFE_PRV_KEY private_key,
  175. DWORD bits
  176. );
  177. /* BSafeMakeKeyPairEx
  178. *
  179. * BSafeMakeKeyPairEx(public_key, private_key, bits, public_exp)
  180. *
  181. * Generate an RSA key pair.
  182. *
  183. * Parameters
  184. *
  185. * LPBSAFE_PUB_KEY public_key - points to the memory to recieve
  186. * the public key. This pointer must
  187. * point to at least the number of bytes
  188. * specified as the public key size by
  189. * BSafeComputeKeySizes.
  190. *
  191. * LPBSAFE_PRV_KEY private_key - points to the memory to recieve
  192. * the private key. This pointer must
  193. * point to at least the number of bytes
  194. * specified as the private key size
  195. * by BSafeComputeKeySizes.
  196. *
  197. * DWORD bits - length of the requested key in bits.
  198. * This value must be even and greater
  199. * than 63
  200. *
  201. * DWORD public_exp = supplies the public key exponent. This
  202. * should be a prime number.
  203. *
  204. *
  205. * Returns
  206. *
  207. * TRUE - keys were successfully generated
  208. * FALSE - not enough memory to generate keys
  209. *
  210. */
  211. BOOL
  212. RSA32API
  213. BSafeMakeKeyPairEx(
  214. LPBSAFE_PUB_KEY public_key,
  215. LPBSAFE_PRV_KEY private_key,
  216. DWORD bits,
  217. DWORD public_exp
  218. );
  219. /* BSafeMakeKeyPairEx2
  220. *
  221. * BSafeMakeKeyPairEx2(pOtherInfo, public_key, private_key, bits, public_exp)
  222. *
  223. * Generate an RSA key pair.
  224. *
  225. * Parameters
  226. *
  227. * BSAFE_OTHER_INFO pOtherInfo - points to a structure with information
  228. * alternate information to be used when
  229. * generating the RSA key pair. Currently
  230. * this structure has a pointer to a callback
  231. * function which may be used when generating
  232. * keys. It also has a information to pass
  233. * into that callback function (see OTHER_INFO).
  234. *
  235. * LPBSAFE_PUB_KEY public_key - points to the memory to recieve
  236. * the public key. This pointer must
  237. * point to at least the number of bytes
  238. * specified as the public key size by
  239. * BSafeComputeKeySizes.
  240. *
  241. * LPBSAFE_PRV_KEY private_key - points to the memory to recieve
  242. * the private key. This pointer must
  243. * point to at least the number of bytes
  244. * specified as the private key size
  245. * by BSafeComputeKeySizes.
  246. *
  247. * DWORD bits - length of the requested key in bits.
  248. * This value must be even and greater
  249. * than 63
  250. *
  251. * DWORD public_exp = supplies the public key exponent. This
  252. * should be a prime number.
  253. *
  254. *
  255. * Returns
  256. *
  257. * TRUE - keys were successfully generated
  258. * FALSE - not enough memory to generate keys
  259. *
  260. */
  261. BOOL
  262. RSA32API
  263. BSafeMakeKeyPairEx2(BSAFE_OTHER_INFO *pOtherInfo,
  264. LPBSAFE_PUB_KEY public_key,
  265. LPBSAFE_PRV_KEY private_key,
  266. DWORD bits,
  267. DWORD dwPubExp);
  268. /* BSafeFreePubKey
  269. *
  270. * BSafeFreePubKey(public_key)
  271. *
  272. * Free the data associated with a public key
  273. *
  274. * Parameters
  275. *
  276. * LPBSAFE_PUB_KEY public_key - points to a BSAFE_PUB_KEY
  277. * structure to free.
  278. *
  279. * Returns
  280. *
  281. * nothing
  282. *
  283. */
  284. void
  285. RSA32API
  286. BSafeFreePubKey(
  287. LPBSAFE_PUB_KEY public_key
  288. );
  289. /* BSafeFreePrvKey
  290. *
  291. * BSafeFreePrvKey(public_key)
  292. *
  293. * Free the data associated with a private key
  294. *
  295. * Parameters
  296. *
  297. * LPBSAFE_PRV_KEY private_key - points to a BSAFE_PRV_KEY
  298. * structure to free.
  299. *
  300. * Returns
  301. *
  302. * nothing
  303. *
  304. */
  305. void
  306. RSA32API
  307. BSafeFreePrvKey(
  308. LPBSAFE_PRV_KEY private_key
  309. );
  310. /* BSafeComputeKeySizes
  311. *
  312. * BSafeComputeKeySizes( LPDWORD PubKeySize,
  313. * LPDWORD PrivKeySize,
  314. * LPDWORD bits )
  315. *
  316. * Computes the required memory to hold a public and private key of
  317. * a specified number of bits.
  318. *
  319. * Parameters:
  320. *
  321. * LPDWORD PubKeySize - pointer to DWORD to return the public
  322. * key size, in bytes.
  323. *
  324. * LPDWORD PrivKeySize - pointer to DWORD to return the private
  325. * key size, in bytes.
  326. *
  327. * LPDWORD bits - pointer to DWORD specifying number of bits
  328. * in the RSA modulus.
  329. *
  330. * Returns:
  331. *
  332. * TRUE if *bits is a valid RSA modulus size.
  333. * FALSE if *bits is an invalid RSA modulus size.
  334. *
  335. */
  336. BOOL
  337. RSA32API
  338. BSafeComputeKeySizes(
  339. LPDWORD PublicKeySize,
  340. LPDWORD PrivateKeySize,
  341. LPDWORD bits
  342. );
  343. /* BSafeGetPrvKeyParts
  344. *
  345. * BOOL BSafeGetPrvKeyParts( LPBSAFE_PRV_KEY key,
  346. * LPBSAFE_KEY_PARTS parts)
  347. *
  348. * Returns pointers to the parts of a private key, and the length of
  349. * the modulus in bytes.
  350. *
  351. * Parameters:
  352. *
  353. * LPBSAFE_PRV_KEY key - the key to disassemble
  354. * LPBSAFE_KEY_PARTS parts - the structure to fill in
  355. *
  356. * Returns -
  357. * FALSE if the key is not valid.
  358. */
  359. BOOL
  360. RSA32API
  361. BSafeGetPrvKeyParts(
  362. LPBSAFE_PRV_KEY key,
  363. LPBSAFE_KEY_PARTS parts
  364. );
  365. /* BSafeGetPubKeyModulus
  366. *
  367. * BYTE *BSafeGetPubKeyModulus(LPBSAFE_PUB_KEY key)
  368. *
  369. * Returns pointer to the modulus of a public key
  370. *
  371. * Parameters:
  372. *
  373. * LPBSAFE_PUB_KEY key - the key to disassemble
  374. *
  375. * Returns -
  376. *
  377. * Pointer to the parts, VOID on error.
  378. * Fails if the key is not valid.
  379. */
  380. BYTE *
  381. RSA32API
  382. BSafeGetPubKeyModulus(
  383. LPBSAFE_PUB_KEY key
  384. );
  385. #ifdef __cplusplus
  386. }
  387. #endif
  388. #endif // __RSA_H__