home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / unix / time_qua.nt < prev    next >
Encoding:
Text File  |  2003-06-11  |  3.0 KB  |  117 lines

  1. /*
  2. X * Simple Unix time quantization package
  3. X * {mab,lacy}@research.att.com
  4. X * v0.5 - 12/95
  5. X *
  6. X * TESTED ONLY UNDER SUNOS 4.x and BSDI 2.0.  This is unsupported
  7. X * software.  Use at own risk.  Test carefully on new platforms.
  8. X */
  9. /*
  10. X * The authors of this software are Matt Blaze and Jack Lacy
  11. X *              Copyright (c) 1995 by AT&T Bell Laboratories.
  12. X *
  13. X * Permission to use, copy, and modify this software without fee is
  14. X * hereby granted, provided that this entire notice is included in all
  15. X * copies of any software which is or includes a copy or modification
  16. X * of this software and in all copies of the supporting documentation
  17. X * for such software.
  18. X *
  19. X * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  20. X * IMPLIED WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE
  21. X * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
  22. X * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
  23. X * PURPOSE.
  24. X */
  25. X
  26. /*
  27. X * WARNING: This package will provide quantized cpu consumption only
  28. X * subject to the limitations of the OS on which it is run.  It will
  29. X * fail in extreme cases (e.g., very very heavy load and very slow
  30. X * machines (e.g., .001 MIPS).  Understand its limits before you use
  31. X * it.
  32. X *
  33. X * Note that start_quantize takes MILLISECONDS, not microseconds.  See
  34. X * quantize.3 for details.
  35. X *
  36. X * To prevent timing attacks (e.g., Kocher) in most PK crypto
  37. X * applications in most applications on most cpus, surrounding the
  38. X * call to the functions that use the secret with
  39. X *    start_quantize(100);
  40. X * and
  41. X *    end_quantize();
  42. X * will do reasonably well.
  43. X */
  44. X
  45. #ifndef NO_QUANTIZE
  46. #include <signal.h>
  47. #include <setjmp.h>
  48. #include <sys/time.h>
  49. #include <stdio.h>
  50. X
  51. static jmp_buf quant_end;
  52. static long quant_quantum=0;
  53. X
  54. static void quant_interrupt()
  55. {
  56. X    long nquantum;
  57. X
  58. X    nquantum = quant_quantum;
  59. X    if (nquantum != 0)
  60. X        set_quant_interrupt(nquantum);
  61. X    else
  62. X        longjmp(quant_end, 1);
  63. X
  64. }
  65. X
  66. static set_quant_interrupt(microsecs)
  67. X     long microsecs;
  68. {
  69. X    struct itimerval it, oit;
  70. X    
  71. X    timerclear(&it.it_interval);
  72. X    it.it_value.tv_sec = microsecs/1000000;
  73. X    it.it_value.tv_usec = microsecs%1000000;
  74. X    (void) signal(SIGVTALRM, quant_interrupt);
  75. X    return setitimer(ITIMER_VIRTUAL, &it, &oit);
  76. }
  77. X
  78. int start_quantize(quantum)
  79. X     int quantum;    /* millisecs */
  80. {
  81. X    if (quantum <= 0)
  82. X        return -1;
  83. X    quant_quantum = (quantum * 1000) + 1; /* microsecs */
  84. X    return set_quant_interrupt(quant_quantum);
  85. }
  86. X
  87. int end_quantize()
  88. {
  89. X    if (setjmp(quant_end))
  90. X        return 0;
  91. X    if (quant_quantum == 0)
  92. X        return -1; /* start_quantize never called */
  93. X    quant_quantum = 0; /* we return at next quantum */
  94. X    while (1)
  95. X        ;
  96. X    return -1; /* should never happen */
  97. }
  98. X
  99. #else /* NO_QUANTIZE */
  100. #include <stdio.h>
  101. X
  102. int start_quantize(quantum)
  103. X     int quantum;
  104. {
  105. X    fprintf(stderr,"Warning: QUANTIZE not available\n");
  106. X    fflush(stderr);
  107. X    return -1;
  108. X
  109. }
  110. X
  111. int end_quantize()
  112. {
  113. X    return -1;
  114. }
  115. X
  116. #endif
  117.