2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A/*
2N/A * lib/crypto/md4/md4.c
2N/A */
2N/A
2N/A/*
2N/A **********************************************************************
2N/A ** md4.c **
2N/A ** RSA Data Security, Inc. MD4 Message Digest Algorithm **
2N/A ** Created: 2/17/90 RLR **
2N/A ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
2N/A **********************************************************************
2N/A */
2N/A
2N/A/*
2N/A **********************************************************************
2N/A ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
2N/A ** **
2N/A ** License to copy and use this software is granted provided that **
2N/A ** it is identified as the "RSA Data Security, Inc. MD4 Message **
2N/A ** Digest Algorithm" in all material mentioning or referencing this **
2N/A ** software or this function. **
2N/A ** **
2N/A ** License is also granted to make and use derivative works **
2N/A ** provided that such works are identified as "derived from the RSA **
2N/A ** Data Security, Inc. MD4 Message Digest Algorithm" in all **
2N/A ** material mentioning or referencing the derived work. **
2N/A ** **
2N/A ** RSA Data Security, Inc. makes no representations concerning **
2N/A ** either the merchantability of this software or the suitability **
2N/A ** of this software for any particular purpose. It is provided "as **
2N/A ** is" without express or implied warranty of any kind. **
2N/A ** **
2N/A ** These notices must be retained in any copies of any part of this **
2N/A ** documentation and/or software. **
2N/A **********************************************************************
2N/A */
2N/A
2N/A#include "k5-int.h"
2N/A#include "rsa-md4.h"
2N/A
2N/A/* forward declaration */
2N/Astatic void Transform (krb5_ui_4 *, krb5_ui_4 *);
2N/A
2N/Astatic const unsigned char PADDING[64] = {
2N/A 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2N/A};
2N/A
2N/A/* F, G and H are basic MD4 functions: selection, majority, parity */
2N/A#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
2N/A#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
2N/A#define H(x, y, z) ((x) ^ (y) ^ (z))
2N/A
2N/A/* ROTATE_LEFT rotates x left n bits */
2N/A#define ROTATE_LEFT(x, n) ((((x) << (n)) & 0xffffffff) | ((x) >> (32-(n))))
2N/A
2N/A/* FF, GG and HH are MD4 transformations for rounds 1, 2 and 3 */
2N/A/* Rotation is separate from addition to prevent recomputation */
2N/A#define FF(a, b, c, d, x, s) \
2N/A {(a) += F ((b), (c), (d)) + (x); \
2N/A (a) &= 0xffffffff; \
2N/A (a) = ROTATE_LEFT ((a), (s));}
2N/A#define GG(a, b, c, d, x, s) \
2N/A {(a) += G ((b), (c), (d)) + (x) + 013240474631UL; \
2N/A (a) &= 0xffffffff; \
2N/A (a) = ROTATE_LEFT ((a), (s));}
2N/A#define HH(a, b, c, d, x, s) \
2N/A {(a) += H ((b), (c), (d)) + (x) + 015666365641UL; \
2N/A (a) &= 0xffffffff; \
2N/A (a) = ROTATE_LEFT ((a), (s));}
2N/A
2N/Avoid
2N/Akrb5_MD4Init (krb5_MD4_CTX *mdContext)
2N/A{
2N/A mdContext->i[0] = mdContext->i[1] = (krb5_ui_4)0;
2N/A
2N/A /* Load magic initialization constants.
2N/A */
2N/A mdContext->buf[0] = 0x67452301UL;
2N/A mdContext->buf[1] = 0xefcdab89UL;
2N/A mdContext->buf[2] = 0x98badcfeUL;
2N/A mdContext->buf[3] = 0x10325476UL;
2N/A}
2N/A
2N/Avoid
2N/Akrb5_MD4Update (krb5_MD4_CTX *mdContext, const unsigned char *inBuf, unsigned int inLen)
2N/A{
2N/A krb5_ui_4 in[16];
2N/A int mdi;
2N/A unsigned int i, ii;
2N/A
2N/A /* compute number of bytes mod 64 */
2N/A mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
2N/A
2N/A /* update number of bits */
2N/A if ((mdContext->i[0] + ((krb5_ui_4)inLen << 3)) < mdContext->i[0])
2N/A mdContext->i[1]++;
2N/A mdContext->i[0] += ((krb5_ui_4)inLen << 3);
2N/A mdContext->i[1] += ((krb5_ui_4)inLen >> 29);
2N/A
2N/A while (inLen--) {
2N/A /* add new character to buffer, increment mdi */
2N/A mdContext->in[mdi++] = *inBuf++;
2N/A
2N/A /* transform if necessary */
2N/A if (mdi == 0x40) {
2N/A for (i = 0, ii = 0; i < 16; i++, ii += 4)
2N/A in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) |
2N/A (((krb5_ui_4)mdContext->in[ii+2]) << 16) |
2N/A (((krb5_ui_4)mdContext->in[ii+1]) << 8) |
2N/A ((krb5_ui_4)mdContext->in[ii]);
2N/A Transform (mdContext->buf, in);
2N/A mdi = 0;
2N/A }
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Akrb5_MD4Final (krb5_MD4_CTX *mdContext)
2N/A{
2N/A krb5_ui_4 in[16];
2N/A int mdi;
2N/A unsigned int i, ii;
2N/A unsigned int padLen;
2N/A
2N/A /* save number of bits */
2N/A in[14] = mdContext->i[0];
2N/A in[15] = mdContext->i[1];
2N/A
2N/A /* compute number of bytes mod 64 */
2N/A mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
2N/A
2N/A /* pad out to 56 mod 64 */
2N/A padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
2N/A krb5_MD4Update (mdContext, PADDING, padLen);
2N/A
2N/A /* append length in bits and transform */
2N/A for (i = 0, ii = 0; i < 14; i++, ii += 4)
2N/A in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) |
2N/A (((krb5_ui_4)mdContext->in[ii+2]) << 16) |
2N/A (((krb5_ui_4)mdContext->in[ii+1]) << 8) |
2N/A ((krb5_ui_4)mdContext->in[ii]);
2N/A Transform (mdContext->buf, in);
2N/A
2N/A
2N/A /* store buffer in digest */
2N/A for (i = 0, ii = 0; i < 4; i++, ii += 4) {
2N/A mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
2N/A mdContext->digest[ii+1] =
2N/A (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
2N/A mdContext->digest[ii+2] =
2N/A (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
2N/A mdContext->digest[ii+3] =
2N/A (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
2N/A }
2N/A}
2N/A
2N/A/* Basic MD4 step. Transform buf based on in.
2N/A */
2N/Astatic void Transform (krb5_ui_4 *buf, krb5_ui_4 *in)
2N/A{
2N/A register krb5_ui_4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
2N/A
2N/A#ifdef CONFIG_SMALL
2N/A int i;
2N/A#define ROTATE { krb5_ui_4 temp; temp = d, d = c, c = b, b = a, a = temp; }
2N/A for (i = 0; i < 16; i++) {
2N/A static const unsigned char round1consts[] = { 3, 7, 11, 19, };
2N/A FF (a, b, c, d, in[i], round1consts[i%4]); ROTATE;
2N/A }
2N/A for (i = 0; i < 16; i++) {
2N/A static const unsigned char round2indices[] = {
2N/A 0,4,8,12,1,5,9,13,2,6,10,14,3,7,11,15
2N/A };
2N/A static const unsigned char round2consts[] = { 3, 5, 9, 13 };
2N/A GG (a, b, c, d, in[round2indices[i]], round2consts[i%4]); ROTATE;
2N/A }
2N/A for (i = 0; i < 16; i++) {
2N/A static const unsigned char round3indices[] = {
2N/A 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
2N/A };
2N/A static const unsigned char round3consts[] = { 3, 9, 11, 15 };
2N/A HH (a, b, c, d, in[round3indices[i]], round3consts[i%4]); ROTATE;
2N/A }
2N/A#else
2N/A /* Round 1 */
2N/A FF (a, b, c, d, in[ 0], 3);
2N/A FF (d, a, b, c, in[ 1], 7);
2N/A FF (c, d, a, b, in[ 2], 11);
2N/A FF (b, c, d, a, in[ 3], 19);
2N/A FF (a, b, c, d, in[ 4], 3);
2N/A FF (d, a, b, c, in[ 5], 7);
2N/A FF (c, d, a, b, in[ 6], 11);
2N/A FF (b, c, d, a, in[ 7], 19);
2N/A FF (a, b, c, d, in[ 8], 3);
2N/A FF (d, a, b, c, in[ 9], 7);
2N/A FF (c, d, a, b, in[10], 11);
2N/A FF (b, c, d, a, in[11], 19);
2N/A FF (a, b, c, d, in[12], 3);
2N/A FF (d, a, b, c, in[13], 7);
2N/A FF (c, d, a, b, in[14], 11);
2N/A FF (b, c, d, a, in[15], 19);
2N/A
2N/A /* Round 2 */
2N/A GG (a, b, c, d, in[ 0], 3);
2N/A GG (d, a, b, c, in[ 4], 5);
2N/A GG (c, d, a, b, in[ 8], 9);
2N/A GG (b, c, d, a, in[12], 13);
2N/A GG (a, b, c, d, in[ 1], 3);
2N/A GG (d, a, b, c, in[ 5], 5);
2N/A GG (c, d, a, b, in[ 9], 9);
2N/A GG (b, c, d, a, in[13], 13);
2N/A GG (a, b, c, d, in[ 2], 3);
2N/A GG (d, a, b, c, in[ 6], 5);
2N/A GG (c, d, a, b, in[10], 9);
2N/A GG (b, c, d, a, in[14], 13);
2N/A GG (a, b, c, d, in[ 3], 3);
2N/A GG (d, a, b, c, in[ 7], 5);
2N/A GG (c, d, a, b, in[11], 9);
2N/A GG (b, c, d, a, in[15], 13);
2N/A
2N/A /* Round 3 */
2N/A HH (a, b, c, d, in[ 0], 3);
2N/A HH (d, a, b, c, in[ 8], 9);
2N/A HH (c, d, a, b, in[ 4], 11);
2N/A HH (b, c, d, a, in[12], 15);
2N/A HH (a, b, c, d, in[ 2], 3);
2N/A HH (d, a, b, c, in[10], 9);
2N/A HH (c, d, a, b, in[ 6], 11);
2N/A HH (b, c, d, a, in[14], 15);
2N/A HH (a, b, c, d, in[ 1], 3);
2N/A HH (d, a, b, c, in[ 9], 9);
2N/A HH (c, d, a, b, in[ 5], 11);
2N/A HH (b, c, d, a, in[13], 15);
2N/A HH (a, b, c, d, in[ 3], 3);
2N/A HH (d, a, b, c, in[11], 9);
2N/A HH (c, d, a, b, in[ 7], 11);
2N/A HH (b, c, d, a, in[15], 15);
2N/A#endif
2N/A
2N/A buf[0] += a;
2N/A buf[1] += b;
2N/A buf[2] += c;
2N/A buf[3] += d;
2N/A}
2N/A
2N/A/*
2N/A **********************************************************************
2N/A ** End of md4.c **
2N/A ******************************* (cut) ********************************
2N/A */