Example #1
0
File: md5.c Project: 4m1g0/Arduino
/**
 * Accepts an array of octets as the next portion of the message.
 */
EXP_FUNC void STDCALL MD5Update(MD5_CTX *ctx, const uint8_t * msg, int len)
{
    uint32_t x;
    int i, partLen;

    /* Compute number of bytes mod 64 */
    x = (uint32_t)((ctx->count[0] >> 3) & 0x3F);

    /* Update number of bits */
    if ((ctx->count[0] += ((uint32_t)len << 3)) < ((uint32_t)len << 3))
        ctx->count[1]++;
    ctx->count[1] += ((uint32_t)len >> 29);

    partLen = 64 - x;

    /* Transform as many times as possible.  */
    if (len >= partLen) 
    {
        memcpy(&ctx->buffer[x], msg, partLen);
        MD5Transform(ctx->state, ctx->buffer);

        for (i = partLen; i + 63 < len; i += 64)
            MD5Transform(ctx->state, &msg[i]);

        x = 0;
    }
    else
        i = 0;

    /* Buffer remaining input */
    memcpy(&ctx->buffer[x], &msg[i], len-i);
}
Example #2
0
/*
 * Update context to reflect the concatenation of another buffer full
 * of bytes.
 */
void
MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
{
	size_t have, need;

	/* Check how many bytes we already have and how many more we need. */
	have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
	need = MD5_BLOCK_LENGTH - have;

	/* Update bitcount */
	ctx->count += (u_int64_t)len << 3;

	if (len >= need) {
		if (have != 0) {
			memcpy(ctx->buffer + have, input, need);
			MD5Transform(ctx->state, ctx->buffer);
			input += need;
			len -= need;
			have = 0;
		}

		/* Process data in MD5_BLOCK_LENGTH-byte chunks. */
		while (len >= MD5_BLOCK_LENGTH) {
			MD5Transform(ctx->state, input);
			input += MD5_BLOCK_LENGTH;
			len -= MD5_BLOCK_LENGTH;
		}
	}

	/* Handle any remaining bytes of data. */
	if (len != 0)
		memcpy(ctx->buffer + have, input, len);
}
Example #3
0
File: md5c.c Project: kulhos/pip
/* 
	MD5 block update operation. Continues an MD5 message-digest
	operation, processing another message block, and updating the context.
*/
void MD5Update (MD5_CTX *context, BYTE *input, UINT inputLen)
{
  UINT i, index, partLen;

  /* Compute number of bytes mod 64 */
  index = (UINT)((context->count[0] >> 3) & 0x3F);

  /* Update number of bits */
  if ((context->count[0] += ((ULONG)inputLen << 3)) < ((ULONG)inputLen << 3))
	context->count[1]++;
  context->count[1] += ((ULONG)inputLen >> 29);

  partLen = 64 - index;

  /* Transform as many times as possible. */
  if (inputLen >= partLen) {
	memcpy((PUSTR)&context->buffer[index], (PUSTR)input, partLen);
	MD5Transform (context->state, context->buffer);

	for (i = partLen; i + 63 < inputLen; i += 64)
		MD5Transform (context->state, &input[i]);

	index = 0;
  }
  else
	i = 0;

  /* Buffer remaining input */
  memcpy((PUSTR)&context->buffer[index], (PUSTR)&input[i], inputLen-i);
}
Example #4
0
/* MD5 block update operation. Continues an MD5 message-digest
   operation, processing another message block, and updating the
   context.
   */
static kvoid MD5Update (MD5_CTX *context, const kuchar *input, kuint inputLen)
{
    kuint i, index, partLen;

    /* Compute number of bytes mod 64 */
    index = (kuint)((context->count[0] >> 3) & 0x3F);

    /* Update number of bits */
    if ((context->count[0] += ((UINT4)inputLen << 3))
            < ((UINT4)inputLen << 3))
        context->count[1]++;
    context->count[1] += ((UINT4)inputLen >> 29);

    partLen = 64 - index;

    /* Transform as many times as possible.
    */
    if (inputLen >= partLen) {
        MD5_memcpy
            ((POINTER)&context->buffer[index], (POINTER)input, partLen);
        MD5Transform (context->state, context->buffer);

        for (i = partLen; i + 63 < inputLen; i += 64)
            MD5Transform (context->state, &input[i]);

        index = 0;
    }
    else
        i = 0;

    /* Buffer remaining input */
    MD5_memcpy
        ((POINTER)&context->buffer[index], (POINTER)&input[i],
         inputLen-i);
}
Example #5
0
/* MD5 block update operation. Continues an MD5 message-digest operation,
 * processing another message block, and updating the context */
