randbyte_os2.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* The high resolution timer API provides access to the hardware timer
  17. * running at around 1.1MHz. The amount this changes in a time slice is
  18. * varies randomly due to system events, hardware interrupts etc
  19. */
  20. static UCHAR randbyte_hrtimer()
  21. {
  22. QWORD t1, t2;
  23. UCHAR byte;
  24. DosTmrQueryTime(&t1);
  25. DosSleep(5);
  26. DosTmrQueryTime(&t2);
  27. byte = (t2.ulLo - t1.ulLo) & 0xFF;
  28. byte ^= (t2.ulLo - t1.ulLo) >> 8;
  29. return byte;
  30. }
  31. /* A bunch of system information like memory & process stats.
  32. * Not highly random but every bit helps....
  33. */
  34. static UCHAR randbyte_sysinfo()
  35. {
  36. UCHAR byte = 0;
  37. UCHAR SysVars[100];
  38. int b;
  39. DosQuerySysInfo(1, QSV_FOREGROUND_PROCESS, SysVars, sizeof(SysVars));
  40. for (b = 0; b < 100; b++) {
  41. byte ^= SysVars[b];
  42. }
  43. return byte;
  44. }
  45. /* Similar in concept to randbyte_hrtimer() but accesses the CPU's internal
  46. * counters which run at the CPU's MHz speed. We get separate
  47. * idle / busy / interrupt cycle counts which should provide very good
  48. * randomness due to interference of hardware events.
  49. * This only works on newer CPUs (at least PPro or K6) and newer OS/2 versions
  50. * which is why it's run-time linked.
  51. */
  52. static APIRET APIENTRY(*DosPerfSysCall) (ULONG ulCommand, ULONG ulParm1,
  53. ULONG ulParm2, ULONG ulParm3) = NULL;
  54. static HMODULE hDoscalls = 0;
  55. #define CMD_KI_RDCNT (0x63)
  56. typedef struct _CPUUTIL {
  57. ULONG ulTimeLow; /* Low 32 bits of time stamp */
  58. ULONG ulTimeHigh; /* High 32 bits of time stamp */
  59. ULONG ulIdleLow; /* Low 32 bits of idle time */
  60. ULONG ulIdleHigh; /* High 32 bits of idle time */
  61. ULONG ulBusyLow; /* Low 32 bits of busy time */
  62. ULONG ulBusyHigh; /* High 32 bits of busy time */
  63. ULONG ulIntrLow; /* Low 32 bits of interrupt time */
  64. ULONG ulIntrHigh; /* High 32 bits of interrupt time */
  65. } CPUUTIL;
  66. static UCHAR randbyte_perf()
  67. {
  68. UCHAR byte = 0;
  69. CPUUTIL util;
  70. int c;
  71. if (hDoscalls == 0) {
  72. char failed_module[20];
  73. ULONG rc;
  74. rc = DosLoadModule(failed_module, sizeof(failed_module), "DOSCALLS",
  75. &hDoscalls);
  76. if (rc == 0) {
  77. rc = DosQueryProcAddr(hDoscalls, 976, NULL, (PFN *)&DosPerfSysCall);
  78. if (rc) {
  79. DosPerfSysCall = NULL;
  80. }
  81. }
  82. }
  83. if (DosPerfSysCall) {
  84. if (DosPerfSysCall(CMD_KI_RDCNT, (ULONG)&util, 0, 0) == 0) {
  85. for (c = 0; c < sizeof(util); c++) {
  86. byte ^= ((UCHAR *)&util)[c];
  87. }
  88. }
  89. else {
  90. DosPerfSysCall = NULL;
  91. }
  92. }
  93. return byte;
  94. }
  95. static UCHAR randbyte()
  96. {
  97. return randbyte_hrtimer() ^ randbyte_sysinfo() ^ randbyte_perf();
  98. }