⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sflcryp.c

📁 短小精悍的C语言标准函数库。提供450个以上的可移植的算法和工具代码。
💻 C
📖 第 1 页 / 共 4 页
字号:

    x0 = (qbyte) *(dataIn++);
    x1 = (qbyte) *(dataIn++);
    x2 = (qbyte) *(dataIn++);
    x3 = (qbyte) *(dataIn);

    for (round = nofRound; round > 0; round--)
      {
        x0  = Mul (x0,  (qbyte) * (key++));
        x1  =     (x1 + (qbyte) * (key++)) & ones;
        x2  =     (x2 + (qbyte) * (key++)) & ones;
        x3  = Mul (x3,  (qbyte) * (key++));
        t0  = Mul (     (qbyte) * (key++), x0 ^ x2);
        t1  = Mul (     (qbyte) * (key++), (t0 + (x1 ^ x3)) & ones);
        t0  = (t0 + t1) & ones;
        x0 ^= t1;
        x3 ^= t0;
        t0 ^= x1;
        x1  = x2 ^ t1;
        x2  = t0;
      }
    *(dataOut++) = (dbyte) (Mul (x0,  (qbyte) * (key++)));
    *(dataOut++) = (dbyte) (    (x2 + (qbyte) * (key++)) & ones);
    *(dataOut++) = (dbyte) (    (x1 + (qbyte) * (key++)) & ones);
    *(dataOut)   = (dbyte) (Mul (x3,  (qbyte) * key));
}


/*  -------------------------------------------------------------------------
    Function: invert_idea_key - internal

    Synopsis: invert decryption / encrytion key for IDEA
    -------------------------------------------------------------------------*/

static void
invert_idea_key (dbyte *key, dbyte *invKey)
{
    register int
        i;

    key_t (dk);

    dk [nofKeyPerRound * nofRound + 0] = MulInv (*(key++));
    dk [nofKeyPerRound * nofRound + 1] = (dbyte) (addMod - *(key++)) & ones;
    dk [nofKeyPerRound * nofRound + 2] = (dbyte) (addMod - *(key++)) & ones;
    dk [nofKeyPerRound * nofRound + 3] = MulInv (*(key++));
    for (i = nofKeyPerRound * (nofRound - 1); i >= 0; i -= nofKeyPerRound)
      {
        dk [i + 4] =         *(key++);
        dk [i + 5] =         *(key++);
        dk [i + 0] = MulInv (*(key++));
        if (i > 0)
          {
            dk [i + 2] = (dbyte) (addMod - *(key++)) & ones;
            dk [i + 1] = (dbyte) (addMod - *(key++)) & ones;
          }
        else
          {
            dk [i + 1] = (dbyte) (addMod - *(key++)) & ones;
            dk [i + 2] = (dbyte) (addMod - *(key++)) & ones;
          }
        dk [i + 3] = MulInv (*(key++));
      }
    for (i = 0; i < keyLen; i++)
        invKey [i] = dk [i];
}


/*  -------------------------------------------------------------------------
    Function: Mul

    Synopsis: Multiplication (used in IDEA).
    -------------------------------------------------------------------------*/

static qbyte
Mul (qbyte a, qbyte b)
{
    long
        p;
    qbyte
        q;

    if (a == 0)
        p = mulMod - b;
    else
    if (b == 0)
        p = mulMod - a;
    else
      {
        q = a * b;
        p = (q & ones) - (q >> 16);
        if (p <= 0)
            p += mulMod;
      }
    return ((qbyte) (p & ones));
}


/*  -------------------------------------------------------------------------
    Function: MulInv

    Synopsis: compute inverse of 'x' by Euclidean gcd algorithm
             (used in IDEA).
    -------------------------------------------------------------------------*/

static dbyte
MulInv (dbyte x)
{
    long
        n1, n2,
        q,  r,
        b1, b2,
        t;

    if (x == 0)
        return (0);

    n1 = mulMod;
    n2 = (long)x;
    b2 = 1;
    b1 = 0;
    do
      {
        r = (n1 % n2);
        q = (n1 - r) / n2;
        if (r == 0)
          {
            if (b2 < 0)
                b2 = mulMod + b2;
          }
        else
          {
            n1 = n2;
            n2 = r;
            t  = b2;
            b2 = b1 - q * b2;
            b1 = t;
          }
      } while (r != 0);

    return ((dbyte) b2);
}


/*  -------------------------------------------------------------------------
    Function: mdc - internal

    Synopsis: Basic transform for Karn encryption.  Take two 16-byte
    half-buffers, two 48-byte keys (which must be distinct), and use
    the MD5 Transform algorithm to produce two 16-byte output
    half-buffers.

    This is reversible: If we get out1 and out2 from in1, in2, key1, key2,
    then we can get in2 and in1 from out2, out1, key1, key2.

    in1, in2, out1, and out2 should point to 16-byte buffers.
    By convention, in1 and in2 are two halves of a 32-byte input
    buffer, and out1 and out2 are two halves of a 32-byte output
    buffer.

    key1 and key2 should point to 48-byte buffers with different contents.
    -------------------------------------------------------------------------*/

