mux/src/sha1.cpp File Reference

#include "copyright.h"
#include "autoconf.h"
#include "config.h"
#include "externs.h"
#include "sha1.h"

Include dependency graph for sha1.cpp:

Go to the source code of this file.

Defines

#define ROTL(d, n)   (((d) << (n)) | ((d) >> (32-(n))))
#define Ch(x, y, z)   (((x) & (y)) ^ (~(x) & (z)))
#define Parity(x, y, z)   ((x) ^ (y) ^ (z))
#define Maj(x, y, z)   (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))

Functions

void SHA1_Init (SHA1_CONTEXT *p)
static void SHA1_HashBlock (SHA1_CONTEXT *p)
void SHA1_Compute (SHA1_CONTEXT *p, size_t n, const char *buf)
void SHA1_Final (SHA1_CONTEXT *p)


Define Documentation

#define Ch ( x,
y,
 )     (((x) & (y)) ^ (~(x) & (z)))

Definition at line 24 of file sha1.cpp.

Referenced by SHA1_HashBlock().

#define Maj ( x,
y,
 )     (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))

Definition at line 26 of file sha1.cpp.

Referenced by SHA1_HashBlock().

#define Parity ( x,
y,
 )     ((x) ^ (y) ^ (z))

Definition at line 25 of file sha1.cpp.

Referenced by SHA1_HashBlock().

#define ROTL ( d,
 )     (((d) << (n)) | ((d) >> (32-(n))))

Definition at line 21 of file sha1.cpp.

Referenced by SHA1_HashBlock().


Function Documentation

void SHA1_Compute ( SHA1_CONTEXT p,
size_t  n,
const char *  buf 
)

Definition at line 99 of file sha1.cpp.

References SHA1_CONTEXT::block, SHA1_CONTEXT::nblock, SHA1_CONTEXT::nTotal, and SHA1_HashBlock().

Referenced by FUNCTION(), and mux_crypt().

00100 {
00101     while (n)
00102     {
00103         size_t m = sizeof(p->block) - p->nblock;
00104         if (n < m)
00105         {
00106             m = n;
00107         }
00108         memcpy(p->block + p->nblock, buf, m);
00109         buf += m;
00110         n -= m;
00111         p->nblock += m;
00112         p->nTotal += m;
00113 
00114         if (p->nblock == sizeof(p->block))
00115         {
00116             SHA1_HashBlock(p);
00117             p->nblock = 0;
00118         }
00119     }
00120 }

void SHA1_Final ( SHA1_CONTEXT p  ) 

Definition at line 122 of file sha1.cpp.

References SHA1_CONTEXT::block, SHA1_CONTEXT::nblock, SHA1_CONTEXT::nTotal, and SHA1_HashBlock().

Referenced by FUNCTION(), and mux_crypt().

00123 {
00124     p->block[p->nblock++] = 0x80;
00125     if (sizeof(p->block) - sizeof(UINT64) <= p->nblock)
00126     {
00127         memset(p->block + p->nblock, 0, sizeof(p->block) - p->nblock);
00128         SHA1_HashBlock(p);
00129         memset(p->block, 0, sizeof(p->block) - sizeof(UINT64));
00130     }
00131     else
00132     {
00133         memset(p->block + p->nblock, 0, sizeof(p->block) - p->nblock - sizeof(UINT64));
00134     }
00135     p->nTotal *= 8;
00136 
00137     p->block[sizeof(p->block) - 8] = (UINT8)(p->nTotal >> 56) & 0xFF;
00138     p->block[sizeof(p->block) - 7] = (UINT8)(p->nTotal >> 48) & 0xFF;
00139     p->block[sizeof(p->block) - 6] = (UINT8)(p->nTotal >> 40) & 0xFF;
00140     p->block[sizeof(p->block) - 5] = (UINT8)(p->nTotal >> 32) & 0xFF;
00141     p->block[sizeof(p->block) - 4] = (UINT8)(p->nTotal >> 24) & 0xFF;
00142     p->block[sizeof(p->block) - 3] = (UINT8)(p->nTotal >> 16) & 0xFF;
00143     p->block[sizeof(p->block) - 2] = (UINT8)(p->nTotal >>  8) & 0xFF;
00144     p->block[sizeof(p->block) - 1] = (UINT8)(p->nTotal      ) & 0xFF;
00145     SHA1_HashBlock(p);
00146 }

static void SHA1_HashBlock ( SHA1_CONTEXT p  )  [static]

Definition at line 28 of file sha1.cpp.

References SHA1_CONTEXT::block, Ch, SHA1_CONTEXT::H, Maj, Parity, and ROTL.

Referenced by SHA1_Compute(), and SHA1_Final().

00029 {
00030     int t;
00031     UINT32 W[80];
00032 
00033     // Prepare Message Schedule, {W sub t}.
00034     //
00035     int j;
00036     for (t = 0, j = 0; t <= 15; t++, j += 4)
00037     {
00038         W[t] = (p->block[j  ] << 24)
00039              | (p->block[j+1] << 16)
00040              | (p->block[j+2] <<  8)
00041              | (p->block[j+3]      );
00042     }
00043     for (t = 16; t <= 79; t++)
00044     {
00045         W[t] = ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
00046     }
00047 
00048     UINT32 a = p->H[0];
00049     UINT32 b = p->H[1];
00050     UINT32 c = p->H[2];
00051     UINT32 d = p->H[3];
00052     UINT32 e = p->H[4];
00053 
00054     UINT32 T;
00055     for (t =  0; t <= 19; t++)
00056     {
00057         T = ROTL(a,5) + Ch(b,c,d) + e + 0x5A827999 + W[t];
00058         e = d;
00059         d = c;
00060         c = ROTL(b,30);
00061         b = a;
00062         a = T;
00063     }
00064     for (t = 20; t <= 39; t++)
00065     {
00066         T = ROTL(a,5) + Parity(b,c,d) + e + 0x6ED9EBA1 + W[t];
00067         e = d;
00068         d = c;
00069         c = ROTL(b,30);
00070         b = a;
00071         a = T;
00072     }
00073     for (t = 40; t <= 59; t++)
00074     {
00075         T = ROTL(a,5) + Maj(b,c,d) + e + 0x8F1BBCDC + W[t];
00076         e = d;
00077         d = c;
00078         c = ROTL(b,30);
00079         b = a;
00080         a = T;
00081     }
00082     for (t = 60; t <= 79; t++)
00083     {
00084         T = ROTL(a,5) + Parity(b,c,d) + e + 0xCA62C1D6 + W[t];
00085         e = d;
00086         d = c;
00087         c = ROTL(b,30);
00088         b = a;
00089         a = T;
00090     }
00091 
00092     p->H[0] += a;
00093     p->H[1] += b;
00094     p->H[2] += c;
00095     p->H[3] += d;
00096     p->H[4] += e;
00097 }

void SHA1_Init ( SHA1_CONTEXT p  ) 

Definition at line 7 of file sha1.cpp.

References SHA1_CONTEXT::H, SHA1_CONTEXT::nblock, and SHA1_CONTEXT::nTotal.

Referenced by FUNCTION(), and mux_crypt().

00008 {
00009     p->H[0] = 0x67452301;
00010     p->H[1] = 0xEFCDAB89;
00011     p->H[2] = 0x98BADCFE;
00012     p->H[3] = 0x10325476;
00013     p->H[4] = 0xC3D2E1F0;
00014     p->nTotal = 0;
00015     p->nblock = 0;
00016 }


Generated on Mon May 28 04:40:22 2007 for MUX by  doxygen 1.4.7