home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / HPACK78S.ZIP / crypt / rsa.h < prev   
C/C++ Source or Header  |  1992-11-28  |  5KB  |  112 lines

  1. /****************************************************************************
  2. *                                                                            *
  3. *                            HPACK Multi-System Archiver                        *
  4. *                            ===========================                        *
  5. *                                                                            *
  6. *                               RSA Library Interface                        *
  7. *                              RSA.H  Updated 23/08/91                        *
  8. *                                                                            *
  9. * This program is protected by copyright and as such any use or copying of    *
  10. *  this code for your own purposes directly or indirectly is highly uncool    *
  11. *                      and if you do so there will be....trubble.                *
  12. *                 And remember: We know where your kids go to school.            *
  13. *                                                                            *
  14. *            Copyright 1991  Phil Zimmermann.  All rights reserved            *
  15. *                        Adapted 1991  Peter C.Gutmann                        *
  16. *                                                                            *
  17. ****************************************************************************/
  18.  
  19. #include "defs.h"
  20.  
  21. /* Define the following to enable fast assembly-language RSA primitives */
  22.  
  23. #ifdef __MSDOS__
  24.   #define ASM_RSA
  25. #endif /* __MSDOS__ */
  26.  
  27. /* Basic type for the multiprecision registers.  The internal representation
  28.    for these extended precision integer registers is an array of "units",
  29.    where a unit is a machine word, in this case a WORD.  To perform arithmetic
  30.    on these huge precision integers, we pass pointers to these unit arrays to
  31.    various subroutines */
  32.  
  33. #define MP_REG            WORD
  34.  
  35. /* Macros to convert a bitcount to various other values */
  36.  
  37. #define UNITSIZE        16            /* 16 bits per machine word */
  38.  
  39. #define bits2units(n)    ( ( ( n ) + 15 ) >> 4 )
  40. #define units2bits(n)    ( ( n ) << 4 )
  41. #define bytes2units(n)    ( ( ( n ) + 1 ) >> 1 )
  42. #define units2bytes(n)    ( ( n ) << 1 )
  43. #define bits2bytes(n)    ( ( ( n ) + 7 ) >> 3 )
  44.  
  45. /* MAX_BIT_PRECISION is the expected upper limit on key size.  Note that
  46.    MAX_BIT_PRECISION >= max.key length + SLOP_BITS.  In our case we have
  47.    a maximum key length of 2048 bits + 17 slop bits = 2080 bits (rounded) */
  48.  
  49. #define    MAX_BIT_PRECISION    2080
  50. #define    MAX_BYTE_PRECISION    ( MAX_BIT_PRECISION / 8 )
  51. #define    MAX_UNIT_PRECISION    ( MAX_BIT_PRECISION / UNITSIZE )
  52.  
  53. extern int globalPrecision;            /* Level of precision for all routines */
  54.  
  55. /* Defines for the RSA routines themselves */
  56.  
  57. #define mp_burn                mp_clear        /* For destroying sensitive data */
  58. #define mp_modsquare(r1,r2)    mp_modmult( r1, r2, r2 )
  59. #define mp_shiftleft(r1)    mp_rotateleft( r1, 0 )
  60.  
  61. #ifndef ASM_RSA
  62.   /* The following primitives should be coded in assembly */
  63.   void mp_setp( const int precision );
  64.   void mp_add( MP_REG *reg1, MP_REG *reg2 );
  65.   BOOLEAN mp_sub( MP_REG *reg1, MP_REG *reg2 );
  66.   void mp_rotateleft( MP_REG *reg1, const BOOLEAN carry );
  67.  
  68.   /* Equivalent functions for some of the primitives */
  69.   #define setPrecision(prec)    mp_setp( units2bits( globalPrecision = ( prec ) ) )
  70.   #define mp_move(dest,src)        memcpy( dest, src, globalPrecision * sizeof( MP_REG ) )
  71.   #define mp_clear(reg,count)    memset( reg, 0, ( count ) * sizeof( MP_REG ) )
  72. #else
  73.   /* The following primitives should be coded in assembly */
  74.   void MP_SETP( const int precision );
  75.   void MP_MOVE( MP_REG *dest, MP_REG *src );
  76.   void MP_CLR( MP_REG *mpReg, const int precision );
  77.   void MP_ADD( MP_REG *reg1, MP_REG *reg2 );
  78.   BOOLEAN MP_SUB( MP_REG *reg1, MP_REG *reg2 );
  79.   void MP_ROL( MP_REG *reg1, const BOOLEAN carry );
  80.  
  81.   /* Define C primitive names as the equivalent calls to assembly primitives */
  82.   #define setPrecision(precision)    MP_SETP( units2bits( globalPrecision = ( precision ) ) )
  83.   #define mp_add                    MP_ADD
  84.   #define mp_sub                    MP_SUB
  85.   #define mp_rotateleft                MP_ROL
  86.   #define mp_move                    MP_MOVE
  87.   #define mp_clear                    MP_CLR
  88. #endif    /* !ASM_RSA */
  89.  
  90. /* SLOP_BITS is how many "carry bits" to allow for intermediate calculation
  91.    results to exceed the size of the modulus.  It is used by modexp to give
  92.    some overflow elbow room for modmult to use to perform modulo operations
  93.    with the modulus.  The number of slop bits required is determined by the
  94.    modmult algorithm */
  95.  
  96. #define SLOP_BITS        ( UNITSIZE + 1 )
  97.  
  98. /* Defines to perform various tests on MPI's */
  99.  
  100. #define testEq(mpi,val)    ( ( *( mpi ) == ( val ) ) && ( significance( mpi ) <= 1 ) )
  101. #define countbits(mpi)    initBitSniffer( mpi )
  102. #define countbytes(mpi)    ( ( countbits( mpi ) + 7 ) >> 3 )
  103. #define normalize(mpi)    mpi += significance( mpi ) - 1
  104.  
  105. /* Prototypes for functions in RSA.C */
  106.  
  107. int initBitSniffer( MP_REG *mpReg );
  108. int significance( MP_REG *mpReg );
  109. void mp_init( MP_REG *mpReg, WORD value );
  110. void mp_modexp( MP_REG *expOut, MP_REG *expIn, MP_REG *exponent, MP_REG *modulus );
  111. void rsaDecrypt( MP_REG *M, MP_REG *C, MP_REG *d, MP_REG *p, MP_REG *q, MP_REG *u );
  112.