static void
mdc (qbyte *out1, qbyte *out2,
     qbyte *in1,  qbyte *in2,
     qbyte *key1, qbyte *key2)
{
    int
       index;
    qbyte
        buffer [16],
        hash   [4],
        temp   [4];

    memcpy (hash, ihash, sizeof (hash));
    memcpy (buffer, in1, 16);
    memcpy (buffer + 4, key1, 48);
    Transform (hash, buffer);

    for (index = 0; index < 4; ++index)
        temp [index] = buffer [index] = in2 [index] ^ hash [index];

    memcpy (hash, ihash, sizeof (hash));
    memcpy (buffer + 4, key2, 48);
    Transform (hash, buffer);

    for (index = 0; index < 4; ++index)
        out2 [index] = buffer [index] = in1 [index] ^ hash [index];

    memcpy (hash, ihash, sizeof (hash));
    memcpy (buffer + 4, key1, 48);
    Transform (hash, buffer);

    for (index = 0; index < 4; ++index)
        out1 [index] = temp [index] ^ hash [index];
}


/*  -------------------------------------------------------------------------
    Function: Transform -- internal

    Synopsis: Basic MD5 step. Transforms buffer based on in
    -------------------------------------------------------------------------*/

static void
Transform (qbyte *buffer, qbyte *in)
{
    qbyte
        a = buffer [0],
        b = buffer [1],
        c = buffer [2],
        d = buffer [3];

    /*  Round 1  */
#   define S11 7
#   define S12 12
#   define S13 17
#   define S14 22
    FF (a, b, c, d, in [0] , S11, 3614090360UL);   /* 1                      */
    FF (d, a, b, c, in [1] , S12, 3905402710UL);   /* 2                      */
    FF (c, d, a, b, in [2] , S13, 606105819UL);    /* 3                      */
    FF (b, c, d, a, in [3] , S14, 3250441966UL);   /* 4                      */
    FF (a, b, c, d, in [4] , S11, 4118548399UL);   /* 5                      */
    FF (d, a, b, c, in [5] , S12, 1200080426UL);   /* 6                      */
    FF (c, d, a, b, in [6] , S13, 2821735955UL);   /* 7                      */
    FF (b, c, d, a, in [7] , S14, 4249261313UL);   /* 8                      */
    FF (a, b, c, d, in [8] , S11, 1770035416UL);   /* 9                      */
    FF (d, a, b, c, in [9] , S12, 2336552879UL);   /* 10                     */
    FF (c, d, a, b, in [10], S13, 4294925233UL);   /* 11                     */
    FF (b, c, d, a, in [11], S14, 2304563134UL);   /* 12                     */
    FF (a, b, c, d, in [12], S11, 1804603682UL);   /* 13                     */
    FF (d, a, b, c, in [13], S12, 4254626195UL);   /* 14                     */
    FF (c, d, a, b, in [14], S13, 2792965006UL);   /* 15                     */
    FF (b, c, d, a, in [15], S14, 1236535329UL);   /* 16                     */

    /*  Round 2    */
#   define S21 5
#   define S22 9
#   define S23 14
#   define S24 20
    GG (a, b, c, d, in [1] , S21, 4129170786UL);   /* 17                     */
    GG (d, a, b, c, in [6] , S22, 3225465664UL);   /* 18                     */
    GG (c, d, a, b, in [11], S23, 643717713UL);    /* 19                     */
    GG (b, c, d, a, in [0] , S24, 3921069994UL);   /* 20                     */
    GG (a, b, c, d, in [5] , S21, 3593408605UL);   /* 21                     */
    GG (d, a, b, c, in [10], S22, 38016083UL);     /* 22                     */
    GG (c, d, a, b, in [15], S23, 3634488961UL);   /* 23                     */
    GG (b, c, d, a, in [4] , S24, 3889429448UL);   /* 24                     */
    GG (a, b, c, d, in [9] , S21, 568446438UL);    /* 25                     */
    GG (d, a, b, c, in [14], S22, 3275163606UL);   /* 26                     */
    GG (c, d, a, b, in [3] , S23, 4107603335UL);   /* 27                     */
    GG (b, c, d, a, in [8] , S24, 1163531501UL);   /* 28                     */
    GG (a, b, c, d, in [13], S21, 2850285829UL);   /* 29                     */
    GG (d, a, b, c, in [2] , S22, 4243563512UL);   /* 30                     */
    GG (c, d, a, b, in [7] , S23, 1735328473UL);   /* 31                     */
    GG (b, c, d, a, in [12], S24, 2368359562UL);   /* 32                     */

    /* Round 3   */
#   define S31 4
#   define S32 11
#   define S33 16
#   define S34 23
    HH (a, b, c, d, in [5] , S31, 4294588738UL);   /* 33                     */
    HH (d, a, b, c, in [8] , S32, 2272392833UL);   /* 34                     */
    HH (c, d, a, b, in [11], S33, 1839030562UL);   /* 35                     */
    HH (b, c, d, a, in [14], S34, 4259657740UL);   /* 36                     */
    HH (a, b, c, d, in [1] , S31, 2763975236UL);   /* 37                     */
    HH (d, a, b, c, in [4] , S32, 1272893353UL);   /* 38                     */
    HH (c, d, a, b, in [7] , S33, 4139469664UL);   /* 39                     */
    HH (b, c, d, a, in [10], S34, 3200236656UL);   /* 40                     */
    HH (a, b, c, d, in [13], S31, 681279174UL);    /* 41                     */
    HH (d, a, b, c, in [0] , S32, 3936430074UL);   /* 42                     */
    HH (c, d, a, b, in [3] , S33, 3572445317UL);   /* 43                     */
    HH (b, c, d, a, in [6] , S34, 76029189UL);     /* 44                     */
    HH (a, b, c, d, in [9] , S31, 3654602809UL);   /* 45                     */
    HH (d, a, b, c, in [12], S32, 3873151461UL);   /* 46                     */
    HH (c, d, a, b, in [15], S33, 530742520UL);    /* 47                     */
    HH (b, c, d, a, in [2] , S34, 3299628645UL);   /* 48                     */

    /*  Round 4    */
#   define S41 6
#   define S42 10
#   define S43 15
#   define S44 21
    II (a, b, c, d, in [0] , S41, 4096336452UL);   /* 49                     */
    II (d, a, b, c, in [7] , S42, 1126891415UL);   /* 50                     */
    II (c, d, a, b, in [14], S43, 2878612391UL);   /* 51                     */
    II (b, c, d, a, in [5] , S44, 4237533241UL);   /* 52                     */
    II (a, b, c, d, in [12], S41, 1700485571UL);   /* 53                     */
    II (d, a, b, c, in [3] , S42, 2399980690UL);   /* 54                     */
    II (c, d, a, b, in [10], S43, 4293915773UL);   /* 55                     */
    II (b, c, d, a, in [1] , S44, 2240044497UL);   /* 56                     */
    II (a, b, c, d, in [8] , S41, 1873313359UL);   /* 57                     */
    II (d, a, b, c, in [15], S42, 4264355552UL);   /* 58                     */
    II (c, d, a, b, in [6] , S43, 2734768916UL);   /* 59                     */
    II (b, c, d, a, in [13], S44, 1309151649UL);   /* 60                     */
    II (a, b, c, d, in [4] , S41, 4149444226UL);   /* 61                     */
    II (d, a, b, c, in [11], S42, 3174756917UL);   /* 62                     */
    II (c, d, a, b, in [2] , S43,  718787259UL);   /* 63                     */
    II (b, c, d, a, in [9] , S44, 3951481745UL);   /* 64                     */

    buffer [0] += a;
    buffer [1] += b;
    buffer [2] += c;
    buffer [3] += d;
}


