Source code of Windows XP (NT5)
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.

97 lines
2.9 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // FILE : csp.c //
  3. // DESCRIPTION : Crypto API interface //
  4. // Global Stuff for CSP
  5. // AUTHOR : Amit Kapoor //
  6. /////////////////////////////////////////////////////////////////////////////
  7. #undef UNICODE
  8. #include <windows.h>
  9. #include <fxupbn.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. // Needed for DLL
  14. BOOLEAN DllInitialize (IN PVOID hmod,IN ULONG Reason,IN PCONTEXT Context)
  15. {
  16. LoadLibrary("offload.dll");
  17. return( TRUE );
  18. }
  19. BOOL WINAPI OffloadModExpo(
  20. IN BYTE *pbBase,
  21. IN BYTE *pbExpo,
  22. IN DWORD cbExpo,
  23. IN BYTE *pbMod,
  24. IN DWORD cbMod,
  25. IN BYTE *pbResult,
  26. IN void *pReserved,
  27. IN DWORD dwFlags
  28. )
  29. {
  30. mp_modulus_t *pModularMod = NULL;
  31. digit_t *pbModularBase = NULL;
  32. digit_t *pbModularResult = NULL;
  33. DWORD dwModularLen = (cbMod + (RADIX_BYTES - 1)) / RADIX_BYTES; // dwLen is length in bytes
  34. BYTE *pbTmpExpo = NULL;
  35. BOOL fAlloc = FALSE;
  36. BOOL fRet = FALSE;
  37. if (cbExpo < cbMod)
  38. {
  39. if (NULL == (pbTmpExpo = (BYTE*)LocalAlloc(LMEM_ZEROINIT, dwModularLen * RADIX_BYTES)))
  40. {
  41. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  42. goto Ret;
  43. }
  44. fAlloc = TRUE;
  45. memcpy(pbTmpExpo, pbExpo, cbExpo);
  46. }
  47. else
  48. {
  49. pbTmpExpo = pbExpo;
  50. }
  51. if (NULL == (pModularMod = (mp_modulus_t*)LocalAlloc(LMEM_ZEROINIT,
  52. sizeof(mp_modulus_t))))
  53. {
  54. goto Ret;
  55. }
  56. if (NULL == (pbModularBase = (digit_t*)LocalAlloc(LMEM_ZEROINIT,
  57. MP_LONGEST * sizeof(digit_t))))
  58. {
  59. goto Ret;
  60. }
  61. if (NULL == (pbModularResult = (digit_t*)LocalAlloc(LMEM_ZEROINIT,
  62. MP_LONGEST * sizeof(digit_t))))
  63. {
  64. goto Ret;
  65. }
  66. // change values into modular form
  67. create_modulus((digit_tc*)pbMod, dwModularLen, FROM_RIGHT, pModularMod);
  68. to_modular((digit_tc*)pbBase, dwModularLen, pbModularBase, pModularMod);
  69. mod_exp(pbModularBase, (digit_tc*)pbTmpExpo, dwModularLen,
  70. pbModularResult, pModularMod);
  71. from_modular(pbModularResult, (digit_t*)pbResult, pModularMod);
  72. fRet = TRUE;
  73. Ret:
  74. if (pModularMod)
  75. LocalFree(pModularMod);
  76. if (pbModularBase)
  77. LocalFree(pbModularBase);
  78. if (pbModularResult)
  79. LocalFree(pbModularResult);
  80. if (fAlloc && pbTmpExpo)
  81. LocalFree(pbTmpExpo);
  82. return fRet;
  83. }
  84. #ifdef __cplusplus
  85. }
  86. #endif