Back to index

lightning-sunbird  0.9+nobinonly
md5.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "prerr.h"
00038 #include "secerr.h"
00039 
00040 #include "prtypes.h"
00041 #include "prlong.h"
00042 
00043 #include "blapi.h"
00044 
00045 #define MD5_HASH_LEN 16
00046 #define MD5_BUFFER_SIZE 64
00047 #define MD5_END_BUFFER (MD5_BUFFER_SIZE - 8)
00048 
00049 #define CV0_1 0x67452301
00050 #define CV0_2 0xefcdab89
00051 #define CV0_3 0x98badcfe
00052 #define CV0_4 0x10325476
00053 
00054 #define T1_0  0xd76aa478
00055 #define T1_1  0xe8c7b756
00056 #define T1_2  0x242070db
00057 #define T1_3  0xc1bdceee
00058 #define T1_4  0xf57c0faf
00059 #define T1_5  0x4787c62a
00060 #define T1_6  0xa8304613
00061 #define T1_7  0xfd469501
00062 #define T1_8  0x698098d8
00063 #define T1_9  0x8b44f7af
00064 #define T1_10 0xffff5bb1
00065 #define T1_11 0x895cd7be
00066 #define T1_12 0x6b901122
00067 #define T1_13 0xfd987193
00068 #define T1_14 0xa679438e
00069 #define T1_15 0x49b40821
00070 
00071 #define T2_0  0xf61e2562
00072 #define T2_1  0xc040b340
00073 #define T2_2  0x265e5a51
00074 #define T2_3  0xe9b6c7aa
00075 #define T2_4  0xd62f105d
00076 #define T2_5  0x02441453
00077 #define T2_6  0xd8a1e681
00078 #define T2_7  0xe7d3fbc8
00079 #define T2_8  0x21e1cde6
00080 #define T2_9  0xc33707d6
00081 #define T2_10 0xf4d50d87
00082 #define T2_11 0x455a14ed
00083 #define T2_12 0xa9e3e905
00084 #define T2_13 0xfcefa3f8
00085 #define T2_14 0x676f02d9
00086 #define T2_15 0x8d2a4c8a
00087 
00088 #define T3_0  0xfffa3942
00089 #define T3_1  0x8771f681
00090 #define T3_2  0x6d9d6122
00091 #define T3_3  0xfde5380c
00092 #define T3_4  0xa4beea44
00093 #define T3_5  0x4bdecfa9
00094 #define T3_6  0xf6bb4b60
00095 #define T3_7  0xbebfbc70
00096 #define T3_8  0x289b7ec6
00097 #define T3_9  0xeaa127fa
00098 #define T3_10 0xd4ef3085
00099 #define T3_11 0x04881d05
00100 #define T3_12 0xd9d4d039
00101 #define T3_13 0xe6db99e5
00102 #define T3_14 0x1fa27cf8
00103 #define T3_15 0xc4ac5665
00104 
00105 #define T4_0  0xf4292244
00106 #define T4_1  0x432aff97
00107 #define T4_2  0xab9423a7
00108 #define T4_3  0xfc93a039
00109 #define T4_4  0x655b59c3
00110 #define T4_5  0x8f0ccc92
00111 #define T4_6  0xffeff47d
00112 #define T4_7  0x85845dd1
00113 #define T4_8  0x6fa87e4f
00114 #define T4_9  0xfe2ce6e0
00115 #define T4_10 0xa3014314
00116 #define T4_11 0x4e0811a1
00117 #define T4_12 0xf7537e82
00118 #define T4_13 0xbd3af235
00119 #define T4_14 0x2ad7d2bb
00120 #define T4_15 0xeb86d391
00121 
00122 #define R1B0  0
00123 #define R1B1  1
00124 #define R1B2  2
00125 #define R1B3  3
00126 #define R1B4  4
00127 #define R1B5  5
00128 #define R1B6  6
00129 #define R1B7  7
00130 #define R1B8  8
00131 #define R1B9  9
00132 #define R1B10 10
00133 #define R1B11 11
00134 #define R1B12 12
00135 #define R1B13 13
00136 #define R1B14 14
00137 #define R1B15 15
00138 
00139 #define R2B0  1
00140 #define R2B1  6
00141 #define R2B2  11
00142 #define R2B3  0
00143 #define R2B4  5
00144 #define R2B5  10
00145 #define R2B6  15
00146 #define R2B7  4
00147 #define R2B8  9
00148 #define R2B9  14
00149 #define R2B10 3 
00150 #define R2B11 8 
00151 #define R2B12 13
00152 #define R2B13 2 
00153 #define R2B14 7 
00154 #define R2B15 12
00155 
00156 #define R3B0  5
00157 #define R3B1  8
00158 #define R3B2  11
00159 #define R3B3  14
00160 #define R3B4  1
00161 #define R3B5  4
00162 #define R3B6  7
00163 #define R3B7  10
00164 #define R3B8  13
00165 #define R3B9  0
00166 #define R3B10 3 
00167 #define R3B11 6 
00168 #define R3B12 9 
00169 #define R3B13 12
00170 #define R3B14 15
00171 #define R3B15 2 
00172 
00173 #define R4B0  0
00174 #define R4B1  7
00175 #define R4B2  14
00176 #define R4B3  5
00177 #define R4B4  12
00178 #define R4B5  3
00179 #define R4B6  10
00180 #define R4B7  1
00181 #define R4B8  8
00182 #define R4B9  15
00183 #define R4B10 6 
00184 #define R4B11 13
00185 #define R4B12 4 
00186 #define R4B13 11
00187 #define R4B14 2 
00188 #define R4B15 9 
00189 
00190 #define S1_0 7
00191 #define S1_1 12
00192 #define S1_2 17
00193 #define S1_3 22
00194 
00195 #define S2_0 5
00196 #define S2_1 9
00197 #define S2_2 14
00198 #define S2_3 20
00199 
00200 #define S3_0 4
00201 #define S3_1 11
00202 #define S3_2 16
00203 #define S3_3 23
00204 
00205 #define S4_0 6
00206 #define S4_1 10
00207 #define S4_2 15
00208 #define S4_3 21
00209 
00210 struct MD5ContextStr {
00211        PRUint32      lsbInput;
00212        PRUint32      msbInput;
00213        PRUint32      cv[4];
00214        union {
00215               PRUint8 b[64];
00216               PRUint32 w[16];
00217        } u;
00218 };
00219 
00220 #define inBuf u.b
00221 
00222 SECStatus 
00223 MD5_Hash(unsigned char *dest, const char *src)
00224 {
00225        return MD5_HashBuf(dest, (unsigned char *)src, PL_strlen(src));
00226 }
00227 
00228 SECStatus 
00229 MD5_HashBuf(unsigned char *dest, const unsigned char *src, uint32 src_length)
00230 {
00231        unsigned int len;
00232        MD5Context cx;
00233 
00234        MD5_Begin(&cx);
00235        MD5_Update(&cx, src, src_length);
00236        MD5_End(&cx, dest, &len, MD5_HASH_LEN);
00237 /*     memset(&cx, 0, sizeof cx); */
00238        return SECSuccess;
00239 }
00240 
00241 MD5Context *
00242 MD5_NewContext(void)
00243 {
00244        /* no need to ZAlloc, MD5_Begin will init the context */
00245        MD5Context *cx = (MD5Context *)PORT_Alloc(sizeof(MD5Context));
00246        if (cx == NULL) {
00247               PORT_SetError(PR_OUT_OF_MEMORY_ERROR);
00248               return NULL;
00249        }
00250        return cx;
00251 }
00252 
00253 void 
00254 MD5_DestroyContext(MD5Context *cx, PRBool freeit)
00255 {
00256 /*     memset(cx, 0, sizeof *cx); */
00257        if (freeit) {
00258            PORT_Free(cx);
00259        }
00260 }
00261 
00262 void 
00263 MD5_Begin(MD5Context *cx)
00264 {
00265        cx->lsbInput = 0;
00266        cx->msbInput = 0;
00267 /*     memset(cx->inBuf, 0, sizeof(cx->inBuf)); */
00268        cx->cv[0] = CV0_1;
00269        cx->cv[1] = CV0_2;
00270        cx->cv[2] = CV0_3;
00271        cx->cv[3] = CV0_4;
00272 }
00273 
00274 #define cls(i32, s) (tmp = i32, tmp << s | tmp >> (32 - s))
00275 
00276 #if defined(SOLARIS) || defined(HPUX)
00277 #define addto64(sumhigh, sumlow, addend) \
00278        sumlow += addend; sumhigh += (sumlow < addend);
00279 #else
00280 #define addto64(sumhigh, sumlow, addend) \
00281        sumlow += addend; if (sumlow < addend) ++sumhigh;
00282 #endif
00283 
00284 #define MASK 0x00ff00ff
00285 #ifdef IS_LITTLE_ENDIAN
00286 #define lendian(i32) \
00287        (i32)
00288 #else
00289 #define lendian(i32) \
00290        (tmp = i32 >> 16 | i32 << 16, (tmp & MASK) << 8 | tmp >> 8 & MASK)
00291 #endif
00292 
00293 #ifndef IS_LITTLE_ENDIAN
00294 
00295 #define lebytes(b4) \
00296        ((b4)[3] << 24 | (b4)[2] << 16 | (b4)[1] << 8 | (b4)[0])
00297 
00298 static void
00299 md5_prep_state_le(MD5Context *cx)
00300 {
00301        PRUint32 tmp;
00302        cx->u.w[0] = lendian(cx->u.w[0]);
00303        cx->u.w[1] = lendian(cx->u.w[1]);
00304        cx->u.w[2] = lendian(cx->u.w[2]);
00305        cx->u.w[3] = lendian(cx->u.w[3]);
00306        cx->u.w[4] = lendian(cx->u.w[4]);
00307        cx->u.w[5] = lendian(cx->u.w[5]);
00308        cx->u.w[6] = lendian(cx->u.w[6]);
00309        cx->u.w[7] = lendian(cx->u.w[7]);
00310        cx->u.w[8] = lendian(cx->u.w[8]);
00311        cx->u.w[9] = lendian(cx->u.w[9]);
00312        cx->u.w[10] = lendian(cx->u.w[10]);
00313        cx->u.w[11] = lendian(cx->u.w[11]);
00314        cx->u.w[12] = lendian(cx->u.w[12]);
00315        cx->u.w[13] = lendian(cx->u.w[13]);
00316        cx->u.w[14] = lendian(cx->u.w[14]);
00317        cx->u.w[15] = lendian(cx->u.w[15]);
00318 }
00319 
00320 static void
00321 md5_prep_buffer_le(MD5Context *cx, const PRUint8 *beBuf)
00322 {
00323        cx->u.w[0] = lebytes(&beBuf[0]);
00324        cx->u.w[1] = lebytes(&beBuf[4]);
00325        cx->u.w[2] = lebytes(&beBuf[8]);
00326        cx->u.w[3] = lebytes(&beBuf[12]);
00327        cx->u.w[4] = lebytes(&beBuf[16]);
00328        cx->u.w[5] = lebytes(&beBuf[20]);
00329        cx->u.w[6] = lebytes(&beBuf[24]);
00330        cx->u.w[7] = lebytes(&beBuf[28]);
00331        cx->u.w[8] = lebytes(&beBuf[32]);
00332        cx->u.w[9] = lebytes(&beBuf[36]);
00333        cx->u.w[10] = lebytes(&beBuf[40]);
00334        cx->u.w[11] = lebytes(&beBuf[44]);
00335        cx->u.w[12] = lebytes(&beBuf[48]);
00336        cx->u.w[13] = lebytes(&beBuf[52]);
00337        cx->u.w[14] = lebytes(&beBuf[56]);
00338        cx->u.w[15] = lebytes(&beBuf[60]);
00339 }
00340 #endif
00341 
00342 
00343 #define F(X, Y, Z) \
00344        ((X & Y) | ((~X) & Z))
00345 
00346 #define G(X, Y, Z) \
00347        ((X & Z) | (Y & (~Z)))
00348 
00349 #define H(X, Y, Z) \
00350        (X ^ Y ^ Z)
00351 
00352 #define I(X, Y, Z) \
00353        (Y ^ (X | (~Z)))
00354 
00355 #define FF(a, b, c, d, bufint, s, ti) \
00356        a = b + cls(a + F(b, c, d) + bufint + ti, s)
00357 
00358 #define GG(a, b, c, d, bufint, s, ti) \
00359        a = b + cls(a + G(b, c, d) + bufint + ti, s)
00360 
00361 #define HH(a, b, c, d, bufint, s, ti) \
00362        a = b + cls(a + H(b, c, d) + bufint + ti, s)
00363 
00364 #define II(a, b, c, d, bufint, s, ti) \
00365        a = b + cls(a + I(b, c, d) + bufint + ti, s)
00366 
00367 static void
00368 md5_compress(MD5Context *cx, const PRUint32 *wBuf)
00369 {
00370        PRUint32 a, b, c, d;
00371        PRUint32 tmp;
00372        a = cx->cv[0];
00373        b = cx->cv[1];
00374        c = cx->cv[2];
00375        d = cx->cv[3];
00376        FF(a, b, c, d, wBuf[R1B0 ], S1_0, T1_0);
00377        FF(d, a, b, c, wBuf[R1B1 ], S1_1, T1_1);
00378        FF(c, d, a, b, wBuf[R1B2 ], S1_2, T1_2);
00379        FF(b, c, d, a, wBuf[R1B3 ], S1_3, T1_3);
00380        FF(a, b, c, d, wBuf[R1B4 ], S1_0, T1_4);
00381        FF(d, a, b, c, wBuf[R1B5 ], S1_1, T1_5);
00382        FF(c, d, a, b, wBuf[R1B6 ], S1_2, T1_6);
00383        FF(b, c, d, a, wBuf[R1B7 ], S1_3, T1_7);
00384        FF(a, b, c, d, wBuf[R1B8 ], S1_0, T1_8);
00385        FF(d, a, b, c, wBuf[R1B9 ], S1_1, T1_9);
00386        FF(c, d, a, b, wBuf[R1B10], S1_2, T1_10);
00387        FF(b, c, d, a, wBuf[R1B11], S1_3, T1_11);
00388        FF(a, b, c, d, wBuf[R1B12], S1_0, T1_12);
00389        FF(d, a, b, c, wBuf[R1B13], S1_1, T1_13);
00390        FF(c, d, a, b, wBuf[R1B14], S1_2, T1_14);
00391        FF(b, c, d, a, wBuf[R1B15], S1_3, T1_15);
00392        GG(a, b, c, d, wBuf[R2B0 ], S2_0, T2_0);
00393        GG(d, a, b, c, wBuf[R2B1 ], S2_1, T2_1);
00394        GG(c, d, a, b, wBuf[R2B2 ], S2_2, T2_2);
00395        GG(b, c, d, a, wBuf[R2B3 ], S2_3, T2_3);
00396        GG(a, b, c, d, wBuf[R2B4 ], S2_0, T2_4);
00397        GG(d, a, b, c, wBuf[R2B5 ], S2_1, T2_5);
00398        GG(c, d, a, b, wBuf[R2B6 ], S2_2, T2_6);
00399        GG(b, c, d, a, wBuf[R2B7 ], S2_3, T2_7);
00400        GG(a, b, c, d, wBuf[R2B8 ], S2_0, T2_8);
00401        GG(d, a, b, c, wBuf[R2B9 ], S2_1, T2_9);
00402        GG(c, d, a, b, wBuf[R2B10], S2_2, T2_10);
00403        GG(b, c, d, a, wBuf[R2B11], S2_3, T2_11);
00404        GG(a, b, c, d, wBuf[R2B12], S2_0, T2_12);
00405        GG(d, a, b, c, wBuf[R2B13], S2_1, T2_13);
00406        GG(c, d, a, b, wBuf[R2B14], S2_2, T2_14);
00407        GG(b, c, d, a, wBuf[R2B15], S2_3, T2_15);
00408        HH(a, b, c, d, wBuf[R3B0 ], S3_0, T3_0);
00409        HH(d, a, b, c, wBuf[R3B1 ], S3_1, T3_1);
00410        HH(c, d, a, b, wBuf[R3B2 ], S3_2, T3_2);
00411        HH(b, c, d, a, wBuf[R3B3 ], S3_3, T3_3);
00412        HH(a, b, c, d, wBuf[R3B4 ], S3_0, T3_4);
00413        HH(d, a, b, c, wBuf[R3B5 ], S3_1, T3_5);
00414        HH(c, d, a, b, wBuf[R3B6 ], S3_2, T3_6);
00415        HH(b, c, d, a, wBuf[R3B7 ], S3_3, T3_7);
00416        HH(a, b, c, d, wBuf[R3B8 ], S3_0, T3_8);
00417        HH(d, a, b, c, wBuf[R3B9 ], S3_1, T3_9);
00418        HH(c, d, a, b, wBuf[R3B10], S3_2, T3_10);
00419        HH(b, c, d, a, wBuf[R3B11], S3_3, T3_11);
00420        HH(a, b, c, d, wBuf[R3B12], S3_0, T3_12);
00421        HH(d, a, b, c, wBuf[R3B13], S3_1, T3_13);
00422        HH(c, d, a, b, wBuf[R3B14], S3_2, T3_14);
00423        HH(b, c, d, a, wBuf[R3B15], S3_3, T3_15);
00424        II(a, b, c, d, wBuf[R4B0 ], S4_0, T4_0);
00425        II(d, a, b, c, wBuf[R4B1 ], S4_1, T4_1);
00426        II(c, d, a, b, wBuf[R4B2 ], S4_2, T4_2);
00427        II(b, c, d, a, wBuf[R4B3 ], S4_3, T4_3);
00428        II(a, b, c, d, wBuf[R4B4 ], S4_0, T4_4);
00429        II(d, a, b, c, wBuf[R4B5 ], S4_1, T4_5);
00430        II(c, d, a, b, wBuf[R4B6 ], S4_2, T4_6);
00431        II(b, c, d, a, wBuf[R4B7 ], S4_3, T4_7);
00432        II(a, b, c, d, wBuf[R4B8 ], S4_0, T4_8);
00433        II(d, a, b, c, wBuf[R4B9 ], S4_1, T4_9);
00434        II(c, d, a, b, wBuf[R4B10], S4_2, T4_10);
00435        II(b, c, d, a, wBuf[R4B11], S4_3, T4_11);
00436        II(a, b, c, d, wBuf[R4B12], S4_0, T4_12);
00437        II(d, a, b, c, wBuf[R4B13], S4_1, T4_13);
00438        II(c, d, a, b, wBuf[R4B14], S4_2, T4_14);
00439        II(b, c, d, a, wBuf[R4B15], S4_3, T4_15);
00440        cx->cv[0] += a;
00441        cx->cv[1] += b;
00442        cx->cv[2] += c;
00443        cx->cv[3] += d;
00444 }
00445 
00446 void 
00447 MD5_Update(MD5Context *cx, const unsigned char *input, unsigned int inputLen)
00448 {
00449        PRUint32 bytesToConsume;
00450        PRUint32 inBufIndex = cx->lsbInput & 63;
00451        const PRUint32 *wBuf;
00452 
00453        /* Add the number of input bytes to the 64-bit input counter. */
00454        addto64(cx->msbInput, cx->lsbInput, inputLen);
00455        if (inBufIndex) {
00456               /* There is already data in the buffer.  Fill with input. */
00457               bytesToConsume = PR_MIN(inputLen, MD5_BUFFER_SIZE - inBufIndex);
00458               memcpy(&cx->inBuf[inBufIndex], input, bytesToConsume);
00459               if (inBufIndex + bytesToConsume >= MD5_BUFFER_SIZE) {
00460                      /* The buffer is filled.  Run the compression function. */
00461 #ifndef IS_LITTLE_ENDIAN
00462                      md5_prep_state_le(cx);
00463 #endif
00464                      md5_compress(cx, cx->u.w);
00465               }
00466               /* Remaining input. */
00467               inputLen -= bytesToConsume;
00468               input += bytesToConsume;
00469        }
00470 
00471        /* Iterate over 64-byte chunks of the message. */
00472        while (inputLen >= MD5_BUFFER_SIZE) {
00473 #ifdef IS_LITTLE_ENDIAN
00474 #ifdef _X86_
00475               /* x86 can handle arithmetic on non-word-aligned buffers */
00476               wBuf = (PRUint32 *)input;
00477 #else
00478               if ((ptrdiff_t)input & 0x3) {
00479                      /* buffer not aligned, copy it to force alignment */
00480                      memcpy(cx->inBuf, input, MD5_BUFFER_SIZE);
00481                      wBuf = cx->u.w;
00482               } else {
00483                      /* buffer is aligned */
00484                      wBuf = (PRUint32 *)input;
00485               }
00486 #endif
00487 #else
00488               md5_prep_buffer_le(cx, input);
00489               wBuf = cx->u.w;
00490 #endif
00491               md5_compress(cx, wBuf);
00492               inputLen -= MD5_BUFFER_SIZE;
00493               input += MD5_BUFFER_SIZE;
00494        }
00495 
00496        /* Tail of message (message bytes mod 64). */
00497        if (inputLen)
00498               memcpy(cx->inBuf, input, inputLen);
00499 }
00500 
00501 static const unsigned char padbytes[] = {
00502        0x80, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00503        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00504        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00505        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00506        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00507        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00508        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00509        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00510        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00511        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00512        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
00513        0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00
00514 };
00515 
00516 void 
00517 MD5_End(MD5Context *cx, unsigned char *digest,
00518         unsigned int *digestLen, unsigned int maxDigestLen)
00519 {
00520 #ifndef IS_LITTLE_ENDIAN
00521        PRUint32 tmp;
00522 #endif
00523        PRUint32 lowInput, highInput;
00524        PRUint32 inBufIndex = cx->lsbInput & 63;
00525 
00526        if (maxDigestLen < MD5_HASH_LEN) {
00527               PORT_SetError(SEC_ERROR_INVALID_ARGS);
00528               return;
00529        }
00530 
00531        /* Copy out the length of bits input before padding. */
00532        lowInput = cx->lsbInput; 
00533        highInput = (cx->msbInput << 3) | (lowInput >> 29);
00534        lowInput <<= 3;
00535 
00536        if (inBufIndex < MD5_END_BUFFER) {
00537               MD5_Update(cx, padbytes, MD5_END_BUFFER - inBufIndex);
00538        } else {
00539               MD5_Update(cx, padbytes, 
00540                          MD5_END_BUFFER + MD5_BUFFER_SIZE - inBufIndex);
00541        }
00542 
00543        /* Store the number of bytes input (before padding) in final 64 bits. */
00544        cx->u.w[14] = lendian(lowInput);
00545        cx->u.w[15] = lendian(highInput);
00546 
00547        /* Final call to compress. */
00548 #ifndef IS_LITTLE_ENDIAN
00549        md5_prep_state_le(cx);
00550 #endif
00551        md5_compress(cx, cx->u.w);
00552 
00553        /* Copy the resulting values out of the chain variables into return buf. */
00554        *digestLen = MD5_HASH_LEN;
00555 #ifndef IS_LITTLE_ENDIAN
00556        cx->cv[0] = lendian(cx->cv[0]);
00557        cx->cv[1] = lendian(cx->cv[1]);
00558        cx->cv[2] = lendian(cx->cv[2]);
00559        cx->cv[3] = lendian(cx->cv[3]);
00560 #endif
00561        memcpy(digest, cx->cv, MD5_HASH_LEN);
00562 }
00563 
00564 unsigned int 
00565 MD5_FlattenSize(MD5Context *cx)
00566 {
00567        return sizeof(*cx);
00568 }
00569 
00570 SECStatus 
00571 MD5_Flatten(MD5Context *cx, unsigned char *space)
00572 {
00573        memcpy(space, cx, sizeof(*cx));
00574        return SECSuccess;
00575 }
00576 
00577 MD5Context * 
00578 MD5_Resurrect(unsigned char *space, void *arg)
00579 {
00580        MD5Context *cx = MD5_NewContext();
00581        if (cx)
00582               memcpy(cx, space, sizeof(*cx));
00583        return cx;
00584 }
00585 
00586 void MD5_Clone(MD5Context *dest, MD5Context *src) 
00587 {
00588        memcpy(dest, src, sizeof *dest);
00589 }
00590 
00591 void 
00592 MD5_TraceState(MD5Context *cx)
00593 {
00594        PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
00595 }