/*  -------------------------------------------------------------------------
    Function: mdc_encrypt - internal

    Synopsis: encrypt a buffer using the MDC algorithm.
    -------------------------------------------------------------------------*/

static void
mdc_encrypt (qbyte *in, qbyte *out, qbyte *key)
{
    mdc (out, &out [4],
         in,  &in  [4],
         key, &key [12]);
}


/*  -------------------------------------------------------------------------
    Function: mdc_decrypt - internal

    Synopsis: decrypt a buffer using the MDC algorithm.
    -------------------------------------------------------------------------*/

static void
mdc_decrypt (qbyte *in, qbyte *out, qbyte *key)
{
    mdc (&out [4], out,
         &in  [4], in,
         key,      &key [12]);
}


/*- DES encryption ----------------------------------------------------------*/

#define c2l(c,l)            (l  = ((qbyte) (*((c)++))),       \
                             l |= ((qbyte) (*((c)++))) <<  8, \
                             l |= ((qbyte) (*((c)++))) << 16, \
                             l |= ((qbyte) (*((c)++))) << 24)

#define l2c(l,c)            (*((c)++) = (byte) (((l))      & 0xff), \
                             *((c)++) = (byte) (((l) >> 8) & 0xff), \
                             *((c)++) = (byte) (((l) >>16) & 0xff), \
                             *((c)++) = (byte) (((l) >>24) & 0xff))

/*  The changes to this macro may help or hinder, depending on the
 *  compiler and the achitecture. gcc2 always seems to do well :-).
 *  Inspired by Dana How <how@isl.stanford.edu>
 *  DO NOT use the alternative version on machines with 8 byte longs.        */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -