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.  
  3. **  BCRYPT - High speed un*x password encryption/compare routines
  4.  
  5. **  Originally written by VIz, modifications by Doctor Dissector
  6.  
  7. **
  8.  
  9. **  Filename   : bcrypt.h
  10.  
  11. **
  12.  
  13. **  Description: definitions, unions, structures, and static tables
  14.  
  15. **               used by bcrypt.c
  16.  
  17. **
  18.  
  19. **  Updated    : 10/07/91
  20.  
  21. */
  22.  
  23.  
  24.  
  25. /*=[ VIz's Original Disclaimer ]============================================*/
  26.  
  27.  
  28.  
  29. /*
  30.  
  31. **                             LARD
  32.  
  33. **                       "The power of LARD"
  34.  
  35. **                            by VIz
  36.  
  37. **
  38.  
  39. **  I am not responsible for any use of this program by anyone,
  40.  
  41. **  on any machine for any purpose, anywhere at anytime....
  42.  
  43. */
  44.  
  45.  
  46.  
  47. /*=[ Definition: _TURBO ]===================================================*/
  48.  
  49.  
  50.  
  51. /*
  52.  
  53. **  _TURBO notifies the bcrypt() functions that you are compiling
  54.  
  55. **  the bcrypt() function using the Turbo C, Turbo C++, or Borland C++
  56.  
  57. **  compilers under the MS/PC-DOS operating environment.  By default,
  58.  
  59. **  this definition is left commented.  Un-comment this definition if
  60.  
  61. **  it pertains to your compiler/operating system.
  62.  
  63. **
  64.  
  65. **  IMPORTANT NOTES:
  66.  
  67. **      -   You MUST also un-comment the NON_NETORDER definition.
  68.  
  69. **      -   You MUST compile and link the bcrypt() routines under the
  70.  
  71. **          COMPACT MEMORY MODEL or any larger memory model (large, huge).
  72.  
  73. */
  74.  
  75.  
  76.  
  77. /*
  78.  
  79. #define _TURBO      1
  80.  
  81. */
  82.  
  83.  
  84.  
  85. /*=[ Definition: _MICROSOFT ]===============================================*/
  86.  
  87.  
  88.  
  89. /*
  90.  
  91. **  _MICROSOFT tells bcrypt() that you are compiling the bcrypt()
  92.  
  93. **  routines with the Microsoft C compiler, under the MS/PC-DOS
  94.  
  95. **  operating environment.  By default, this definition is left
  96.  
  97. **  commented.  Un-comment this definition if it pertains to your
  98.  
  99. **  compiler/operating system.
  100.  
  101. **
  102.  
  103. **  IMPORTANT NOTES:
  104.  
  105. **      -   You MUST also un-comment the NON_NETORDER definition.
  106.  
  107. **      -   You MUST compile and link the bcrypt() routines under the
  108.  
  109. **          COMPACT MEMORY MODEL or any larger memory model (large, huge).
  110.  
  111. */
  112.  
  113.  
  114.  
  115. /*
  116.  
  117. #define _MICROSOFT  1
  118.  
  119. */
  120.  
  121.  
  122.  
  123. /*=[ Definition: NON_NETORDER ]=============================================*/
  124.  
  125.  
  126.  
  127. /*
  128.  
  129. **  Bcrypt uses unions and bitfields to extract individual bits and bit data
  130.  
  131. **  from each 32 bit long it uses within its functions.  Bit ordering within
  132.  
  133. **  a union containing bitfields appears to be system dependent.  If you
  134.  
  135. **  have a question about your own machine's bit ordering, compile and
  136.  
  137. **  execute the program "b_order.c" included with this package.  If you have
  138.  
  139. **  Network Byte Ordering, be sure the definition NON_NETORDER is NOT
  140.  
  141. **  defined in your source code.  If you have Non-Network Byte Ordering,
  142.  
  143. **  be sure to un-comment the following declaration for NON_NETORDER in
  144.  
  145. **  order for bcrypt() to gain the proper results.
  146.  
  147. */
  148.  
  149.  
  150.  
  151. /*
  152.  
  153. #define NON_NETORDER    1
  154.  
  155. */
  156.  
  157.  
  158.  
  159. /*=[ Definition: INT_32BIT ]================================================*/
  160.  
  161.  
  162.  
  163. /*
  164.  
  165. **  In the old days, integers were defined as 16 bit values which limited
  166.  
  167. **  their maximum value (unsigned) to a mere 65535.  In order to represent
  168.  
  169. **  any larger integer, one had to declare a variable as a "long int", which
  170.  
  171. **  defined a 32 bit integer with a maximum (unsigned) value of 4294967295.
  172.  
  173. **  However, most modern 32-bit compilers now endow the "normal" integer
  174.  
  175. **  with 32 default bits and "long int" values are allocated 64 bits; bcrypt
  176.  
  177. **  happens to only need 32 bits for most of its application, and utilizing
  178.  
  179. **  64 bits in a "long int" would surely be wasteful and more time-consuming
  180.  
  181. **  than manipulating a 32 bit value.  As a result, by defining INT_32BIT
  182.  
  183. **  below (un-commentig it), you will notify bcrypt that the compiler you
  184.  
  185. **  are using DEFAULTS TO 32 BIT INTEGERS.  NOTE: *MOST* MS/PC-DOS compilers
  186.  
  187. **  are NOT 32-bit and therefore do not default to a 32-bit integer; most
  188.  
  189. **  modern Un*x flavors utilize 32 bit compilers and generally default to
  190.  
  191. **  32 bit integers.  To determine the bit-size of the default integer of your
  192.  
  193. **  compiler, compile and execute the program "int_size.c"; if your compiler
  194.  
  195. **  generates any default ints LESS THAN 32 bits (ANY amount less), then DO
  196.  
  197. **  NOT un-comment this definition.
  198.  
  199. */
  200.  
  201.  
  202.  
  203. /*
  204.  
  205. #define INT_32BIT       1
  206.  
  207. */
  208.  
  209.  
  210.  
  211. /*=[ Definition: TESTING ]==================================================*/
  212.  
  213.  
  214.  
  215. /*
  216.  
  217. **  If you are testing the bcrypt() routines and would like to use bcrypt()
  218.  
  219. **  in a situation where it is called by "char *bcrypt(char *pw, char *salt)"
  220.  
  221. **  then this definition (default, off), will tell your compiler to add the
  222.  
  223. **  left out code that would be used to test the bcrypt() function in
  224.  
  225. **  a similar manner as the crypt() (original crypt) function.
  226.  
  227. */
  228.  
  229.  
  230.  
  231. /*
  232.  
  233. #define TESTING     1
  234.  
  235. */
  236.  
  237.  
  238.  
  239. /*=[ General Definitions ]==================================================*/
  240.  
  241.  
  242.  
  243. #define REG     register
  244.  
  245.  
  246.  
  247. #ifdef INT_32BIT
  248.  
  249. #define U32     unsigned int
  250.  
  251. #else
  252.  
  253. #define U32     unsigned long
  254.  
  255. #endif
  256.  
  257.  
  258.  
  259. /*=[ Union: char_union ]====================================================*/
  260.  
  261.  
  262.  
  263. #ifdef NON_NETORDER
  264.  
  265. union char_union {
  266.  
  267.     struct {
  268.  
  269.         unsigned    b0:1;
  270.  
  271.         unsigned    b1:1;
  272.  
  273.         unsigned    b2:1;
  274.  
  275.         unsigned    b3:1;
  276.  
  277.         unsigned    b4:1;
  278.  
  279.         unsigned    b5:1;
  280.  
  281.         unsigned    b6:1;
  282.  
  283.         unsigned    b7:1;
  284.  
  285.     } bits;
  286.  
  287.     char c;
  288.  
  289. };
  290.  
  291. #else
  292.  
  293. union char_union {
  294.  
  295.     struct {
  296.  
  297.         unsigned    b7:1;
  298.  
  299.         unsigned    b6:1;
  300.  
  301.         unsigned    b5:1;
  302.  
  303.         unsigned    b4:1;
  304.  
  305.         unsigned    b3:1;
  306.  
  307.         unsigned    b2:1;
  308.  
  309.         unsigned    b1:1;
  310.  
  311.         unsigned    b0:1;
  312.  
  313.     } bits;
  314.  
  315.     char c;
  316.  
  317. };
  318.  
  319. #endif
  320.  
  321.  
  322.  
  323. /*=[ Union: BU32 ]==========================================================*/
  324.  
  325.  
  326.  
  327. #ifdef NON_NETORDER
  328.  
  329. typedef union {
  330.  
  331.     /* individiual bits */
  332.  
  333.     struct {
  334.  
  335.         unsigned  b0:1;
  336.  
  337.         unsigned  b1:1;
  338.  
  339.         unsigned  b2:1;
  340.  
  341.         unsigned  b3:1;
  342.  
  343.         unsigned  b4:1;
  344.  
  345.         unsigned  b5:1;
  346.  
  347.         unsigned  b6:1;
  348.  
  349.         unsigned  b7:1;
  350.  
  351.         unsigned  b8:1;
  352.  
  353.         unsigned  b9:1;
  354.  
  355.         unsigned  b10:1;
  356.  
  357.         unsigned  b11:1;
  358.  
  359.         unsigned  b12:1;
  360.  
  361.         unsigned  b13:1;
  362.  
  363.         unsigned  b14:1;
  364.  
  365.         unsigned  b15:1;
  366.  
  367.         unsigned  b16:1;
  368.  
  369.         unsigned  b17:1;
  370.  
  371.         unsigned  b18:1;
  372.  
  373.         unsigned  b19:1;
  374.  
  375.         unsigned  b20:1;
  376.  
  377.         unsigned  b21:1;
  378.  
  379.         unsigned  b22:1;
  380.  
  381.         unsigned  b23:1;
  382.  
  383.         unsigned  b24:1;
  384.  
  385.         unsigned  b25:1;
  386.  
  387.         unsigned  b26:1;
  388.  
  389.         unsigned  b27:1;
  390.  
  391.         unsigned  b28:1;
  392.  
  393.         unsigned  b29:1;
  394.  
  395.         unsigned  b30:1;
  396.  
  397.         unsigned  b31:1;
  398.  
  399.     } N;
  400.  
  401.     /* Feldmeier expansion part 0 */
  402.  
  403.     struct {
  404.  
  405.         unsigned z2:2;
  406.  
  407.         unsigned b13_2:12;
  408.  
  409.         unsigned z1:4;
  410.  
  411.         unsigned b29_18:12;
  412.  
  413.         unsigned z0:2;
  414.  
  415.     } FE0;
  416.  
  417.     /* Feldmeier expanion part 1 */
  418.  
  419.     struct {
  420.  
  421.         unsigned b5_0:6;
  422.  
  423.         unsigned z1:4;
  424.  
  425.         unsigned b21_10:12;
  426.  
  427.         unsigned z0:4;
  428.  
  429.         unsigned b31_26:6;
  430.  
  431.     } FE1;
  432.  
  433.     struct {
  434.  
  435.         unsigned b5_0:6;
  436.  
  437.         unsigned b11_6:6;
  438.  
  439.         unsigned z1:10;
  440.  
  441.         unsigned z0:10;
  442.  
  443.     } F12;
  444.  
  445.     struct {
  446.  
  447.         unsigned b30_31:2;
  448.  
  449.         unsigned b24_29:6;
  450.  
  451.         unsigned b18_23:6;
  452.  
  453.         unsigned b12_17:6;
  454.  
  455.         unsigned b6_11:6;
  456.  
  457.         unsigned b0_5:6;
  458.  
  459.     } B6;
  460.  
  461.     struct {
  462.  
  463.         unsigned b28_31:4;
  464.  
  465.         unsigned b22_27:6;
  466.  
  467.         unsigned b16_21:6;
  468.  
  469.         unsigned b10_15:6;
  470.  
  471.         unsigned b4_9:6;
  472.  
  473.         unsigned b0_3:4;
  474.  
  475.     } B6_;
  476.  
  477.     U32 U;
  478.  
  479. } BU32;
  480.  
  481. #else
  482.  
  483. typedef union {
  484.  
  485.     /* individual bits */
  486.  
  487.     struct {
  488.  
  489.         unsigned  b31:1;
  490.  
  491.         unsigned  b30:1;
  492.  
  493.         unsigned  b29:1;
  494.  
  495.         unsigned  b28:1;
  496.  
  497.         unsigned  b27:1;
  498.  
  499.         unsigned  b26:1;
  500.  
  501.         unsigned  b25:1;
  502.  
  503.         unsigned  b24:1;
  504.  
  505.         unsigned  b23:1;
  506.  
  507.         unsigned  b22:1;
  508.  
  509.         unsigned  b21:1;
  510.  
  511.         unsigned  b20:1;
  512.  
  513.         unsigned  b19:1;
  514.  
  515.         unsigned  b18:1;
  516.  
  517.         unsigned  b17:1;
  518.  
  519.         unsigned  b16:1;
  520.  
  521.         unsigned  b15:1;
  522.  
  523.         unsigned  b14:1;
  524.  
  525.         unsigned  b13:1;
  526.  
  527.         unsigned  b12:1;
  528.  
  529.         unsigned  b11:1;
  530.  
  531.         unsigned  b10:1;
  532.  
  533.         unsigned  b9:1;
  534.  
  535.         unsigned  b8:1;
  536.  
  537.         unsigned  b7:1;
  538.  
  539.         unsigned  b6:1;
  540.  
  541.         unsigned  b5:1;
  542.  
  543.         unsigned  b4:1;
  544.  
  545.         unsigned  b3:1;
  546.  
  547.         unsigned  b2:1;
  548.  
  549.         unsigned  b1:1;
  550.  
  551.         unsigned  b0:1;
  552.  
  553.     } N;
  554.  
  555.     /* Feldmeier expansion part 0 */
  556.  
  557.     struct {
  558.  
  559.         unsigned z0:2;
  560.  
  561.         unsigned b29_18:12;
  562.  
  563.         unsigned z1:4;
  564.  
  565.         unsigned b13_2:12;
  566.  
  567.         unsigned z2:2;
  568.  
  569.     } FE0;
  570.  
  571.     /* Feldmeier expanion part 1 */
  572.  
  573.     struct {
  574.  
  575.         unsigned b31_26:6;
  576.  
  577.         unsigned z0:4;
  578.  
  579.         unsigned b21_10:12;
  580.  
  581.         unsigned z1:4;
  582.  
  583.         unsigned b5_0:6;
  584.  
  585.     } FE1;
  586.  
  587.     struct {
  588.  
  589.         unsigned z0:10;
  590.  
  591.         unsigned z1:10;
  592.  
  593.         unsigned b11_6:6;
  594.  
  595.         unsigned b5_0:6;
  596.  
  597.     } F12;
  598.  
  599.     struct {
  600.  
  601.         unsigned  b0_5:6;
  602.  
  603.         unsigned  b6_11:6;
  604.  
  605.         unsigned  b12_17:6;
  606.  
  607.         unsigned  b18_23:6;
  608.  
  609.         unsigned  b24_29:6;
  610.  
  611.         unsigned  b30_31:2;
  612.  
  613.     } B6;
  614.  
  615.     struct {
  616.  
  617.         unsigned  b0_3:4;
  618.  
  619.         unsigned  b4_9:6;
  620.  
  621.         unsigned  b10_15:6;
  622.  
  623.         unsigned  b16_21:6;
  624.  
  625.         unsigned  b22_27:6;
  626.  
  627.         unsigned  b28_31:4;
  628.  
  629.     } B6_;
  630.  
  631.     U32 U;
  632.  
  633. } BU32;
  634.  
  635. #endif
  636.  
  637.  
  638.  
  639. /*=[ Structure: BU64 ]======================================================*/
  640.  
  641.  
  642.  
  643. typedef struct {
  644.  
  645.     BU32 L, R;
  646.  
  647. } BU64;
  648.  
  649.  
  650.  
  651. /*=[ Static Tables ]========================================================*/
  652.  
  653.  
  654.  
  655. /* Shifts (shifts[]), Minus 1 */
  656.  
  657. static char SHIFTS_M1[] =
  658.  
  659.  {
  660.  
  661.   0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0,
  662.  
  663.  };
  664.  
  665.  
  666.  
  667. /*
  668.  
  669. ** The 8 original selection functions.  For some reason, they give
  670.  
  671. ** a 0-origin index, unlike everything else.
  672.  
  673. */
  674.  
  675. static char OS[8][64] =
  676.  
  677.  {
  678.  
  679.   14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
  680.  
  681.    0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
  682.  
  683.    4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
  684.  
  685.   15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,
  686.  
  687.  
  688.  
  689.   15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
  690.  
  691.    3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
  692.  
  693.    0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
  694.  
  695.   13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,
  696.  
  697.  
  698.  
  699.   10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
  700.  
  701.   13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
  702.  
  703.   13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
  704.  
  705.    1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,
  706.  
  707.  
  708.  
  709.    7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
  710.  
  711.   13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
  712.  
  713.   10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
  714.  
  715.    3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,
  716.  
  717.  
  718.  
  719.    2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
  720.  
  721.   14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
  722.  
  723.    4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
  724.  
  725.   11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,
  726.  
  727.  
  728.  
  729.   12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
  730.  
  731.   10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
  732.  
  733.    9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
  734.  
  735.    4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,
  736.  
  737.  
  738.  
  739.    4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
  740.  
  741.   13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
  742.  
  743.    1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
  744.  
  745.    6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,
  746.  
  747.  
  748.  
  749.   13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
  750.  
  751.    1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
  752.  
  753.    7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
  754.  
  755.    2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11,
  756.  
  757.  };
  758.  
  759.