andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifdef FREEBL_NO_DEPEND andre@0: #include "stubs.h" andre@0: #endif andre@0: andre@0: #include "prerr.h" andre@0: #include "secerr.h" andre@0: andre@0: #include "prtypes.h" andre@0: #include "prlong.h" andre@0: andre@0: #include "blapi.h" andre@0: andre@0: #define MD5_HASH_LEN 16 andre@0: #define MD5_BUFFER_SIZE 64 andre@0: #define MD5_END_BUFFER (MD5_BUFFER_SIZE - 8) andre@0: andre@0: #define CV0_1 0x67452301 andre@0: #define CV0_2 0xefcdab89 andre@0: #define CV0_3 0x98badcfe andre@0: #define CV0_4 0x10325476 andre@0: andre@0: #define T1_0 0xd76aa478 andre@0: #define T1_1 0xe8c7b756 andre@0: #define T1_2 0x242070db andre@0: #define T1_3 0xc1bdceee andre@0: #define T1_4 0xf57c0faf andre@0: #define T1_5 0x4787c62a andre@0: #define T1_6 0xa8304613 andre@0: #define T1_7 0xfd469501 andre@0: #define T1_8 0x698098d8 andre@0: #define T1_9 0x8b44f7af andre@0: #define T1_10 0xffff5bb1 andre@0: #define T1_11 0x895cd7be andre@0: #define T1_12 0x6b901122 andre@0: #define T1_13 0xfd987193 andre@0: #define T1_14 0xa679438e andre@0: #define T1_15 0x49b40821 andre@0: andre@0: #define T2_0 0xf61e2562 andre@0: #define T2_1 0xc040b340 andre@0: #define T2_2 0x265e5a51 andre@0: #define T2_3 0xe9b6c7aa andre@0: #define T2_4 0xd62f105d andre@0: #define T2_5 0x02441453 andre@0: #define T2_6 0xd8a1e681 andre@0: #define T2_7 0xe7d3fbc8 andre@0: #define T2_8 0x21e1cde6 andre@0: #define T2_9 0xc33707d6 andre@0: #define T2_10 0xf4d50d87 andre@0: #define T2_11 0x455a14ed andre@0: #define T2_12 0xa9e3e905 andre@0: #define T2_13 0xfcefa3f8 andre@0: #define T2_14 0x676f02d9 andre@0: #define T2_15 0x8d2a4c8a andre@0: andre@0: #define T3_0 0xfffa3942 andre@0: #define T3_1 0x8771f681 andre@0: #define T3_2 0x6d9d6122 andre@0: #define T3_3 0xfde5380c andre@0: #define T3_4 0xa4beea44 andre@0: #define T3_5 0x4bdecfa9 andre@0: #define T3_6 0xf6bb4b60 andre@0: #define T3_7 0xbebfbc70 andre@0: #define T3_8 0x289b7ec6 andre@0: #define T3_9 0xeaa127fa andre@0: #define T3_10 0xd4ef3085 andre@0: #define T3_11 0x04881d05 andre@0: #define T3_12 0xd9d4d039 andre@0: #define T3_13 0xe6db99e5 andre@0: #define T3_14 0x1fa27cf8 andre@0: #define T3_15 0xc4ac5665 andre@0: andre@0: #define T4_0 0xf4292244 andre@0: #define T4_1 0x432aff97 andre@0: #define T4_2 0xab9423a7 andre@0: #define T4_3 0xfc93a039 andre@0: #define T4_4 0x655b59c3 andre@0: #define T4_5 0x8f0ccc92 andre@0: #define T4_6 0xffeff47d andre@0: #define T4_7 0x85845dd1 andre@0: #define T4_8 0x6fa87e4f andre@0: #define T4_9 0xfe2ce6e0 andre@0: #define T4_10 0xa3014314 andre@0: #define T4_11 0x4e0811a1 andre@0: #define T4_12 0xf7537e82 andre@0: #define T4_13 0xbd3af235 andre@0: #define T4_14 0x2ad7d2bb andre@0: #define T4_15 0xeb86d391 andre@0: andre@0: #define R1B0 0 andre@0: #define R1B1 1 andre@0: #define R1B2 2 andre@0: #define R1B3 3 andre@0: #define R1B4 4 andre@0: #define R1B5 5 andre@0: #define R1B6 6 andre@0: #define R1B7 7 andre@0: #define R1B8 8 andre@0: #define R1B9 9 andre@0: #define R1B10 10 andre@0: #define R1B11 11 andre@0: #define R1B12 12 andre@0: #define R1B13 13 andre@0: #define R1B14 14 andre@0: #define R1B15 15 andre@0: andre@0: #define R2B0 1 andre@0: #define R2B1 6 andre@0: #define R2B2 11 andre@0: #define R2B3 0 andre@0: #define R2B4 5 andre@0: #define R2B5 10 andre@0: #define R2B6 15 andre@0: #define R2B7 4 andre@0: #define R2B8 9 andre@0: #define R2B9 14 andre@0: #define R2B10 3 andre@0: #define R2B11 8 andre@0: #define R2B12 13 andre@0: #define R2B13 2 andre@0: #define R2B14 7 andre@0: #define R2B15 12 andre@0: andre@0: #define R3B0 5 andre@0: #define R3B1 8 andre@0: #define R3B2 11 andre@0: #define R3B3 14 andre@0: #define R3B4 1 andre@0: #define R3B5 4 andre@0: #define R3B6 7 andre@0: #define R3B7 10 andre@0: #define R3B8 13 andre@0: #define R3B9 0 andre@0: #define R3B10 3 andre@0: #define R3B11 6 andre@0: #define R3B12 9 andre@0: #define R3B13 12 andre@0: #define R3B14 15 andre@0: #define R3B15 2 andre@0: andre@0: #define R4B0 0 andre@0: #define R4B1 7 andre@0: #define R4B2 14 andre@0: #define R4B3 5 andre@0: #define R4B4 12 andre@0: #define R4B5 3 andre@0: #define R4B6 10 andre@0: #define R4B7 1 andre@0: #define R4B8 8 andre@0: #define R4B9 15 andre@0: #define R4B10 6 andre@0: #define R4B11 13 andre@0: #define R4B12 4 andre@0: #define R4B13 11 andre@0: #define R4B14 2 andre@0: #define R4B15 9 andre@0: andre@0: #define S1_0 7 andre@0: #define S1_1 12 andre@0: #define S1_2 17 andre@0: #define S1_3 22 andre@0: andre@0: #define S2_0 5 andre@0: #define S2_1 9 andre@0: #define S2_2 14 andre@0: #define S2_3 20 andre@0: andre@0: #define S3_0 4 andre@0: #define S3_1 11 andre@0: #define S3_2 16 andre@0: #define S3_3 23 andre@0: andre@0: #define S4_0 6 andre@0: #define S4_1 10 andre@0: #define S4_2 15 andre@0: #define S4_3 21 andre@0: andre@0: struct MD5ContextStr { andre@0: PRUint32 lsbInput; andre@0: PRUint32 msbInput; andre@0: PRUint32 cv[4]; andre@0: union { andre@0: PRUint8 b[64]; andre@0: PRUint32 w[16]; andre@0: } u; andre@0: }; andre@0: andre@0: #define inBuf u.b andre@0: andre@0: SECStatus andre@0: MD5_Hash(unsigned char *dest, const char *src) andre@0: { andre@0: return MD5_HashBuf(dest, (const unsigned char *)src, PORT_Strlen(src)); andre@0: } andre@0: andre@0: SECStatus andre@0: MD5_HashBuf(unsigned char *dest, const unsigned char *src, PRUint32 src_length) andre@0: { andre@0: unsigned int len; andre@0: MD5Context cx; andre@0: andre@0: MD5_Begin(&cx); andre@0: MD5_Update(&cx, src, src_length); andre@0: MD5_End(&cx, dest, &len, MD5_HASH_LEN); andre@0: memset(&cx, 0, sizeof cx); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: MD5Context * andre@0: MD5_NewContext(void) andre@0: { andre@0: /* no need to ZAlloc, MD5_Begin will init the context */ andre@0: MD5Context *cx = (MD5Context *)PORT_Alloc(sizeof(MD5Context)); andre@0: if (cx == NULL) { andre@0: PORT_SetError(PR_OUT_OF_MEMORY_ERROR); andre@0: return NULL; andre@0: } andre@0: return cx; andre@0: } andre@0: andre@0: void andre@0: MD5_DestroyContext(MD5Context *cx, PRBool freeit) andre@0: { andre@0: memset(cx, 0, sizeof *cx); andre@0: if (freeit) { andre@0: PORT_Free(cx); andre@0: } andre@0: } andre@0: andre@0: void andre@0: MD5_Begin(MD5Context *cx) andre@0: { andre@0: cx->lsbInput = 0; andre@0: cx->msbInput = 0; andre@0: /* memset(cx->inBuf, 0, sizeof(cx->inBuf)); */ andre@0: cx->cv[0] = CV0_1; andre@0: cx->cv[1] = CV0_2; andre@0: cx->cv[2] = CV0_3; andre@0: cx->cv[3] = CV0_4; andre@0: } andre@0: andre@0: #define cls(i32, s) (tmp = i32, tmp << s | tmp >> (32 - s)) andre@0: andre@0: #if defined(SOLARIS) || defined(HPUX) andre@0: #define addto64(sumhigh, sumlow, addend) \ andre@0: sumlow += addend; sumhigh += (sumlow < addend); andre@0: #else andre@0: #define addto64(sumhigh, sumlow, addend) \ andre@0: sumlow += addend; if (sumlow < addend) ++sumhigh; andre@0: #endif andre@0: andre@0: #define MASK 0x00ff00ff andre@0: #ifdef IS_LITTLE_ENDIAN andre@0: #define lendian(i32) \ andre@0: (i32) andre@0: #else andre@0: #define lendian(i32) \ andre@0: (tmp = i32 >> 16 | i32 << 16, (tmp & MASK) << 8 | tmp >> 8 & MASK) andre@0: #endif andre@0: andre@0: #ifndef IS_LITTLE_ENDIAN andre@0: andre@0: #define lebytes(b4) \ andre@0: ((b4)[3] << 24 | (b4)[2] << 16 | (b4)[1] << 8 | (b4)[0]) andre@0: andre@0: static void andre@0: md5_prep_state_le(MD5Context *cx) andre@0: { andre@0: PRUint32 tmp; andre@0: cx->u.w[0] = lendian(cx->u.w[0]); andre@0: cx->u.w[1] = lendian(cx->u.w[1]); andre@0: cx->u.w[2] = lendian(cx->u.w[2]); andre@0: cx->u.w[3] = lendian(cx->u.w[3]); andre@0: cx->u.w[4] = lendian(cx->u.w[4]); andre@0: cx->u.w[5] = lendian(cx->u.w[5]); andre@0: cx->u.w[6] = lendian(cx->u.w[6]); andre@0: cx->u.w[7] = lendian(cx->u.w[7]); andre@0: cx->u.w[8] = lendian(cx->u.w[8]); andre@0: cx->u.w[9] = lendian(cx->u.w[9]); andre@0: cx->u.w[10] = lendian(cx->u.w[10]); andre@0: cx->u.w[11] = lendian(cx->u.w[11]); andre@0: cx->u.w[12] = lendian(cx->u.w[12]); andre@0: cx->u.w[13] = lendian(cx->u.w[13]); andre@0: cx->u.w[14] = lendian(cx->u.w[14]); andre@0: cx->u.w[15] = lendian(cx->u.w[15]); andre@0: } andre@0: andre@0: static void andre@0: md5_prep_buffer_le(MD5Context *cx, const PRUint8 *beBuf) andre@0: { andre@0: cx->u.w[0] = lebytes(&beBuf[0]); andre@0: cx->u.w[1] = lebytes(&beBuf[4]); andre@0: cx->u.w[2] = lebytes(&beBuf[8]); andre@0: cx->u.w[3] = lebytes(&beBuf[12]); andre@0: cx->u.w[4] = lebytes(&beBuf[16]); andre@0: cx->u.w[5] = lebytes(&beBuf[20]); andre@0: cx->u.w[6] = lebytes(&beBuf[24]); andre@0: cx->u.w[7] = lebytes(&beBuf[28]); andre@0: cx->u.w[8] = lebytes(&beBuf[32]); andre@0: cx->u.w[9] = lebytes(&beBuf[36]); andre@0: cx->u.w[10] = lebytes(&beBuf[40]); andre@0: cx->u.w[11] = lebytes(&beBuf[44]); andre@0: cx->u.w[12] = lebytes(&beBuf[48]); andre@0: cx->u.w[13] = lebytes(&beBuf[52]); andre@0: cx->u.w[14] = lebytes(&beBuf[56]); andre@0: cx->u.w[15] = lebytes(&beBuf[60]); andre@0: } andre@0: #endif andre@0: andre@0: andre@0: #define F(X, Y, Z) \ andre@0: ((X & Y) | ((~X) & Z)) andre@0: andre@0: #define G(X, Y, Z) \ andre@0: ((X & Z) | (Y & (~Z))) andre@0: andre@0: #define H(X, Y, Z) \ andre@0: (X ^ Y ^ Z) andre@0: andre@0: #define I(X, Y, Z) \ andre@0: (Y ^ (X | (~Z))) andre@0: andre@0: #define FF(a, b, c, d, bufint, s, ti) \ andre@0: a = b + cls(a + F(b, c, d) + bufint + ti, s) andre@0: andre@0: #define GG(a, b, c, d, bufint, s, ti) \ andre@0: a = b + cls(a + G(b, c, d) + bufint + ti, s) andre@0: andre@0: #define HH(a, b, c, d, bufint, s, ti) \ andre@0: a = b + cls(a + H(b, c, d) + bufint + ti, s) andre@0: andre@0: #define II(a, b, c, d, bufint, s, ti) \ andre@0: a = b + cls(a + I(b, c, d) + bufint + ti, s) andre@0: andre@0: static void andre@0: md5_compress(MD5Context *cx, const PRUint32 *wBuf) andre@0: { andre@0: PRUint32 a, b, c, d; andre@0: PRUint32 tmp; andre@0: a = cx->cv[0]; andre@0: b = cx->cv[1]; andre@0: c = cx->cv[2]; andre@0: d = cx->cv[3]; andre@0: FF(a, b, c, d, wBuf[R1B0 ], S1_0, T1_0); andre@0: FF(d, a, b, c, wBuf[R1B1 ], S1_1, T1_1); andre@0: FF(c, d, a, b, wBuf[R1B2 ], S1_2, T1_2); andre@0: FF(b, c, d, a, wBuf[R1B3 ], S1_3, T1_3); andre@0: FF(a, b, c, d, wBuf[R1B4 ], S1_0, T1_4); andre@0: FF(d, a, b, c, wBuf[R1B5 ], S1_1, T1_5); andre@0: FF(c, d, a, b, wBuf[R1B6 ], S1_2, T1_6); andre@0: FF(b, c, d, a, wBuf[R1B7 ], S1_3, T1_7); andre@0: FF(a, b, c, d, wBuf[R1B8 ], S1_0, T1_8); andre@0: FF(d, a, b, c, wBuf[R1B9 ], S1_1, T1_9); andre@0: FF(c, d, a, b, wBuf[R1B10], S1_2, T1_10); andre@0: FF(b, c, d, a, wBuf[R1B11], S1_3, T1_11); andre@0: FF(a, b, c, d, wBuf[R1B12], S1_0, T1_12); andre@0: FF(d, a, b, c, wBuf[R1B13], S1_1, T1_13); andre@0: FF(c, d, a, b, wBuf[R1B14], S1_2, T1_14); andre@0: FF(b, c, d, a, wBuf[R1B15], S1_3, T1_15); andre@0: GG(a, b, c, d, wBuf[R2B0 ], S2_0, T2_0); andre@0: GG(d, a, b, c, wBuf[R2B1 ], S2_1, T2_1); andre@0: GG(c, d, a, b, wBuf[R2B2 ], S2_2, T2_2); andre@0: GG(b, c, d, a, wBuf[R2B3 ], S2_3, T2_3); andre@0: GG(a, b, c, d, wBuf[R2B4 ], S2_0, T2_4); andre@0: GG(d, a, b, c, wBuf[R2B5 ], S2_1, T2_5); andre@0: GG(c, d, a, b, wBuf[R2B6 ], S2_2, T2_6); andre@0: GG(b, c, d, a, wBuf[R2B7 ], S2_3, T2_7); andre@0: GG(a, b, c, d, wBuf[R2B8 ], S2_0, T2_8); andre@0: GG(d, a, b, c, wBuf[R2B9 ], S2_1, T2_9); andre@0: GG(c, d, a, b, wBuf[R2B10], S2_2, T2_10); andre@0: GG(b, c, d, a, wBuf[R2B11], S2_3, T2_11); andre@0: GG(a, b, c, d, wBuf[R2B12], S2_0, T2_12); andre@0: GG(d, a, b, c, wBuf[R2B13], S2_1, T2_13); andre@0: GG(c, d, a, b, wBuf[R2B14], S2_2, T2_14); andre@0: GG(b, c, d, a, wBuf[R2B15], S2_3, T2_15); andre@0: HH(a, b, c, d, wBuf[R3B0 ], S3_0, T3_0); andre@0: HH(d, a, b, c, wBuf[R3B1 ], S3_1, T3_1); andre@0: HH(c, d, a, b, wBuf[R3B2 ], S3_2, T3_2); andre@0: HH(b, c, d, a, wBuf[R3B3 ], S3_3, T3_3); andre@0: HH(a, b, c, d, wBuf[R3B4 ], S3_0, T3_4); andre@0: HH(d, a, b, c, wBuf[R3B5 ], S3_1, T3_5); andre@0: HH(c, d, a, b, wBuf[R3B6 ], S3_2, T3_6); andre@0: HH(b, c, d, a, wBuf[R3B7 ], S3_3, T3_7); andre@0: HH(a, b, c, d, wBuf[R3B8 ], S3_0, T3_8); andre@0: HH(d, a, b, c, wBuf[R3B9 ], S3_1, T3_9); andre@0: HH(c, d, a, b, wBuf[R3B10], S3_2, T3_10); andre@0: HH(b, c, d, a, wBuf[R3B11], S3_3, T3_11); andre@0: HH(a, b, c, d, wBuf[R3B12], S3_0, T3_12); andre@0: HH(d, a, b, c, wBuf[R3B13], S3_1, T3_13); andre@0: HH(c, d, a, b, wBuf[R3B14], S3_2, T3_14); andre@0: HH(b, c, d, a, wBuf[R3B15], S3_3, T3_15); andre@0: II(a, b, c, d, wBuf[R4B0 ], S4_0, T4_0); andre@0: II(d, a, b, c, wBuf[R4B1 ], S4_1, T4_1); andre@0: II(c, d, a, b, wBuf[R4B2 ], S4_2, T4_2); andre@0: II(b, c, d, a, wBuf[R4B3 ], S4_3, T4_3); andre@0: II(a, b, c, d, wBuf[R4B4 ], S4_0, T4_4); andre@0: II(d, a, b, c, wBuf[R4B5 ], S4_1, T4_5); andre@0: II(c, d, a, b, wBuf[R4B6 ], S4_2, T4_6); andre@0: II(b, c, d, a, wBuf[R4B7 ], S4_3, T4_7); andre@0: II(a, b, c, d, wBuf[R4B8 ], S4_0, T4_8); andre@0: II(d, a, b, c, wBuf[R4B9 ], S4_1, T4_9); andre@0: II(c, d, a, b, wBuf[R4B10], S4_2, T4_10); andre@0: II(b, c, d, a, wBuf[R4B11], S4_3, T4_11); andre@0: II(a, b, c, d, wBuf[R4B12], S4_0, T4_12); andre@0: II(d, a, b, c, wBuf[R4B13], S4_1, T4_13); andre@0: II(c, d, a, b, wBuf[R4B14], S4_2, T4_14); andre@0: II(b, c, d, a, wBuf[R4B15], S4_3, T4_15); andre@0: cx->cv[0] += a; andre@0: cx->cv[1] += b; andre@0: cx->cv[2] += c; andre@0: cx->cv[3] += d; andre@0: } andre@0: andre@0: void andre@0: MD5_Update(MD5Context *cx, const unsigned char *input, unsigned int inputLen) andre@0: { andre@0: PRUint32 bytesToConsume; andre@0: PRUint32 inBufIndex = cx->lsbInput & 63; andre@0: const PRUint32 *wBuf; andre@0: andre@0: /* Add the number of input bytes to the 64-bit input counter. */ andre@0: addto64(cx->msbInput, cx->lsbInput, inputLen); andre@0: if (inBufIndex) { andre@0: /* There is already data in the buffer. Fill with input. */ andre@0: bytesToConsume = PR_MIN(inputLen, MD5_BUFFER_SIZE - inBufIndex); andre@0: memcpy(&cx->inBuf[inBufIndex], input, bytesToConsume); andre@0: if (inBufIndex + bytesToConsume >= MD5_BUFFER_SIZE) { andre@0: /* The buffer is filled. Run the compression function. */ andre@0: #ifndef IS_LITTLE_ENDIAN andre@0: md5_prep_state_le(cx); andre@0: #endif andre@0: md5_compress(cx, cx->u.w); andre@0: } andre@0: /* Remaining input. */ andre@0: inputLen -= bytesToConsume; andre@0: input += bytesToConsume; andre@0: } andre@0: andre@0: /* Iterate over 64-byte chunks of the message. */ andre@0: while (inputLen >= MD5_BUFFER_SIZE) { andre@0: #ifdef IS_LITTLE_ENDIAN andre@0: #ifdef NSS_X86_OR_X64 andre@0: /* x86 can handle arithmetic on non-word-aligned buffers */ andre@0: wBuf = (PRUint32 *)input; andre@0: #else andre@0: if ((ptrdiff_t)input & 0x3) { andre@0: /* buffer not aligned, copy it to force alignment */ andre@0: memcpy(cx->inBuf, input, MD5_BUFFER_SIZE); andre@0: wBuf = cx->u.w; andre@0: } else { andre@0: /* buffer is aligned */ andre@0: wBuf = (PRUint32 *)input; andre@0: } andre@0: #endif andre@0: #else andre@0: md5_prep_buffer_le(cx, input); andre@0: wBuf = cx->u.w; andre@0: #endif andre@0: md5_compress(cx, wBuf); andre@0: inputLen -= MD5_BUFFER_SIZE; andre@0: input += MD5_BUFFER_SIZE; andre@0: } andre@0: andre@0: /* Tail of message (message bytes mod 64). */ andre@0: if (inputLen) andre@0: memcpy(cx->inBuf, input, inputLen); andre@0: } andre@0: andre@0: static const unsigned char padbytes[] = { andre@0: 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, andre@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 andre@0: }; andre@0: andre@0: void andre@0: MD5_End(MD5Context *cx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: #ifndef IS_LITTLE_ENDIAN andre@0: PRUint32 tmp; andre@0: #endif andre@0: PRUint32 lowInput, highInput; andre@0: PRUint32 inBufIndex = cx->lsbInput & 63; andre@0: andre@0: if (maxDigestLen < MD5_HASH_LEN) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return; andre@0: } andre@0: andre@0: /* Copy out the length of bits input before padding. */ andre@0: lowInput = cx->lsbInput; andre@0: highInput = (cx->msbInput << 3) | (lowInput >> 29); andre@0: lowInput <<= 3; andre@0: andre@0: if (inBufIndex < MD5_END_BUFFER) { andre@0: MD5_Update(cx, padbytes, MD5_END_BUFFER - inBufIndex); andre@0: } else { andre@0: MD5_Update(cx, padbytes, andre@0: MD5_END_BUFFER + MD5_BUFFER_SIZE - inBufIndex); andre@0: } andre@0: andre@0: /* Store the number of bytes input (before padding) in final 64 bits. */ andre@0: cx->u.w[14] = lendian(lowInput); andre@0: cx->u.w[15] = lendian(highInput); andre@0: andre@0: /* Final call to compress. */ andre@0: #ifndef IS_LITTLE_ENDIAN andre@0: md5_prep_state_le(cx); andre@0: #endif andre@0: md5_compress(cx, cx->u.w); andre@0: andre@0: /* Copy the resulting values out of the chain variables into return buf. */ andre@0: if (digestLen) andre@0: *digestLen = MD5_HASH_LEN; andre@0: #ifndef IS_LITTLE_ENDIAN andre@0: cx->cv[0] = lendian(cx->cv[0]); andre@0: cx->cv[1] = lendian(cx->cv[1]); andre@0: cx->cv[2] = lendian(cx->cv[2]); andre@0: cx->cv[3] = lendian(cx->cv[3]); andre@0: #endif andre@0: memcpy(digest, cx->cv, MD5_HASH_LEN); andre@0: } andre@0: andre@0: void andre@0: MD5_EndRaw(MD5Context *cx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: #ifndef IS_LITTLE_ENDIAN andre@0: PRUint32 tmp; andre@0: #endif andre@0: PRUint32 cv[4]; andre@0: andre@0: if (maxDigestLen < MD5_HASH_LEN) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return; andre@0: } andre@0: andre@0: memcpy(cv, cx->cv, sizeof(cv)); andre@0: #ifndef IS_LITTLE_ENDIAN andre@0: cv[0] = lendian(cv[0]); andre@0: cv[1] = lendian(cv[1]); andre@0: cv[2] = lendian(cv[2]); andre@0: cv[3] = lendian(cv[3]); andre@0: #endif andre@0: memcpy(digest, cv, MD5_HASH_LEN); andre@0: if (digestLen) andre@0: *digestLen = MD5_HASH_LEN; andre@0: } andre@0: andre@0: unsigned int andre@0: MD5_FlattenSize(MD5Context *cx) andre@0: { andre@0: return sizeof(*cx); andre@0: } andre@0: andre@0: SECStatus andre@0: MD5_Flatten(MD5Context *cx, unsigned char *space) andre@0: { andre@0: memcpy(space, cx, sizeof(*cx)); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: MD5Context * andre@0: MD5_Resurrect(unsigned char *space, void *arg) andre@0: { andre@0: MD5Context *cx = MD5_NewContext(); andre@0: if (cx) andre@0: memcpy(cx, space, sizeof(*cx)); andre@0: return cx; andre@0: } andre@0: andre@0: void MD5_Clone(MD5Context *dest, MD5Context *src) andre@0: { andre@0: memcpy(dest, src, sizeof *dest); andre@0: } andre@0: andre@0: void andre@0: MD5_TraceState(MD5Context *cx) andre@0: { andre@0: PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); andre@0: }