void MD5_Update(R_MD5_CTX *context, const ut8 *input, ut32 inputLen) {
	ut32 i;

	/* Compute number of bytes mod 64 */
	ut32 index = (ut32)((context->count[0] >> 3) & 0x3F);

	/* Update number of bits */
	if ((context->count[0] += ((ut32)inputLen << 3)) < ((ut32)inputLen << 3)) {
		context->count[1]++;
	}
	context->count[1] += ((ut32)inputLen >> 29);

	ut32 partLen = 64 - index;

	// Transform as many times as possible
	if (inputLen >= partLen) {
		memmove ((void*)&context->buffer[index], (void*)input, partLen);
		MD5Transform (context->state, context->buffer);
		for (i = partLen; i + 63 < inputLen; i += 64) {
			MD5Transform (context->state, &input[i]);
		}
		index = 0;
	} else {
		i = 0;
	}
	// remaining input
	memmove ((void*)&context->buffer[index], (void*)&input[i], inputLen - i);
}
Example #6
0
void random_stir(RandomState * state)
{
    word32 iv[4];
    unsigned int i;

    /* Start IV from last block of random pool. */
    iv[0] = GET_32BIT(state->state);
    iv[1] = GET_32BIT(state->state + 4);
    iv[2] = GET_32BIT(state->state + 8);
    iv[3] = GET_32BIT(state->state + 12);

    /* First CFB pass. */
    for (i = 0; i < RANDOM_STATE_BYTES; i += 16) {
        MD5Transform((md5_uint32 *)(char*)iv, state->stir_key);
        iv[0] ^= GET_32BIT(state->state + i);
        PUT_32BIT(state->state + i, iv[0]);
        iv[1] ^= GET_32BIT(state->state + i + 4);
        PUT_32BIT(state->state + i + 4, iv[1]);
        iv[2] ^= GET_32BIT(state->state + i + 8);
        PUT_32BIT(state->state + i + 8, iv[2]);
        iv[3] ^= GET_32BIT(state->state + i + 12);
        PUT_32BIT(state->state + i + 12, iv[3]);
    }

    /* Get new key. */
    memcpy(state->stir_key, state->state, sizeof(state->stir_key));

    /* Second CFB pass. */
    for (i = 0; i < RANDOM_STATE_BYTES; i += 16) {
        MD5Transform((md5_uint32 *)(char*)iv, state->stir_key);
        iv[0] ^= GET_32BIT(state->state + i);
        PUT_32BIT(state->state + i, iv[0]);
        iv[1] ^= GET_32BIT(state->state + i + 4);
        PUT_32BIT(state->state + i + 4, iv[1]);
        iv[2] ^= GET_32BIT(state->state + i + 8);
        PUT_32BIT(state->state + i + 8, iv[2]);
        iv[3] ^= GET_32BIT(state->state + i + 12);
        PUT_32BIT(state->state + i + 12, iv[3]);
    }

    memset(iv, 0, sizeof(iv));

    state->add_position = 0;

    /* Some data in the beginning is not returned to aboid giving an observer
       complete knowledge of the contents of our random pool. */
    state->next_available_byte = sizeof(state->stir_key);
}
Example #7
0
/** Generate a pseudo-random number.
 * This uses the #localkey structure plus current time as input to
 * MD5, feeding most of the MD5 output back to #localkey and using one
 * output words as the pseudo-random output.
 * @return A 32-bit pseudo-random number.
 */
