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.

132 lines
2.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: rng.c
  7. //
  8. // Contents: Random Number Generator
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 5-12-93 RichardW Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <nt.h>
  18. #include <ntrtl.h>
  19. #include <nturtl.h>
  20. #include <security.h>
  21. #include <cryptdll.h>
  22. #include <windows.h>
  23. #include <randlib.h>
  24. #include <crypt.h>
  25. #ifdef KERNEL_MODE
  26. int _fltused = 0x9875;
  27. #endif
  28. BOOLEAN NTAPI DefaultRngFn(PUCHAR, ULONG);
  29. #ifdef KERNEL_MODE
  30. #pragma alloc_text( PAGEMSG, DefaultRngFn )
  31. #endif
  32. #define MAX_RNGS 4
  33. RANDOM_NUMBER_GENERATOR Rngs[MAX_RNGS];
  34. ULONG cRngs = 0;
  35. RANDOM_NUMBER_GENERATOR DefaultRng = {CD_BUILTIN_RNG,
  36. RNG_PSEUDO_RANDOM,
  37. 0,
  38. DefaultRngFn };
  39. BOOLEAN NTAPI
  40. CDGenerateRandomBits( PUCHAR pBuffer,
  41. ULONG cbBuffer);
  42. BOOLEAN NTAPI
  43. CDRegisterRng(PRANDOM_NUMBER_GENERATOR pRng);
  44. BOOLEAN NTAPI
  45. CDLocateRng(ULONG Id,
  46. PRANDOM_NUMBER_GENERATOR * ppRng);
  47. BOOLEAN NTAPI
  48. DefaultRngFn( PUCHAR pbBuffer,
  49. ULONG cbBuffer);
  50. #ifdef KERNEL_MODE
  51. #pragma alloc_text( PAGEMSG, CDGenerateRandomBits )
  52. #pragma alloc_text( PAGEMSG, CDRegisterRng )
  53. #pragma alloc_text( PAGEMSG, CDLocateRng )
  54. #pragma alloc_text( PAGEMSG, DefaultRngFn )
  55. #endif
  56. //
  57. // Management functions:
  58. //
  59. BOOLEAN NTAPI
  60. CDGenerateRandomBits( PUCHAR pBuffer,
  61. ULONG cbBuffer)
  62. {
  63. return(Rngs[cRngs-1].GenerateBitstream(pBuffer, cbBuffer));
  64. }
  65. BOOLEAN NTAPI
  66. CDRegisterRng(PRANDOM_NUMBER_GENERATOR pRng)
  67. {
  68. if (cRngs < MAX_RNGS)
  69. {
  70. Rngs[cRngs++] = *pRng;
  71. return(TRUE);
  72. }
  73. return(FALSE);
  74. }
  75. BOOLEAN NTAPI
  76. CDLocateRng(ULONG Id,
  77. PRANDOM_NUMBER_GENERATOR * ppRng)
  78. {
  79. ULONG i;
  80. for (i = 0; i < MAX_RNGS ; i++ )
  81. {
  82. if (Rngs[i].GeneratorId == Id)
  83. {
  84. *ppRng = &Rngs[i];
  85. return(TRUE);
  86. }
  87. }
  88. *ppRng = NULL;
  89. return(FALSE);
  90. }
  91. BOOLEAN NTAPI
  92. DefaultRngFn( PUCHAR pbBuffer,
  93. ULONG cbBuffer)
  94. {
  95. //
  96. // use the crypto group provided random number generator.
  97. //
  98. #ifdef KERNEL_MODE
  99. NewGenRandom(NULL, NULL, pbBuffer, cbBuffer);
  100. return TRUE;
  101. #else
  102. return RtlGenRandom( pbBuffer, cbBuffer );
  103. #endif
  104. }