home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / programs / kc9_src.arj / BCRYPT.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-07  |  11.6 KB  |  380 lines

  1. /*
  2. **  BCRYPT - High speed un*x password encryption/compare routines
  3. **  Originally written by VIz, modifications by Doctor Dissector
  4. **
  5. **  Filename   : bcrypt.h
  6. **
  7. **  Description: definitions, unions, structures, and static tables
  8. **               used by bcrypt.c
  9. **
  10. **  Updated    : 10/07/91
  11. */
  12.  
  13. /*=[ VIz's Original Disclaimer ]============================================*/
  14.  
  15. /*
  16. **                             LARD
  17. **                       "The power of LARD"
  18. **                            by VIz
  19. **
  20. **  I am not responsible for any use of this program by anyone,
  21. **  on any machine for any purpose, anywhere at anytime....
  22. */
  23.  
  24. /*=[ Definition: _TURBO ]===================================================*/
  25.  
  26. /*
  27. **  _TURBO notifies the bcrypt() functions that you are compiling
  28. **  the bcrypt() function using the Turbo C, Turbo C++, or Borland C++
  29. **  compilers under the MS/PC-DOS operating environment.  By default,
  30. **  this definition is left commented.  Un-comment this definition if
  31. **  it pertains to your compiler/operating system.
  32. **
  33. **  IMPORTANT NOTES:
  34. **      -   You MUST also un-comment the NON_NETORDER definition.
  35. **      -   You MUST compile and link the bcrypt() routines under the
  36. **          COMPACT MEMORY MODEL or any larger memory model (large, huge).
  37. */
  38.  
  39. /*
  40. #define _TURBO      1
  41. */
  42.  
  43. /*=[ Definition: _MICROSOFT ]===============================================*/
  44.  
  45. /*
  46. **  _MICROSOFT tells bcrypt() that you are compiling the bcrypt()
  47. **  routines with the Microsoft C compiler, under the MS/PC-DOS
  48. **  operating environment.  By default, this definition is left
  49. **  commented.  Un-comment this definition if it pertains to your
  50. **  compiler/operating system.
  51. **
  52. **  IMPORTANT NOTES:
  53. **      -   You MUST also un-comment the NON_NETORDER definition.
  54. **      -   You MUST compile and link the bcrypt() routines under the
  55. **          COMPACT MEMORY MODEL or any larger memory model (large, huge).
  56. */
  57.  
  58. /*
  59. #define _MICROSOFT  1
  60. */
  61.  
  62. /*=[ Definition: NON_NETORDER ]=============================================*/
  63.  
  64. /*
  65. **  Bcrypt uses unions and bitfields to extract individual bits and bit data
  66. **  from each 32 bit long it uses within its functions.  Bit ordering within
  67. **  a union containing bitfields appears to be system dependent.  If you
  68. **  have a question about your own machine's bit ordering, compile and
  69. **  execute the program "b_order.c" included with this package.  If you have
  70. **  Network Byte Ordering, be sure the definition NON_NETORDER is NOT
  71. **  defined in your source code.  If you have Non-Network Byte Ordering,
  72. **  be sure to un-comment the following declaration for NON_NETORDER in
  73. **  order for bcrypt() to gain the proper results.
  74. */
  75.  
  76. /*
  77. #define NON_NETORDER    1
  78. */
  79.  
  80. /*=[ Definition: INT_32BIT ]================================================*/
  81.  
  82. /*
  83. **  In the old days, integers were defined as 16 bit values which limited
  84. **  their maximum value (unsigned) to a mere 65535.  In order to represent
  85. **  any larger integer, one had to declare a variable as a "long int", which
  86. **  defined a 32 bit integer with a maximum (unsigned) value of 4294967295.
  87. **  However, most modern 32-bit compilers now endow the "normal" integer
  88. **  with 32 default bits and "long int" values are allocated 64 bits; bcrypt
  89. **  happens to only need 32 bits for most of its application, and utilizing
  90. **  64 bits in a "long int" would surely be wasteful and more time-consuming
  91. **  than manipulating a 32 bit value.  As a result, by defining INT_32BIT
  92. **  below (un-commentig it), you will notify bcrypt that the compiler you
  93. **  are using DEFAULTS TO 32 BIT INTEGERS.  NOTE: *MOST* MS/PC-DOS compilers
  94. **  are NOT 32-bit and therefore do not default to a 32-bit integer; most
  95. **  modern Un*x flavors utilize 32 bit compilers and generally default to
  96. **  32 bit integers.  To determine the bit-size of the default integer of your
  97. **  compiler, compile and execute the program "int_size.c"; if your compiler
  98. **  generates any default ints LESS THAN 32 bits (ANY amount less), then DO
  99. **  NOT un-comment this definition.
  100. */
  101.  
  102. /*
  103. #define INT_32BIT       1
  104. */
  105.  
  106. /*=[ Definition: TESTING ]==================================================*/
  107.  
  108. /*
  109. **  If you are testing the bcrypt() routines and would like to use bcrypt()
  110. **  in a situation where it is called by "char *bcrypt(char *pw, char *salt)"
  111. **  then this definition (default, off), will tell your compiler to add the
  112. **  left out code that would be used to test the bcrypt() function in
  113. **  a similar manner as the crypt() (original crypt) function.
  114. */
  115.  
  116. /*
  117. #define TESTING     1
  118. */
  119.  
  120. /*=[ General Definitions ]==================================================*/
  121.  
  122. #define REG     register
  123.  
  124. #ifdef INT_32BIT
  125. #define U32     unsigned int
  126. #else
  127. #define U32     unsigned long
  128. #endif
  129.  
  130. /*=[ Union: char_union ]====================================================*/
  131.  
  132. #ifdef NON_NETORDER
  133. union char_union {
  134.     struct {
  135.         unsigned    b0:1;
  136.         unsigned    b1:1;
  137.         unsigned    b2:1;
  138.         unsigned    b3:1;
  139.         unsigned    b4:1;
  140.         unsigned    b5:1;
  141.         unsigned    b6:1;
  142.         unsigned    b7:1;
  143.     } bits;
  144.     char c;
  145. };
  146. #else
  147. union char_union {
  148.     struct {
  149.         unsigned    b7:1;
  150.         unsigned    b6:1;
  151.         unsigned    b5:1;
  152.         unsigned    b4:1;
  153.         unsigned    b3:1;
  154.         unsigned    b2:1;
  155.         unsigned    b1:1;
  156.         unsigned    b0:1;
  157.     } bits;
  158.     char c;
  159. };
  160. #endif
  161.  
  162. /*=[ Union: BU32 ]==========================================================*/
  163.  
  164. #ifdef NON_NETORDER
  165. typedef union {
  166.     /* individiual bits */
  167.     struct {
  168.         unsigned  b0:1;
  169.         unsigned  b1:1;
  170.         unsigned  b2:1;
  171.         unsigned  b3:1;
  172.         unsigned  b4:1;
  173.         unsigned  b5:1;
  174.         unsigned  b6:1;
  175.         unsigned  b7:1;
  176.         unsigned  b8:1;
  177.         unsigned  b9:1;
  178.         unsigned  b10:1;
  179.         unsigned  b11:1;
  180.         unsigned  b12:1;
  181.         unsigned  b13:1;
  182.         unsigned  b14:1;
  183.         unsigned  b15:1;
  184.         unsigned  b16:1;
  185.         unsigned  b17:1;
  186.         unsigned  b18:1;
  187.         unsigned  b19:1;
  188.         unsigned  b20:1;
  189.         unsigned  b21:1;
  190.         unsigned  b22:1;
  191.         unsigned  b23:1;
  192.         unsigned  b24:1;
  193.         unsigned  b25:1;
  194.         unsigned  b26:1;
  195.         unsigned  b27:1;
  196.         unsigned  b28:1;
  197.         unsigned  b29:1;
  198.         unsigned  b30:1;
  199.         unsigned  b31:1;
  200.     } N;
  201.     /* Feldmeier expansion part 0 */
  202.     struct {
  203.         unsigned z2:2;
  204.         unsigned b13_2:12;
  205.         unsigned z1:4;
  206.         unsigned b29_18:12;
  207.         unsigned z0:2;
  208.     } FE0;
  209.     /* Feldmeier expanion part 1 */
  210.     struct {
  211.         unsigned b5_0:6;
  212.         unsigned z1:4;
  213.         unsigned b21_10:12;
  214.         unsigned z0:4;
  215.         unsigned b31_26:6;
  216.     } FE1;
  217.     struct {
  218.         unsigned b5_0:6;
  219.         unsigned b11_6:6;
  220.         unsigned z1:10;
  221.         unsigned z0:10;
  222.     } F12;
  223.     struct {
  224.         unsigned b30_31:2;
  225.         unsigned b24_29:6;
  226.         unsigned b18_23:6;
  227.         unsigned b12_17:6;
  228.         unsigned b6_11:6;
  229.         unsigned b0_5:6;
  230.     } B6;
  231.     struct {
  232.         unsigned b28_31:4;
  233.         unsigned b22_27:6;
  234.         unsigned b16_21:6;
  235.         unsigned b10_15:6;
  236.         unsigned b4_9:6;
  237.         unsigned b0_3:4;
  238.     } B6_;
  239.     U32 U;
  240. } BU32;
  241. #else
  242. typedef union {
  243.     /* individual bits */
  244.     struct {
  245.         unsigned  b31:1;
  246.         unsigned  b30:1;
  247.         unsigned  b29:1;
  248.         unsigned  b28:1;
  249.         unsigned  b27:1;
  250.         unsigned  b26:1;
  251.         unsigned  b25:1;
  252.         unsigned  b24:1;
  253.         unsigned  b23:1;
  254.         unsigned  b22:1;
  255.         unsigned  b21:1;
  256.         unsigned  b20:1;
  257.         unsigned  b19:1;
  258.         unsigned  b18:1;
  259.         unsigned  b17:1;
  260.         unsigned  b16:1;
  261.         unsigned  b15:1;
  262.         unsigned  b14:1;
  263.         unsigned  b13:1;
  264.         unsigned  b12:1;
  265.         unsigned  b11:1;
  266.         unsigned  b10:1;
  267.         unsigned  b9:1;
  268.         unsigned  b8:1;
  269.         unsigned  b7:1;
  270.         unsigned  b6:1;
  271.         unsigned  b5:1;
  272.         unsigned  b4:1;
  273.         unsigned  b3:1;
  274.         unsigned  b2:1;
  275.         unsigned  b1:1;
  276.         unsigned  b0:1;
  277.     } N;
  278.     /* Feldmeier expansion part 0 */
  279.     struct {
  280.         unsigned z0:2;
  281.         unsigned b29_18:12;
  282.         unsigned z1:4;
  283.         unsigned b13_2:12;
  284.         unsigned z2:2;
  285.     } FE0;
  286.     /* Feldmeier expanion part 1 */
  287.     struct {
  288.         unsigned b31_26:6;
  289.         unsigned z0:4;
  290.         unsigned b21_10:12;
  291.         unsigned z1:4;
  292.         unsigned b5_0:6;
  293.     } FE1;
  294.     struct {
  295.         unsigned z0:10;
  296.         unsigned z1:10;
  297.         unsigned b11_6:6;
  298.         unsigned b5_0:6;
  299.     } F12;
  300.     struct {
  301.         unsigned  b0_5:6;
  302.         unsigned  b6_11:6;
  303.         unsigned  b12_17:6;
  304.         unsigned  b18_23:6;
  305.         unsigned  b24_29:6;
  306.         unsigned  b30_31:2;
  307.     } B6;
  308.     struct {
  309.         unsigned  b0_3:4;
  310.         unsigned  b4_9:6;
  311.         unsigned  b10_15:6;
  312.         unsigned  b16_21:6;
  313.         unsigned  b22_27:6;
  314.         unsigned  b28_31:4;
  315.     } B6_;
  316.     U32 U;
  317. } BU32;
  318. #endif
  319.  
  320. /*=[ Structure: BU64 ]======================================================*/
  321.  
  322. typedef struct {
  323.     BU32 L, R;
  324. } BU64;
  325.  
  326. /*=[ Static Tables ]========================================================*/
  327.  
  328. /* Shifts (shifts[]), Minus 1 */
  329. static char SHIFTS_M1[] =
  330.  {
  331.   0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0,
  332.  };
  333.  
  334. /*
  335. ** The 8 original selection functions.  For some reason, they give
  336. ** a 0-origin index, unlike everything else.
  337. */
  338. static char OS[8][64] =
  339.  {
  340.   14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
  341.    0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
  342.    4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
  343.   15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,
  344.  
  345.   15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
  346.    3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
  347.    0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
  348.   13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,
  349.  
  350.   10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
  351.   13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
  352.   13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
  353.    1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,
  354.  
  355.    7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
  356.   13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
  357.   10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
  358.    3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,
  359.  
  360.    2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
  361.   14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
  362.    4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
  363.   11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,
  364.  
  365.   12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
  366.   10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
  367.    9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
  368.    4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,
  369.  
  370.    4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
  371.   13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
  372.    1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
  373.    6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,
  374.  
  375.   13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
  376.    1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
  377.    7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
  378.    2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11,
  379.  };
  380.