unsigned int ircrandom(void)
{
  struct timeval tv;
  char usec[3];

  /* Add some randomness to the pool. */
  gettimeofday(&tv, 0);
  usec[0] = tv.tv_usec;
  usec[1] = tv.tv_usec >> 8;
  usec[2] = tv.tv_usec >> 16;
  random_add_entropy(usec, 3);

  /* Perform MD5 step. */
  localkey.buf[0] = 0x67452301;
  localkey.buf[1] = 0xefcdab89;
  localkey.buf[2] = 0x98badcfe;
  localkey.buf[3] = 0x10325476;
  MD5Transform(localkey.buf, (uint32*)localkey.in);

  /* Feed back 12 bytes of hash value into randomness pool. */
  random_add_entropy((char*)localkey.buf, 12);

  /* Return the final word of hash, which should not provide any
   * useful insight into current pool contents. */
  return localkey.buf[3];
}
Example #8
0
/*
 * Destroys already-used random numbers.  Ensures no sensitive data
 * remains in memory that can be recovered later.  This is also
 * called to "stir in" newly acquired environmental noise bits before
 * removing any random bytes.
 *
 * The transformation is carried out by "encrypting" the data in CFB
 * mode with MD5 as the block cipher.  Then, to make certain the stirring
 * operation is strictly one-way, we destroy the key, getting 64 bytes
 * from the beginning of the pool and using them to reinitialize the
 * key.  These bytes are not returned by randPoolGetBytes().
 *
 * The key for the stirring operation is the XOR of some bytes from the
 * previous pool contents (not provably necessary, but it produces uniformly
 * distributed keys, which "feels better") and the newly added raw noise,
 * which will have a profound effect on every bit in the pool.
 *
 * To make this useful for pseudo-random (that is, repeatable) operations,
 * the MD5 transformation is always done with a consistent byte order.
 * MD5Transform itself works with 32-bit words, not bytes, so the pool,
 * usually an array of bytes, is transformed into an array of 32-bit words,
 * taking each group of 4 bytes in big-endian order.  At the end of the
 * stirring, the transformation is reversed.
 */
void
randPoolStir(void)
{
	int i;
	word32 iv[4];

	/* Convert to word32s for stirring operation */
	byteSwap(randPool, RANDPOOLWORDS);
	byteSwap(randKey, RANDKEYWORDS);

	/* Start IV from last block of randPool */
	memcpy(iv, randPool+RANDPOOLWORDS-4, sizeof(iv));

	/* CFB pass */
	for (i = 0; i < RANDPOOLWORDS; i += 4) {
		MD5Transform(iv, randKey);
		iv[0] = randPool[i  ] ^= iv[0];
		iv[1] = randPool[i+1] ^= iv[1];
		iv[2] = randPool[i+2] ^= iv[2];
		iv[3] = randPool[i+3] ^= iv[3];
	}

	/* Wipe iv from memory */
	iv[3] = iv[2] = iv[1] = iv[0] = 0;

	/* Convert randPool back to bytes for further use */
	byteSwap(randPool, RANDPOOLWORDS);

	/* Get new key */
	memcpy(randKey, randPool, sizeof(randKey));

	/* Set up pointers for future addition or removal of random bytes */
	randKeyAddPos = 0;
	randPoolGetPos = sizeof(randKey);
}
Example #9
0
//更新函数
void CMD5::MD5Update (unsigned char * input, unsigned int inputLen)
{
	unsigned int i,index,partLen;
	index=(unsigned int)((this->count[0]>>3)&0x3F);
	if ((count[0]+=((unsigned long int)inputLen<<3))<((unsigned long int)inputLen<<3)) count[1]++;
	count[1]+=((unsigned long int)inputLen>>29);
	partLen=64-index;
	if (inputLen>=partLen) 
	{
		MD5_memcpy((unsigned char*)&buffer[index],(unsigned char *)input,partLen);
		MD5Transform(state,buffer);
		for (i=partLen;i+63<inputLen;i+=64) MD5Transform(state,&input[i]);
		index=0;
	}
	else i=0;
	MD5_memcpy((unsigned char*)&buffer[index],(unsigned char *)&input[i],inputLen-i);
	return;
}
Example #10
0
//更新函数
VOID CMD5Aide::MD5Update (BYTE * pcbInput, UINT inputLen)
{
	UINT i,nIndex,nPartLen;
	nIndex=(UINT)((this->m_lCount[0]>>3)&0x3F);
	if ((m_lCount[0]+=((ULONG)inputLen<<3))<((ULONG)inputLen<<3)) m_lCount[1]++;
	m_lCount[1]+=((ULONG)inputLen>>29);
	nPartLen=64-nIndex;
	if (inputLen>=nPartLen) 
	{
		MD5Memcpy((BYTE*)&m_cbBuffer[nIndex],(BYTE *)pcbInput,nPartLen);
		MD5Transform(m_lState,m_cbBuffer);
		for (i=nPartLen;i+63<inputLen;i+=64) MD5Transform(m_lState,&pcbInput[i]);
		nIndex=0;
	}
	else i=0;
	MD5Memcpy((BYTE*)&m_cbBuffer[nIndex],(BYTE *)&pcbInput[i],inputLen-i);

	return;
}
Example #11
0
/***************************************************************************
*
* MD5Update - MD5 block update operation.
*
* Continues an MD5 message-digest operation by processing another
* message block and updating the context.
*
* RETURNS: N/A
*/
void MD5Update
    (
    MD5_CTX_T * context,
    UCHAR *     input,
    UINT        inputLen
    )
    {
    UINT i, index, partLen;

    /* Compute number of bytes mod 64 */
    index = (UINT)((context->count[0] >> 3) & 0x3F);

    /* Update number of bits */
    if ((context->count[0] += ((ULONG)inputLen << 3)) < ((ULONG)inputLen << 3))
        {
        context->count[1]++;
        }

    context->count[1] += ((ULONG)inputLen >> 29);

    partLen = 64 - index;

    /* Transform as many times as possible */
    if (inputLen >= partLen)
        {
        memcpy((UCHAR *)&context->buffer[index], (UCHAR *)input, partLen);
        MD5Transform((ULONG *)context->state, (UCHAR *)context->buffer);
        for (i = partLen; (i + 63) < inputLen; i += 64)
            {
            MD5Transform((ULONG *)context->state, (UCHAR *)&input[i]);
            }
        index = 0;
        }
    else
        {
        i = 0;
        }

    /* Buffer remaining input */
    memcpy((UCHAR *)&context->buffer[index], (UCHAR *)&input[i], inputLen - i);
    }
