Changeset 6497 for trunk/licq

Show
Ignore:
Timestamp:
08/24/08 23:56:35 (3 months ago)
Author:
flynd
Message:

Fixed indentation and some other coding style for the MD5 functions.

Location:
trunk/licq
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/licq/include/licq_md5.h

    r6496 r6497  
    33 *         Modified by Ian Jackson so as not to use Colin Plumb's 
    44 *         'usuals.h'. 
    5  *  
     5 * 
    66 *         This file is in the public domain. 
    77 */ 
     
    1717void md5(const uint8_t* buf, size_t len, uint8_t* digest); 
    1818 
    19 struct MD5Context 
     19struct Md5Context 
    2020{ 
    2121  uint32_t buf[4]; 
     
    2424}; 
    2525 
    26 void MD5Init(struct MD5Context *context); 
    27 void MD5Update(struct MD5Context* context, uint8_t const* buf, size_t len); 
    28 void MD5Final(unsigned char digest[16], struct MD5Context *context); 
    29 void MD5Transform(uint32_t buf[4], const uint32_t in[16]); 
     26void md5Init(struct Md5Context* context); 
     27void md5Update(struct Md5Context* context, uint8_t const* buf, size_t len); 
     28void md5Final(struct Md5Context* context, unsigned char digest[16]); 
     29void md5Transform(uint32_t buf[4], const uint32_t in[16]); 
    3030 
    31 void byteSwap(uint32_t* buf, unsigned words); 
    32  
    33 #endif              /* !MD5_H */ 
     31#endif 
  • trunk/licq/src/md5.cpp

    r6496 r6497  
    1919 */ 
    2020 
    21 #include <cstring>      /* for memcpy() */ 
     21#include <cstring> // for memcpy() 
    2222 
    2323#include "licq_md5.h" 
     
    2525void md5(const uint8_t* buf, size_t len, uint8_t* digest) 
    2626{ 
    27   MD5Context context; 
    28  
    29   MD5Init(&context); 
    30   MD5Update(&context, buf, len); 
    31   MD5Final(digest, &context); 
     27  Md5Context context; 
     28 
     29  md5Init(&context); 
     30  md5Update(&context, buf, len); 
     31  md5Final(&context, digest); 
    3232} 
    3333 
     
    4848 * initialization constants. 
    4949 */ 
    50 void MD5Init(struct MD5Context *ctx) 
    51 { 
    52     ctx->buf[0]     = 0x67452301; 
    53     ctx->buf[1]     = 0xefcdab89; 
    54     ctx->buf[2]     = 0x98badcfe; 
    55     ctx->buf[3]     = 0x10325476; 
     50void md5Init(struct Md5Context* ctx) 
     51{ 
     52  ctx->buf[0] = 0x67452301; 
     53  ctx->buf[1] = 0xefcdab89; 
     54  ctx->buf[2] = 0x98badcfe; 
     55  ctx->buf[3] = 0x10325476; 
    5656  ctx->bytes = 0; 
    5757} 
     
    6161 * of bytes. 
    6262 */ 
    63 void MD5Update(struct MD5Context* ctx, uint8_t const* buf, size_t len) 
    64 { 
    65   /* Space available in ctx->in (at least 1) */ 
     63void md5Update(struct Md5Context* ctx, uint8_t const* buf, size_t len) 
     64{ 
     65  // Space available in ctx->in (at least 1) 
    6666  uint32_t t = 64 - (ctx->bytes & 0x3f); 
    6767 
    68   /* Update byte count */ 
     68  // Update byte count 
    6969  ctx->bytes += len; 
    7070 
    71     if (t > len) { 
     71  if (t > len) 
     72  { 
    7273    memcpy((uint8_t*)ctx->in + 64 - t, buf, len); 
    73         return; 
    74     } 
    75     /* First chunk is an odd size */ 
     74    return; 
     75  } 
     76 
     77  // First chunk is an odd size 
    7678  memcpy((uint8_t*)ctx->in + 64 - t, buf, t); 
    77     byteSwap(ctx->in, 16); 
    78     MD5Transform(ctx->buf, ctx->in); 
    79     buf += t; 
    80     len -= t; 
    81  
    82     /* Process data in 64-byte chunks */ 
    83     while (len >= 64) { 
    84         memcpy(ctx->in, buf, 64); 
    85         byteSwap(ctx->in, 16); 
    86         MD5Transform(ctx->buf, ctx->in); 
    87         buf += 64; 
    88         len -= 64; 
    89     } 
    90  
    91     /* Handle any remaining bytes of data. */ 
    92     memcpy(ctx->in, buf, len); 
    93 } 
    94  
    95 /* 
    96  * Final wrapup - pad to 64-byte boundary with the bit pattern  
     79  byteSwap(ctx->in, 16); 
     80  md5Transform(ctx->buf, ctx->in); 
     81  buf += t; 
     82  len -= t; 
     83 
     84  // Process data in 64-byte chunks 
     85  for ( ; len >= 64; len -= 64) 
     86  { 
     87    memcpy(ctx->in, buf, 64); 
     88    byteSwap(ctx->in, 16); 
     89    md5Transform(ctx->buf, ctx->in); 
     90    buf += 64; 
     91  } 
     92 
     93  // Handle any remaining bytes of data. 
     94  memcpy(ctx->in, buf, len); 
     95} 
     96 
     97/* 
     98 * Final wrapup - pad to 64-byte boundary with the bit pattern 
    9799 * 1 0* (64-bit count of bits processed, MSB-first) 
    98100 */ 
    99 void MD5Final(uint8_t digest[16], struct MD5Context *ctx) 
    100 { 
    101   int count = ctx->bytes & 0x3f; /* Number of bytes in ctx->in */ 
     101void md5Final(struct Md5Context* ctx, uint8_t digest[16]) 
     102{ 
     103  // Number of bytes in ctx->in 
     104  int count = ctx->bytes & 0x3f; 
    102105  uint8_t* p = (uint8_t*)ctx->in + count; 
    103106 
    104     /* Set the first char of padding to 0x80.  There is always room. */ 
    105     *p++ = 0x80; 
    106  
    107     /* Bytes of padding needed to make 56 bytes (-8..55) */ 
    108     count = 56 - 1 - count; 
    109  
    110     if (count < 0) {    /* Padding forces an extra block */ 
    111         memset(p, 0, count + 8); 
    112         byteSwap(ctx->in, 16); 
    113         MD5Transform(ctx->buf, ctx->in); 
     107  // Set the first char of padding to 0x80.  There is always room. 
     108  *p++ = 0x80; 
     109 
     110  // Bytes of padding needed to make 56 bytes (-8..55) 
     111  count = 56 - 1 - count; 
     112 
     113  if (count < 0) 
     114  { 
     115    // Padding forces an extra block 
     116    memset(p, 0, count + 8); 
     117    byteSwap(ctx->in, 16); 
     118    md5Transform(ctx->buf, ctx->in); 
    114119    p = (uint8_t*)ctx->in; 
    115         count = 56; 
    116     } 
    117     memset(p, 0, count); 
    118     byteSwap(ctx->in, 14); 
    119  
    120     /* Append length in bits and transform */ 
     120    count = 56; 
     121  } 
     122  memset(p, 0, count); 
     123  byteSwap(ctx->in, 14); 
     124 
     125  // Append length in bits and transform 
    121126  ctx->in[14] = ctx->bytes << 3; 
    122127  ctx->in[15] = ctx->bytes >> 29; 
    123     MD5Transform(ctx->buf, ctx->in); 
    124  
    125     byteSwap(ctx->buf, 4); 
    126     memcpy(digest, ctx->buf, 16); 
    127     memset(ctx, 0, sizeof(ctx));    /* In case it's sensitive */ 
    128 } 
    129  
    130 #ifndef ASM_MD5 
    131  
    132 /* The four core functions - F1 is optimized somewhat */ 
    133  
    134 /* #define F1(x, y, z) (x & y | ~x & z) */ 
     128  md5Transform(ctx->buf, ctx->in); 
     129 
     130  byteSwap(ctx->buf, 4); 
     131  memcpy(digest, ctx->buf, 16); 
     132  memset(ctx, 0, sizeof(ctx)); // In case it's sensitive 
     133} 
     134 
     135// The four core functions - F1 is optimized somewhat 
     136 
     137// #define F1(x, y, z) (x & y | ~x & z) 
    135138#define F1(x, y, z) (z ^ (x & (y ^ z))) 
    136139#define F2(x, y, z) F1(z, x, y) 
     
    138141#define F4(x, y, z) (y ^ (x | ~z)) 
    139142 
    140 /* This is the central step in the MD5 algorithm. */ 
     143// This is the central step in the MD5 algorithm. 
    141144#define MD5STEP(f,w,x,y,z,in,s) \ 
    142145         (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x) 
     
    147150 * the data and converts bytes into longwords for this routine. 
    148151 */ 
    149 void MD5Transform(uint32_t buf[4], const uint32_t in[16]) 
     152void md5Transform(uint32_t buf[4], const uint32_t in[16]) 
    150153{ 
    151154  register uint32_t a, b, c, d; 
    152155 
    153     a = buf[0]; 
    154     b = buf[1]; 
    155     c = buf[2]; 
    156     d = buf[3]; 
    157  
    158     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 
    159     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 
    160     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 
    161     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 
    162     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 
    163     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 
    164     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 
    165     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 
    166     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 
    167     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 
    168     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 
    169     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 
    170     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 
    171     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 
    172     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 
    173     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 
    174  
    175     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 
    176     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 
    177     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 
    178     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 
    179     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 
    180     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 
    181     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 
    182     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 
    183     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 
    184     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 
    185     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 
    186     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 
    187     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 
    188     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 
    189     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 
    190     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 
    191  
    192     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 
    193     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 
    194     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 
    195     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 
    196     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 
    197     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 
    198     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 
    199     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 
    200     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 
    201     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 
    202     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 
    203     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 
    204     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 
    205     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 
    206     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 
    207     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 
    208  
    209     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 
    210     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 
    211     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 
    212     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 
    213     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 
    214     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 
    215     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 
    216     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 
    217     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 
    218     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 
    219     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 
    220     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 
    221     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 
    222     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 
    223     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 
    224     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 
    225  
    226     buf[0] += a; 
    227     buf[1] += b; 
    228     buf[2] += c; 
    229     buf[3] += d; 
    230 } 
    231  
    232 #endif 
     156  a = buf[0]; 
     157  b = buf[1]; 
     158  c = buf[2]; 
     159  d = buf[3]; 
     160 
     161  MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 
     162  MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 
     163  MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 
     164  MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 
     165  MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 
     166  MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 
     167  MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 
     168  MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 
     169  MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 
     170  MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 
     171  MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 
     172  MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 
     173  MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 
     174  MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 
     175  MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 
     176  MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 
     177 
     178  MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 
     179  MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 
     180  MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 
     181  MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 
     182  MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 
     183  MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 
     184  MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 
     185  MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 
     186  MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 
     187  MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 
     188  MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 
     189  MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 
     190  MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 
     191  MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 
     192  MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 
     193  MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 
     194 
     195  MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 
     196  MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 
     197  MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 
     198  MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 
     199  MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 
     200  MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 
     201  MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 
     202  MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 
     203  MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 
     204  MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 
     205  MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 
     206  MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 
     207  MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 
     208  MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 
     209  MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 
     210  MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 
     211 
     212  MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 
     213  MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 
     214  MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 
     215  MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 
     216  MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 
     217  MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 
     218  MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 
     219  MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 
     220  MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 
     221  MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 
     222  MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 
     223  MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 
     224  MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 
     225  MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 
     226  MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 
     227  MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 
     228 
     229  buf[0] += a; 
     230  buf[1] += b; 
     231  buf[2] += c; 
     232  buf[3] += d; 
     233}