Example #12
0
/* MD5 block update operation. Continues an MD5 message-digest
  operation, processing another message block, and updating the
  context.
 */
void MD5Update (MD5_CTX *context, const rho::u8 *input, rho::u32 inputLen)
//context;                              /* context */
//input;                                /* input block */
//inputLen;                             /* length of input block */
{
    rho::u32 i, index, partLen;

  /* Compute number of bytes mod 64 */
  index = (rho::u32)((context->count[0] >> 3) & 0x3F);

  /* Update number of bits */
  if ((context->count[0] += ((UINT4)inputLen << 3))
   < ((UINT4)inputLen << 3))
 context->count[1]++;
  context->count[1] += ((UINT4)inputLen >> 29);

  partLen = 64 - index;

  /* Transform as many times as possible.
*/
  if (inputLen >= partLen) {
 MD5_memcpy
   ((POINTER)&context->buffer[index], input, partLen);
 MD5Transform (context->state, context->buffer);

 for (i = partLen; i + 63 < inputLen; i += 64)
   MD5Transform (context->state, &input[i]);

 index = 0;
  }
  else
 i = 0;

  /* Buffer remaining input */
  MD5_memcpy
 ((POINTER)&context->buffer[index], &input[i],
  inputLen-i);
}
Example #13
0
/**
  Copy data segment into the M field of MD5_CTX structure for later transform.
  If the length of data segment is larger than 64 bytes, then does the transform
  immediately and the generated Md5 code is stored in the States field of MD5_CTX
  data struct for later accumulation. 
  All of Md5 code generated for the sequential 64-bytes data segaments are be
  accumulated in MD5Final() function.

  @param[in, out]  Md5Ctx  The data structure of storing the original data
                           segment and the final result.
  @param[in]       Data    The data wanted to be transformed.
  @param[in]       DataLen The length of data.
**/
VOID
MD5UpdateBlock (
  IN OUT MD5_CTX  *Md5Ctx,
  IN CONST UINT8  *Data,
  IN       UINTN  DataLen
  )
{
  UINTN Limit;

  for (Limit = 64 - Md5Ctx->Count; DataLen >= 64 - Md5Ctx->Count; Limit = 64) {
    CopyMem (Md5Ctx->M + Md5Ctx->Count, (VOID *)Data, Limit);
    MD5Transform (Md5Ctx);
    
    Md5Ctx->Count = 0;
    Data         += Limit;
    DataLen      -= Limit;
  }

  CopyMem (Md5Ctx->M + Md5Ctx->Count, (VOID *)Data, DataLen);
  Md5Ctx->Count += DataLen;
}
Example #14
0
File: Md5.c Project: Kohrara/edk
STATIC
VOID
MD5UpdateBlock (
  IN MD5_CTX      *Md5Ctx,
  IN CONST UINT8  *Data,
  IN       UINTN  DataLen
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  Md5Ctx  - GC_TODO: add argument description
  Data    - GC_TODO: add argument description
  DataLen - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  UINTN Limit;

  for (Limit = 64 - Md5Ctx->Count; DataLen >= 64 - Md5Ctx->Count; Limit = 64) {
    EfiCopyMem (Md5Ctx->M + Md5Ctx->Count, (VOID *)Data, Limit);
    MD5Transform (Md5Ctx);
    
    Md5Ctx->Count = 0;
    Data         += Limit;
    DataLen      -= Limit;
  }

  EfiCopyMem (Md5Ctx->M + Md5Ctx->Count, (VOID *)Data, DataLen);
  Md5Ctx->Count += DataLen;
}
Example #15
0
EXPORT(sqInt) primitiveMD5Transform(void) {
    sqInt hashOop;
    unsigned int * hash;
    sqInt bufOop;
    unsigned int * buffer;

    if (!((interpreterProxy->methodArgumentCount()) == 2)) {
        return interpreterProxy->primitiveFail();
    }
    hashOop = interpreterProxy->stackObjectValue(0);
    if (!((interpreterProxy->isWords(hashOop)) && ((interpreterProxy->slotSizeOf(hashOop)) == 4))) {
        return interpreterProxy->primitiveFail();
    }
    hash = interpreterProxy->firstIndexableField(hashOop);
    bufOop = interpreterProxy->stackObjectValue(1);
    if (!((interpreterProxy->isWords(bufOop)) && ((interpreterProxy->slotSizeOf(bufOop)) == 16))) {
        return interpreterProxy->primitiveFail();
    }
    buffer = interpreterProxy->firstIndexableField(bufOop);
    MD5Transform(hash, buffer);
    interpreterProxy->pop((interpreterProxy->methodArgumentCount()) + 1);
    interpreterProxy->push(bufOop);
}
Example #16
0
/*
 * Destroys already-used random numbers.  Ensures no sensitive data
 * remains in memory that can be recovered later.  This is also
 * called to "stir in" newly acquired environmental noise bits before
 * removing any random bytes.
 *
 * The transformation is carried out by "encrypting" the data in CFB
 * mode with MD5 as the block cipher.  Then, to make certain the stirring
 * operation is strictly one-way, we destroy the key, getting 64 bytes
 * from the beginning of the pool and using them to reinitialize the
 * key.  These bytes are not returned by randPoolGetBytes().
 *
 * The stirring operation is done twice, to ensure that each bit in the
 * pool depends on each bit of entropy XORed in after each call to
 * randPoolStir().
 *
 * To make this useful for pseudo-random (that is, repeatable) operations,
 * the MD5 transformation is always done with a consistent byte order.
 * MD5Transform itself works with 32-bit words, not bytes, so the pool,
 * usually an array of bytes, is transformed into an array of 32-bit words,
 * taking each group of 4 bytes in big-endian order.  At the end of the
 * stirring, the transformation is reversed.
 */
void
randPoolStir(void)
{
	int i;
	byte *p;
	word32 t;
	word32 iv[4];
	static word32 randPoolKey[16] = {0};

	/* Convert to word32s for stirring operation */
	p = (byte *)randPool;
	for (i = 0; i < RANDPOOLWORDS; i++) {
		t = (word32)((unsigned)p[3]<<8 | p[2]) << 16 |
		             (unsigned)p[1]<<8 | p[0];
		randPool[i] = t;
		p += 4;
	}

	/* Start IV from last block of randPool */
	memcpy(iv, randPool+RANDPOOLWORDS-4, sizeof(iv));

	/* First CFB pass */
	for (i = 0; i < RANDPOOLWORDS; i += 4) {
		MD5Transform(iv, randPoolKey);
		iv[0] = randPool[i  ] ^= iv[0];
		iv[1] = randPool[i+1] ^= iv[1];
		iv[2] = randPool[i+2] ^= iv[2];
		iv[3] = randPool[i+3] ^= iv[3];
	}

	/* Get new key */
	memcpy(randPoolKey, randPool, sizeof(randPoolKey));

	/* Second CFB pass */
	for (i = 0; i < RANDPOOLWORDS; i += 4) {
		MD5Transform(iv, randPoolKey);
		iv[0] = randPool[i  ] ^= iv[0];
		iv[1] = randPool[i+1] ^= iv[1];
		iv[2] = randPool[i+2] ^= iv[2];
		iv[3] = randPool[i+3] ^= iv[3];
	}

	/* Get new key */
	memcpy(randPoolKey, randPool, sizeof(randPoolKey));

	/* Wipe iv from memory */
	memset(iv, 0, sizeof(iv));

	/* Convert randPool back to bytes for further use */
	p = (byte *)randPool;
	for (i = 0; i < RANDPOOLWORDS; i++) {
		t = randPool[i];
		p[0] = t>>24;
		p[1] = t>>16;
		p[2] = t>>8;
		p[3] = t;
		p += 4;
	}

	/* Set up pointers for future addition or removal of random bytes */
	randPoolAddPos = 0;
	randPoolGetPos = sizeof(randPoolKey);
}